@@ -6,9 +6,10 @@ import * as express from 'express';
66import * as _ from 'lodash' ;
77import { CollectionOptions , MongoClient , ReadPreference } from 'mongodb' ;
88import { DokiConfiguration , Mongodoki } from 'mongodoki' ;
9+ import { OpenAPIV3 } from 'openapi-police' ;
910import * as supertest from 'supertest' ;
1011import { API } from '../../dist/api' ;
11- import { MongoJob , MongoOperation , MongoResource , PatchMongoOperation , QueryMongoOperation } from '../../dist/mongo/index' ;
12+ import { CreateMongoOperation , MongoJob , MongoOperation , MongoResource , PatchMongoOperation , QueryMongoOperation } from '../../dist/mongo/index' ;
1213import { APIRequest , APIResponse , Method } from '../../dist/types' ;
1314
1415const should = chai . should ( ) ;
@@ -195,7 +196,21 @@ describe('mongo', function () {
195196 }
196197 }
197198
198- let db , id , coll , coll2 , r1 , r2 , r3 , r4 , r5 , r6 , r7 , server ;
199+ class DateOp extends CreateMongoOperation {
200+ get requestSchema ( ) : OpenAPIV3 . SchemaObject {
201+ return {
202+ type : 'object' ,
203+ properties : {
204+ ts : {
205+ type : 'string' ,
206+ format : 'date-time' ,
207+ } ,
208+ } ,
209+ } ;
210+ }
211+ }
212+
213+ let db , id , coll , coll2 , r1 , r2 , r3 , r4 , r5 , r6 , r7 , r8 , server ;
199214
200215 before ( async function ( ) {
201216 db = await MongoClient . connect ( 'mongodb://localhost:27017/local' ) . then ( ( c ) => c . db ( ) ) ;
@@ -206,13 +221,15 @@ describe('mongo', function () {
206221 r5 = new MongoResource ( db , { name : 'Aggregation' , collection : collectionName } , { '/' : { get : Aggregation } } ) ;
207222 r6 = new MongoResource ( db , { name : 'Escape' , collection : collectionName , id : 'myid' , escapeProperties : true } ) ;
208223 r7 = new MongoResource ( db , { name : 'SecondaryAggregation' , collection : collectionName } , { '/' : { get : AggregationFromSecondary } } ) ;
224+ r8 = new MongoResource ( db , { name : 'Date' , collection : collectionName } , { '/' : { post : DateOp } } ) ;
209225 api . addResource ( r1 ) ;
210226 api . addResource ( r2 ) ;
211227 api . addResource ( r3 ) ;
212228 api . addResource ( r4 ) ;
213229 api . addResource ( r5 ) ;
214230 api . addResource ( r6 ) ;
215231 api . addResource ( r7 ) ;
232+ api . addResource ( r8 ) ;
216233
217234 let router = await api . router ( ) ;
218235 app . use ( router ) ;
@@ -331,6 +348,20 @@ describe('mongo', function () {
331348 data . should . deep . equal ( { myid : 'bbb' , 'a.b.c.d' : 1 } ) ;
332349 } ) ;
333350 } ) ;
351+
352+ it ( 'should create a record with a date preserving its type' , function ( ) {
353+ debugger ;
354+ return request
355+ . post ( '/dates' )
356+ . send ( { myid : 'ts' , ts : new Date ( ) } )
357+ . expect ( 201 )
358+ . expect ( 'Content-Type' , / j s o n / )
359+ . then ( async ( { body : data } ) => {
360+ const ref = await coll . findOne ( { } , { sort : { _id : - 1 } } ) ;
361+ await coll . remove ( { myid : 'ts' } ) ;
362+ ref . ts . should . be . a ( 'Date' ) ;
363+ } ) ;
364+ } ) ;
334365 } ) ;
335366
336367 describe ( 'collection' , function ( ) {
0 commit comments