@maiyunnet/kebab 5.1.0 → 5.3.0

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/index.d.ts CHANGED
@@ -5,7 +5,7 @@
5
5
  * --- 本文件用来定义每个目录实体地址的常量 ---
6
6
  */
7
7
  /** --- 当前系统版本号 --- */
8
- export declare const VER = "5.1.0";
8
+ export declare const VER = "5.3.0";
9
9
  /** --- 框架根目录,以 / 结尾 --- */
10
10
  export declare const ROOT_PATH: string;
11
11
  export declare const LIB_PATH: string;
package/index.js CHANGED
@@ -6,7 +6,7 @@
6
6
  * --- 本文件用来定义每个目录实体地址的常量 ---
7
7
  */
8
8
  /** --- 当前系统版本号 --- */
9
- export const VER = '5.1.0';
9
+ export const VER = '5.3.0';
10
10
  // --- 服务端用的路径 ---
11
11
  const imu = decodeURIComponent(import.meta.url).replace('file://', '').replace(/^\/(\w:)/, '$1');
12
12
  /** --- /xxx/xxx --- */
package/lib/vector.d.ts CHANGED
@@ -29,9 +29,9 @@ export declare class Vector {
29
29
  'collection': string;
30
30
  /** --- 查询的向量 --- */
31
31
  'data': number[];
32
- /** --- 过滤器,如 word_count > 0 --- */
32
+ /** --- 过滤器,如 word_count > 0 and book_id in [1, 2, 3] --- */
33
33
  'filter'?: string;
34
- /** --- 返回的结果数量,默认为 20 --- */
34
+ /** --- 返回的结果数量,默认为 3 --- */
35
35
  'limit'?: number;
36
36
  /** --- 计算两个向量相似度的度量,默认 L2 --- */
37
37
  'metric'?: 'L2' | 'IP' | 'COSINE';
@@ -45,6 +45,20 @@ export declare class Vector {
45
45
  metric_type: "L2" | "IP" | "COSINE";
46
46
  output_fields: string[] | undefined;
47
47
  }>>;
48
+ /** --- 插入数据 --- */
49
+ insert(data: {
50
+ /** --- 表名 --- */
51
+ 'collection': string;
52
+ /** --- 要插入的数据 --- */
53
+ 'data': milvus.RowData[];
54
+ }): Promise<milvus.MutationResult | false>;
55
+ /** --- 删除数据 --- */
56
+ delete(data: {
57
+ /** --- 表名 --- */
58
+ 'collection': string;
59
+ /** --- 过滤器,如 word_count > 0 and book_id in [1, 2, 3] --- */
60
+ 'filter': string;
61
+ }): Promise<milvus.MutationResult | false>;
48
62
  /**
49
63
  * --- 从连接池中获取一个符合要求的连接 ---
50
64
  */
package/lib/vector.js CHANGED
@@ -24,7 +24,7 @@ export class Vector {
24
24
  'collection_name': data.collection,
25
25
  'data': data.data,
26
26
  'filter': data.filter,
27
- 'limit': data.limit ?? 20,
27
+ 'limit': data.limit ?? 3,
28
28
  'metric_type': data.metric ?? 'L2',
29
29
  'output_fields': data.fields,
30
30
  });
@@ -34,6 +34,40 @@ export class Vector {
34
34
  return false;
35
35
  }
36
36
  }
37
+ /** --- 插入数据 --- */
38
+ async insert(data) {
39
+ const link = await this._getConnection();
40
+ if (!link) {
41
+ return false;
42
+ }
43
+ try {
44
+ const res = await link.insert({
45
+ 'collection_name': data.collection,
46
+ 'data': data.data,
47
+ });
48
+ return res;
49
+ }
50
+ catch {
51
+ return false;
52
+ }
53
+ }
54
+ /** --- 删除数据 --- */
55
+ async delete(data) {
56
+ const link = await this._getConnection();
57
+ if (!link) {
58
+ return false;
59
+ }
60
+ try {
61
+ const res = await link.delete({
62
+ 'collection_name': data.collection,
63
+ 'filter': data.filter,
64
+ });
65
+ return res;
66
+ }
67
+ catch {
68
+ return false;
69
+ }
70
+ }
37
71
  /**
38
72
  * --- 从连接池中获取一个符合要求的连接 ---
39
73
  */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@maiyunnet/kebab",
3
- "version": "5.1.0",
3
+ "version": "5.3.0",
4
4
  "description": "Simple, easy-to-use, and fully-featured Node.js framework that is ready-to-use out of the box.",
5
5
  "type": "module",
6
6
  "keywords": [
package/sys/mod.d.ts CHANGED
@@ -40,6 +40,8 @@ export default class Mod {
40
40
  protected static _$key: string;
41
41
  /** --- 若使用 _$key 并且有多个 unique 索引,这里指定 _$key 的索引名 --- */
42
42
  protected static _$index: string;
43
+ /** --- 前缀,顺序:选项前缀 -> 本前缀 -> 配置文件前缀 --- */
44
+ protected static _$pre: string | null;
43
45
  /** --- 要 update 的内容 --- */
44
46
  protected _updates: Record<string, boolean>;
45
47
  /** --- 模型获取的属性 --- */
@@ -94,7 +96,8 @@ export default class Mod {
94
96
  * @param opt 选项
95
97
  */
96
98
  static insert(db: lDb.Pool | lDb.Transaction, cs: string[] | Record<string, any>, vs?: any[] | any[][], opt?: {
97
- 'pre'?: sCtr.Ctr | string;
99
+ 'pre'?: string;
100
+ 'ctr'?: sCtr.Ctr;
98
101
  'index'?: string;
99
102
  }): Promise<boolean | null | false>;
100
103
  /**
@@ -105,7 +108,8 @@ export default class Mod {
105
108
  * @param opt 选项
106
109
  */
107
110
  static insertSql(db: lDb.Pool | lDb.Transaction, cs: string[] | Record<string, any>, vs?: any[] | any[][], opt?: {
108
- 'pre'?: sCtr.Ctr | string;
111
+ 'pre'?: string;
112
+ 'ctr'?: sCtr.Ctr;
109
113
  'index'?: string;
110
114
  }): string;
111
115
  /**
@@ -116,7 +120,8 @@ export default class Mod {
116
120
  */
117
121
  static removeByWhere(db: lDb.Pool | lDb.Transaction, where: string | kebab.Json, opt?: {
118
122
  'raw'?: boolean;
119
- 'pre'?: sCtr.Ctr | string;
123
+ 'pre'?: string;
124
+ 'ctr'?: sCtr.Ctr;
120
125
  'index'?: string | string[];
121
126
  'by'?: [string | string[], 'DESC' | 'ASC'];
122
127
  'limit'?: [number, number?];
@@ -129,7 +134,8 @@ export default class Mod {
129
134
  */
130
135
  static removeByWhereSql(db: lDb.Pool | lDb.Transaction, where: string | kebab.Json, opt?: {
131
136
  'raw'?: boolean;
132
- 'pre'?: sCtr.Ctr | string;
137
+ 'pre'?: string;
138
+ 'ctr'?: sCtr.Ctr;
133
139
  'index'?: string;
134
140
  'by'?: [string | string[], 'DESC' | 'ASC'];
135
141
  'limit'?: [number, number?];
@@ -143,7 +149,8 @@ export default class Mod {
143
149
  */
144
150
  static updateByWhere(db: lDb.Pool | lDb.Transaction, data: kebab.Json, where: string | kebab.Json, opt?: {
145
151
  'raw'?: boolean;
146
- 'pre'?: sCtr.Ctr | string;
152
+ 'pre'?: string;
153
+ 'ctr'?: sCtr.Ctr;
147
154
  'index'?: string | string[];
148
155
  'by'?: [string | string[], 'DESC' | 'ASC'];
149
156
  'limit'?: [number, number?];
@@ -157,7 +164,8 @@ export default class Mod {
157
164
  */
158
165
  static updateByWhereSql(db: lDb.Pool | lDb.Transaction, data: kebab.Json, where: string | kebab.Json, opt?: {
159
166
  'raw'?: boolean;
160
- 'pre'?: sCtr.Ctr | string;
167
+ 'pre'?: string;
168
+ 'ctr'?: sCtr.Ctr;
161
169
  'index'?: string;
162
170
  'by'?: [string | string[], 'DESC' | 'ASC'];
163
171
  'limit'?: [number, number?];
package/sys/mod.js CHANGED
@@ -7,7 +7,6 @@ import * as lSql from '#kebab/lib/sql.js';
7
7
  import * as lDb from '#kebab/lib/db.js';
8
8
  import * as lCore from '#kebab/lib/core.js';
9
9
  import * as lText from '#kebab/lib/text.js';
10
- import * as sCtr from '#kebab/sys/ctr.js';
11
10
  /** --- 条数列表 --- */
12
11
  class Rows {
13
12
  constructor(initialItems = []) {
@@ -54,6 +53,8 @@ export default class Mod {
54
53
  static { this._$key = ''; }
55
54
  /** --- 若使用 _$key 并且有多个 unique 索引,这里指定 _$key 的索引名 --- */
56
55
  static { this._$index = ''; }
56
+ /** --- 前缀,顺序:选项前缀 -> 本前缀 -> 配置文件前缀 --- */
57
+ static { this._$pre = null; }
57
58
  /**
58
59
  * --- 构造函数 ---
59
60
  * @param ctr Ctr 对象
@@ -79,7 +80,7 @@ export default class Mod {
79
80
  /** --- 导入数据库连接 --- */
80
81
  this._db = opt.db;
81
82
  /** --- 新建 sql 对象 --- */
82
- this._sql = lSql.get(opt.pre ?? opt.ctr, {
83
+ this._sql = lSql.get(opt.pre ?? this.constructor._$pre ?? opt.ctr, {
83
84
  'service': this._db.getService() ?? lDb.ESERVICE.PGSQL,
84
85
  });
85
86
  if (opt.index) {
@@ -118,7 +119,7 @@ export default class Mod {
118
119
  * @param opt 选项
119
120
  */
120
121
  static async insert(db, cs, vs, opt = {}) {
121
- const sq = lSql.get(opt.pre, {
122
+ const sq = lSql.get(opt.pre ?? this.constructor._$pre ?? opt.ctr, {
122
123
  'service': db.getService() ?? lDb.ESERVICE.PGSQL,
123
124
  });
124
125
  if (!vs) {
@@ -127,7 +128,7 @@ export default class Mod {
127
128
  sq.values(cs);
128
129
  const r = await db.execute(sq.getSql(), sq.getData());
129
130
  if (r.packet === null) {
130
- lCore.log(opt.pre instanceof sCtr.Ctr ? opt.pre : {}, '[insert, mod] ' + lText.stringifyJson(r.error?.message ?? '').slice(1, -1), '-error');
131
+ lCore.log(opt.ctr ?? {}, '[MOD][insert] ' + lText.stringifyJson(r.error?.message ?? '').slice(1, -1), '-error');
131
132
  return false;
132
133
  }
133
134
  return r.packet.affected ? true : null;
@@ -147,7 +148,7 @@ export default class Mod {
147
148
  sq.values(cs, vs.slice(i * line, (i + 1) * line));
148
149
  const r = await db.execute(sq.getSql(), sq.getData());
149
150
  if (r.packet === null) {
150
- lCore.log(opt.pre instanceof sCtr.Ctr ? opt.pre : {}, '[insert, mod] ' + lText.stringifyJson(r.error?.message ?? '').slice(1, -1), '-error');
151
+ lCore.log(opt.ctr ?? {}, '[MOD][insert] ' + lText.stringifyJson(r.error?.message ?? '').slice(1, -1), '-error');
151
152
  return false;
152
153
  }
153
154
  if (r.packet.affected) {
@@ -165,7 +166,7 @@ export default class Mod {
165
166
  * @param opt 选项
166
167
  */
167
168
  static insertSql(db, cs, vs, opt = {}) {
168
- const sq = lSql.get(opt.pre, {
169
+ const sq = lSql.get(opt.pre ?? this.constructor._$pre ?? opt.ctr, {
169
170
  'service': db.getService() ?? lDb.ESERVICE.PGSQL,
170
171
  });
171
172
  sq.insert(this._$table + (opt.index ? ('_' + opt.index) : '')).values(cs, vs);
@@ -181,7 +182,7 @@ export default class Mod {
181
182
  const indexs = opt.index ? (typeof opt.index === 'string' ? [opt.index] : [...new Set(opt.index)]) : [''];
182
183
  let ar = 0;
183
184
  for (const index of indexs) {
184
- const sq = lSql.get(opt.pre, {
185
+ const sq = lSql.get(opt.pre ?? this.constructor._$pre ?? opt.ctr, {
185
186
  'service': db.getService() ?? lDb.ESERVICE.PGSQL,
186
187
  });
187
188
  sq.delete(this._$table + (index ? ('_' + index) : '')).where(where);
@@ -193,7 +194,7 @@ export default class Mod {
193
194
  }
194
195
  const r = await db.execute(sq.getSql(), sq.getData());
195
196
  if (r.packet === null) {
196
- lCore.log(opt.pre instanceof sCtr.Ctr ? opt.pre : {}, '[MOD][removeByWhere] ' + lText.stringifyJson(r.error?.message ?? '').slice(1, -1).replace(/"/g, '""'), '-error');
197
+ lCore.log(opt.ctr ?? {}, '[MOD][removeByWhere] ' + lText.stringifyJson(r.error?.message ?? '').slice(1, -1).replace(/"/g, '""'), '-error');
197
198
  return false;
198
199
  }
199
200
  if (r.packet.affected) {
@@ -209,7 +210,7 @@ export default class Mod {
209
210
  * @param opt 选项
210
211
  */
211
212
  static removeByWhereSql(db, where, opt = {}) {
212
- const sq = lSql.get(opt.pre, {
213
+ const sq = lSql.get(opt.pre ?? this.constructor._$pre ?? opt.ctr, {
213
214
  'service': db.getService() ?? lDb.ESERVICE.PGSQL,
214
215
  });
215
216
  sq.delete(this._$table + (opt.index ? ('_' + opt.index) : '')).where(where);
@@ -232,7 +233,7 @@ export default class Mod {
232
233
  const indexs = opt.index ? (typeof opt.index === 'string' ? [opt.index] : [...new Set(opt.index)]) : [''];
233
234
  let ar = 0;
234
235
  for (const index of indexs) {
235
- const sq = lSql.get(opt.pre, {
236
+ const sq = lSql.get(opt.pre ?? this.constructor._$pre ?? opt.ctr, {
236
237
  'service': db.getService() ?? lDb.ESERVICE.PGSQL,
237
238
  });
238
239
  sq.update(this._$table + (index ? ('_' + index) : ''), data).where(where);
@@ -244,7 +245,7 @@ export default class Mod {
244
245
  }
245
246
  const r = await db.execute(sq.getSql(), sq.getData());
246
247
  if (r.packet === null) {
247
- lCore.log(opt.pre instanceof sCtr.Ctr ? opt.pre : {}, '[MOD][updateByWhere] ' + lText.stringifyJson(r.error?.message ?? '').slice(1, -1).replace(/"/g, '""'), '-error');
248
+ lCore.log(opt.ctr ?? {}, '[MOD][updateByWhere] ' + lText.stringifyJson(r.error?.message ?? '').slice(1, -1).replace(/"/g, '""'), '-error');
248
249
  return false;
249
250
  }
250
251
  if (r.packet.affected) {
@@ -261,7 +262,7 @@ export default class Mod {
261
262
  * @param opt 选项
262
263
  */
263
264
  static updateByWhereSql(db, data, where, opt = {}) {
264
- const sq = lSql.get(opt.pre, {
265
+ const sq = lSql.get(opt.pre ?? this.constructor._$pre ?? opt.ctr, {
265
266
  'service': db.getService() ?? lDb.ESERVICE.PGSQL,
266
267
  });
267
268
  sq.update(this._$table + (opt.index ? ('_' + opt.index) : ''), data).where(where);
@@ -408,7 +409,7 @@ export default class Mod {
408
409
  * @param opt 选项
409
410
  */
410
411
  static async primarys(db, where = '', opt = {}) {
411
- const sq = lSql.get(opt.pre ?? opt.ctr, {
412
+ const sq = lSql.get(opt.pre ?? this.constructor._$pre ?? opt.ctr, {
412
413
  'service': db.getService() ?? lDb.ESERVICE.PGSQL,
413
414
  });
414
415
  sq.select(this._$primary, this._$table + (opt.index ? ('_' + opt.index) : '')).where(where);
@@ -573,7 +573,8 @@ Result:<pre id="result">Nothing.</pre>` + this._getEnd();
573
573
  await mTest.removeByWhere(db, [
574
574
  ['token', 'LIKE', 'test_%'],
575
575
  ], {
576
- 'pre': this._get['s'] === 'pgsql' ? 'm' : this,
576
+ 'ctr': this,
577
+ 'pre': this._get['s'] === 'pgsql' ? 'm' : undefined,
577
578
  });
578
579
  return this._location('test/mod-test' + (this._get['s'] === 'pgsql' ? '?s=pgsql' : ''));
579
580
  }
@@ -834,7 +835,7 @@ CREATE TABLE \`m_test_data_0\` (
834
835
  ]);
835
836
  }
836
837
  const res = await mTestData.insert(db, ['test_id', 'content', 'time_add'], datas, {
837
- 'pre': this,
838
+ 'ctr': this,
838
839
  'index': '0',
839
840
  });
840
841
  echo.push('<br><br>Result: ' + JSON.stringify(res));