ECONNREFUSED for Postgres on nodeJS with dockers

Your DATABASE_URL refers to 127.0.0.1, which is the loopback adapter (more here). This means “connect to myself”. When running both applications (without using Docker) on the same host, they are both addressable on the same adapter (also known as localhost). When running both applications in containers they are not both on localhost as before. Instead … Read more

Sequelize Where statement with date

Just like Molda says, you can use $gt, $lt, $gte or $lte with a date: model.findAll({ where: { start_datetime: { $gte: moment().subtract(7, ‘days’).toDate() } } }) If you’re using v5 of Sequelize, you’ve to include Op because the key was moved into Symbol const { Op } = require(‘sequelize’) model.findAll({ where: { start_datetime: { [Op.gte]: … Read more

sequelize table without column ‘id’

If you don’t define a primaryKey then sequelize uses id by default. If you want to set your own, just use primaryKey: true on your column. AcademyModule = sequelize.define(‘academy_module’, { academy_id: { type: DataTypes.INTEGER, primaryKey: true }, module_id: DataTypes.INTEGER, module_module_type_id: DataTypes.INTEGER, sort_number: DataTypes.INTEGER, requirements_id: DataTypes.INTEGER }, { freezeTableName: true });

Sequelize OR condition object

Seems there is another format now where: { LastName: “Doe”, $or: [ { FirstName: { $eq: “John” } }, { FirstName: { $eq: “Jane” } }, { Age: { $gt: 18 } } ] } Will generate WHERE LastName=”Doe” AND (FirstName=”John” OR FirstName=”Jane” OR Age > 18) See the doc: http://docs.sequelizejs.com/en/latest/docs/querying/#where

How to auto generate migrations with Sequelize CLI from Sequelize models?

If you don’t want to recreate your model from scratch, you can manually generate a migration file using the following CLI command: sequelize migration:generate –name [name_of_your_migration] This will generate a blank skeleton migration file. While it doesn’t copy your model structure over to the file, I do find it easier and cleaner than regenerating everything. … Read more

sequelize findAll sort order in nodejs

In sequelize you can easily add order by clauses. exports.getStaticCompanies = function () { return Company.findAll({ where: { id: [46128, 2865, 49569, 1488, 45600, 61991, 1418, 61919, 53326, 61680] }, // Add order conditions here…. order: [ [‘id’, ‘DESC’], [‘name’, ‘ASC’], ], attributes: [‘id’, ‘logo_version’, ‘logo_content_type’, ‘name’, ‘updated_at’] }); }; See how I’ve added the … Read more

How to make Sequelize use singular table names

The docs state that you can use the property freezeTableName. Please take a look at this example: var Bar = sequelize.define(‘Bar’, { /* bla */ }, { // don’t add the timestamp attributes (updatedAt, createdAt) timestamps: false, // don’t delete database entries but set the newly added attribute deletedAt // to the current date (when … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)