mirror of
https://github.com/getpaseo/paseo.git
synced 2026-11-05 07:54:38 +00:00
Ship the platform storage engines behind a dark interface so the cache migration can wire them separately. Both engines share a contract, including atomic writes and destructive schema-version resets.
50 lines
1.5 KiB
TypeScript
50 lines
1.5 KiB
TypeScript
import { openDatabaseAsync, type SQLiteDatabase } from "expo-sqlite";
|
|
import type { ReplicaRowStore } from "./row-store";
|
|
import { REPLICA_ROW_STORE_SCHEMA_VERSION } from "./row-store-schema";
|
|
import {
|
|
createSqliteReplicaRowStore,
|
|
type ReplicaSqliteConnection,
|
|
type ReplicaSqliteDriver,
|
|
type SqliteValue,
|
|
} from "./row-store-sqlite";
|
|
|
|
export type {
|
|
ReplicaHostRows,
|
|
ReplicaRow,
|
|
ReplicaRowChanges,
|
|
ReplicaRowKey,
|
|
ReplicaRowKind,
|
|
ReplicaRowStore,
|
|
} from "./row-store";
|
|
export { REPLICA_ROW_STORE_SCHEMA_VERSION, REPLICA_SINGLETON_ROW_ID } from "./row-store-schema";
|
|
|
|
const DATABASE_NAME = "paseo-replica-row-store.db";
|
|
|
|
function bind(params: readonly SqliteValue[]): SqliteValue[] {
|
|
return [...params];
|
|
}
|
|
|
|
function createConnection(database: SQLiteDatabase): ReplicaSqliteConnection {
|
|
return {
|
|
exec: (sql) => database.execAsync(sql),
|
|
run: async (sql, params = []) => {
|
|
await database.runAsync(sql, bind(params));
|
|
},
|
|
all: (sql, params = []) => database.getAllAsync(sql, bind(params)),
|
|
transaction: (operation) =>
|
|
database.withExclusiveTransactionAsync(async (transaction) => {
|
|
await operation(createConnection(transaction));
|
|
}),
|
|
};
|
|
}
|
|
|
|
const expoSqliteDriver: ReplicaSqliteDriver = {
|
|
async open() {
|
|
return createConnection(await openDatabaseAsync(DATABASE_NAME));
|
|
},
|
|
};
|
|
|
|
export function createReplicaRowStore(): ReplicaRowStore {
|
|
return createSqliteReplicaRowStore(expoSqliteDriver, REPLICA_ROW_STORE_SCHEMA_VERSION);
|
|
}
|