@ai0x0/utils 0.5.0 → 0.6.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/es/backend/actions/create-delete-action.d.ts +2 -3
- package/es/backend/actions/create-delete-action.js +8 -44
- package/es/backend/actions/create-put-action.d.ts +1 -1
- package/es/backend/actions/create-put-action.js +15 -39
- package/es/backend/actions/get-list-query-core.d.ts +46 -0
- package/es/backend/actions/get-list-query-core.js +236 -0
- package/es/backend/actions/get-list-query.d.ts +5 -18
- package/es/backend/actions/get-list-query.js +20 -220
- package/es/backend/actions/write-action-core.d.ts +18 -0
- package/es/backend/actions/write-action-core.js +119 -0
- package/es/backend/route-operation/operation-common.d.ts +13 -1
- package/es/backend/route-operation/operation-common.js +25 -0
- package/es/backend/route-operation/read-operation-core.js +46 -33
- package/es/backend/route-operation/write-operation-core.d.ts +3 -1
- package/es/backend/route-operation/write-operation-core.js +69 -26
- package/es/backend/schemas/common.d.ts +112 -0
- package/es/backend/schemas/common.js +158 -0
- package/es/backend/schemas/index.d.ts +735 -364
- package/es/backend/schemas/index.js +8 -139
- package/es/backend/sqlite/actions/create-delete-action.d.ts +1 -2
- package/es/backend/sqlite/actions/create-delete-action.js +7 -43
- package/es/backend/sqlite/actions/create-put-action.d.ts +1 -1
- package/es/backend/sqlite/actions/create-put-action.js +13 -36
- package/es/backend/sqlite/actions/get-list-query.d.ts +4 -9
- package/es/backend/sqlite/actions/get-list-query.js +20 -217
- package/es/backend/sqlite/errors.d.ts +1 -4
- package/es/backend/sqlite/errors.js +6 -31
- package/es/backend/sqlite/schemas/index.d.ts +1 -111
- package/es/backend/sqlite/schemas/index.js +8 -139
- package/lib/backend/actions/create-delete-action.d.ts +2 -3
- package/lib/backend/actions/create-delete-action.js +2 -14
- package/lib/backend/actions/create-put-action.d.ts +1 -1
- package/lib/backend/actions/create-put-action.js +3 -16
- package/lib/backend/actions/get-list-query-core.d.ts +46 -0
- package/lib/backend/actions/get-list-query-core.js +151 -0
- package/lib/backend/actions/get-list-query.d.ts +5 -18
- package/lib/backend/actions/get-list-query.js +16 -141
- package/lib/backend/actions/write-action-core.d.ts +18 -0
- package/lib/backend/actions/write-action-core.js +70 -0
- package/lib/backend/route-operation/operation-common.d.ts +13 -1
- package/lib/backend/route-operation/operation-common.js +8 -0
- package/lib/backend/route-operation/read-operation-core.js +14 -4
- package/lib/backend/route-operation/write-operation-core.d.ts +3 -1
- package/lib/backend/route-operation/write-operation-core.js +24 -4
- package/lib/backend/schemas/common.d.ts +112 -0
- package/lib/backend/schemas/common.js +112 -0
- package/lib/backend/schemas/index.d.ts +735 -364
- package/lib/backend/schemas/index.js +12 -83
- package/lib/backend/sqlite/actions/create-delete-action.d.ts +1 -2
- package/lib/backend/sqlite/actions/create-delete-action.js +2 -14
- package/lib/backend/sqlite/actions/create-put-action.d.ts +1 -1
- package/lib/backend/sqlite/actions/create-put-action.js +3 -16
- package/lib/backend/sqlite/actions/get-list-query.d.ts +4 -9
- package/lib/backend/sqlite/actions/get-list-query.js +16 -141
- package/lib/backend/sqlite/errors.d.ts +1 -4
- package/lib/backend/sqlite/errors.js +2 -8
- package/lib/backend/sqlite/schemas/index.d.ts +1 -111
- package/lib/backend/sqlite/schemas/index.js +12 -83
- package/package.json +1 -1
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
|
|
2
|
+
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
|
|
3
|
+
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
|
|
4
|
+
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
|
|
5
|
+
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
|
|
6
|
+
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
|
|
7
|
+
// ==============================================================================
|
|
8
|
+
// 与方言无关的那几份 schema 助手(pg / sqlite 共用)
|
|
9
|
+
// ==============================================================================
|
|
10
|
+
// 下面这些统统只碰 zod,与用哪种数据库没有半点关系 —— 从前 pg 与 sqlite 的 schemas/index
|
|
11
|
+
// 各抄了一份,170 行逐字相同,只有一句注释的措辞不一样。
|
|
12
|
+
//
|
|
13
|
+
// 抄两份的代价在 queryListSchema 上最明显:那七个字段的 `.describe()` 会流进每一个 list 端点
|
|
14
|
+
// 的 OpenAPI spec,再从那里进生成的 client 与 CLI 文档。改一处措辞而忘了另一棵树,两边的接口
|
|
15
|
+
// 文档就开始各说各话,而没有任何东西会报错。
|
|
16
|
+
//
|
|
17
|
+
// 留在各自 index 里的是真·方言相关的两样:basicFields(那几列怎么建)与 createTableSchema
|
|
18
|
+
// (怎么拼表),它们绕不开各自的列构造器。
|
|
19
|
+
|
|
20
|
+
import { z } from "zod";
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* 业务侧 insert schema 需要从 drizzle-zod 生成的 schema 中排除的基础字段。
|
|
24
|
+
* 这些字段由后端在 createPostAction 中自动写入。
|
|
25
|
+
*
|
|
26
|
+
* 使用方式(在业务 schema 中):
|
|
27
|
+
* const insertFooSchema = createInsertSchema(foos)
|
|
28
|
+
* .omit(BASIC_INSERT_OMIT)
|
|
29
|
+
* .extend({ ... });
|
|
30
|
+
*
|
|
31
|
+
* 说明:上游导出为 const 而不是 helper function,是为了避免经过泛型
|
|
32
|
+
* wrapper 后 TS 把 drizzle-zod 推导出的深层条件 Shape 归约成 Omit,
|
|
33
|
+
* 从而在下游 `.extend(...)` 时丢失原有字段类型。
|
|
34
|
+
*/
|
|
35
|
+
export var BASIC_INSERT_OMIT = {
|
|
36
|
+
id: true,
|
|
37
|
+
creatorId: true,
|
|
38
|
+
editorId: true,
|
|
39
|
+
accessedAt: true,
|
|
40
|
+
createdAt: true,
|
|
41
|
+
updatedAt: true
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* 业务侧 update schema 需要从 drizzle-zod 生成的 schema 中排除的基础字段。
|
|
46
|
+
* update 保留 id(随后用 `.required({ id: true })` 变成必填),其余由
|
|
47
|
+
* 后端 createPutAction 自动维护。
|
|
48
|
+
*/
|
|
49
|
+
export var BASIC_UPDATE_OMIT = {
|
|
50
|
+
creatorId: true,
|
|
51
|
+
editorId: true,
|
|
52
|
+
accessedAt: true,
|
|
53
|
+
createdAt: true,
|
|
54
|
+
updatedAt: true
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
// =============================================================================
|
|
58
|
+
// 列表查询字段
|
|
59
|
+
// =============================================================================
|
|
60
|
+
// 说明写成 `.describe()` 而不是行末注释:注释只有读源码的人看得到,而这七个字段会出现在每一个
|
|
61
|
+
// list 端点的 OpenAPI spec 里,再从那里流进生成的 client 与 CLI 文档。写成注释的代价是下游只能
|
|
62
|
+
// 自己再手写一份 —— 那份必然会漂,而且漂了没有任何东西会报错。
|
|
63
|
+
//
|
|
64
|
+
// 类型是 string 而不是 number:query 参数在线上只有字符串,服务端收下之后才转数字。默认值也
|
|
65
|
+
// 因此是 "1" / "10"。
|
|
66
|
+
export var queryListSchema = function queryListSchema(schema) {
|
|
67
|
+
return z.object({
|
|
68
|
+
current: z.string().optional().default("1").describe("Page number, starting at 1."),
|
|
69
|
+
pageSize: z.string().optional().default("10").describe("Rows per page. Omitting it yields only 10 — pass a larger value to get the whole set."),
|
|
70
|
+
createdAtFrom: z.string().optional().describe("Keep only rows created at or after this instant (ISO 8601, e.g. 2026-08-01T00:00:00Z)."),
|
|
71
|
+
createdAtTo: z.string().optional().describe("Keep only rows created at or before this instant."),
|
|
72
|
+
orderBy: z.string().optional().describe("Column to sort by — a column name of this resource (createdAt / updatedAt and the like)."),
|
|
73
|
+
creatorId: z.string().optional().describe("Keep only rows created by this user. Useful in shared spaces to filter down to one member."),
|
|
74
|
+
orderDir: z.enum(["asc", "desc"]).optional().describe("Sort ascending or descending.")
|
|
75
|
+
}).merge(schema);
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
// =============================================================================
|
|
79
|
+
// 列表返回字段
|
|
80
|
+
// =============================================================================
|
|
81
|
+
// 这两个字段出现在**每一个** list 端点的响应里,所以说明写在这儿一次就够 —— 下游那边它们是
|
|
82
|
+
// 逐个 list 命令重复一遍的(在一个 101 端点的项目上是 24 行空白)。
|
|
83
|
+
export var listBodySchema = function listBodySchema(schema) {
|
|
84
|
+
return z.object({
|
|
85
|
+
total: z.number().describe("Total number of rows matching the filters — not the length of `data`. Use it to decide whether another page exists."),
|
|
86
|
+
data: z.array(schema).describe("This page's rows.")
|
|
87
|
+
});
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
// =============================================================================
|
|
91
|
+
// 给基础字段挂说明
|
|
92
|
+
// =============================================================================
|
|
93
|
+
// basicFields 是 drizzle 列,而 drizzle 列没有「描述」这个概念 —— 所以说明只能挂在
|
|
94
|
+
// drizzle-zod 出来的 schema 上。值得做是因为这六个字段出现在**每一张表**的 select schema 里:
|
|
95
|
+
// 一处写完,所有 list / get 端点的返回字段表都有了(在 do-tv 上是 173 行)。
|
|
96
|
+
//
|
|
97
|
+
// 一律只挂元数据,**不换类型**:`.describe()` / `.meta()` 返回的是带元数据的克隆,校验行为一模
|
|
98
|
+
// 一样。selectSchema 是有人拿去 safeParse 的(本包自己的测试就在用 Date 校验它),换类型等于改
|
|
99
|
+
// 这个包的契约。
|
|
100
|
+
//
|
|
101
|
+
// 时间戳那三列额外用 `.meta()` 补上 JSON Schema 表示,因为不补就是错的:drizzle-zod 给的是
|
|
102
|
+
// `z.date()`,而 `Date` 在 JSON Schema 里表达不出来,于是这三个字段在 spec 里是**空对象** ——
|
|
103
|
+
// 没有 `type`,照 spec 生成的 client / CLI 只能得到 `unknown`。而线上根本不可能是 Date:响应一经
|
|
104
|
+
// JSON 序列化就是 ISO 8601 字符串。
|
|
105
|
+
//
|
|
106
|
+
// `.meta({ type, format })` 恰好只改「怎么描述」不改「怎么校验」:spec 里成为
|
|
107
|
+
// `{"type":"string","format":"date-time"}`,而 `safeParse(new Date())` 照样通过。
|
|
108
|
+
// 不用 `z.iso.datetime()` 换掉它,是因为那既改了校验(不再收 Date),又会往 JSON Schema 里塞一条
|
|
109
|
+
// 380 字节的正则 —— 乘上每张表几十 KB,而 `format` 已经把「按日期时间解析」说清楚了。
|
|
110
|
+
var BASIC_FIELD_NOTES = {
|
|
111
|
+
creatorId: "User id of the creator. Stamped by the server; clients cannot send it.",
|
|
112
|
+
editorId: "User id of whoever changed this row last. Stamped by the server; clients cannot send it.",
|
|
113
|
+
id: "Primary key of this row (uuid)."
|
|
114
|
+
};
|
|
115
|
+
var BASIC_TIMESTAMP_NOTES = {
|
|
116
|
+
accessedAt: "When this row was last accessed.",
|
|
117
|
+
createdAt: "When this row was created.",
|
|
118
|
+
updatedAt: "When this row was last modified."
|
|
119
|
+
};
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* 给 schema 上已有的字段挂说明。字段不在这个 schema 里就跳过 —— 同一张表的
|
|
123
|
+
* insert / update / select 三份含的列不一样(basicFields 只在 select 里),一份说明表要能同时
|
|
124
|
+
* 喂给三者,就不能对「字段一定存在」有要求。**不给不存在的字段凭空造一个。**
|
|
125
|
+
*/
|
|
126
|
+
export var describeFields = function describeFields(schema, notes) {
|
|
127
|
+
var overlay = {};
|
|
128
|
+
for (var _i = 0, _Object$entries = Object.entries(notes); _i < _Object$entries.length; _i++) {
|
|
129
|
+
var _Object$entries$_i = _slicedToArray(_Object$entries[_i], 2),
|
|
130
|
+
field = _Object$entries$_i[0],
|
|
131
|
+
note = _Object$entries$_i[1];
|
|
132
|
+
var existing = schema.shape[field];
|
|
133
|
+
if (existing) {
|
|
134
|
+
overlay[field] = existing.describe(note);
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
return Object.keys(overlay).length ? schema.extend(overlay) : schema;
|
|
138
|
+
};
|
|
139
|
+
|
|
140
|
+
// 只有两棵树的 createTableSchema 用得到它,所以是具名导出而不是公开 API 的一部分。
|
|
141
|
+
export var describeBasicFields = function describeBasicFields(schema) {
|
|
142
|
+
var withNotes = describeFields(schema, BASIC_FIELD_NOTES);
|
|
143
|
+
var overlay = {};
|
|
144
|
+
for (var _i2 = 0, _Object$entries2 = Object.entries(BASIC_TIMESTAMP_NOTES); _i2 < _Object$entries2.length; _i2++) {
|
|
145
|
+
var _Object$entries2$_i = _slicedToArray(_Object$entries2[_i2], 2),
|
|
146
|
+
field = _Object$entries2$_i[0],
|
|
147
|
+
note = _Object$entries2$_i[1];
|
|
148
|
+
var existing = withNotes.shape[field];
|
|
149
|
+
if (existing) {
|
|
150
|
+
overlay[field] = existing.meta({
|
|
151
|
+
description: note,
|
|
152
|
+
format: "date-time",
|
|
153
|
+
type: "string"
|
|
154
|
+
});
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
return Object.keys(overlay).length ? withNotes.extend(overlay) : withNotes;
|
|
158
|
+
};
|