express-example

A proposal for the usage of Sequelize within an Express.JS application.

Github星跟蹤圖

Express Example

This repository demonstrates the usage of Sequelize within an Express application.
The implemented logic is a simple task tracking tool.

Deploy

Starting App

Without Migrations

npm install
npm start

With Migrations

npm install
node_modules/.bin/sequelize db:migrate
npm start

This will start the application and create an sqlite database in your app dir.
Just open http://localhost:3000.

Running Tests

We have added some Mocha based test. You can run them by npm test

Setup in Details

In order to understand how this application has been built, you can find the
executed steps in the following snippet. You should be able to adjust those
steps according to your needs. Please note that the view and the routes aren't
described. You can find those files in the repo.

Express Setup

First we will create a bare Express App using express-generator Express Generator

# install express generator globally
npm install -g express-generator

# create the sample app
express express-example
cd express-example

# install all node modules
npm install

Sequelize Setup

Now we will install all sequelize related modules.

# install ORM , CLI and SQLite dialect
npm install sequelize sequelize-cli sqlite3

# generate models
node_modules/.bin/sequelize init
node_modules/.bin/sequelize model:create --name User --attributes username:string
node_modules/.bin/sequelize model:create --name Task --attributes title:string

We are using .sequelizerc setup change config path for migrations. You can read more about this in migration docs

// .sequelizerc
const path = require('path');

module.exports = {
  'config': path.resolve('config', 'config.js')
}

You will now have a basic express application with some additional directories
(config, models, migrations). Also you will find two migrations and models.
One for the User and one for the Task.

In order to associate the models with each other, you need to change the models
like this:

// task.js
// ...
  Task.associate = function(models) {
    // Using additional options like CASCADE etc for demonstration
    // Can also simply do Task.belongsTo(models.User);
    Task.belongsTo(models.User, {
      onDelete: "CASCADE",
      foreignKey: {
        allowNull: false
      }
    });
  }
// ...
// user.js
// ...
  User.associate = function(models) {
    User.hasMany(models.Task);
  }
// ...

This association will create an attribute UserId in Task model. We have to amend our create-task migration and add this column.

// xxxxxxx-create-task.js
// ...
  UserId: {
    type: Sequelize.INTEGER,
    onDelete: "CASCADE",
    allowNull: false,
    references: {
      model: 'Users',
      key: 'id'
    }
  }
// ...

If you want to use the automatic table creation that sequelize provides,
you have to adjust the bin/www file to this:

#!/usr/bin/env node

var app = require('../app');
var debug = require('debug')('init:server');
var http = require('http');
var models = require("../models");

var port = normalizePort(process.env.PORT, '3000');
app.set('port', port);

var server = http.createServer(app);

// sync() will create all table if they doesn't exist in database
models.sequelize.sync().then(function () {
  server.listen(port);
  server.on('error', onError);
  server.on('listening', onListening);
});

function normalizePort(val) { /* ... */ }
function onError(error) { /* ... */ }
function onListening() { /* ... */ }

And finally you have to adjust the config/config.js to fit your environment.
Once thats done, your database configuration is ready!

主要指標

概覽
名稱與所有者sequelize/express-example
主編程語言JavaScript
編程語言JavaScript (語言數: 1)
平台
許可證MIT License
所有者活动
創建於2013-12-08 20:07:26
推送於2022-03-06 07:05:56
最后一次提交2022-03-06 08:05:56
發布數0
用户参与
星數2.5k
關注者數91
派生數768
提交數81
已啟用問題?
問題數63
打開的問題數5
拉請求數37
打開的拉請求數2
關閉的拉請求數13
项目设置
已啟用Wiki?
已存檔?
是復刻?
已鎖定?
是鏡像?
是私有?