Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

crsqlite drizzle adapter #49

Open
wants to merge 22 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
},
"prettier.enable": true,
"explorer.fileNesting.patterns": {
"*package.json": "${capture}.eslint*, ${capture}.prettier*, ${capture}pnpm*, ${capture}turbo*, ${capture}tsconfig.json, ${capture}vite.config.ts, ${capture}vitest.*, ${capture}.gitignore, ${capture}esbuild.*, ${capture}knip.*, ${capture}.nvmrc, ${capture}.cspell.json, ${capture}*.dict.txt",
"*package.json": "${capture}.eslint*, ${capture}.prettier*, ${capture}pnpm*, ${capture}turbo*, ${capture}tsconfig.json, ${capture}vite.config.ts, ${capture}vitest.*, ${capture}.gitignore, ${capture}esbuild.*, ${capture}knip.*, ${capture}.nvmrc, ${capture}.cspell.json, ${capture}*.dict.txt, ${capture}drizzle.config.*",
"*.js": "${capture}.js.*, ${capture}.d.ts*",
"*.ts": "${capture}.ts.*, ${capture}.d.ts*, ${capture}.js*, ${capture}.mjs*",
"*.css": "${capture}.css.*",
Expand Down
4 changes: 4 additions & 0 deletions client/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { WorkerDemo } from "client/components/WorkerDemo"
import { DbDemo } from "client/components/DbDemo"
import { Hero } from "client/components/Hero/Hero"
import styles from "client/App.module.css"
import { DrizzleTest } from "client/components/drizzle/Test"

fooBar()

Expand Down Expand Up @@ -37,6 +38,9 @@ export default function App() {
<Cell row>
<DbDemo />
</Cell>
<Cell row>
<DrizzleTest />
</Cell>
</Grid>
</main>
)
Expand Down
136 changes: 136 additions & 0 deletions client/src/components/drizzle/Test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
import * as schema from "shared/drizzle-test/schema"
import { eq } from "drizzle-orm"
import { useEffect } from "react"
import initWasm from "@vlcn.io/crsqlite-wasm"
import tblrx from "@vlcn.io/rx-tbl"

import { getMigrations } from "./getMigrations"
import { useQuery, useQueryClient } from "@tanstack/react-query"
import { migrate } from "drizzle-orm-crsqlite-wasm/migrator"
import { drizzle, type CRSQLiteDatabase } from "drizzle-orm-crsqlite-wasm"

async function make() {
const sqlite = await initWasm()
const sql = await sqlite.open("test")
const db = drizzle(sql, { schema, logger: true })
await migrate(db, { migrations: await getMigrations() }).catch(console.error)
const rx = tblrx(sql)
return { db, rx }
}

export function DrizzleTest() {
const client = useQueryClient()
useEffect(() => {
const key = "test"
make()
.then((ctx) => {
console.log("ctx", ctx)
client.setQueryData([key], {
// schema: cleanSchema,
// schemaName,
name: "test",
ctx,
})
})
.catch(console.error)
}, [])
return (
<div>
Drizzle
<hr />
<TestChild />
</div>
)
}

declare module "drizzle-orm/session" {
export interface PreparedQuery {
finalize(): Promise<void>
}
}

function TestChild() {
const { data } = useQuery({ queryKey: ["test"] })
useEffect(() => {
if (!data) return
const db = data.ctx.db as CRSQLiteDatabase<typeof schema>

Check failure on line 56 in client/src/components/drizzle/Test.tsx

View workflow job for this annotation

GitHub Actions / 🐙 Test (20.x, 8.x)

tsc > 2339

Property 'ctx' does not exist on type '{}'. (2339)

Check failure on line 56 in client/src/components/drizzle/Test.tsx

View workflow job for this annotation

GitHub Actions / 🐙 Test (21.x, 8.x)

tsc > 2339

Property 'ctx' does not exist on type '{}'. (2339)
void (async function () {
let [usa] = await db
.select()
.from(schema.countries)
.where(eq(schema.countries.name, "USA"))
if (!usa) {
;[usa] = await db
.insert(schema.countries)
.values({
id: crypto.randomUUID(),
name: "USA",
population: 331_900_000,
})
.returning()
}
let [nyc] = await db
.select()
.from(schema.cities)
.where(eq(schema.cities.name, "New York"))
console.log("::::::::::: nyc", nyc)
if (!nyc) {
;[nyc] = await db
.insert(schema.cities)
.values({
id: crypto.randomUUID(),
name: "New York",
population: 8_336_817,
countryId: usa!.id,
})
.returning()
}
const res = db.query.countries
.findMany({
with: {
cities: {
where: (city, { eq, sql }) =>
eq(city.name, sql.placeholder("cityName")),
},
},
})
.prepare()

console.log(":::::::::::::: user-query", res)
await res
.all({ cityName: "New York" })
.then((a) => console.log("DRIZZLE", a))
.catch(console.error)
// @ts-expect-error -- the lib does not expose this method on the type level, but it does exist
await res.finalize()
console.log("--------------------------")
const foo = await db.transaction(async (tx) => {
// throw "rollback"
console.log("inside tx function")
const [usa] = await tx
.select({
name: schema.countries.name,
pop: schema.countries.population,
})
.from(schema.countries)
.where(eq(schema.countries.name, "USA"))
console.log("after tx select", usa)
const nyc = await tx.transaction(async (tx) => {
console.log("inside nested tx function")
const [nyc] = await tx
.select({
name: schema.cities.name,
pop: schema.cities.population,
})
.from(schema.cities)
.where(eq(schema.cities.name, "New York"))
tx.rollback()
return nyc
})
return { usa, nyc }
})
console.log("FOO", foo)
})()
}, [data])
return <div>Test</div>
}
39 changes: 39 additions & 0 deletions client/src/components/drizzle/getMigrations.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import type { MigrationMeta } from "drizzle-orm/migrator"
import migrationJournal from "shared/drizzle-migrations/meta/_journal.json"
import { migrations } from "shared/drizzle-migrations/index"

/**
* TODO: this should be done at build-time through Vite `define` config
*/
export async function getMigrations() {
const journal = migrationJournal as {
entries: Array<{ idx: number; when: number; tag: string; breakpoints: boolean }>
}
const migrationQueries: MigrationMeta[] = []
for (const journalEntry of journal.entries) {
const query = migrations[journalEntry.tag as keyof typeof migrations] as string
const result = query.split("--> statement-breakpoint")
migrationQueries.push({
sql: result,
bps: journalEntry.breakpoints,
folderMillis: journalEntry.when,
hash: await createSha256Hash(query),
})
}
return migrationQueries
}

/**
* Cross-platform implementation of node's
* ```ts
* crypto.createHash("sha256").update(query).digest("hex")
* ```
*/
async function createSha256Hash(query: string) {
const encoder = new TextEncoder()
const data = encoder.encode(query)
const hash = await globalThis.crypto.subtle.digest("SHA-256", data)
const hashArray = Array.from(new Uint8Array(hash))
const hashHex = hashArray.map((b) => b.toString(16).padStart(2, "0")).join("")
return hashHex
}
9 changes: 9 additions & 0 deletions drizzle.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { defineConfig } from "drizzle-kit"

export default defineConfig({
schema: "./shared/src/drizzle-test/schema.ts",
driver: "better-sqlite",
out: "./shared/src/drizzle-migrations",
verbose: true,
strict: true,
})
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@
"root:format": "prettier --check '*.ts' '*.json' 'types/*' '.github/**/*' --config .prettierrc.json --log-level warn",
"root:format:fix": "prettier --write '*.ts' '*.json' 'types/*' '.github/**/*' --config .prettierrc.json --log-level warn",
"root:spell": "cspell --config .cspell.json --quiet --gitignore '*' 'types/*' '.github/**/*'",
"root:clear": "rm -rf dist; rm -rf node_modules; rm -rf .turbo; find . -name '*.tsbuildinfo' -type f -delete;"
"root:clear": "rm -rf dist; rm -rf node_modules; rm -rf .turbo; find . -name '*.tsbuildinfo' -type f -delete;",
"generate": "drizzle-kit generate:sqlite"
},
"dependencies": {
"@fastify/cookie": "^9.3.1",
Expand All @@ -46,6 +47,8 @@
"better-sqlite3": "^9.4.3",
"clsx": "^2.1.0",
"dotenv": "^16.4.5",
"drizzle-orm": "^0.30.6",
"drizzle-orm-crsqlite-wasm": "0.0.3",
"fastify": "^4.26.2",
"grant": "^5.4.22",
"pino-pretty": "10.3.1",
Expand All @@ -69,6 +72,7 @@
"chalk": "^5.3.0",
"chokidar": "3.6.0",
"cspell": "^8.6.0",
"drizzle-kit": "^0.20.14",
"esbuild": "0.20.1",
"eslint": "^8.57.0",
"eslint-config-prettier": "^9.1.0",
Expand Down
Loading
Loading