如何在 SailsJs 中使用 Sequelize [英] How to use Sequelize in SailsJs

查看:11
本文介绍了如何在 SailsJs 中使用 Sequelize的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Waterline 是一个出色的 ORM,但我注意到有许多功能尚未出现在 Waterline 上,但 Sequelize 已经具备.所以我决定切换到续集,但仍然使用 Sails 来处理其他事情.我有搜索教程如何切换到续集但没有.如何在 Sails Js 中替换 Waterline 以进行续集?

Waterline is an excellent ORM but I noticed that there are many features that are not present yet on waterline but Sequelize already have. So I have decided to switch to sequelize but still using Sails for the others things. I have search tutorial how to switch to sequelize but nothing. How can I replace Waterline for sequelize in sails Js?

推荐答案

sequelize我也做了,最近有两个项目出来了,所以我想宣布一下.

I've moved forward with sequelize as well, there are two project that came out really recently, so i would like to announce them.

sails-hook-sequelize:

它遵循 Manuel Darveau 的回答,它将获取您的所有模型,通过 sequelize 导入并将您的模型作为全局变量提供,您可以使用与 migrate: 'drop'<相同的方式强制 sequelize 同步/代码>

It follows the answer by Manuel Darveau, it will fetch all your models, import through sequelize and serves your models as a global variables, you can force the sequelize syncronization with the same way with migrate: 'drop'

sails-hook-sequelize-blueprints

Sails 蓝图为我节省了大量时间,因此我编写了一个用于 sequelize 的 fork,它的工作方式与原始蓝图相同,并且您仍将拥有相同的蓝图配置,例如 restshortcutsprefix 等,由于 waterline 使用 populateEach() 函数填充模型,它使用 include: [{ all: true }] 结果是一样的.

Sails blueprints has saved me a LOT of time, so i've wrote a fork to work with sequelize, it work the same way than original blueprints, and you'll still have the same blueprints configurations such as rest, shortcuts, prefix and so on, since waterline populate models with populateEach() function, it uses include: [{ all: true }] which the result is the same.

一个完整的例子:

$ npm install sails-hook-sequelize
$ npm install sails-hook-sequelize-blueprints
$ npm install sequelize
$ npm install pg pg-hstore
$ npm install continuation-local-storage

.sailsrc

"hooks": {
    "blueprints": false,
    "orm": false,
    "pubsub": false
}

connections.js

connections.js

somePostgresqlServer: {
    user: 'postgres',
    password: '',
    database: 'database',
    dialect: 'postgres',
    options: {
        dialect: 'postgres',
        host   : 'localhost',
        port   : 5432,
        logging: true
   }
}

你的模型定义

// user.js
module.exports = {
  attributes: {
    name: {
      type: Sequelize.STRING,
      allowNull: false
    },
    age: {
      type: Sequelize.INTEGER
    }
  },
  associations: function() {
    user.hasMany(image, {
      foreignKey: {
        name: 'owner',
        allowNull: false
      }
    });
  },
  options: {
    tableName: 'user',
    classMethods: {},
    instanceMethods: {},
    hooks: {}
  }
};

就是这样.

这篇关于如何在 SailsJs 中使用 Sequelize的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆