forked from labring/laf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcollection.ts
78 lines (69 loc) · 1.82 KB
/
collection.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
import { Db } from './index'
import { DocumentReference } from './document'
import { Query } from './query'
import Aggregation from './aggregate'
import { CreateIndexRes, DropIndexRes, ListIndexesRes } from './result-types'
/**
* 集合模块,继承 Query 模块
*
*/
export class CollectionReference extends Query {
/**
* 初始化
*
* @internal
*
* @param db - 数据库的引用
* @param coll - 集合名称
*/
constructor(db: Db, coll: string) {
super(db, coll)
}
/**
* 读取集合名字
*/
get name() {
return this._coll
}
/**
* 获取文档的引用
*
* @param docID - 文档 ID
*/
doc(docID: string | number): DocumentReference {
if (!docID) {
throw new Error('docID cannot be empty')
}
return new DocumentReference(this._db, this._coll, docID)
}
/**
* 添加一篇文档
*
* @param data - 数据
*/
add(data: Object, options?: { multi: boolean }) {
let docRef = new DocumentReference(this._db, this._coll, undefined)
return docRef.create(data, options)
}
aggregate(rawPipeline: object[] = []) {
return new Aggregation(this._db, this._coll, rawPipeline)
}
/**
* 创建索引
*/
public async createIndex(keys: Record<string, number | string>, options?: any): Promise<CreateIndexRes> {
let docRef = new DocumentReference(this._db, this._coll, undefined)
return docRef.createIndex(keys, options)
}
/**
* 删除索引
*/
public async dropIndex(index: string | Record<string, number | string>): Promise<DropIndexRes> {
let docRef = new DocumentReference(this._db, this._coll, undefined)
return docRef.dropIndex(index)
}
public async listIndexes(): Promise<ListIndexesRes> {
let docRef = new DocumentReference(this._db, this._coll, undefined)
return docRef.listIndexes()
}
}