forked from imbytecat/fullstack-starter
refactor(db): 适配 drizzle-orm 0.x API 并引入 drizzle-zod
drizzle-orm 从 1.0 beta 降级到 0.45 后,1.0 的 defineRelations、drizzle-orm/zod 子路径以及 RQB v2 的 orderBy 对象语法均不可用。改用 schema 作为 drizzle() 入参、从独立的 drizzle-zod 包导入 schema 生成器,并将 orderBy 改回 0.x 的 回调写法。同时删除因降级而失效的旧迁移。
This commit is contained in:
@@ -17,6 +17,7 @@
|
|||||||
"@tanstack/react-router-ssr-query": "^1.166.11",
|
"@tanstack/react-router-ssr-query": "^1.166.11",
|
||||||
"@tanstack/react-start": "^1.167.43",
|
"@tanstack/react-start": "^1.167.43",
|
||||||
"drizzle-orm": "0.45.2",
|
"drizzle-orm": "0.45.2",
|
||||||
|
"drizzle-zod": "^0.8.3",
|
||||||
"postgres": "^3.4.9",
|
"postgres": "^3.4.9",
|
||||||
"react": "^19.2.5",
|
"react": "^19.2.5",
|
||||||
"react-dom": "^19.2.5",
|
"react-dom": "^19.2.5",
|
||||||
@@ -445,6 +446,8 @@
|
|||||||
|
|
||||||
"drizzle-orm": ["drizzle-orm@0.45.2", "", { "peerDependencies": { "@aws-sdk/client-rds-data": ">=3", "@cloudflare/workers-types": ">=4", "@electric-sql/pglite": ">=0.2.0", "@libsql/client": ">=0.10.0", "@libsql/client-wasm": ">=0.10.0", "@neondatabase/serverless": ">=0.10.0", "@op-engineering/op-sqlite": ">=2", "@opentelemetry/api": "^1.4.1", "@planetscale/database": ">=1.13", "@prisma/client": "*", "@tidbcloud/serverless": "*", "@types/better-sqlite3": "*", "@types/pg": "*", "@types/sql.js": "*", "@upstash/redis": ">=1.34.7", "@vercel/postgres": ">=0.8.0", "@xata.io/client": "*", "better-sqlite3": ">=7", "bun-types": "*", "expo-sqlite": ">=14.0.0", "gel": ">=2", "knex": "*", "kysely": "*", "mysql2": ">=2", "pg": ">=8", "postgres": ">=3", "sql.js": ">=1", "sqlite3": ">=5" }, "optionalPeers": ["@aws-sdk/client-rds-data", "@cloudflare/workers-types", "@electric-sql/pglite", "@libsql/client", "@libsql/client-wasm", "@neondatabase/serverless", "@op-engineering/op-sqlite", "@opentelemetry/api", "@planetscale/database", "@prisma/client", "@tidbcloud/serverless", "@types/better-sqlite3", "@types/pg", "@types/sql.js", "@upstash/redis", "@vercel/postgres", "@xata.io/client", "better-sqlite3", "bun-types", "expo-sqlite", "gel", "knex", "kysely", "mysql2", "pg", "postgres", "sql.js", "sqlite3"] }, "sha512-kY0BSaTNYWnoDMVoyY8uxmyHjpJW1geOmBMdSSicKo9CIIWkSxMIj2rkeSR51b8KAPB7m+qysjuHme5nKP+E5Q=="],
|
"drizzle-orm": ["drizzle-orm@0.45.2", "", { "peerDependencies": { "@aws-sdk/client-rds-data": ">=3", "@cloudflare/workers-types": ">=4", "@electric-sql/pglite": ">=0.2.0", "@libsql/client": ">=0.10.0", "@libsql/client-wasm": ">=0.10.0", "@neondatabase/serverless": ">=0.10.0", "@op-engineering/op-sqlite": ">=2", "@opentelemetry/api": "^1.4.1", "@planetscale/database": ">=1.13", "@prisma/client": "*", "@tidbcloud/serverless": "*", "@types/better-sqlite3": "*", "@types/pg": "*", "@types/sql.js": "*", "@upstash/redis": ">=1.34.7", "@vercel/postgres": ">=0.8.0", "@xata.io/client": "*", "better-sqlite3": ">=7", "bun-types": "*", "expo-sqlite": ">=14.0.0", "gel": ">=2", "knex": "*", "kysely": "*", "mysql2": ">=2", "pg": ">=8", "postgres": ">=3", "sql.js": ">=1", "sqlite3": ">=5" }, "optionalPeers": ["@aws-sdk/client-rds-data", "@cloudflare/workers-types", "@electric-sql/pglite", "@libsql/client", "@libsql/client-wasm", "@neondatabase/serverless", "@op-engineering/op-sqlite", "@opentelemetry/api", "@planetscale/database", "@prisma/client", "@tidbcloud/serverless", "@types/better-sqlite3", "@types/pg", "@types/sql.js", "@upstash/redis", "@vercel/postgres", "@xata.io/client", "better-sqlite3", "bun-types", "expo-sqlite", "gel", "knex", "kysely", "mysql2", "pg", "postgres", "sql.js", "sqlite3"] }, "sha512-kY0BSaTNYWnoDMVoyY8uxmyHjpJW1geOmBMdSSicKo9CIIWkSxMIj2rkeSR51b8KAPB7m+qysjuHme5nKP+E5Q=="],
|
||||||
|
|
||||||
|
"drizzle-zod": ["drizzle-zod@0.8.3", "", { "peerDependencies": { "drizzle-orm": ">=0.36.0", "zod": "^3.25.0 || ^4.0.0" } }, "sha512-66yVOuvGhKJnTdiqj1/Xaaz9/qzOdRJADpDa68enqS6g3t0kpNkwNYjUuaeXgZfO/UWuIM9HIhSlJ6C5ZraMww=="],
|
||||||
|
|
||||||
"electron-to-chromium": ["electron-to-chromium@1.5.344", "", {}, "sha512-4MxfbmNDm+KPh066EZy+eUnkcDPcZ35wNmOWzFuh/ijvHsve6kbLTLURy88uCNK5FbpN+yk2nQY6BYh1GEt+wg=="],
|
"electron-to-chromium": ["electron-to-chromium@1.5.344", "", {}, "sha512-4MxfbmNDm+KPh066EZy+eUnkcDPcZ35wNmOWzFuh/ijvHsve6kbLTLURy88uCNK5FbpN+yk2nQY6BYh1GEt+wg=="],
|
||||||
|
|
||||||
"encoding-sniffer": ["encoding-sniffer@0.2.1", "", { "dependencies": { "iconv-lite": "^0.6.3", "whatwg-encoding": "^3.1.1" } }, "sha512-5gvq20T6vfpekVtqrYQsSCFZ1wEg5+wW0/QaZMWkFr6BqD3NfKs0rLCx4rrVlSWJeZb5NBJgVLswK/w2MWU+Gw=="],
|
"encoding-sniffer": ["encoding-sniffer@0.2.1", "", { "dependencies": { "iconv-lite": "^0.6.3", "whatwg-encoding": "^3.1.1" } }, "sha512-5gvq20T6vfpekVtqrYQsSCFZ1wEg5+wW0/QaZMWkFr6BqD3NfKs0rLCx4rrVlSWJeZb5NBJgVLswK/w2MWU+Gw=="],
|
||||||
|
|||||||
@@ -1,7 +0,0 @@
|
|||||||
CREATE TABLE "todo" (
|
|
||||||
"id" uuid PRIMARY KEY,
|
|
||||||
"created_at" timestamp with time zone DEFAULT now() NOT NULL,
|
|
||||||
"updated_at" timestamp with time zone DEFAULT now() NOT NULL,
|
|
||||||
"title" text NOT NULL,
|
|
||||||
"completed" boolean DEFAULT false NOT NULL
|
|
||||||
);
|
|
||||||
@@ -1,88 +0,0 @@
|
|||||||
{
|
|
||||||
"version": "8",
|
|
||||||
"dialect": "postgres",
|
|
||||||
"id": "91ea16cc-3353-493c-bc2c-4fc9dea2fce7",
|
|
||||||
"prevIds": ["00000000-0000-0000-0000-000000000000"],
|
|
||||||
"ddl": [
|
|
||||||
{
|
|
||||||
"isRlsEnabled": false,
|
|
||||||
"name": "todo",
|
|
||||||
"entityType": "tables",
|
|
||||||
"schema": "public"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"type": "uuid",
|
|
||||||
"typeSchema": null,
|
|
||||||
"notNull": true,
|
|
||||||
"dimensions": 0,
|
|
||||||
"default": null,
|
|
||||||
"generated": null,
|
|
||||||
"identity": null,
|
|
||||||
"name": "id",
|
|
||||||
"entityType": "columns",
|
|
||||||
"schema": "public",
|
|
||||||
"table": "todo"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"type": "timestamp with time zone",
|
|
||||||
"typeSchema": null,
|
|
||||||
"notNull": true,
|
|
||||||
"dimensions": 0,
|
|
||||||
"default": "now()",
|
|
||||||
"generated": null,
|
|
||||||
"identity": null,
|
|
||||||
"name": "created_at",
|
|
||||||
"entityType": "columns",
|
|
||||||
"schema": "public",
|
|
||||||
"table": "todo"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"type": "timestamp with time zone",
|
|
||||||
"typeSchema": null,
|
|
||||||
"notNull": true,
|
|
||||||
"dimensions": 0,
|
|
||||||
"default": "now()",
|
|
||||||
"generated": null,
|
|
||||||
"identity": null,
|
|
||||||
"name": "updated_at",
|
|
||||||
"entityType": "columns",
|
|
||||||
"schema": "public",
|
|
||||||
"table": "todo"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"type": "text",
|
|
||||||
"typeSchema": null,
|
|
||||||
"notNull": true,
|
|
||||||
"dimensions": 0,
|
|
||||||
"default": null,
|
|
||||||
"generated": null,
|
|
||||||
"identity": null,
|
|
||||||
"name": "title",
|
|
||||||
"entityType": "columns",
|
|
||||||
"schema": "public",
|
|
||||||
"table": "todo"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"type": "boolean",
|
|
||||||
"typeSchema": null,
|
|
||||||
"notNull": true,
|
|
||||||
"dimensions": 0,
|
|
||||||
"default": "false",
|
|
||||||
"generated": null,
|
|
||||||
"identity": null,
|
|
||||||
"name": "completed",
|
|
||||||
"entityType": "columns",
|
|
||||||
"schema": "public",
|
|
||||||
"table": "todo"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"columns": ["id"],
|
|
||||||
"nameExplicit": false,
|
|
||||||
"name": "todo_pkey",
|
|
||||||
"schema": "public",
|
|
||||||
"table": "todo",
|
|
||||||
"entityType": "pks"
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"renames": []
|
|
||||||
}
|
|
||||||
@@ -35,6 +35,7 @@
|
|||||||
"@tanstack/react-router-ssr-query": "^1.166.11",
|
"@tanstack/react-router-ssr-query": "^1.166.11",
|
||||||
"@tanstack/react-start": "^1.167.43",
|
"@tanstack/react-start": "^1.167.43",
|
||||||
"drizzle-orm": "0.45.2",
|
"drizzle-orm": "0.45.2",
|
||||||
|
"drizzle-zod": "^0.8.3",
|
||||||
"postgres": "^3.4.9",
|
"postgres": "^3.4.9",
|
||||||
"react": "^19.2.5",
|
"react": "^19.2.5",
|
||||||
"react-dom": "^19.2.5",
|
"react-dom": "^19.2.5",
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import { oc } from '@orpc/contract'
|
import { oc } from '@orpc/contract'
|
||||||
import { createInsertSchema, createSelectSchema, createUpdateSchema } from 'drizzle-orm/zod'
|
import { createInsertSchema, createSelectSchema, createUpdateSchema } from 'drizzle-zod'
|
||||||
import { z } from 'zod'
|
import { z } from 'zod'
|
||||||
import { generatedFieldKeys } from '@/server/db/fields'
|
import { generatedFieldKeys } from '@/server/db/fields'
|
||||||
import { todoTable } from '@/server/db/schema'
|
import { todoTable } from '@/server/db/schema'
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ import { todoTable } from '@/server/db/schema'
|
|||||||
|
|
||||||
export const list = os.todo.list.use(db).handler(async ({ context }) => {
|
export const list = os.todo.list.use(db).handler(async ({ context }) => {
|
||||||
const todos = await context.db.query.todoTable.findMany({
|
const todos = await context.db.query.todoTable.findMany({
|
||||||
orderBy: { createdAt: 'desc' },
|
orderBy: (table, { desc }) => desc(table.createdAt),
|
||||||
})
|
})
|
||||||
return todos
|
return todos
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -1,11 +1,11 @@
|
|||||||
import { drizzle } from 'drizzle-orm/postgres-js'
|
import { drizzle } from 'drizzle-orm/postgres-js'
|
||||||
import { env } from '@/env'
|
import { env } from '@/env'
|
||||||
import { relations } from '@/server/db/relations'
|
import * as schema from '@/server/db/schema'
|
||||||
|
|
||||||
export const createDB = () =>
|
export const createDB = () =>
|
||||||
drizzle({
|
drizzle({
|
||||||
connection: env.DATABASE_URL,
|
connection: env.DATABASE_URL,
|
||||||
relations,
|
schema,
|
||||||
})
|
})
|
||||||
|
|
||||||
export type DB = ReturnType<typeof createDB>
|
export type DB = ReturnType<typeof createDB>
|
||||||
|
|||||||
@@ -1,4 +0,0 @@
|
|||||||
import { defineRelations } from 'drizzle-orm'
|
|
||||||
import * as schema from './schema'
|
|
||||||
|
|
||||||
export const relations = defineRelations(schema, (_r) => ({}))
|
|
||||||
Reference in New Issue
Block a user