Skip to content

Commit

Permalink
Add expo-sqlite optional peer dependency and make storage required/ex…
Browse files Browse the repository at this point in the history
…plicit
  • Loading branch information
catalinmiron committed Jan 21, 2025
1 parent 29c2195 commit 7071ddd
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 5 deletions.
6 changes: 6 additions & 0 deletions bun.lock
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@
"tsup": "^8.0.2",
"typescript": "^5.7.2",
},
"peerDependencies": {
"expo-sqlite": "^15.0.0",
},
"optionalPeers": [
"expo-sqlite",
],
},
},
"overrides": {
Expand Down
8 changes: 8 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,14 @@
"dependencies": {
"use-sync-external-store": "^1.2.2"
},
"peerDependencies": {
"expo-sqlite": "^15.0.0"
},
"peerDependenciesMeta": {
"expo-sqlite": {
"optional": true
}
},
"overrides": {
"react": "18.3.1"
},
Expand Down
13 changes: 8 additions & 5 deletions src/persist-plugins/expo-sqlite.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
import type { Change } from '@legendapp/state';
import { applyChanges, internal } from '@legendapp/state';
import type { ObservablePersistPlugin, PersistMetadata } from '@legendapp/state/sync';
import { SQLiteStorage, Storage } from 'expo-sqlite/kv-store';
import { SQLiteStorage } from 'expo-sqlite/kv-store';

const { safeParse, safeStringify } = internal;

const MetadataSuffix = '__m';

export class ObservablePersistSqlite implements ObservablePersistPlugin {
private data: Record<string, any> = {};
private storage: SQLiteStorage | undefined;
constructor(storage: SQLiteStorage | undefined) {
this.storage = storage || Storage;
private storage: SQLiteStorage;
constructor(storage: SQLiteStorage) {
console.error(
'[legend-state] ObservablePersistSqlite failed to initialize. You need to pass the SQLiteStorage instance.',
);
this.storage = storage;
}
public getTable(table: string, init: any) {
if (!this.storage) return undefined;
Expand Down Expand Up @@ -62,6 +65,6 @@ export class ObservablePersistSqlite implements ObservablePersistPlugin {
}
}

export function observablePersistSqlite(storage?: SQLiteStorage) {
export function observablePersistSqlite(storage: SQLiteStorage) {
return new ObservablePersistSqlite(storage);
}

0 comments on commit 7071ddd

Please sign in to comment.