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