Skip to content

Commit 1bee7ab

Browse files
emadumljhaywar
authored andcommitted
feat(NODE-3095): add timeseries options to db.createCollection (#2878)
1 parent 70ec50b commit 1bee7ab

File tree

6 files changed

+428
-3
lines changed

6 files changed

+428
-3
lines changed

src/index.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,10 @@ export type {
244244
export type { IndexInformationOptions } from './operations/common_functions';
245245
export type { CountOptions } from './operations/count';
246246
export type { CountDocumentsOptions } from './operations/count_documents';
247-
export type { CreateCollectionOptions } from './operations/create_collection';
247+
export type {
248+
CreateCollectionOptions,
249+
TimeSeriesCollectionOptions
250+
} from './operations/create_collection';
248251
export type { DeleteOptions, DeleteResult, DeleteStatement } from './operations/delete';
249252
export type { DistinctOptions } from './operations/distinct';
250253
export type { DropCollectionOptions, DropDatabaseOptions } from './operations/drop';

src/operations/create_collection.ts

+14
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,16 @@ const ILLEGAL_COMMAND_FIELDS = new Set([
3030
'ignoreUndefined'
3131
]);
3232

33+
/** @public
34+
* Configuration options for timeseries collections
35+
* @see https://docs.mongodb.com/manual/core/timeseries-collections/
36+
*/
37+
export interface TimeSeriesCollectionOptions extends Document {
38+
timeField: string;
39+
metaField?: string;
40+
granularity?: string;
41+
}
42+
3343
/** @public */
3444
export interface CreateCollectionOptions extends CommandOperationOptions {
3545
/** Returns an error if the collection does not exist */
@@ -60,6 +70,10 @@ export interface CreateCollectionOptions extends CommandOperationOptions {
6070
pipeline?: Document[];
6171
/** A primary key factory function for generation of custom _id keys. */
6272
pkFactory?: PkFactory;
73+
/** A document specifying configuration options for timeseries collections. */
74+
timeseries?: TimeSeriesCollectionOptions;
75+
/** The number of seconds after which a document in a timeseries collection expires. */
76+
expireAfterSeconds?: number;
6377
}
6478

6579
/** @internal */
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
'use strict';
2+
3+
const { expect } = require('chai');
4+
const { loadSpecTests } = require('../spec/index');
5+
const { runUnifiedTest } = require('./unified-spec-runner/runner');
6+
7+
describe('Collection management unified spec tests', function () {
8+
for (const collectionManagementTest of loadSpecTests('collection-management')) {
9+
expect(collectionManagementTest).to.exist;
10+
context(String(collectionManagementTest.description), function () {
11+
for (const test of collectionManagementTest.tests) {
12+
it(String(test.description), {
13+
metadata: { sessions: { skipLeakTests: true } },
14+
test: async function () {
15+
await runUnifiedTest(this, collectionManagementTest, test);
16+
}
17+
});
18+
}
19+
});
20+
}
21+
});

test/functional/unified-spec-runner/operations.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -212,8 +212,11 @@ operations.set('createChangeStream', async ({ entities, operation }) => {
212212

213213
operations.set('createCollection', async ({ entities, operation }) => {
214214
const db = entities.getEntity('db', operation.object);
215-
const session = entities.getEntity('session', operation.arguments.session, false);
216-
await db.createCollection(operation.arguments.collection, { session });
215+
const { session, collection, ...opts } = operation.arguments;
216+
await db.createCollection(collection, {
217+
session: entities.getEntity('session', session, false),
218+
...opts
219+
});
217220
});
218221

219222
operations.set('createIndex', async ({ entities, operation }) => {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,255 @@
1+
{
2+
"description": "timeseries-collection",
3+
"schemaVersion": "1.0",
4+
"runOnRequirements": [
5+
{
6+
"minServerVersion": "5.0"
7+
}
8+
],
9+
"createEntities": [
10+
{
11+
"client": {
12+
"id": "client0",
13+
"observeEvents": [
14+
"commandStartedEvent"
15+
]
16+
}
17+
},
18+
{
19+
"database": {
20+
"id": "database0",
21+
"client": "client0",
22+
"databaseName": "ts-tests"
23+
}
24+
},
25+
{
26+
"collection": {
27+
"id": "collection0",
28+
"database": "database0",
29+
"collectionName": "test"
30+
}
31+
}
32+
],
33+
"initialData": [
34+
{
35+
"collectionName": "test",
36+
"databaseName": "ts-tests",
37+
"documents": []
38+
}
39+
],
40+
"tests": [
41+
{
42+
"description": "createCollection with all options",
43+
"operations": [
44+
{
45+
"name": "dropCollection",
46+
"object": "database0",
47+
"arguments": {
48+
"collection": "test"
49+
}
50+
},
51+
{
52+
"name": "createCollection",
53+
"object": "database0",
54+
"arguments": {
55+
"collection": "test",
56+
"expireAfterSeconds": 604800,
57+
"timeseries": {
58+
"timeField": "time",
59+
"metaField": "meta",
60+
"granularity": "minutes"
61+
}
62+
}
63+
},
64+
{
65+
"name": "assertCollectionExists",
66+
"object": "testRunner",
67+
"arguments": {
68+
"databaseName": "ts-tests",
69+
"collectionName": "test"
70+
}
71+
}
72+
],
73+
"expectEvents": [
74+
{
75+
"client": "client0",
76+
"events": [
77+
{
78+
"commandStartedEvent": {
79+
"command": {
80+
"drop": "test"
81+
},
82+
"databaseName": "ts-tests"
83+
}
84+
},
85+
{
86+
"commandStartedEvent": {
87+
"command": {
88+
"create": "test",
89+
"expireAfterSeconds": 604800,
90+
"timeseries": {
91+
"timeField": "time",
92+
"metaField": "meta",
93+
"granularity": "minutes"
94+
}
95+
},
96+
"databaseName": "ts-tests"
97+
}
98+
}
99+
]
100+
}
101+
]
102+
},
103+
{
104+
"description": "insertMany with duplicate ids",
105+
"operations": [
106+
{
107+
"name": "dropCollection",
108+
"object": "database0",
109+
"arguments": {
110+
"collection": "test"
111+
}
112+
},
113+
{
114+
"name": "createCollection",
115+
"object": "database0",
116+
"arguments": {
117+
"collection": "test",
118+
"expireAfterSeconds": 604800,
119+
"timeseries": {
120+
"timeField": "time",
121+
"metaField": "meta",
122+
"granularity": "minutes"
123+
}
124+
}
125+
},
126+
{
127+
"name": "assertCollectionExists",
128+
"object": "testRunner",
129+
"arguments": {
130+
"databaseName": "ts-tests",
131+
"collectionName": "test"
132+
}
133+
},
134+
{
135+
"name": "insertMany",
136+
"object": "collection0",
137+
"arguments": {
138+
"documents": [
139+
{
140+
"_id": 1,
141+
"time": {
142+
"$date": {
143+
"$numberLong": "1552949630482"
144+
}
145+
}
146+
},
147+
{
148+
"_id": 1,
149+
"time": {
150+
"$date": {
151+
"$numberLong": "1552949630483"
152+
}
153+
}
154+
}
155+
]
156+
}
157+
},
158+
{
159+
"name": "find",
160+
"object": "collection0",
161+
"arguments": {
162+
"filter": {},
163+
"sort": {
164+
"time": 1
165+
}
166+
},
167+
"expectResult": [
168+
{
169+
"_id": 1,
170+
"time": {
171+
"$date": {
172+
"$numberLong": "1552949630482"
173+
}
174+
}
175+
},
176+
{
177+
"_id": 1,
178+
"time": {
179+
"$date": {
180+
"$numberLong": "1552949630483"
181+
}
182+
}
183+
}
184+
]
185+
}
186+
],
187+
"expectEvents": [
188+
{
189+
"client": "client0",
190+
"events": [
191+
{
192+
"commandStartedEvent": {
193+
"command": {
194+
"drop": "test"
195+
},
196+
"databaseName": "ts-tests"
197+
}
198+
},
199+
{
200+
"commandStartedEvent": {
201+
"command": {
202+
"create": "test",
203+
"expireAfterSeconds": 604800,
204+
"timeseries": {
205+
"timeField": "time",
206+
"metaField": "meta",
207+
"granularity": "minutes"
208+
}
209+
},
210+
"databaseName": "ts-tests"
211+
}
212+
},
213+
{
214+
"commandStartedEvent": {
215+
"command": {
216+
"insert": "test",
217+
"documents": [
218+
{
219+
"_id": 1,
220+
"time": {
221+
"$date": {
222+
"$numberLong": "1552949630482"
223+
}
224+
}
225+
},
226+
{
227+
"_id": 1,
228+
"time": {
229+
"$date": {
230+
"$numberLong": "1552949630483"
231+
}
232+
}
233+
}
234+
]
235+
}
236+
}
237+
},
238+
{
239+
"commandStartedEvent": {
240+
"command": {
241+
"find": "test",
242+
"filter": {},
243+
"sort": {
244+
"time": 1
245+
}
246+
},
247+
"databaseName": "ts-tests"
248+
}
249+
}
250+
]
251+
}
252+
]
253+
}
254+
]
255+
}

0 commit comments

Comments
 (0)