@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
|
@@ -19,18 +19,20 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
19
19
|
// src/backend/schemas/index.ts
|
|
20
20
|
var schemas_exports = {};
|
|
21
21
|
__export(schemas_exports, {
|
|
22
|
-
BASIC_INSERT_OMIT: () => BASIC_INSERT_OMIT,
|
|
23
|
-
BASIC_UPDATE_OMIT: () => BASIC_UPDATE_OMIT,
|
|
22
|
+
BASIC_INSERT_OMIT: () => import_common2.BASIC_INSERT_OMIT,
|
|
23
|
+
BASIC_UPDATE_OMIT: () => import_common2.BASIC_UPDATE_OMIT,
|
|
24
24
|
basicFields: () => basicFields,
|
|
25
25
|
createTableSchema: () => createTableSchema,
|
|
26
|
-
describeFields: () => describeFields,
|
|
27
|
-
listBodySchema: () => listBodySchema,
|
|
28
|
-
queryListSchema: () => queryListSchema
|
|
26
|
+
describeFields: () => import_common2.describeFields,
|
|
27
|
+
listBodySchema: () => import_common2.listBodySchema,
|
|
28
|
+
queryListSchema: () => import_common2.queryListSchema
|
|
29
29
|
});
|
|
30
30
|
module.exports = __toCommonJS(schemas_exports);
|
|
31
31
|
var import_zod = require("zod");
|
|
32
32
|
var import_pg_core = require("drizzle-orm/pg-core");
|
|
33
33
|
var import_drizzle_zod = require("drizzle-zod");
|
|
34
|
+
var import_common = require("./common");
|
|
35
|
+
var import_common2 = require("./common");
|
|
34
36
|
var timestamptz = (name) => (0, import_pg_core.timestamp)(name, { withTimezone: true });
|
|
35
37
|
var createdAt = () => timestamptz("created_at").notNull().defaultNow();
|
|
36
38
|
var updatedAt = () => timestamptz("updated_at").notNull().defaultNow();
|
|
@@ -43,79 +45,6 @@ var basicFields = {
|
|
|
43
45
|
createdAt: createdAt(),
|
|
44
46
|
updatedAt: updatedAt()
|
|
45
47
|
};
|
|
46
|
-
var BASIC_INSERT_OMIT = {
|
|
47
|
-
id: true,
|
|
48
|
-
creatorId: true,
|
|
49
|
-
editorId: true,
|
|
50
|
-
accessedAt: true,
|
|
51
|
-
createdAt: true,
|
|
52
|
-
updatedAt: true
|
|
53
|
-
};
|
|
54
|
-
var BASIC_UPDATE_OMIT = {
|
|
55
|
-
creatorId: true,
|
|
56
|
-
editorId: true,
|
|
57
|
-
accessedAt: true,
|
|
58
|
-
createdAt: true,
|
|
59
|
-
updatedAt: true
|
|
60
|
-
};
|
|
61
|
-
var queryListSchema = (schema) => import_zod.z.object({
|
|
62
|
-
current: import_zod.z.string().optional().default("1").describe("Page number, starting at 1."),
|
|
63
|
-
pageSize: import_zod.z.string().optional().default("10").describe(
|
|
64
|
-
"Rows per page. Omitting it yields only 10 — pass a larger value to get the whole set."
|
|
65
|
-
),
|
|
66
|
-
createdAtFrom: import_zod.z.string().optional().describe(
|
|
67
|
-
"Keep only rows created at or after this instant (ISO 8601, e.g. 2026-08-01T00:00:00Z)."
|
|
68
|
-
),
|
|
69
|
-
createdAtTo: import_zod.z.string().optional().describe("Keep only rows created at or before this instant."),
|
|
70
|
-
orderBy: import_zod.z.string().optional().describe(
|
|
71
|
-
"Column to sort by — a column name of this resource (createdAt / updatedAt and the like)."
|
|
72
|
-
),
|
|
73
|
-
creatorId: import_zod.z.string().optional().describe(
|
|
74
|
-
"Keep only rows created by this user. Useful in shared spaces to filter down to one member."
|
|
75
|
-
),
|
|
76
|
-
orderDir: import_zod.z.enum(["asc", "desc"]).optional().describe("Sort ascending or descending.")
|
|
77
|
-
}).merge(schema);
|
|
78
|
-
var listBodySchema = (schema) => import_zod.z.object({
|
|
79
|
-
total: import_zod.z.number().describe(
|
|
80
|
-
"Total number of rows matching the filters — not the length of `data`. Use it to decide whether another page exists."
|
|
81
|
-
),
|
|
82
|
-
data: import_zod.z.array(schema).describe("This page's rows.")
|
|
83
|
-
});
|
|
84
|
-
var BASIC_FIELD_NOTES = {
|
|
85
|
-
creatorId: "User id of the creator. Stamped by the server; clients cannot send it.",
|
|
86
|
-
editorId: "User id of whoever changed this row last. Stamped by the server; clients cannot send it.",
|
|
87
|
-
id: "Primary key of this row (uuid)."
|
|
88
|
-
};
|
|
89
|
-
var BASIC_TIMESTAMP_NOTES = {
|
|
90
|
-
accessedAt: "When this row was last accessed.",
|
|
91
|
-
createdAt: "When this row was created.",
|
|
92
|
-
updatedAt: "When this row was last modified."
|
|
93
|
-
};
|
|
94
|
-
var describeFields = (schema, notes) => {
|
|
95
|
-
const overlay = {};
|
|
96
|
-
for (const [field, note] of Object.entries(notes)) {
|
|
97
|
-
const existing = schema.shape[field];
|
|
98
|
-
if (existing) {
|
|
99
|
-
overlay[field] = existing.describe(note);
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
|
-
return Object.keys(overlay).length ? schema.extend(overlay) : schema;
|
|
103
|
-
};
|
|
104
|
-
var describeBasicFields = (schema) => {
|
|
105
|
-
const withNotes = describeFields(schema, BASIC_FIELD_NOTES);
|
|
106
|
-
const overlay = {};
|
|
107
|
-
for (const [field, note] of Object.entries(BASIC_TIMESTAMP_NOTES)) {
|
|
108
|
-
const existing = withNotes.shape[field];
|
|
109
|
-
if (existing) {
|
|
110
|
-
overlay[field] = existing.meta({
|
|
111
|
-
description: note,
|
|
112
|
-
format: "date-time",
|
|
113
|
-
type: "string"
|
|
114
|
-
});
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
return Object.keys(overlay).length ? withNotes.extend(overlay) : withNotes;
|
|
118
|
-
};
|
|
119
48
|
var createTableSchema = ({
|
|
120
49
|
name,
|
|
121
50
|
columns,
|
|
@@ -130,15 +59,15 @@ var createTableSchema = ({
|
|
|
130
59
|
};
|
|
131
60
|
const table = (0, import_pg_core.pgTable)(name, mergedColumns, extraConfig);
|
|
132
61
|
const notes = describe ?? {};
|
|
133
|
-
const selectSchema = describeFields(
|
|
134
|
-
describeBasicFields((0, import_drizzle_zod.createSelectSchema)(table)),
|
|
62
|
+
const selectSchema = (0, import_common.describeFields)(
|
|
63
|
+
(0, import_common.describeBasicFields)((0, import_drizzle_zod.createSelectSchema)(table)),
|
|
135
64
|
notes
|
|
136
65
|
);
|
|
137
|
-
const insertSchema = describeFields(
|
|
66
|
+
const insertSchema = (0, import_common.describeFields)(
|
|
138
67
|
(0, import_drizzle_zod.createInsertSchema)((0, import_pg_core.pgTable)(name, columns)),
|
|
139
68
|
notes
|
|
140
69
|
);
|
|
141
|
-
const updateSchema = describeFields(
|
|
70
|
+
const updateSchema = (0, import_common.describeFields)(
|
|
142
71
|
(0, import_drizzle_zod.createUpdateSchema)((0, import_pg_core.pgTable)(name, { id: (0, import_pg_core.uuid)("id"), ...columns })).extend({
|
|
143
72
|
id: import_zod.z.string().describe("Id of the row to update.")
|
|
144
73
|
}),
|
|
@@ -147,7 +76,7 @@ var createTableSchema = ({
|
|
|
147
76
|
const querySchema = import_zod.z.object({
|
|
148
77
|
id: import_zod.z.string().describe("Id of the row to fetch.")
|
|
149
78
|
});
|
|
150
|
-
const queryListWithSchema = queryListSchema(
|
|
79
|
+
const queryListWithSchema = (0, import_common.queryListSchema)(
|
|
151
80
|
import_zod.z.object({}).catchall(import_zod.z.unknown())
|
|
152
81
|
);
|
|
153
82
|
return {
|
|
@@ -1,10 +1,9 @@
|
|
|
1
1
|
import { BaseTable } from "../types.js";
|
|
2
|
-
import { ScopeArg } from "../../scope.js";
|
|
3
2
|
export declare function createDeleteAction<TTable extends BaseTable>({ table, db, }: {
|
|
4
3
|
db: any;
|
|
5
4
|
table: TTable;
|
|
6
5
|
}): ({ id }: {
|
|
7
6
|
id: string;
|
|
8
7
|
}, { scope }: {
|
|
9
|
-
scope: ScopeArg;
|
|
8
|
+
scope: import("../../index.js").ScopeArg;
|
|
10
9
|
}) => Promise<any>;
|
|
@@ -22,24 +22,12 @@ __export(create_delete_action_exports, {
|
|
|
22
22
|
createDeleteAction: () => createDeleteAction
|
|
23
23
|
});
|
|
24
24
|
module.exports = __toCommonJS(create_delete_action_exports);
|
|
25
|
-
var
|
|
26
|
-
var import_scope = require("../../scope");
|
|
27
|
-
var import_errors = require("../errors");
|
|
25
|
+
var import_write_action_core = require("../../actions/write-action-core");
|
|
28
26
|
function createDeleteAction({
|
|
29
27
|
table,
|
|
30
28
|
db
|
|
31
29
|
}) {
|
|
32
|
-
return
|
|
33
|
-
if (!id) {
|
|
34
|
-
throw new import_errors.HttpError(400, "缺少 id");
|
|
35
|
-
}
|
|
36
|
-
const guard = (0, import_scope.scopeCondition)(table, scope);
|
|
37
|
-
const rows = await db.delete(table).where(guard ? (0, import_drizzle_orm.and)((0, import_drizzle_orm.eq)(table.id, id), guard) : (0, import_drizzle_orm.eq)(table.id, id)).returning();
|
|
38
|
-
if (rows.length === 0 && scope !== import_scope.NO_SCOPE) {
|
|
39
|
-
throw new import_errors.HttpError(404, "未找到删除对象,或没有权限");
|
|
40
|
-
}
|
|
41
|
-
return rows;
|
|
42
|
-
};
|
|
30
|
+
return (0, import_write_action_core.createDeleteActionCore)({ db, table });
|
|
43
31
|
}
|
|
44
32
|
// Annotate the CommonJS export names for ESM import in node:
|
|
45
33
|
0 && (module.exports = {
|
|
@@ -22,26 +22,13 @@ __export(create_put_action_exports, {
|
|
|
22
22
|
createPutAction: () => createPutAction
|
|
23
23
|
});
|
|
24
24
|
module.exports = __toCommonJS(create_put_action_exports);
|
|
25
|
-
var
|
|
26
|
-
var import_drizzle_orm = require("drizzle-orm");
|
|
27
|
-
var import_scope = require("../../scope");
|
|
28
|
-
var import_errors = require("../errors");
|
|
25
|
+
var import_write_action_core = require("../../actions/write-action-core");
|
|
29
26
|
function createPutAction({
|
|
30
27
|
db,
|
|
31
28
|
table
|
|
32
29
|
}) {
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
if (!id) {
|
|
36
|
-
throw new import_errors.HttpError(400, "缺少 id");
|
|
37
|
-
}
|
|
38
|
-
const guard = (0, import_scope.scopeCondition)(table, scope);
|
|
39
|
-
const [data] = await db.update(table).set((0, import_transform_body.transformBody)(body)).where(guard ? (0, import_drizzle_orm.and)((0, import_drizzle_orm.eq)(table.id, id), guard) : (0, import_drizzle_orm.eq)(table.id, id)).returning();
|
|
40
|
-
if (!data && scope !== import_scope.NO_SCOPE) {
|
|
41
|
-
throw new import_errors.HttpError(404, "未找到编辑对象,或没有权限");
|
|
42
|
-
}
|
|
43
|
-
return data;
|
|
44
|
-
};
|
|
30
|
+
const run = (0, import_write_action_core.createPutActionCore)({ db, table });
|
|
31
|
+
return async (body, options) => run(body, options);
|
|
45
32
|
}
|
|
46
33
|
// Annotate the CommonJS export names for ESM import in node:
|
|
47
34
|
0 && (module.exports = {
|
|
@@ -1,19 +1,14 @@
|
|
|
1
|
-
import { ScopeArg } from "../../scope.js";
|
|
2
1
|
import { BaseTable, GetListRelations } from "../types.js";
|
|
3
2
|
import { SelectedFields } from "drizzle-orm/sqlite-core/query-builders/select.types";
|
|
4
|
-
|
|
3
|
+
import { ScopeArg } from "../../scope.js";
|
|
4
|
+
import { type ListQueryParams } from "../../actions/get-list-query-core.js";
|
|
5
|
+
export declare function getListQuery<TTable extends BaseTable, TSelection extends SelectedFields>(args: {
|
|
5
6
|
db: any;
|
|
6
7
|
fields: TSelection;
|
|
7
8
|
jsonArrayFields?: string[];
|
|
8
9
|
/** 必填。与 pg 那套同义,见 backend/scope.ts。 */
|
|
9
10
|
scope: ScopeArg;
|
|
10
|
-
params:
|
|
11
|
-
[field: string]: unknown;
|
|
12
|
-
orderBy?: keyof BaseTable;
|
|
13
|
-
orderDir?: "asc" | "desc";
|
|
14
|
-
page?: number;
|
|
15
|
-
pageSize?: number;
|
|
16
|
-
};
|
|
11
|
+
params: ListQueryParams;
|
|
17
12
|
relations?: GetListRelations;
|
|
18
13
|
table: TTable;
|
|
19
14
|
}): {
|
|
@@ -6,11 +6,11 @@ var __export = (target, all) => {
|
|
|
6
6
|
for (var name in all)
|
|
7
7
|
__defProp(target, name, { get: all[name], enumerable: true });
|
|
8
8
|
};
|
|
9
|
-
var __copyProps = (to, from, except,
|
|
9
|
+
var __copyProps = (to, from, except, desc) => {
|
|
10
10
|
if (from && typeof from === "object" || typeof from === "function") {
|
|
11
11
|
for (let key of __getOwnPropNames(from))
|
|
12
12
|
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
13
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(
|
|
13
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
14
14
|
}
|
|
15
15
|
return to;
|
|
16
16
|
};
|
|
@@ -22,146 +22,21 @@ __export(get_list_query_exports, {
|
|
|
22
22
|
getListQuery: () => getListQuery
|
|
23
23
|
});
|
|
24
24
|
module.exports = __toCommonJS(get_list_query_exports);
|
|
25
|
-
var import_scope = require("../../scope");
|
|
26
25
|
var import_drizzle_orm = require("drizzle-orm");
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
...filters
|
|
42
|
-
} = params;
|
|
43
|
-
function buildBaseQuery(table2, fields2, relations2) {
|
|
44
|
-
let queryFields = { ...fields2 };
|
|
45
|
-
if (relations2 == null ? void 0 : relations2.length) {
|
|
46
|
-
for (const { select } of relations2) {
|
|
47
|
-
queryFields = { ...queryFields, ...select };
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
const query2 = db.select(queryFields).from(table2);
|
|
51
|
-
if (relations2 == null ? void 0 : relations2.length) {
|
|
52
|
-
query2.groupBy(
|
|
53
|
-
table2.id,
|
|
54
|
-
...relations2.filter(({ groupBy }) => groupBy).filter(({ table: table3 }) => table3).map(({ table: table3 }) => table3.id)
|
|
55
|
-
);
|
|
56
|
-
}
|
|
57
|
-
return query2;
|
|
58
|
-
}
|
|
59
|
-
function findTargetColumn(key, table2, relations2) {
|
|
60
|
-
var _a;
|
|
61
|
-
return (_a = [table2].concat(
|
|
62
|
-
(relations2 == null ? void 0 : relations2.filter(({ table: table3 }) => table3).map(({ table: table3 }) => table3)) || []
|
|
63
|
-
).find((table3) => table3[key])) == null ? void 0 : _a[key];
|
|
64
|
-
}
|
|
65
|
-
function addCondition(conditions2, key, value, targetColumn) {
|
|
66
|
-
const isIdField = /id/i.test(key);
|
|
67
|
-
const isJsonArray = jsonArrayFields == null ? void 0 : jsonArrayFields.includes(key);
|
|
68
|
-
if (typeof value === "string") {
|
|
69
|
-
if (value.includes(",")) {
|
|
70
|
-
const values = value.split(",").map((val) => val.trim()).filter(Boolean);
|
|
71
|
-
if (values.length > 0) {
|
|
72
|
-
if (isJsonArray) {
|
|
73
|
-
conditions2.push(
|
|
74
|
-
import_drizzle_orm.sql`
|
|
75
|
-
EXISTS (
|
|
76
|
-
SELECT 1 FROM json_each(${targetColumn}) tag
|
|
77
|
-
WHERE tag.value LIKE ${`%${values[0]}%`}
|
|
78
|
-
)
|
|
79
|
-
`
|
|
80
|
-
);
|
|
81
|
-
} else if (isIdField) {
|
|
82
|
-
conditions2.push((0, import_drizzle_orm.inArray)(targetColumn, values));
|
|
83
|
-
} else {
|
|
84
|
-
conditions2.push(
|
|
85
|
-
(0, import_drizzle_orm.or)(
|
|
86
|
-
...values.map((val) => (0, import_drizzle_orm.like)(targetColumn, `%${val}%`))
|
|
87
|
-
)
|
|
88
|
-
);
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
} else {
|
|
92
|
-
if (isJsonArray) {
|
|
93
|
-
conditions2.push(
|
|
94
|
-
import_drizzle_orm.sql`
|
|
95
|
-
EXISTS (
|
|
96
|
-
SELECT 1 FROM json_each(${targetColumn}) tag
|
|
97
|
-
WHERE tag.value LIKE ${`%${value}%`}
|
|
98
|
-
)
|
|
99
|
-
`
|
|
100
|
-
);
|
|
101
|
-
} else if (isIdField) {
|
|
102
|
-
conditions2.push((0, import_drizzle_orm.eq)(targetColumn, value));
|
|
103
|
-
} else {
|
|
104
|
-
conditions2.push((0, import_drizzle_orm.like)(targetColumn, `%${value}%`));
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
} else {
|
|
108
|
-
conditions2.push((0, import_drizzle_orm.eq)(targetColumn, value));
|
|
109
|
-
}
|
|
110
|
-
}
|
|
111
|
-
function buildConditions(filters2, table2, relations2) {
|
|
112
|
-
const conditions2 = [];
|
|
113
|
-
for (const [key, value] of Object.entries(filters2)) {
|
|
114
|
-
if (!value) {
|
|
115
|
-
continue;
|
|
116
|
-
}
|
|
117
|
-
if (key.endsWith("AtFrom") || key.endsWith("AtTo")) {
|
|
118
|
-
const baseFieldName = key.replace(/(AtFrom|AtTo)$/, "");
|
|
119
|
-
const targetColumn2 = findTargetColumn(
|
|
120
|
-
`${baseFieldName}At`,
|
|
121
|
-
table2,
|
|
122
|
-
relations2
|
|
123
|
-
);
|
|
124
|
-
if (!targetColumn2) {
|
|
125
|
-
continue;
|
|
126
|
-
}
|
|
127
|
-
if (key.endsWith("AtFrom")) {
|
|
128
|
-
conditions2.push((0, import_drizzle_orm.gte)(targetColumn2, new Date(value)));
|
|
129
|
-
} else if (key.endsWith("AtTo")) {
|
|
130
|
-
conditions2.push((0, import_drizzle_orm.lte)(targetColumn2, new Date(value)));
|
|
131
|
-
}
|
|
132
|
-
continue;
|
|
133
|
-
}
|
|
134
|
-
const targetColumn = findTargetColumn(
|
|
135
|
-
key,
|
|
136
|
-
table2,
|
|
137
|
-
relations2
|
|
138
|
-
);
|
|
139
|
-
if (!targetColumn) {
|
|
140
|
-
continue;
|
|
141
|
-
}
|
|
142
|
-
addCondition(conditions2, key, value, targetColumn);
|
|
143
|
-
}
|
|
144
|
-
return conditions2;
|
|
145
|
-
}
|
|
146
|
-
function buildOrderBy(table2, orderBy2, orderDir2) {
|
|
147
|
-
const orderColumn = table2[orderBy2];
|
|
148
|
-
return orderDir2 === "asc" ? (0, import_drizzle_orm.asc)(orderColumn) : (0, import_drizzle_orm.desc)(orderColumn);
|
|
149
|
-
}
|
|
150
|
-
const baseQuery = buildBaseQuery(table, fields, relations);
|
|
151
|
-
const scopeSql = (0, import_scope.scopeCondition)(table, scope);
|
|
152
|
-
const conditions = scopeSql ? [scopeSql] : [];
|
|
153
|
-
conditions.push(...buildConditions(filters, table, relations));
|
|
154
|
-
const query = baseQuery.where(conditions.length > 0 ? (0, import_drizzle_orm.and)(...conditions) : void 0).orderBy(buildOrderBy(table, orderBy, orderDir)).limit(pageSize).offset((page - 1) * pageSize);
|
|
155
|
-
const countQuery = db.select({ count: import_drizzle_orm.sql`COUNT(DISTINCT ${table.id})` }).from(table).where(conditions.length > 0 ? (0, import_drizzle_orm.and)(...conditions) : void 0);
|
|
156
|
-
if (relations == null ? void 0 : relations.length) {
|
|
157
|
-
for (const { table: relationTable, sql: joinSql } of relations) {
|
|
158
|
-
if (relationTable) {
|
|
159
|
-
query.leftJoin(relationTable, joinSql);
|
|
160
|
-
countQuery.leftJoin(relationTable, joinSql);
|
|
161
|
-
}
|
|
162
|
-
}
|
|
163
|
-
}
|
|
164
|
-
return { countQuery, query };
|
|
26
|
+
var import_get_list_query_core = require("../../actions/get-list-query-core");
|
|
27
|
+
var run = (0, import_get_list_query_core.createGetListQuery)({
|
|
28
|
+
contains: (column, keyword) => (0, import_drizzle_orm.like)(column, `%${keyword}%`),
|
|
29
|
+
// json_each 展开出来的行里,值在 tag.value 上(pg 那个 _text 变体是直接给字符串的)——
|
|
30
|
+
// 这是两边唯一一处连字段名都不同的地方。
|
|
31
|
+
jsonArrayContains: (column, keyword) => import_drizzle_orm.sql`
|
|
32
|
+
EXISTS (
|
|
33
|
+
SELECT 1 FROM json_each(${column}) tag
|
|
34
|
+
WHERE tag.value LIKE ${`%${keyword}%`}
|
|
35
|
+
)
|
|
36
|
+
`
|
|
37
|
+
});
|
|
38
|
+
function getListQuery(args) {
|
|
39
|
+
return run(args);
|
|
165
40
|
}
|
|
166
41
|
// Annotate the CommonJS export names for ESM import in node:
|
|
167
42
|
0 && (module.exports = {
|
|
@@ -19,16 +19,10 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
19
19
|
// src/backend/sqlite/errors.ts
|
|
20
20
|
var errors_exports = {};
|
|
21
21
|
__export(errors_exports, {
|
|
22
|
-
HttpError: () => HttpError
|
|
22
|
+
HttpError: () => import_errors.HttpError
|
|
23
23
|
});
|
|
24
24
|
module.exports = __toCommonJS(errors_exports);
|
|
25
|
-
var
|
|
26
|
-
constructor(status, message) {
|
|
27
|
-
super(message);
|
|
28
|
-
this.name = "HttpError";
|
|
29
|
-
this.status = status;
|
|
30
|
-
}
|
|
31
|
-
};
|
|
25
|
+
var import_errors = require("../errors");
|
|
32
26
|
// Annotate the CommonJS export names for ESM import in node:
|
|
33
27
|
0 && (module.exports = {
|
|
34
28
|
HttpError
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { z } from "zod";
|
|
2
1
|
import { type SQLiteColumnBuilderBase, type SQLiteTableExtraConfigValue } from "drizzle-orm/sqlite-core";
|
|
3
2
|
import type { BuildExtraConfigColumns } from "drizzle-orm";
|
|
4
3
|
export declare const basicFields: {
|
|
@@ -9,116 +8,7 @@ export declare const basicFields: {
|
|
|
9
8
|
createdAt: import("drizzle-orm").HasRuntimeDefault<import("drizzle-orm").HasDefault<import("drizzle-orm").NotNull<import("drizzle-orm/sqlite-core").SQLiteTimestampBuilderInitial<string>>>>;
|
|
10
9
|
updatedAt: import("drizzle-orm").HasRuntimeDefault<import("drizzle-orm").HasDefault<import("drizzle-orm").NotNull<import("drizzle-orm/sqlite-core").SQLiteTimestampBuilderInitial<string>>>>;
|
|
11
10
|
};
|
|
12
|
-
|
|
13
|
-
* 业务侧 insert schema 需要从 drizzle-zod 生成的 schema 中排除的基础字段。
|
|
14
|
-
* 这些字段由后端在 createPostAction 中自动写入。
|
|
15
|
-
*
|
|
16
|
-
* 使用方式(在业务 schema 中):
|
|
17
|
-
* const insertFooSchema = createInsertSchema(foos)
|
|
18
|
-
* .omit(BASIC_INSERT_OMIT)
|
|
19
|
-
* .extend({ ... });
|
|
20
|
-
*
|
|
21
|
-
* 说明:上游导出为 const 而不是 helper function,是为了避免经过泛型
|
|
22
|
-
* wrapper 后 TS 把 drizzle-zod 推导出的深层条件 Shape 归约成 Omit,
|
|
23
|
-
* 从而在下游 `.extend(...)` 时丢失原有字段类型。
|
|
24
|
-
*/
|
|
25
|
-
export declare const BASIC_INSERT_OMIT: {
|
|
26
|
-
readonly id: true;
|
|
27
|
-
readonly creatorId: true;
|
|
28
|
-
readonly editorId: true;
|
|
29
|
-
readonly accessedAt: true;
|
|
30
|
-
readonly createdAt: true;
|
|
31
|
-
readonly updatedAt: true;
|
|
32
|
-
};
|
|
33
|
-
/**
|
|
34
|
-
* 业务侧 update schema 需要从 drizzle-zod 生成的 schema 中排除的基础字段。
|
|
35
|
-
* update 保留 id(随后用 `.required({ id: true })` 变成必填),其余由
|
|
36
|
-
* 后端 createPutAction 自动维护。
|
|
37
|
-
*/
|
|
38
|
-
export declare const BASIC_UPDATE_OMIT: {
|
|
39
|
-
readonly creatorId: true;
|
|
40
|
-
readonly editorId: true;
|
|
41
|
-
readonly accessedAt: true;
|
|
42
|
-
readonly createdAt: true;
|
|
43
|
-
readonly updatedAt: true;
|
|
44
|
-
};
|
|
45
|
-
export declare const queryListSchema: <Incoming extends z.ZodObject<z.core.$ZodLooseShape, z.core.$strip>>(schema: Incoming) => z.ZodObject<(("creatorId" | "orderBy" | "orderDir" | "pageSize" | "current" | "createdAtFrom" | "createdAtTo") & keyof Incoming["shape"] extends never ? {
|
|
46
|
-
current: z.ZodDefault<z.ZodOptional<z.ZodString>>;
|
|
47
|
-
pageSize: z.ZodDefault<z.ZodOptional<z.ZodString>>;
|
|
48
|
-
createdAtFrom: z.ZodOptional<z.ZodString>;
|
|
49
|
-
createdAtTo: z.ZodOptional<z.ZodString>;
|
|
50
|
-
orderBy: z.ZodOptional<z.ZodString>;
|
|
51
|
-
creatorId: z.ZodOptional<z.ZodString>;
|
|
52
|
-
orderDir: z.ZodOptional<z.ZodEnum<{
|
|
53
|
-
asc: "asc";
|
|
54
|
-
desc: "desc";
|
|
55
|
-
}>>;
|
|
56
|
-
} & Incoming["shape"] : ({
|
|
57
|
-
current: z.ZodDefault<z.ZodOptional<z.ZodString>>;
|
|
58
|
-
pageSize: z.ZodDefault<z.ZodOptional<z.ZodString>>;
|
|
59
|
-
createdAtFrom: z.ZodOptional<z.ZodString>;
|
|
60
|
-
createdAtTo: z.ZodOptional<z.ZodString>;
|
|
61
|
-
orderBy: z.ZodOptional<z.ZodString>;
|
|
62
|
-
creatorId: z.ZodOptional<z.ZodString>;
|
|
63
|
-
orderDir: z.ZodOptional<z.ZodEnum<{
|
|
64
|
-
asc: "asc";
|
|
65
|
-
desc: "desc";
|
|
66
|
-
}>>;
|
|
67
|
-
} extends infer T_1 extends z.core.util.SomeObject ? { [K in keyof T_1 as K extends keyof Incoming["shape"] ? never : K]: {
|
|
68
|
-
current: z.ZodDefault<z.ZodOptional<z.ZodString>>;
|
|
69
|
-
pageSize: z.ZodDefault<z.ZodOptional<z.ZodString>>;
|
|
70
|
-
createdAtFrom: z.ZodOptional<z.ZodString>;
|
|
71
|
-
createdAtTo: z.ZodOptional<z.ZodString>;
|
|
72
|
-
orderBy: z.ZodOptional<z.ZodString>;
|
|
73
|
-
creatorId: z.ZodOptional<z.ZodString>;
|
|
74
|
-
orderDir: z.ZodOptional<z.ZodEnum<{
|
|
75
|
-
asc: "asc";
|
|
76
|
-
desc: "desc";
|
|
77
|
-
}>>;
|
|
78
|
-
}[K]; } : never) & (Incoming["shape"] extends infer T_2 extends z.core.util.SomeObject ? { [K_1 in keyof T_2]: Incoming["shape"][K_1]; } : never)) extends infer T ? { [k in keyof T]: (("creatorId" | "orderBy" | "orderDir" | "pageSize" | "current" | "createdAtFrom" | "createdAtTo") & keyof Incoming["shape"] extends never ? {
|
|
79
|
-
current: z.ZodDefault<z.ZodOptional<z.ZodString>>;
|
|
80
|
-
pageSize: z.ZodDefault<z.ZodOptional<z.ZodString>>;
|
|
81
|
-
createdAtFrom: z.ZodOptional<z.ZodString>;
|
|
82
|
-
createdAtTo: z.ZodOptional<z.ZodString>;
|
|
83
|
-
orderBy: z.ZodOptional<z.ZodString>;
|
|
84
|
-
creatorId: z.ZodOptional<z.ZodString>;
|
|
85
|
-
orderDir: z.ZodOptional<z.ZodEnum<{
|
|
86
|
-
asc: "asc";
|
|
87
|
-
desc: "desc";
|
|
88
|
-
}>>;
|
|
89
|
-
} & Incoming["shape"] : ({
|
|
90
|
-
current: z.ZodDefault<z.ZodOptional<z.ZodString>>;
|
|
91
|
-
pageSize: z.ZodDefault<z.ZodOptional<z.ZodString>>;
|
|
92
|
-
createdAtFrom: z.ZodOptional<z.ZodString>;
|
|
93
|
-
createdAtTo: z.ZodOptional<z.ZodString>;
|
|
94
|
-
orderBy: z.ZodOptional<z.ZodString>;
|
|
95
|
-
creatorId: z.ZodOptional<z.ZodString>;
|
|
96
|
-
orderDir: z.ZodOptional<z.ZodEnum<{
|
|
97
|
-
asc: "asc";
|
|
98
|
-
desc: "desc";
|
|
99
|
-
}>>;
|
|
100
|
-
} extends infer T_1 extends z.core.util.SomeObject ? { [K in keyof T_1 as K extends keyof Incoming["shape"] ? never : K]: {
|
|
101
|
-
current: z.ZodDefault<z.ZodOptional<z.ZodString>>;
|
|
102
|
-
pageSize: z.ZodDefault<z.ZodOptional<z.ZodString>>;
|
|
103
|
-
createdAtFrom: z.ZodOptional<z.ZodString>;
|
|
104
|
-
createdAtTo: z.ZodOptional<z.ZodString>;
|
|
105
|
-
orderBy: z.ZodOptional<z.ZodString>;
|
|
106
|
-
creatorId: z.ZodOptional<z.ZodString>;
|
|
107
|
-
orderDir: z.ZodOptional<z.ZodEnum<{
|
|
108
|
-
asc: "asc";
|
|
109
|
-
desc: "desc";
|
|
110
|
-
}>>;
|
|
111
|
-
}[K]; } : never) & (Incoming["shape"] extends infer T_2 extends z.core.util.SomeObject ? { [K_1 in keyof T_2]: Incoming["shape"][K_1]; } : never))[k]; } : never, Incoming["_zod"]["config"]>;
|
|
112
|
-
export declare const listBodySchema: <T extends z.ZodType<unknown, unknown, z.core.$ZodTypeInternals<unknown, unknown>>>(schema: T) => z.ZodObject<{
|
|
113
|
-
total: z.ZodNumber;
|
|
114
|
-
data: z.ZodArray<T>;
|
|
115
|
-
}, z.core.$strip>;
|
|
116
|
-
/**
|
|
117
|
-
* 给 schema 上已有的字段挂说明。字段不在这个 schema 里就跳过 —— 同一张表的
|
|
118
|
-
* insert / update / select 三份含的列不一样(basicFields 只在 select 里),一份说明表要能同时
|
|
119
|
-
* 喂给三者,就不能对「字段一定存在」有要求。**不给不存在的字段凭空造一个。**
|
|
120
|
-
*/
|
|
121
|
-
export declare const describeFields: <T extends z.ZodObject<z.core.$ZodLooseShape, z.core.$strip>>(schema: T, notes: Record<string, string>) => T;
|
|
11
|
+
export { BASIC_INSERT_OMIT, BASIC_UPDATE_OMIT, describeFields, listBodySchema, queryListSchema, } from "../../schemas/common.js";
|
|
122
12
|
/**
|
|
123
13
|
* 基于 drizzle + drizzle-zod 快速生成带基础字段(id / 创建时间等)的
|
|
124
14
|
* 表定义以及 select / insert / update / query / list zod schema。
|