From 63ae48a592690e9b649c8d0eff4422bdc309f532 Mon Sep 17 00:00:00 2001 From: abearxiong Date: Fri, 12 Dec 2025 11:50:30 +0800 Subject: [PATCH] updates --- auto/callback/index.ts | 21 ++++++ auto/common/base-table.ts | 136 ++++++++++++++++++++++++++++++++++++++ auto/common/core.ts | 99 +++++++++++++++++++++++++++ auto/common/index.ts | 2 + auto/control/index.ts | 7 ++ auto/index.ts | 19 ++++++ auto/life/index.ts | 7 ++ mod.ts | 1 + package.json | 5 +- src/meta/tables.ts | 5 ++ src/record.ts | 15 +++++ test/list-table.ts | 13 ++-- 12 files changed, 324 insertions(+), 6 deletions(-) create mode 100644 auto/callback/index.ts create mode 100644 auto/common/base-table.ts create mode 100644 auto/common/core.ts create mode 100644 auto/common/index.ts create mode 100644 auto/control/index.ts create mode 100644 auto/index.ts create mode 100644 auto/life/index.ts create mode 100644 mod.ts diff --git a/auto/callback/index.ts b/auto/callback/index.ts new file mode 100644 index 0000000..e00c49f --- /dev/null +++ b/auto/callback/index.ts @@ -0,0 +1,21 @@ + +export type BaseNocoItem = { + Id: number, + CreatedAt: string + UpdatedAt: string +} & T; +export type NocoWehookPayload = { + /** 请求id */ + id: string; + type: "records.after.trigger"; + /** + * 多维表base id + **/ + base_id: string; + version: "v3", + data: { + table_id: string; + table_name: string; + rows: BaseNocoItem[]; + } +} \ No newline at end of file diff --git a/auto/common/base-table.ts b/auto/common/base-table.ts new file mode 100644 index 0000000..2b82c19 --- /dev/null +++ b/auto/common/base-table.ts @@ -0,0 +1,136 @@ +import { CreateColumnData } from "../../src/index.ts" +export const columns: CreateColumnData[] = [ + { + title: 'Id', + uidt: "ID", + pk: true, + pv: true, + }, + { + + title: '标题', + uidt: 'SingleLineText', + description: '简单的标题, 最简单的介绍', + }, + { + title: '标签', + uidt: 'MultiSelect', + description: '标签分类,对每一条数据的标签定义,快速分类和筛选', + }, + { + title: '总结', + uidt: 'LongText', + description: '概览性总结', + }, + { + title: '描述', + uidt: 'LongText', + description: '长文本描述', + }, + { + title: '数据', + uidt: 'JSON', + description: '扩列数据,存储更多的自定义信息', + }, + { + title: '链接', + uidt: 'URL', + description: '快速跳转链接,默认为空,比如我这里是一个人生日程的链接,在外部打开', + }, + { + title: '类型', + uidt: 'SingleSelect', + description: '任务类型:备忘和其他,如果是备忘,只做记录,如果是其他的,属于任务管理,到达对应的时间,进行任务提醒,归档是自己不再查询。', + cdf: '备忘', + colOptions: { + // 每日,每周,每月,每年,一次性,备忘,归档,智能 + options: [ + { + title: '每日', + }, + { + title: '每周', + }, + { + title: '每月', + }, + { + title: '每年', + }, + { + title: '每年农历', + }, + { + title: '备忘', + }, + { + title: '归档', + }, + { + title: '智能', + }, + ] + }, + }, + { + title: '启动时间', + description: '任务启动的时间点, 下次启动的时间点。到达当天,显示当天的任务,然后如果执行了,如果是循环周期任务,更新下次启动时间。', + uidt: 'DateTime', + }, + { + title: '任务', + uidt: 'MultiSelect', + description: '任务状态,如果是任务,需要判断运行还是非运行中', + cdf: '非任务', + colOptions: { + // 非任务, 运行中,已停止,个人计划,已完成 + options: [ + { + title: '非任务', + }, + { + title: '运行中', + }, + { + title: '已停止', + }, + { + title: '个人计划', + }, + { + title: '已完成', + }, + { + title: 'AI自动化' + } + ] + }, + }, + { + title: '任务结果', + description: '任务结果描述, 执行后回馈', + uidt: 'LongText' + }, + { + title: '提示词', + uidt: 'LongText', + description: '和AI交互时候简单的实时提示词', + }] + + +export type ColumnItem = { + 'Id': number, + 'CreatedAt': string, + 'UpdatedAt': string, + "标题": string, + "标签"?: string, + "总结"?: string, + "描述"?: string, + "数据"?: string, + "链接"?: string, + "类型"?: string, + "启动时间"?: string, + "任务"?: "非任务" | "运行中" | "已停止" | "个人计划" | "已完成" | "AI自动化", + "任务结果"?: string, + "提示词"?: string, +} & T; \ No newline at end of file diff --git a/auto/common/core.ts b/auto/common/core.ts new file mode 100644 index 0000000..0d6b57c --- /dev/null +++ b/auto/common/core.ts @@ -0,0 +1,99 @@ +import { NocoApi } from "@kevisual/noco"; +import { columns } from "../common/base-table.ts"; +import { ColumnItem } from "./base-table.ts"; +type ReponseData = { + code: number, + message?: string, + data?: T +} +export type CoreOptions = { + nocoApi: NocoApi, + baseId?: string +} & T + +export class Core { + nocoApi: NocoApi; + #baseId?: string; + key = 'core'; + title = '默认表'; + description = '默认表描述'; + #tableId?: string; + constructor(opts: { + nocoApi: NocoApi, + baseId?: string, + tableId?: string + }) { + this.nocoApi = opts.nocoApi; + this.baseId = opts.baseId; + this.tableId = opts.tableId; + } + get tableId() { + return this.#tableId; + } + set tableId(id: string | undefined) { + this.#tableId = id; + if (this.nocoApi.record && id) { + this.nocoApi.record.table = id; + } + } + get baseId() { + return this.#baseId; + } + set baseId(id: string | undefined) { + this.#baseId = id; + } + async createTable(opts?: { columns?: any[], title?: string, description?: string, baseId?: string }): Promise> { + const baseId = opts?.baseId ?? this.baseId!; + const title = opts?.title ?? this.title; + const description = opts?.description ?? this.description; + const _columns = opts?.columns ?? columns; + let tableId = ''; + const res = await this.nocoApi.meta.tables.createTable(baseId, { + title, + description, + columns: _columns, + }) + let code = 200; + if (res.code !== 200) { + const res = await this.nocoApi.meta.tables.list(baseId); + const list = res.data?.list || []; + const existTable = list.find(t => t.title === title); + if (existTable) { + tableId = existTable.id; + } else { + return { + code: res.code, + message: `创建表失败,且未找到同名表`, + } + } + } else { + tableId = res?.data?.id; + } + this.tableId = tableId; + if (this.nocoApi.record) { + this.nocoApi.record.table = tableId; + } + return { + code, + data: { + id: tableId, + title, + } + }; + } + getItem(id: number): Promise> { + return this.nocoApi.record.read(id); + } + + getList(params: any): Promise> { + return this.nocoApi.record.list({ + ...params, + }); + } + updateItem(data: Partial) { + return this.nocoApi.record.update(data); + } + createItem(data: Partial) { + return this.nocoApi.record.create(data); + } +} \ No newline at end of file diff --git a/auto/common/index.ts b/auto/common/index.ts new file mode 100644 index 0000000..4e1a677 --- /dev/null +++ b/auto/common/index.ts @@ -0,0 +1,2 @@ +export * from "./core.ts"; +export * from "./base-table.ts"; diff --git a/auto/control/index.ts b/auto/control/index.ts new file mode 100644 index 0000000..7f64cb5 --- /dev/null +++ b/auto/control/index.ts @@ -0,0 +1,7 @@ +import { Core } from "../common/index.ts"; + +export class Control extends Core { + key = 'control'; + title = '控制中枢' + description = '管理和控制系统的运行' +} \ No newline at end of file diff --git a/auto/index.ts b/auto/index.ts new file mode 100644 index 0000000..be16789 --- /dev/null +++ b/auto/index.ts @@ -0,0 +1,19 @@ +import { NocoApi } from "../src/index.ts"; +import { ColumnItem, columns, } from "./common/base-table.ts"; +import { Life } from "./life/index.ts"; +import { Control } from "./control/index.ts"; +import { Core } from "./common/core.ts"; + +import { NocoWehookPayload } from "./callback/index.ts"; +export { + NocoApi, + columns, + Control, + Life, + Core, +} + +export type { + NocoWehookPayload, + ColumnItem +} \ No newline at end of file diff --git a/auto/life/index.ts b/auto/life/index.ts new file mode 100644 index 0000000..c079f9f --- /dev/null +++ b/auto/life/index.ts @@ -0,0 +1,7 @@ +import { Core } from "../common/index.ts"; + +export class Life extends Core { + key = 'life'; + title = '人生备忘录' + description = '记录和管理你的人生大事小事' +} \ No newline at end of file diff --git a/mod.ts b/mod.ts new file mode 100644 index 0000000..d13e163 --- /dev/null +++ b/mod.ts @@ -0,0 +1 @@ +export * from './auto/index.ts'; \ No newline at end of file diff --git a/package.json b/package.json index df9217b..2cc9b7b 100644 --- a/package.json +++ b/package.json @@ -12,7 +12,9 @@ }, "files": [ "dist", - "src" + "src", + "auto", + "mod.ts" ], "keywords": [ "nocodb", @@ -33,6 +35,7 @@ }, "exports": { ".": "./dist/app.js", + "./mod.ts": "./mod.ts", "./src/**/*": "./src/**/" }, "dependencies": {} diff --git a/src/meta/tables.ts b/src/meta/tables.ts index 7df2b27..9c8bfb7 100644 --- a/src/meta/tables.ts +++ b/src/meta/tables.ts @@ -28,6 +28,11 @@ type TableMetaInfo = { uuid: string | null; password: string | null; fk_custom_url_id: string | null; + + views?: Array; + columns?: Array; + columnsById?: Record; + columnHash?: string; } export class MetaTables { diff --git a/src/record.ts b/src/record.ts index a6234e2..5fde556 100644 --- a/src/record.ts +++ b/src/record.ts @@ -102,6 +102,21 @@ export class Record { getTableMeta() { return this.meta.tables.getTableMeta(this.table); } + async getTableSchema() { + const res = await this.getTableMeta(); + if (res.code === 200) { + const columns = res.data.columns; + return columns.map(col => ({ + id: col.id, + name: col.title, + type: col.uidt, + required: col.rqd, + primary: col.pv, + options: col.colOptions, + })); + } + return []; + } } diff --git a/test/list-table.ts b/test/list-table.ts index 3bba2bd..7e39fa3 100644 --- a/test/list-table.ts +++ b/test/list-table.ts @@ -3,8 +3,11 @@ import { nocoApi } from './common.ts'; import util from 'node:util'; import { writeFileSync } from 'node:fs'; -const res =await nocoApi.record.getTableMeta() -// console.log(util.inspect(res, { depth: null, colors: true })) -const keys = Object.keys(res.data || {}) -console.log('表字段列表:', keys); -writeFileSync('table-metadata.json', JSON.stringify(res, null, 2)); \ No newline at end of file +// const res =await nocoApi.record.getTableMeta() +// // console.log(util.inspect(res, { depth: null, colors: true })) +// const keys = Object.keys(res.data || {}) +// console.log('表字段列表:', keys, res.data.schema); +// writeFileSync('table-metadata.json', JSON.stringify(res, null, 2)); + +const res2 = await nocoApi.record.getTableSchema() +console.log(util.inspect(res2, { depth: null, colors: true })) \ No newline at end of file