semo-plugin-sequelize
A Semo plugin to privide sequelize integration in Semo way.
CLI Usage
npm i semo-plugin-sequelize
semo sql cli <dbKey> Connect with pgcli/mycli/sqlite3 connector
semo sql describe <dbKey> <tableName> Sequelize db table describe [aliases: d, desc]
semo sql generate <dbKey> <tableName> [fieldName] Sequelize db migration generator [aliases: g, create]
semo sql list <dbKey> List all table of specific database [aliases: l, ls]
semo sql query <dbKey> <sql> Execute SQL, only support SELECT [aliases: q]
Programming way
npm i @semo/core semo-plugin-sequelize
semo init
Add migration config and dbConfig in .semorc.yml
# Useful for migrationmigrationMakeDir: src/migrationsmigrationDir: lib/migrations semo-plugin-sequelize: defaultConnection: dbKeyStyle1 # if provided, commands do not need --db-key connection: dbKeyStyle1: database: d8 username: d8 password: d8 host: localhost port: 5432 dialect: postgres dbKeyStyle2: postgres://d8:d8@localhost:5432/d8
Except adding db connection info in .semorc.yml, also we can declare that info in hook_sequelize_connection.
// src/hooks/index.ts const export hook_sequelize_connection = async { return dbKeyStyle1: database: 'd8' username: 'd8' password: 'd8' host: 'localhost' port: 5432 dialect: 'postgres' }
You may have noticed, there is a async
for the hook_sequelize_connection, it's useful for fetching db config from config center.
This plugin support 3 dialects: mysql
, postgres
, sqlite
, and support 2 style's config format: literal object and DSN.
Add .sequelizerc file for migration
const path = ; const SequelizeOps = processargv // Operations with careif SequelizeOps > -1 && processenvNODE_ENV === 'production' throw 'Sequelize undo disabled on production' moduleexports = 'config': path 'migrations-path': path
Add config.db.js for migration to connnect to db
/** * @file * * For Sequelize Cli db connection */ moduleexports = sequelize
Here, sequelize-cli only can choose to use one database to migrate.
Access db instance and table model.
const sequelize = await Utilsconst Op Sequelize = sequelizeconst db models: YourModel = await sequelize
To use cli command, you need to install related cli tools. This is an example on MacOS
brew install pgcli # For PostgresSQL brew install mycli # For MySQL
Access database in REPL
This plugin has expose objects and methods to REPL, so you can assess db data from REPL.
$ semo repl --hook>>> const models: YourModel = await Semosequelize>>> YourModel2
License
MIT