Nest Resource Schematics
Installation
$ npm install nest-resource
Usage
Create folder and files
- Create "schemas" folder root of the project
- All schema files should be in "schemas" folder
Schema File
sample schema file: schemas/cargo.json
{
"modelName": "Cargo",
"api": "cargos",
"fields": [
{
"name": "quantity",
"type": "number",
"dbType": {
"allowNull": false,
"autoIncrement": true,
"primaryKey": true,
"type": "INTEGER"
},
"dto": [
"IsNotEmpty", "IsString"
]
},
{
"name": "description",
"type": "string",
"dbType": {
"allowNull": false,
"type": "STRING"
},
"dto": [
"IsNotEmpty"
]
},
{
"name": "remarks",
"type": "string"
}
],
"indexes": [
"description"
]
}
Generate Resource
if schema args not provided, file name and model name should be same
nest g -c nest-resource rs <model-name> OR
nest g -c nest-resource rs <model-name> --schema=[file name without .json]
Available Commands
[//]: # (table)
alias | command | description |
---|---|---|
rs / resource | nest g -c nest-resource rs | Generate full resource with module file |
dt / dto | nest g -c nest-resource dt | Generate DTO file only with given name |
mg / migration | nest g -c nest-resource mg | Generate migration file only with given name |
et / entity | nest g -c nest-resource et | Generate entity file only with given name |
Database Type Mapping
lowercase also supported
[
'ABSTRACT',
'STRING',
'CHAR',
'TEXT',
'NUMBER',
'TINYINT',
'SMALLINT',
'MEDIUMINT',
'INTEGER',
'BIGINT',
'FLOAT',
'TIME',
'DATE',
'DATEONLY',
'BOOLEAN',
'NOW',
'BLOB',
'DECIMAL',
'NUMERIC',
'UUID',
'UUIDV1',
'UUIDV4',
'HSTORE',
'JSON',
'JSONB',
'VIRTUAL',
'ARRAY',
'NONE',
'ENUM',
'RANGE',
'REAL',
'DOUBLE',
'DOUBLE PRECISION',
'GEOMETRY',
];
Types
export interface SchemaModel {
modelName: string;
api: string;
fields: Field[];
indexes?: string[];
}
export interface Field {
name: string;
type: string;
dbType?: { [key: string]: any };
dto?: string[];
}