From 6d04a91fee21291cd176814afe803d0ed827f03f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Francisco=20Javier=20Ribo=CC=81=20Labrador?= Date: Tue, 19 Dec 2023 17:54:28 -0500 Subject: [PATCH] =?UTF-8?q?fix:=20Allow=20multiple=20independent=20instanc?= =?UTF-8?q?es=20of=20levelDB.=20Signed-off-by:=20Francisco=20Javier=20Rib?= =?UTF-8?q?=C3=B3=20Labrador=20?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .vscode/launch.json | 17 +++--------- packages/leveldb/.gitignore | 2 ++ packages/leveldb/src/index.ts | 18 +++++++++---- packages/leveldb/tests/init.test.ts | 40 +++++++++++++++++++++++++++++ 4 files changed, 58 insertions(+), 19 deletions(-) create mode 100644 packages/leveldb/.gitignore diff --git a/.vscode/launch.json b/.vscode/launch.json index ba0f5965..e4f7ea1b 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -47,21 +47,10 @@ "type": "node" }, { - "cwd": "${workspaceFolder}/packages/database", - "type": "node", + "command": "npm run test", + "name": "Test2", "request": "launch", - "console": "integratedTerminal", - "name": "TEST", - "program": "${workspaceRoot}/node_modules/.bin/vitest", - "args": [ - "--pool", - "threads", - "--poolOptions.threads.singleThread", - ], - "skipFiles": [ - "${workspaceRoot}/../../node_modules/**/*", - "/**/*" - ] + "type": "node-terminal" } ] } \ No newline at end of file diff --git a/packages/leveldb/.gitignore b/packages/leveldb/.gitignore new file mode 100644 index 00000000..531dde09 --- /dev/null +++ b/packages/leveldb/.gitignore @@ -0,0 +1,2 @@ +db* +prism-db \ No newline at end of file diff --git a/packages/leveldb/src/index.ts b/packages/leveldb/src/index.ts index 0f26a36c..bafea59a 100644 --- a/packages/leveldb/src/index.ts +++ b/packages/leveldb/src/index.ts @@ -33,7 +33,6 @@ export type * from './leveldb/types'; export type { MangoQuerySelectorAndIndex, CRDTSchemaOptions, RxQueryPlanKey, PrimaryKey, StringKeys, TopLevelProperty, MangoQuerySortDirection } from "rxdb/dist/types/types"; export type { DefaultPreparedQuery, RxJsonSchema, FilledMangoQuery, CompressionMode, RxQueryPlan, MangoQuery, MangoQueryNoLimit, MangoQuerySortPart } from 'rxdb' export type { Level } from 'level' -let internalInstance: LevelDBInternal export const RX_STORAGE_NAME_LEVELDB = 'leveldb'; async function preloadData(constructorProps: LevelDBInternalConstructor) { @@ -46,6 +45,8 @@ async function preloadData(constructorProps: LevelDBInternalConstruct } } +let internalInstance: Map> = new Map() + function getRxStorageLevel(settings: LevelDBSettings): RxStorageLevelDBType { const instance: RxStorageLevelDBType = { name: RX_STORAGE_NAME_LEVELDB, @@ -64,10 +65,17 @@ function getRxStorageLevel(settings: LevelDBSettings): RxStorageLevel schema: params.schema, }; - if (!internalInstance) { - internalInstance = await preloadData(levelDBConstructorProps); + const databasePath = "level" in levelDBConstructorProps ? + levelDBConstructorProps.level.path : + levelDBConstructorProps.dbPath; + + const existingInstance = internalInstance.get(databasePath); + + if (!existingInstance) { + internalInstance.set(databasePath, await preloadData(levelDBConstructorProps)) } else { - internalInstance.refCount++ + existingInstance.refCount++; + internalInstance.set(databasePath, existingInstance) } const rxStorageInstance = new RxStorageIntanceLevelDB( @@ -75,7 +83,7 @@ function getRxStorageLevel(settings: LevelDBSettings): RxStorageLevel params.databaseName, params.collectionName, params.schema, - internalInstance, + internalInstance.get(databasePath)!, settings ) diff --git a/packages/leveldb/tests/init.test.ts b/packages/leveldb/tests/init.test.ts index 1b6300f5..8cdace05 100644 --- a/packages/leveldb/tests/init.test.ts +++ b/packages/leveldb/tests/init.test.ts @@ -45,4 +45,44 @@ describe("LevelDb init", () => { // await db.close() }) + + it('should be able to instanciate multiple databases in the same thread', async ({ expect }) => { + + const db = await Database.createEncrypted( + { + name: databaseName, + encryptionKey: defaultPassword, + storage: createLevelDBStorage({ + dbPath: "./db" + }), + } + ); + + await db.storeLinkSecret("first", "first") + + const db2 = await Database.createEncrypted( + { + name: databaseName, + encryptionKey: defaultPassword, + storage: createLevelDBStorage({ + dbPath: "./db2" + }), + } + ); + + await db2.storeLinkSecret("second", "second") + + const firstLinkSecret = await db.getLinkSecret(); + + const secondLinkSecret = await db2.getLinkSecret(); + + expect(firstLinkSecret).toBe("first") + expect(secondLinkSecret).toBe("second") + + await db.clear() + await db2.clear() + + + + }) })