@cloudcome/utils-uni 1.13.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/_command.class.d.ts +15 -0
- package/dist/database/{db.class.d.ts → _db.class.d.ts} +61 -46
- package/dist/database/command.d.ts +117 -8
- package/dist/database/proxy.d.ts +7 -10
- package/dist/database/transaction.d.ts +3 -4
- package/dist/database/types.d.ts +0 -141
- package/dist/database/unique.d.ts +1 -1
- package/dist/database/upsert.d.ts +2 -2
- package/dist/database.cjs +268 -126
- package/dist/database.cjs.map +1 -1
- package/dist/database.mjs +269 -127
- package/dist/database.mjs.map +1 -1
- package/dist/index.cjs +1 -1
- package/dist/index.mjs +1 -1
- package/package.json +1 -1
package/dist/database.cjs
CHANGED
|
@@ -4,52 +4,191 @@ const type = require("@cloudcome/utils-core/type");
|
|
|
4
4
|
const _helpers = require("./_helpers.cjs");
|
|
5
5
|
const object = require("@cloudcome/utils-core/object");
|
|
6
6
|
const _try = require("@cloudcome/utils-core/try");
|
|
7
|
-
|
|
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
|
+
};
|
|
8
147
|
const dbAgg = uniCloud.database().command.aggregate;
|
|
9
148
|
const db0 = uniCloud.database();
|
|
10
149
|
let gid = 0;
|
|
11
150
|
class Db {
|
|
12
|
-
|
|
151
|
+
_host;
|
|
13
152
|
/**
|
|
14
153
|
* 是否为事务环境
|
|
15
154
|
* - 查询条件只能是 id
|
|
16
155
|
* - 不能聚合操作
|
|
17
156
|
*/
|
|
18
|
-
|
|
19
|
-
|
|
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
|
|
27
|
-
this
|
|
28
|
-
this
|
|
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
|
|
170
|
+
return this._options.table;
|
|
32
171
|
}
|
|
33
172
|
/**
|
|
34
173
|
* 获取聚合操作实例
|
|
35
174
|
* @returns 聚合操作实例
|
|
36
175
|
*/
|
|
37
176
|
aggregate() {
|
|
38
|
-
return this
|
|
177
|
+
return this._host.aggregate();
|
|
39
178
|
}
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
if (this
|
|
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
|
|
186
|
+
if (isWhereId && this._hasLimit) {
|
|
48
187
|
throw new Error(`db.${_toWhereIdMethod(from)} 方法不能与 db.limit() 方法同时调用`);
|
|
49
188
|
}
|
|
50
|
-
this
|
|
51
|
-
this
|
|
52
|
-
if (isWhereId) this
|
|
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
|
|
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
|
|
208
|
+
return this._doWhere({ _id: id }, "whereId");
|
|
70
209
|
}
|
|
71
|
-
|
|
72
|
-
|
|
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
|
|
80
|
-
this
|
|
81
|
-
this
|
|
218
|
+
if (this._hasSelect) throw new Error("db.select() 方法只能调用一次");
|
|
219
|
+
this._hasSelect++;
|
|
220
|
+
this._select = fields;
|
|
82
221
|
return this;
|
|
83
222
|
}
|
|
84
|
-
|
|
85
|
-
|
|
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
|
|
93
|
-
this
|
|
231
|
+
this._hasOrder++;
|
|
232
|
+
this._order = order;
|
|
94
233
|
return this;
|
|
95
234
|
}
|
|
96
|
-
|
|
97
|
-
|
|
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
|
|
105
|
-
this
|
|
106
|
-
this
|
|
243
|
+
if (this._hasSkip) throw new Error("db.skip() 方法只能调用一次");
|
|
244
|
+
this._hasSkip++;
|
|
245
|
+
this._skip = skip;
|
|
107
246
|
return this;
|
|
108
247
|
}
|
|
109
|
-
|
|
110
|
-
|
|
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
|
|
118
|
-
if (this
|
|
119
|
-
throw new Error(`db.limit() 方法不能与 ${_toWhereIdMethod(this
|
|
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
|
|
122
|
-
this
|
|
260
|
+
this._hasLimit++;
|
|
261
|
+
this._limit = limit;
|
|
123
262
|
return this;
|
|
124
263
|
}
|
|
125
|
-
|
|
264
|
+
_hasLookup = 0;
|
|
126
265
|
get hasLookup() {
|
|
127
|
-
return this
|
|
266
|
+
return this._hasLookup > 0;
|
|
128
267
|
}
|
|
129
|
-
|
|
268
|
+
_lookups = [];
|
|
130
269
|
lookup(table, lookup) {
|
|
131
|
-
table
|
|
132
|
-
this
|
|
133
|
-
this
|
|
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
|
-
|
|
141
|
-
|
|
142
|
-
if (this
|
|
143
|
-
this
|
|
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,
|
|
147
|
-
const
|
|
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
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
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
|
-
[
|
|
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
|
|
176
|
-
if (this
|
|
177
|
-
if (this
|
|
178
|
-
if (this
|
|
179
|
-
if (this
|
|
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
|
-
|
|
183
|
-
if (this
|
|
184
|
-
this
|
|
319
|
+
_endHost() {
|
|
320
|
+
if (this._hasWhere) {
|
|
321
|
+
this._host = this._host.where(_mapQueryCommandWhere(this._where));
|
|
185
322
|
}
|
|
186
|
-
if (this
|
|
187
|
-
this
|
|
323
|
+
if (this._hasSelect) {
|
|
324
|
+
this._host = this._host.field(this._select);
|
|
188
325
|
}
|
|
189
|
-
if (this
|
|
190
|
-
object.objectEach(this
|
|
191
|
-
this
|
|
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
|
|
195
|
-
if (this
|
|
196
|
-
else if (this
|
|
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
|
|
341
|
+
if (this._hasLookup) {
|
|
205
342
|
const aggRef = this.aggregate();
|
|
206
|
-
this
|
|
343
|
+
this._endAggregate(aggRef);
|
|
207
344
|
res = await aggRef.end();
|
|
208
345
|
} else {
|
|
209
|
-
this
|
|
210
|
-
res = await this
|
|
346
|
+
this._endHost();
|
|
347
|
+
res = await this._host.get();
|
|
211
348
|
}
|
|
212
349
|
type.isArray(res.data) ? res.data : [res.data];
|
|
213
350
|
const { data } = _helpers.parseDatabaseOutput(res);
|
|
214
351
|
return data;
|
|
215
352
|
}
|
|
216
|
-
async queryOne(
|
|
217
|
-
if (this
|
|
218
|
-
if (!this
|
|
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 (!
|
|
358
|
+
if (!allowMiss && !res) throw new Error("未找到匹配记录");
|
|
222
359
|
return res;
|
|
223
360
|
}
|
|
224
361
|
/**
|
|
@@ -226,13 +363,13 @@ class Db {
|
|
|
226
363
|
* @returns 记录总数
|
|
227
364
|
*/
|
|
228
365
|
async count() {
|
|
229
|
-
if (this
|
|
230
|
-
if (this
|
|
231
|
-
if (this
|
|
232
|
-
if (this
|
|
233
|
-
if (this
|
|
234
|
-
this
|
|
235
|
-
const res = await this
|
|
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();
|
|
236
373
|
const { total } = _helpers.parseDatabaseOutput(res);
|
|
237
374
|
return total;
|
|
238
375
|
}
|
|
@@ -242,14 +379,14 @@ class Db {
|
|
|
242
379
|
* @returns 创建结果
|
|
243
380
|
*/
|
|
244
381
|
async create(data) {
|
|
245
|
-
if (this
|
|
246
|
-
if (this
|
|
247
|
-
if (this
|
|
248
|
-
if (this
|
|
249
|
-
if (this
|
|
250
|
-
if (this
|
|
251
|
-
this
|
|
252
|
-
const res = await this
|
|
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);
|
|
253
390
|
const { id } = _helpers.parseDatabaseOutput(res);
|
|
254
391
|
return id;
|
|
255
392
|
}
|
|
@@ -259,15 +396,15 @@ class Db {
|
|
|
259
396
|
* @returns 更新结果
|
|
260
397
|
*/
|
|
261
398
|
async update(data) {
|
|
262
|
-
if (this
|
|
263
|
-
if (!this
|
|
264
|
-
if (this
|
|
265
|
-
if (this
|
|
266
|
-
if (this
|
|
267
|
-
if (this
|
|
268
|
-
if (this
|
|
269
|
-
this
|
|
270
|
-
const res = await this
|
|
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);
|
|
271
408
|
const { updated } = _helpers.parseDatabaseOutput(res);
|
|
272
409
|
return updated;
|
|
273
410
|
}
|
|
@@ -276,15 +413,15 @@ class Db {
|
|
|
276
413
|
* @returns 删除结果
|
|
277
414
|
*/
|
|
278
415
|
async remove() {
|
|
279
|
-
if (this
|
|
280
|
-
if (!this
|
|
281
|
-
if (this
|
|
282
|
-
if (this
|
|
283
|
-
if (this
|
|
284
|
-
if (this
|
|
285
|
-
if (this
|
|
286
|
-
this
|
|
287
|
-
const res = await this
|
|
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();
|
|
288
425
|
const { deleted } = _helpers.parseDatabaseOutput(res);
|
|
289
426
|
return deleted;
|
|
290
427
|
}
|
|
@@ -295,6 +432,11 @@ function _toWhereMethod(whereFrom) {
|
|
|
295
432
|
function _toWhereIdMethod(whereFrom) {
|
|
296
433
|
return whereFrom === "where" ? "where({ _id })" : "whereId(id)";
|
|
297
434
|
}
|
|
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
|
+
}
|
|
298
440
|
function dbProxy(name) {
|
|
299
441
|
return new Proxy(
|
|
300
442
|
{},
|
|
@@ -366,9 +508,9 @@ async function dbTransaction(transacting, _mockDatabase, _mockDbInstance) {
|
|
|
366
508
|
return result;
|
|
367
509
|
}
|
|
368
510
|
exports.parseDatabaseOutput = _helpers.parseDatabaseOutput;
|
|
369
|
-
exports.
|
|
370
|
-
exports.dbCmd = dbCmd;
|
|
511
|
+
exports.dbMutate = dbMutate;
|
|
371
512
|
exports.dbProxy = dbProxy;
|
|
513
|
+
exports.dbQuery = dbQuery;
|
|
372
514
|
exports.dbTransaction = dbTransaction;
|
|
373
515
|
exports.dbUnique = dbUnique;
|
|
374
516
|
exports.dbUpsert = dbUpsert;
|