@ai0x0/utils 0.1.1 → 0.1.3

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.
@@ -8,7 +8,9 @@ import { z } from "zod";
8
8
  import { pgTable, timestamp, uuid } from "drizzle-orm/pg-core";
9
9
  import { createInsertSchema, createSelectSchema, createUpdateSchema } from "drizzle-zod";
10
10
 
11
- //============================基本字段============================//
11
+ // =============================================================================
12
+ // 基本字段 —— 每张表都注入的那几列
13
+ // =============================================================================
12
14
  var timestamptz = function timestamptz(name) {
13
15
  return timestamp(name, {
14
16
  withTimezone: true
@@ -67,7 +69,9 @@ export var BASIC_UPDATE_OMIT = {
67
69
  updatedAt: true
68
70
  };
69
71
 
70
- //============================列表查询字段============================//
72
+ // =============================================================================
73
+ // 列表查询字段
74
+ // =============================================================================
71
75
  export var queryListSchema = function queryListSchema(schema) {
72
76
  return z.object({
73
77
  current: z.string().optional().default("1"),
@@ -84,7 +88,9 @@ export var queryListSchema = function queryListSchema(schema) {
84
88
  }).merge(schema);
85
89
  };
86
90
 
87
- //============================列表返回字段============================//
91
+ // =============================================================================
92
+ // 列表返回字段
93
+ // =============================================================================
88
94
  export var listBodySchema = function listBodySchema(schema) {
89
95
  return z.object({
90
96
  total: z.number(),
@@ -92,7 +98,9 @@ export var listBodySchema = function listBodySchema(schema) {
92
98
  });
93
99
  };
94
100
 
95
- //============================创建一个pg表============================//
101
+ // =============================================================================
102
+ // 建表 —— 表 + 5 份 zod schema
103
+ // =============================================================================
96
104
  /**
97
105
  * 基于 drizzle + drizzle-zod 快速生成带基础字段(id / 创建时间等)的
98
106
  * 表定义以及 select / insert / update / query / list zod schema。
@@ -100,10 +108,12 @@ export var listBodySchema = function listBodySchema(schema) {
100
108
  export var createTableSchema = function createTableSchema(_ref) {
101
109
  var name = _ref.name,
102
110
  columns = _ref.columns,
111
+ serverColumns = _ref.serverColumns,
103
112
  extraConfig = _ref.extraConfig;
104
- var mergedColumns = _objectSpread(_objectSpread({}, basicFields), columns);
113
+ var mergedColumns = _objectSpread(_objectSpread(_objectSpread({}, basicFields), serverColumns), columns);
105
114
  var table = pgTable(name, mergedColumns, extraConfig);
106
115
  var selectSchema = createSelectSchema(table);
116
+ // 只从 columns 推 —— basicFields 与 serverColumns 都由服务端写,不该出现在请求体里。
107
117
  var insertSchema = createInsertSchema(pgTable(name, columns));
108
118
  var updateSchema = createUpdateSchema(pgTable(name, _objectSpread({
109
119
  id: uuid("id")
@@ -119,7 +119,10 @@ export declare const listBodySchema: <T extends z.ZodType<unknown, unknown, z.co
119
119
  */
120
120
  export declare const createTableSchema: <TTableName extends string, TColumnsMap extends Record<string, SQLiteColumnBuilderBase>>(_options: {
121
121
  name: TTableName;
122
+ /** 客户端能写的业务列。insert / update schema 只从这里推。 */
122
123
  columns: TColumnsMap;
124
+ /** 由服务端盖、绝不接受客户端传的业务列(归属、租户 id 这类)。语义与 pg 版一致,见那边的说明。 */
125
+ serverColumns?: Record<string, SQLiteColumnBuilderBase>;
123
126
  extraConfig?: (_self: BuildExtraConfigColumns<TTableName, typeof basicFields & TColumnsMap, "sqlite">) => SQLiteTableExtraConfigValue[];
124
127
  }) => {
125
128
  table: any;
@@ -8,7 +8,9 @@ import { z } from "zod";
8
8
  import { integer, sqliteTable, text } from "drizzle-orm/sqlite-core";
9
9
  import { createInsertSchema, createSelectSchema, createUpdateSchema } from "drizzle-zod";
10
10
 
11
- //============================基本字段============================//
11
+ // =============================================================================
12
+ // 基本字段 —— 每张表都注入的那几列
13
+ // =============================================================================
12
14
  var timestamp = function timestamp(name) {
13
15
  return integer(name, {
14
16
  mode: "timestamp_ms"
@@ -73,7 +75,9 @@ export var BASIC_UPDATE_OMIT = {
73
75
  updatedAt: true
74
76
  };
75
77
 
76
- //============================列表查询字段============================//
78
+ // =============================================================================
79
+ // 列表查询字段
80
+ // =============================================================================
77
81
  export var queryListSchema = function queryListSchema(schema) {
78
82
  return z.object({
79
83
  current: z.string().optional().default("1"),
@@ -90,7 +94,9 @@ export var queryListSchema = function queryListSchema(schema) {
90
94
  }).merge(schema);
91
95
  };
92
96
 
93
- //============================列表返回字段============================//
97
+ // =============================================================================
98
+ // 列表返回字段
99
+ // =============================================================================
94
100
  export var listBodySchema = function listBodySchema(schema) {
95
101
  return z.object({
96
102
  total: z.number(),
@@ -98,7 +104,9 @@ export var listBodySchema = function listBodySchema(schema) {
98
104
  });
99
105
  };
100
106
 
101
- //============================创建一个sqlite表============================//
107
+ // =============================================================================
108
+ // 建表 —— 表 + 5 份 zod schema
109
+ // =============================================================================
102
110
  /**
103
111
  * 基于 drizzle + drizzle-zod 快速生成带基础字段(id / 创建时间等)的
104
112
  * 表定义以及 select / insert / update / query / list zod schema。
@@ -106,10 +114,12 @@ export var listBodySchema = function listBodySchema(schema) {
106
114
  export var createTableSchema = function createTableSchema(_ref) {
107
115
  var name = _ref.name,
108
116
  columns = _ref.columns,
117
+ serverColumns = _ref.serverColumns,
109
118
  extraConfig = _ref.extraConfig;
110
- var mergedColumns = _objectSpread(_objectSpread({}, basicFields), columns);
119
+ var mergedColumns = _objectSpread(_objectSpread(_objectSpread({}, basicFields), serverColumns), columns);
111
120
  var table = sqliteTable(name, mergedColumns, extraConfig);
112
121
  var selectSchema = createSelectSchema(table);
122
+ // 只从 columns 推 —— basicFields 与 serverColumns 都由服务端写,不该出现在请求体里。
113
123
  var insertSchema = createInsertSchema(sqliteTable(name, columns));
114
124
  var updateSchema = createUpdateSchema(sqliteTable(name, _objectSpread({
115
125
  id: text("id")
@@ -70,7 +70,7 @@ function createPostOperationFactory({
70
70
  });
71
71
  const body = await (0, import_operation_common.readPostBody)(req, { contentType, parseBody });
72
72
  const extraBody = await (setBody == null ? void 0 : setBody(req)) || {};
73
- const params = { ...scopeStamp(scope), ...body, ...extraBody };
73
+ const params = { ...body, ...extraBody, ...scopeStamp(scope) };
74
74
  const raw = table && db ? (await createAction({
75
75
  bodySchema: schemas.body,
76
76
  db,
@@ -130,13 +130,8 @@ function createPutOperationFactory({
130
130
  const extraBody = await (setBody == null ? void 0 : setBody(
131
131
  req
132
132
  )) || {};
133
- const params = {
134
- // 「谁改的」与「能不能改」是两件事,所以这里自己取一次 —— 取值器带缓存,
135
- // 上面 resolveAccess 若已取过就不会再往返一次。
136
- editorId: (_a = await loadSession()) == null ? void 0 : _a.userId,
137
- ...body,
138
- ...extraBody
139
- };
133
+ const editorId = (_a = await loadSession()) == null ? void 0 : _a.userId;
134
+ const params = { ...body, ...extraBody, editorId };
140
135
  const raw = table && db ? await createAction({
141
136
  bodySchema: schemas.body,
142
137
  db,