From 3464bd240b91d0f859e69fbf94bc62d5770d0a4d Mon Sep 17 00:00:00 2001 From: abearxiong Date: Mon, 26 Jan 2026 03:46:32 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E6=95=B0=E6=8D=AE=E5=BA=93?= =?UTF-8?q?=E9=85=8D=E7=BD=AE=EF=BC=8C=E6=B7=BB=E5=8A=A0=E9=BB=98=E8=AE=A4?= =?UTF-8?q?=E6=97=B6=E9=97=B4=E6=88=B3=EF=BC=8C=E4=BC=98=E5=8C=96=E8=BD=BB?= =?UTF-8?q?=E4=BB=A3=E7=A0=81=E8=B7=AF=E7=94=B1=E5=A4=84=E7=90=86=EF=BC=8C?= =?UTF-8?q?=E6=96=B0=E5=A2=9E=E7=A4=BA=E4=BE=8B=E4=BB=A3=E7=A0=81=E6=96=87?= =?UTF-8?q?=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- drizzle.config.ts | 6 +- src/db/drizzle/schema.ts | 111 ++++++++++++++++---------------- src/routes/light-code/list.ts | 31 ++++++--- src/test/add-demo-light-code.ts | 44 +++++++++++++ src/test/common.ts | 2 + 5 files changed, 127 insertions(+), 67 deletions(-) create mode 100644 src/test/add-demo-light-code.ts diff --git a/drizzle.config.ts b/drizzle.config.ts index 4326af1..b2f3ffa 100644 --- a/drizzle.config.ts +++ b/drizzle.config.ts @@ -1,6 +1,8 @@ +import { useConfig } from '@kevisual/use-config/context'; import type { Config } from 'drizzle-kit'; -import 'dotenv/config'; -const url = process.env.DATABASE_URL!; +const config = useConfig(); +const url = config.DATABASE_URL!; +console.log('Drizzle config using database url:', url); export default { schema: './src/db/schema.ts', diff --git a/src/db/drizzle/schema.ts b/src/db/drizzle/schema.ts index 3e56ca5..4572925 100644 --- a/src/db/drizzle/schema.ts +++ b/src/db/drizzle/schema.ts @@ -10,8 +10,8 @@ export const testPromptTools = pgTable("TestPromptTools", { args: jsonb().notNull(), process: jsonb().notNull(), type: varchar({ length: 255 }).notNull(), - createdAt: timestamp({ withTimezone: true, mode: 'string' }).notNull(), - updatedAt: timestamp({ withTimezone: true, mode: 'string' }).notNull(), + createdAt: timestamp({ withTimezone: true, mode: 'string' }).notNull().defaultNow(), + updatedAt: timestamp({ withTimezone: true, mode: 'string' }).notNull().defaultNow(), }); export const aiAgent = pgTable("ai_agent", { @@ -22,8 +22,8 @@ export const aiAgent = pgTable("ai_agent", { temperature: doublePrecision(), cache: varchar({ length: 255 }), cacheName: varchar({ length: 255 }), - createdAt: timestamp({ withTimezone: true, mode: 'string' }).notNull(), - updatedAt: timestamp({ withTimezone: true, mode: 'string' }).notNull(), + createdAt: timestamp({ withTimezone: true, mode: 'string' }).notNull().defaultNow(), + updatedAt: timestamp({ withTimezone: true, mode: 'string' }).notNull().defaultNow(), model: varchar({ length: 255 }).notNull(), data: json().default({}), status: varchar({ length: 255 }).default('open'), @@ -43,8 +43,8 @@ export const appsTrades = pgTable("apps_trades", { type: varchar({ length: 255 }).default('alipay').notNull(), data: jsonb().default({ "list": [] }), uid: uuid(), - createdAt: timestamp({ withTimezone: true, mode: 'string' }).notNull(), - updatedAt: timestamp({ withTimezone: true, mode: 'string' }).notNull(), + createdAt: timestamp({ withTimezone: true, mode: 'string' }).notNull().defaultNow(), + updatedAt: timestamp({ withTimezone: true, mode: 'string' }).notNull().defaultNow(), deletedAt: timestamp({ withTimezone: true, mode: 'string' }), }, (table) => [ unique("apps_trades_out_trade_no_key").on(table.outTradeNo), @@ -54,8 +54,8 @@ export const cfOrgs = pgTable("cf_orgs", { id: uuid().primaryKey().notNull(), username: varchar({ length: 255 }).notNull(), users: jsonb().default([]), - createdAt: timestamp({ withTimezone: true, mode: 'string' }).notNull(), - updatedAt: timestamp({ withTimezone: true, mode: 'string' }).notNull(), + createdAt: timestamp({ withTimezone: true, mode: 'string' }).notNull().defaultNow(), + updatedAt: timestamp({ withTimezone: true, mode: 'string' }).notNull().defaultNow(), deletedAt: timestamp({ withTimezone: true, mode: 'string' }), description: varchar({ length: 255 }), }, (table) => [ @@ -70,8 +70,8 @@ export const cfRouterCode = pgTable("cf_router_code", { project: varchar({ length: 255 }).default('default'), code: text().default(''), type: enumCfRouterCodeType().default('route'), - createdAt: timestamp({ withTimezone: true, mode: 'string' }).notNull(), - updatedAt: timestamp({ withTimezone: true, mode: 'string' }).notNull(), + createdAt: timestamp({ withTimezone: true, mode: 'string' }).notNull().defaultNow(), + updatedAt: timestamp({ withTimezone: true, mode: 'string' }).notNull().defaultNow(), middleware: varchar({ length: 255 }).array().default(["RRAY[]::character varying[])::character varying(25"]), next: varchar({ length: 255 }).default(''), exec: text().default(''), @@ -86,8 +86,8 @@ export const cfUser = pgTable("cf_user", { password: varchar({ length: 255 }), salt: varchar({ length: 255 }), needChangePassword: boolean().default(false), - createdAt: timestamp({ withTimezone: true, mode: 'string' }).notNull(), - updatedAt: timestamp({ withTimezone: true, mode: 'string' }).notNull(), + createdAt: timestamp({ withTimezone: true, mode: 'string' }).notNull().defaultNow(), + updatedAt: timestamp({ withTimezone: true, mode: 'string' }).notNull().defaultNow(), description: text(), data: jsonb().default({}), deletedAt: timestamp({ withTimezone: true, mode: 'string' }), @@ -111,8 +111,8 @@ export const cfUserSecrets = pgTable("cf_user_secrets", { userId: uuid(), data: jsonb().default({}), orgId: uuid(), - createdAt: timestamp({ withTimezone: true, mode: 'string' }).notNull(), - updatedAt: timestamp({ withTimezone: true, mode: 'string' }).notNull(), + createdAt: timestamp({ withTimezone: true, mode: 'string' }).notNull().defaultNow(), + updatedAt: timestamp({ withTimezone: true, mode: 'string' }).notNull().defaultNow(), }); export const chatHistories = pgTable("chat_histories", { @@ -123,8 +123,8 @@ export const chatHistories = pgTable("chat_histories", { root: boolean().default(false), show: boolean().default(true), uid: varchar({ length: 255 }), - createdAt: timestamp({ withTimezone: true, mode: 'string' }).notNull(), - updatedAt: timestamp({ withTimezone: true, mode: 'string' }).notNull(), + createdAt: timestamp({ withTimezone: true, mode: 'string' }).notNull().defaultNow(), + updatedAt: timestamp({ withTimezone: true, mode: 'string' }).notNull().defaultNow(), role: varchar({ length: 255 }).default('user'), }); @@ -135,8 +135,8 @@ export const chatPrompts = pgTable("chat_prompts", { data: json(), key: varchar({ length: 255 }).default('').notNull(), uid: varchar({ length: 255 }), - createdAt: timestamp({ withTimezone: true, mode: 'string' }).notNull(), - updatedAt: timestamp({ withTimezone: true, mode: 'string' }).notNull(), + createdAt: timestamp({ withTimezone: true, mode: 'string' }).notNull().defaultNow(), + updatedAt: timestamp({ withTimezone: true, mode: 'string' }).notNull().defaultNow(), deletedAt: timestamp({ withTimezone: true, mode: 'string' }), }); @@ -146,8 +146,8 @@ export const chatSessions = pgTable("chat_sessions", { chatPromptId: uuid(), type: varchar({ length: 255 }).default('production'), uid: varchar({ length: 255 }), - createdAt: timestamp({ withTimezone: true, mode: 'string' }).notNull(), - updatedAt: timestamp({ withTimezone: true, mode: 'string' }).notNull(), + createdAt: timestamp({ withTimezone: true, mode: 'string' }).notNull().defaultNow(), + updatedAt: timestamp({ withTimezone: true, mode: 'string' }).notNull().defaultNow(), title: varchar({ length: 255 }).default(''), key: varchar({ length: 255 }), }); @@ -159,8 +159,8 @@ export const fileSync = pgTable("file_sync", { stat: jsonb().default({}), data: jsonb().default({}), checkedAt: timestamp({ withTimezone: true, mode: 'string' }), - createdAt: timestamp({ withTimezone: true, mode: 'string' }).notNull(), - updatedAt: timestamp({ withTimezone: true, mode: 'string' }).notNull(), + createdAt: timestamp({ withTimezone: true, mode: 'string' }).notNull().defaultNow(), + updatedAt: timestamp({ withTimezone: true, mode: 'string' }).notNull().defaultNow(), }, (table) => [ index("file_sync_name_idx").using("btree", table.name.asc().nullsLast()), ]); @@ -177,8 +177,8 @@ export const kvAiChatHistory = pgTable("kv_ai_chat_history", { completionTokens: integer("completion_tokens").default(0), data: jsonb().default({}), uid: uuid(), - createdAt: timestamp({ withTimezone: true, mode: 'string' }).notNull(), - updatedAt: timestamp({ withTimezone: true, mode: 'string' }).notNull(), + createdAt: timestamp({ withTimezone: true, mode: 'string' }).notNull().defaultNow(), + updatedAt: timestamp({ withTimezone: true, mode: 'string' }).notNull().defaultNow(), version: integer().default(0), type: varchar({ length: 255 }).default('keep').notNull(), }); @@ -189,8 +189,8 @@ export const kvApp = pgTable("kv_app", { version: varchar({ length: 255 }).default(''), key: varchar({ length: 255 }), uid: uuid(), - createdAt: timestamp({ withTimezone: true, mode: 'string' }).notNull(), - updatedAt: timestamp({ withTimezone: true, mode: 'string' }).notNull(), + createdAt: timestamp({ withTimezone: true, mode: 'string' }).notNull().defaultNow(), + updatedAt: timestamp({ withTimezone: true, mode: 'string' }).notNull().defaultNow(), deletedAt: timestamp({ withTimezone: true, mode: 'string' }), title: varchar({ length: 255 }).default(''), description: varchar({ length: 255 }).default(''), @@ -208,8 +208,8 @@ export const kvAppDomain = pgTable("kv_app_domain", { domain: varchar({ length: 255 }).notNull(), appId: varchar({ length: 255 }), uid: varchar({ length: 255 }), - createdAt: timestamp({ withTimezone: true, mode: 'string' }).notNull(), - updatedAt: timestamp({ withTimezone: true, mode: 'string' }).notNull(), + createdAt: timestamp({ withTimezone: true, mode: 'string' }).notNull().defaultNow(), + updatedAt: timestamp({ withTimezone: true, mode: 'string' }).notNull().defaultNow(), deletedAt: timestamp({ withTimezone: true, mode: 'string' }), data: jsonb(), status: varchar({ length: 255 }).default('running').notNull(), @@ -222,8 +222,8 @@ export const kvAppList = pgTable("kv_app_list", { data: json().default({}), version: varchar({ length: 255 }).default(''), uid: uuid(), - createdAt: timestamp({ withTimezone: true, mode: 'string' }).notNull(), - updatedAt: timestamp({ withTimezone: true, mode: 'string' }).notNull(), + createdAt: timestamp({ withTimezone: true, mode: 'string' }).notNull().defaultNow(), + updatedAt: timestamp({ withTimezone: true, mode: 'string' }).notNull().defaultNow(), deletedAt: timestamp({ withTimezone: true, mode: 'string' }), key: varchar({ length: 255 }), status: varchar({ length: 255 }).default('running'), @@ -237,24 +237,23 @@ export const kvConfig = pgTable("kv_config", { tags: jsonb().default([]), data: jsonb().default({}), uid: uuid(), - createdAt: timestamp({ withTimezone: true, mode: 'string' }).notNull(), - updatedAt: timestamp({ withTimezone: true, mode: 'string' }).notNull(), + createdAt: timestamp({ withTimezone: true, mode: 'string' }).notNull().defaultNow(), + updatedAt: timestamp({ withTimezone: true, mode: 'string' }).notNull().defaultNow(), deletedAt: timestamp({ withTimezone: true, mode: 'string' }), hash: text().default(''), }); -export const kvContainer = pgTable("kv_container", { - id: uuid().primaryKey().notNull(), +export const kvContainer = pgTable("kv_light_code", { + id: uuid().primaryKey().notNull().defaultRandom(), title: text().default(''), description: text().default(''), - type: varchar({ length: 255 }).default('render-js'), + type: text().default('render-js'), code: text().default(''), - data: json().default({}), - createdAt: timestamp({ withTimezone: true, mode: 'string' }).notNull(), - updatedAt: timestamp({ withTimezone: true, mode: 'string' }).notNull(), + data: jsonb().default({}), + createdAt: timestamp('createdAt').notNull().defaultNow(), + updatedAt: timestamp('updatedAt').notNull().defaultNow(), uid: uuid(), - tags: json().default([]), - deletedAt: timestamp({ withTimezone: true, mode: 'string' }), + tags: jsonb().default([]), hash: text().default(''), }); @@ -263,8 +262,8 @@ export const kvGithub = pgTable("kv_github", { title: varchar({ length: 255 }).default(''), githubToken: varchar({ length: 255 }).default(''), uid: uuid(), - createdAt: timestamp({ withTimezone: true, mode: 'string' }).notNull(), - updatedAt: timestamp({ withTimezone: true, mode: 'string' }).notNull(), + createdAt: timestamp({ withTimezone: true, mode: 'string' }).notNull().defaultNow(), + updatedAt: timestamp({ withTimezone: true, mode: 'string' }).notNull().defaultNow(), deletedAt: timestamp({ withTimezone: true, mode: 'string' }), }); @@ -277,8 +276,8 @@ export const kvPackages = pgTable("kv_packages", { publish: jsonb().default({}), expand: jsonb().default({}), uid: uuid(), - createdAt: timestamp({ withTimezone: true, mode: 'string' }).notNull(), - updatedAt: timestamp({ withTimezone: true, mode: 'string' }).notNull(), + createdAt: timestamp({ withTimezone: true, mode: 'string' }).notNull().defaultNow(), + updatedAt: timestamp({ withTimezone: true, mode: 'string' }).notNull().defaultNow(), deletedAt: timestamp({ withTimezone: true, mode: 'string' }), }); @@ -289,8 +288,8 @@ export const kvPage = pgTable("kv_page", { type: varchar({ length: 255 }).default(''), data: json().default({}), uid: uuid(), - createdAt: timestamp({ withTimezone: true, mode: 'string' }).notNull(), - updatedAt: timestamp({ withTimezone: true, mode: 'string' }).notNull(), + createdAt: timestamp({ withTimezone: true, mode: 'string' }).notNull().defaultNow(), + updatedAt: timestamp({ withTimezone: true, mode: 'string' }).notNull().defaultNow(), deletedAt: timestamp({ withTimezone: true, mode: 'string' }), publish: json().default({}), }); @@ -304,8 +303,8 @@ export const kvResource = pgTable("kv_resource", { version: varchar({ length: 255 }).default('0.0.0'), data: json().default({}), uid: uuid(), - createdAt: timestamp({ withTimezone: true, mode: 'string' }).notNull(), - updatedAt: timestamp({ withTimezone: true, mode: 'string' }).notNull(), + createdAt: timestamp({ withTimezone: true, mode: 'string' }).notNull().defaultNow(), + updatedAt: timestamp({ withTimezone: true, mode: 'string' }).notNull().defaultNow(), deletedAt: timestamp({ withTimezone: true, mode: 'string' }), }); @@ -317,8 +316,8 @@ export const kvVip = pgTable("kv_vip", { startDate: timestamp({ withTimezone: true, mode: 'string' }), endDate: timestamp({ withTimezone: true, mode: 'string' }), data: jsonb().default({}), - createdAt: timestamp({ withTimezone: true, mode: 'string' }).notNull(), - updatedAt: timestamp({ withTimezone: true, mode: 'string' }).notNull(), + createdAt: timestamp({ withTimezone: true, mode: 'string' }).notNull().defaultNow(), + updatedAt: timestamp({ withTimezone: true, mode: 'string' }).notNull().defaultNow(), deletedAt: timestamp({ withTimezone: true, mode: 'string' }), title: text().default('').notNull(), description: text().default('').notNull(), @@ -335,8 +334,8 @@ export const microAppsUpload = pgTable("micro_apps_upload", { share: boolean().default(false), uname: varchar({ length: 255 }).default(''), uid: uuid(), - createdAt: timestamp({ withTimezone: true, mode: 'string' }).notNull(), - updatedAt: timestamp({ withTimezone: true, mode: 'string' }).notNull(), + createdAt: timestamp({ withTimezone: true, mode: 'string' }).notNull().defaultNow(), + updatedAt: timestamp({ withTimezone: true, mode: 'string' }).notNull().defaultNow(), }); export const microMark = pgTable("micro_mark", { @@ -347,8 +346,8 @@ export const microMark = pgTable("micro_mark", { data: jsonb().default({}), uname: varchar({ length: 255 }).default(''), uid: uuid(), - createdAt: timestamp({ withTimezone: true, mode: 'string' }).notNull(), - updatedAt: timestamp({ withTimezone: true, mode: 'string' }).notNull(), + createdAt: timestamp({ withTimezone: true, mode: 'string' }).notNull().defaultNow(), + updatedAt: timestamp({ withTimezone: true, mode: 'string' }).notNull().defaultNow(), cover: text().default(''), thumbnail: text().default(''), link: text().default(''), @@ -380,8 +379,8 @@ export const workShareMark = pgTable("work_share_mark", { markedAt: timestamp({ withTimezone: true, mode: 'string' }), uid: uuid(), puid: uuid(), - createdAt: timestamp({ withTimezone: true, mode: 'string' }).notNull(), - updatedAt: timestamp({ withTimezone: true, mode: 'string' }).notNull(), + createdAt: timestamp({ withTimezone: true, mode: 'string' }).notNull().defaultNow(), + updatedAt: timestamp({ withTimezone: true, mode: 'string' }).notNull().defaultNow(), deletedAt: timestamp({ withTimezone: true, mode: 'string' }), }); diff --git a/src/routes/light-code/list.ts b/src/routes/light-code/list.ts index 5983bcd..747fbf8 100644 --- a/src/routes/light-code/list.ts +++ b/src/routes/light-code/list.ts @@ -81,6 +81,7 @@ app path: 'light-code', key: 'update', middleware: ['auth'], + isDebug: true, }) .define(async (ctx) => { const tokenUser = ctx.state.tokenUser; @@ -111,16 +112,28 @@ app ctx.body = null; } } else { - const [created] = await db - .insert(schema.kvContainer) - .values({ - ...container, - uid: tokenUser.id, - }) - .returning(); - ctx.body = created; + try { + + console.log('created', container, 'userId', tokenUser.id); + const [created] = await db + .insert(schema.kvContainer) + .values({ + title: container.title || '', + description: container.description || '', + type: container.type || 'light-code', + code: container.code || '', + data: container.data || {}, + tags: container.tags || [], + hash: container.hash || '', + uid: tokenUser.id, + }) + .returning(); + ctx.body = created; + } catch (error) { + console.error('Error creating container:', error); + throw error; + } } - return ctx; }) .addTo(app); diff --git a/src/test/add-demo-light-code.ts b/src/test/add-demo-light-code.ts new file mode 100644 index 0000000..3f3bd71 --- /dev/null +++ b/src/test/add-demo-light-code.ts @@ -0,0 +1,44 @@ +import { app, showMore, showRes } from './common.ts'; +import crypto from 'node:crypto'; + +export const getStringHash = (str: string) => { + return crypto.createHash('md5').update(str).digest('hex'); +} +const code = `// 这是一个示例代码文件 +import {App} from '@kevisual/router'; + +const app = new App(); + +app.route({ + path: 'hello', + description: 'LightCode 示例路由', + metadata: { + tags: ['light-code', 'example'], + }, +}).define(async (ctx) => { + console.log('tokenUser:', ctx.query?.tokenUser); + ctx.body = 'Hello from LightCode!'; +}).addTo(app); + +app.wait();` +const code2 = `const a = 1` + +const res = await app.run({ + path: 'light-code', + key: 'update', + payload: { + data: { + title: 'Demo Light Code', + description: '这是一个演示用的轻代码项目,包含一个简单的路由示例。', + type: 'light-code', + tags: ['demo', 'light-code'], + data: {}, + code: code, + hash: getStringHash(code), + }, + token: "st_idht7xpffhgu2eeh94zd8ze1t7ew3amy", + }, + +}) + +console.log('showMore', showMore(res)); diff --git a/src/test/common.ts b/src/test/common.ts index b556ad9..822c458 100644 --- a/src/test/common.ts +++ b/src/test/common.ts @@ -3,7 +3,9 @@ import '@/route.ts'; import { useConfig, useContextKey } from '@kevisual/context'; import { Query } from '@kevisual/query'; import util from 'node:util'; +import dotenv from 'dotenv'; +dotenv.config(); export { app, useContextKey