@nichoth/replicache-supabase
Version:
Use replicache with supabase
99 lines • 4 kB
JavaScript
export async function getClientGroup(executor, id) {
const row = await executor.oneOrNone('select * from replicache_client_group where id = $1', [id]);
if (!row) {
return null;
}
const cg = {
id: row.id,
userID: row.user_id,
};
return cg;
}
export async function createClientGroup(executor, id, userID) {
await executor.none('insert into replicache_client_group (id, user_id) values ($1, $2)', [id, userID]);
return { id, userID };
}
export async function getClient(executor, id) {
const row = await executor.oneOrNone('select * from replicache_client where id = $1', [id]);
if (!row)
return null;
const client = {
id: row.id,
clientGroupID: row.client_group_id,
lastMutationID: row.last_mutation_id,
lastModifiedVersion: row.last_modified_version,
};
return client;
}
export async function createClient(executor, id, clientGroupID, version) {
await executor.none(`insert into replicache_client (
id, client_group_id, last_mutation_id, last_modified_version
) values (
$1, $2, 0, $3
)`, [id, clientGroupID, version]);
return {
id,
clientGroupID,
lastMutationID: 0,
lastModifiedVersion: version,
};
}
export async function updateClient(executor, client) {
await executor.none(`update replicache_client set
last_mutation_id = $1, last_modified_version = $2 where id = $3`, [client.lastMutationID, client.lastModifiedVersion, client.id]);
}
export async function getEntry(executor, key) {
const row = await executor.one('select value from entry where key = $1 and deleted = false', [key]);
if (!row) {
return undefined;
}
return JSON.parse(row.value);
}
export async function putEntry(executor, key, value, version) {
await executor.none(`
insert into entry (key, value, deleted, last_modified_version)
values ($1, $2, false, $3)
on conflict (key) do update set
value = $2, deleted = false, last_modified_version = $3
`, [key, JSON.stringify(value), version]);
}
export async function delEntry(executor, key, version) {
await executor.none(`update entry set deleted = true, last_modified_version = $2
where key = $1`, [key, version]);
}
export async function* getEntries(executor, fromKey) {
const rows = await executor.manyOrNone('select key, value from entry where key >= $1 and deleted = false order by key', [fromKey]);
for (const row of rows) {
yield [row.key, JSON.parse(row.value)];
}
}
export async function getChangedEntries(executor, prevVersion) {
const rows = await executor.manyOrNone('select key, value, deleted from entry where last_modified_version > $1', [prevVersion]);
return rows.map((row) => [row.key, JSON.parse(row.value), row.deleted]);
}
export async function getGlobalVersion(executor) {
const row = await executor.one('select version from replicache_space');
const { version } = row;
return version;
}
export async function setGlobalVersion(executor, version) {
await executor.none('update replicache_space set version = $1', [version]);
}
export async function getChangedLastMutationIDs(executor, clientGroupID, sinceVersion) {
const rows = await executor.manyOrNone(`select id, last_mutation_id from replicache_client
where client_group_id = $1 and last_modified_version > $2`, [clientGroupID, sinceVersion]);
const result = {};
for (const r of rows) {
result[r.id] = r.last_mutation_id;
}
return result;
}
export async function setLastMutationID(executor, clientID, lastMutationID, lastModifiedVersion) {
await executor.none(`
insert into replicache_client (id, last_mutation_id, last_modified_version)
values ($1, $2, $3)
on conflict (id) do update set
lastmutationid = $2, last_modified_version = $3
`, [clientID, lastMutationID, lastModifiedVersion]);
}
//# sourceMappingURL=util.js.map