MCPcopy Create free account
hub / github.com/clientdb/clientdb / createSyncServer

Function createSyncServer

engine/server/index.ts:34–100  ·  view source on GitHub ↗
(config: SyncServerConfig)

Source from the content-addressed store, hash-verified

32 */
33
34export function createSyncServer(config: SyncServerConfig) {
35 const app = express();
36 const httpServer = http.createServer(app);
37 const socketServer = new SocketServer(httpServer);
38
39 const realTimeManager = createRealTimeManager(socketServer);
40
41 const dbConnection = knex({
42 connection: config.dbConnection,
43 });
44
45 const { requestHandlers, schema } = config;
46
47 async function createContext(req: Request) {
48 const context = await createRequestContext(
49 req,
50 requestHandlers,
51 schema,
52 dbConnection
53 );
54
55 return context;
56 }
57
58 app.get("/init", async (req, res) => {
59 const context = await createContext(req);
60
61 const bootData = await fetchInitialData(context);
62 res.json(bootData);
63 });
64
65 app.get("/sync", async (req, res) => {
66 const context = await createContext(req);
67
68 const result = await fetchSyncDelta(context);
69
70 res.json(result);
71 });
72
73 app.post("/mutate", async (req, res) => {
74 const context = await createContext(req);
75
76 const {
77 body: { input },
78 } = req;
79
80 const result = await performMutation(context, input);
81
82 res.json(result);
83
84 realTimeManager.requestEveryoneToSync();
85 });
86
87 function listen(port: number) {
88 return new Promise<void>((resolve) => {
89 app.listen(port, () => {
90 console.log(`Sync server listening on port ${port}`);
91 resolve();

Callers

nothing calls this directly

Calls 5

createRealTimeManagerFunction · 0.90
fetchInitialDataFunction · 0.90
fetchSyncDeltaFunction · 0.90
performMutationFunction · 0.90
createContextFunction · 0.85

Tested by

no test coverage detected