diff --git a/.gitignore b/.gitignore index 33eca0b..9ef7686 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ node_modules .vscode -dist \ No newline at end of file +dist +coverage diff --git a/package-lock.json b/package-lock.json index abf1671..ff21df9 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,14 +1,15 @@ { "name": "smodg", - "version": "1.0.2", + "version": "1.1.0", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "smodg", - "version": "1.0.2", + "version": "1.1.0", "license": "ISC", "dependencies": { + "minimist": "^1.2.8", "typescript": "^4.9.5" }, "bin": { @@ -16,6 +17,7 @@ }, "devDependencies": { "@types/jest": "^29.4.1", + "@types/minimist": "^1.2.2", "copyfiles": "^2.4.1", "jest": "^29.5.0", "rimraf": "^4.4.0", @@ -1113,6 +1115,12 @@ "pretty-format": "^29.0.0" } }, + "node_modules/@types/minimist": { + "version": "1.2.2", + "resolved": "https://registry.npmjs.org/@types/minimist/-/minimist-1.2.2.tgz", + "integrity": "sha512-jhuKLIRrhvCPLqwPcx6INqmKeiA5EWrsCOPhrlFSrbrmU4ZMPjj5Ul/oLCMDO98XRUIwVm78xICz4EPCektzeQ==", + "dev": true + }, "node_modules/@types/node": { "version": "18.15.3", "resolved": "https://registry.npmjs.org/@types/node/-/node-18.15.3.tgz", @@ -2918,6 +2926,14 @@ "node": "*" } }, + "node_modules/minimist": { + "version": "1.2.8", + "resolved": "https://registry.npmjs.org/minimist/-/minimist-1.2.8.tgz", + "integrity": "sha512-2yyAR8qBkN3YuheJanUpWC5U3bb5osDywNB8RzDVlDwDHbocAJveqqj1u8+SVD7jkWT4yvsHCpWqqWqAxb0zCA==", + "funding": { + "url": "https://github.com/sponsors/ljharb" + } + }, "node_modules/minipass": { "version": "4.2.5", "resolved": "https://registry.npmjs.org/minipass/-/minipass-4.2.5.tgz", @@ -4880,6 +4896,12 @@ "pretty-format": "^29.0.0" } }, + "@types/minimist": { + "version": "1.2.2", + "resolved": "https://registry.npmjs.org/@types/minimist/-/minimist-1.2.2.tgz", + "integrity": "sha512-jhuKLIRrhvCPLqwPcx6INqmKeiA5EWrsCOPhrlFSrbrmU4ZMPjj5Ul/oLCMDO98XRUIwVm78xICz4EPCektzeQ==", + "dev": true + }, "@types/node": { "version": "18.15.3", "resolved": "https://registry.npmjs.org/@types/node/-/node-18.15.3.tgz", @@ -6243,6 +6265,11 @@ "brace-expansion": "^1.1.7" } }, + "minimist": { + "version": "1.2.8", + "resolved": "https://registry.npmjs.org/minimist/-/minimist-1.2.8.tgz", + "integrity": "sha512-2yyAR8qBkN3YuheJanUpWC5U3bb5osDywNB8RzDVlDwDHbocAJveqqj1u8+SVD7jkWT4yvsHCpWqqWqAxb0zCA==" + }, "minipass": { "version": "4.2.5", "resolved": "https://registry.npmjs.org/minipass/-/minipass-4.2.5.tgz", diff --git a/package.json b/package.json index df78ca9..c7f644a 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "smodg", - "version": "1.0.3", + "version": "1.1.0", "description": "Generate basic Sequelize models from TypeScript declaration files", "main": "index.js", "bin": { @@ -23,6 +23,7 @@ "license": "ISC", "devDependencies": { "@types/jest": "^29.4.1", + "@types/minimist": "^1.2.2", "copyfiles": "^2.4.1", "jest": "^29.5.0", "rimraf": "^4.4.0", @@ -30,6 +31,7 @@ "ts-node": "^10.9.1" }, "dependencies": { + "minimist": "^1.2.8", "typescript": "^4.9.5" } } diff --git a/src/formatters.ts b/src/formatters.ts index 2af58f9..7d3fd94 100644 --- a/src/formatters.ts +++ b/src/formatters.ts @@ -1,3 +1,6 @@ +/** +* Convert a string to snake_case +* */ const snakeCase = (str: string) => { return str.replace(/[A-Z]/g, (match, index) => { if (index === 0) { @@ -10,6 +13,9 @@ const snakeCase = (str: string) => { }); } +/** +* Convert a string to kebab-case +*/ const kebabCase = (str: string) => { return str.replace(/[A-Z]/g, (match, index) => { if (index === 0) { @@ -22,4 +28,4 @@ const kebabCase = (str: string) => { }); } -export { snakeCase, kebabCase } \ No newline at end of file +export { snakeCase, kebabCase } diff --git a/src/index.ts b/src/index.ts index a0301b6..3a8ccc7 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,27 +1,152 @@ #!/usr/bin/env node import fs from 'node:fs' -import { argv } from 'node:process'; +import minimist from 'minimist'; import { generateModelInputs } from './parser'; -import { modelTemplate } from './templates/model.smodg'; +import { modelTemplate, migrationTemplate } from './templates'; import { kebabCase } from './formatters'; +import { version } from '../package.json' -try { - const sourceCode = fs.readFileSync(argv[2], 'utf-8') - const modelInputs = generateModelInputs(sourceCode) +export const main = (args: minimist.ParsedArgs) => { - const model = modelTemplate(modelInputs) + const filePath = args._[0] + + let generateMigrationFile = false + let schema = "" + let outputDir = 'src/models' + + if(args.h || args.help){ + printHelp() + return + } + + if(args.v || args.version){ + printVersion() + return + } + + if(args.migration){ + generateMigrationFile = true + } + + if(args.s){ + schema = args.s + } else if (args.schema) { + schema = args.schema + } + + if(args.o){ + outputDir = args.o + } else if (args.outputDir) { + outputDir = args.outputDir + } - if (!fs.existsSync('./src/models')) { - fs.mkdirSync('./src/models') + if(!filePath){ + console.error('error: path to type file is required!') + return } try { - fs.writeFileSync(`./src/models/${kebabCase(modelInputs.modelName)}.model.ts`, model) + + const sourceCode = fs.readFileSync(filePath, 'utf-8') + const modelInputs = generateModelInputs(sourceCode, schema) + const model = modelTemplate(modelInputs) + + writeModelToFile(model, { outputDir, modelName: modelInputs.modelName }) + + if (generateMigrationFile) { + + const migrationInputs = { + tableDefinition: modelInputs.tableDefinition, + columnDefinitions: modelInputs.columnDefinitions + } + + const migration = migrationTemplate(migrationInputs) + writeMigrationToFile(migration, {modelName: modelInputs.modelName}) + } + } catch(error) { + console.error(error) + } + +} + +export const writeModelToFile = (model: string, options: {outputDir: string, modelName: string}) => { + + if (!fs.existsSync(`./${options.outputDir}`)) { + fs.mkdirSync(`./${options.outputDir}`) + } + + try { + fs.writeFileSync(`./${options.outputDir}/${kebabCase(options.modelName)}.model.ts`, model) } catch (error) { console.error(error) } +} + +const writeMigrationToFile = (migration: string, options: {modelName: string}) => { + + if (!fs.existsSync(`./src/migrations`)) { + fs.mkdirSync(`./src/migrations`) + } + + try { + fs.writeFileSync(`./src/migrations/${dateFormatString()}-Create-Table-${options.modelName}.ts`, migration) + } catch(error) { + console.error(error) + } +} + + +const dateFormatString = () => { + const currentDate = new Date(); + const year = currentDate.getFullYear(); + const month = String(currentDate.getMonth() + 1).padStart(2, '0'); + const day = String(currentDate.getDate()).padStart(2, '0'); + const hours = String(currentDate.getHours()).padStart(2, '0'); + const minutes = String(currentDate.getMinutes()).padStart(2, '0'); + const seconds = String(currentDate.getSeconds()).padStart(2, '0'); + + return `${year}.${month}.${day}T${hours}.${minutes}.${seconds}`; +} + +export const printHelp = () => { + const helpText = ` +================================= + Sequelize Model Generator +================================= + +Generate a Sequelize model based on a TypeScript type declaration. + +Usage: + + smodg [options] + +Options: + + --help, -h show help + + --migration create an Umzug migration. default: false + + --outputDir=PATH, model output directory, relative to current path. default: "src/models" + -o PATH + + --schema=NAME specify a schema. default is no schema + -s NAME + + --version, -v print installed version +` + + console.log(helpText) +} + +export const printVersion = () => { + console.log(`smodg v${version}`) +} + + +const args = minimist(process.argv.slice(2), { + stopEarly: true, + boolean: true +}) -} catch (error) { - console.error(error) -} \ No newline at end of file +main(args) diff --git a/src/models/.model.ts b/src/models/.model.ts new file mode 100644 index 0000000..ef2d749 --- /dev/null +++ b/src/models/.model.ts @@ -0,0 +1,83 @@ + +import { + Column, + Table, + Model, + DataType, + CreatedAt, + UpdatedAt, + ForeignKey, + HasMany, +} from 'sequelize-typescript'; + +import { as Type } from '@_YOUR_TYPES' +import { ModelAttributeColumnOptions } from 'sequelize'; + +interface CreationAttributes extends Type {} + +interface Attributes extends CreationAttributes { + id: number; + createdBy: string; + createdDate: Date; + updatedBy: string; + updatedDate: Date; +} + +type Keys = keyof Attributes + +interface ColumnOptions extends ModelAttributeColumnOptions { + field: string +} + +export const tableDefinition = { + tableName: '', +} + +export const columnDefinition: Record = { + id: { + field: "id", + type: DataType.INTEGER, + primaryKey: true, + autoIncrement: true, + }, + + createdBy: { + field: "created_by", + type: DataType.STRING, + }, + createdDate: { + field: "created_date", + type: DataType.DATE, + }, + updatedBy: { + field: "updated_by", + type: DataType.STRING, + }, + updatedDate: { + field: "updated_date", + type: DataType.DATE, + }, +} + +@Table(tableDefinition) +export class +extends Model +implements Attributes { + @Column(columnDefinition.id) + id!: number; + + @Column(columnDefinition.createdBy) + createdBy!: string; + + @CreatedAt + @Column(columnDefinition.createdDate) + createdDate!: Date; + + @Column(columnDefinition.updatedBy) + updatedBy!: string; + + @UpdatedAt + @Column(columnDefinition.updatedDate) + updatedDate!: Date; +} + \ No newline at end of file diff --git a/src/parser.ts b/src/parser.ts index 6dac572..e5a1fb5 100644 --- a/src/parser.ts +++ b/src/parser.ts @@ -1,7 +1,7 @@ import ts from 'typescript'; import { snakeCase } from './formatters'; -export const generateModelInputs = (fileContent: string) => { +export const generateModelInputs = (fileContent: string, schemaName: string) => { const tokens = readTokensFromSource(fileContent); const { modelName, types } = parseTypeObjects(tokens) @@ -13,35 +13,16 @@ export const generateModelInputs = (fileContent: string) => { columnDefinitions = `${columnDefinitions}\t${obj.key}: {\n\t\tfield: '${snakeCase(obj.key)}',\n\t\ttype: DataType.${getSequelizeType(obj.type)}\n\t},\n` }) - return {modelName, columnDecorators, columnDefinitions} + const tableDefinition = generateTableDefinition(modelName, schemaName) -} - -const generateModel = (fileContent: string) => { - const tokens = readTokensFromSource(fileContent); - const { modelName, types } = parseTypeObjects(tokens) + return {modelName, columnDecorators, columnDefinitions, tableDefinition} - let columnDecorators = `` - types.forEach(obj => { - columnDecorators = `${columnDecorators}\n\t@Column(columnDefinition.${obj.key})\n\t${obj.key}!: ${obj.type}\n` - }) - - return columnDecorators } -const generateColumnDefinition = (fileContent: string) => { - const tokens = readTokensFromSource(fileContent); - const { modelName, types } = parseTypeObjects(tokens) - - let columnDefinitions = `` - types.forEach(obj => { - columnDefinitions = `${columnDefinitions}\t${obj.key}: {\n\t\tfield: '${snakeCase(obj.key)}',\n\t\ttype: DataType.${getSequelizeType(obj.type)}\n\t},\n` - }) - - return columnDefinitions +const generateTableDefinition = (modelName: string, schemaName: string) => { + return `\ttableName: '${snakeCase(modelName)}', ${schemaName ? `\n\tschema: '${snakeCase(schemaName)}',`: ''}` } - const readTokensFromSource = (sourceCode: string) => { const sourceFile = ts.createSourceFile("test.ts", sourceCode, ts.ScriptTarget.Latest); let tokens: { text: string, kind: string }[] = []; @@ -103,12 +84,15 @@ const getSequelizeType = (jsType: string) => { case 'Date': return 'DATE' case 'number': - return 'DECIMAL' + return 'FLOAT' default: break; } } - - -export { generateModel, readTokensFromSource, parseTypeObjects, getSequelizeType, generateColumnDefinition } \ No newline at end of file +export { + readTokensFromSource, + parseTypeObjects, + getSequelizeType, + generateTableDefinition +} diff --git a/src/templates/index.ts b/src/templates/index.ts new file mode 100644 index 0000000..1d7e3ef --- /dev/null +++ b/src/templates/index.ts @@ -0,0 +1,2 @@ +export * from './model.smodg' +export * from './migration.smodg' diff --git a/src/templates/migration.smodg.ts b/src/templates/migration.smodg.ts new file mode 100644 index 0000000..4d92419 --- /dev/null +++ b/src/templates/migration.smodg.ts @@ -0,0 +1,19 @@ +export const migrationTemplate = (input: {tableDefinition: string, columnDefinitions: string}) => { + return ` +import { DataType } from 'sequelize-typescript'; +import type { Migration } from '../db'; + +const tableDefinition = { +${input.tableDefinition} +} + +const columnDefinition = { +${input.columnDefinitions} +}; + +export const up: Migration = async ({ context: queryInterface }) => { + await queryInterface.createTable(tableDefinition, columnDefinition); +}; + + ` +} diff --git a/src/templates/model.smodg.ts b/src/templates/model.smodg.ts index b4747d6..4aea03e 100644 --- a/src/templates/model.smodg.ts +++ b/src/templates/model.smodg.ts @@ -1,6 +1,12 @@ -import { snakeCase } from "../formatters" -export const modelTemplate = (input: any) => { +export type ModelTemplateInput = { + modelName: string, + tableDefinition: string, + columnDefinitions: string, + columnDecorators: string +} + +export const modelTemplate = (input: ModelTemplateInput) => { return ` import { Column, @@ -13,10 +19,12 @@ import { HasMany, } from 'sequelize-typescript'; -import { ${input.modelName} as ${input.modelName}API } from '@_YOUR_TYPES' +import { ${input.modelName} as ${input.modelName}Type } from '@_YOUR_TYPES' import { ModelAttributeColumnOptions } from 'sequelize'; -interface ${input.modelName}Attributes extends ${input.modelName}API { +interface ${input.modelName}CreationAttributes extends ${input.modelName}Type {} + +interface ${input.modelName}Attributes extends ${input.modelName}CreationAttributes { id: number; createdBy: string; createdDate: Date; @@ -31,7 +39,7 @@ interface ColumnOptions extends ModelAttributeColumnOptions { } export const tableDefinition = { - tableName: '${snakeCase(input.modelName)}', ${input.schemaName ? `\n\tschema: '${input.schemaName}',`: ''} +${input.tableDefinition} } export const columnDefinition: Record<${input.modelName}Keys, ColumnOptions> = { @@ -61,7 +69,9 @@ ${input.columnDefinitions} } @Table(tableDefinition) -export class ${input.modelName} extends Model<${input.modelName}Attributes> implements ${input.modelName}Attributes { +export class ${input.modelName} +extends Model<${input.modelName}Attributes, ${input.modelName}CreationAttributes> +implements ${input.modelName}Attributes { @Column(columnDefinition.id) id!: number; ${input.columnDecorators} @@ -80,4 +90,4 @@ ${input.columnDecorators} updatedDate!: Date; } ` -} \ No newline at end of file +} diff --git a/tests/formatters.spec.ts b/tests/formatters.spec.ts index 21af0a1..704bf4b 100644 --- a/tests/formatters.spec.ts +++ b/tests/formatters.spec.ts @@ -23,4 +23,27 @@ describe('formatters', () => { expect(result).toEqual(expected) }) }) -}) \ No newline at end of file + + describe('kebabCase', () => { + test('should convert camelCase to kebab-case', () => { + const text = 'thisIsCamelCase' + const expected = 'this-is-camel-case' + const result = kebabCase(text) + expect(result).toEqual(expected) + }) + + test('should convert consecutive uppercase to kebab-case', () => { + const text = 'thisIsCostUSD' + const expected = 'this-is-cost-usd' + const result = kebabCase(text) + expect(result).toEqual(expected) + }) + + test('should convert PascalCase to kebab-case', () => { + const text = 'ThisIsPascalCase' + const expected = 'this-is-pascal-case' + const result = kebabCase(text) + expect(result).toEqual(expected) + }) + }) +}) diff --git a/tests/index.spec.ts b/tests/index.spec.ts new file mode 100644 index 0000000..cda13ae --- /dev/null +++ b/tests/index.spec.ts @@ -0,0 +1,148 @@ +const fs = require('node:fs') + +jest.mock('node:fs') + +jest.mock('../package.json', () => ({ + version: '1.2.3' +}), {virtual: true}) + +import { + printVersion, + printHelp, + writeModelToFile, + main +} from '../src/index' + +describe('cli', () => { + describe('printVersion', () => { + test('should print the current app version', () => { + console.log = jest.fn() + printVersion() + expect(console.log).toHaveBeenCalledWith('smodg v1.2.3') + }) + }) + describe('printHelp', () => { + test('should print help text', () => { + console.log = jest.fn() + printHelp() + expect(console.log).toHaveBeenCalled() + }) + }) + + describe('writeModelToFile', () => { + + beforeEach(() => { + jest.resetAllMocks() + }) + + test('should create the output directory if not exists', () => { + fs.existsSync.mockReturnValue(false) + + const options = { + outputDir: 'non_existent_dir', + modelName: 'doesntMatter' + } + + writeModelToFile('blah', options) + expect(fs.mkdirSync).toHaveBeenCalled() + expect(fs.mkdirSync).toHaveBeenCalledWith(`./${options.outputDir}`) + + }) + + test('should not create the output directory if it already exists', () => { + fs.existsSync.mockReturnValue(true) + + const options = { + outputDir: 'existent_dir', + modelName: 'doesntMatter' + } + + writeModelToFile('blah', options) + expect(fs.mkdirSync).not.toHaveBeenCalled() + }) + + + test('should write model content to file path', () => { + fs.writeFileSync = jest.fn() + + const modelData = 'blah' + + const options = { + outputDir: 'a/path/to/a/directory', + modelName: 'importantModelName' + } + + const expectedFileName = 'important-model-name.model.ts' + + writeModelToFile(modelData, options) + + expect(fs.writeFileSync).toHaveBeenCalledWith(`./${options.outputDir}/${expectedFileName}`, modelData) + + }) + + test('should print error if thrown', () => { + fs.writeFileSync.mockImplementation(() => { + throw new Error('borked') + }) + + console.error = jest.fn() + + const options = { + outputDir: 'dont_matter_dir', + modelName: 'doesntMatterEither' + } + + writeModelToFile('blah', options) + + expect(console.error).toHaveBeenCalledWith(new Error('borked')) + + }) + }) + + describe('command line arguments', () => { + test('-v and --version should print the version', () => { + + console.log = jest.fn() + + let args: any; + + // smodg -v + args = {_: [], v: true} + main(args) + expect(console.log).toHaveBeenCalledWith('smodg v1.2.3') + + jest.resetAllMocks() + + // smodg --version + args = {_: [], version: true} + main(args) + expect(console.log).toHaveBeenCalledWith('smodg v1.2.3') + + }) + + test('-h and --help should print helptext', () => { + + console.log = jest.fn() + + let args: any; + + // smodg -h + args = {_: [], h: true} + main(args) + expect(console.log).toHaveBeenCalled() + + jest.resetAllMocks() + + // smodg --help + args = {_: [], help: true} + main(args) + expect(console.log).toHaveBeenCalled() + }) + + test('-m and --migration should trigger a migration generation', () => {}) + test('-s and --schema should set a schema name', () => {}) + test('-o and --outputDir should set the output directory', () => {}) + test('should pass filepath to readFileSync', () => {}) + test('should probably be implemented differently because these tests are getting wacky', () => {}) + }) +}) diff --git a/tests/parser.spec.ts b/tests/parser.spec.ts index 956f3a0..7af7845 100644 --- a/tests/parser.spec.ts +++ b/tests/parser.spec.ts @@ -1,4 +1,10 @@ -import { generateModel, readTokensFromSource, parseTypeObjects, getSequelizeType, generateColumnDefinition } from '../src/parser' +import { + readTokensFromSource, + parseTypeObjects, + getSequelizeType, + generateTableDefinition, + generateModelInputs +} from '../src/parser' describe('parser', () => { @@ -74,63 +80,87 @@ describe('parser', () => { }) }) - describe('generateModel', () => { - test('should return a sequelize model', () => { + describe('getSequelizeType', () => { + test('should return STRING for string', () => { + const result = getSequelizeType('string') + expect(result).toEqual('STRING') + }) + test('should return BOOLEAN for boolean', () => { + const result = getSequelizeType('boolean') + expect(result).toEqual('BOOLEAN') + }) + test('should return DATE for Date', () => { + const result = getSequelizeType('Date') + expect(result).toEqual('DATE') + }) + test('should return DECIMAL for number', () => { + const result = getSequelizeType('number') + expect(result).toEqual('FLOAT') + }) + test('should return undefined for unknown type', () => { + const result = getSequelizeType('FooBar') + expect(result).toBe(undefined) + }) + }) + + describe('generateTableDefinition', () => { + test('should return a table def string with no schema if schema is blank', () => { + const expected = `\ttableName: 'my_model', ` + const result = generateTableDefinition("MyModel", "") + expect(result).toEqual(expected) + }) + test('should return a table def string with schema if schema is not blank', () => { + const expected = `\ttableName: 'my_model', \n\tschema: 'my_schema',` + const result = generateTableDefinition("MyModel", "MySchema") + expect(result).toEqual(expected) + }) + }) + + describe('generateModelInputs', () => { + test('should return the model name', () => { const fileContent = `export type SmallTest = { name: string; + anotherName: string; }` - const expectedOutput = `\n\t@Column(columnDefinition.name)\n\tname!: string\n` - - const result = generateModel(fileContent) - - expect(result).toEqual(expectedOutput) + const result = generateModelInputs(fileContent, '') + expect(result.modelName).toEqual('SmallTest') }) - test('should handle a multiple properties', () => { + + test('should return the table definition', () => { const fileContent = `export type SmallTest = { name: string; anotherName: string; }` - const expectedOutput = `\n\t@Column(columnDefinition.name)\n\tname!: string\n\n\t@Column(columnDefinition.anotherName)\n\tanotherName!: string\n` + const expected = `\ttableName: 'small_test', ` - const result = generateModel(fileContent) - - expect(result).toEqual(expectedOutput) + const result = generateModelInputs(fileContent, '') + expect(result.tableDefinition).toEqual(expected) }) - }) - - describe('generateColumnDefinition', () => { - test('should return column definition', () => { - const fileContent = `export type SmallTest = { + test('should return the column definitions', () => { + const fileContent = `export type SmallTest = { name: string; anotherName: string; }` - const expectedOuput = `\tname: {\n\t\tfield: 'name',\n\t\ttype: DataType.STRING\n\t},\n\tanotherName: {\n\t\tfield: 'another_name',\n\t\ttype: DataType.STRING\n\t},\n` - const result = generateColumnDefinition(fileContent) - expect(result).toEqual(expectedOuput) + const expected = `\tname: {\n\t\tfield: 'name',\n\t\ttype: DataType.STRING\n\t},\n\tanotherName: {\n\t\tfield: 'another_name',\n\t\ttype: DataType.STRING\n\t},\n` + const result = generateModelInputs(fileContent, '') + expect(result.columnDefinitions).toEqual(expected) }) - }) - describe('getSequelizeType', () => { - test('should return STRING for string', () => { - const result = getSequelizeType('string') - expect(result).toEqual('STRING') - }) - test('should return BOOLEAN for boolean', () => { - const result = getSequelizeType('boolean') - expect(result).toEqual('BOOLEAN') - }) - test('should return DATE for Date', () => { - const result = getSequelizeType('Date') - expect(result).toEqual('DATE') - }) - test('should return DECIMAL for number', () => { - const result = getSequelizeType('number') - expect(result).toEqual('DECIMAL') + test('should return column decorators', () => { + const fileContent = `export type SmallTest = { + name: string; + anotherName: string; + }` + + const expected = `\n\t@Column(columnDefinition.name)\n\tname!: string\n\n\t@Column(columnDefinition.anotherName)\n\tanotherName!: string\n` + const result = generateModelInputs(fileContent, '') + + expect(result.columnDecorators).toEqual(expected) }) }) -}) \ No newline at end of file +}) diff --git a/tsconfig.json b/tsconfig.json index 852635e..a33dfd9 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -35,7 +35,7 @@ // "types": [], /* Specify type package names to be included without being referenced in a source file. */ // "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */ // "moduleSuffixes": [], /* List of file name suffixes to search when resolving a module. */ - // "resolveJsonModule": true, /* Enable importing .json files. */ + "resolveJsonModule": true, /* Enable importing .json files. */ // "noResolve": true, /* Disallow 'import's, 'require's or ''s from expanding the number of files TypeScript should add to a project. */ /* JavaScript Support */