Skip to content
Source
Sign inSign up
TanStack/db
Open History to see the last commit

index.ts

529 lines12.9 KB

1import { Readable } from 'node:stream'
2import { pipeline } from 'node:stream/promises'
3import cors from 'cors'
4import express from 'express'
5import postgres from 'postgres'
6import { ELECTRIC_PROTOCOL_QUERY_PARAMS } from '@electric-sql/client'
7​
8const app = express()
9const PORT = 3001
10const ELECTRIC_URL = process.env.ELECTRIC_URL ?? `http://localhost:3003`
11const sql = postgres({
12 host: `localhost`,
13 port: 54322,
14 user: `postgres`,
15 password: `postgres`,
16 database: `shopping_list`,
17})
18​
19const HOP_BY_HOP_HEADERS = new Set([
20 `connection`,
21 `content-length`,
22 `keep-alive`,
23 `proxy-authenticate`,
24 `proxy-authorization`,
25 `te`,
26 `trailer`,
27 `transfer-encoding`,
28 `upgrade`,
29 `host`,
30])
31​
32app.use(cors())
33app.use(express.json())
34​
35interface ShoppingList {
36 id: string
37 name: string
38 createdAt: string
40​
41interface ShoppingItem {
42 id: string
43 listId: string
44 text: string
45 checked: boolean
46 createdAt: string
48​
49function asIso(value: unknown): string {
50 if (value instanceof Date) return value.toISOString()
51 return new Date(String(value)).toISOString()
53​
54function toShoppingList(row: {
55 id: string
56 name: string
57 createdAt: unknown
58}): ShoppingList {
59 return {
60 id: row.id,
61 name: row.name,
62 createdAt: asIso(row.createdAt),
63 }
65​
66function toShoppingItem(row: {
67 id: string
68 listId: string
69 text: string
70 checked: boolean
71 createdAt: unknown
72}): ShoppingItem {
73 return {
74 id: row.id,
75 listId: row.listId,
76 text: row.text,
77 checked: row.checked,
78 createdAt: asIso(row.createdAt),
79 }
81​
82function buildElectricShapeUrl(requestUrl: string, table: string): URL {
83 const url = new URL(requestUrl)
84 const originUrl = new URL(`/v1/shape`, ELECTRIC_URL)
85​
86 url.searchParams.forEach((value, key) => {
87 if (ELECTRIC_PROTOCOL_QUERY_PARAMS.includes(key)) {
88 originUrl.searchParams.set(key, value)
89 }
90 })
91​
92 originUrl.searchParams.set(`table`, table)
93​
94 if (process.env.ELECTRIC_SOURCE_ID) {
95 originUrl.searchParams.set(`source_id`, process.env.ELECTRIC_SOURCE_ID)
96 }
97 const sourceSecret =
98 process.env.ELECTRIC_SOURCE_SECRET ?? process.env.ELECTRIC_SECRET
99 if (sourceSecret) {
100 originUrl.searchParams.set(`secret`, sourceSecret)
101 }
102​
103 return originUrl
105​
106function buildForwardHeaders(req: express.Request): Headers {
107 const headers = new Headers()
108 for (const [key, value] of Object.entries(req.headers)) {
109 const lowerKey = key.toLowerCase()
110 if (HOP_BY_HOP_HEADERS.has(lowerKey)) {
111 continue
112 }
113 if (value === undefined) {
114 continue
115 }
116 headers.set(key, Array.isArray(value) ? value.join(`,`) : value)
117 }
118 return headers
120​
121async function proxyElectricShape(
122 req: express.Request,
123 res: express.Response,
124 table: string,
125) {
126 const requestUrl = `${req.protocol}://${req.get(`host`)}${req.originalUrl}`
127 const originUrl = buildElectricShapeUrl(requestUrl, table)
128 const forwardHeaders = buildForwardHeaders(req)
129 const body =
130 req.method === `POST` && req.body !== undefined
131 ? JSON.stringify(req.body)
132 : undefined
133​
134 if (body && !forwardHeaders.has(`content-type`)) {
135 forwardHeaders.set(`content-type`, `application/json`)
136 }
137​
138 const response = await fetch(originUrl, {
139 method: req.method,
140 headers: forwardHeaders,
141 body,
142 })
143​
144 response.headers.forEach((value, key) => {
145 const lower = key.toLowerCase()
146 if (
147 lower === `content-encoding` ||
148 lower === `content-length` ||
149 lower === `transfer-encoding`
150 ) {
151 return
152 }
153 res.setHeader(key, value)
154 })
155 const varyHeader = response.headers.get(`vary`)
156 res.setHeader(
157 `Vary`,
158 varyHeader ? `${varyHeader}, Authorization` : `Authorization`,
159 )
160 res.status(response.status)
161​
162 if (!response.body) {
163 res.end()
164 return
165 }
166​
167 const nodeStream = Readable.fromWeb(response.body as any)
168 res.on(`close`, () => nodeStream.destroy())
169 await pipeline(nodeStream, res)
171​
172async function ensureDb() {
173 await sql`
174 CREATE TABLE IF NOT EXISTS shopping_lists (
175 id text PRIMARY KEY,
176 name text NOT NULL,
177 "createdAt" timestamptz NOT NULL DEFAULT now()
178 )
179 `
180​
181 await sql`
182 CREATE TABLE IF NOT EXISTS shopping_items (
183 id text PRIMARY KEY,
184 "listId" text NOT NULL REFERENCES shopping_lists(id) ON DELETE CASCADE,
185 text text NOT NULL,
186 checked boolean NOT NULL DEFAULT false,
187 "createdAt" timestamptz NOT NULL DEFAULT now()
188 )
189 `
190​
191 const [{ count }] = await sql<Array<{ count: string }>>`
192 SELECT count(*)::text as count FROM shopping_lists
193 `
194 if (Number.parseInt(count, 10) > 0) {
195 return
196 }
197​
198 await sql`
199 INSERT INTO shopping_lists (id, name)
200 VALUES
201 ('list-grocery', 'Grocery'),
202 ('list-hardware', 'Hardware Store')
203 ON CONFLICT (id) DO NOTHING
204 `
205​
206 await sql`
207 INSERT INTO shopping_items (id, "listId", text, checked)
208 VALUES
209 ('item-milk', 'list-grocery', 'Milk', false),
210 ('item-eggs', 'list-grocery', 'Eggs', false),
211 ('item-bread', 'list-grocery', 'Bread', true),
212 ('item-screwdriver', 'list-hardware', 'Screwdriver', false),
213 ('item-nails', 'list-hardware', 'Nails', false)
214 ON CONFLICT (id) DO NOTHING
215 `
217​
218app.get('/api/shapes/lists', async (req, res) => {
219 try {
220 await proxyElectricShape(req, res, `shopping_lists`)
221 } catch (error) {
222 console.error(`Failed to proxy lists shape`, error)
223 if (!res.headersSent) {
224 res.status(502).json({ error: `Failed to proxy lists shape` })
225 }
226 }
227})
228​
229app.post('/api/shapes/lists', async (req, res) => {
230 try {
231 await proxyElectricShape(req, res, `shopping_lists`)
232 } catch (error) {
233 console.error(`Failed to proxy lists shape`, error)
234 if (!res.headersSent) {
235 res.status(502).json({ error: `Failed to proxy lists shape` })
236 }
237 }
238})
239​
240app.get('/api/shapes/items', async (req, res) => {
241 try {
242 await proxyElectricShape(req, res, `shopping_items`)
243 } catch (error) {
244 console.error(`Failed to proxy items shape`, error)
245 if (!res.headersSent) {
246 res.status(502).json({ error: `Failed to proxy items shape` })
247 }
248 }
249})
250​
251app.post('/api/shapes/items', async (req, res) => {
252 try {
253 await proxyElectricShape(req, res, `shopping_items`)
254 } catch (error) {
255 console.error(`Failed to proxy items shape`, error)
256 if (!res.headersSent) {
257 res.status(502).json({ error: `Failed to proxy items shape` })
258 }
259 }
260})
261​
262app.get('/api/lists', async (_req, res) => {
263 const rows = await sql<
264 Array<{
265 id: string
266 name: string
267 createdAt: unknown
268 }>
269 >`
270 SELECT id, name, "createdAt"
271 FROM shopping_lists
272 ORDER BY "createdAt" DESC
273 `
274 res.json(rows.map(toShoppingList))
275})
276​
277app.post('/api/lists', async (req, res) => {
278 const { id, name, createdAt } = req.body as {
279 id?: string
280 name?: string
281 createdAt?: string
282 }
283 if (!name?.trim()) {
284 return res.status(400).json({ error: `List name is required` })
285 }
286​
287 const [inserted] = await sql<
288 Array<{
289 txid: string
290 id: string
291 name: string
292 createdAt: unknown
293 }>
294 >`
295 WITH tx AS (
296 SELECT pg_current_xact_id()::xid::text as txid
297 ),
298 inserted AS (
299 INSERT INTO shopping_lists (id, name, "createdAt")
300 VALUES (
301 ${id ?? crypto.randomUUID()},
302 ${name.trim()},
303 COALESCE(${createdAt ?? null}, now())
304 )
305 ON CONFLICT (id) DO UPDATE SET name = EXCLUDED.name
306 RETURNING id, name, "createdAt"
307 )
308 SELECT tx.txid, inserted.id, inserted.name, inserted."createdAt"
309 FROM tx, inserted
310 `
311​
312 return res.status(201).json({
313 list: toShoppingList(inserted),
314 txid: Number.parseInt(inserted.txid, 10),
315 })
316})
317​
318app.put('/api/lists/:id', async (req, res) => {
319 const { id } = req.params
320 const { name } = req.body as { name?: string }
321 if (name !== undefined && !name.trim()) {
322 return res.status(400).json({ error: `List name cannot be empty` })
323 }
324​
325 const updatedRows = await sql`
326 WITH tx AS (
327 SELECT pg_current_xact_id()::xid::text as txid
328 ),
329 updated AS (
330 UPDATE shopping_lists
331 SET name = COALESCE(${name?.trim() ?? null}, name)
332 WHERE id = ${id}
333 RETURNING id, name, "createdAt"
334 )
335 SELECT tx.txid, updated.id, updated.name, updated."createdAt"
336 FROM tx, updated
337 `
338 const updated = updatedRows[0] as
339 | { txid: string; id: string; name: string; createdAt: unknown }
340 | undefined
341​
342 if (!updated) {
343 return res.status(404).json({ error: `List not found` })
344 }
345​
346 return res.json({
347 list: toShoppingList(updated),
348 txid: Number.parseInt(updated.txid, 10),
349 })
350})
351​
352app.delete('/api/lists/:id', async (req, res) => {
353 const { id } = req.params
354​
355 const deletedRows = await sql`
356 WITH tx AS (
357 SELECT pg_current_xact_id()::xid::text as txid
358 ),
359 deleted AS (
360 DELETE FROM shopping_lists
361 WHERE id = ${id}
362 RETURNING id
363 )
364 SELECT tx.txid, deleted.id
365 FROM tx, deleted
366 `
367 const deleted = deletedRows[0] as { txid: string; id: string } | undefined
368​
369 if (!deleted) {
370 return res.status(404).json({ error: `List not found` })
371 }
372​
373 return res.json({ success: true, txid: Number.parseInt(deleted.txid, 10) })
374})
375​
376app.get('/api/items', async (_req, res) => {
377 const rows = await sql<
378 Array<{
379 id: string
380 listId: string
381 text: string
382 checked: boolean
383 createdAt: unknown
384 }>
385 >`
386 SELECT id, "listId", text, checked, "createdAt"
387 FROM shopping_items
388 ORDER BY "createdAt" ASC
389 `
390 res.json(rows.map(toShoppingItem))
391})
392​
393app.post('/api/items', async (req, res) => {
394 const { id, listId, text, checked, createdAt } = req.body as {
395 id?: string
396 listId?: string
397 text?: string
398 checked?: boolean
399 createdAt?: string
400 }
401​
402 if (!listId || !text?.trim()) {
403 return res.status(400).json({ error: `listId and text are required` })
404 }
405​
406 const [inserted] = await sql<
407 Array<{
408 txid: string
409 id: string
410 listId: string
411 text: string
412 checked: boolean
413 createdAt: unknown
414 }>
415 >`
416 WITH tx AS (
417 SELECT pg_current_xact_id()::xid::text as txid
418 ),
419 inserted AS (
420 INSERT INTO shopping_items (id, "listId", text, checked, "createdAt")
421 VALUES (
422 ${id ?? crypto.randomUUID()},
423 ${listId},
424 ${text.trim()},
425 COALESCE(${checked ?? null}, false),
426 COALESCE(${createdAt ?? null}, now())
427 )
428 ON CONFLICT (id) DO UPDATE SET
429 text = EXCLUDED.text,
430 checked = EXCLUDED.checked
431 RETURNING id, "listId", text, checked, "createdAt"
432 )
433 SELECT tx.txid, inserted.id, inserted."listId", inserted.text, inserted.checked, inserted."createdAt"
434 FROM tx, inserted
435 `
436​
437 return res.status(201).json({
438 item: toShoppingItem(inserted),
439 txid: Number.parseInt(inserted.txid, 10),
440 })
441})
442​
443app.put('/api/items/:id', async (req, res) => {
444 const { id } = req.params
445 const { text, checked } = req.body as { text?: string; checked?: boolean }
446 if (text !== undefined && !text.trim()) {
447 return res.status(400).json({ error: `Item text cannot be empty` })
448 }
449​
450 const updatedRows = await sql`
451 WITH tx AS (
452 SELECT pg_current_xact_id()::xid::text as txid
453 ),
454 updated AS (
455 UPDATE shopping_items
456 SET
457 text = COALESCE(${text?.trim() ?? null}, text),
458 checked = COALESCE(${checked ?? null}, checked)
459 WHERE id = ${id}
460 RETURNING id, "listId", text, checked, "createdAt"
461 )
462 SELECT tx.txid, updated.id, updated."listId", updated.text, updated.checked, updated."createdAt"
463 FROM tx, updated
464 `
465 const updated = updatedRows[0] as
466 | {
467 txid: string
468 id: string
469 listId: string
470 text: string
471 checked: boolean
472 createdAt: unknown
473 }
474 | undefined
475​
476 if (!updated) {
477 return res.status(404).json({ error: `Item not found` })
478 }
479​
480 return res.json({
481 item: toShoppingItem(updated),
482 txid: Number.parseInt(updated.txid, 10),
483 })
484})
485​
486app.delete('/api/items/:id', async (req, res) => {
487 const { id } = req.params
488​
489 const deletedRows = await sql`
490 WITH tx AS (
491 SELECT pg_current_xact_id()::xid::text as txid
492 ),
493 deleted AS (
494 DELETE FROM shopping_items
495 WHERE id = ${id}
496 RETURNING id
497 )
498 SELECT tx.txid, deleted.id
499 FROM tx, deleted
500 `
501 const deleted = deletedRows[0] as { txid: string; id: string } | undefined
502​
503 if (!deleted) {
504 return res.status(404).json({ error: `Item not found` })
505 }
506​
507 return res.json({ success: true, txid: Number.parseInt(deleted.txid, 10) })
508})
509​
510async function start() {
511 try {
512 await ensureDb()
513​
514 app.listen(PORT, '0.0.0.0', () => {
515 console.log(`Server running at http://0.0.0.0:${PORT}`)
516 console.log(`For Android emulator API: http://10.0.2.2:${PORT}`)
517 console.log(`For iOS simulator API: http://localhost:${PORT}`)
518 console.log(`Electric shape endpoint: http://localhost:3003/v1/shape`)
519 })
520 } catch (error) {
521 console.error(`Failed to start shopping-list server`, error)
522 console.error(
523 `Did you run 'pnpm db:up' in examples/react-native/shopping-list?`,
524 )
525 process.exit(1)
526 }
528​
529void start()