Gathering detailed insights and metrics for egg-sequelize
Gathering detailed insights and metrics for egg-sequelize
Gathering detailed insights and metrics for egg-sequelize
Gathering detailed insights and metrics for egg-sequelize
npm install egg-sequelize
Typescript
Module System
Min. Node Version
Node Version
NPM Version
63
Supply Chain
94
Quality
81.1
Maintenance
100
Vulnerability
98.2
License
JavaScript (100%)
Total Downloads
1,644,399
Last Day
799
Last Week
5,973
Last Month
29,418
Last Year
346,625
614 Stars
111 Commits
136 Forks
21 Watching
10 Branches
46 Contributors
Latest Version
6.0.0
Package Id
egg-sequelize@6.0.0
Unpacked Size
29.15 kB
Size
9.56 kB
File Count
9
NPM Version
6.12.0
Node Version
10.16.0
Cumulative downloads
Total Downloads
Last day
-18.5%
799
Compared to previous day
Last week
-10.7%
5,973
Compared to previous week
Last month
-3.5%
29,418
Compared to previous month
Last year
20%
346,625
Compared to previous year
3
Sequelize plugin for Egg.js.
NOTE: This plugin just for integrate Sequelize into Egg.js, more documentation please visit http://sequelizejs.com.
1$ npm i --save egg-sequelize 2$ npm install --save mysql2 # For both mysql and mariadb dialects 3 4# Or use other database backend. 5$ npm install --save pg pg-hstore # PostgreSQL 6$ npm install --save tedious # MSSQL
Read the tutorials to see a full example.
config/plugin.js
1exports.sequelize = { 2 enable: true, 3 package: 'egg-sequelize' 4}
conif/config.{env}.js
1exports.sequelize = { 2 dialect: 'mysql', // support: mysql, mariadb, postgres, mssql 3 database: 'test', 4 host: 'localhost', 5 port: 3306, 6 username: 'root', 7 password: '', 8 // delegate: 'myModel', // load all models to `app[delegate]` and `ctx[delegate]`, default to `model` 9 // baseDir: 'my_model', // load all files in `app/${baseDir}` as models, default to `model` 10 // exclude: 'index.js', // ignore `app/${baseDir}/index.js` when load models, support glob and array 11 // more sequelize options 12};
You can also use the connection uri
to configure the connection:
1exports.sequelize = { 2 dialect: 'mysql', // support: mysql, mariadb, postgres, mssql 3 connectionUri: 'mysql://root:@127.0.0.1:3306/test', 4 // delegate: 'myModel', // load all models to `app[delegate]` and `ctx[delegate]`, default to `model` 5 // baseDir: 'my_model', // load all files in `app/${baseDir}` as models, default to `model` 6 // exclude: 'index.js', // ignore `app/${baseDir}/index.js` when load models, support glob and array 7 // more sequelize options 8};
egg-sequelize has a default sequelize options below
1{ 2 delegate: 'model', 3 baseDir: 'model', 4 logging(...args) { 5 // if benchmark enabled, log used 6 const used = typeof args[1] === 'number' ? `[${args[1]}ms]` : ''; 7 app.logger.info('[egg-sequelize]%s %s', used, args[0]); 8 }, 9 host: 'localhost', 10 port: 3306, 11 username: 'root', 12 benchmark: true, 13 define: { 14 freezeTableName: false, 15 underscored: true, 16 }, 17 };
More documents please refer to Sequelize.js
Please put models under app/model
dir by default.
model file | class name |
---|---|
user.js | app.model.User |
person.js | app.model.Person |
user_group.js | app.model.UserGroup |
user/profile.js | app.model.User.Profile |
created_at datetime
, updated_at datetime
.user_id
, comments_count
.Define a model first.
NOTE:
options.delegate
default tomodel
, soapp.model
is an Instance of Sequelize, so you can use methods like:app.model.sync, app.model.query ...
1// app/model/user.js 2 3module.exports = app => { 4 const { STRING, INTEGER, DATE } = app.Sequelize; 5 6 const User = app.model.define('user', { 7 login: STRING, 8 name: STRING(30), 9 password: STRING(32), 10 age: INTEGER, 11 last_sign_in_at: DATE, 12 created_at: DATE, 13 updated_at: DATE, 14 }); 15 16 User.findByLogin = async function(login) { 17 return await this.findOne({ 18 where: { 19 login: login 20 } 21 }); 22 } 23 24 // don't use arraw function 25 User.prototype.logSignin = async function() { 26 return await this.update({ last_sign_in_at: new Date() }); 27 } 28 29 return User; 30}; 31
Now you can use it in your controller:
1// app/controller/user.js 2class UserController extends Controller { 3 async index() { 4 const users = await this.ctx.model.User.findAll(); 5 this.ctx.body = users; 6 } 7 8 async show() { 9 const user = await this.ctx.model.User.findByLogin(this.ctx.params.login); 10 await user.logSignin(); 11 this.ctx.body = user; 12 } 13}
Define all your associations in Model.associate()
and egg-sequelize will execute it after all models loaded. See example below.
egg-sequelize support load multiple datasources independently. You can use config.sequelize.datasources
to configure and load multiple datasources.
1// config/config.default.js 2exports.sequelize = { 3 datasources: [ 4 { 5 delegate: 'model', // load all models to app.model and ctx.model 6 baseDir: 'model', // load models from `app/model/*.js` 7 database: 'biz', 8 // other sequelize configurations 9 }, 10 { 11 delegate: 'admninModel', // load all models to app.adminModel and ctx.adminModel 12 baseDir: 'admin_model', // load models from `app/admin_model/*.js` 13 database: 'admin', 14 // other sequelize configurations 15 }, 16 ], 17};
Then we can define model like this:
1// app/model/user.js 2module.exports = app => { 3 const { STRING, INTEGER, DATE } = app.Sequelize; 4 5 const User = app.model.define('user', { 6 login: STRING, 7 name: STRING(30), 8 password: STRING(32), 9 age: INTEGER, 10 last_sign_in_at: DATE, 11 created_at: DATE, 12 updated_at: DATE, 13 }); 14 15 return User; 16}; 17 18// app/admin_model/user.js 19module.exports = app => { 20 const { STRING, INTEGER, DATE } = app.Sequelize; 21 22 const User = app.adminModel.define('user', { 23 login: STRING, 24 name: STRING(30), 25 password: STRING(32), 26 age: INTEGER, 27 last_sign_in_at: DATE, 28 created_at: DATE, 29 updated_at: DATE, 30 }); 31 32 return User; 33};
If you define the same model for different datasource, the same model file will be excute twice for different database, so we can use the secound argument to get the sequelize instance:
1// app/model/user.js 2// if this file will load multiple times for different datasource 3// we can use the secound argument to get the sequelize instance 4module.exports = (app, model) => { 5 const { STRING, INTEGER, DATE } = app.Sequelize; 6 7 const User = model.define('user', { 8 login: STRING, 9 name: STRING(30), 10 password: STRING(32), 11 age: INTEGER, 12 last_sign_in_at: DATE, 13 created_at: DATE, 14 updated_at: DATE, 15 }); 16 17 return User; 18};
By default, egg-sequelize will use sequelize@5, you can cusomize sequelize version by pass sequelize instance with config.sequelize.Sequelize
like this:
1// config/config.default.js 2exports.sequelize = { 3 Sequelize: require('sequelize'), 4};
1// app/model/post.js 2 3module.exports = app => { 4 const { STRING, INTEGER, DATE } = app.Sequelize; 5 6 const Post = app.model.define('Post', { 7 name: STRING(30), 8 user_id: INTEGER, 9 created_at: DATE, 10 updated_at: DATE, 11 }); 12 13 Post.associate = function() { 14 app.model.Post.belongsTo(app.model.User, { as: 'user' }); 15 } 16 17 return Post; 18};
1// app/controller/post.js
2class PostController extends Controller {
3 async index() {
4 const posts = await this.ctx.model.Post.findAll({
5 attributes: [ 'id', 'user_id' ],
6 include: { model: this.ctx.model.User, as: 'user' },
7 where: { status: 'publish' },
8 order: 'id desc',
9 });
10
11 this.ctx.body = posts;
12 }
13
14 async show() {
15 const post = await this.ctx.model.Post.findByPk(this.params.id);
16 const user = await post.getUser();
17 post.setDataValue('user', user);
18 this.ctx.body = post;
19 }
20
21 async destroy() {
22 const post = await this.ctx.model.Post.findByPk(this.params.id);
23 await post.destroy();
24 this.ctx.body = { success: true };
25 }
26}
We strongly recommend you to use Sequelize - Migrations to create or migrate database.
This code should only be used in development.
1// {app_root}/app.js 2module.exports = app => { 3 if (app.config.env === 'local' || app.config.env === 'unittest') { 4 app.beforeStart(async () => { 5 await app.model.sync({force: true}); 6 }); 7 } 8};
Using sequelize-cli to help manage your database, data structures and seed data. Please read Sequelize - Migrations to learn more infomations.
Please open an issue here.
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
0 existing vulnerabilities detected
Reason
license file detected
Details
Reason
Found 13/30 approved changesets -- score normalized to 4
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
Reason
security policy file not detected
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Score
Last Scanned on 2024-12-16
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More