Skip to content

Commit

Permalink
Add autoIndexStore database option
Browse files Browse the repository at this point in the history
  • Loading branch information
leandrogehlen committed Aug 3, 2023
1 parent e0a69a2 commit 4233bb0
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 34 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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);
},
}
});
```

2 changes: 1 addition & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
testPathIgnorePatterns: ['<rootDir>/dist/'],
coveragePathIgnorePatterns: ['/dist/', '/node_modules/', '/tests/database.ts'],
coveragePathIgnorePatterns: ['/dist/', '/node_modules/'],
};
24 changes: 18 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -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<void>;

export interface RxDatabaseIndexMethods {
exportIndexes(handler: RxExportIndexHandler): Promise<void>;
Expand Down Expand Up @@ -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));
}
})
);
Expand Down Expand Up @@ -66,6 +64,9 @@ async function search(this: RxCollectionSearch, query: string): Promise<RxDocume

export function initialize(collection: RxCollection) {
const index = new Index({ tokenize: 'full' });
const database = collection.database as RxDatabaseSearch;

const { autoIndexStore } = database.options;
const { primaryPath } = collection.schema;
const { properties } = collection.schema.jsonSchema;
const searchFields = collection.options.searchFields || Object.keys(properties);
Expand All @@ -74,14 +75,26 @@ export function initialize(collection: RxCollection) {

collection.postRemove((data) => {
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);
}

Expand All @@ -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);
}
},
Expand Down
15 changes: 4 additions & 11 deletions tests/database.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { createRxDatabase } from 'rxdb';
import { getRxStorageMemory } from 'rxdb/plugins/storage-memory';
import { RxDatabaseSearch } from '../src';

export const userSchema = {
version: 0,
Expand All @@ -20,20 +21,12 @@ export const userSchema = {
},
};

export async function initDatabase() {
export async function initDatabase(options?: any): Promise<RxDatabaseSearch> {
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;
}
64 changes: 48 additions & 16 deletions tests/search.spec.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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: {
Expand All @@ -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({
Expand Down Expand Up @@ -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({
Expand All @@ -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,
});
});
});

0 comments on commit 4233bb0

Please sign in to comment.