using an enviroment variable for local sequelize configuration

you should change config.json file to a config.js module and make sure to require the dotenv at the very top. require(‘dotenv’).config(); // this is important! module.exports = { “development”: { “username”: process.env.DB_USERNAME, “password”: process.env.DB_PASSWORD, “database”: process.env.DB_DATABASE, “host”: process.env.DB_HOST, “dialect”: “mysql” }, “test”: { “username”: “root”, “password”: null, “database”: “database_test”, “host”: “127.0.0.1”, “dialect”: “mysql” }, “production”: …

Read more

How to update with Sequelize with ‘NOW()’ on a timestamp?

You can use Sequelize.fn to wrap it appropriately: instance.updateAttributes({syncedAt: sequelize.fn(‘NOW’)}); Here’s a full working example: ‘use strict’; var Sequelize = require(‘sequelize’); var sequelize = new Sequelize(/*database*/’test’, /*username*/’test’, /*password*/’test’, {host: ‘localhost’, dialect: ‘postgres’}); var model = sequelize.define(‘model’, { syncedAt: {type: Sequelize.DATE} }); sequelize.sync({force: true}) .then(function () { return model.create({}); }) .then(function () { return model.find({}); }) …

Read more

Using group by and joins in sequelize

This issue has been fixed on Sequelize 3.0.1, the primary key of the included models must be excluded with attributes: [] and the aggregation must be done on the main model (infos in this github issue). Thus for my use case, the code is the following models.contracts.findAll({ attributes: [‘id’, [models.sequelize.fn(‘sum’, models.sequelize.col(‘payments.payment_amount’)), ‘total_cost’]], include: [ { …

Read more

Sequelize Create Database

I may have a reasonable solution. I am sure it could be written more cleanly though. For this example I’m using Postgres, the answer would be slightly different for MySQL. I’m heavily borrowing from this answer: node-postgres create database In database.js I have the following init function var Sequelize = require(‘sequelize’), pg = require(‘pg’); module.exports.init …

Read more