Skip to content

Commit

Permalink
feat(NODE-3095): add timeseries options to db.createCollection (#2878)
Browse files Browse the repository at this point in the history
  • Loading branch information
emadum committed Jul 8, 2021
1 parent 063ea23 commit c145c91
Show file tree
Hide file tree
Showing 6 changed files with 428 additions and 3 deletions.
5 changes: 4 additions & 1 deletion src/index.ts
Expand Up @@ -244,7 +244,10 @@ export type {
export type { IndexInformationOptions } from './operations/common_functions';
export type { CountOptions } from './operations/count';
export type { CountDocumentsOptions } from './operations/count_documents';
export type { CreateCollectionOptions } from './operations/create_collection';
export type {
CreateCollectionOptions,
TimeSeriesCollectionOptions
} from './operations/create_collection';
export type { DeleteOptions, DeleteResult, DeleteStatement } from './operations/delete';
export type { DistinctOptions } from './operations/distinct';
export type { DropCollectionOptions, DropDatabaseOptions } from './operations/drop';
Expand Down
14 changes: 14 additions & 0 deletions src/operations/create_collection.ts
Expand Up @@ -30,6 +30,16 @@ const ILLEGAL_COMMAND_FIELDS = new Set([
'ignoreUndefined'
]);

/** @public
* Configuration options for timeseries collections
* @see https://docs.mongodb.com/manual/core/timeseries-collections/
*/
export interface TimeSeriesCollectionOptions extends Document {
timeField: string;
metaField?: string;
granularity?: string;
}

/** @public */
export interface CreateCollectionOptions extends CommandOperationOptions {
/** Returns an error if the collection does not exist */
Expand Down Expand Up @@ -60,6 +70,10 @@ export interface CreateCollectionOptions extends CommandOperationOptions {
pipeline?: Document[];
/** A primary key factory function for generation of custom _id keys. */
pkFactory?: PkFactory;
/** A document specifying configuration options for timeseries collections. */
timeseries?: TimeSeriesCollectionOptions;
/** The number of seconds after which a document in a timeseries collection expires. */
expireAfterSeconds?: number;
}

/** @internal */
Expand Down
21 changes: 21 additions & 0 deletions test/functional/collection_management_spec.test.js
@@ -0,0 +1,21 @@
'use strict';

const { expect } = require('chai');
const { loadSpecTests } = require('../spec/index');
const { runUnifiedTest } = require('./unified-spec-runner/runner');

describe('Collection management unified spec tests', function () {
for (const collectionManagementTest of loadSpecTests('collection-management')) {
expect(collectionManagementTest).to.exist;
context(String(collectionManagementTest.description), function () {
for (const test of collectionManagementTest.tests) {
it(String(test.description), {
metadata: { sessions: { skipLeakTests: true } },
test: async function () {
await runUnifiedTest(this, collectionManagementTest, test);
}
});
}
});
}
});
7 changes: 5 additions & 2 deletions test/functional/unified-spec-runner/operations.ts
Expand Up @@ -212,8 +212,11 @@ operations.set('createChangeStream', async ({ entities, operation }) => {

operations.set('createCollection', async ({ entities, operation }) => {
const db = entities.getEntity('db', operation.object);
const session = entities.getEntity('session', operation.arguments.session, false);
await db.createCollection(operation.arguments.collection, { session });
const { session, collection, ...opts } = operation.arguments;
await db.createCollection(collection, {
session: entities.getEntity('session', session, false),
...opts
});
});

operations.set('createIndex', async ({ entities, operation }) => {
Expand Down
255 changes: 255 additions & 0 deletions test/spec/collection-management/timeseries-collection.json
@@ -0,0 +1,255 @@
{
"description": "timeseries-collection",
"schemaVersion": "1.0",
"runOnRequirements": [
{
"minServerVersion": "5.0"
}
],
"createEntities": [
{
"client": {
"id": "client0",
"observeEvents": [
"commandStartedEvent"
]
}
},
{
"database": {
"id": "database0",
"client": "client0",
"databaseName": "ts-tests"
}
},
{
"collection": {
"id": "collection0",
"database": "database0",
"collectionName": "test"
}
}
],
"initialData": [
{
"collectionName": "test",
"databaseName": "ts-tests",
"documents": []
}
],
"tests": [
{
"description": "createCollection with all options",
"operations": [
{
"name": "dropCollection",
"object": "database0",
"arguments": {
"collection": "test"
}
},
{
"name": "createCollection",
"object": "database0",
"arguments": {
"collection": "test",
"expireAfterSeconds": 604800,
"timeseries": {
"timeField": "time",
"metaField": "meta",
"granularity": "minutes"
}
}
},
{
"name": "assertCollectionExists",
"object": "testRunner",
"arguments": {
"databaseName": "ts-tests",
"collectionName": "test"
}
}
],
"expectEvents": [
{
"client": "client0",
"events": [
{
"commandStartedEvent": {
"command": {
"drop": "test"
},
"databaseName": "ts-tests"
}
},
{
"commandStartedEvent": {
"command": {
"create": "test",
"expireAfterSeconds": 604800,
"timeseries": {
"timeField": "time",
"metaField": "meta",
"granularity": "minutes"
}
},
"databaseName": "ts-tests"
}
}
]
}
]
},
{
"description": "insertMany with duplicate ids",
"operations": [
{
"name": "dropCollection",
"object": "database0",
"arguments": {
"collection": "test"
}
},
{
"name": "createCollection",
"object": "database0",
"arguments": {
"collection": "test",
"expireAfterSeconds": 604800,
"timeseries": {
"timeField": "time",
"metaField": "meta",
"granularity": "minutes"
}
}
},
{
"name": "assertCollectionExists",
"object": "testRunner",
"arguments": {
"databaseName": "ts-tests",
"collectionName": "test"
}
},
{
"name": "insertMany",
"object": "collection0",
"arguments": {
"documents": [
{
"_id": 1,
"time": {
"$date": {
"$numberLong": "1552949630482"
}
}
},
{
"_id": 1,
"time": {
"$date": {
"$numberLong": "1552949630483"
}
}
}
]
}
},
{
"name": "find",
"object": "collection0",
"arguments": {
"filter": {},
"sort": {
"time": 1
}
},
"expectResult": [
{
"_id": 1,
"time": {
"$date": {
"$numberLong": "1552949630482"
}
}
},
{
"_id": 1,
"time": {
"$date": {
"$numberLong": "1552949630483"
}
}
}
]
}
],
"expectEvents": [
{
"client": "client0",
"events": [
{
"commandStartedEvent": {
"command": {
"drop": "test"
},
"databaseName": "ts-tests"
}
},
{
"commandStartedEvent": {
"command": {
"create": "test",
"expireAfterSeconds": 604800,
"timeseries": {
"timeField": "time",
"metaField": "meta",
"granularity": "minutes"
}
},
"databaseName": "ts-tests"
}
},
{
"commandStartedEvent": {
"command": {
"insert": "test",
"documents": [
{
"_id": 1,
"time": {
"$date": {
"$numberLong": "1552949630482"
}
}
},
{
"_id": 1,
"time": {
"$date": {
"$numberLong": "1552949630483"
}
}
}
]
}
}
},
{
"commandStartedEvent": {
"command": {
"find": "test",
"filter": {},
"sort": {
"time": 1
}
},
"databaseName": "ts-tests"
}
}
]
}
]
}
]
}

0 comments on commit c145c91

Please sign in to comment.