@mongosh/service-provider-server
Advanced tools
Comparing version 0.0.8 to 0.1.0
import { MongoClient } from 'mongodb'; | ||
import { ServiceProvider, Document, Cursor, Result, BulkWriteResult, DatabaseOptions, WriteConcern, CommandOptions, ReplPlatform } from '@mongosh/service-provider-core'; | ||
import { ServiceProvider, Document, Cursor, Result, BulkWriteResult, DatabaseOptions, WriteConcern, CommandOptions, ReplPlatform, ServiceProviderCore, AuthOptions } from '@mongosh/service-provider-core'; | ||
import NodeOptions from './node/node-options'; | ||
@@ -8,9 +8,10 @@ declare type DropDatabaseResult = { | ||
}; | ||
declare class CliServiceProvider implements ServiceProvider { | ||
declare class CliServiceProvider extends ServiceProviderCore implements ServiceProvider { | ||
readonly platform: ReplPlatform; | ||
readonly initialDb: string; | ||
static connect(uri: string, options?: NodeOptions, cliOptions?: any): Promise<CliServiceProvider>; | ||
private readonly mongoClient; | ||
private mongoClient; | ||
private readonly uri?; | ||
constructor(mongoClient: MongoClient, uri?: string); | ||
private initialOptions; | ||
constructor(mongoClient: MongoClient, clientOptions?: {}, uri?: string); | ||
getNewConnection(uri: string, options?: NodeOptions): Promise<CliServiceProvider>; | ||
@@ -57,3 +58,5 @@ getConnectionInfo(): Promise<any>; | ||
dropCollection(database: string, collection: string, dbOptions?: DatabaseOptions): Promise<boolean>; | ||
authenticate(authDoc: AuthOptions): Promise<any>; | ||
createCollection(dbName: string, collName: string, options: any, dbOptions?: any): Promise<any>; | ||
} | ||
export default CliServiceProvider; |
"use strict"; | ||
var __extends = (this && this.__extends) || (function () { | ||
var extendStatics = function (d, b) { | ||
extendStatics = Object.setPrototypeOf || | ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || | ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; | ||
return extendStatics(d, b); | ||
}; | ||
return function (d, b) { | ||
extendStatics(d, b); | ||
function __() { this.constructor = d; } | ||
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); | ||
}; | ||
})(); | ||
var __assign = (this && this.__assign) || function () { | ||
@@ -49,24 +62,35 @@ __assign = Object.assign || function(t) { | ||
}; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
var __importStar = (this && this.__importStar) || function (mod) { | ||
if (mod && mod.__esModule) return mod; | ||
var result = {}; | ||
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k]; | ||
result["default"] = mod; | ||
return result; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var mongodb_1 = require("mongodb"); | ||
var mongodb_1 = __importStar(require("mongodb")); | ||
var service_provider_core_1 = require("@mongosh/service-provider-core"); | ||
var node_cursor_1 = __importDefault(require("./node/node-cursor")); | ||
var DEFAULT_OPTIONS = Object.freeze({ | ||
var DEFAULT_DRIVER_OPTIONS = Object.freeze({ | ||
useNewUrlParser: true, | ||
useUnifiedTopology: true | ||
}); | ||
var CliServiceProvider = (function () { | ||
function CliServiceProvider(mongoClient, uri) { | ||
this.mongoClient = mongoClient; | ||
this.uri = uri; | ||
this.platform = service_provider_core_1.ReplPlatform.CLI; | ||
var DEFAULT_BASE_OPTIONS = Object.freeze({ | ||
serializeFunctions: true | ||
}); | ||
var CliServiceProvider = (function (_super) { | ||
__extends(CliServiceProvider, _super); | ||
function CliServiceProvider(mongoClient, clientOptions, uri) { | ||
if (clientOptions === void 0) { clientOptions = {}; } | ||
var _this = _super.call(this, mongodb_1.default) || this; | ||
_this.mongoClient = mongoClient; | ||
_this.uri = uri; | ||
_this.platform = service_provider_core_1.ReplPlatform.CLI; | ||
try { | ||
this.initialDb = mongoClient.s.options.dbName || service_provider_core_1.DEFAULT_DB; | ||
_this.initialDb = mongoClient.s.options.dbName || service_provider_core_1.DEFAULT_DB; | ||
} | ||
catch (err) { | ||
this.initialDb = service_provider_core_1.DEFAULT_DB; | ||
_this.initialDb = service_provider_core_1.DEFAULT_DB; | ||
} | ||
_this.initialOptions = clientOptions; | ||
return _this; | ||
} | ||
@@ -81,3 +105,3 @@ CliServiceProvider.connect = function (uri, options, cliOptions) { | ||
case 0: | ||
clientOptions = __assign(__assign({}, DEFAULT_OPTIONS), options); | ||
clientOptions = __assign(__assign({}, DEFAULT_DRIVER_OPTIONS), options); | ||
if (!!cliOptions.nodb) return [3, 2]; | ||
@@ -93,3 +117,3 @@ return [4, mongodb_1.MongoClient.connect(uri, clientOptions)]; | ||
mongoClient = _a; | ||
return [2, new CliServiceProvider(mongoClient, uri)]; | ||
return [2, new CliServiceProvider(mongoClient, clientOptions, uri)]; | ||
} | ||
@@ -106,3 +130,3 @@ }); | ||
case 0: | ||
clientOptions = __assign(__assign({}, DEFAULT_OPTIONS), options); | ||
clientOptions = __assign(__assign({}, DEFAULT_DRIVER_OPTIONS), options); | ||
return [4, mongodb_1.MongoClient.connect(uri, clientOptions)]; | ||
@@ -151,7 +175,10 @@ case 1: | ||
CliServiceProvider.prototype.renameCollection = function (database, oldName, newName, options, dbOptions) { | ||
if (options === void 0) { options = {}; } | ||
return __awaiter(this, void 0, void 0, function () { | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: return [4, this.db(database, dbOptions) | ||
.renameCollection(oldName, newName, options)]; | ||
case 0: | ||
options = __assign(__assign({}, DEFAULT_BASE_OPTIONS), options); | ||
return [4, this.db(database, dbOptions) | ||
.renameCollection(oldName, newName, options)]; | ||
case 1: return [2, _a.sent()]; | ||
@@ -163,8 +190,11 @@ } | ||
CliServiceProvider.prototype.findAndModify = function (database, collection, query, sort, update, options, dbOptions) { | ||
if (options === void 0) { options = {}; } | ||
return __awaiter(this, void 0, void 0, function () { | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: return [4, this.db(database, dbOptions) | ||
.collection(collection) | ||
.findAndModify(query, sort, update, options)]; | ||
case 0: | ||
options = __assign(__assign({}, DEFAULT_BASE_OPTIONS), options); | ||
return [4, this.db(database, dbOptions) | ||
.collection(collection) | ||
.findAndModify(query, sort, update, options)]; | ||
case 1: return [2, _a.sent()]; | ||
@@ -176,2 +206,3 @@ } | ||
CliServiceProvider.prototype.convertToCapped = function (database, collection, size, options, dbOptions) { | ||
if (options === void 0) { options = {}; } | ||
return __awaiter(this, void 0, void 0, function () { | ||
@@ -181,6 +212,8 @@ var result; | ||
switch (_a.label) { | ||
case 0: return [4, this.runCommand(database, { | ||
convertToCapped: collection, | ||
size: size | ||
}, options, dbOptions)]; | ||
case 0: | ||
options = __assign(__assign({}, DEFAULT_BASE_OPTIONS), options); | ||
return [4, this.runCommand(database, { | ||
convertToCapped: collection, | ||
size: size | ||
}, options, dbOptions)]; | ||
case 1: | ||
@@ -196,4 +229,4 @@ result = _a.sent(); | ||
}; | ||
CliServiceProvider.prototype.db = function (name, options) { | ||
var optionsWithForceNewInstace = __assign(__assign({}, options), { returnNonCachedInstance: true }); | ||
CliServiceProvider.prototype.db = function (name, dbOptions) { | ||
var optionsWithForceNewInstace = __assign(__assign({}, dbOptions), { returnNonCachedInstance: true }); | ||
return this.mongoClient.db(name, optionsWithForceNewInstace); | ||
@@ -204,5 +237,6 @@ }; | ||
if (options === void 0) { options = {}; } | ||
return new node_cursor_1.default(this.db(database, dbOptions) | ||
options = __assign(__assign({}, DEFAULT_BASE_OPTIONS), options); | ||
return this.db(database, dbOptions) | ||
.collection(collection) | ||
.aggregate(pipeline, options)); | ||
.aggregate(pipeline, options); | ||
}; | ||
@@ -212,7 +246,9 @@ CliServiceProvider.prototype.aggregateDb = function (database, pipeline, options, dbOptions) { | ||
if (options === void 0) { options = {}; } | ||
options = __assign(__assign({}, DEFAULT_BASE_OPTIONS), options); | ||
var db = this.db(database, dbOptions); | ||
return new node_cursor_1.default(db.aggregate(pipeline, options)); | ||
return db.aggregate(pipeline, options); | ||
}; | ||
CliServiceProvider.prototype.bulkWrite = function (database, collection, requests, options, dbOptions) { | ||
if (options === void 0) { options = {}; } | ||
options = __assign(__assign({}, DEFAULT_BASE_OPTIONS), options); | ||
return this.db(database, dbOptions) | ||
@@ -228,2 +264,3 @@ .collection(collection) | ||
if (options === void 0) { options = {}; } | ||
options = __assign(__assign({}, DEFAULT_BASE_OPTIONS), options); | ||
return this.db(database, dbOptions) | ||
@@ -236,2 +273,3 @@ .collection(collection) | ||
if (options === void 0) { options = {}; } | ||
options = __assign(__assign({}, DEFAULT_BASE_OPTIONS), options); | ||
return this.db(database, dbOptions) | ||
@@ -244,2 +282,3 @@ .collection(collection) | ||
if (options === void 0) { options = {}; } | ||
options = __assign(__assign({}, DEFAULT_BASE_OPTIONS), options); | ||
return this.db(database, dbOptions) | ||
@@ -252,2 +291,3 @@ .collection(collection) | ||
if (options === void 0) { options = {}; } | ||
options = __assign(__assign({}, DEFAULT_BASE_OPTIONS), options); | ||
return this.db(database, dbOptions) | ||
@@ -260,2 +300,3 @@ .collection(collection) | ||
if (options === void 0) { options = {}; } | ||
options = __assign(__assign({}, DEFAULT_BASE_OPTIONS), options); | ||
return this.db(database, dbOptions) | ||
@@ -267,2 +308,3 @@ .collection(collection) | ||
if (options === void 0) { options = {}; } | ||
options = __assign(__assign({}, DEFAULT_BASE_OPTIONS), options); | ||
return this.db(database, dbOptions) | ||
@@ -275,3 +317,3 @@ .collection(collection) | ||
if (options === void 0) { options = {}; } | ||
var findOptions = __assign({}, options); | ||
var findOptions = __assign(__assign({}, DEFAULT_BASE_OPTIONS), options); | ||
if ('allowPartialResults' in findOptions) { | ||
@@ -286,5 +328,5 @@ findOptions.partial = findOptions.allowPartialResults; | ||
} | ||
return new node_cursor_1.default(this.db(database, dbOptions) | ||
return this.db(database, dbOptions) | ||
.collection(collection) | ||
.find(filter, options)); | ||
.find(filter, findOptions); | ||
}; | ||
@@ -294,2 +336,3 @@ CliServiceProvider.prototype.findOneAndDelete = function (database, collection, filter, options, dbOptions) { | ||
if (options === void 0) { options = {}; } | ||
options = __assign(__assign({}, DEFAULT_BASE_OPTIONS), options); | ||
return this.db(database, dbOptions) | ||
@@ -303,3 +346,3 @@ .collection(collection). | ||
if (options === void 0) { options = {}; } | ||
var findOneAndReplaceOptions = __assign({}, options); | ||
var findOneAndReplaceOptions = __assign(__assign({}, DEFAULT_BASE_OPTIONS), options); | ||
if ('returnDocument' in options) { | ||
@@ -315,3 +358,3 @@ findOneAndReplaceOptions.returnOriginal = options.returnDocument; | ||
if (options === void 0) { options = {}; } | ||
var findOneAndUpdateOptions = __assign({}, options); | ||
var findOneAndUpdateOptions = __assign(__assign({}, DEFAULT_BASE_OPTIONS), options); | ||
if ('returnDocument' in options) { | ||
@@ -328,2 +371,3 @@ findOneAndUpdateOptions.returnOriginal = options.returnDocument; | ||
if (options === void 0) { options = {}; } | ||
options = __assign(__assign({}, DEFAULT_BASE_OPTIONS), options); | ||
return this.db(database, dbOptions) | ||
@@ -336,2 +380,3 @@ .collection(collection) | ||
if (options === void 0) { options = {}; } | ||
options = __assign(__assign({}, DEFAULT_BASE_OPTIONS), options); | ||
return this.db(database, dbOptions) | ||
@@ -347,2 +392,3 @@ .collection(collection) | ||
if (options === void 0) { options = {}; } | ||
options = __assign(__assign({}, DEFAULT_BASE_OPTIONS), options); | ||
return this.db(database, dbOptions) | ||
@@ -354,2 +400,3 @@ .collection(collection) | ||
if (options === void 0) { options = {}; } | ||
options = __assign(__assign({}, DEFAULT_BASE_OPTIONS), options); | ||
return this.db(database, dbOptions).collection(collection). | ||
@@ -362,2 +409,3 @@ save(doc, options); | ||
if (options === void 0) { options = {}; } | ||
options = __assign(__assign({}, DEFAULT_BASE_OPTIONS), options); | ||
return this.db(database, dbOptions) | ||
@@ -369,2 +417,4 @@ .collection(collection) | ||
if (spec === void 0) { spec = {}; } | ||
if (options === void 0) { options = {}; } | ||
options = __assign(__assign({}, DEFAULT_BASE_OPTIONS), options); | ||
var db = this.db(database, dbOptions); | ||
@@ -383,5 +433,7 @@ return db.command(spec, options); | ||
switch (_a.label) { | ||
case 0: return [4, this.db(database, dbOptions) | ||
.collection(collection) | ||
.updateMany(filter, update, options)]; | ||
case 0: | ||
options = __assign(__assign({}, DEFAULT_BASE_OPTIONS), options); | ||
return [4, this.db(database, dbOptions) | ||
.collection(collection) | ||
.updateMany(filter, update, options)]; | ||
case 1: return [2, _a.sent()]; | ||
@@ -396,2 +448,3 @@ } | ||
if (options === void 0) { options = {}; } | ||
options = __assign(__assign({}, DEFAULT_BASE_OPTIONS), options); | ||
return this.db(database, dbOptions) | ||
@@ -457,2 +510,3 @@ .collection(collection) | ||
return __generator(this, function (_a) { | ||
options = __assign(__assign({}, DEFAULT_BASE_OPTIONS), options); | ||
return [2, this.db(database, dbOptions) | ||
@@ -475,9 +529,12 @@ .collection(collection) | ||
CliServiceProvider.prototype.dropIndexes = function (database, collection, indexes, options, dbOptions) { | ||
if (options === void 0) { options = {}; } | ||
return __awaiter(this, void 0, void 0, function () { | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: return [4, this.runCommand(database, { | ||
dropIndexes: collection, | ||
index: indexes, | ||
}, options, dbOptions)]; | ||
case 0: | ||
options = __assign(__assign({}, DEFAULT_BASE_OPTIONS), options); | ||
return [4, this.runCommand(database, { | ||
dropIndexes: collection, | ||
index: indexes, | ||
}, options, dbOptions)]; | ||
case 1: return [2, _a.sent()]; | ||
@@ -494,3 +551,5 @@ } | ||
switch (_a.label) { | ||
case 0: return [4, this.db(database, dbOptions).listCollections(filter, options).toArray()]; | ||
case 0: | ||
options = __assign(__assign({}, DEFAULT_BASE_OPTIONS), options); | ||
return [4, this.db(database, dbOptions).listCollections(filter, options).toArray()]; | ||
case 1: return [2, _a.sent()]; | ||
@@ -506,5 +565,7 @@ } | ||
switch (_a.label) { | ||
case 0: return [4, this.db(database, dbOptions) | ||
.collection(collection) | ||
.stats(options)]; | ||
case 0: | ||
options = __assign(__assign({}, DEFAULT_BASE_OPTIONS), options); | ||
return [4, this.db(database, dbOptions) | ||
.collection(collection) | ||
.stats(options)]; | ||
case 1: return [2, _a.sent()]; | ||
@@ -516,8 +577,11 @@ } | ||
CliServiceProvider.prototype.reIndex = function (database, collection, options, dbOptions) { | ||
if (options === void 0) { options = {}; } | ||
return __awaiter(this, void 0, void 0, function () { | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: return [4, this.runCommand(database, { | ||
reIndex: collection | ||
}, options, dbOptions)]; | ||
case 0: | ||
options = __assign(__assign({}, DEFAULT_BASE_OPTIONS), options); | ||
return [4, this.runCommand(database, { | ||
reIndex: collection | ||
}, options, dbOptions)]; | ||
case 1: return [2, _a.sent()]; | ||
@@ -537,5 +601,44 @@ } | ||
}; | ||
CliServiceProvider.prototype.authenticate = function (authDoc) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var clientOptions, _a; | ||
return __generator(this, function (_b) { | ||
switch (_b.label) { | ||
case 0: | ||
clientOptions = __assign(__assign(__assign({}, DEFAULT_DRIVER_OPTIONS), this.initialOptions), { auth: { user: authDoc.user, password: authDoc.pwd }, authMechanism: authDoc.mechanism, authSource: authDoc.authDb }); | ||
_a = this; | ||
return [4, mongodb_1.MongoClient.connect(this.uri, clientOptions)]; | ||
case 1: | ||
_a.mongoClient = _b.sent(); | ||
return [2, { ok: 1 }]; | ||
} | ||
}); | ||
}); | ||
}; | ||
CliServiceProvider.prototype.createCollection = function (dbName, collName, options, dbOptions) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var e_2; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
_a.trys.push([0, 2, , 3]); | ||
return [4, this.db(dbName, dbOptions).createCollection(collName, options)]; | ||
case 1: | ||
_a.sent(); | ||
return [2, { ok: 1 }]; | ||
case 2: | ||
e_2 = _a.sent(); | ||
return [2, { | ||
ok: 0, | ||
errmsg: e_2.message, | ||
code: e_2.code | ||
}]; | ||
case 3: return [2]; | ||
} | ||
}); | ||
}); | ||
}; | ||
return CliServiceProvider; | ||
}()); | ||
}(service_provider_core_1.ServiceProviderCore)); | ||
exports.default = CliServiceProvider; | ||
//# sourceMappingURL=cli-service-provider.js.map |
{ | ||
"name": "@mongosh/service-provider-server", | ||
"version": "0.0.8", | ||
"version": "0.1.0", | ||
"description": "MongoDB Shell Server Service Provider Package", | ||
@@ -41,4 +41,4 @@ "main": "lib/index.js", | ||
"dependencies": { | ||
"@mongosh/errors": "^0.0.8", | ||
"@mongosh/service-provider-core": "^0.0.8", | ||
"@mongosh/errors": "^0.1.0", | ||
"@mongosh/service-provider-core": "^0.1.0", | ||
"@types/sinon": "^7.5.1", | ||
@@ -48,3 +48,3 @@ "@types/sinon-chai": "^3.2.3", | ||
}, | ||
"gitHead": "ca72bca50bd9654910ea8f36bb6eb3c823cd8a49" | ||
"gitHead": "57433c03fa9dbbd1a2448c396ec963174643fa1d" | ||
} |
@@ -79,2 +79,28 @@ import CliServiceProvider from './cli-service-provider'; | ||
describe('#aggregate', () => { | ||
// NOTE: this will only run on 4.4+, so if we change mongodb-runner to a different version we should be aware this will fail. | ||
context('when passing a $function to be serialized by the driver', () => { | ||
let result; | ||
beforeEach(async() => { | ||
const pipeline = [ | ||
{ | ||
'$addFields': { | ||
'attr.namespace': { | ||
'$function': { | ||
'body': function(value): any { if (value) { return value; } }, | ||
'args': [ '$attr.namespace' ], | ||
'lang': 'js' | ||
} | ||
} | ||
} | ||
} | ||
]; | ||
result = await serviceProvider.aggregate('music', 'bands', pipeline); | ||
}); | ||
it('executes the command and resolves the result', async() => { | ||
const docs = await result.toArray(); | ||
expect(docs).to.deep.equal([]); | ||
}); | ||
}); | ||
context('when running against a collection', () => { | ||
@@ -603,16 +629,14 @@ let result; | ||
nIndexes: 1, | ||
indexes: [ | ||
{ | ||
v: 2, | ||
key: { | ||
'_id': 1 | ||
}, | ||
name: '_id_', | ||
ns: `${dbName}.${collName}` | ||
} | ||
], | ||
ok: 1 | ||
}); | ||
expect(result.indexes.length).to.equal(1); | ||
expect(result.indexes[0]).to.deep.include({ | ||
v: 2, | ||
key: { | ||
'_id': 1 | ||
}, | ||
name: '_id_' | ||
}); | ||
}); | ||
}); | ||
}); |
@@ -9,2 +9,4 @@ import mongodb, { MongoClient, Db } from 'mongodb'; | ||
const DEFAULT_BASE_OPTS = { serializeFunctions: true }; | ||
/** | ||
@@ -140,3 +142,3 @@ * Create a client stub from the provided collection stub. | ||
const distinctMock = sinon.mock().once(). | ||
withArgs('name', {}, {}).resolves(distinctResult); | ||
withArgs('name', {}, DEFAULT_BASE_OPTS).resolves(distinctResult); | ||
@@ -159,3 +161,3 @@ beforeEach(() => { | ||
const countResult = 10; | ||
const countMock = sinon.mock().once().withArgs({}).resolves(countResult); | ||
const countMock = sinon.mock().once().withArgs(DEFAULT_BASE_OPTS).resolves(countResult); | ||
@@ -196,3 +198,26 @@ beforeEach(() => { | ||
}); | ||
describe('#find with options', () => { | ||
const filter = { name: 'Aphex Twin' }; | ||
const findResult = [{ name: 'Aphex Twin' }]; | ||
const options = { allowPartialResults: true, noCursorTimeout: true, tailable: true }; | ||
const findMock = sinon.mock().withArgs( | ||
filter, | ||
{ ...DEFAULT_BASE_OPTS, ...options, partial: true, timeout: true, cursorType: 'TAILABLE' } | ||
).returns({ toArray: () => Promise.resolve(findResult) }); | ||
beforeEach(() => { | ||
const collectionStub = sinon.createStubInstance(Collection, { | ||
find: findMock | ||
}); | ||
serviceProvider = new CliServiceProvider(createClientStub(collectionStub)); | ||
}); | ||
it('executes the command against the database', async() => { | ||
const cursor = await serviceProvider.find('music', 'bands', filter, options); | ||
const result = await cursor.toArray(); | ||
expect(result).to.deep.equal(findResult); | ||
(findMock as any).verify(); | ||
}); | ||
}); | ||
describe('#findAndModify', () => { | ||
@@ -204,3 +229,3 @@ const commandResult = { result: { n: 1, ok: 1 } }; | ||
{ update: 1 }, | ||
{ options: 1 } | ||
{ ...DEFAULT_BASE_OPTS, options: 1 } | ||
).resolves(commandResult); | ||
@@ -490,3 +515,3 @@ | ||
commandMock = sinon.mock() | ||
.withArgs({ buildInfo: 1 }, {}) | ||
.withArgs({ buildInfo: 1 }, DEFAULT_BASE_OPTS) | ||
.resolves(buildInfoResult); | ||
@@ -537,3 +562,3 @@ | ||
commandMock = sinon.mock() | ||
.withArgs({ getCmdLineOpts: 1 }, {}) | ||
.withArgs({ getCmdLineOpts: 1 }, DEFAULT_BASE_OPTS) | ||
.resolves(cmdLineOptsResult); | ||
@@ -709,3 +734,3 @@ | ||
commandMock = sinon.mock() | ||
.withArgs({}, {}) | ||
.withArgs({}, DEFAULT_BASE_OPTS) | ||
.returns({ | ||
@@ -755,3 +780,3 @@ toArray: () => { | ||
beforeEach(() => { | ||
options = { scale: 1 }; | ||
options = { ...DEFAULT_BASE_OPTS, scale: 1 }; | ||
expectedResult = { ok: 1 }; | ||
@@ -818,2 +843,3 @@ statsMock = sinon.mock().once().withArgs(options). | ||
{ | ||
...DEFAULT_BASE_OPTS, | ||
dropTarget: true, | ||
@@ -851,2 +877,42 @@ session: 1 | ||
}); | ||
describe('#createCollection', () => { | ||
let commandMock; | ||
let dbMock; | ||
let clientStub: MongoClient; | ||
beforeEach(() => { | ||
commandMock = sinon.mock() | ||
.withArgs('newcoll', {}) | ||
.returns({ | ||
toArray: () => { | ||
return Promise.resolve([ | ||
{ collectionType: 1 } | ||
]); | ||
} | ||
}); | ||
const dbStub = sinon.createStubInstance(Db, { | ||
createCollection: commandMock | ||
}); | ||
dbMock = sinon.mock() | ||
.withArgs('db1') | ||
.returns(dbStub); | ||
clientStub = sinon.createStubInstance(MongoClient, { | ||
db: dbMock | ||
}); | ||
serviceProvider = new CliServiceProvider(clientStub); | ||
}); | ||
it('executes the command', async() => { | ||
const result = await serviceProvider.createCollection('db1', 'newcoll', {}); | ||
expect(result).to.deep.equal({ ok: 1 }); | ||
(dbMock as any).verify(); | ||
(commandMock as any).verify(); | ||
}); | ||
}); | ||
}); |
@@ -1,2 +0,2 @@ | ||
import { | ||
import mongodb, { | ||
MongoClient, | ||
@@ -17,7 +17,8 @@ Db | ||
ReplPlatform, | ||
DEFAULT_DB | ||
DEFAULT_DB, | ||
ServiceProviderCore, | ||
AuthOptions | ||
} from '@mongosh/service-provider-core'; | ||
import NodeOptions from './node/node-options'; | ||
import NodeCursor from './node/node-cursor'; | ||
@@ -32,3 +33,3 @@ type DropDatabaseResult = { | ||
*/ | ||
const DEFAULT_OPTIONS = Object.freeze({ | ||
const DEFAULT_DRIVER_OPTIONS = Object.freeze({ | ||
useNewUrlParser: true, | ||
@@ -39,5 +40,12 @@ useUnifiedTopology: true | ||
/** | ||
* Default driver method options we always use. | ||
*/ | ||
const DEFAULT_BASE_OPTIONS = Object.freeze({ | ||
serializeFunctions: true | ||
}); | ||
/** | ||
* Encapsulates logic for the service provider for the mongosh CLI. | ||
*/ | ||
class CliServiceProvider implements ServiceProvider { | ||
class CliServiceProvider extends ServiceProviderCore implements ServiceProvider { | ||
public readonly platform: ReplPlatform; | ||
@@ -60,3 +68,3 @@ public readonly initialDb: string; | ||
const clientOptions: any = { | ||
...DEFAULT_OPTIONS, | ||
...DEFAULT_DRIVER_OPTIONS, | ||
...options | ||
@@ -72,7 +80,8 @@ }; | ||
return new CliServiceProvider(mongoClient, uri); | ||
return new CliServiceProvider(mongoClient, clientOptions, uri); | ||
} | ||
private readonly mongoClient: MongoClient; | ||
private mongoClient: MongoClient; | ||
private readonly uri?: string; | ||
private initialOptions: any; | ||
@@ -86,3 +95,4 @@ /** | ||
*/ | ||
constructor(mongoClient: MongoClient, uri?: string) { | ||
constructor(mongoClient: MongoClient, clientOptions = {}, uri?: string) { | ||
super(mongodb); | ||
this.mongoClient = mongoClient; | ||
@@ -96,2 +106,3 @@ this.uri = uri; | ||
} | ||
this.initialOptions = clientOptions; | ||
} | ||
@@ -101,3 +112,3 @@ | ||
const clientOptions: any = { | ||
...DEFAULT_OPTIONS, | ||
...DEFAULT_DRIVER_OPTIONS, | ||
...options | ||
@@ -143,4 +154,5 @@ }; | ||
newName: string, | ||
options?: Document, | ||
options: Document = {}, | ||
dbOptions?: DatabaseOptions): Promise<any> { | ||
options = { ...DEFAULT_BASE_OPTIONS, ...options }; | ||
return await this.db(database, dbOptions) | ||
@@ -156,5 +168,6 @@ .renameCollection(oldName, newName, options); | ||
update: Document, | ||
options?: Document, | ||
options: Document = {}, | ||
dbOptions?: DatabaseOptions | ||
): Promise<any> { | ||
options = { ...DEFAULT_BASE_OPTIONS, ...options }; | ||
return await this.db(database, dbOptions) | ||
@@ -179,5 +192,6 @@ .collection(collection) | ||
size: number, | ||
options?: CommandOptions, | ||
options: CommandOptions = {}, | ||
dbOptions?: DatabaseOptions | ||
): Promise<any> { | ||
options = { ...DEFAULT_BASE_OPTIONS, ...options }; | ||
const result: any = await this.runCommand( | ||
@@ -208,5 +222,5 @@ database, | ||
*/ | ||
private db(name: string, options?: DatabaseOptions): Db { | ||
private db(name: string, dbOptions?: DatabaseOptions): Db { | ||
const optionsWithForceNewInstace: DatabaseOptions = { | ||
...options, | ||
...dbOptions, | ||
@@ -249,7 +263,6 @@ // Without this option any read/write concerns | ||
dbOptions?: DatabaseOptions): Cursor { | ||
return new NodeCursor( | ||
this.db(database, dbOptions) | ||
.collection(collection) | ||
.aggregate(pipeline, options) | ||
); | ||
options = { ...DEFAULT_BASE_OPTIONS, ...options }; | ||
return this.db(database, dbOptions) | ||
.collection(collection) | ||
.aggregate(pipeline, options); | ||
} | ||
@@ -280,4 +293,5 @@ | ||
dbOptions?: DatabaseOptions): Cursor { | ||
options = { ...DEFAULT_BASE_OPTIONS, ...options }; | ||
const db: any = (this.db(database, dbOptions) as any); | ||
return new NodeCursor(db.aggregate(pipeline, options)); | ||
return db.aggregate(pipeline, options); | ||
} | ||
@@ -306,2 +320,3 @@ | ||
dbOptions?: DatabaseOptions): Promise<BulkWriteResult> { | ||
options = { ...DEFAULT_BASE_OPTIONS, ...options }; | ||
return this.db(database, dbOptions) | ||
@@ -344,2 +359,3 @@ .collection(collection) | ||
dbOptions?: DatabaseOptions): Promise<Result> { | ||
options = { ...DEFAULT_BASE_OPTIONS, ...options }; | ||
return this.db(database, dbOptions) | ||
@@ -369,2 +385,3 @@ .collection(collection) | ||
dbOptions?: DatabaseOptions): Promise<Result> { | ||
options = { ...DEFAULT_BASE_OPTIONS, ...options }; | ||
return this.db(database, dbOptions) | ||
@@ -392,2 +409,3 @@ .collection(collection) | ||
dbOptions?: DatabaseOptions): Promise<Result> { | ||
options = { ...DEFAULT_BASE_OPTIONS, ...options }; | ||
return this.db(database, dbOptions) | ||
@@ -415,2 +433,3 @@ .collection(collection) | ||
dbOptions?: DatabaseOptions): Promise<Result> { | ||
options = { ...DEFAULT_BASE_OPTIONS, ...options }; | ||
return this.db(database, dbOptions) | ||
@@ -440,2 +459,3 @@ .collection(collection) | ||
dbOptions?: DatabaseOptions): Promise<any> { | ||
options = { ...DEFAULT_BASE_OPTIONS, ...options }; | ||
return this.db(database, dbOptions) | ||
@@ -461,2 +481,3 @@ .collection(collection) | ||
dbOptions?: DatabaseOptions): Promise<Result> { | ||
options = { ...DEFAULT_BASE_OPTIONS, ...options }; | ||
return this.db(database, dbOptions) | ||
@@ -483,3 +504,3 @@ .collection(collection) | ||
dbOptions?: DatabaseOptions): Cursor { | ||
const findOptions: any = { ...options }; | ||
const findOptions: any = { ...DEFAULT_BASE_OPTIONS, ...options }; | ||
if ('allowPartialResults' in findOptions) { | ||
@@ -494,7 +515,5 @@ findOptions.partial = findOptions.allowPartialResults; | ||
} | ||
return new NodeCursor( | ||
this.db(database, dbOptions) | ||
.collection(collection) | ||
.find(filter, options) | ||
); | ||
return this.db(database, dbOptions) | ||
.collection(collection) | ||
.find(filter, findOptions); | ||
} | ||
@@ -519,2 +538,3 @@ | ||
dbOptions?: DatabaseOptions): Promise<Result> { | ||
options = { ...DEFAULT_BASE_OPTIONS, ...options }; | ||
return this.db(database, dbOptions) | ||
@@ -543,3 +563,3 @@ .collection(collection). | ||
dbOptions?: DatabaseOptions): Promise<Result> { | ||
const findOneAndReplaceOptions: any = { ...options }; | ||
const findOneAndReplaceOptions: any = { ...DEFAULT_BASE_OPTIONS, ...options }; | ||
if ('returnDocument' in options) { | ||
@@ -573,3 +593,3 @@ findOneAndReplaceOptions.returnOriginal = options.returnDocument; | ||
dbOptions?: DatabaseOptions): Promise<Result> { | ||
const findOneAndUpdateOptions: any = { ...options }; | ||
const findOneAndUpdateOptions: any = { ...DEFAULT_BASE_OPTIONS, ...options }; | ||
if ('returnDocument' in options) { | ||
@@ -606,2 +626,3 @@ findOneAndUpdateOptions.returnOriginal = options.returnDocument; | ||
dbOptions?: DatabaseOptions): Promise<Result> { | ||
options = { ...DEFAULT_BASE_OPTIONS, ...options }; | ||
return this.db(database, dbOptions) | ||
@@ -629,2 +650,3 @@ .collection(collection) | ||
dbOptions?: DatabaseOptions): Promise<Result> { | ||
options = { ...DEFAULT_BASE_OPTIONS, ...options }; | ||
return this.db(database, dbOptions) | ||
@@ -664,2 +686,3 @@ .collection(collection) | ||
dbOptions?: DatabaseOptions): Promise<Result> { | ||
options = { ...DEFAULT_BASE_OPTIONS, ...options }; | ||
return this.db(database, dbOptions) | ||
@@ -689,2 +712,3 @@ .collection(collection) | ||
dbOptions?: DatabaseOptions): Promise<Result> { | ||
options = { ...DEFAULT_BASE_OPTIONS, ...options }; | ||
return this.db(database, dbOptions).collection(collection). | ||
@@ -714,2 +738,3 @@ save(doc, options); | ||
): Promise<Result> { | ||
options = { ...DEFAULT_BASE_OPTIONS, ...options }; | ||
return this.db(database, dbOptions) | ||
@@ -732,5 +757,6 @@ .collection(collection) | ||
spec: Document = {}, | ||
options?: CommandOptions, | ||
options: CommandOptions = {}, | ||
dbOptions?: DatabaseOptions | ||
): Promise<Result> { | ||
options = { ...DEFAULT_BASE_OPTIONS, ...options }; | ||
const db: any = this.db(database, dbOptions); | ||
@@ -773,2 +799,3 @@ return db.command( | ||
dbOptions?: DatabaseOptions): Promise<Result> { | ||
options = { ...DEFAULT_BASE_OPTIONS, ...options }; | ||
return await this.db(database, dbOptions) | ||
@@ -798,2 +825,3 @@ .collection(collection) | ||
dbOptions?: DatabaseOptions): Promise<Result> { | ||
options = { ...DEFAULT_BASE_OPTIONS, ...options }; | ||
return this.db(database, dbOptions) | ||
@@ -886,2 +914,3 @@ .collection(collection) | ||
dbOptions?: DatabaseOptions): Promise<Result> { | ||
options = { ...DEFAULT_BASE_OPTIONS, ...options }; | ||
return this.db(database, dbOptions) | ||
@@ -928,4 +957,5 @@ .collection(collection) | ||
indexes: string|string[]|Document|Document[], | ||
options?: CommandOptions, | ||
options: CommandOptions = {}, | ||
dbOptions?: DatabaseOptions): Promise<Result> { | ||
options = { ...DEFAULT_BASE_OPTIONS, ...options }; | ||
return await this.runCommand(database, { | ||
@@ -953,2 +983,3 @@ dropIndexes: collection, | ||
dbOptions?: DatabaseOptions): Promise<Result> { | ||
options = { ...DEFAULT_BASE_OPTIONS, ...options }; | ||
return await this.db(database, dbOptions).listCollections( | ||
@@ -973,2 +1004,3 @@ filter, options | ||
dbOptions?: DatabaseOptions): Promise<any> { | ||
options = { ...DEFAULT_BASE_OPTIONS, ...options }; | ||
return await this.db(database, dbOptions) | ||
@@ -980,3 +1012,3 @@ .collection(collection) | ||
/** | ||
* Reindex all indexes on the collection. | ||
* Reindex all indexes on the collection.i | ||
* | ||
@@ -992,5 +1024,6 @@ * @param {String} database - The db name. | ||
collection: string, | ||
options?: CommandOptions, | ||
options: CommandOptions = {}, | ||
dbOptions?: DatabaseOptions | ||
): Promise<Result> { | ||
options = { ...DEFAULT_BASE_OPTIONS, ...options }; | ||
return await this.runCommand(database, { | ||
@@ -1019,4 +1052,47 @@ reIndex: collection | ||
} | ||
/** | ||
* Authenticate | ||
* | ||
* @param authDoc | ||
*/ | ||
async authenticate( | ||
authDoc: AuthOptions | ||
): Promise<any> { | ||
// NOTE: we keep all the original options and just overwrite the auth ones. | ||
const clientOptions: any = { | ||
...DEFAULT_DRIVER_OPTIONS, | ||
...this.initialOptions, | ||
auth: { user: authDoc.user, password: authDoc.pwd }, | ||
authMechanism: authDoc.mechanism, | ||
authSource: authDoc.authDb | ||
}; | ||
this.mongoClient = await MongoClient.connect( | ||
this.uri, | ||
clientOptions | ||
); | ||
return { ok: 1 }; | ||
} | ||
async createCollection( | ||
dbName: string, | ||
collName: string, | ||
options: any, | ||
dbOptions?: any | ||
): Promise<any> { | ||
try { | ||
await this.db(dbName, dbOptions).createCollection( | ||
collName, options | ||
); | ||
return { ok: 1 }; | ||
} catch (e) { | ||
return { | ||
ok: 0, | ||
errmsg: e.message, | ||
code: e.code | ||
}; | ||
} | ||
} | ||
} | ||
export default CliServiceProvider; |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
240742
57
4645
+ Added@mongosh/errors@0.1.0(transitive)
+ Added@mongosh/i18n@0.1.0(transitive)
+ Added@mongosh/service-provider-core@0.1.0(transitive)
+ Addedbase64-js@1.5.1(transitive)
+ Addedbson@4.0.4(transitive)
+ Addedbuffer@5.7.1(transitive)
+ Addedieee754@1.2.1(transitive)
+ Addedlong@4.0.0(transitive)
- Removed@mongosh/errors@0.0.8(transitive)
- Removed@mongosh/i18n@0.0.8(transitive)
- Removed@mongosh/service-provider-core@0.0.8(transitive)
Updated@mongosh/errors@^0.1.0