-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,73 @@ | ||
import type { DB } from "@vlcn.io/crsqlite-wasm" | ||
import { drizzle } from "./crsqlite" | ||
import * as schema from "./schema" | ||
import * as schema from "../../../../shared/src/drizzle-test/schema" | ||
import type { Dialect, SQL } from "drizzle-orm" | ||
import { useEffect } from "react" | ||
import initWasm from "@vlcn.io/crsqlite-wasm" | ||
import tblrx from "@vlcn.io/rx-tbl" | ||
|
||
export function makeDrizzleDb(db: DB) { | ||
return drizzle(db, { schema }) | ||
} | ||
// export function makeDrizzleDb(db: DB) { | ||
// return drizzle(db, { schema }) | ||
// } | ||
|
||
// const db = makeDrizzleDb({} as DB) | ||
|
||
// const res = db.query.countries | ||
// .findMany({ | ||
// with: { | ||
// cities: { | ||
// where: (city, { eq, sql }) => eq(city.name, sql.placeholder("cityName")), | ||
// }, | ||
// }, | ||
// }) | ||
// .prepare() | ||
|
||
// type foo = keyof typeof res & {} & string | ||
// // ^? | ||
|
||
// res.finalize() | ||
|
||
const db = makeDrizzleDb({} as DB) | ||
// const data = await res.all({ cityName: "New York" }) | ||
// // ^? | ||
|
||
const res = db.query.countries | ||
.findMany({ | ||
with: { | ||
cities: { | ||
where: (city, { eq, sql }) => eq(city.name, sql.placeholder("cityName")), | ||
}, | ||
}, | ||
}) | ||
.prepare() | ||
// console.log(data) | ||
|
||
const data = await res.all({ cityName: "New York" }) | ||
// ^? | ||
import { migrate } from "./crsqlite/migrator" | ||
import { useQueryClient } from "@tanstack/react-query" | ||
|
||
console.log(data) | ||
async function make() { | ||
const sqlite = await initWasm() | ||
const sql = await sqlite.open("test") | ||
const db = drizzle(sql, { schema }) | ||
await migrate(db, { migrationsFolder: "drizzle" }) | ||
const rx = tblrx(sql) | ||
return { db, rx } | ||
} | ||
|
||
export function DrizzleTest() { | ||
const client = useQueryClient() | ||
useEffect(() => { | ||
const key = "test" | ||
make() | ||
.then((db) => { | ||
client.setQueryData<DbStore>(key, { | ||
Check failure on line 53 in client/src/components/drizzle/Test.tsx GitHub Actions / 🐙 Test (20.x, 8.x)tsc > 2304
Check failure on line 53 in client/src/components/drizzle/Test.tsx GitHub Actions / 🐙 Test (20.x, 8.x)tsc > 2345
Check failure on line 53 in client/src/components/drizzle/Test.tsx GitHub Actions / 🐙 Test (21.x, 8.x)tsc > 2304
|
||
// schema: cleanSchema, | ||
// schemaName, | ||
name: "test", | ||
db, | ||
}) | ||
}) | ||
.catch(console.error) | ||
}, []) | ||
return ( | ||
<div> | ||
Drizzle | ||
<hr /> | ||
<TestChild /> | ||
</div> | ||
) | ||
} | ||
|
||
function TestChild() { | ||
return <div>Test</div> | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import type { MigrationConfig, MigrationMeta } from "drizzle-orm/migrator" | ||
import type { CRSQLite3Database } from "./driver" | ||
import migrationJournal from "shared/drizzle-migrations/meta/_journal.json" | ||
import { migrations } from "shared/drizzle-migrations/index" | ||
|
||
export async function migrate<TSchema extends Record<string, unknown>>( | ||
db: CRSQLite3Database<TSchema>, | ||
config: string | MigrationConfig | ||
) { | ||
const migrations = await getMigrations() | ||
db.dialect.migrate(migrations, db.session, config) | ||
Check failure on line 11 in client/src/components/drizzle/crsqlite/migrator.ts GitHub Actions / 🐙 Test (20.x, 8.x)tsc > 2339
Check failure on line 11 in client/src/components/drizzle/crsqlite/migrator.ts GitHub Actions / 🐙 Test (20.x, 8.x)tsc > 2339
Check failure on line 11 in client/src/components/drizzle/crsqlite/migrator.ts GitHub Actions / 🐙 Test (21.x, 8.x)tsc > 2339
|
||
} | ||
|
||
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] | ||
const result = query.split("--> statement-breakpoint") | ||
migrationQueries.push({ | ||
sql: result, | ||
bps: journalEntry.breakpoints, | ||
folderMillis: journalEntry.when, | ||
hash: await createSha256Hash(query), | ||
}) | ||
} | ||
return migrationQueries | ||
} | ||
|
||
async function createSha256Hash(query: string) { | ||
const encoder = new TextEncoder() | ||
const data = encoder.encode(query) | ||
const hash = await window.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 | ||
} |
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, | ||
}) |