Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@mongosh/service-provider-server

Package Overview
Dependencies
Maintainers
7
Versions
121
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@mongosh/service-provider-server - npm Package Compare versions

Comparing version 0.0.1-alpha.6 to 0.0.1-alpha.10

8

lib/cli-service-provider.d.ts

@@ -34,3 +34,3 @@ import { MongoClient } from 'mongodb';

replaceOne(database: string, collection: string, filter?: Document, replacement?: Document, options?: Document, dbOptions?: Document): Promise<Result>;
runCommand(database: string, spec?: Document, options?: Document): Promise<Result>;
runCommand(database: string, spec?: Document, options?: Document, dbOptions?: Document): Promise<Result>;
listDatabases(database: string): Promise<Result>;

@@ -41,3 +41,9 @@ updateMany(database: string, collection: string, filter?: Document, update?: Document, options?: Document, dbOptions?: Document): Promise<Result>;

dropDatabase(db: string, writeConcern?: Document): Promise<DropDatabaseResult>;
createIndexes(database: string, collection: string, indexSpecs: Document[], options?: Document, dbOptions?: Document): Promise<Result>;
getIndexes(database: string, collection: string, dbOptions?: Document): Promise<Result>;
dropIndexes(database: string, collection: string, indexes: string | string[] | Document | Document[], options?: Document, dbOptions?: Document): Promise<Result>;
listCollections(database: string, filter?: Document, options?: Document, dbOptions?: Document): Promise<Result>;
stats(database: string, collection: string, options?: Document, dbOptions?: Document): Promise<any>;
reIndex(database: string, collection: string, options?: Document, dbOptions?: Document): Promise<Result>;
}
export default CliServiceProvider;

@@ -255,6 +255,7 @@ "use strict";

};
CliServiceProvider.prototype.runCommand = function (database, spec, options) {
CliServiceProvider.prototype.runCommand = function (database, spec, options, dbOptions) {
if (spec === void 0) { spec = {}; }
if (options === void 0) { options = {}; }
return this.db(database).command(spec, options);
if (dbOptions === void 0) { dbOptions = {}; }
return this.db(database, dbOptions).command(spec, options);
};

@@ -269,4 +270,14 @@ CliServiceProvider.prototype.listDatabases = function (database) {

if (dbOptions === void 0) { dbOptions = {}; }
return this.db(database, dbOptions).collection(collection).
updateMany(filter, update, options);
return __awaiter(this, void 0, void 0, function () {
var result;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4, this.db(database, dbOptions).collection(collection).
updateMany(filter, update, options)];
case 1:
result = _a.sent();
return [2, result];
}
});
});
};

@@ -315,2 +326,76 @@ CliServiceProvider.prototype.updateOne = function (database, collection, filter, update, options, dbOptions) {

};
CliServiceProvider.prototype.createIndexes = function (database, collection, indexSpecs, options, dbOptions) {
if (options === void 0) { options = {}; }
if (dbOptions === void 0) { dbOptions = {}; }
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
return [2, this.db(database, dbOptions)
.collection(collection)
.createIndexes(indexSpecs, options)];
});
});
};
CliServiceProvider.prototype.getIndexes = function (database, collection, dbOptions) {
if (dbOptions === void 0) { dbOptions = {}; }
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
return [2, this.db(database, dbOptions)
.collection(collection)
.listIndexes()
.toArray()];
});
});
};
CliServiceProvider.prototype.dropIndexes = function (database, collection, indexes, options, dbOptions) {
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 1: return [2, _a.sent()];
}
});
});
};
CliServiceProvider.prototype.listCollections = function (database, filter, options, dbOptions) {
if (filter === void 0) { filter = {}; }
if (options === void 0) { options = {}; }
if (dbOptions === void 0) { dbOptions = {}; }
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4, this.db(database, dbOptions).listCollections(filter, options).toArray()];
case 1: return [2, _a.sent()];
}
});
});
};
CliServiceProvider.prototype.stats = function (database, collection, options, dbOptions) {
if (options === void 0) { options = {}; }
if (dbOptions === void 0) { dbOptions = {}; }
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)
.stats(options)];
case 1: return [2, _a.sent()];
}
});
});
};
CliServiceProvider.prototype.reIndex = function (database, collection, options, dbOptions) {
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 1: return [2, _a.sent()];
}
});
});
};
return CliServiceProvider;

@@ -317,0 +402,0 @@ }());

7

lib/compass-service-provider.d.ts
import CliServiceProvider from './cli-service-provider';
import { MongoClient } from 'mongodb';
interface DataService {
client: {
client: MongoClient;
};
}
declare class CompassServiceProvider extends CliServiceProvider {
static fromDataService(dataService: DataService): CompassServiceProvider;
}
export default CompassServiceProvider;

@@ -20,3 +20,2 @@ "use strict";

var cli_service_provider_1 = __importDefault(require("./cli-service-provider"));
var mongosh_transport_server_1 = require("mongosh-transport-server");
var CompassServiceProvider = (function (_super) {

@@ -27,7 +26,2 @@ __extends(CompassServiceProvider, _super);

}
CompassServiceProvider.fromDataService = function (dataService) {
var mongoClient = dataService.client.client;
var nodeTransport = new mongosh_transport_server_1.NodeTransport(mongoClient);
return new CompassServiceProvider(nodeTransport);
};
return CompassServiceProvider;

@@ -34,0 +28,0 @@ }(cli_service_provider_1.default));

{
"name": "@mongosh/service-provider-server",
"version": "0.0.1-alpha.6",
"version": "0.0.1-alpha.10",
"description": "MongoDB Shell Server Service Provider Package",
"main": "lib/index.js",
"types": "lib/index.d.ts",
"config": {
"unsafe-perm": true
},
"repository": {
"type": "git",
"url": "git://github.com/mongodb-js/mongosh.git"
},
"scripts": {
"compile-ts": "tsc -p tsconfig.json",
"test": "mocha --colors -r ts-node/register \"./src/**/*.spec.ts\"",
"test-ci": "mocha -r ts-node/register \"./src/**/*.spec.ts\"",
"test": "mocha --timeout 15000 --colors -r ts-node/register \"./src/**/*.spec.ts\"",
"test-ci": "mocha --timeout 15000 -r ts-node/register \"./src/**/*.spec.ts\"",
"prepublish": "npm run compile-ts",

@@ -15,3 +22,3 @@ "lint": "eslint \"**/*.{js,ts,tsx}\"",

},
"license": "SSPL",
"license": "Apache-2.0",
"publishConfig": {

@@ -36,6 +43,6 @@ "access": "public"

"dependencies": {
"@mongosh/service-provider-core": "^0.0.1-alpha.6",
"@mongosh/service-provider-core": "^0.0.1-alpha.10",
"mongodb": "3.5.3 || ^3.5.5"
},
"gitHead": "280033fe0c406fc520db253d06688f36546692fd"
"gitHead": "72e9d284d6e622e9844b063ab5a11df66fa08aee"
}

@@ -394,3 +394,3 @@ import CliServiceProvider from './cli-service-provider';

await nativeCollection.insertOne({ doc: 1 });
await db.createCollection(collName);

@@ -412,2 +412,182 @@ expect(

});
describe('#createIndexes', () => {
it('creates a new index', async() => {
const collName = 'coll1';
const nativeCollection = db.collection(collName);
await db.createCollection(collName);
expect(
await nativeCollection.indexExists('index-1')
).to.be.false;
await serviceProvider.createIndexes(
dbName,
collName,
[{
name: 'index-1',
key: { x: 1 }
}]
);
expect(
await nativeCollection.indexExists('index-1')
).to.be.true;
});
});
describe('#getIndexes', () => {
it('returns indexes', async() => {
const collName = 'coll1';
const nativeCollection = db.collection(collName);
await nativeCollection.createIndex('x');
const result = await serviceProvider.getIndexes(
dbName,
collName
);
expect(
result.map((spec) => spec.key)
).to.deep.equal([{ _id: 1 }, { x: 1 }]);
});
});
describe('#dropIndexes', () => {
it('drop existing indexes', async() => {
const collName = 'coll1';
const nativeCollection = db.collection(collName);
await nativeCollection.createIndex({ x: 1 }, { name: 'index-1' });
expect(
await nativeCollection.indexExists('index-1')
).to.be.true;
await serviceProvider.dropIndexes(
dbName,
collName,
['index-1']
);
expect(
await nativeCollection.indexExists('index-1')
).to.be.false;
});
it('throws an error if index does not exist', async() => {
const collName = 'coll1';
await db.createCollection(collName);
let error;
await serviceProvider.dropIndexes(
dbName,
collName,
['index-1']
).catch(err => {error = err;});
expect(error.ok).to.equal(0);
expect(error.codeName).to.equal('IndexNotFound');
});
});
describe('stats', () => {
it('returns collection stats', async() => {
const collName = 'coll1';
await db.createCollection(collName);
const stats = await serviceProvider.stats(
dbName,
collName
);
expect(Object.keys(stats)).to.contain.members([
'ns',
'size',
'count',
'storageSize',
'capped',
'wiredTiger',
'nindexes',
'indexDetails',
'indexBuilds',
'totalIndexSize',
'indexSizes',
'scaleFactor',
'ok'
]);
});
});
describe('#listCollections', () => {
it('returns the list of collections', async() => {
await db.createCollection('coll1');
expect(
(await serviceProvider.listCollections(dbName)).map((c) => c.name)
).to.deep.equal(['coll1']);
});
it('filter the list of collections', async() => {
await db.createCollection('coll1');
await db.createCollection('coll2');
expect(
(await serviceProvider.listCollections(dbName, { name: 'coll2' })).map((c) => c.name)
).to.deep.equal(['coll2']);
});
it('allows options', async() => {
await db.createCollection('coll1');
await db.createCollection('coll2');
const collections = await serviceProvider.listCollections(dbName, {}, { nameOnly: true });
expect(
collections
).to.deep.contain({
name: 'coll1',
type: 'collection'
});
expect(
collections
).to.deep.contain({
name: 'coll2',
type: 'collection'
});
});
});
describe('#reIndex', () => {
it('runs against the db', async() => {
const collName = 'coll1';
await db.createCollection(collName);
const result = await serviceProvider.reIndex(
dbName,
collName
);
expect(
result
).to.deep.equal({
nIndexesWas: 1,
nIndexes: 1,
indexes: [
{
v: 2,
key: {
'_id': 1
},
name: '_id_',
ns: `${dbName}.${collName}`
}
],
ok: 1
});
});
});
});

@@ -507,2 +507,210 @@ import mongodb, { MongoClient, Db } from 'mongodb';

});
describe('#createIndexes', () => {
let indexSpecs;
let nativeMethodResult;
let nativeMethodMock;
beforeEach(() => {
indexSpecs = [
{ key: 'x' }
];
nativeMethodResult = {
createdCollectionAutomatically: false,
numIndexesBefore: 2,
numIndexesAfter: 3,
ok: 1
};
nativeMethodMock = sinon.mock().once().withArgs(indexSpecs).
resolves(nativeMethodResult);
const collectionStub = sinon.createStubInstance(Collection, {
createIndexes: nativeMethodMock
});
serviceProvider = new CliServiceProvider(createClientStub(collectionStub));
});
it('executes the command against the database', async() => {
const result = await serviceProvider.createIndexes(
'db1',
'coll1',
indexSpecs);
expect(result).to.deep.equal(nativeMethodResult);
nativeMethodMock.verify();
});
});
describe('#getIndexes', () => {
let indexSpecs;
let nativeMethodResult;
let nativeMethodMock;
beforeEach(() => {
indexSpecs = [
{ key: 'x' }
];
nativeMethodResult = {
toArray: (): Promise<any[]> => Promise.resolve(indexSpecs)
};
nativeMethodMock = sinon.mock().once().withArgs().
returns(nativeMethodResult);
const collectionStub = sinon.createStubInstance(Collection, {
listIndexes: nativeMethodMock
});
serviceProvider = new CliServiceProvider(createClientStub(collectionStub));
});
it('executes the command against the database', async() => {
const result = await (serviceProvider as any).getIndexes(
'db1',
'coll1'
);
expect(result).to.deep.equal(indexSpecs);
nativeMethodMock.verify();
});
});
describe('#dropIndexes', () => {
let commandMock;
let dbMock;
let clientStub: MongoClient;
beforeEach(() => {
commandMock = sinon.mock()
.withArgs({ dropIndexes: 'coll1', index: ['index-1'] }, {})
.resolves({ ok: 1 });
const dbStub = sinon.createStubInstance(Db, {
command: 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.dropIndexes('db1', 'coll1', ['index-1']);
expect(result).to.deep.equal({ ok: 1 });
dbMock.verify();
commandMock.verify();
});
});
describe('#listCollections', () => {
let commandMock;
let dbMock;
let clientStub: MongoClient;
beforeEach(() => {
commandMock = sinon.mock()
.withArgs({}, {})
.returns({
toArray: () => {
return Promise.resolve([
{
name: 'coll1'
}
]);
}
});
const dbStub = sinon.createStubInstance(Db, {
listCollections: 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.listCollections('db1');
expect(result).to.deep.equal([
{
name: 'coll1'
}
]);
dbMock.verify();
commandMock.verify();
});
});
describe('#stats', () => {
let options;
let expectedResult;
let statsMock;
beforeEach(() => {
options = { scale: 1 };
expectedResult = { ok: 1 };
statsMock = sinon.mock().once().withArgs(options).
resolves(expectedResult);
const collectionStub = sinon.createStubInstance(Collection, {
stats: statsMock
});
serviceProvider = new CliServiceProvider(createClientStub(collectionStub));
});
it('executes the command against the database', async() => {
const result = await serviceProvider.stats('db1', 'coll1', options);
expect(result).to.deep.equal(expectedResult);
statsMock.verify();
});
});
describe('#reIndex', () => {
let commandMock;
let dbMock;
let clientStub: MongoClient;
beforeEach(() => {
commandMock = sinon.mock()
.withArgs({ reIndex: 'coll1' }, {})
.resolves({ ok: 1 });
const dbStub = sinon.createStubInstance(Db, {
command: commandMock
});
dbMock = sinon.mock()
.withArgs('db1')
.returns(dbStub);
clientStub = sinon.createStubInstance(MongoClient, {
db: dbMock
});
serviceProvider = new CliServiceProvider(clientStub);
});
it('executes the command against the database', async() => {
const result = await serviceProvider.reIndex('db1', 'coll1');
expect(result).to.deep.equal({ ok: 1 });
dbMock.verify();
commandMock.verify();
});
});
});

@@ -451,3 +451,3 @@ import { MongoClient, Db } from 'mongodb';

/**
* Insert many documents into the colleciton.
* Insert many documents into the collection.
*

@@ -583,4 +583,5 @@ * @param {String} database - The database name.

spec: Document = {},
options: Document = {}): Promise<Result> {
return (this.db(database) as any).command(spec, options as any);
options: Document = {},
dbOptions: Document = {}): Promise<Result> {
return (this.db(database, dbOptions) as any).command(spec, options as any);
}

@@ -611,3 +612,3 @@

*/
updateMany(
async updateMany(
database: string,

@@ -619,4 +620,6 @@ collection: string,

dbOptions: Document = {}): Promise<Result> {
return this.db(database, dbOptions).collection(collection).
const result = await this.db(database, dbOptions).collection(collection).
updateMany(filter, update, options);
return result;
}

@@ -689,4 +692,127 @@

}
/**
* Adds new indexes to a collection.
*
* @param {String} database - The db name.
* @param {String} collection - The collection name.
* @param {Object[]} indexSpecs the spec of the intexes to be created.
* @param {Object} options - The command options.
* @param {Object} dbOptions - The database options (i.e. readConcern, writeConcern. etc).
* @return {Promise}
*/
async createIndexes(
database: string,
collection: string,
indexSpecs: Document[],
options: Document = {},
dbOptions: Document = {}): Promise<Result> {
return this.db(database, dbOptions)
.collection(collection)
.createIndexes(indexSpecs, options);
}
/**
* Returns an array that holds a list of documents that identify and
* describe the existing indexes on the collection.
*
* @param {String} database - The db name.
* @param {String} collection - The collection name.
* @param {Object} dbOptions - The database options
* (i.e. readConcern, writeConcern. etc).
*
* @return {Promise}
*/
async getIndexes(
database: string,
collection: string,
dbOptions: Document = {}): Promise<Result> {
return this.db(database, dbOptions)
.collection(collection)
.listIndexes()
.toArray();
}
/**
* Drop indexes for a collection.
*
* @param {String} database - The db name.
* @param {String} collection - The collection name.
* @param {string|string[]|Object|Object[]} indexes the indexes to be removed.
* @param {Object} options - The command options.
* @param {Object} dbOptions - The database options (i.e. readConcern, writeConcern. etc).
* @return {Promise}
*/
async dropIndexes(
database: string,
collection: string,
indexes: string|string[]|Document|Document[],
options?: Document,
dbOptions?: Document): Promise<Result> {
return await this.runCommand(database, {
dropIndexes: collection,
index: indexes,
}, options, dbOptions);
}
/**
* Returns an array of collection infos
*
* @param {String} database - The db name.
* @param {Document} filter - The filter.
* @param {Document} options - The command options.
* @param {Object} dbOptions - The database options
* (i.e. readConcern, writeConcern. etc).
*
* @return {Promise}
*/
async listCollections(
database: string,
filter: Document = {},
options: Document = {},
dbOptions: Document = {}): Promise<Result> {
return await this.db(database, dbOptions).listCollections(
filter, options
).toArray();
}
/**
* Get all the collection statistics.
*
* @param {String} database - The db name.
* @param {String} collection - The collection name.
* @param {Object} options - The count options.
* @param {Object} dbOptions - The database options
* @return {Promise} returns Promise
*/
async stats(
database: string,
collection: string,
options: Document = {},
dbOptions: Document = {}): Promise<any> {
return await this.db(database, dbOptions)
.collection(collection)
.stats(options);
}
/**
* Reindex all indexes on the collection.
*
* @param {String} database - The db name.
* @param {String} collection - The collection name.
* @param {Object} options - The command options.
* @param {Object} dbOptions - The database options (i.e. readConcern, writeConcern. etc).
* @return {Promise}
*/
async reIndex(
database: string,
collection: string,
options?: Document,
dbOptions?: Document): Promise<Result> {
return await this.runCommand(database, {
reIndex: collection
}, options, dbOptions);
}
}
export default CliServiceProvider;

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc