Node + Sequelize: How to check if item exists before adding? (async confusion)

Author.count isn’t really needed, unless you need the count. See findOrCreate(). With findOrCreate() you could have the following. (Edited trex005’s snippet for this) async.eachSeries(authors, function(item, callback) { Author.sync().then(function() { Author.findOrCreate({ where: { name: item.trim() }, defaults: { // set the default properties if it doesn’t exist name: item.trim() } }).then(function(result) { var author = result[0], … Read more

How to insert initial data using sequelize migrations/seeds?

You can use next: const City = sequelize.define(‘city’, { name: { type: Sequelize.STRING }, order_: { type: Sequelize.INTEGER } }); City.sync().then(() => { City.create({ name: ‘Neuquen’, order_: 0 }); City.create({ name: ‘General Roca’, order_: 1 }); }); Or read about “migrations” at http://docs.sequelizejs.com/en/latest/docs/migrations/

GraphQL queries with tables join using Node.js

The concept you are refering to is called batching. There are several libraries out there that offer this. For example: Dataloader: generic utility maintained by Facebook that provides “a consistent API over various backends and reduce requests to those backends via batching and caching” join-monster: “A GraphQL-to-SQL query execution layer for batch data fetching.”

How to prevent sql-injection in nodejs and sequelize? [closed]

Sequelize escapes replacements, which avoids the problem at the heart of SQL injection attacks: unescaped strings. It also supports binding parameters when using SQLite or PostgreSQL, which alleviates the risk further by sending the parameters to the database separately to the query, as documented here: Bind parameters are like replacements. Except replacements are escaped and … Read more

Want to get crystal clear about NodeJS app structure (Full JavaScript Stack) [closed]

Alright, this is a pretty broad question and I’m definitely no expert, but I’ll do my best here. TL;DR routes are controllers that tell what logic to execute when a user navigates their browser to a certain path within your app, including which views to render and what data to send to those views models … Read more

Check mysql connection in sequelize

As of latest version of Sequelize (i.e. 3.3.2), authenticate can be used to check the connection: var sequelize = new Sequelize(“db”, “user”, “pass”); sequelize.authenticate().then(function(errors) { console.log(errors) }); authenticate simply runs SELECT 1+1 AS result query to check the db connection. UPDATE: Errors by the newest API need to be handled in catch: sequelize .authenticate() .then(() … Read more

Error [ERR_UNSUPPORTED_DIR_IMPORT]: Directory import when attempting to start Nodejs App locally

Explicitly, point to your main file (usually index.js). E. g. import … from ‘./models’ // ❌ import … from ‘./models/index.js’ // ✅ Alternative way (see the below UPDATE): Use –experimental-specifier-resolution=node flag. For example: node –experimental-specifier-resolution=node main.js See: https://nodejs.org/api/esm.html#esm_customizing_esm_specifier_resolution_algorithm UPDATE (Node.js v19+): Node.js has removed the –experimental-specifier-resolution flag. Its functionality can now be achieved via custom … Read more

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