@nichoth/replicache-supabase
Version:
Use replicache with supabase
44 lines • 1.46 kB
JavaScript
import { z } from 'zod';
import { getChangedEntries, getChangedLastMutationIDs, getClientGroup, getGlobalVersion, } from './data.js';
import { tx } from './db.js';
export const authError = new Error('Users can only access their own groups');
export const PullRequestSchema = z.object({
clientGroupID: z.string(),
cookie: z.union([z.number(), z.null()]),
});
export async function processPull(req, userID) {
const { clientGroupID, cookie: requestCookie } = req;
const [entries, lastMutationIDChanges, responseCookie] = await tx(async (executor) => {
const clientGroup = await getClientGroup(executor, req.clientGroupID);
if (clientGroup && (clientGroup.userID !== userID)) {
throw authError;
}
return Promise.all([
getChangedEntries(executor, requestCookie ?? 0),
getChangedLastMutationIDs(executor, clientGroupID, requestCookie ?? 0),
getGlobalVersion(executor),
]);
});
const res = {
lastMutationIDChanges,
cookie: responseCookie,
patch: [],
};
for (const [key, value, deleted] of entries) {
if (deleted) {
res.patch.push({
op: 'del',
key,
});
}
else {
res.patch.push({
op: 'put',
key,
value,
});
}
}
return res;
}
//# sourceMappingURL=pull.js.map