-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
175 changed files
with
1,405 additions
and
3,995 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,54 +1,20 @@ | ||
import { ModelDefinition } from '@nestjs/mongoose'; | ||
import { AsyncModelFactory } from '@nestjs/mongoose'; | ||
|
||
import { Schema } from 'mongoose'; | ||
import AutoPopulate from 'mongoose-autopopulate'; | ||
|
||
const convertToKST = (date: Date): Date => new Date(date.getTime() + 9 * 60 * 60 * 1000); | ||
|
||
export class MongoModelFactory { | ||
static generate<T>(name: string, schema: Schema<T>): ModelDefinition { | ||
schema = MongoModelFactory.setSchemaOptions(schema); | ||
|
||
return { name, schema }; | ||
} | ||
|
||
static generateRecursive<T>( | ||
name: string, | ||
schema: Schema<T>, | ||
subSchemas: { type: string; schema: Schema }[], | ||
): ModelDefinition { | ||
schema = MongoModelFactory.setSchemaOptions(schema); | ||
subSchemas = subSchemas.map(({ type, schema }) => { | ||
schema = MongoModelFactory.setSchemaOptions(schema); | ||
|
||
return { type, schema }; | ||
}); | ||
|
||
static generate<T>(name: string, schema: Schema<T>): AsyncModelFactory { | ||
return { | ||
name, | ||
schema, | ||
discriminators: subSchemas.map(({ type, schema }) => { | ||
return { name: type, schema }; | ||
}), | ||
}; | ||
} | ||
|
||
private static setSchemaOptions(schema: Schema): Schema { | ||
schema.set('versionKey', false); | ||
schema.set('timestamps', true); | ||
|
||
schema.pre('save', function (next) { | ||
if ('createdAt' in this) { | ||
this['createdAt'] = convertToKST(new Date()); | ||
} | ||
useFactory: () => { | ||
schema.set('timestamps', true); | ||
schema.set('versionKey', false); | ||
|
||
this['updatedAt'] = convertToKST(new Date()); | ||
schema.plugin(AutoPopulate); | ||
|
||
next(); | ||
}); | ||
|
||
schema.plugin(AutoPopulate); | ||
|
||
return schema; | ||
return schema; | ||
}, | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.