Skip to content

Commit

Permalink
remove collection limit for rxdb
Browse files Browse the repository at this point in the history
  • Loading branch information
kilbot committed Feb 16, 2025
1 parent 4ec59f6 commit 61fe685
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 36 deletions.
74 changes: 64 additions & 10 deletions apps/main/polyfills.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,67 @@
import * as Crypto from 'expo-crypto';

// Polyfill for global.crypto if it doesn't exist.
if (typeof global.crypto === 'undefined') {
global.crypto = {
getRandomValues: <T extends ArrayBufferView | null>(buffer: T): T => {
if (!buffer) return buffer;
const ints = new Uint8Array(buffer.byteLength);
Crypto.getRandomValues(ints);
new Uint8Array(buffer.buffer).set(ints);
return buffer;
},
} as Crypto;
}
global.crypto = {
// Polyfill getRandomValues
getRandomValues: <T extends ArrayBufferView | null>(buffer: T): T => {
if (!buffer) return buffer;
const ints = new Uint8Array(buffer.byteLength);
Crypto.getRandomValues(ints);
new Uint8Array(buffer.buffer).set(ints);
return buffer;
},
// Polyfill subtle if missing
subtle: {
async digest(algorithm: string, data: BufferSource): Promise<ArrayBuffer> {
// Convert BufferSource to ArrayBuffer
let dataBuffer: ArrayBuffer;
if (data instanceof ArrayBuffer) {
dataBuffer = data;
} else if (ArrayBuffer.isView(data)) {
dataBuffer = data.buffer;
} else {
throw new Error('Unsupported data type for digest');
}

// Convert ArrayBuffer to a UTF-8 string.
// WARNING: This assumes that the input is valid UTF-8.
const decoded = new TextDecoder().decode(dataBuffer);

// Calculate digest as a hex string using Expo Crypto.
const digestHex = await Crypto.digestStringAsync(algorithm, decoded);

// Convert hex string back to ArrayBuffer
const buffer = new ArrayBuffer(digestHex.length / 2);
const view = new Uint8Array(buffer);
for (let i = 0; i < digestHex.length; i += 2) {
view[i / 2] = parseInt(digestHex.substr(i, 2), 16);
}
return buffer;
},
},
};
} else if (typeof global.crypto.subtle === 'undefined') {
// If crypto exists but crypto.subtle is missing, add the subtle polyfill.
global.crypto.subtle = {
async digest(algorithm: string, data: BufferSource): Promise<ArrayBuffer> {
let dataBuffer: ArrayBuffer;
if (data instanceof ArrayBuffer) {
dataBuffer = data;
} else if (ArrayBuffer.isView(data)) {
dataBuffer = data.buffer;
} else {
throw new Error('Unsupported data type for digest');
}

const decoded = new TextDecoder().decode(dataBuffer);
const digestHex = await Crypto.digestStringAsync(algorithm, decoded);
const buffer = new ArrayBuffer(digestHex.length / 2);
const view = new Uint8Array(buffer);
for (let i = 0; i < digestHex.length; i += 2) {
view[i / 2] = parseInt(digestHex.substr(i, 2), 16);
}
return buffer;
},
};
}
7 changes: 6 additions & 1 deletion packages/database/src/create-db.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import { createRxDatabase, removeRxDatabase } from 'rxdb';
import { disableVersionCheck } from 'rxdb-premium/plugins/shared';
import { setPremiumFlag } from 'rxdb-premium/plugins/shared';

import log from '@wcpos/utils/logger';

import config from './adapter';
import { fastConfig } from './fast-adapter';

import './plugins';
disableVersionCheck();
setPremiumFlag();

import './plugins';

/**
* creates the generic database
Expand All @@ -19,6 +22,7 @@ export async function createDB<DBCollections>(name: string) {
...config,
allowSlowCount: true,
// password: 'posInstanceId',
ignoreDuplicate: !!__DEV__,
multiInstance: false,
});

Expand All @@ -40,6 +44,7 @@ export async function createMemorySyncedDB<DBCollections>(name: string) {
...fastConfig,
allowSlowCount: true,
// password: 'posInstanceId',
ignoreDuplicate: !!__DEV__,
multiInstance: false,
});

Expand Down
15 changes: 10 additions & 5 deletions packages/database/src/fast-adapter/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,14 @@ import {
getRxStorageSQLite,
getSQLiteBasicsExpoSQLiteAsync,
} from 'rxdb-premium/plugins/storage-sqlite';
import { wrappedValidateZSchemaStorage } from 'rxdb/plugins/validate-z-schema';

export const fastConfig = {
storage: getRxStorageSQLite({
sqliteBasics: getSQLiteBasicsExpoSQLiteAsync(SQLite.openDatabaseAsync),
}),
};
const storage = getRxStorageSQLite({
sqliteBasics: getSQLiteBasicsExpoSQLiteAsync(SQLite.openDatabaseAsync),
});

const devStorage = wrappedValidateZSchemaStorage({
storage,
});

export const fastConfig = { storage: __DEV__ ? devStorage : storage };
45 changes: 25 additions & 20 deletions packages/database/src/fast-adapter/index.web.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,27 @@
import { getRxStorageWorker } from 'rxdb-premium/plugins/storage-worker';
import { wrappedValidateZSchemaStorage } from 'rxdb/plugins/validate-z-schema';

export const fastConfig = {
storage: getRxStorageWorker({
/**
* Contains any value that can be used as parameter
* to the Worker constructor of thread.js
* Most likely you want to put the path to the worker.js file in here.
*
* @link https://developer.mozilla.org/en-US/docs/Web/API/Worker/Worker
*/
workerInput: globalThis.idbWorker,
/**
* (Optional) options
* for the worker.
*/
// workerOptions: {
// type: 'module',
// credentials: 'omit',
// },
}),
};
const storage = getRxStorageWorker({
/**
* Contains any value that can be used as parameter
* to the Worker constructor of thread.js
* Most likely you want to put the path to the worker.js file in here.
*
* @link https://developer.mozilla.org/en-US/docs/Web/API/Worker/Worker
*/
workerInput: globalThis.idbWorker,
/**
* (Optional) options
* for the worker.
*/
// workerOptions: {
// type: 'module',
// credentials: 'omit',
// },
});

const devStorage = wrappedValidateZSchemaStorage({
storage,
});

export const fastConfig = { storage: __DEV__ ? devStorage : storage };

0 comments on commit 61fe685

Please sign in to comment.