db.js
14.5 KB
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Db = void 0;
const admin_1 = require("./admin");
const bson_1 = require("./bson");
const change_stream_1 = require("./change_stream");
const collection_1 = require("./collection");
const CONSTANTS = require("./constants");
const aggregation_cursor_1 = require("./cursor/aggregation_cursor");
const error_1 = require("./error");
const logger_1 = require("./logger");
const add_user_1 = require("./operations/add_user");
const collections_1 = require("./operations/collections");
const create_collection_1 = require("./operations/create_collection");
const drop_1 = require("./operations/drop");
const execute_operation_1 = require("./operations/execute_operation");
const indexes_1 = require("./operations/indexes");
const list_collections_1 = require("./operations/list_collections");
const profiling_level_1 = require("./operations/profiling_level");
const remove_user_1 = require("./operations/remove_user");
const rename_1 = require("./operations/rename");
const run_command_1 = require("./operations/run_command");
const set_profiling_level_1 = require("./operations/set_profiling_level");
const stats_1 = require("./operations/stats");
const read_concern_1 = require("./read_concern");
const read_preference_1 = require("./read_preference");
const utils_1 = require("./utils");
const write_concern_1 = require("./write_concern");
// Allowed parameters
const DB_OPTIONS_ALLOW_LIST = [
'writeConcern',
'readPreference',
'readPreferenceTags',
'native_parser',
'forceServerObjectId',
'pkFactory',
'serializeFunctions',
'raw',
'authSource',
'ignoreUndefined',
'readConcern',
'retryMiliSeconds',
'numberOfRetries',
'loggerLevel',
'logger',
'promoteBuffers',
'promoteLongs',
'bsonRegExp',
'enableUtf8Validation',
'promoteValues',
'compression',
'retryWrites'
];
/**
* The **Db** class is a class that represents a MongoDB Database.
* @public
*
* @example
* ```js
* const { MongoClient } = require('mongodb');
* // Connection url
* const url = 'mongodb://localhost:27017';
* // Database Name
* const dbName = 'test';
* // Connect using MongoClient
* MongoClient.connect(url, function(err, client) {
* // Select the database by name
* const testDb = client.db(dbName);
* client.close();
* });
* ```
*/
class Db {
/**
* Creates a new Db instance
*
* @param client - The MongoClient for the database.
* @param databaseName - The name of the database this instance represents.
* @param options - Optional settings for Db construction
*/
constructor(client, databaseName, options) {
var _a;
options = options !== null && options !== void 0 ? options : {};
// Filter the options
options = (0, utils_1.filterOptions)(options, DB_OPTIONS_ALLOW_LIST);
// Ensure we have a valid db name
validateDatabaseName(databaseName);
// Internal state of the db object
this.s = {
// Client
client,
// Options
options,
// Logger instance
logger: new logger_1.Logger('Db', options),
// Unpack read preference
readPreference: read_preference_1.ReadPreference.fromOptions(options),
// Merge bson options
bsonOptions: (0, bson_1.resolveBSONOptions)(options, client),
// Set up the primary key factory or fallback to ObjectId
pkFactory: (_a = options === null || options === void 0 ? void 0 : options.pkFactory) !== null && _a !== void 0 ? _a : utils_1.DEFAULT_PK_FACTORY,
// ReadConcern
readConcern: read_concern_1.ReadConcern.fromOptions(options),
writeConcern: write_concern_1.WriteConcern.fromOptions(options),
// Namespace
namespace: new utils_1.MongoDBNamespace(databaseName)
};
}
get databaseName() {
return this.s.namespace.db;
}
// Options
get options() {
return this.s.options;
}
/**
* slaveOk specified
* @deprecated Use secondaryOk instead
*/
get slaveOk() {
return this.secondaryOk;
}
/**
* Check if a secondary can be used (because the read preference is *not* set to primary)
*/
get secondaryOk() {
var _a;
return ((_a = this.s.readPreference) === null || _a === void 0 ? void 0 : _a.preference) !== 'primary' || false;
}
get readConcern() {
return this.s.readConcern;
}
/**
* The current readPreference of the Db. If not explicitly defined for
* this Db, will be inherited from the parent MongoClient
*/
get readPreference() {
if (this.s.readPreference == null) {
return this.s.client.readPreference;
}
return this.s.readPreference;
}
get bsonOptions() {
return this.s.bsonOptions;
}
// get the write Concern
get writeConcern() {
return this.s.writeConcern;
}
get namespace() {
return this.s.namespace.toString();
}
createCollection(name, options, callback) {
if (typeof options === 'function')
(callback = options), (options = {});
return (0, execute_operation_1.executeOperation)(this, new create_collection_1.CreateCollectionOperation(this, name, (0, utils_1.resolveOptions)(this, options)), callback);
}
command(command, options, callback) {
if (typeof options === 'function')
(callback = options), (options = {});
// Intentionally, we do not inherit options from parent for this operation.
return (0, execute_operation_1.executeOperation)(this, new run_command_1.RunCommandOperation(this, command, options !== null && options !== void 0 ? options : {}), callback);
}
/**
* Execute an aggregation framework pipeline against the database, needs MongoDB \>= 3.6
*
* @param pipeline - An array of aggregation stages to be executed
* @param options - Optional settings for the command
*/
aggregate(pipeline = [], options) {
if (arguments.length > 2) {
throw new error_1.MongoInvalidArgumentError('Method "db.aggregate()" accepts at most two arguments');
}
if (typeof pipeline === 'function') {
throw new error_1.MongoInvalidArgumentError('Argument "pipeline" must not be function');
}
if (typeof options === 'function') {
throw new error_1.MongoInvalidArgumentError('Argument "options" must not be function');
}
return new aggregation_cursor_1.AggregationCursor((0, utils_1.getTopology)(this), this.s.namespace, pipeline, (0, utils_1.resolveOptions)(this, options));
}
/** Return the Admin db instance */
admin() {
return new admin_1.Admin(this);
}
/**
* Returns a reference to a MongoDB Collection. If it does not exist it will be created implicitly.
*
* @param name - the collection name we wish to access.
* @returns return the new Collection instance
*/
collection(name, options = {}) {
if (typeof options === 'function') {
throw new error_1.MongoInvalidArgumentError('The callback form of this helper has been removed.');
}
const finalOptions = (0, utils_1.resolveOptions)(this, options);
return new collection_1.Collection(this, name, finalOptions);
}
stats(options, callback) {
if (typeof options === 'function')
(callback = options), (options = {});
return (0, execute_operation_1.executeOperation)(this, new stats_1.DbStatsOperation(this, (0, utils_1.resolveOptions)(this, options)), callback);
}
listCollections(filter = {}, options = {}) {
return new list_collections_1.ListCollectionsCursor(this, filter, (0, utils_1.resolveOptions)(this, options));
}
renameCollection(fromCollection, toCollection, options, callback) {
if (typeof options === 'function')
(callback = options), (options = {});
// Intentionally, we do not inherit options from parent for this operation.
options = { ...options, readPreference: read_preference_1.ReadPreference.PRIMARY };
// Add return new collection
options.new_collection = true;
return (0, execute_operation_1.executeOperation)(this, new rename_1.RenameOperation(this.collection(fromCollection), toCollection, options), callback);
}
dropCollection(name, options, callback) {
if (typeof options === 'function')
(callback = options), (options = {});
return (0, execute_operation_1.executeOperation)(this, new drop_1.DropCollectionOperation(this, name, (0, utils_1.resolveOptions)(this, options)), callback);
}
dropDatabase(options, callback) {
if (typeof options === 'function')
(callback = options), (options = {});
return (0, execute_operation_1.executeOperation)(this, new drop_1.DropDatabaseOperation(this, (0, utils_1.resolveOptions)(this, options)), callback);
}
collections(options, callback) {
if (typeof options === 'function')
(callback = options), (options = {});
return (0, execute_operation_1.executeOperation)(this, new collections_1.CollectionsOperation(this, (0, utils_1.resolveOptions)(this, options)), callback);
}
createIndex(name, indexSpec, options, callback) {
if (typeof options === 'function')
(callback = options), (options = {});
return (0, execute_operation_1.executeOperation)(this, new indexes_1.CreateIndexOperation(this, name, indexSpec, (0, utils_1.resolveOptions)(this, options)), callback);
}
addUser(username, password, options, callback) {
if (typeof password === 'function') {
(callback = password), (password = undefined), (options = {});
}
else if (typeof password !== 'string') {
if (typeof options === 'function') {
(callback = options), (options = password), (password = undefined);
}
else {
(options = password), (callback = undefined), (password = undefined);
}
}
else {
if (typeof options === 'function')
(callback = options), (options = {});
}
return (0, execute_operation_1.executeOperation)(this, new add_user_1.AddUserOperation(this, username, password, (0, utils_1.resolveOptions)(this, options)), callback);
}
removeUser(username, options, callback) {
if (typeof options === 'function')
(callback = options), (options = {});
return (0, execute_operation_1.executeOperation)(this, new remove_user_1.RemoveUserOperation(this, username, (0, utils_1.resolveOptions)(this, options)), callback);
}
setProfilingLevel(level, options, callback) {
if (typeof options === 'function')
(callback = options), (options = {});
return (0, execute_operation_1.executeOperation)(this, new set_profiling_level_1.SetProfilingLevelOperation(this, level, (0, utils_1.resolveOptions)(this, options)), callback);
}
profilingLevel(options, callback) {
if (typeof options === 'function')
(callback = options), (options = {});
return (0, execute_operation_1.executeOperation)(this, new profiling_level_1.ProfilingLevelOperation(this, (0, utils_1.resolveOptions)(this, options)), callback);
}
indexInformation(name, options, callback) {
if (typeof options === 'function')
(callback = options), (options = {});
return (0, execute_operation_1.executeOperation)(this, new indexes_1.IndexInformationOperation(this, name, (0, utils_1.resolveOptions)(this, options)), callback);
}
/**
* Unref all sockets
* @deprecated This function is deprecated and will be removed in the next major version.
*/
unref() {
(0, utils_1.getTopology)(this).unref();
}
/**
* Create a new Change Stream, watching for new changes (insertions, updates,
* replacements, deletions, and invalidations) in this database. Will ignore all
* changes to system collections.
*
* @remarks
* watch() accepts two generic arguments for distinct usecases:
* - The first is to provide the schema that may be defined for all the collections within this database
* - The second is to override the shape of the change stream document entirely, if it is not provided the type will default to ChangeStreamDocument of the first argument
*
* @param pipeline - An array of {@link https://docs.mongodb.com/manual/reference/operator/aggregation-pipeline/|aggregation pipeline stages} through which to pass change stream documents. This allows for filtering (using $match) and manipulating the change stream documents.
* @param options - Optional settings for the command
* @typeParam TSchema - Type of the data being detected by the change stream
* @typeParam TChange - Type of the whole change stream document emitted
*/
watch(pipeline = [], options = {}) {
// Allow optionally not specifying a pipeline
if (!Array.isArray(pipeline)) {
options = pipeline;
pipeline = [];
}
return new change_stream_1.ChangeStream(this, pipeline, (0, utils_1.resolveOptions)(this, options));
}
/** Return the db logger */
getLogger() {
return this.s.logger;
}
get logger() {
return this.s.logger;
}
}
exports.Db = Db;
Db.SYSTEM_NAMESPACE_COLLECTION = CONSTANTS.SYSTEM_NAMESPACE_COLLECTION;
Db.SYSTEM_INDEX_COLLECTION = CONSTANTS.SYSTEM_INDEX_COLLECTION;
Db.SYSTEM_PROFILE_COLLECTION = CONSTANTS.SYSTEM_PROFILE_COLLECTION;
Db.SYSTEM_USER_COLLECTION = CONSTANTS.SYSTEM_USER_COLLECTION;
Db.SYSTEM_COMMAND_COLLECTION = CONSTANTS.SYSTEM_COMMAND_COLLECTION;
Db.SYSTEM_JS_COLLECTION = CONSTANTS.SYSTEM_JS_COLLECTION;
// TODO(NODE-3484): Refactor into MongoDBNamespace
// Validate the database name
function validateDatabaseName(databaseName) {
if (typeof databaseName !== 'string')
throw new error_1.MongoInvalidArgumentError('Database name must be a string');
if (databaseName.length === 0)
throw new error_1.MongoInvalidArgumentError('Database name cannot be the empty string');
if (databaseName === '$external')
return;
const invalidChars = [' ', '.', '$', '/', '\\'];
for (let i = 0; i < invalidChars.length; i++) {
if (databaseName.indexOf(invalidChars[i]) !== -1)
throw new error_1.MongoAPIError(`database names cannot contain the character '${invalidChars[i]}'`);
}
}
//# sourceMappingURL=db.js.map