Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
"@evilmartians/lefthook": "^1.6.13",
"@happy-dom/global-registrator": "^14.12.0",
"@jest/globals": "^29.7.0",
"@nozbe/watermelondb": "^0.28.0",
"@react-native-async-storage/async-storage": "^1.23.1",
"@release-it/conventional-changelog": "^8.0.1",
"@supabase/supabase-js": "^2.43.4",
Expand Down
90 changes: 90 additions & 0 deletions src/persist-plugins/watermelondb.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import { applyChanges, internal } from '@legendapp/state';

import type { ObservablePersistPlugin, PersistMetadata, PersistOptions } from '@legendapp/state/sync';
import type { Change } from '@legendapp/state';
import type LocalStorage from '@nozbe/watermelondb/Database/LocalStorage';

const MetadataSuffix = '__m';

const { safeParse, safeStringify } = internal;

class ObservablePersistWatermelonDB implements ObservablePersistPlugin {
private readonly storage: LocalStorage;
private data: Record<string, unknown> = {};

constructor(storage: LocalStorage) {
if (!storage) {
throw new Error(
'[legend-state] ObservablePersistWatermelonDB failed to initialize. You need to pass the WatermelonDB localStorage instance.',
);
}

this.storage = storage;
}

getTable<T = any>(table: string, init: any): T {
if (this.data[table] === undefined) {
try {
this.storage._getSync(table, (val) => {
this.data[table] = val ? safeParse(val) : init;
});
} catch (e) {
console.error('[legend-state] ObservablePersistWatermelonDB parse failed', table, e);
}
}

return this.data[table] as T;
}

deleteTable(table: string): Promise<void> | void {
if (!this.storage) return undefined;

delete this.data[table];

return this.storage.remove(table);
}

set(table: string, changes: Change[]): Promise<void> | void {
const current = this.data[table] ?? {};
const updated = applyChanges(current, changes);
this.data[table] = updated;

return this.storage.set(table, safeStringify(updated));
}

getMetadata(table: string, _config: PersistOptions): PersistMetadata {
return this.getTable(table + MetadataSuffix, {});
}

setMetadata(table: string, metadata: PersistMetadata): Promise<void> | void {
const key = table + MetadataSuffix;
this.data[key] = metadata;

return this.storage.set(key, metadata);
}

deleteMetadata(table: string): Promise<void> | void {
const key = table + MetadataSuffix;
delete this.data[key];

return this.storage.remove(key);
}
}

/**
* Usage:
* ```ts
* import { observablePersistWatermelonDB } from '@legendapp/state/sync-plugins/ObservablePersistWatermelonDB'
* import { database } from '@/lib/db'
*
* syncObservable(settings$, {
* persist: {
* name: 'settings',
* plugin: observablePersistWatermelonDB(database.localStorage),
* },
* })
* ```
*/
export function observablePersistWatermelonDB(localStorage: LocalStorage) {
return new ObservablePersistWatermelonDB(localStorage);
}