
Research
/Security News
Toptal’s GitHub Organization Hijacked: 10 Malicious Packages Published
Threat actors hijacked Toptal’s GitHub org, publishing npm packages with malicious payloads that steal tokens and attempt to wipe victim systems.
mysql-types-generator
Advanced tools
Inspects a mysql database and generates Typescript types for each table. Useful when you are using knex
or raw mysql clients instead of an ORM.
Table names in the database must be in snake_case and will be converted to PascalCase for type names.
The tool can be used with the javascript API or as a CLI with npx
.
Create a file to configure and run the generator:
src/db/updateTypes.js
import { generateMysqlTypes } from 'mysql-types-generator';
const dbConfig = {
host: 'localhost',
port: 3306,
user: 'myuser',
password: 'mypassword',
database: 'mydatabase',
ssl: {
rejectUnauthorized: true
}
};
// OR
const dbConfig = {
uri: 'mysql://myuser:mypassword@localhost:3306/mydatabase',
database: 'mydatabase',
ssl: {
rejectUnauthorized: true
}
};
generateMysqlTypes({
db: dbConfig,
output: {
// Specify only one of the following 2 options:
dir: 'src/db/types',
file: 'src/db/types.ts'
},
suffix: 'PO',
ignoreTables: [
'my_table_a',
'my_table_b',
],
overrides: [
{
tableName: 'my_table',
columnName: 'my_actual_tinyint_column',
columnType: 'int',
},
{
tableName: 'my_table',
columnName: 'my_column',
columnType: 'enum',
enumString: `enum('a','b','c')`
}
],
tinyintIsBoolean: true,
})
db
: Required - the database connection and credentials
ssl
is optional. Defaults to the mysql2
default.uri
is specified, the host, port, username and password options are ignored.output
: Required - you should define one of the following 2 options:
dir
: Each type will be output in a separate file in this directory, along with an index.ts
. WARNING: This directory will be emptied and overwritten if it already exists.file
: Each type will be output into this single file. WARNING: This file will be overwritten if it already exists.dir
and file
are provided, file
will take precedence.suffix
: Optional - a string appended to the PascalCase Type name (PO
in the example refers to Persistence Object
but you should use whatever convention you wish)ignoreTables
: Optional - a list of tables to ignore; types won't be generated for theseoverrides
: Optional - a list of columns where the column type in the database is ignored and the specified columnType
is used instead
columnType
can be any of the mysql
column types, e.g. 'varchar'
, 'json'
, etc. Check the file src/getColumnDataType.ts
in this repo for a list
columnType
= 'enum'
, you should specify enumString
enumString
: Optional unless columnType
= 'enum'
. Specify the enum options, for example enum('a','b','c')
will become 'a' | 'b' | 'c'
tinyintIsBoolean
: Optional. Controls if tinyint(1)
should be converted to boolean or kept as number. Default is false
true
: convert to booleanfalse
: keep as number (default)Run this file after running your database migrations. For example with knex
:
package.json
(...)
"scripts": {
"migrate:dev": "npm run build && npx knex migrate:latest && node src/db/updateTypes.js"
}
(...)
You can use env-cmd to load environment variables from a .env
file before running: env-cmd node src/db/updateTypes.js
NOTE: node v18.3 or greater is required to use the CLI as it uses parseArgs
from node:util
under the hood
npx mysql-types-generator [options] --outFile [output file] [database name]
npx mysql-types-generator [options] --outDir [output directory] [database name]
// example
npx mysql-types-generator -h localhost -P 3306 -u myuser -p mypassword --outFile ./src/db/types.ts mydatabase
Most options from the Javascript API are available, run npx mysql-types-generator --help
for details
SET
data type is treated as a simple string because knex
returns a comma-delimited string in queries. You need to manually split it by comma if you want to convert it to an array or javascript Set<>
type.2.0.0
decimal
column to string instead of numberincludeTables
option (optional) to specify a list of tables to generate types forignoreTables
and includeTables
optionsSTDOUT
if no --outDir
or --outFile
option is providedstream
option to ouput to a writeStream
1.0.8
1.0.3
README.md
to indicate that node v18.3 or greater is required to use the CLI1.0.2
uri
(e.g. mysql://user:password@host:port/database
)1.0.1
tinyintIsBoolean
config option in CLI1.0.0
npx
tinyintIsBoolean
config option in JS API0.0.12
README.md
0.0.11
overrides
config option wasn't working properlyoutput
can now be a path to a single file instead of a directoryFAQs
Generate Typescript types from a MySQL database
The npm package mysql-types-generator receives a total of 807 weekly downloads. As such, mysql-types-generator popularity was classified as not popular.
We found that mysql-types-generator demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
/Security News
Threat actors hijacked Toptal’s GitHub org, publishing npm packages with malicious payloads that steal tokens and attempt to wipe victim systems.
Research
/Security News
Socket researchers investigate 4 malicious npm and PyPI packages with 56,000+ downloads that install surveillance malware.
Security News
The ongoing npm phishing campaign escalates as attackers hijack the popular 'is' package, embedding malware in multiple versions.