forked from labring/laf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdocument.ts
261 lines (225 loc) · 5.57 KB
/
document.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
import { Db } from './index'
import { serialize } from './serializer/datatype'
import { UpdateCommand } from './commands/update'
import { ActionType } from './constant'
import { AddRes, CreateIndexRes, DropIndexRes, GetOneRes, ListIndexesRes, RemoveRes, UpdateRes } from './result-types'
import { ProjectionType } from './interface'
import { Query } from './query'
/**
* Db document
*/
export class DocumentReference {
/**
* document id
*/
readonly id: any
/**
* query object
*/
private _query: Query
/**
* db reference
*/
private _db: Db
/**
* collection name
*/
readonly _coll: string
/**
* @param db - db ref
* @param coll - collection
* @param docID - document id
*/
constructor(db: Db, coll: string, docID: any, query?: Query) {
this._db = db
this._coll = coll
this.id = docID
this._query = query || new Query(db, coll)
}
/**
* 创建一篇文档
*
* @param data - document data
*/
async create(data: any, options?: { multi: boolean }): Promise<AddRes> {
if (!data || typeof data !== 'object' || 0 === Object.keys(data)?.length) {
throw new Error('data cannot be empty object')
}
const params = {
collectionName: this._coll,
data: serialize(data),
multi: options?.multi ?? false
}
const res = await this._query
.send(ActionType.add, params)
if (res.error) {
return {
requestId: res.requestId,
error: res.error,
ok: false,
id: undefined,
insertedCount: undefined,
code: res.code
}
}
return {
id: res.data._id || res.data[this._db.primaryKey],
insertedCount: res.data.insertedCount,
requestId: res.requestId,
ok: true
}
}
/**
* 创建或添加数据
*
* 如果该文档 ID 在数据库中不存在,则创建该文档并插入数据,根据返回数据的 upsertId 判断
*
* @param data - document data
*/
async set(data: Object): Promise<UpdateRes> {
if (!this.id) {
throw new Error('document id cannot be empty')
}
let hasOperator = false
const checkMixed = (objs: any) => {
if (typeof objs === 'object') {
for (let key in objs) {
if (objs[key] instanceof UpdateCommand) {
hasOperator = true
} else if (typeof objs[key] === 'object') {
checkMixed(objs[key])
}
}
}
}
checkMixed(data)
if (hasOperator) {
// 不能包含操作符
throw new Error('data cannot contain operator')
}
// merge === false indicates replace operation
const merge = false
const res = await this._query
.where({ [this._db.primaryKey]: this.id })
.update(serialize(data), { merge, multi: false, upsert: true })
return res
}
/**
* 更新数据
*
* @param data - 文档数据
*/
async update(data: Object): Promise<UpdateRes> {
// 把所有更新数据转为带操作符的
const merge = true
const options = { merge, multi: false, upsert: false }
const res = await this._query
.where({ [this._db.primaryKey]: this.id })
.update(data, options)
return res
}
/**
* 删除文档
*/
async remove(): Promise<RemoveRes> {
const res = await this._query
.where({ [this._db.primaryKey]: this.id })
.remove({ multi: false })
return res
}
/**
* 返回选中的文档
*/
async get<T = any>(): Promise<GetOneRes<T>> {
const res = await this._query
.where({ [this._db.primaryKey]: this.id })
.getOne()
return res
}
/**
* 指定要返回的字段
*
* @param projection
*/
field(projection: string[] | ProjectionType): DocumentReference {
return new DocumentReference(this._db, this._coll, this.id, this._query.field(projection))
}
/**
* 创建索引
*
* @param data - document data
*/
async createIndex(keys: object, options?: object): Promise<CreateIndexRes> {
if (typeof keys !== 'object' || Object.keys(keys)?.length === 0) {
throw new Error('keys cannot be empty object')
}
const data = {
keys,
options
}
const params = {
collectionName: this._coll,
data: serialize(data),
}
const res = await this._query
.send(ActionType.createIndex, params)
if (res.error) {
return {
requestId: res.requestId,
error: res.error,
ok: false,
code: res.code,
indexName: null,
}
}
return {
requestId: res.requestId,
ok: true,
indexName: res.data.indexName,
}
}
async dropIndex(index: string | object): Promise<DropIndexRes> {
const params = {
collectionName: this._coll,
data: serialize(index),
}
const res = await this._query
.send(ActionType.dropIndex, params)
if (res.error) {
return {
requestId: res.requestId,
error: res.error,
ok: false,
code: res.code,
result: null,
}
}
return {
requestId: res.requestId,
ok: true,
result: res.data.result,
}
}
async listIndexes(options?: object): Promise<ListIndexesRes> {
const params = {
collectionName: this._coll,
data: serialize(options),
}
const res = await this._query
.send(ActionType.listIndexes, params)
if (res.error) {
return {
requestId: res.requestId,
error: res.error,
ok: false,
code: res.code,
list: res.data.list
}
}
return {
requestId: res.requestId,
ok: true,
list: res.data.list,
}
}
}