@kne/fastify-app-manager 0.1.27 → 0.1.29

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.
@@ -1,4 +1,10 @@
1
1
  const fp = require('fastify-plugin');
2
+ const { str, num, bool, any, strArray, list, obj, success, column, tableInfo, cleanupResult, exportResult, DB_SCOPE } = require('../schema/response');
3
+
4
+ const TAG = '应用中心管理后台-数据';
5
+ const APP_NAME = '应用 name';
6
+ const TABLE = '表名,必须是应用所属的表';
7
+ const ROW_ID = '单列主键的值;联合主键用 pk';
2
8
 
3
9
  module.exports = fp(async (fastify, options) => {
4
10
  const { services } = fastify[options.name];
@@ -9,15 +15,33 @@ module.exports = fp(async (fastify, options) => {
9
15
  {
10
16
  onRequest: auth(),
11
17
  schema: {
18
+ tags: [TAG],
12
19
  summary: '列出应用所属数据表',
13
20
  query: {
14
21
  type: 'object',
15
22
  required: ['name'],
16
23
  properties: {
17
- name: { type: 'string' },
18
- scope: { type: 'string', enum: ['owned', 'all'] }
24
+ name: { type: 'string', description: APP_NAME },
25
+ scope: { type: 'string', enum: ['owned', 'all'], description: 'owned 只列应用所属的表(默认),all 列出库中全部表(仅独享库可用)' }
19
26
  }
20
- }
27
+ },
28
+ response: success(
29
+ '数据表与连接信息',
30
+ obj('数据表', {
31
+ dbScope: str(DB_SCOPE),
32
+ softDeleteField: str('软删除字段名,默认 deleted_at'),
33
+ connection: obj('数据库连接信息(不含密码)', {
34
+ dialect: str('数据库类型'),
35
+ host: any('主机(sqlite 为文件路径)'),
36
+ port: any('端口'),
37
+ database: any('数据库名(sqlite 为文件路径)'),
38
+ hasCustomDb: bool('是否配置了应用自有数据库')
39
+ }),
40
+ ownedTables: strArray('登记为应用所属的表'),
41
+ pageData: list('表结构', tableInfo),
42
+ totalCount: num('表数量')
43
+ })
44
+ )
21
45
  }
22
46
  },
23
47
  async request => services.dbops.listTables(request.query)
@@ -28,16 +52,18 @@ module.exports = fp(async (fastify, options) => {
28
52
  {
29
53
  onRequest: auth(),
30
54
  schema: {
55
+ tags: [TAG],
31
56
  summary: '登记应用所属表',
32
57
  body: {
33
58
  type: 'object',
34
59
  required: ['name', 'tables'],
35
60
  properties: {
36
- name: { type: 'string' },
37
- tables: { type: 'array', items: { type: 'string' } },
38
- mode: { type: 'string', enum: ['union', 'replace'] }
61
+ name: { type: 'string', description: APP_NAME },
62
+ tables: { type: 'array', items: { type: 'string' }, description: '要登记的表名' },
63
+ mode: { type: 'string', enum: ['union', 'replace'], description: 'union 与已登记的合并(默认),replace 整体替换' }
39
64
  }
40
- }
65
+ },
66
+ response: success('登记结果', obj('登记结果', { ownedTables: strArray('登记后的所属表'), dbScope: str(DB_SCOPE) }))
41
67
  }
42
68
  },
43
69
  async request => services.dbops.registerTables(request.body)
@@ -48,14 +74,25 @@ module.exports = fp(async (fastify, options) => {
48
74
  {
49
75
  onRequest: auth(),
50
76
  schema: {
77
+ tags: [TAG],
51
78
  summary: '扫描数据库并登记本应用可管理表(共享库仅领取未被其它应用登记的表)',
79
+ description: '共享库优先领取带应用表前缀的表,并在未设置时写入 DB_TABLE_PREFIX 环境变量',
52
80
  body: {
53
81
  type: 'object',
54
82
  required: ['name'],
55
83
  properties: {
56
- name: { type: 'string' }
84
+ name: { type: 'string', description: APP_NAME }
57
85
  }
58
- }
86
+ },
87
+ response: success(
88
+ '扫描结果',
89
+ obj('扫描结果', {
90
+ dbScope: str(DB_SCOPE),
91
+ ownedTables: strArray('登记后的所属表'),
92
+ discovered: strArray('库中扫描到的表(不含系统表)'),
93
+ claimed: strArray('本次新领取的表')
94
+ })
95
+ )
59
96
  }
60
97
  },
61
98
  async request => services.dbops.syncOwnedFromDatabase(request.body)
@@ -66,24 +103,37 @@ module.exports = fp(async (fastify, options) => {
66
103
  {
67
104
  onRequest: auth(),
68
105
  schema: {
106
+ tags: [TAG],
69
107
  summary: '分页查询表数据',
108
+ description: '默认不返回已软删除(软删除字段非空)的行',
70
109
  query: {
71
110
  type: 'object',
72
111
  required: ['name', 'table'],
73
112
  properties: {
74
- name: { type: 'string' },
75
- table: { type: 'string' },
76
- currentPage: { type: 'number', default: 1 },
77
- perPage: { type: 'number', default: 20 },
113
+ name: { type: 'string', description: APP_NAME },
114
+ table: { type: 'string', description: TABLE },
115
+ currentPage: { type: 'number', default: 1, description: '页码,从 1 开始' },
116
+ perPage: { type: 'number', default: 20, description: '每页条数' },
78
117
  filter: { type: 'string', description: 'JSON object: equality or range { $gte,$lte,$gt,$lt }' },
79
- includeDeleted: { type: 'boolean', default: false },
118
+ includeDeleted: { type: 'boolean', default: false, description: '是否包含已软删除的行' },
80
119
  keyword: { type: 'string', description: 'Fuzzy search across string columns (OR LIKE)' },
81
120
  sort: {
82
121
  type: 'string',
83
122
  description: 'JSON array of { name, sort: ASC|DESC } for ORDER BY'
84
123
  }
85
124
  }
86
- }
125
+ },
126
+ response: success(
127
+ '表数据与表结构',
128
+ obj('表数据', {
129
+ pageData: list('当前页的行(原始列名与值)', obj('行')),
130
+ totalCount: num('符合条件的总行数'),
131
+ primaryKey: strArray('主键列名'),
132
+ columns: list('列信息', column),
133
+ softDeleteField: str('软删除字段名'),
134
+ softDeleteSupported: bool('表中是否有软删除字段')
135
+ })
136
+ )
87
137
  }
88
138
  },
89
139
  async request => {
@@ -115,17 +165,19 @@ module.exports = fp(async (fastify, options) => {
115
165
  {
116
166
  onRequest: auth(),
117
167
  schema: {
168
+ tags: [TAG],
118
169
  summary: '按主键查询一行',
119
170
  query: {
120
171
  type: 'object',
121
172
  required: ['name', 'table'],
122
173
  properties: {
123
- name: { type: 'string' },
124
- table: { type: 'string' },
125
- id: {},
174
+ name: { type: 'string', description: APP_NAME },
175
+ table: { type: 'string', description: TABLE },
176
+ id: { description: ROW_ID },
126
177
  pk: { type: 'string', description: 'JSON primary key object' }
127
178
  }
128
- }
179
+ },
180
+ response: success('查询结果', obj('查询结果', { data: obj('行数据,不存在为 null'), primaryKey: strArray('主键列名') }))
129
181
  }
130
182
  },
131
183
  async request => {
@@ -142,21 +194,24 @@ module.exports = fp(async (fastify, options) => {
142
194
  {
143
195
  onRequest: auth(),
144
196
  schema: {
197
+ tags: [TAG],
145
198
  summary: '插入或更新一行(autoGenerate 可对主键走雪花自动生成)',
199
+ description: '主键值已存在时更新,否则插入',
146
200
  body: {
147
201
  type: 'object',
148
202
  required: ['name', 'table', 'data'],
149
203
  properties: {
150
- name: { type: 'string' },
151
- table: { type: 'string' },
152
- data: { type: 'object' },
204
+ name: { type: 'string', description: APP_NAME },
205
+ table: { type: 'string', description: TABLE },
206
+ data: { type: 'object', description: '行数据 { 列名: 值 }' },
153
207
  autoGenerate: {
154
208
  type: 'object',
155
209
  additionalProperties: { type: 'boolean' },
156
210
  description: '主键字段名 → 是否用 snowflake generateId 自动填充(仅空值时)'
157
211
  }
158
212
  }
159
- }
213
+ },
214
+ response: success('保存结果', obj('保存结果', { data: obj('保存后的行'), action: str('执行的操作:insert 插入,update 更新') }))
160
215
  }
161
216
  },
162
217
  async request => services.dbops.saveRow(request.body)
@@ -167,18 +222,27 @@ module.exports = fp(async (fastify, options) => {
167
222
  {
168
223
  onRequest: auth(),
169
224
  schema: {
225
+ tags: [TAG],
170
226
  summary: '按主键软删除一行(默认写 deleted_at;hard=true 才物理删除)',
171
227
  body: {
172
228
  type: 'object',
173
229
  required: ['name', 'table'],
174
230
  properties: {
175
- name: { type: 'string' },
176
- table: { type: 'string' },
177
- id: {},
178
- pk: { type: 'object' },
179
- hard: { type: 'boolean', default: false }
231
+ name: { type: 'string', description: APP_NAME },
232
+ table: { type: 'string', description: TABLE },
233
+ id: { description: ROW_ID },
234
+ pk: { type: 'object', description: '主键对象 { 列名: 值 }' },
235
+ hard: { type: 'boolean', default: false, description: '是否物理删除' }
180
236
  }
181
- }
237
+ },
238
+ response: success(
239
+ '删除结果',
240
+ obj('删除结果', {
241
+ ok: bool('是否成功'),
242
+ mode: str('删除方式:soft 软删除,hard 物理删除'),
243
+ softDeleteField: str('软删除字段名(软删除时返回)')
244
+ })
245
+ )
182
246
  }
183
247
  },
184
248
  async request => services.dbops.removeRow(request.body)
@@ -189,17 +253,19 @@ module.exports = fp(async (fastify, options) => {
189
253
  {
190
254
  onRequest: auth(),
191
255
  schema: {
256
+ tags: [TAG],
192
257
  summary: '按主键恢复软删除行(清空 deleted_at)',
193
258
  body: {
194
259
  type: 'object',
195
260
  required: ['name', 'table'],
196
261
  properties: {
197
- name: { type: 'string' },
198
- table: { type: 'string' },
199
- id: {},
200
- pk: { type: 'object' }
262
+ name: { type: 'string', description: APP_NAME },
263
+ table: { type: 'string', description: TABLE },
264
+ id: { description: ROW_ID },
265
+ pk: { type: 'object', description: '主键对象 { 列名: 值 }' }
201
266
  }
202
- }
267
+ },
268
+ response: success('恢复结果', obj('恢复结果', { ok: bool('是否成功'), softDeleteField: str('软删除字段名') }))
203
269
  }
204
270
  },
205
271
  async request => services.dbops.restoreRow(request.body)
@@ -210,17 +276,28 @@ module.exports = fp(async (fastify, options) => {
210
276
  {
211
277
  onRequest: auth(),
212
278
  schema: {
279
+ tags: [TAG],
213
280
  summary: '执行只读查询 SQL',
281
+ description: '只允许只读语句;共享库中只能查询应用所属的表。结果最多返回插件配置的 dbQueryMaxRows 行',
214
282
  body: {
215
283
  type: 'object',
216
284
  required: ['name', 'sql'],
217
285
  properties: {
218
- name: { type: 'string' },
219
- sql: { type: 'string' },
220
- replacements: { type: 'object' },
221
- maxRows: { type: 'number' }
286
+ name: { type: 'string', description: APP_NAME },
287
+ sql: { type: 'string', description: '只读查询 SQL' },
288
+ replacements: { type: 'object', description: 'SQL 命名参数 { 参数名: 值 }' },
289
+ maxRows: { type: 'number', description: '最多返回的行数,不超过插件配置的 dbQueryMaxRows' }
222
290
  }
223
- }
291
+ },
292
+ response: success(
293
+ '查询结果',
294
+ obj('查询结果', {
295
+ columns: strArray('列名(取第一行的键)'),
296
+ rows: list('结果行', obj('行')),
297
+ rowCount: num('返回的行数'),
298
+ truncated: bool('是否因超过最大行数被截断')
299
+ })
300
+ )
224
301
  }
225
302
  },
226
303
  async request => services.dbops.runQuery(request.body)
@@ -231,16 +308,19 @@ module.exports = fp(async (fastify, options) => {
231
308
  {
232
309
  onRequest: auth(),
233
310
  schema: {
311
+ tags: [TAG],
234
312
  summary: '导出应用所属表数据',
313
+ description: '导出文件写到服务器上的应用 exports 目录',
235
314
  body: {
236
315
  type: 'object',
237
316
  required: ['name'],
238
317
  properties: {
239
- name: { type: 'string' },
240
- tables: { type: 'array', items: { type: 'string' } },
241
- mode: { type: 'string', enum: ['json', 'file'] }
318
+ name: { type: 'string', description: APP_NAME },
319
+ tables: { type: 'array', items: { type: 'string' }, description: '只导出这些表(须为所属表);不传导出全部所属表' },
320
+ mode: { type: 'string', enum: ['json', 'file'], description: 'json 按表导出为 JSON 打包成 zip(默认),file 复制 sqlite 文件(仅独享 sqlite 库)' }
242
321
  }
243
- }
322
+ },
323
+ response: success('导出结果', exportResult)
244
324
  }
245
325
  },
246
326
  async request => services.dbops.exportData(request.body)
@@ -251,16 +331,19 @@ module.exports = fp(async (fastify, options) => {
251
331
  {
252
332
  onRequest: auth(),
253
333
  schema: {
334
+ tags: [TAG],
254
335
  summary: '清理应用所属表(失败时返回手工 SQL)',
336
+ description: '删除应用所属的表及其迁移记录;成功删除的表从所属表中移除',
255
337
  body: {
256
338
  type: 'object',
257
339
  required: ['name'],
258
340
  properties: {
259
- name: { type: 'string' },
260
- dryRun: { type: 'boolean' },
261
- removeSqliteFile: { type: 'boolean' }
341
+ name: { type: 'string', description: APP_NAME },
342
+ dryRun: { type: 'boolean', description: '只返回将要执行的 SQL,不实际删除' },
343
+ removeSqliteFile: { type: 'boolean', description: '独享 sqlite 库时同时删除数据库文件' }
262
344
  }
263
- }
345
+ },
346
+ response: success('清理结果', cleanupResult)
264
347
  }
265
348
  },
266
349
  async request => services.dbops.cleanup(request.body)
@@ -1,12 +1,16 @@
1
1
  const fp = require('fastify-plugin');
2
+ const { str, num, bool, any, list, obj, success, migrationResult } = require('../schema/response');
3
+
4
+ const TAG = '应用中心管理后台-迁移脚本';
5
+ const SQL_FILE = 'SQL 文件名:只能包含 [A-Za-z0-9_-.] 且以 .sql 结尾';
2
6
 
3
7
  module.exports = fp(async (fastify, options) => {
4
8
  const { services } = fastify[options.name];
5
9
  const auth = () => options.createAuthenticate();
6
10
 
7
11
  const versionProps = {
8
- name: { type: 'string' },
9
- versionId: { type: 'string' }
12
+ name: { type: 'string', description: '应用 name' },
13
+ versionId: { type: 'string', description: '版本 ID' }
10
14
  };
11
15
 
12
16
  fastify.get(
@@ -14,12 +18,35 @@ module.exports = fp(async (fastify, options) => {
14
18
  {
15
19
  onRequest: auth(),
16
20
  schema: {
21
+ tags: [TAG],
17
22
  summary: '版本迁移脚本列表(含执行状态)',
23
+ description: '脚本内容以数据库为准;执行状态读自应用数据库的迁移记录表,读取失败时 executed 为 null 并返回 dbError',
18
24
  query: {
19
25
  type: 'object',
20
26
  required: ['name', 'versionId'],
21
27
  properties: versionProps
22
- }
28
+ },
29
+ response: success(
30
+ '迁移脚本列表',
31
+ obj('迁移脚本列表', {
32
+ versionId: any('版本 ID'),
33
+ version: str('版本号'),
34
+ migrationPath: str('相对 server 的 SQL 目录'),
35
+ migrateBeforeStart: bool('插件是否开启部署时执行迁移'),
36
+ dbError: str('读取执行记录失败的原因,成功为 null'),
37
+ pageData: list(
38
+ '迁移脚本,按文件名排序',
39
+ obj('迁移脚本', {
40
+ name: str('文件名'),
41
+ size: num('内容大小(字节)'),
42
+ updatedAt: any('最后修改时间'),
43
+ executed: bool('是否已执行,读取执行记录失败时为 null'),
44
+ executedAt: any('执行时间')
45
+ })
46
+ ),
47
+ totalCount: num('脚本数量')
48
+ })
49
+ )
23
50
  }
24
51
  },
25
52
  async request => services.migration.list(request.query)
@@ -30,12 +57,14 @@ module.exports = fp(async (fastify, options) => {
30
57
  {
31
58
  onRequest: auth(),
32
59
  schema: {
60
+ tags: [TAG],
33
61
  summary: '读取迁移脚本内容',
34
62
  query: {
35
63
  type: 'object',
36
64
  required: ['name', 'versionId', 'file'],
37
- properties: Object.assign({}, versionProps, { file: { type: 'string' } })
38
- }
65
+ properties: Object.assign({}, versionProps, { file: { type: 'string', description: SQL_FILE } })
66
+ },
67
+ response: success('脚本内容', obj('脚本内容', { name: str('文件名'), content: str('SQL 内容') }))
39
68
  }
40
69
  },
41
70
  async request => services.migration.content(request.query)
@@ -46,15 +75,18 @@ module.exports = fp(async (fastify, options) => {
46
75
  {
47
76
  onRequest: auth(),
48
77
  schema: {
78
+ tags: [TAG],
49
79
  summary: '新增或覆盖迁移脚本',
80
+ description: '保存到数据库,版本目录在磁盘上时同步写入对应 .sql 文件',
50
81
  body: {
51
82
  type: 'object',
52
83
  required: ['name', 'versionId', 'file', 'content'],
53
84
  properties: Object.assign({}, versionProps, {
54
- file: { type: 'string' },
55
- content: { type: 'string' }
85
+ file: { type: 'string', description: SQL_FILE },
86
+ content: { type: 'string', description: 'SQL 内容' }
56
87
  })
57
- }
88
+ },
89
+ response: success('保存结果', obj('保存结果', { name: str('文件名') }))
58
90
  }
59
91
  },
60
92
  async request => services.migration.save(request.body)
@@ -65,12 +97,14 @@ module.exports = fp(async (fastify, options) => {
65
97
  {
66
98
  onRequest: auth(),
67
99
  schema: {
100
+ tags: [TAG],
68
101
  summary: '删除迁移脚本文件(不改执行记录)',
69
102
  body: {
70
103
  type: 'object',
71
104
  required: ['name', 'versionId', 'file'],
72
- properties: Object.assign({}, versionProps, { file: { type: 'string' } })
73
- }
105
+ properties: Object.assign({}, versionProps, { file: { type: 'string', description: SQL_FILE } })
106
+ },
107
+ response: success('删除结果', obj('删除结果', { name: str('文件名') }))
74
108
  }
75
109
  },
76
110
  async request => services.migration.remove(request.body)
@@ -81,15 +115,18 @@ module.exports = fp(async (fastify, options) => {
81
115
  {
82
116
  onRequest: auth(),
83
117
  schema: {
118
+ tags: [TAG],
84
119
  summary: '执行 / 标记已执行 / 撤销执行记录',
120
+ description: 'execute 在应用数据库执行脚本并记录,mark 只记录为已执行,unmark 删除执行记录(不回滚已执行的 SQL)',
85
121
  body: {
86
122
  type: 'object',
87
123
  required: ['name', 'versionId', 'file', 'action'],
88
124
  properties: Object.assign({}, versionProps, {
89
- file: { type: 'string' },
90
- action: { type: 'string', enum: ['execute', 'mark', 'unmark'] }
125
+ file: { type: 'string', description: SQL_FILE },
126
+ action: { type: 'string', enum: ['execute', 'mark', 'unmark'], description: '操作:execute 执行,mark 标记已执行,unmark 撤销执行记录' }
91
127
  })
92
- }
128
+ },
129
+ response: success('操作结果', migrationResult)
93
130
  }
94
131
  },
95
132
  async request => services.migration.action(request.body)