Skip to content

Commit 4338d98

Browse files
committed
chore: add neon serverless adapter
1 parent fa2d7aa commit 4338d98

File tree

8 files changed

+385
-3
lines changed

8 files changed

+385
-3
lines changed

.changeset/gold-fans-draw.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@systemfsoftware/neon-serverless-adapter": patch
3+
---
4+
5+
chore: add neon serverless adapter
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"$schema": "https://developer.microsoft.com/json-schemas/api-extractor/v7/api-extractor.schema.json",
3+
"extends": "@systemfsoftware/api-extractor/api-extractor.json"
4+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
{
2+
"name": "@systemfsoftware/neon-serverless-adapter",
3+
"version": "0.0.0",
4+
"type": "module",
5+
"exports": {
6+
"./package.json": "./package.json",
7+
".": {
8+
"import": {
9+
"@systemfsoftware/source": "./src/mod.ts",
10+
"types": "./dist/esm/mod.d.ts",
11+
"default": "./dist/esm/mod.js"
12+
}
13+
}
14+
},
15+
"module": "./dist/esm/mod.js",
16+
"files": [
17+
"dist"
18+
],
19+
"tshy": {
20+
"dialects": [
21+
"esm"
22+
],
23+
"project": "./tsconfig.build.json",
24+
"exports": {
25+
"./package.json": "./package.json",
26+
".": "./src/mod.ts"
27+
},
28+
"sourceDialects": [
29+
"@systemfsoftware/source"
30+
]
31+
},
32+
"scripts": {
33+
"prepare": "pnpm turbo build",
34+
"clean": "rimraf dist .tshy .tshy-build",
35+
"build": "pnpm run clean && tshy && pnpm run extract",
36+
"extract": "api-extractor run"
37+
},
38+
"dependencies": {
39+
"@effect/platform": "catalog:",
40+
"drizzle-orm": "~0.34.1",
41+
"@neondatabase/serverless": "^0.9.5"
42+
},
43+
"devDependencies": {
44+
"effect": "catalog:",
45+
"@systemfsoftware/api-extractor": "workspace:^"
46+
},
47+
"peerDependencies": {
48+
"effect": "catalog:"
49+
}
50+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import * as Socket from '@effect/platform/Socket'
2+
import * as neon from '@neondatabase/serverless'
3+
import type { DrizzleConfig } from 'drizzle-orm'
4+
import { drizzle, type NeonDatabase } from 'drizzle-orm/neon-serverless'
5+
import { Config as C, Effect, pipe } from 'effect'
6+
import type { ConfigError } from 'effect/ConfigError'
7+
8+
/**
9+
* @public
10+
*/
11+
export namespace Adapter {
12+
export type TSchema = Record<string, unknown>
13+
14+
export type DB<T extends TSchema = Record<string, never>> = NeonDatabase<T>
15+
export type Options<T extends TSchema = Record<string, never>> = DrizzleConfig<T>
16+
export type Pool = neon.Pool
17+
export type WebSocketConstructor = Socket.WebSocketConstructor
18+
19+
export type Adapter<T extends TSchema = Record<string, never>> = Effect.Effect<
20+
{
21+
pool: Pool
22+
db: DB<T>
23+
},
24+
ConfigError,
25+
Socket.WebSocketConstructor
26+
>
27+
28+
export type Config = Readonly<{
29+
DATABASE_URL: C.Config<string>
30+
}>
31+
}
32+
33+
/**
34+
* @public
35+
*/
36+
export function Adapter<T extends Adapter.TSchema = Record<string, never>>(
37+
options?: Adapter.Options<T>,
38+
): Adapter.Adapter<T> {
39+
return Effect.gen(function*() {
40+
const connectionString = yield* Config.DATABASE_URL
41+
const webSocketConstructor = yield* Socket.WebSocketConstructor
42+
43+
yield* Effect.sync(() => {
44+
neon.neonConfig.webSocketConstructor = webSocketConstructor
45+
})
46+
47+
const pool = yield* Effect.sync(() => new neon.Pool({ connectionString }))
48+
const db = yield* Effect.sync(() => drizzle(pool, options))
49+
50+
return { pool, db }
51+
})
52+
}
53+
54+
/**
55+
* @public
56+
*/
57+
export const Config: Adapter.Config = Object.freeze({
58+
DATABASE_URL: pipe(
59+
C.nonEmptyString('DATABASE_URL'),
60+
C.withDescription('Neon database connection string'),
61+
),
62+
})
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"extends": "./tsconfig.json",
3+
"compilerOptions": {
4+
"declaration": true,
5+
"sourceMap": true,
6+
"declarationMap": true,
7+
"stripInternal": true
8+
}
9+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"$schema": "https://json.schemastore.org/tsconfig",
3+
"extends": "@total-typescript/tsconfig/tsc/no-dom/library-monorepo",
4+
"compilerOptions": {
5+
"customConditions": ["@systemfsoftware/source"]
6+
},
7+
"include": ["src"]
8+
}

0 commit comments

Comments
 (0)