convex
Version:
Client for the Convex Cloud
54 lines (50 loc) • 1.58 kB
text/typescript
import {
convexToJson,
GenericId,
jsonToConvex,
Value,
} from "../../values/index.js";
import { performAsyncSyscall } from "./syscall.js";
import { DatabaseReader, DatabaseWriter } from "../database.js";
import { QueryInitializerImpl } from "./query_impl.js";
import { GenericDataModel, GenericDocument } from "../data_model.js";
export function setupReader(): DatabaseReader<GenericDataModel> {
return {
get: async (id: GenericId<string>) => {
const args = { id: convexToJson(id) };
const syscallJSON = await performAsyncSyscall("get", args);
return jsonToConvex(syscallJSON) as GenericDocument;
},
query: (tableName: string) => new QueryInitializerImpl(tableName),
};
}
export function setupWriter(): DatabaseWriter<GenericDataModel> {
const reader = setupReader();
return {
get: reader.get,
query: reader.query,
insert: async (table, value) => {
const syscallJSON = await performAsyncSyscall("insert", {
table,
value: convexToJson(value),
});
const syscallResult = jsonToConvex(syscallJSON) as any;
return syscallResult._id;
},
patch: async (id, value) => {
await performAsyncSyscall("update", {
id: convexToJson(id),
value: convexToJson(value as Value),
});
},
replace: async (id, value) => {
await performAsyncSyscall("replace", {
id: convexToJson(id),
value: convexToJson(value),
});
},
delete: async id => {
await performAsyncSyscall("remove", { id: convexToJson(id) });
},
};
}