Skip to content

Commit 3eac85d

Browse files
committed
Added ability to define properties of 'index' function in Schema decorator.
1 parent 199d59d commit 3eac85d

6 files changed

Lines changed: 48 additions & 11 deletions

File tree

packages/mongo/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@assemblerjs/mongo",
3-
"version": "0.9.1",
3+
"version": "0.9.2",
44
"main": "./dist/index.js",
55
"module": "./dist/index.mjs",
66
"types": "./dist/index.d.ts",

packages/mongo/src/decorators/decorators.spec.ts

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { Prop } from './prop.decorator';
55
import { Schema } from './schema.decorator';
66
import { ModelFactory } from './factories/model.factory';
77
import { 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
})
1823
export 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

2937
const PatternModel = ModelFactory.createForClass(Pattern);
3038

3139
const 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';
3543
const PORT = 33333;
36-
const DB_NAME = 'decorators';
44+
const DB_NAME = 'assemblerjs-mongo-test';
3745
const DB = `${process.cwd()}/test-db/${DB_NAME}/data`;
3846
const 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

packages/mongo/src/decorators/factories/model.factory.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,14 @@ export class ModelFactory {
1717
target: Function
1818
): mongoose.Model<TClass> {
1919
const schemaDefinition = DefinitionsFactory.createForClass(target);
20+
2021
const schemaMetadata =
2122
TypeMetadataStorage.getSchemaMetadataByTarget(target);
2223
const schemaOpts = schemaMetadata?.options;
2324

25+
const index = schemaOpts?.index;
26+
delete schemaOpts?.index;
27+
2428
const schema = new mongoose.Schema<TClass>(
2529
schemaDefinition as mongoose.SchemaDefinition<
2630
mongoose.SchemaDefinitionType<TClass>
@@ -35,6 +39,11 @@ export class ModelFactory {
3539
});
3640
}
3741

42+
if (index) {
43+
// Add index to the schema.
44+
schema.index(index.fields, index.options);
45+
}
46+
3847
return mongoose.model<TClass>(schemaOpts?.name || target.name, schema);
3948
}
4049
}

packages/mongo/src/decorators/factories/schema.factory.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { forOf } from '@assemblerjs/core';
44
import { TypeMetadataStorage } from '../storages/type-metadata.storage';
55
import { DefinitionsFactory } from './definitions.factory';
66

7+
// FIXME/ This is never used???
78
export class SchemaFactory {
89
/**
910
* Create a mongoose `Schema`.

packages/mongo/src/decorators/metadata/index.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { IndexDefinition, IndexOptions } from 'mongoose';
12
import { PropOptions } from '../prop.decorator';
23
import { SchemaOptions } from '../schema.decorator';
34

@@ -7,8 +8,15 @@ export interface PropertyMetadata {
78
options: PropOptions;
89
}
910

11+
export interface ExtendedSchemaOptions extends SchemaOptions {
12+
index?: {
13+
fields: IndexDefinition;
14+
options?: IndexOptions;
15+
};
16+
}
17+
1018
export interface SchemaMetadata {
1119
target: Function;
12-
options?: SchemaOptions;
20+
options?: ExtendedSchemaOptions;
1321
properties?: PropertyMetadata[];
1422
}

packages/mongo/src/decorators/schema.decorator.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import mongoose from 'mongoose';
22
import { TypeMetadataStorage } from './storages/type-metadata.storage';
3+
import { ExtendedSchemaOptions } from './metadata';
34

45
export interface SchemaOptions extends mongoose.SchemaOptions {
56
name?: string;
67
plugins?: Array<{ package: Function; options?: any }>; // TODO: Correct type.
78
}
89

9-
export const Schema = (options?: SchemaOptions): ClassDecorator => {
10+
export const Schema = (options?: ExtendedSchemaOptions): ClassDecorator => {
1011
return (target: Function) => {
1112
TypeMetadataStorage.addSchemaMetadata({
1213
target,

0 commit comments

Comments
 (0)