diff --git a/mongodb-legacy.d.ts b/mongodb-legacy.d.ts index cc214dd..4408d14 100644 --- a/mongodb-legacy.d.ts +++ b/mongodb-legacy.d.ts @@ -20,7 +20,6 @@ import { // Dependencies (options, etc.) import type { AbstractCursorEvents, - AddUserOptions, AggregateOptions, AnyBulkWriteOperation, BulkWriteOptions, @@ -31,8 +30,6 @@ import type { ClientSessionOptions, CollectionInfo, CollectionOptions, - CollStats, - CollStatsOptions, CommandOperationOptions, CountDocumentsOptions, CountOptions, @@ -93,10 +90,10 @@ import type { type NonConstructorKeys = { [P in keyof T]: T[P] extends new () => any ? never : P }[keyof T]; type NonConstructor = Pick>; -declare const Admin: new () => Omit; +declare const Admin: new () => Omit; declare const ChangeStream: new >() => Omit, 'close' | 'hasNext' | 'next' | 'tryNext'>; -declare const Collection: new () => Omit, 'initializeUnorderedBulkOp' | 'initializeOrderedBulkOp' | 'bulkWrite'| 'count'| 'countDocuments'| 'estimatedDocumentCount'| 'createIndex'| 'createIndexes'| 'dropIndex'| 'dropIndexes'| 'deleteMany'| 'deleteOne'| 'distinct'| 'drop'| 'findOne'| 'findOneAndDelete'| 'findOneAndReplace'| 'findOneAndUpdate'| 'indexExists'| 'indexInformation'| 'indexes'| 'insertMany'| 'insertOne'| 'isCapped'| 'options'| 'rename'| 'replaceOne'| 'stats'| 'updateMany'| 'updateOne'| 'aggregate'| 'find'| 'listIndexes'| 'watch'>; -declare const Db: new () => Omit; +declare const Collection: new () => Omit, 'initializeUnorderedBulkOp' | 'initializeOrderedBulkOp' | 'bulkWrite'| 'count'| 'countDocuments'| 'estimatedDocumentCount'| 'createIndex'| 'createIndexes'| 'dropIndex'| 'dropIndexes'| 'deleteMany'| 'deleteOne'| 'distinct'| 'drop'| 'findOne'| 'findOneAndDelete'| 'findOneAndReplace'| 'findOneAndUpdate'| 'indexExists'| 'indexInformation'| 'indexes'| 'insertMany'| 'insertOne'| 'isCapped'| 'options'| 'rename'| 'replaceOne'| 'updateMany'| 'updateOne'| 'aggregate'| 'find'| 'listIndexes'| 'watch'>; +declare const Db: new () => Omit; declare const GridFSBucket: new (db: LegacyDb, options: GridFSBucketOptions) => Omit, 'delete' | 'rename' | 'drop' | 'find'>; declare const MongoClient: new (url: string, options?: MongoClientOptions) => Omit, 'connect' | 'close' | 'db' | 'watch' | 'withSession' | 'startSession'>; declare const ClientSession: new () => Omit @@ -156,22 +153,6 @@ declare class LegacyAdmin extends Admin { ping(callback: Callback): void; ping(options: CommandOperationOptions): Promise; ping(options: CommandOperationOptions, callback: Callback): void; - /** - * Add a user to the database - * - * @param username - The username for the new user - * @param password - An optional password for the new user - * @param options - Optional settings for the command - * @param callback - An optional callback, a Promise will be returned if none is provided - */ - addUser(username: string): Promise; - addUser(username: string, callback: Callback): void; - addUser(username: string, password: string): Promise; - addUser(username: string, password: string, callback: Callback): void; - addUser(username: string, options: AddUserOptions): Promise; - addUser(username: string, options: AddUserOptions, callback: Callback): void; - addUser(username: string, password: string, options: AddUserOptions): Promise; - addUser(username: string, password: string, options: AddUserOptions, callback: Callback): void; /** * Remove a user from a database * @@ -637,16 +618,6 @@ declare class LegacyCollection extends Coll indexes(callback: Callback): void; indexes(options: IndexInformationOptions): Promise; indexes(options: IndexInformationOptions, callback: Callback): void; - /** - * Get all the collection statistics. - * - * @param options - Optional settings for the command - * @param callback - An optional callback, a Promise will be returned if none is provided - */ - stats(): Promise; - stats(callback: Callback): void; - stats(options: CollStatsOptions): Promise; - stats(options: CollStatsOptions, callback: Callback): void; /** * Find a document and delete it in one atomic operation. Requires a write lock for the duration of the operation. * @@ -868,22 +839,6 @@ declare class LegacyDb extends Db { createIndex(name: string, indexSpec: IndexSpecification, callback?: Callback): void; createIndex(name: string, indexSpec: IndexSpecification, options: CreateIndexesOptions): Promise; createIndex(name: string, indexSpec: IndexSpecification, options: CreateIndexesOptions, callback: Callback): void; - /** - * Add a user to the database - * - * @param username - The username for the new user - * @param password - An optional password for the new user - * @param options - Optional settings for the command - * @param callback - An optional callback, a Promise will be returned if none is provided - */ - addUser(username: string): Promise; - addUser(username: string, callback: Callback): void; - addUser(username: string, password: string): Promise; - addUser(username: string, password: string, callback: Callback): void; - addUser(username: string, options: AddUserOptions): Promise; - addUser(username: string, options: AddUserOptions, callback: Callback): void; - addUser(username: string, password: string, options: AddUserOptions): Promise; - addUser(username: string, password: string, options: AddUserOptions, callback: Callback): void; /** * Remove a user from a database * diff --git a/src/legacy_wrappers/admin.js b/src/legacy_wrappers/admin.js index 028261b..827689e 100644 --- a/src/legacy_wrappers/admin.js +++ b/src/legacy_wrappers/admin.js @@ -7,25 +7,6 @@ Object.defineProperty(module.exports, '__esModule', { value: true }); module.exports.makeLegacyAdmin = function (baseClass) { class LegacyAdmin extends baseClass { - addUser(username, password, options, callback) { - callback = - typeof callback === 'function' - ? callback - : typeof options === 'function' - ? options - : typeof password === 'function' - ? password - : undefined; - options = - options != null && typeof options === 'object' - ? options - : password != null && typeof password === 'object' - ? password - : undefined; - password = typeof password === 'string' ? password : undefined; - return maybeCallback(super.addUser(username, password, options), callback); - } - buildInfo(options, callback) { callback = typeof callback === 'function' diff --git a/src/legacy_wrappers/collection.js b/src/legacy_wrappers/collection.js index 87f4191..c1b7ff4 100644 --- a/src/legacy_wrappers/collection.js +++ b/src/legacy_wrappers/collection.js @@ -307,17 +307,6 @@ module.exports.makeLegacyCollection = function (baseClass) { return maybeCallback(super.replaceOne(filter, replacement, options), callback); } - stats(options, callback) { - callback = - typeof callback === 'function' - ? callback - : typeof options === 'function' - ? options - : undefined; - options = typeof options !== 'function' ? options : undefined; - return maybeCallback(super.stats(options), callback); - } - updateMany(filter, update, options, callback) { callback = typeof callback === 'function' diff --git a/src/legacy_wrappers/db.js b/src/legacy_wrappers/db.js index 1ab740f..0f0acba 100644 --- a/src/legacy_wrappers/db.js +++ b/src/legacy_wrappers/db.js @@ -18,26 +18,6 @@ module.exports.makeLegacyDb = function (baseClass) { return maybeCallback(super.command(command, options), callback); } - // Async APIs - addUser(username, password, options, callback) { - callback = - typeof callback === 'function' - ? callback - : typeof options === 'function' - ? options - : typeof password === 'function' - ? password - : undefined; - options = - options != null && typeof options === 'object' - ? options - : password != null && typeof password === 'object' - ? password - : undefined; - password = typeof password === 'string' ? password : undefined; - return maybeCallback(super.addUser(username, password, options), callback); - } - removeUser(username, options, callback) { callback = typeof callback === 'function' diff --git a/test/tools/api.js b/test/tools/api.js index 60215d1..477f2f8 100644 --- a/test/tools/api.js +++ b/test/tools/api.js @@ -28,7 +28,6 @@ const api = [ // Super class of cursors, we do not directly override these but override them in the inherited classes ...commonCursorApis.flatMap(({ method, returnType, possibleCallbackPositions }) => cursorClasses.map(cursorClass => ({ className: cursorClass, method, returnType, possibleCallbackPositions }))), - { className: 'Admin', method: 'addUser', returnType: 'Promise', special: 'addUser takes 4 arguments: [username, password, options, callback] the last 3 are optional, unlike other functions with this length password needs to be filtered to only be a string, this is the only case where we make a decision to pass an argument based on its type (instead of what type it is not)' }, { className: 'Admin', method: 'buildInfo', returnType: 'Promise' }, { className: 'Admin', method: 'command', returnType: 'Promise' }, { className: 'Admin', method: 'listDatabases', returnType: 'Promise' }, @@ -88,7 +87,6 @@ const api = [ { className: 'Collection', method: 'options', returnType: 'Promise' }, { className: 'Collection', method: 'rename', returnType: 'Promise', changesPromise: true }, { className: 'Collection', method: 'replaceOne', returnType: 'Promise' }, - { className: 'Collection', method: 'stats', returnType: 'Promise' }, { className: 'Collection', method: 'updateMany', returnType: 'Promise' }, { className: 'Collection', method: 'updateOne', returnType: 'Promise' }, { className: 'Collection', method: 'initializeOrderedBulkOp', returnType: 'OrderedBulkOperation', notAsync: true }, @@ -98,7 +96,6 @@ const api = [ { className: 'Collection', method: 'listIndexes', returnType: 'ListIndexesCursor', notAsync: true }, { className: 'Collection', method: 'watch', returnType: 'ChangeStream', notAsync: true }, - { className: 'Db', method: 'addUser', returnType: 'Promise', special: 'see Admin.addUser' }, { className: 'Db', method: 'collections', returnType: 'Promise', changesPromise: true }, { className: 'Db', method: 'command', returnType: 'Promise' }, { className: 'Db', method: 'createCollection', returnType: 'Promise>', changesPromise: true }, diff --git a/test/unit/legacy_wrappers/admin.test.js b/test/unit/legacy_wrappers/admin.test.js deleted file mode 100644 index 7f5b548..0000000 --- a/test/unit/legacy_wrappers/admin.test.js +++ /dev/null @@ -1,163 +0,0 @@ -'use strict'; - -const { MongoClient: LegacyMongoClient } = require('../../../src/index'); -const { Admin } = require('mongodb'); -const sinon = require('sinon'); -const { expect } = require('chai'); -const { runMicroTask } = require('../../tools/utils'); - -describe('legacy_wrappers/admin.js', () => { - let admin; - let client; - - afterEach(async function () { - sinon.restore(); - await client.close(); - }); - - describe('addUser()', () => { - let stubbedMethod; - let callback; - let superPromise; - let actualReturnValue; - - beforeEach(async () => { - client = new LegacyMongoClient('mongodb://iLoveJs'); - admin = client.db().admin(); - superPromise = Promise.resolve({ message: 'success!' }); - stubbedMethod = sinon.stub(Admin.prototype, 'addUser').returns(superPromise); - callback = sinon.stub(); - }); - - describe(`and addUser is called with ('name', callback)`, () => { - beforeEach(() => { - actualReturnValue = admin.addUser('name', callback); - }); - - it('should return void', () => expect(actualReturnValue).to.be.undefined); - - it('should call the callback with undefined error and successful result', async () => { - await runMicroTask(); - expect(callback).to.have.been.calledOnceWith(undefined, { message: 'success!' }); - }); - - it(`should pass only ('name') to the driver api`, () => { - expect(stubbedMethod).to.have.been.calledOnceWithExactly('name', undefined, undefined); - }); - }); - - describe(`and addUser is called with ('name', 'pass', callback)`, () => { - beforeEach(() => { - actualReturnValue = admin.addUser('name', 'pass', callback); - }); - - it('should return void', () => expect(actualReturnValue).to.be.undefined); - - it('should call the callback with undefined error and successful result', async () => { - await runMicroTask(); - expect(callback).to.have.been.calledOnceWith(undefined, { message: 'success!' }); - }); - - it(`should pass only ('name', 'pass') to the driver api`, () => { - expect(stubbedMethod).to.have.been.calledOnceWithExactly('name', 'pass', undefined); - }); - }); - - describe(`and addUser is called with ('name', 'pass', options, callback)`, () => { - beforeEach(() => { - actualReturnValue = admin.addUser('name', 'pass', { options: true }, callback); - }); - - it('should return void', () => expect(actualReturnValue).to.be.undefined); - - it('should call the callback with undefined error and successful result', async () => { - await runMicroTask(); - expect(callback).to.have.been.calledOnceWith(undefined, { message: 'success!' }); - }); - - it(`should pass only ('name', 'pass', options) to the driver api`, () => { - expect(stubbedMethod).to.have.been.calledOnceWithExactly('name', 'pass', { - options: true - }); - }); - }); - - describe(`and addUser is called with ('name', options, callback)`, () => { - beforeEach(() => { - actualReturnValue = admin.addUser('name', { options: true }, callback); - }); - - it('should return void', () => expect(actualReturnValue).to.be.undefined); - - it('should call the callback with undefined error and successful result', async () => { - await runMicroTask(); - expect(callback).to.have.been.calledOnceWith(undefined, { message: 'success!' }); - }); - - it(`should pass only ('name', undefined, options) to the driver api`, () => { - expect(stubbedMethod).to.have.been.calledOnceWithExactly('name', undefined, { - options: true - }); - }); - }); - - describe(`and addUser is called with ('name')`, () => { - beforeEach(() => { - actualReturnValue = admin.addUser('name'); - }); - - it('should return the same promise the driver returns', async () => { - expect(actualReturnValue).to.equal(superPromise); - }); - - it('should return a resolved promise', async () => { - const result = await actualReturnValue; - expect(result).to.have.property('message', 'success!'); - }); - - it(`should pass only ('name') to the driver api`, () => { - expect(stubbedMethod).to.have.been.calledOnceWithExactly('name', undefined, undefined); - }); - }); - - describe(`and addUser is called with ('name', 'pass')`, () => { - beforeEach(() => { - actualReturnValue = admin.addUser('name', 'pass'); - }); - - it('should return the same promise the driver returns', async () => { - expect(actualReturnValue).to.equal(superPromise); - }); - - it('should return a resolved promise', async () => { - const result = await actualReturnValue; - expect(result).to.have.property('message', 'success!'); - }); - - it(`should pass only ('name', 'pass') to the driver api`, () => { - expect(stubbedMethod).to.have.been.calledOnceWithExactly('name', 'pass', undefined); - }); - }); - - describe(`and addUser is called with ('name', 'pass', options)`, () => { - beforeEach(() => { - actualReturnValue = admin.addUser('name', 'pass', { options: true }); - }); - - it('should return the same promise the driver returns', async () => { - expect(actualReturnValue).to.equal(superPromise); - }); - - it('should return a resolved promise', async () => { - const result = await actualReturnValue; - expect(result).to.have.property('message', 'success!'); - }); - - it(`should pass only ('name', 'pass', options) to the driver api`, () => { - expect(stubbedMethod).to.have.been.calledOnceWithExactly('name', 'pass', { - options: true - }); - }); - }); - }); -}); diff --git a/test/unit/legacy_wrappers/db.test.js b/test/unit/legacy_wrappers/db.test.js index b8c9a97..6409c35 100644 --- a/test/unit/legacy_wrappers/db.test.js +++ b/test/unit/legacy_wrappers/db.test.js @@ -4,7 +4,8 @@ const { MongoClient: LegacyMongoClient, ListCollectionsCursor: LegacyListCollectionsCursor, ChangeStream: LegacyChangeStream, - AggregationCursor: LegacyAggregationCursor + AggregationCursor: LegacyAggregationCursor, + Admin: LegacyAdmin } = require('../../../src/index'); const { Db } = require('mongodb'); const sinon = require('sinon'); @@ -40,151 +41,8 @@ describe('legacy_wrappers/db.js', () => { expect(db.aggregate()).to.be.instanceOf(LegacyAggregationCursor); }); - describe('addUser()', () => { - let db; - let stubbedMethod; - let callback; - let superPromise; - let actualReturnValue; - - beforeEach(async () => { - client = new LegacyMongoClient('mongodb://iLoveJs'); - db = client.db(); - superPromise = Promise.resolve({ message: 'success!' }); - stubbedMethod = sinon.stub(Db.prototype, 'addUser').returns(superPromise); - callback = sinon.stub(); - }); - - describe(`and addUser is called with ('name', callback)`, () => { - beforeEach(() => { - actualReturnValue = db.addUser('name', callback); - }); - - it('should return void', () => expect(actualReturnValue).to.be.undefined); - - it('should call the callback with undefined error and successful result', async () => { - await runMicroTask(); - expect(callback).to.have.been.calledOnceWith(undefined, { message: 'success!' }); - }); - - it(`should pass only ('name') to the driver api`, () => { - expect(stubbedMethod).to.have.been.calledOnceWithExactly('name', undefined, undefined); - }); - }); - - describe(`and addUser is called with ('name', 'pass', callback)`, () => { - beforeEach(() => { - actualReturnValue = db.addUser('name', 'pass', callback); - }); - - it('should return void', () => expect(actualReturnValue).to.be.undefined); - - it('should call the callback with undefined error and successful result', async () => { - await runMicroTask(); - expect(callback).to.have.been.calledOnceWith(undefined, { message: 'success!' }); - }); - - it(`should pass only ('name', 'pass') to the driver api`, () => { - expect(stubbedMethod).to.have.been.calledOnceWithExactly('name', 'pass', undefined); - }); - }); - - describe(`and addUser is called with ('name', 'pass', options, callback)`, () => { - beforeEach(() => { - actualReturnValue = db.addUser('name', 'pass', { options: true }, callback); - }); - - it('should return void', () => expect(actualReturnValue).to.be.undefined); - - it('should call the callback with undefined error and successful result', async () => { - await runMicroTask(); - expect(callback).to.have.been.calledOnceWith(undefined, { message: 'success!' }); - }); - - it(`should pass only ('name', 'pass', options) to the driver api`, () => { - expect(stubbedMethod).to.have.been.calledOnceWithExactly('name', 'pass', { - options: true - }); - }); - }); - - describe(`and addUser is called with ('name', options, callback)`, () => { - beforeEach(() => { - actualReturnValue = db.addUser('name', { options: true }, callback); - }); - - it('should return void', () => expect(actualReturnValue).to.be.undefined); - - it('should call the callback with undefined error and successful result', async () => { - await runMicroTask(); - expect(callback).to.have.been.calledOnceWith(undefined, { message: 'success!' }); - }); - - it(`should pass only ('name', undefined, options) to the driver api`, () => { - expect(stubbedMethod).to.have.been.calledOnceWithExactly('name', undefined, { - options: true - }); - }); - }); - - describe(`and addUser is called with ('name')`, () => { - beforeEach(() => { - actualReturnValue = db.addUser('name'); - }); - - it('should return the same promise the driver returns', async () => { - expect(actualReturnValue).to.equal(superPromise); - }); - - it('should return a resolved promise', async () => { - const result = await actualReturnValue; - expect(result).to.have.property('message', 'success!'); - }); - - it(`should pass only ('name') to the driver api`, () => { - expect(stubbedMethod).to.have.been.calledOnceWithExactly('name', undefined, undefined); - }); - }); - - describe(`and addUser is called with ('name', 'pass')`, () => { - beforeEach(() => { - actualReturnValue = db.addUser('name', 'pass'); - }); - - it('should return the same promise the driver returns', async () => { - expect(actualReturnValue).to.equal(superPromise); - }); - - it('should return a resolved promise', async () => { - const result = await actualReturnValue; - expect(result).to.have.property('message', 'success!'); - }); - - it(`should pass only ('name', 'pass') to the driver api`, () => { - expect(stubbedMethod).to.have.been.calledOnceWithExactly('name', 'pass', undefined); - }); - }); - - describe(`and addUser is called with ('name', 'pass', options)`, () => { - beforeEach(() => { - actualReturnValue = db.addUser('name', 'pass', { options: true }); - }); - - it('should return the same promise the driver returns', async () => { - expect(actualReturnValue).to.equal(superPromise); - }); - - it('should return a resolved promise', async () => { - const result = await actualReturnValue; - expect(result).to.have.property('message', 'success!'); - }); - - it(`should pass only ('name', 'pass', options) to the driver api`, () => { - expect(stubbedMethod).to.have.been.calledOnceWithExactly('name', 'pass', { - options: true - }); - }); - }); + it('should return legacy Admin', () => { + expect(db.admin()).to.be.instanceOf(LegacyAdmin); }); describe('renameCollection()', () => {