Skip to content

feat(shell-api): search index management helpers MONGOSH-1455 MONGOSH-1456 #1477

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jun 6, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14,700 changes: 3,319 additions & 11,381 deletions package-lock.json

Large diffs are not rendered by default.

7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@
"karma-typescript": "^5.5.1",
"lerna": "^4.0.0",
"mocha": "^10.2.0",
"mongodb": "^5.5.0",
"mongodb": "lerouxb/node-mongodb-native#search-indexes-hack",
"mongodb-download-url": "^1.3.0",
"mongodb-js-precommit": "^2.0.0",
"nock": "^13.0.11",
Expand Down Expand Up @@ -164,5 +164,8 @@
"workspaces": [
"packages/*",
"scripts/docker"
]
],
"overrides": {
"mongodb": "lerouxb/node-mongodb-native#search-indexes-hack"
}
}
2 changes: 1 addition & 1 deletion packages/arg-parser/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,6 @@
},
"devDependencies": {
"@mongodb-js/devtools-connect": "^2.1.0",
"mongodb": "^5.5.0"
"mongodb": "lerouxb/node-mongodb-native#search-indexes-hack"
}
}
25 changes: 25 additions & 0 deletions packages/i18n/src/locales/en_US.ts
Original file line number Diff line number Diff line change
Expand Up @@ -698,6 +698,31 @@ const translations: Catalog = {
link: 'https://docs.mongodb.com/manual/reference/method/db.collection.configureQueryAnalyzer',
description: 'Starts or stops collecting metrics about reads and writes against an unsharded or sharded collection.',
example: 'db.coll.configureQueryAnalyzer(options)'
},
getSearchIndexes: {
link: 'https://docs.mongodb.com/manual/reference/method/db.collection.getSearchIndexes',
description: 'Returns an array that holds a list of documents that identify and describe the existing search indexes on the collection.',
example: 'db.coll.getSearchIndexes()'
},
createSearchIndexes: {
link: 'https://docs.mongodb.com/manual/reference/method/db.collection.createSearchIndexes',
description: 'Creates one or more search indexes on a collection',
example: 'db.coll.createSearchIndexes(descriptions)'
},
createSearchIndex: {
link: 'https://docs.mongodb.com/manual/reference/method/db.collection.createSearchIndex',
description: 'Creates one search indexes on a collection',
example: 'db.coll.createSearchIndex(description)'
},
dropSearchIndex: {
link: 'https://docs.mongodb.com/manual/reference/method/db.collection.dropSearchIndex',
description: 'Drops or removes the specified search index from a collection.',
example: 'db.coll.dropSearchIndex(name)'
},
updateSearchIndex: {
link: 'https://docs.mongodb.com/manual/reference/method/db.collection.updateSearchIndex',
description: 'Updates the sepecified search index.',
example: 'db.coll.updateSearchIndex(name, definition)'
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/logging/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"mongodb-redact": "^0.2.2"
},
"devDependencies": {
"mongodb": "^5.5.0"
"mongodb": "lerouxb/node-mongodb-native#search-indexes-hack"
},
"scripts": {
"test": "mocha -r \"../../scripts/import-expansions.js\" --timeout 15000 -r ts-node/register \"./src/**/*.spec.ts\"",
Expand Down
2 changes: 1 addition & 1 deletion packages/service-provider-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"@aws-sdk/credential-providers": "^3.262.0",
"@mongosh/errors": "0.0.0-dev.0",
"bson": "^5.2.0",
"mongodb": "^5.5.0",
"mongodb": "lerouxb/node-mongodb-native#search-indexes-hack",
"mongodb-build-info": "^1.5.0"
},
"optionalDependencies": {
Expand Down
5 changes: 4 additions & 1 deletion packages/service-provider-core/src/all-transport-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,5 +74,8 @@ export type {
AutoEncryptionOptions,
ServerApi,
ServerApiVersion,
MongoClient // mostly for testing
MongoClient, // mostly for testing
SearchIndexDescription,
ListSearchIndexesCursor,
ListSearchIndexesOptions
} from 'mongodb';
21 changes: 20 additions & 1 deletion packages/service-provider-core/src/readable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ import type {
FindCursor,
DbOptions,
ReadPreferenceFromOptions,
ReadPreferenceLike
ReadPreferenceLike,
ListSearchIndexesCursor,
ListSearchIndexesOptions
} from './all-transport-types';
import { ChangeStream, ChangeStreamOptions } from './all-transport-types';

Expand Down Expand Up @@ -206,5 +208,22 @@ export default interface Readable {
db?: string,
coll?: string
): ChangeStream<Document>;

/**
* Returns a promise of a cursor for the documents that identify
* and describe the existing search indexes on the collection.
*
* @param {String} database - The db name.
* @param {String} collection - The collection name.
* @param {Document} options - The command options.
* @param {DbOptions} dbOptions - The database options
*
* @return {Promise}
*/
listSearchIndexes(
database: string,
collection: string,
options: ListSearchIndexesOptions,
dbOptions?: DbOptions): ListSearchIndexesCursor;
}

50 changes: 49 additions & 1 deletion packages/service-provider-core/src/writable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ import type {
RunCommandOptions,
DbOptions,
OrderedBulkOperation,
UnorderedBulkOperation
UnorderedBulkOperation,
SearchIndexDescription
} from './all-transport-types';

/**
Expand Down Expand Up @@ -345,5 +346,52 @@ export default interface Writable {
options?: BulkWriteOptions,
dbOptions?: DbOptions
): Promise<OrderedBulkOperation | UnorderedBulkOperation>;

/**
* Adds new search indexes to a collection.
*
* @param {String} database - The db name.
* @param {String} collection - The collection name.
* @param {Object[]} descriptions - The specs of the indexes to be created.
* @param {DbOptions} dbOptions - The database options
*/
createSearchIndexes(
database: string,
collection: string,
descriptions: SearchIndexDescription[],
dbOptions?: DbOptions
): Promise<string[]>

/**
* Drops a search index.
*
* @param {String} database - The db name.
* @param {String} collection - The collection name.
* @param {String} indexName - The index name
* @param {DbOptions} dbOptions - The database options
*/
dropSearchIndex(
database: string,
collection: string,
index: string,
dbOptions?: DbOptions
): Promise<void>

/**
* Update a search index.
*
* @param {String} database - The db name.
* @param {String} collection - The collection name.
* @param {String} indexName - The index name.
* @param {Object} description - The update.
* @param {DbOptions} dbOptions - The database options
*/
updateSearchIndex(
database: string,
collection: string,
index: string,
description: SearchIndexDescription,
dbOptions?: DbOptions
): Promise<void>
}

2 changes: 1 addition & 1 deletion packages/service-provider-server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
"@mongosh/types": "0.0.0-dev.0",
"@types/sinon-chai": "^3.2.3",
"aws4": "^1.11.0",
"mongodb": "^5.5.0",
"mongodb": "lerouxb/node-mongodb-native#search-indexes-hack",
"mongodb-connection-string-url": "^2.6.0",
"saslprep": "mongodb-js/saslprep#v1.0.4"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -577,6 +577,8 @@ describe('CliServiceProvider [integration]', function() {
});
});

// TODO(MONGOSH-1465): integration tests for search indexes

describe('#listCollections', () => {
it('returns the list of collections', async() => {
await db.createCollection('coll1');
Expand Down
109 changes: 109 additions & 0 deletions packages/service-provider-server/src/cli-service-provider.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -861,4 +861,113 @@ describe('CliServiceProvider', () => {
).parentState).to.equal(undefined);
});
});

describe('#getSearchIndexes', () => {
let descriptions;
let nativeMethodResult;
let listSearchIndexesOptions;

beforeEach(() => {
descriptions = [
{ name: 'foo', description: {} },
{ name: 'bar', description: {} }
];

nativeMethodResult = {
toArray: () => {
return Promise.resolve(descriptions);
}
};

listSearchIndexesOptions = { allowDiskUse: true };

collectionStub = stubInterface<Collection>();
collectionStub.listSearchIndexes.returns(nativeMethodResult);
serviceProvider = new CliServiceProvider(createClientStub(collectionStub), bus, dummyOptions);
});

it('calls listSearchIndexes', async() => {
const cursor = serviceProvider.listSearchIndexes(
'db1',
'coll1',
listSearchIndexesOptions);
expect(await cursor.toArray()).to.deep.equal(descriptions);
expect(collectionStub.listSearchIndexes).to.have.been.calledWith(listSearchIndexesOptions);
});
});

describe('#createSearchIndexes', () => {
let descriptions;
let nativeMethodResult;

beforeEach(() => {
descriptions = [
{ name: 'foo', description: {} },
{ name: 'bar', description: {} }
];

nativeMethodResult = [
'index_1',
];

collectionStub = stubInterface<Collection>();
collectionStub.createSearchIndexes.resolves(nativeMethodResult);
serviceProvider = new CliServiceProvider(createClientStub(collectionStub), bus, dummyOptions);
});

it('executes the command against the database', async() => {
const result = await serviceProvider.createSearchIndexes(
'db1',
'coll1',
descriptions);
expect(result).to.deep.equal(nativeMethodResult);
expect(collectionStub.createSearchIndexes).to.have.been.calledWith(descriptions);
});
});

describe('#dropSearchIndex', () => {
let indexName;

beforeEach(() => {
indexName = 'foo';

collectionStub = stubInterface<Collection>();
collectionStub.dropSearchIndex.resolves();
serviceProvider = new CliServiceProvider(createClientStub(collectionStub), bus, dummyOptions);
});

it('executes the command against the database', async() => {
const result = await serviceProvider.dropSearchIndex(
'db1',
'coll1',
indexName);
expect(result).to.deep.equal(undefined);
expect(collectionStub.dropSearchIndex).to.have.been.calledWith(indexName);
});
});


describe('#updateSearchIndex', () => {
let indexName;
let description;

beforeEach(() => {
indexName = 'foo';
description = { x: 1, y: 2 };

collectionStub = stubInterface<Collection>();
collectionStub.updateSearchIndex.resolves();
serviceProvider = new CliServiceProvider(createClientStub(collectionStub), bus, dummyOptions);
});

it('executes the command against the database', async() => {
const result = await serviceProvider.updateSearchIndex(
'db1',
'coll1',
indexName,
description);
expect(result).to.deep.equal(undefined);
expect(collectionStub.updateSearchIndex).to.have.been.calledWith(indexName, description);
});
});
});
46 changes: 45 additions & 1 deletion packages/service-provider-server/src/cli-service-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,10 @@ import {
ChangeStream,
FLE,
AutoEncryptionOptions,
ClientEncryption as MongoCryptClientEncryption
ClientEncryption as MongoCryptClientEncryption,
SearchIndexDescription,
ListSearchIndexesCursor,
ListSearchIndexesOptions
} from '@mongosh/service-provider-core';

import { connectMongoClient, DevtoolsConnectOptions } from '@mongodb-js/devtools-connect';
Expand Down Expand Up @@ -1206,6 +1209,47 @@ class CliServiceProvider extends ServiceProviderCore implements ServiceProvider
...opts
});
}

listSearchIndexes(
database: string,
collection: string,
options: ListSearchIndexesOptions,
dbOptions?: DbOptions): ListSearchIndexesCursor {
return this.db(database, dbOptions)
.collection(collection)
.listSearchIndexes(options);
}

createSearchIndexes(
database: string,
collection: string,
descriptions: SearchIndexDescription[],
dbOptions?: DbOptions): Promise<string[]> {
return this.db(database, dbOptions)
.collection(collection)
.createSearchIndexes(descriptions);
}

dropSearchIndex(
database: string,
collection: string,
indexName: string,
dbOptions?: DbOptions): Promise<void> {
return this.db(database, dbOptions)
.collection(collection)
.dropSearchIndex(indexName);
}

updateSearchIndex(
database: string,
collection: string,
indexName: string,
description: SearchIndexDescription,
dbOptions?: DbOptions): Promise<void> {
return this.db(database, dbOptions)
.collection(collection)
.updateSearchIndex(indexName, description);
}
}

export default CliServiceProvider;
Expand Down
Loading