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