@zcong/ts-mongoose-code-generator
v2.0.0
Published
generate ts model types from mongoose schema
Downloads
89
Maintainers
Readme
ts-mongoose-code-generator
generate ts model types from mongoose schema
Install
$ yarn add @zcong/ts-mongoose-code-generator
Usage
modelGenerator
import { ModelGenerator } from '@zcong/ts-mongoose-code-generator'
const testSchema = new Schema({
name: String,
age: {
type: Number
},
requiredName: {
type: String,
required: true
}
}
// useInterface: use class or interface as type declaration
const dtoGen = new ModelGenerator({
filename: `${__dirname}/modelGen.ts`,
useInterface: true, // use interface type or class type
arrayStyle: 'squareBrackets', // arrayStyle squareBrackets(T[]) or generic(Array<T>)
stringEnumUseUnionType: true, // if set true, String type with enum field ['test1', 'test2'] will be type `'test1' | 'test2'`
})
dtoGen.generateModelBySchema(testSchema, 'Test')
dtoGen.getFile().saveSync() // save generated code as file
dtoGen.getGeneratedCode() // get generated code content
// export interface TestModel {
// name?: string;
// age?: number;
// requiredName: string;
// }
License
MIT © zcong1993