From 4233bb0818c70389a31ee9859b1f3de82e249e2f Mon Sep 17 00:00:00 2001 From: Leandro Guindani Gehlen Date: Thu, 3 Aug 2023 16:46:39 -0300 Subject: [PATCH] Add `autoIndexStore` database option --- README.md | 12 +++++++++ jest.config.js | 2 +- src/index.ts | 24 ++++++++++++----- tests/database.ts | 15 +++-------- tests/search.spec.ts | 64 +++++++++++++++++++++++++++++++++----------- 5 files changed, 83 insertions(+), 34 deletions(-) diff --git a/README.md b/README.md index 4de9026..83e0e81 100644 --- a/README.md +++ b/README.md @@ -54,4 +54,16 @@ await database.exportIndexes({ ``` +You can use the `autoIndexStore` database option to automatically export indexes when the collection is modified. + +```js +const database = await createRxDatabase({ + storage: getRxStorageMemory(), + options: { + autoIndexStore: (key, value) => { + localStorage.setItem(key, value); + }, + } +}); +``` diff --git a/jest.config.js b/jest.config.js index 5325ad8..decad38 100644 --- a/jest.config.js +++ b/jest.config.js @@ -3,5 +3,5 @@ module.exports = { preset: 'ts-jest', testEnvironment: 'node', testPathIgnorePatterns: ['/dist/'], - coveragePathIgnorePatterns: ['/dist/', '/node_modules/', '/tests/database.ts'], + coveragePathIgnorePatterns: ['/dist/', '/node_modules/'], }; diff --git a/src/index.ts b/src/index.ts index 4f9cea6..5f5f9f9 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,7 +1,7 @@ import { RxCollection, RxDatabase, RxDocument, RxPlugin } from 'rxdb'; import { Index } from 'flexsearch'; -export type RxExportIndexHandler = (id: string, value: string) => void; +export type RxExportIndexHandler = (key: string, value: string) => void | Promise; export interface RxDatabaseIndexMethods { exportIndexes(handler: RxExportIndexHandler): Promise; @@ -31,9 +31,7 @@ async function exportIndexes( entries.map(([key, collection]) => { const { $index } = collection as RxCollectionSearch; if ($index) { - return $index.export((_, value) => { - handler(key, value); - }); + return $index.export((_, value) => handler(key, value)); } }) ); @@ -66,6 +64,9 @@ async function search(this: RxCollectionSearch, query: string): Promise { index.remove(data[primaryPath]); + + if (autoIndexStore) { + index.export((_, data) => autoIndexStore(collection.name, data)); + } }, false); collection.postSave((data) => { index.add(data[primaryPath], serialize(data, searchFields)); + + if (autoIndexStore) { + index.export((_, data) => autoIndexStore(collection.name, data)); + } }, false); collection.postInsert((data) => { index.add(data[primaryPath], serialize(data, searchFields)); + + if (autoIndexStore) { + index.export((_, data) => autoIndexStore(collection.name, data)); + } }, false); } @@ -95,13 +108,12 @@ export const RxDBFlexSearchPlugin: RxPlugin = { }, RxCollection(proto: any) { proto.search = search; - proto.options = { searchable: false }; }, }, hooks: { createRxCollection: { after: ({ collection }) => { - if (collection.options.searchable) { + if (collection.options?.searchable) { initialize(collection); } }, diff --git a/tests/database.ts b/tests/database.ts index 3b82470..ef6f0c9 100644 --- a/tests/database.ts +++ b/tests/database.ts @@ -1,5 +1,6 @@ import { createRxDatabase } from 'rxdb'; import { getRxStorageMemory } from 'rxdb/plugins/storage-memory'; +import { RxDatabaseSearch } from '../src'; export const userSchema = { version: 0, @@ -20,20 +21,12 @@ export const userSchema = { }, }; -export async function initDatabase() { +export async function initDatabase(options?: any): Promise { const database = await createRxDatabase({ name: 'testdb', storage: getRxStorageMemory(), + ...options, }); - await database.addCollections({ - users: { - schema: userSchema, - options: { - searchFields: ['name', 'age'], - }, - }, - }); - - return database; + return database as RxDatabaseSearch; } diff --git a/tests/search.spec.ts b/tests/search.spec.ts index 0c4f787..2022dc6 100644 --- a/tests/search.spec.ts +++ b/tests/search.spec.ts @@ -1,8 +1,7 @@ -import { addRxPlugin, createRxDatabase } from 'rxdb'; -import { getRxStorageMemory } from 'rxdb/plugins/storage-memory'; +import { addRxPlugin } from 'rxdb'; import { RxDBDevModePlugin } from 'rxdb/plugins/dev-mode'; import { RxCollectionSearch, RxDBFlexSearchPlugin, RxDatabaseSearch } from '../src'; -import { userSchema } from './database'; +import { initDatabase, userSchema } from './database'; describe('Replication', () => { let database: RxDatabaseSearch; @@ -12,11 +11,12 @@ describe('Replication', () => { addRxPlugin(RxDBDevModePlugin); }); - beforeEach(async () => { - database = (await createRxDatabase({ - name: 'testdb', - storage: getRxStorageMemory(), - })) as RxDatabaseSearch; + afterEach(async () => { + await database.remove(); + }); + + it('should update index', async () => { + database = await initDatabase(); await database.addCollections({ users: { @@ -26,13 +26,7 @@ describe('Replication', () => { }, }, }); - }); - - afterEach(async () => { - await database.remove(); - }); - it('should update index', async () => { const collection = database.users as RxCollectionSearch; await collection.insert({ @@ -76,9 +70,21 @@ describe('Replication', () => { expect(results.length).toBe(0); }); - it('should export/import indexes', async () => { + it('should import/export indexes', async () => { let indexId: string; - let indexData; + let indexData: string; + + database = await initDatabase(); + + await database.addCollections({ + users: { + schema: userSchema, + options: { + searchable: true, + }, + }, + }); + const collection = database.users as RxCollectionSearch; await collection.insert({ @@ -98,4 +104,30 @@ describe('Replication', () => { const { $index } = collection; expect($index.contain('1')).toBe(true); }); + + it('should auto export indexes', async () => { + database = await initDatabase({ + options: { + autoIndexStore: (key: string, value: string) => { + expect(key).toEqual('users'); + expect(value).toBeTruthy(); + }, + }, + }); + + await database.addCollections({ + users: { + schema: userSchema, + options: { + searchable: true, + }, + }, + }); + + await database.users.insert({ + id: '1', + name: 'Mark Zuckerberg', + age: 39, + }); + }); });