Skip to content

Commit

Permalink
Upgrade electric and convert the write server to Hono
Browse files Browse the repository at this point in the history
  • Loading branch information
samwillis committed Nov 28, 2024
1 parent ae73344 commit 0d1e75c
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 304 deletions.
6 changes: 2 additions & 4 deletions demos/linearlite/backend/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,10 @@ services:
- config_file=/etc/postgresql/postgresql.conf

backend:
image: electricsql/electric:canary
image: electricsql/electric
environment:
DATABASE_URL: postgresql://postgres:password@postgres:5432/linearlite?sslmode=disable
ports:
- 3000:3000
build:
context: ../packages/sync-service/
depends_on:
- postgres
- postgres
8 changes: 4 additions & 4 deletions demos/linearlite/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"private": true,
"type": "module",
"scripts": {
"backend:up": "dotenv -- docker compose -f ./backend/docker-compose.yml up -d ",
"backend:up": "dotenv -- docker compose -f ./backend/docker-compose.yml up -d && pnpm db:migrate",
"backend:down": "dotenv -- docker compose -f ./backend/docker-compose.yml down --volumes",
"db:migrate": "dotenv -- pnpm exec pg-migrations apply --directory ./db/migrations-server",
"db:load-data": "dotenv -- node ./db/load_data.js",
Expand Down Expand Up @@ -44,7 +44,6 @@
"cors": "^2.8.5",
"dayjs": "^1.11.11",
"dotenv": "^16.4.5",
"express": "^4.21.1",
"fractional-indexing": "^3.2.0",
"jsonwebtoken": "^9.0.2",
"lodash.debounce": "^4.0.8",
Expand All @@ -61,15 +60,16 @@
"tiptap-markdown": "^0.8.2",
"uuid": "^9.0.0",
"vite-plugin-svgr": "^3.2.0",
"zod": "^3.23.8"
"zod": "^3.23.8",
"hono": "^4.0.0",
"@hono/node-server": "^1.8.0"
},
"devDependencies": {
"@databases/pg": "^5.5.0",
"@databases/pg-migrations": "^5.0.3",
"@faker-js/faker": "^8.4.1",
"@tailwindcss/typography": "^0.5.10",
"@types/body-parser": "^1.19.5",
"@types/express": "^5.0.0",
"@types/jest": "^29.5.12",
"@types/lodash.debounce": "^4.0.9",
"@types/node": "^20.14.10",
Expand Down
38 changes: 22 additions & 16 deletions demos/linearlite/server.ts
Original file line number Diff line number Diff line change
@@ -1,52 +1,57 @@
import express from 'express'
import bodyParser from 'body-parser'
import cors from 'cors'
import { Hono } from 'hono'
import { cors } from 'hono/cors'
import pg from 'pg'
import {
ChangeSet,
changeSetSchema,
CommentChange,
IssueChange,
} from './src/utils/changes'
import { serve } from '@hono/node-server'

const DATABASE_URL = process.env.DATABASE_URL

const { Client } = pg
const client = new Client(DATABASE_URL)
client.connect()

const app = express()
const app = new Hono()

app.use(bodyParser.json())
app.use(cors())
// Middleware
app.use('/*', cors())

app.get('/', async (_req, res) => {
// Routes
app.get('/', async (c) => {
const result = await client.query(
"SELECT 'ok' as status, version() as postgres_version, now() as server_time"
)
res.send(result.rows[0])
return c.json(result.rows[0])
})

app.post('/apply-changes', async (req, res) => {
const content = req.body
app.post('/apply-changes', async (c) => {
const content = await c.req.json()
let parsedChanges: ChangeSet
try {
parsedChanges = changeSetSchema.parse(content)
// Any additional validation of the changes can be done here!
} catch (error) {
console.error(error)
res.status(400).send('Invalid changes')
return
return c.json({ error: 'Invalid changes' }, 400)
}
const changeResponse = await applyChanges(parsedChanges)
res.send(changeResponse)
return c.json(changeResponse)
})

app.listen(3001, () => {
console.log('Server is running on port 3001')
// Start the server
const port = 3001
console.log(`Server is running on port ${port}`)

serve({
fetch: app.fetch,
port
})

async function applyChanges(changes: ChangeSet): Promise<void> {
async function applyChanges(changes: ChangeSet): Promise<{ success: boolean }> {
const { issues, comments } = changes
client.query('BEGIN')
try {
Expand All @@ -57,6 +62,7 @@ async function applyChanges(changes: ChangeSet): Promise<void> {
for (const comment of comments) {
await applyTableChange('comment', comment)
}
return { success: true }
} catch (error) {
await client.query('ROLLBACK')
throw error
Expand Down
6 changes: 4 additions & 2 deletions demos/linearlite/src/pglite-worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,17 @@ worker({
await migrate(pg)
await pg.sync.syncShapeToTable({
shape: {
url: `${ELECTRIC_URL}/v1/shape/issue`,
url: `${ELECTRIC_URL}/v1/shape`,
table: 'issue',
},
table: 'issue',
primaryKey: ['id'],
shapeKey: 'issues',
})
await pg.sync.syncShapeToTable({
shape: {
url: `${ELECTRIC_URL}/v1/shape/comment`,
url: `${ELECTRIC_URL}/v1/shape`,
table: 'comment',
},
table: 'comment',
primaryKey: ['id'],
Expand Down
Loading

0 comments on commit 0d1e75c

Please sign in to comment.