Tag: sequelize.js

Sequelize包含在数据库中计算的计算字段?

在Node.js中使用Sequelize是否有可能定义一个在检索数据时在数据库中计算的字段? 我知道我可以添加一个自定义getter到DAO来计算节点中的东西,但只能在已经加载的数据上工作。 我的目标是定义在数据库中运行的计算,因此可以使用子查询来计算使用其他数据。 例如,我最好喜欢这样做: Products = Sequelize.define('Product', { 'productId': { type: Sequelize.INTEGER, primaryKey: true, autoIncrement: true }, 'isProductActive': Sequelize.INTEGER, 'productName': Sequelize.STRING, 'productPrice': Sequelize.DECIMAL, // This is what I don't think Sequelize supports // but is what I ideally want to do… 'totalSales': { type: Sequelize.DECIMAL, subQuery: "SELECT SUM(OD.TotalPrice) FROM OrderDetails OD WHERE OD.productID = Products.productID" […]

Sequelize同步与迁移

我正在学习Sequelize,我希望对于同步和迁移进行一些解释。 据我所知,同步将根据我的模型模式创build缺失的表,但我也读过同步是为了初始化数据库,而迁移是为了生产。 如果是这种情况,快速示例显示从bin/www调用同步 。 这是不应该在生产中使用的东西吗? 作为这个的延伸,如果我不在生产中使用同步,你如何应用模型关联? 我是否需要手动将它们添加到迁移? 本质上,我要求解释这两个概念是如何一起工作的。 谢谢

无法使用Sequelize将Schema(postgresql)中的模型关联起来

我正在尝试使用Postgresql(9.3)的Sequelize(1.7)并尝试使用模式。 我在数据库中创build了外部模式,但在我的模型中使用它们。 当我试图做模型关联时,Sequelize抱怨这个关系不存在。 当我重新定义模型使用PostgreSQL的默认“公共”模式时,它工作正常。 这里是testing用例的模型定义和代码 var Promise = require("bluebird"); var Sequelize = require("sequelize"); var _ = require('lodash-node'); var schemaName ="test_schema"; var sequelize = new Sequelize( c.database,c.username, c.password,{dialect :'postgres', port:'5432',schema:schemaName}); var User =sequelize.define('User',{ id:{ type: Sequelize.INTEGER,primaryKey:true, autoIncrement:true}, name:{type:Sequelize.TEXT,allowNull:false}, nick_name:{type:Sequelize.TEXT,allowNull:true}, date_of_joining:{type:Sequelize.DATE,allowNull:false,defaultValue:Sequelize.NOW} }, { tableName:"user", underscored:true, timestamps:false } ); var Task = sequelize.define( 'Task', { id:{ type: Sequelize.INTEGER,primaryKey:true, […]

sequelize.js – 按ID查找并返回结果

我有一个function, var findUserDevice = function(userDeviceId){ var device = db.DeviceUser.find({ where: { id: userDeviceId } }).then(function(device) { if (!device) { return 'not find'; } return device.dataValues; }); }; 但是这个函数不会返回任何东西… var UserDevice = findUserDevice(req.body.deviceUserId); console.log(UserDevice);// undefined

查询连接表,但不会在Sequelize中获取两个关联

考虑以下模型: var User = sequelize.define('User', { _id:{ type: Datatypes.INTEGER, allowNull: false, primaryKey: true, autoIncrement: true }, name: Datatypes.STRING, email:{ type: Datatypes.STRING, unique: { msg: 'Email Taken' }, validate: { isEmail: true } } }); var Location= sequelize.define('Location', { _id:{ type: Datatypes.INTEGER, allowNull: false, primaryKey: true, autoIncrement: true }, name: Datatypes.STRING, address: type: Datatypes.STRING }); Location.belongsToMany(User, […]

交易restAPI

我正在一个项目,我需要实现事务回滚PostgreSQL数据库durin REST操作使用Nodejs。 我已经分别为GET,PUT和POST方法实现了事务。 我需要使用交易一次还是我正确的轨道? 在此先感谢您的帮助。 我想确保我的数据库回滚数据,如果需要的话。 我正在使用pg-promise库来获得结果。 db.tx(t => { return t.batch([ t.query('UPDATE users SET active = $1 WHERE id = $2', [true, 123]), t.query('INSERT INTO audit(event, id) VALUES($1, $2)', ['activate', 123]) ]); }) .then(data => { // success; }) .catch(error => { // error; }); 或者,我应该在下面实施方法吗? module.exports = { // if you can run […]

Node + Sequelize:如何在添加之前检查项目是否存在? (asynchronous混淆)

不幸的是,对于节点的asynchronous/同步执行,我对节点很陌生。 我正在使用节点,与sqlite和async.js sequelize。 我有一系列的Articles ,每个Articles都有一些Authors 。 对于每篇Article每位Authors ,我想检查Author存在。 如果没有,创build它。 问题是,在最初运行时,重复作者正在创build,我认为由于asynchronousfunction导致检查存在的问题。 例如,用数组: authors = ['A. Test', 'B. Test', 'C. Test', 'A. Test'] authors = ['A. Test', 'B. Test', 'C. Test', 'A. Test'] 和代码: async.each(authors, function(item, callback){ Author.sync().then(function(){ Author.count({ where: {name: item.trim()} }).then(function(count){ if (count != 0) { console.log('Author already exists') } else { console.log('Creating author…') Author.create({ […]

将外键作为复合主键encryption

可以将两个外键定义为模型的复合主键? 用户只能是一个家庭的成员,一个家庭可以有很多成员,家庭成员表需要用户和家庭的参考 const User = sequelize.define( 'User', { id: { type: dataTypes.INTEGER.UNSIGNED, autoIncrement: true, primaryKey: true }, name: { type: dataTypes.STRING(30) }, email: { type: dataTypes.STRING(30) } … }, { classMethods: { associate(models) { User.hasOne(models.FamilyMember, { foreignKey: 'user_id' } } } } ) const Family = sequelize.define( 'Family', { name: { type: dataTypes.STRING(30) } }, […]

如何将本地date时间存储在续集中?

我使用的是续集版本2.0.0-rc8。 当我存储本地date时间,它始终存储UTCdate时间。 是否有可能使用sequelize存储本地date时间?

续编upsert

我需要得到插入/更新logging的id时使用.upsert()在sequelize。 .upsert()返回一个布尔值,指示行是否被创build或更新。 return db.VenueAddress.upsert({ addressId:address.addressId, venueId: venue.venueId, street: address.street, zipCode: address.zipCode, venueAddressDeletedAt: null }).then(function(test){ //test returned here as true or false how can i get the inserted id here so i can insert data in other tables using this new id? });