Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions generate.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
npm install -g sqlite3
npm install -g mysql2
npm install pg pg-hstore
npm i
npm run build
npm pack
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
},
"scripts": {
"tsc": "tsc",
"prepare": "npm run build",
"clean": "rimraf lib && rimraf types && rimraf **/models && rimraf *.tgz",
"build": "npm run clean && tsc",
"prepack": "crlf --set=LF bin/sequelize-auto",
Expand Down Expand Up @@ -87,8 +88,8 @@
"mysql": "^2.18.1",
"mysql2": "^2.2.5",
"nyc": "^15.1.0",
"pg": "^8.5.1",
"pg-hstore": "^2.3.3",
"pg": "^8.7.3",
"pg-hstore": "^2.3.4",
"rimraf": "^3.0.2",
"sequelize": "^6.11",
"sqlite3": "5.0.2",
Expand Down
13 changes: 9 additions & 4 deletions src/auto-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,12 @@ export class AutoGenerator {
}

if (defaultVal === null || defaultVal === undefined) {
return true;
// Default value for "created_at" and "updated_at" column
if (recase('c', field) === 'createdAt' || recase('c', field) === 'updatedAt') {
defaultVal = 'CURRENT_TIMESTAMP';
} else {
return true;
}
}
if (isSerialKey) {
return true; // value generated in the database
Expand Down Expand Up @@ -746,7 +751,7 @@ export class AutoGenerator {
private getEnumValues(fieldObj: TSField): string[] {
if (fieldObj.special) {
// postgres
return fieldObj.special.map((v) => `"${v}"`);
return fieldObj.special.map((v) => `"${v}"`).sort();
} else {
// mysql
return fieldObj.type.substring(5, fieldObj.type.length - 1).split(',');
Expand Down Expand Up @@ -795,7 +800,7 @@ export class AutoGenerator {
}

private isNumber(fieldType: string): boolean {
return /^(smallint|mediumint|tinyint|int|bigint|float|money|smallmoney|double|decimal|numeric|real|oid)/.test(fieldType);
return /^(smallint|mediumint|tinyint|int|bigint|float|money|smallmoney|double|decimal|real|oid)/.test(fieldType);
}

private isBoolean(fieldType: string): boolean {
Expand All @@ -807,7 +812,7 @@ export class AutoGenerator {
}

private isString(fieldType: string): boolean {
return /^(char|nchar|string|varying|varchar|nvarchar|text|longtext|mediumtext|tinytext|ntext|uuid|uniqueidentifier|date|time|inet|cidr|macaddr)/.test(fieldType);
return /^(char|nchar|string|varying|varchar|nvarchar|text|longtext|mediumtext|tinytext|ntext|uuid|uniqueidentifier|date|time|inet|cidr|macaddr|numeric)/.test(fieldType);
}

private isArray(fieldType: string): boolean {
Expand Down
5 changes: 4 additions & 1 deletion src/auto-relater.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ export class AutoRelater {
caseModel: CaseOption;
caseProp: CaseOption;
singularize: boolean;
childPropDisableSingularize: boolean;

pkSuffixes: string[];

relations: Relation[];
Expand All @@ -18,6 +20,7 @@ export class AutoRelater {
this.caseModel = options.caseModel || 'o';
this.caseProp = options.caseProp || 'o';
this.singularize = options.singularize;
this.childPropDisableSingularize = options.childPropDisableSingularize || false;
this.pkSuffixes = options.pkSuffixes || [];

if (!this.pkSuffixes || this.pkSuffixes.length == 0){
Expand Down Expand Up @@ -69,7 +72,7 @@ export class AutoRelater {
parentProp: alias,
parentTable: qNameJoin(spec.foreignSources.target_schema || schema, spec.foreignSources.target_table),
childModel: modelName,
childProp: isOne ? singularize(childAlias) : pluralize(childAlias),
childProp: !this.childPropDisableSingularize && isOne ? singularize(childAlias) : pluralize(childAlias),
childTable: qNameJoin(spec.foreignSources.source_schema || schema, spec.foreignSources.source_table),
isOne: isOne,
isM2M: false
Expand Down
3 changes: 2 additions & 1 deletion src/auto-writer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export class AutoWriter {
caseFile?: CaseFileOption;
caseModel?: CaseOption;
caseProp?: CaseOption;
caseInitModelProp?: CaseOption;
directory: string;
lang?: LangOption;
noAlias?: boolean;
Expand Down Expand Up @@ -163,7 +164,7 @@ export class AutoWriter {
// return the models
str += `\n${sp}return {\n`;
modelNames.forEach(m => {
str += `${this.space[2]}${m}: ${m},\n`;
str += `${this.space[2]}${recase(this.options.caseInitModelProp, m)}: ${m},\n`;
});
str += `${sp}};\n`;
str += '}\n';
Expand Down
2 changes: 1 addition & 1 deletion src/dialects/dialects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { sqliteOptions } from "./sqlite";
import { DialectOptions } from "./dialect-options";
import { Dialect } from "sequelize";

export const dialects: { [name in Dialect]: DialectOptions } = {
export const dialects: any = {
mssql: mssqlOptions,
mysql: mysqlOptions,
mariadb: mysqlOptions,
Expand Down
4 changes: 4 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,8 @@ export interface AutoOptions {
caseModel?: CaseOption;
/** Case of property names */
caseProp?: CaseOption;
/** Case of the property in init-model function */
caseInitModelProp?: CaseOption;
/** Close connection after export (default true) */
closeConnectionAutomatically?: boolean;
/** Database name */
Expand Down Expand Up @@ -167,6 +169,8 @@ export interface AutoOptions {
schema?: string;
/** Whether to singularize model names */
singularize: boolean;
/** The prop is always plural */
childPropDisableSingularize?: boolean;
/** Tables to skip exporting */
skipTables?: string[];
/** Fields to skip exporting */
Expand Down