-
Notifications
You must be signed in to change notification settings - Fork 96
feat(zero-client): expo-sqlite storage #4669
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
29 commits
Select commit
Hold shift + click to select a range
08f94ae
@austinm911's changes to add first-class expo integrations
austinm911 9b3e643
fix: only have zero-expo
0xcadams 3c3c2ba
feat: added expo sqlite
0xcadams 0c8bbfb
fix: remove unused prepared statements
0xcadams e0c0bee
fix: entrypoint
0xcadams 0b78396
fix: cleanup
0xcadams 5e867f8
Merge branch 'main' into 0xcadams/expo
0xcadams 0e0e73c
fix: add back WITHOUT ROWID
0xcadams 5fc9311
Merge remote-tracking branch 'origin/0xcadams/expo' into 0xcadams/expo
0xcadams edaca04
chore: fix global types
0xcadams 59e6b7c
chore: update peer dep version
0xcadams de0b1a9
fix: move to use tx/savepoint for reads
0xcadams 99c12e8
test: simplify bench
0xcadams e0b779e
fix: moved defaults to be config and fixed test config
0xcadams c726a5c
Merge branch 'main' into 0xcadams/expo
0xcadams caf8fc1
fix: test config
0xcadams f9ff100
Merge branch 'main' into 0xcadams/expo
0xcadams 1a0de26
fix: move to use read pool and single write conn
0xcadams fd47385
Merge remote-tracking branch 'origin/0xcadams/expo' into 0xcadams/expo
0xcadams 1320e5c
Merge remote-tracking branch 'origin/main' into 0xcadams/expo
0xcadams 891d60a
fix: remove readonly flag
0xcadams 3d536a4
Merge branch 'main' into 0xcadams/expo
0xcadams b430406
Merge branch 'main' into 0xcadams/expo
0xcadams 0710a6d
fix: update based on @arv feedback
0xcadams 5eada28
fix: rename
0xcadams baae8b6
Merge branch 'main' into 0xcadams/expo
0xcadams 57d26d3
Merge branch 'main' into 0xcadams/expo
0xcadams c9b14ce
Merge branch 'main' into 0xcadams/expo
0xcadams 589d170
Merge branch 'main' into 0xcadams/expo
0xcadams File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next
Next commit
@austinm911's changes to add first-class expo integrations
- Loading branch information
commit 08f94aea4a1b3f3e4ae7cfe6fbf82a8e7d6e6fc3
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,220 @@ | ||
import type {ReadonlyJSONValue} from '../../../shared/src/json.ts'; | ||
import {deepFreeze} from '../frozen-json.ts'; | ||
import type {Store as KVStore, Read, Write} from './store.ts'; | ||
|
||
export interface SQLiteTransaction { | ||
begin(readonly?: boolean): Promise<void>; | ||
execute<T>( | ||
sqlStatement: string, | ||
args?: (string | number | null)[] | undefined, | ||
): Promise<T[]>; | ||
commit(): Promise<void>; | ||
} | ||
|
||
export interface SQLDatabase { | ||
transaction(): SQLiteTransaction; | ||
destroy(): Promise<void>; | ||
close(): Promise<void>; | ||
} | ||
|
||
export interface GenericSQLiteDatabaseManager { | ||
open(name: string): Promise<SQLDatabase>; | ||
} | ||
|
||
/** | ||
* A SQLite-based Store implementation. | ||
* | ||
* This store provides a generic SQLite implementation that can be used with different | ||
* SQLite providers (like expo-sqlite, better-sqlite3, etc). It implements the Store | ||
* interface using a single 'entry' table with key-value pairs. | ||
* | ||
* The store ensures strict serializable transactions by using SQLite's native | ||
* transaction support. Read transactions use SQLite's READ mode while write | ||
* transactions use the default mode. | ||
*/ | ||
export class SQLiteStore implements KVStore { | ||
readonly #name: string; | ||
readonly #dbm: SQLiteDatabaseManager; | ||
#closed = false; | ||
|
||
constructor(name: string, dbm: SQLiteDatabaseManager) { | ||
this.#name = name; | ||
this.#dbm = dbm; | ||
} | ||
|
||
async read() { | ||
const db = await this.#getDb(); | ||
const tx = db.transaction(); | ||
await tx.begin(true); | ||
return new SQLiteStoreRead(tx); | ||
} | ||
|
||
async write(): Promise<Write> { | ||
const db = await this.#getDb(); | ||
const tx = db.transaction(); | ||
await tx.begin(false); | ||
return new SQLiteStoreWrite(tx); | ||
} | ||
|
||
async close() { | ||
await this.#dbm.close(this.#name); | ||
this.#closed = true; | ||
} | ||
|
||
get closed(): boolean { | ||
return this.#closed; | ||
} | ||
|
||
#getDb() { | ||
return this.#dbm.open(this.#name); | ||
} | ||
} | ||
|
||
/** | ||
* Creates a function that returns new SQLite store instances. | ||
* This is the main entry point for using the SQLite store implementation. | ||
* | ||
* @param db The SQLite database manager implementation | ||
* @returns A function that creates new store instances | ||
*/ | ||
export function getCreateSQLiteStore(db: SQLiteDatabaseManager) { | ||
return (name: string) => new SQLiteStore(name, db); | ||
} | ||
|
||
/** | ||
* Implementation of the Read interface for SQLite stores. | ||
* Provides read-only access to the underlying SQLite database. | ||
*/ | ||
export class SQLiteStoreRead implements Read { | ||
protected _tx: SQLiteTransaction | null; | ||
|
||
constructor(tx: SQLiteTransaction) { | ||
this._tx = tx; | ||
} | ||
|
||
async has(key: string) { | ||
const unsafeValue = await this.#getSql(key); | ||
return unsafeValue !== undefined; | ||
} | ||
|
||
async get(key: string) { | ||
const unsafeValue = await this.#getSql(key); | ||
if (unsafeValue === undefined) return; | ||
const parsedValue = JSON.parse(unsafeValue) as ReadonlyJSONValue; | ||
const frozenValue = deepFreeze(parsedValue); | ||
return frozenValue; | ||
} | ||
|
||
async release() { | ||
const tx = this._assertTx(); | ||
await tx.commit(); | ||
this._tx = null; | ||
} | ||
|
||
get closed(): boolean { | ||
return this._tx === null; | ||
} | ||
|
||
async #getSql(key: string) { | ||
const rows = await this._assertTx().execute<{value: string}>( | ||
'SELECT value FROM entry WHERE key = ?', | ||
[key], | ||
); | ||
|
||
if (rows.length === 0) return undefined; | ||
|
||
return rows[0].value; | ||
} | ||
|
||
protected _assertTx() { | ||
if (this._tx === null) throw new Error('Transaction is closed'); | ||
return this._tx; | ||
} | ||
} | ||
|
||
/** | ||
* Implementation of the Write interface for SQLite stores. | ||
* Extends SQLiteStoreRead to provide write capabilities. | ||
*/ | ||
export class SQLiteStoreWrite extends SQLiteStoreRead implements Write { | ||
async put(key: string, value: ReadonlyJSONValue) { | ||
const jsonValueString = JSON.stringify(value); | ||
await this._assertTx().execute( | ||
'INSERT OR REPLACE INTO entry (key, value) VALUES (?, ?)', | ||
[key, jsonValueString], | ||
); | ||
} | ||
|
||
async del(key: string) { | ||
await this._assertTx().execute('DELETE FROM entry WHERE key = ?', [key]); | ||
} | ||
|
||
async commit() { | ||
// Do nothing and wait for release. | ||
} | ||
} | ||
|
||
/** | ||
* Manages SQLite database instances and their lifecycle. | ||
* Handles database creation, schema setup, and cleanup. | ||
*/ | ||
export class SQLiteDatabaseManager { | ||
readonly #dbm: GenericSQLiteDatabaseManager; | ||
readonly #dbInstances = new Map< | ||
string, | ||
{db: SQLDatabase; state: 'open' | 'closed'} | ||
>(); | ||
|
||
constructor(dbm: GenericSQLiteDatabaseManager) { | ||
this.#dbm = dbm; | ||
} | ||
|
||
async open(name: string) { | ||
const dbInstance = this.#dbInstances.get(name); | ||
if (dbInstance?.state === 'open') return dbInstance.db; | ||
|
||
const newDb = await this.#dbm.open(`replicache-${name}.sqlite`); | ||
if (!dbInstance) { | ||
await this.#setupSchema(newDb); | ||
this.#dbInstances.set(name, {state: 'open', db: newDb}); | ||
} else { | ||
dbInstance.state = 'open'; | ||
} | ||
|
||
return newDb; | ||
} | ||
|
||
async close(name: string) { | ||
const dbInstance = this.#dbInstances.get(name); | ||
if (!dbInstance) return; | ||
|
||
await dbInstance.db.close(); | ||
dbInstance.state = 'closed'; | ||
} | ||
|
||
async truncate(name: string) { | ||
const db = await this.open(name); | ||
const tx = db.transaction(); | ||
await tx.begin(false); | ||
await tx.execute('DELETE FROM entry', []); | ||
await tx.commit(); | ||
} | ||
|
||
async destroy(name: string) { | ||
const dbInstance = this.#dbInstances.get(name); | ||
if (!dbInstance) return; | ||
|
||
await dbInstance.db.destroy(); | ||
this.#dbInstances.delete(name); | ||
} | ||
|
||
async #setupSchema(db: SQLDatabase) { | ||
const tx = db.transaction(); | ||
await tx.begin(false); | ||
await tx.execute( | ||
'CREATE TABLE IF NOT EXISTS entry (key TEXT PRIMARY KEY, value TEXT)', | ||
[], | ||
); | ||
await tx.commit(); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.