Skip to content

Commit 52916c2

Browse files
committed
remove electric sql
1 parent 8c15e99 commit 52916c2

71 files changed

Lines changed: 2581 additions & 5531 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.github/workflows/lint-check.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ jobs:
2929
- name: Setup pnpm
3030
uses: pnpm/action-setup@v4
3131
with:
32-
version: 11.5.2
32+
version: 11.8.0
3333

3434
- name: Install dependencies
3535
run: pnpm install --frozen-lockfile --ignore-scripts

.github/workflows/release.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ jobs:
1919
- name: Install pnpm
2020
uses: pnpm/action-setup@v4
2121
with:
22-
version: 11.5.2
22+
version: 11.8.0
2323

2424
- name: Install Dependencies
2525
run: pnpm install --frozen-lockfile

apps/api/.env.example

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,3 @@ INFISICAL_CLIENT_SECRET=
3030
INFISICAL_ENVIRONMENT=dev
3131
INFISICAL_PROJECT_ID=
3232
INFISICAL_SITE_URL=http://localhost:8082
33-
ELECTRIC_URL=http://localhost:3100
34-
ELECTRIC_SECRET=devfe1e207b8cfb185b406438a42adev

apps/api/env.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,6 @@ const envType = type({
3333
GITHUB_TOKEN: 'string',
3434
TODESKTOP_WEBHOOK_SECRET: 'string',
3535
PROXY_SHARED_SECRET: 'string',
36-
ELECTRIC_URL: 'string',
37-
ELECTRIC_SECRET: 'string',
3836
})
3937

4038
const devOptionalEnvs = [

apps/api/index.ts

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,7 @@ import { createContext } from './orpc/context'
1111
import { router } from './orpc/routers'
1212
import { sendEmail } from './lib/resend'
1313
import { sanitizeLogData } from '@conar/shared/utils/sanitize-log'
14-
import { ELECTRIC_EXPOSED_HEADERS } from './lib/electric'
1514
import { healthRouter } from './routers/health'
16-
import { shapesRouter } from './routers/shapes'
1715

1816
const handler = new RPCHandler(router, {
1917
interceptors: [
@@ -71,7 +69,6 @@ const app = new Hono<{
7169
return origin.endsWith('.conar.app') || allowedOrigins.includes(origin) ? origin : null
7270
},
7371
credentials: true,
74-
exposeHeaders: [...ELECTRIC_EXPOSED_HEADERS],
7572
}))
7673
.get('/', c => c.redirect(env.MAIN_URL))
7774
.use('*', async (c, next) => {
@@ -150,7 +147,6 @@ const app = new Hono<{
150147

151148
return auth.handler(req)
152149
})
153-
.route('/shapes', shapesRouter)
154150
.use('/rpc/*', async (c, next) => {
155151
const { matched, response } = await handler.handle(c.req.raw.clone(), {
156152
prefix: '/rpc',
@@ -168,11 +164,4 @@ const app = new Hono<{
168164
export default {
169165
fetch: app.fetch,
170166
port: Number(process.env.PORT || 3000),
171-
// Electric live shape requests (`live=true`) long-poll: Electric holds the
172-
// connection open (~20 s) until new data arrives or the poll window elapses.
173-
// Bun's default 30 s idle timeout can close these mid-poll, so portless sees
174-
// a reset and serves its 502 page. Raise the ceiling above the poll window
175-
// (255 s is Bun's max) so legitimate polls survive while truly stuck sockets
176-
// still time out.
177-
idleTimeout: 255,
178167
}

apps/api/lib/electric.ts

Lines changed: 0 additions & 91 deletions
This file was deleted.

apps/api/orpc/context.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export function createContext(c: HonoContext<{ Variables: AppVariables }>) {
2020
return {
2121
request: c.req.raw,
2222
headers: c.req.raw.headers,
23+
clientId: c.req.header('x-client-id'),
2324
setHeader: (key: string, value: string) => {
2425
c.res.headers.set(key, value)
2526
},

apps/api/orpc/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export const orpc = os.$context<Context>()
1414

1515
export const getUserSecret = memoize((userId: string) => {
1616
return infisical.secrets.get({ path: ['users', userId], name: INFISICAL_USER_ENCRYPTION_SECRET_NAME })
17-
}, { maxAge: 10 * 60 * 1000 }) // 10 minutes
17+
}, { maxAge: 5 * 60 * 1000 }) // 5 minutes
1818

1919
async function getSession(headers: Headers) {
2020
const session = await auth.api.getSession({ headers })
@@ -29,7 +29,7 @@ async function getSession(headers: Headers) {
2929
export const logMiddleware = orpc.middleware(async ({ context, next }, input) => {
3030
const result = await next()
3131

32-
if (!context.request.url.endsWith('/sync') && !context.request.url.includes('/shapes/')) {
32+
if (!context.request.url.endsWith('/sync') && !context.request.url.endsWith('/resolveConnectionString')) {
3333
context.addLogData({
3434
input,
3535
output: (Array.isArray(result.output) && result.output.length > 0)

apps/api/orpc/lib/sync.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
import type { Type } from 'arktype'
2+
import { IORedisPublisher } from '@orpc/experimental-publisher/ioredis'
3+
import { eventIterator } from '@orpc/server'
24
import { type } from 'arktype'
5+
import { redis } from '~/lib/redis'
6+
import { authMiddleware, orpc } from '~/orpc'
37

48
export function createSyncOutputSchema<const T>(
59
schema: type.validate<T>,
@@ -16,6 +20,17 @@ export function createSyncOutputSchema(schema: Type) {
1620
)
1721
}
1822

23+
export function createSyncPublisher<T extends Type<{ type: 'insert' | 'update' | 'delete' }>>(
24+
output: T,
25+
prefix: string,
26+
) {
27+
return new IORedisPublisher<Record<string, typeof output.infer>>({
28+
commander: redis.duplicate(),
29+
listener: redis.duplicate(),
30+
prefix,
31+
})
32+
}
33+
1934
export async function syncDiff<TItem>(opts: {
2035
input: { id: string, updatedAt: Date }[]
2136
queries: {
@@ -33,3 +48,15 @@ export async function syncDiff<TItem>(opts: {
3348
const missingIds = inputIds.filter(id => !existingIds.includes(id))
3449
return { updatedItems, newItems, missingIds }
3550
}
51+
52+
// eslint-disable-next-line ts/no-explicit-any
53+
export function createEventsEndpoint<O extends Type<{ type: 'insert' | 'update' | 'delete' }>, P extends IORedisPublisher<any>>(output: O, publisher: P) {
54+
return orpc
55+
.use(authMiddleware)
56+
.output(eventIterator(output))
57+
.handler(async function* ({ context, signal, lastEventId }) {
58+
for await (const payload of publisher.subscribe(context.user.id, { signal, lastEventId })) {
59+
yield payload
60+
}
61+
})
62+
}

apps/api/orpc/routers/chats-messages/create.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,14 @@ import { chats, chatsMessages, chatsMessagesInsertSchema } from '@conar/db/schem
33
import { ORPCError } from '@orpc/server'
44
import { type } from 'arktype'
55
import { and, eq, inArray } from 'drizzle-orm'
6-
import { generateTxId } from '~/lib/electric'
76
import { orpc, subscriptionMiddleware } from '~/orpc'
7+
import { publisher } from './events'
88

99
const schema = chatsMessagesInsertSchema
1010

1111
export const create = orpc
1212
.use(subscriptionMiddleware)
13-
.input(type.or(
14-
schema,
15-
schema.array(),
16-
).pipe(data => Array.isArray(data) ? data : [data]))
13+
.input(type.or(schema, schema.array()).pipe(data => Array.isArray(data) ? data : [data]))
1714
.handler(async ({ context, input }) => {
1815
const chatIds = input.map(item => item.chatId)
1916
const foundChats = await db.select({ id: chats.id })
@@ -26,9 +23,12 @@ export const create = orpc
2623
})
2724
}
2825

29-
return db.transaction(async (tx) => {
30-
await tx.insert(chatsMessages).values(input)
26+
const inserted = await db.insert(chatsMessages).values(input).returning()
3127

32-
return { txid: await generateTxId(tx) }
33-
})
28+
for (const message of inserted) {
29+
publisher.publish(context.user.id, {
30+
type: 'insert',
31+
value: message,
32+
})
33+
}
3434
})

0 commit comments

Comments
 (0)