@cloudcome/utils-uni 1.12.0 → 1.14.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/dist/database.cjs CHANGED
@@ -1,55 +1,194 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
- const error = require("@cloudcome/utils-core/error");
4
- const object = require("@cloudcome/utils-core/object");
5
3
  const type = require("@cloudcome/utils-core/type");
4
+ const _helpers = require("./_helpers.cjs");
5
+ const object = require("@cloudcome/utils-core/object");
6
6
  const _try = require("@cloudcome/utils-core/try");
7
+ class DbBaseCommand {
8
+ command;
9
+ parameter;
10
+ constructor(command, parameter) {
11
+ this.command = command;
12
+ this.parameter = parameter;
13
+ }
14
+ getValue(db) {
15
+ return db.command[this.command].call(
16
+ db.command,
17
+ this.parameter
18
+ );
19
+ }
20
+ getExpression(fieldName) {
21
+ return {
22
+ [`$${this.command}`]: [fieldName, this.parameter]
23
+ };
24
+ }
25
+ }
26
+ class DbQueryCommand extends DbBaseCommand {
27
+ isQueryCommand = true;
28
+ }
29
+ class DbMutateCommand extends DbBaseCommand {
30
+ isMutateCommand = true;
31
+ }
32
+ const dbQuery = {
33
+ /**
34
+ * 等于操作符
35
+ * @param value 比较值
36
+ * @returns DbQueryCommand 查询命令对象
37
+ */
38
+ eq: (value) => new DbQueryCommand("eq", value),
39
+ /**
40
+ * 不等于操作符
41
+ * @param value 比较值
42
+ * @returns DbQueryCommand 查询命令对象
43
+ */
44
+ neq: (value) => new DbQueryCommand("neq", value),
45
+ /**
46
+ * 大于操作符
47
+ * @param value 比较值
48
+ * @returns DbQueryCommand 查询命令对象
49
+ */
50
+ gt: (value) => new DbQueryCommand("gt", value),
51
+ /**
52
+ * 大于等于操作符
53
+ * @param value 比较值
54
+ * @returns DbQueryCommand 查询命令对象
55
+ */
56
+ gte: (value) => new DbQueryCommand("gte", value),
57
+ /**
58
+ * 小于操作符
59
+ * @param value 比较值
60
+ * @returns DbQueryCommand 查询命令对象
61
+ */
62
+ lt: (value) => new DbQueryCommand("lt", value),
63
+ /**
64
+ * 小于等于操作符
65
+ * @param value 比较值
66
+ * @returns DbQueryCommand 查询命令对象
67
+ */
68
+ lte: (value) => new DbQueryCommand("lte", value),
69
+ /**
70
+ * 包含在数组中操作符
71
+ * @param value 值数组
72
+ * @returns DbQueryCommand 查询命令对象
73
+ */
74
+ in: (value) => new DbQueryCommand("in", value),
75
+ /**
76
+ * 不包含在数组中操作符
77
+ * @param value 值数组
78
+ * @returns DbQueryCommand 查询命令对象
79
+ */
80
+ nin: (value) => new DbQueryCommand("nin", value),
81
+ /**
82
+ * 逻辑与操作符
83
+ * @param conditions 查询条件参数
84
+ * @returns DbQueryCommand 查询命令对象
85
+ */
86
+ and: (conditions) => new DbQueryCommand("and", conditions),
87
+ /**
88
+ * 逻辑或操作符
89
+ * @param conditions 查询条件参数
90
+ * @returns DbQueryCommand 查询命令对象
91
+ */
92
+ or: (conditions) => new DbQueryCommand("or", conditions),
93
+ /**
94
+ * 数组长度匹配操作符
95
+ * @param size 数组长度
96
+ * @returns DbQueryCommand 查询命令对象
97
+ */
98
+ size: (size) => new DbQueryCommand("size", size)
99
+ };
100
+ const dbMutate = {
101
+ /**
102
+ * 数值增加操作符
103
+ * @param value 增加的数值
104
+ * @returns DbMutateCommand 变更命令对象
105
+ */
106
+ inc: (value) => new DbMutateCommand("inc", value),
107
+ /**
108
+ * 数值乘法操作符
109
+ * @param value 乘数
110
+ * @returns DbMutateCommand 变更命令对象
111
+ */
112
+ mul: (value) => new DbMutateCommand("mul", value),
113
+ /**
114
+ * 设置字段值操作符
115
+ * @param value 设置的值
116
+ * @returns DbMutateCommand 变更命令对象
117
+ */
118
+ set: (value) => new DbMutateCommand("set", value),
119
+ /**
120
+ * 向数组末尾添加元素操作符
121
+ * @param value 添加的值
122
+ * @returns DbMutateCommand 变更命令对象
123
+ */
124
+ push: (value) => new DbMutateCommand("push", value),
125
+ /**
126
+ * 向数组开头添加元素操作符
127
+ * @param value 添加的值
128
+ * @returns DbMutateCommand 变更命令对象
129
+ */
130
+ unshift: (value) => new DbMutateCommand("unshift", value),
131
+ /**
132
+ * 从数组末尾移除元素操作符
133
+ * @returns DbMutateCommand 变更命令对象
134
+ */
135
+ pop: () => new DbMutateCommand("pop", void 0),
136
+ /**
137
+ * 从数组开头移除元素操作符
138
+ * @returns DbMutateCommand 变更命令对象
139
+ */
140
+ shift: () => new DbMutateCommand("shift", void 0),
141
+ /**
142
+ * 移除字段操作符
143
+ * @returns DbMutateCommand 变更命令对象
144
+ */
145
+ remove: () => new DbMutateCommand("remove", void 0)
146
+ };
147
+ const dbAgg = uniCloud.database().command.aggregate;
7
148
  const db0 = uniCloud.database();
8
- const dbCmd = db0.command;
9
- const dbAgg = db0.command.aggregate;
10
149
  let gid = 0;
11
150
  class Db {
12
- #host;
151
+ _host;
13
152
  /**
14
153
  * 是否为事务环境
15
154
  * - 查询条件只能是 id
16
155
  * - 不能聚合操作
17
156
  */
18
- #isTransaction = false;
19
- #options;
157
+ _isTransaction = false;
158
+ _options;
20
159
  /**
21
160
  * 构造函数,初始化数据库集合引用
22
161
  * @param collection 数据表名称
23
162
  * @param _mockDatabase 模拟数据库,用于单元测试
24
163
  */
25
164
  constructor(options) {
26
- this.#options = options;
27
- this.#host = options._mockDatabase || options.transaction?.collection(options.table) || db0.collection(options.table);
28
- this.#isTransaction = !!options.transaction;
165
+ this._options = options;
166
+ this._host = options._mockDatabase || options.transaction?.collection(options.table) || db0.collection(options.table);
167
+ this._isTransaction = !!options.transaction;
29
168
  }
30
169
  get table() {
31
- return this.#options.table;
170
+ return this._options.table;
32
171
  }
33
172
  /**
34
173
  * 获取聚合操作实例
35
174
  * @returns 聚合操作实例
36
175
  */
37
176
  aggregate() {
38
- return this.#host.aggregate();
177
+ return this._host.aggregate();
39
178
  }
40
- #hasWhere = void 0;
41
- #hasWhereId = void 0;
42
- #where = {};
43
- #doWhere(where, from) {
44
- if (this.#hasWhere) throw new Error(`已调用过一次 db.${_toWhereMethod(this.#hasWhere)} 了`);
179
+ _hasWhere = void 0;
180
+ _hasWhereId = void 0;
181
+ _where = {};
182
+ _doWhere(where, from) {
183
+ if (this._hasWhere) throw new Error(`已调用过一次 db.${_toWhereMethod(this._hasWhere)} 了`);
45
184
  const whereKeys = Object.keys(where);
46
185
  const isWhereId = whereKeys.length === 1 && "_id" in where && (type.isString(where._id) || type.isNumber(where._id));
47
- if (isWhereId && this.#hasLimit) {
186
+ if (isWhereId && this._hasLimit) {
48
187
  throw new Error(`db.${_toWhereIdMethod(from)} 方法不能与 db.limit() 方法同时调用`);
49
188
  }
50
- this.#hasWhere = from;
51
- this.#where = where;
52
- if (isWhereId) this.#hasWhereId = from;
189
+ this._hasWhere = from;
190
+ this._where = where;
191
+ if (isWhereId) this._hasWhereId = from;
53
192
  return this;
54
193
  }
55
194
  /**
@@ -58,7 +197,7 @@ class Db {
58
197
  * @returns 当前Db实例,支持链式调用
59
198
  */
60
199
  where(where) {
61
- return this.#doWhere(where, "where");
200
+ return this._doWhere(where, "where");
62
201
  }
63
202
  /**
64
203
  * 根据ID设置查询条件
@@ -66,102 +205,100 @@ class Db {
66
205
  * @returns 当前Db实例,支持链式调用
67
206
  */
68
207
  whereId(id) {
69
- return this.#doWhere({ _id: id }, "whereId");
208
+ return this._doWhere({ _id: id }, "whereId");
70
209
  }
71
- #hasSelect = 0;
72
- #select = {};
210
+ _hasSelect = 0;
211
+ _select = {};
73
212
  /**
74
213
  * 指定要返回的字段
75
214
  * @param fields 要返回的字段对象,true表示返回,false表示不返回
76
215
  * @returns 当前Db实例,支持链式调用
77
216
  */
78
217
  select(fields) {
79
- if (this.#hasSelect) throw new Error("db.select() 方法只能调用一次");
80
- this.#hasSelect++;
81
- this.#select = fields;
218
+ if (this._hasSelect) throw new Error("db.select() 方法只能调用一次");
219
+ this._hasSelect++;
220
+ this._select = fields;
82
221
  return this;
83
222
  }
84
- #hasOrder = 0;
85
- #order = {};
223
+ _hasOrder = 0;
224
+ _order = {};
86
225
  /**
87
226
  * 设置排序规则
88
227
  * @param order 排序规则对象,key为字段名,value为"asc"或"desc"
89
228
  * @returns 当前Db实例,支持链式调用
90
229
  */
91
230
  order(order) {
92
- this.#hasOrder++;
93
- this.#order = order;
231
+ this._hasOrder++;
232
+ this._order = order;
94
233
  return this;
95
234
  }
96
- #hasSkip = 0;
97
- #skip = 0;
235
+ _hasSkip = 0;
236
+ _skip = 0;
98
237
  /**
99
238
  * 跳过指定数量的记录
100
239
  * @param skip 要跳过的记录数
101
240
  * @returns 当前Db实例,支持链式调用
102
241
  */
103
242
  skip(skip) {
104
- if (this.#hasSkip) throw new Error("db.skip() 方法只能调用一次");
105
- this.#hasSkip++;
106
- this.#skip = skip;
243
+ if (this._hasSkip) throw new Error("db.skip() 方法只能调用一次");
244
+ this._hasSkip++;
245
+ this._skip = skip;
107
246
  return this;
108
247
  }
109
- #hasLimit = 0;
110
- #limit = 0;
248
+ _hasLimit = 0;
249
+ _limit = 0;
111
250
  /**
112
251
  * 限制返回的记录数量
113
252
  * @param limit 最大返回记录数
114
253
  * @returns 当前Db实例,支持链式调用
115
254
  */
116
255
  limit(limit) {
117
- if (this.#hasLimit) throw new Error("db.limit() 方法只能调用一次");
118
- if (this.#hasWhereId) {
119
- throw new Error(`db.limit() 方法不能与 ${_toWhereIdMethod(this.#hasWhereId)} 方法同时调用`);
256
+ if (this._hasLimit) throw new Error("db.limit() 方法只能调用一次");
257
+ if (this._hasWhereId) {
258
+ throw new Error(`db.limit() 方法不能与 ${_toWhereIdMethod(this._hasWhereId)} 方法同时调用`);
120
259
  }
121
- this.#hasLimit++;
122
- this.#limit = limit;
260
+ this._hasLimit++;
261
+ this._limit = limit;
123
262
  return this;
124
263
  }
125
- #hasLookup = 0;
264
+ _hasLookup = 0;
126
265
  get hasLookup() {
127
- return this.#hasLookup > 0;
266
+ return this._hasLookup > 0;
128
267
  }
129
- #lookups = [];
268
+ _lookups = [];
130
269
  lookup(table, lookup) {
131
- table.#hasLookup++;
132
- this.#hasLookup++;
133
- this.#lookups.push({
270
+ table._hasLookup++;
271
+ this._hasLookup++;
272
+ this._lookups.push({
134
273
  ...lookup,
135
- table,
136
- from: table.table
274
+ table
137
275
  });
138
276
  return this;
139
277
  }
140
- #aggregated = false;
141
- #endAggregate(aggRef) {
142
- if (this.#aggregated) throw new Error(`相同的数据表实例(${this.table})不能重复使用`);
143
- this.#aggregated = true;
278
+ _aggregated = false;
279
+ _endAggregate(aggRef) {
280
+ if (this._aggregated) throw new Error(`相同的数据表实例(${this.table})不能重复使用`);
281
+ this._aggregated = true;
144
282
  let returnAggRef = aggRef;
145
283
  const projects = {};
146
- for (const { type: type2, as, foreignField, from, localField, table } of this.#lookups) {
147
- const varName = `v${gid++}`;
284
+ for (const { type: type2, as, foreignField, localField, table, unselect } of this._lookups) {
285
+ const letName = `let${gid++}`;
148
286
  let pipeline = dbAgg.pipeline();
149
- pipeline = pipeline.match(
150
- dbCmd.expr(
151
- type2 === "n:1" ? (
152
- // @ts-ignore
153
- dbAgg.in([`$${foreignField}`, `$$${varName}`])
154
- ) : dbAgg.eq([`$${foreignField}`, `$$${varName}`])
155
- )
156
- );
157
- pipeline = table.#endAggregate(pipeline);
287
+ pipeline = pipeline.match({
288
+ $expr: {
289
+ $and: [
290
+ type2 === "n:1" ? { $in: [`$${foreignField}`, `$$${letName}`] } : { $eq: [`$${foreignField}`, `$$${letName}`] }
291
+ ]
292
+ }
293
+ });
294
+ pipeline = table._endAggregate(pipeline);
158
295
  pipeline = pipeline.done();
159
296
  returnAggRef = returnAggRef.lookup({
160
297
  let: {
161
- [varName]: `$${localField}`
298
+ [letName]: `$${localField}`
162
299
  },
163
300
  as,
164
- from,
301
+ from: table.table,
165
302
  pipeline
166
303
  });
167
304
  if (type2 === "1:1") {
@@ -170,30 +307,30 @@ class Db {
170
307
  preserveNullAndEmptyArrays: true
171
308
  });
172
309
  }
173
- projects[as] = true;
310
+ if (as && !unselect) projects[as] = true;
174
311
  }
175
- if (this.#hasWhere) returnAggRef = returnAggRef.match(this.#where);
176
- if (this.#hasSelect) returnAggRef = returnAggRef.project({ ...this.#select, ...projects });
177
- if (this.#hasOrder) returnAggRef = returnAggRef.sort(object.objectMap(this.#order, (v) => v === "asc" ? 1 : -1));
178
- if (this.#hasSkip) returnAggRef = returnAggRef.skip(this.#skip);
179
- if (this.#hasLimit) returnAggRef = returnAggRef.limit(this.#limit);
312
+ if (this._hasWhere) returnAggRef = returnAggRef.match(_mapQueryCommandWhere(this._where));
313
+ if (this._hasSelect) returnAggRef = returnAggRef.project({ ...this._select, ...projects });
314
+ if (this._hasOrder) returnAggRef = returnAggRef.sort(object.objectMap(this._order, (v) => v === "asc" ? 1 : -1));
315
+ if (this._hasSkip) returnAggRef = returnAggRef.skip(this._skip);
316
+ if (this._hasLimit) returnAggRef = returnAggRef.limit(this._limit);
180
317
  return returnAggRef;
181
318
  }
182
- #endHost() {
183
- if (this.#hasWhere) {
184
- this.#host = this.#host.where(this.#where);
319
+ _endHost() {
320
+ if (this._hasWhere) {
321
+ this._host = this._host.where(_mapQueryCommandWhere(this._where));
185
322
  }
186
- if (this.#hasSelect) {
187
- this.#host = this.#host.field(this.#select);
323
+ if (this._hasSelect) {
324
+ this._host = this._host.field(this._select);
188
325
  }
189
- if (this.#hasOrder) {
190
- object.objectEach(this.#order, (val, key) => {
191
- this.#host = this.#host.orderBy(key, val);
326
+ if (this._hasOrder) {
327
+ object.objectEach(this._order, (val, key) => {
328
+ this._host = this._host.orderBy(key, val);
192
329
  });
193
330
  }
194
- if (this.#hasSkip) this.#host = this.#host.skip(this.#skip);
195
- if (this.#hasLimit) this.#host = this.#host.limit(this.#limit);
196
- else if (this.#hasWhereId) this.#host = this.#host.limit(1);
331
+ if (this._hasSkip) this._host = this._host.skip(this._skip);
332
+ if (this._hasLimit) this._host = this._host.limit(this._limit);
333
+ else if (this._hasWhereId) this._host = this._host.limit(1);
197
334
  }
198
335
  /**
199
336
  * 执行查询操作
@@ -201,24 +338,24 @@ class Db {
201
338
  */
202
339
  async query() {
203
340
  let res;
204
- if (this.#hasLookup) {
341
+ if (this._hasLookup) {
205
342
  const aggRef = this.aggregate();
206
- this.#endAggregate(aggRef);
343
+ this._endAggregate(aggRef);
207
344
  res = await aggRef.end();
208
345
  } else {
209
- this.#endHost();
210
- res = await this.#host.get();
346
+ this._endHost();
347
+ res = await this._host.get();
211
348
  }
212
349
  type.isArray(res.data) ? res.data : [res.data];
213
- const { data } = parseDatabaseOutput(res);
350
+ const { data } = _helpers.parseDatabaseOutput(res);
214
351
  return data;
215
352
  }
216
- async queryOne(ignoreMiss = false) {
217
- if (this.#hasLimit) throw new Error("db.queryOne() 方法不支持 limit 条件");
218
- if (!this.#hasWhereId) this.limit(1);
353
+ async queryOne(allowMiss = false) {
354
+ if (this._hasLimit) throw new Error("db.queryOne() 方法不支持 limit 条件");
355
+ if (!this._hasWhereId) this.limit(1);
219
356
  const data = await this.query();
220
357
  const res = data.at(0);
221
- if (!ignoreMiss && !res) throw new Error("未找到匹配记录");
358
+ if (!allowMiss && !res) throw new Error("未找到匹配记录");
222
359
  return res;
223
360
  }
224
361
  /**
@@ -226,14 +363,14 @@ class Db {
226
363
  * @returns 记录总数
227
364
  */
228
365
  async count() {
229
- if (this.#hasLookup) throw new Error("db.count() 方法不支持 lookup 聚合");
230
- if (this.#hasSelect) throw new Error("db.count() 方法不支持 select 条件");
231
- if (this.#hasOrder) throw new Error("db.count() 方法不支持 order 条件");
232
- if (this.#hasSkip) throw new Error("db.count() 方法不支持 skip 条件");
233
- if (this.#hasLimit) throw new Error("db.count() 方法不支持 limit 条件");
234
- this.#endHost();
235
- const res = await this.#host.count();
236
- const { total } = parseDatabaseOutput(res);
366
+ if (this._hasLookup) throw new Error("db.count() 方法不支持 lookup 聚合");
367
+ if (this._hasSelect) throw new Error("db.count() 方法不支持 select 条件");
368
+ if (this._hasOrder) throw new Error("db.count() 方法不支持 order 条件");
369
+ if (this._hasSkip) throw new Error("db.count() 方法不支持 skip 条件");
370
+ if (this._hasLimit) throw new Error("db.count() 方法不支持 limit 条件");
371
+ this._endHost();
372
+ const res = await this._host.count();
373
+ const { total } = _helpers.parseDatabaseOutput(res);
237
374
  return total;
238
375
  }
239
376
  /**
@@ -242,15 +379,15 @@ class Db {
242
379
  * @returns 创建结果
243
380
  */
244
381
  async create(data) {
245
- if (this.#hasLookup) throw new Error("db.create() 方法不支持 lookup 聚合");
246
- if (this.#hasWhere) throw new Error("db.create() 方法不支持 where 条件");
247
- if (this.#hasSelect) throw new Error("db.create() 方法不支持 select 条件");
248
- if (this.#hasOrder) throw new Error("db.create() 方法不支持 order 条件");
249
- if (this.#hasSkip) throw new Error("db.create() 方法不支持 skip 条件");
250
- if (this.#hasLimit) throw new Error("db.create() 方法不支持 limit 条件");
251
- this.#endHost();
252
- const res = await this.#host.add(data);
253
- const { id } = parseDatabaseOutput(res);
382
+ if (this._hasLookup) throw new Error("db.create() 方法不支持 lookup 聚合");
383
+ if (this._hasWhere) throw new Error("db.create() 方法不支持 where 条件");
384
+ if (this._hasSelect) throw new Error("db.create() 方法不支持 select 条件");
385
+ if (this._hasOrder) throw new Error("db.create() 方法不支持 order 条件");
386
+ if (this._hasSkip) throw new Error("db.create() 方法不支持 skip 条件");
387
+ if (this._hasLimit) throw new Error("db.create() 方法不支持 limit 条件");
388
+ this._endHost();
389
+ const res = await this._host.add(data);
390
+ const { id } = _helpers.parseDatabaseOutput(res);
254
391
  return id;
255
392
  }
256
393
  /**
@@ -259,16 +396,16 @@ class Db {
259
396
  * @returns 更新结果
260
397
  */
261
398
  async update(data) {
262
- if (this.#hasLookup) throw new Error("db.update() 方法不支持 lookup 聚合");
263
- if (!this.#hasWhere) throw new Error("设置 where 条件后才能执行 db.update() 方法");
264
- if (this.#hasSelect) throw new Error("db.update() 方法不支持 select 条件");
265
- if (this.#hasOrder) throw new Error("db.update() 方法不支持 order 条件");
266
- if (this.#hasSkip) throw new Error("db.update() 方法不支持 skip 条件");
267
- if (this.#hasLimit) throw new Error("db.update() 方法不支持 limit 条件");
268
- if (this.#isTransaction && !this.#hasWhereId) throw new Error("事务模式下 db.update() 的 where 条件必须是 _id");
269
- this.#endHost();
270
- const res = await this.#host.update(data);
271
- const { updated } = parseDatabaseOutput(res);
399
+ if (this._hasLookup) throw new Error("db.update() 方法不支持 lookup 聚合");
400
+ if (!this._hasWhere) throw new Error("设置 where 条件后才能执行 db.update() 方法");
401
+ if (this._hasSelect) throw new Error("db.update() 方法不支持 select 条件");
402
+ if (this._hasOrder) throw new Error("db.update() 方法不支持 order 条件");
403
+ if (this._hasSkip) throw new Error("db.update() 方法不支持 skip 条件");
404
+ if (this._hasLimit) throw new Error("db.update() 方法不支持 limit 条件");
405
+ if (this._isTransaction && !this._hasWhereId) throw new Error("事务模式下 db.update() 的 where 条件必须是 _id");
406
+ this._endHost();
407
+ const res = await this._host.update(data);
408
+ const { updated } = _helpers.parseDatabaseOutput(res);
272
409
  return updated;
273
410
  }
274
411
  /**
@@ -276,58 +413,45 @@ class Db {
276
413
  * @returns 删除结果
277
414
  */
278
415
  async remove() {
279
- if (this.#hasLookup) throw new Error("db.remove() 方法不支持 lookup 聚合");
280
- if (!this.#hasWhere) throw new Error("设置 where 条件后才能执行 db.remove() 方法");
281
- if (this.#hasSelect) throw new Error("db.remove() 方法不支持 select 条件");
282
- if (this.#hasOrder) throw new Error("db.remove() 方法不支持 order 条件");
283
- if (this.#hasSkip) throw new Error("db.remove() 方法不支持 skip 条件");
284
- if (this.#hasLimit) throw new Error("db.remove() 方法不支持 limit 条件");
285
- if (this.#isTransaction && !this.#hasWhereId) throw new Error("事务模式下 db.remove() 的 where 条件必须是 _id");
286
- this.#endHost();
287
- const res = await this.#host.remove();
288
- const { deleted } = parseDatabaseOutput(res);
416
+ if (this._hasLookup) throw new Error("db.remove() 方法不支持 lookup 聚合");
417
+ if (!this._hasWhere) throw new Error("设置 where 条件后才能执行 db.remove() 方法");
418
+ if (this._hasSelect) throw new Error("db.remove() 方法不支持 select 条件");
419
+ if (this._hasOrder) throw new Error("db.remove() 方法不支持 order 条件");
420
+ if (this._hasSkip) throw new Error("db.remove() 方法不支持 skip 条件");
421
+ if (this._hasLimit) throw new Error("db.remove() 方法不支持 limit 条件");
422
+ if (this._isTransaction && !this._hasWhereId) throw new Error("事务模式下 db.remove() 的 where 条件必须是 _id");
423
+ this._endHost();
424
+ const res = await this._host.remove();
425
+ const { deleted } = _helpers.parseDatabaseOutput(res);
289
426
  return deleted;
290
427
  }
291
428
  }
292
- const db = {
293
- /**
294
- * 获取指定名称的数据库集合实例
295
- * @param name 数据表名称
296
- * @returns Db类实例,用于执行数据库操作
297
- */
298
- // biome-ignore lint/complexity/noBannedTypes: <explanation>
299
- table(name) {
300
- return new Proxy(
301
- {},
302
- {
303
- get(target, prop) {
304
- if (prop === "_isProxy") return true;
305
- const table = new Db({ table: name });
306
- const tableProp = prop;
307
- const ref = table[tableProp];
308
- return type.isFunction(ref) ? ref.bind(table) : ref;
309
- }
310
- }
311
- );
312
- }
313
- };
314
- function parseDatabaseOutput(res) {
315
- const keys = Object.keys(res);
316
- const isClient = keys.length === 1 && keys[0] === "result";
317
- if (isClient) {
318
- const { result } = res;
319
- if (!result.errCode) return object.objectOmit(result, ["errCode", "errMsg", "code", "message"]);
320
- throw error.errorAssign(new Error(result.errMsg), result);
321
- }
322
- return res;
323
- }
324
429
  function _toWhereMethod(whereFrom) {
325
430
  return whereFrom === "where" ? "where({...})" : "whereId(id)";
326
431
  }
327
432
  function _toWhereIdMethod(whereFrom) {
328
433
  return whereFrom === "where" ? "where({ _id })" : "whereId(id)";
329
434
  }
330
- async function dbUpsert(dbProxy, options) {
435
+ function _mapQueryCommandWhere(where) {
436
+ return object.objectMap(where, (val, key) => {
437
+ return type.isObject(val) && val instanceof DbQueryCommand ? val.getValue(db0) : val;
438
+ });
439
+ }
440
+ function dbProxy(name) {
441
+ return new Proxy(
442
+ {},
443
+ {
444
+ get(target, prop) {
445
+ if (prop === "_isProxy") return true;
446
+ const table = new Db({ table: name });
447
+ const tableProp = prop;
448
+ const ref = table[tableProp];
449
+ return type.isFunction(ref) ? ref.bind(table) : ref;
450
+ }
451
+ }
452
+ );
453
+ }
454
+ async function dbUpsert(dbProxy2, options) {
331
455
  const {
332
456
  where,
333
457
  select = {},
@@ -340,7 +464,7 @@ async function dbUpsert(dbProxy, options) {
340
464
  _mockDbInstance
341
465
  } = options;
342
466
  if ("_id" in select) throw new Error("select 条件不能包含 _id 字段");
343
- const _db = _mockDbInstance || dbProxy;
467
+ const _db = _mockDbInstance || dbProxy2;
344
468
  const exist = await _db.where(where).select(select || {}).queryOne(true);
345
469
  if (exist) {
346
470
  const skipUpdate = await onBeforeUpdate?.(exist) === false;
@@ -357,12 +481,20 @@ async function dbUpsert(dbProxy, options) {
357
481
  await onAfterCreate?.(createdId);
358
482
  return { id: createdId, updated: false, created: true };
359
483
  }
484
+ async function dbUnique(dbProxy2, options) {
485
+ const { id, created } = await dbUpsert(dbProxy2, {
486
+ ...options,
487
+ update: {},
488
+ onBeforeUpdate: () => false
489
+ });
490
+ return { id, created };
491
+ }
360
492
  async function dbTransaction(transacting, _mockDatabase, _mockDbInstance) {
361
493
  const transactionDb = _mockDatabase || uniCloud.database();
362
494
  const [err1, transaction] = await _try.tryFlatten(transactionDb.startTransaction());
363
495
  if (err1) throw err1;
364
- const withTransaction = (dbProxy) => {
365
- return _mockDbInstance || new Db({ table: dbProxy.table, transaction });
496
+ const withTransaction = (dbProxy2) => {
497
+ return _mockDbInstance || new Db({ table: dbProxy2.table, transaction });
366
498
  };
367
499
  const [err2, result] = await _try.tryFlatten(async () => {
368
500
  const result2 = await transacting(withTransaction);
@@ -375,11 +507,11 @@ async function dbTransaction(transacting, _mockDatabase, _mockDbInstance) {
375
507
  }
376
508
  return result;
377
509
  }
378
- exports.Db = Db;
379
- exports.db = db;
380
- exports.dbAgg = dbAgg;
381
- exports.dbCmd = dbCmd;
510
+ exports.parseDatabaseOutput = _helpers.parseDatabaseOutput;
511
+ exports.dbMutate = dbMutate;
512
+ exports.dbProxy = dbProxy;
513
+ exports.dbQuery = dbQuery;
382
514
  exports.dbTransaction = dbTransaction;
515
+ exports.dbUnique = dbUnique;
383
516
  exports.dbUpsert = dbUpsert;
384
- exports.parseDatabaseOutput = parseDatabaseOutput;
385
517
  //# sourceMappingURL=database.cjs.map