Files
paseo/packages/app/src/runtime/replica-cache/row-store.native.ts
T
Mohamed Boudra f59db03d67 feat(app): add replica cache row store
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.
2026-08-25 23:38:38 +02:00

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);
}