-
-
Notifications
You must be signed in to change notification settings - Fork 96
/
database.ts
131 lines (119 loc) Β· 3.33 KB
/
database.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
import { Collection } from "./collection/mod.ts";
import { CommandCursor } from "./protocol/mod.ts";
import { CreateCollectionOptions, CreateUserOptions } from "./types.ts";
import { Cluster } from "./cluster.ts";
import { Document } from "../deps.ts";
import { WriteConcern } from "./types/read_write_concern.ts";
interface ListCollectionsReponse {
cursor: {
id: bigint;
ns: string;
firstBatch: [
{
name: string;
type: "collection";
},
];
};
ok: 1;
}
export interface ListCollectionsResult {
name: string;
type: "collection";
}
export class Database {
#cluster: Cluster;
constructor(cluster: Cluster, readonly name: string) {
this.#cluster = cluster;
}
async dropDatabase(writeConcern?: WriteConcern) {
return await this.#cluster.protocol.commandSingle(this.name, {
dropDatabase: 1,
writeConcern,
});
}
collection<T = Document>(name: string): Collection<T> {
return new Collection<T>(this.#cluster.protocol, this.name, name);
}
listCollections(options: {
filter?: Document;
nameOnly?: boolean;
authorizedCollections?: boolean;
comment?: Document;
} = {}): CommandCursor<ListCollectionsResult> {
return new CommandCursor<ListCollectionsResult>(
this.#cluster.protocol,
async () => {
const { cursor } = await this.#cluster.protocol.commandSingle<
ListCollectionsReponse
>(this.name, {
listCollections: 1,
...options,
});
return {
id: cursor.id,
ns: cursor.ns,
firstBatch: cursor.firstBatch,
};
},
);
}
async listCollectionNames(options: {
filter?: Document;
authorizedCollections?: boolean;
comment?: Document;
} = {}): Promise<string[]> {
const cursor = this.listCollections({
...options,
nameOnly: true,
authorizedCollections: true,
});
const names: string[] = [];
for await (const item of cursor) {
names.push(item.name);
}
return names;
}
/**
* `createCollection` executes a create command to create a new collection with the specified name and options.
*
* https://www.mongodb.com/docs/manual/reference/command/create/#mongodb-dbcommand-dbcmd.create
*/
async createCollection<T>(
name: string,
options?: CreateCollectionOptions,
): Promise<Collection<T>> {
await this.#cluster.protocol.commandSingle(
this.name,
{ create: name, ...options },
);
return this.collection<T>(name);
}
createUser(
username: string,
password: string,
options?: CreateUserOptions,
) {
return this.#cluster.protocol.commandSingle(this.name, {
createUser: options?.username ?? username,
pwd: options?.password ?? password,
customData: options?.customData,
roles: options?.roles ?? [],
writeConcern: options?.writeConcern,
authenticationRestrictions: options?.authenticationRestrictions,
mechanisms: options?.mechanisms,
digestPassword: options?.digestPassword,
comment: options?.comment,
});
}
dropUser(username: string, options: {
writeConcern?: Document;
comment?: Document;
} = {}) {
return this.#cluster.protocol.commandSingle(this.name, {
dropUser: username,
writeConcern: options?.writeConcern,
comment: options?.comment,
});
}
}