-
-
Notifications
You must be signed in to change notification settings - Fork 529
feat: configuration for seamless cli use in a typescript project #1439
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
'use strict'; | ||
import { Sequelize } from 'sequelize'; | ||
const env = process.env.NODE_ENV || 'development'; | ||
const config = require(<%= configFile %>)[env]; | ||
|
||
const sequelizeConnection<%= isTypescriptProject ? ': Sequelize' : '' %> = config.config.use_env_variable | ||
? new Sequelize( process.env[config.config.use_env_variable], config) | ||
: new Sequelize(config.database, config.username, config.password, config); | ||
|
||
module.exports = sequelizeConnection; |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,19 @@ async function supportsDynamicImport() { | |
} | ||
} | ||
|
||
function isPackageInstalled(packageName) { | ||
try { | ||
// Try to require the package | ||
require.resolve(packageName); | ||
return true; | ||
} catch (error) { | ||
// If require.resolve throws an error, the package is not installed | ||
return false; | ||
} | ||
} | ||
|
||
const isTypescriptProject = isPackageInstalled('typescript'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what about having a way to decide it from the CLI? e.g. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is also how we are going to do it in the new CLI; https://github.com/sequelize/sequelize/blob/main/packages/cli/src/commands/migration/generate.ts |
||
|
||
/** | ||
* Imports a JSON, CommonJS or ESM module | ||
* based on feature detection. | ||
|
@@ -39,4 +52,6 @@ async function importModule(modulePath) { | |
module.exports = { | ||
supportsDynamicImport, | ||
importModule, | ||
isPackageInstalled, | ||
isTypescriptProject, | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,27 @@ import helpers from './index'; | |
|
||
const Sequelize = helpers.generic.getSequelize(); | ||
const validAttributeFunctionType = ['array', 'enum']; | ||
const typescriptTypesForDbFieldTypes = { | ||
string: 'string', | ||
text: 'string', | ||
uuid: 'string', | ||
CHAR: 'string', | ||
number: 'number', | ||
float: 'number', | ||
integer: 'number', | ||
bigint: 'number', | ||
mediumint: 'number', | ||
tinyint: 'number', | ||
smallint: 'number', | ||
double: 'number', | ||
'double precision': 'number', | ||
real: 'number', | ||
decimal: 'number', | ||
date: 'data', | ||
now: 'data', | ||
dateonly: 'data', | ||
boolean: 'boolean', | ||
}; | ||
|
||
/** | ||
* Check the given dataType actual exists. | ||
|
@@ -15,6 +36,17 @@ function validateDataType(dataType) { | |
return dataType; | ||
} | ||
|
||
function getTsTypeForDbColumnType(db_type, attribute_func, values) { | ||
db_type = db_type.toLowerCase(); | ||
if (attribute_func === 'array') { | ||
return `${typescriptTypesForDbFieldTypes[db_type]}[]`; | ||
} else if (attribute_func === 'enum') { | ||
return values.join(' | '); | ||
} | ||
|
||
return typescriptTypesForDbFieldTypes[db_type] || 'any'; | ||
} | ||
|
||
function formatAttributes(attribute) { | ||
let result; | ||
const split = attribute.split(':'); | ||
|
@@ -24,10 +56,13 @@ function formatAttributes(attribute) { | |
fieldName: split[0], | ||
dataType: split[1], | ||
dataFunction: null, | ||
tsType: getTsTypeForDbColumnType(split[1]), | ||
dataValues: null, | ||
}; | ||
} else if (split.length === 3) { | ||
const validValues = /^\{(,? ?[A-z0-9 ]+)+\}$/; | ||
const validValues = | ||
/^\{((('[A-z0-9 ]+')|("[A-z0-9 ]+")|([A-z0-9 ]+)))(, ?(('[A-z0-9 ]+')|("[A-z0-9 ]+")|([A-z0-9 ]+)))*\}$/; | ||
Check failureCode scanning / CodeQL Inefficient regular expression
This part of the regular expression may cause exponential backtracking on strings starting with '{{ ,' and containing many repetitions of ' ,'.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it's a false positive, @WikiRik can you please dismiss this alert? Check warningCode scanning / CodeQL Overly permissive regular expression range
Suspicious character range that is equivalent to \[A-Z\\[\\\\]^_`a-z\].
Check warningCode scanning / CodeQL Overly permissive regular expression range
Suspicious character range that is equivalent to \[A-Z\\[\\\\]^_`a-z\].
Check warningCode scanning / CodeQL Overly permissive regular expression range
Suspicious character range that is equivalent to \[A-Z\\[\\\\]^_`a-z\].
Check warningCode scanning / CodeQL Overly permissive regular expression range
Suspicious character range that is equivalent to \[A-Z\\[\\\\]^_`a-z\].
Check warningCode scanning / CodeQL Overly permissive regular expression range
Suspicious character range that is equivalent to \[A-Z\\[\\\\]^_`a-z\].
Check warningCode scanning / CodeQL Overly permissive regular expression range
Suspicious character range that is equivalent to \[A-Z\\[\\\\]^_`a-z\].
|
||
|
||
const isValidFunction = | ||
validAttributeFunctionType.indexOf(split[1].toLowerCase()) !== -1; | ||
const isValidValue = | ||
|
@@ -40,20 +75,23 @@ function formatAttributes(attribute) { | |
fieldName: split[0], | ||
dataType: split[2], | ||
dataFunction: split[1], | ||
tsType: getTsTypeForDbColumnType(split[2], split[1]), | ||
dataValues: null, | ||
}; | ||
} | ||
|
||
if (isValidFunction && !isValidValue && isValidValues) { | ||
const values = split[2] | ||
.replace(/(^\{|\}$)/g, '') | ||
.split(/\s*,\s*/) | ||
.map((s) => (s.startsWith('"') || s.startsWith("'") ? s : `'${s}'`)); | ||
|
||
result = { | ||
fieldName: split[0], | ||
dataType: split[1], | ||
tsType: getTsTypeForDbColumnType(split[2], split[1], values), | ||
dataFunction: null, | ||
dataValues: split[2] | ||
.replace(/(^\{|\}$)/g, '') | ||
.split(/\s*,\s*/) | ||
.map((s) => `'${s}'`) | ||
.join(', '), | ||
dataValues: values.join(', '), | ||
}; | ||
} | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i think also worth
export default
no?