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

wip #50

Open
wants to merge 14 commits into
base: drizzle-orm-adapter
Choose a base branch
from
3 changes: 3 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@
"@typescript-eslint/consistent-type-definitions": "off",
"@typescript-eslint/no-empty-function": "off",
"@typescript-eslint/no-unused-vars": ["error", { "varsIgnorePattern": "^_" }],
"@typescript-eslint/restrict-template-expressions": "off", // good rule in theory, but doesn't work
"@typescript-eslint/restrict-plus-operands": "off", // good rule in theory, but doesn't work
"@typescript-eslint/use-unknown-in-catch-callback-variable": "off",
"no-empty": "off",
"react-refresh/only-export-components": ["error", { "allowConstantExport": true }],
"no-unused-labels": "off",
Expand Down
20 changes: 17 additions & 3 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
"pnpm-lock.yaml": true
},
"files.exclude": {
"**/dist/**": true,
"**/node_modules": true,
// "**/dist/**": true,
// "**/node_modules": true,
"**/.turbo": true,
"client/public/sw.js": true,
"client/public/sw.js.map": true,
Expand All @@ -15,7 +15,7 @@
"**/*.module.css.d.ts": true,
"**/*.sql.d.ts": true,
"**/*.sql.d.ts.map": true,
"**/*.js": true,
// "**/*.js": true,
"**/*.mjs": true,
"**/*.tsbuildinfo": true
},
Expand Down Expand Up @@ -59,6 +59,20 @@
"*.wasm": "${capture}.wasm.*",
"*.sql": "${capture}.sql.d.ts, ${capture}.sql.d.ts.map"
},
"workbench.editor.customLabels.patterns": {
".github/**/action.yml": "${dirname}/action.${extname}",
"**/src/index.*": "${dirname(1)}/src/index.${extname}",
"**/index.*": "${dirname}/index.${extname}",
"**/package.json": "${dirname}/package.json",
"**/tsconfig*.json": "${dirname}/${filename}.json",
"**/.eslintrc.*": "${dirname}/.eslintrc.${extname}",
"**/esbuild.ts": "${dirname}/esbuild.ts",
"**/turbo.json": "${dirname}/turbo.json",
"**/.gitignore": "${dirname}/.gitignore",
"**/.nvmrc": "${dirname}/.nvmrc",
"**/vitest.config.ts": "${dirname}/vitest.config.ts",
"**/vite.config.ts": "${dirname}/vite.config.ts"
},
"github-actions.workflows.pinned.refresh.enabled": true,
"cSpell.spellCheckOnlyWorkspaceFiles": true,
"files.associations": {
Expand Down
2 changes: 1 addition & 1 deletion assets/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"extends": "../.eslintrc.json",
"parserOptions": {
"project": ["tsconfig.json"]
"project": ["tsconfig.lib.json", "tsconfig.tools.json"]
}
}
84 changes: 54 additions & 30 deletions assets/esbuild.ts
Original file line number Diff line number Diff line change
@@ -1,42 +1,67 @@
import chalk from "chalk"
import { watch as chokidar } from "chokidar"
import { writeFile } from "node:fs/promises"
import { basename, dirname, join, relative } from "node:path"
import { createHash } from "node:crypto"
import { type Stats } from "node:fs"
import { writeFile, readFile } from "node:fs/promises"
import { dirname, join } from "node:path"
import { walkFsTree } from "script/walkFsTree"

function dTsTemplate(name: string) {
return `declare const query: string; export = query //# sourceMappingURL=${name}.d.ts.map`
}

function dTsMapTemplate(name: string) {
return `{"version":3,"file":"${name}.d.ts","sourceRoot":"","sources":["${name}"],"names":[],"mappings":";AAAA"}`
}

async function buildMap(path: string) {
const name = basename(path)
const promise = Promise.all([
writeFile(`${path}.d.ts`, dTsTemplate(name)),
writeFile(`${path}.d.ts.map`, dTsMapTemplate(name)),
])
void promise.then(() => {
console.log("Built TS maps for:", path)
})
return promise
function onChange(path: string, stats: Stats | undefined) {
// read all files in the directory
const dir = stats?.isFile() ? dirname(path) : path
readFile(join(dir, "meta", "_journal.json"), "utf8").then((data) => {
const journal = JSON.parse(data) as {
entries: Array<{
idx: number
version: string
when: number
tag: string
breakpoints: boolean
}>
}
Promise.all(
journal.entries.map((entry) =>
readFile(join(dir, entry.tag + ".sql"), "utf8").then((data) => ({
sql: data
.split("--> statement-breakpoint")
.map((s) => s.trim())
.filter(Boolean),
bps: entry.breakpoints,
folderMillis: entry.when,
hash: createHash("sha256").update(data).digest("hex"),
}))
)
).then((migrations) => {
const result = JSON.stringify(migrations, null, 2)
const resultPath = join(dir, "..", "migrations.json")
writeFile(resultPath, result).then(() => {
console.log(chalk.gray("compiled migrations:"), chalk.green(resultPath))
for (const entry of journal.entries) {
console.log(
chalk.gray(" -"),
chalk.white(`${entry.tag}.sql`),
chalk.gray(`#${entry.idx} v${entry.version} @${entry.when}`)
)
}
}, console.error)
}, console.error)
}, console.error)
}

function watch() {
const watcher = chokidar("./src/*.sql", {
const watcher = chokidar("./src/*/migrations/*.sql", {
ignoreInitial: false,
persistent: true,
atomic: true,
awaitWriteFinish: {
stabilityThreshold: 50,
},
})
watcher.on("add", (path, stats) => {
if (stats?.isFile()) {
void buildMap(path)
}
})

watcher.on("add", onChange)
watcher.on("change", onChange)
watcher.on("unlink", onChange)

// eslint-disable-next-line @typescript-eslint/no-misused-promises
process.on("SIGINT", async () => {
console.log("\nStopping assets watcher...")
Expand All @@ -50,10 +75,9 @@ async function build() {
const current = dirname(new URL(import.meta.url).pathname)
const promises: Array<Promise<any>> = []
await walkFsTree(join(current, "src"), ({ path, stats }) => {
if (!path.endsWith(".sql")) return
if (stats.isFile()) {
const rel = relative(current, path)
promises.push(buildMap(rel))
if (!stats.isDirectory()) return
if (path.match(/src\/[^/]+\/migrations$/)) {
onChange(path, stats)
}
})
return promises
Expand Down
4 changes: 3 additions & 1 deletion assets/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
"type": "module",
"scripts": {
"tsc:build": "tsx ./esbuild.ts --build",
"tsc": "tsc --noEmit",
"tsc:lib": "tsc --noEmit --project tsconfig.lib.json",
"tsc:tools": "tsc --noEmit --project tsconfig.tools.json",
"tsc": "echo 'done'",
"dev": "tsx ./esbuild.ts",
"lint": "eslint .",
"lint:fix": "eslint . --fix",
Expand Down
13 changes: 13 additions & 0 deletions assets/src/drizzle-test/drizzle.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { defineConfig } from "drizzle-kit"
import { relative, resolve } from "node:path"

const schema = relative(".", resolve(__dirname, "schema.ts"))
const out = relative(".", resolve(__dirname, "migrations"))

export default defineConfig({
schema,
driver: "better-sqlite",
out,
verbose: true,
strict: true,
})
4 changes: 4 additions & 0 deletions assets/src/drizzle-test/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import * as schema from "./schema"
import migrations from "./migrations.json"

export { schema, migrations }
13 changes: 13 additions & 0 deletions assets/src/drizzle-test/migrations.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[
{
"sql": [
"CREATE TABLE `list` (\n\t`id` text PRIMARY KEY NOT NULL,\n\t`content` text,\n\t`position` text\n);",
"SELECT\n\tcrsql_as_crr ('list');",
"SELECT\n\tcrsql_fract_as_ordered ('list', 'position');",
"CREATE INDEX `positionIdx` ON `list` (`position`);"
],
"bps": true,
"folderMillis": 1711879378057,
"hash": "3b557ab5472a1772140cca199e8f1f61933d42d7dd4c578fb42032f214213036"
}
]
13 changes: 13 additions & 0 deletions assets/src/drizzle-test/migrations/0000_worried_mongu.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
CREATE TABLE `list` (
`id` text PRIMARY KEY NOT NULL,
`content` text,
`position` text
);
--> statement-breakpoint
SELECT
crsql_as_crr ('list');
--> statement-breakpoint
SELECT
crsql_fract_as_ordered ('list', 'position');
--> statement-breakpoint
CREATE INDEX `positionIdx` ON `list` (`position`);
52 changes: 52 additions & 0 deletions assets/src/drizzle-test/migrations/meta/0000_snapshot.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
{
"version": "5",
"dialect": "sqlite",
"id": "822c009f-7a62-472e-bff5-2047825663d0",
"prevId": "00000000-0000-0000-0000-000000000000",
"tables": {
"list": {
"name": "list",
"columns": {
"id": {
"name": "id",
"type": "text",
"primaryKey": true,
"notNull": true,
"autoincrement": false
},
"content": {
"name": "content",
"type": "text",
"primaryKey": false,
"notNull": false,
"autoincrement": false
},
"position": {
"name": "position",
"type": "text",
"primaryKey": false,
"notNull": false,
"autoincrement": false
}
},
"indexes": {
"positionIdx": {
"name": "positionIdx",
"columns": [
"position"
],
"isUnique": false
}
},
"foreignKeys": {},
"compositePrimaryKeys": {},
"uniqueConstraints": {}
}
},
"enums": {},
"_meta": {
"schemas": {},
"tables": {},
"columns": {}
}
}
13 changes: 13 additions & 0 deletions assets/src/drizzle-test/migrations/meta/_journal.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"version": "5",
"dialect": "sqlite",
"entries": [
{
"idx": 0,
"version": "5",
"when": 1711879378057,
"tag": "0000_worried_mongu",
"breakpoints": true
}
]
}
13 changes: 13 additions & 0 deletions assets/src/drizzle-test/schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { sqliteTable, text, index } from "drizzle-orm/sqlite-core"

export const list = sqliteTable(
"list",
{
id: text("id").primaryKey(),
content: text("content"),
position: text("position"),
},
(list) => ({
positionIdx: index("positionIdx").on(list.position),
})
)
1 change: 0 additions & 1 deletion assets/src/faa.sql

This file was deleted.

17 changes: 0 additions & 17 deletions assets/src/test-v0.sql

This file was deleted.

17 changes: 11 additions & 6 deletions assets/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
{
"extends": "../tsconfig.base.json",
"compilerOptions": {
"types": ["node"],
"module": "ESNext",
},
"files": ["esbuild.ts"],
"include": ["../script/src/**/*.ts"],
"compilerOptions": {},
"include": [],
"files": [],
"references": [
{
"path": "./tsconfig.lib.json"
},
{
"path": "./tsconfig.tools.json"
}
]
}
8 changes: 8 additions & 0 deletions assets/tsconfig.lib.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"extends": "../tsconfig.base.json",
"compilerOptions": {
"types": []
},
"exclude": ["**/*.drizzle.config.ts"],
"include": ["src/*/schema.ts", "src/*/index.ts", "types/*", "../types/*.d.ts"]
}
9 changes: 9 additions & 0 deletions assets/tsconfig.tools.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"extends": "../tsconfig.base.json",
"compilerOptions": {
"types": ["node"],
"module": "ESNext"
},
"files": ["esbuild.ts"],
"include": ["../script/src/**/*.ts", "**/*.drizzle.config.ts"]
}
Loading
Loading