@@ -5,6 +5,7 @@ import { Prop } from './prop.decorator';
55import { Schema } from './schema.decorator' ;
66import { ModelFactory } from './factories/model.factory' ;
77import { isMongoRunning , startMongo , stopMongo , whichMongo } from '@/utils' ;
8+ import { create } from 'domain' ;
89
910@Schema ( {
1011 timestamps : true ,
@@ -14,6 +15,10 @@ import { isMongoRunning, startMongo, stopMongo, whichMongo } from '@/utils';
1415 options : { deletedBy : true } ,
1516 } ,
1617 ] ,
18+ index : {
19+ fields : { name : 1 , email : 1 } ,
20+ options : { unique : true , partialFilterExpression : { name : true } } ,
21+ } ,
1722} )
1823export class Pattern {
1924 @Prop ( )
@@ -24,16 +29,19 @@ export class Pattern {
2429
2530 @Prop ( { type : String , required : true } )
2631 public description : string ;
32+
33+ @Prop ( { unique : true , default : false } )
34+ public default : boolean ;
2735}
2836
2937const PatternModel = ModelFactory . createForClass ( Pattern ) ;
3038
3139const STOP_AT_END = true ;
32- const CLEAR_AT_END = false ;
40+ const CLEAR_AT_END = true ;
3341
34- const HOST = 'localhost ' ;
42+ const HOST = '127.0.0.1 ' ;
3543const PORT = 33333 ;
36- const DB_NAME = 'decorators ' ;
44+ const DB_NAME = 'assemblerjs-mongo-test ' ;
3745const DB = `${ process . cwd ( ) } /test-db/${ DB_NAME } /data` ;
3846const LOGS = `${ process . cwd ( ) } /test-db/${ DB_NAME } /logs/mongo/mongo.log` ;
3947
@@ -43,9 +51,21 @@ describe('Schema', () => {
4351 if ( ( await whichMongo ( ) ) === null )
4452 throw new Error ( `Mongo is not installed.` ) ;
4553
46- const runningOrError = await isMongoRunning ( ) ;
54+ let runningOrError = await isMongoRunning ( ) ;
4755 if ( runningOrError instanceof Error ) throw runningOrError ;
4856
57+ if ( runningOrError ) {
58+ // If Mongo is running, we stop it.
59+ const stoppedOrError : boolean | Error = await stopMongo ( ) ;
60+ if ( stoppedOrError instanceof Error ) {
61+ throw stoppedOrError ;
62+ }
63+ expect ( stoppedOrError ) . toBeTruthy ( ) ;
64+ await new Promise ( ( resolve ) => setTimeout ( resolve , 2000 ) ) ;
65+ expect ( await isMongoRunning ( ) ) . toBeFalsy ( ) ;
66+ runningOrError = false ;
67+ }
68+
4969 if ( ! runningOrError ) {
5070 const runOrError : string | Error = await startMongo ( {
5171 bindIp : [ HOST ] ,
@@ -59,15 +79,13 @@ describe('Schema', () => {
5979 throw runOrError ;
6080 }
6181 }
62-
63- await mongoose . connect ( `mongodb://${ HOST } :${ PORT } ` , {
64- dbName : DB_NAME ,
65- } ) ;
82+ await mongoose . connect ( `mongodb://${ HOST } :${ PORT } /${ DB_NAME } ` ) ;
6683
6784 const created = await PatternModel . create ( {
6885 description : 'An user.' ,
6986 name : 'John Doe' ,
7087 email : 'john@doe.com' ,
88+ default : true ,
7189 } ) ;
7290 expect ( created ) . toBeDefined ( ) ;
7391
0 commit comments