Node Mysql Serialize 'ERR_BUFFER_OUT_OF_BOUNDS' - mysql

so when i try to run the project i get the above mentioned error. I am simply creating 2 tables and then running serialize.sync(). I am new to both node, mysql, and serielize so i have absolutely no clue why is this happening. Also if you wanna see eveything here is the github project: https://github.com/EmilBabazade/BooksBLog/tree/migrate_to_mysql
I also tried deleting blog_books_dev with mysql cli and running again, it gives the same error regardless of wether the database exist or not.
Here is what error looks like:
[nodemon] restarting due to changes...
[nodemon] starting `node index.js`
server is running on port 3001
tedious deprecated The default value for `config.options.trustServerCertificate` will change from `true` to `false` in the next major version of `tedious`. Set the value to `true` or `false` explicitly to silence this message. node_modules/sequelize/lib/dialects/mssql/connection-manager.js:63:26
tedious deprecated In the next major version of `tedious`, creating a new `Connection` instance will no longer establish a connection to the server automatically. Please use the new `connect` helper function or call the `.connect` method on the newly created `Connection` object to silence this message. internal/process/task_queues.js:79:11
internal/buffer.js:75
throw new ERR_BUFFER_OUT_OF_BOUNDS();
^
RangeError [ERR_BUFFER_OUT_OF_BOUNDS]: Attempt to access memory outside buffer bounds
at boundsError (internal/buffer.js:75:11)
at Buffer.readUInt8 (internal/buffer.js:243:5)
at Packet.type (/home/emil/emil/projects/blogBooks/node_modules/tedious/lib/packet.js:143:24)
at IncomingMessageStream.processBufferedData (/home/emil/emil/projects/blogBooks/node_modules/tedious/lib/incoming-message-stream.js:72:26)
at IncomingMessageStream._transform (/home/emil/emil/projects/blogBooks/node_modules/tedious/lib/incoming-message-stream.js:107:10)
at IncomingMessageStream.Transform._read (/home/emil/emil/projects/blogBooks/node_modules/tedious/node_modules/readable-stream/lib/_stream_transform.js:177:10)
at IncomingMessageStream.Transform._write (/home/emil/emil/projects/blogBooks/node_modules/tedious/node_modules/readable-stream/lib/_stream_transform.js:164:83)
at doWrite (/home/emil/emil/projects/blogBooks/node_modules/tedious/node_modules/readable-stream/lib/_stream_writable.js:409:139)
at writeOrBuffer (/home/emil/emil/projects/blogBooks/node_modules/tedious/node_modules/readable-stream/lib/_stream_writable.js:398:5)
at IncomingMessageStream.Writable.write (/home/emil/emil/projects/blogBooks/node_modules/tedious/node_modules/readable-stream/lib/_stream_writable.js:307:11)
at Socket.ondata (_stream_readable.js:708:22)
at Socket.emit (events.js:315:20)
at addChunk (_stream_readable.js:297:12)
at readableAddChunk (_stream_readable.js:273:9)
at Socket.Readable.push (_stream_readable.js:214:10)
at TCP.onStreamRead (internal/stream_base_commons.js:186:23) {
code: 'ERR_BUFFER_OUT_OF_BOUNDS'
}
[nodemon] app crashed - waiting for file changes before starting...
Here is the code for database:
const Sequelize = require('sequelize')
const User = require('./models/user')
const Blog = require('./models/blog')
const logger = require('../utils/logger')
const dbConfigs = require('./config')
let sequelize
if (process.env.NODE_ENV === 'production') {
sequelize = new Sequelize(dbConfigs.production)
} else if (process.env.NODE_ENV === 'development') {
sequelize = new Sequelize(dbConfigs.development)
} else if (process.env.NODE_ENV === 'test') {
sequelize = new Sequelize(dbConfigs.test)
} else {
logger.error('error when connecting to db, invalid or empty NODE_ENV')
// throw something ..?
}
const userModel = sequelize.define('user', User)
const blogModel = sequelize.define('blog', Blog)
userModel.hasMany(blogModel)
blogModel.belongsTo(userModel)
module.exports = {
sequelize,
User: userModel,
Blog: blogModel,
}
Code for user model:
const Sequelize = require('sequelize')
module.exports = {
id: {
type: Sequelize.INTEGER,
primaryKey: true,
autoIncrement: true,
},
username: {
type: Sequelize.STRING,
allowNull: false,
unique: true,
validate: {
len: [5, 20],
},
},
name: Sequelize.STRING,
passwordHash: {
type: Sequelize.STRING,
allowNull: false,
},
isAdmin: {
type: Sequelize.BOOLEAN,
defaultValue: false,
},
}
code for blog model:
const Sequelize = require('sequelize')
module.exports = {
id: {
type: Sequelize.INTEGER,
primaryKey: true,
autoIncrement: true,
},
title: {
type: Sequelize.STRING,
AllowNull: false,
validate: {
len: [5, 20],
},
},
content: {
type: Sequelize.STRING,
AllowNull: false,
validate: {
len: [20, 500],
},
},
date: {
type: Sequelize.DATE,
defaultValue: Sequelize.NOW,
},
}
and the options for new Serialize(options) (i deleted password because its my main password for everything irl):
const production = 'link to production db'
const development = {
host: 'localhost',
database: 'blog_books_dev',
username: 'root',
password: '',
dialect: 'mssql',
// open mysql command line and type SHOW GLOBAL VARIABLES LIKE 'PORT';
// to see waht port mysql is running on
port: '3306',
}
const test = {
host: 'localhost',
database: 'blog_books_test',
username: 'root',
password: '',
dialect: 'mssql',
port: '3306',
}
module.exports = { production, development, test }

I wrote mssql instead of mysql:
const development = {
host: 'localhost',
database: 'blog_books_dev',
username: 'root',
password: '',
dialect: 'mssql',
// open mysql command line and type SHOW GLOBAL VARIABLES LIKE 'PORT';
// to see waht port mysql is running on
port: '3306',
}
should be:
const development = {
host: 'localhost',
database: 'blog_books_dev',
username: 'root',
password: '',
dialect: 'mysql', // THIS WAS THE PROBLEM
// open mysql command line and type SHOW GLOBAL VARIABLES LIKE 'PORT';
// to see waht port mysql is running on
port: '3306',
}

Related

Unhandled rejection SequelizeDatabaseError: No database selected

I'm using sequelize in my Express.js application with MySQL.
All database credentials are stored in .env file and are correct:
DATABASE_NAME= mydb
DATABASE_USERNAME= root
DATABASE_PASSWORD= pass
DATABASE_HOST= localhost
DATABASE_PORT= 3306
database.js file:
require('dotenv').config();
const Sequelize = require('sequelize');
module.exports = new Sequelize(
process.env.DATABAST_NAME,
process.env.DATABASE_USERNAME,
process.env.DATABASE_PASSWORD,
{
port: process.env.DATABASE_PORT,
host: process.env.DATABASE_HOST,
dialect: 'mysql'
}
);
In user model:
const Sequelize = require('sequelize');
const db = require('../config/database');
module.exports = db.define('user', {
username: {
type: Sequelize.STRING,
unique: true,
primaryKey: true
},
email: {
type: Sequelize.STRING,
unique: true
},
password: {
type: Sequelize.STRING
},
}, {
freezeTableName: true,
timestamps: false
});
user.findAll().then(data=>{
console.log(data)
});
Database 'mydb' has only one table 'user', with the application start, I got an error:
Executing (default): SELECT `username`, `email`, `password` FROM `user` AS `user`;
Unhandled rejection SequelizeDatabaseError: No database selected
at Query.formatError (C:\Users\me\WebstormProjects\Backend\node_modules\sequelize\lib\dialects\mysql\query.js:241:16)
at Query.handler [as onResult] (C:\Users\me\WebstormProjects\Backend\node_modules\sequelize\lib\dialects\mysql\query.js:48:23)
at Query.execute (C:\Users\me\WebstormProjects\Backend\node_modules\mysql2\lib\commands\command.js:30:14)
at Connection.handlePacket (C:\Users\me\WebstormProjects\Backend\node_modules\mysql2\lib\connection.js:449:32)
at PacketParser.Connection.packetParser.p [as onPacket] (C:\Users\me\WebstormProjects\Backend\node_modules\mysql2\lib\connection.js:72:12)
at PacketParser.executeStart (C:\Users\me\WebstormProjects\Backend\node_modules\mysql2\lib\packet_parser.js:75:16)
at Socket.Connection.stream.on.data (C:\Users\me\WebstormProjects\Backend\node_modules\mysql2\lib\connection.js:79:25)
at Socket.emit (events.js:189:13)
at addChunk (_stream_readable.js:284:12)
at readableAddChunk (_stream_readable.js:265:11)
at Socket.Readable.push (_stream_readable.js:220:10)
at TCP.onStreamRead [as onread] (internal/stream_base_commons.js:94:17)
I tried to test .env file with below code, and everything is readable:
var config = require('dotenv').config();
console.log(config)
I tried to add database name in front of 'user', in db.defind(), and i got the same issue.
Please, if you have any suggestions, let me know.
Your database name is empty when you're initializing Sequelize. It looks like you spelled DATABASE wrong.
module.exports = new Sequelize(
process.env.DATABAST_NAME, // <--- This is an empty string or null.
process.env.DATABASE_USERNAME,
process.env.DATABASE_PASSWORD,
{
port: process.env.DATABASE_PORT,
host: process.env.DATABASE_HOST,
dialect: 'mysql'
}
);
Install dotenv package from npm with command:
npm install dotenv
Require dotenv in your main.ts file which is located inside the 'src' folder by adding the below lines:
import * as dotenv from "dotenv";
dotenv.config();
Run your project once again.

unable to set multipleStatements to true in knex node express

I have inherited an node.js app for RESTFUL api, which uses knex pooling to connect to mysql. I have a nned to perform multiple queries in single function (statement), and as I understand, for that I need multipleStatemnt to be set to truu in knex pooling settings. I have done that:
const connection = (user, password) =>
knex({
client: "mysql",
connection: {
multipleStatements: true,
host: process.env.MYSQL_IP,
port: process.env.MYSQL_PORT,
user,
password,
database: "",
dateStrings: true,
}
});
However, this does not seem to get applied and I cannot execute multiple statements in single query (as per documentation in mysql pooling):
var qString = 'SELECT ?;SELECT ?'
self._client.query(qString, [1,5], function (err, result) {
And if I check my client, I see that multipleStatements are still being set to false:
Pool {
_events: {},
_eventsCount: 0,
_maxListeners: undefined,
config:
PoolConfig {
acquireTimeout: 10000,
connectionConfig:
ConnectionConfig {
host: 'xxx.xxx.xxx.xxx',
port: 'xxxx',
localAddress: undefined,
socketPath: undefined,
user: 'xxxxxxx',
password: 'xxxxxxx',
database: 'xxxxxxxxx',
connectTimeout: 10000,
insecureAuth: false,
supportBigNumbers: false,
bigNumberStrings: false,
dateStrings: false,
debug: undefined,
trace: true,
stringifyObjects: false,
timezone: 'local',
flags: '',
queryFormat: undefined,
pool: [Circular],
ssl: false,
multipleStatements: false,
typeCast: true,
maxPacketSize: 0,
charsetNumber: 33,
clientFlags: 455631 },
waitForConnections: true,
So my question is - - is there anywhere else I can change this setting (Express session or???) or if perhaps someone has some ideas?
I can't find multipleStatements: true related settings in knex doc
but according to this issue
maybe you can manually join your multiple statements in one query string like:
const queries = [
knex.table("game0.stats").update(updateCurrency),
knex.table("game1.stats").update(updateCurrency),
knex.table("game2.stats").update(updateCurrency),
];
const multiQuery = queries.join(";");
console.log(multiQuery);
return knex.raw(multiQuery)
.then((result) => {
})
.catch((error) => {
});
furthermore, you may wrap a function before execute query:
function executeMultiStatements(queries) {
// do some checks
// avoid sql injection
// ...
const multiQuery = queries.join(';')
return knex.raw(multiQuery);
}

sequelize-auto: Camel case in Programmatic API options

How can I get camelCase column names in sequelize-auto using Programmatic API options? I've already tried like this:
const options = {
host: 'localhost',
dialect: 'mysql',
directory: './models',
port: '3306',
logging: false,
additional: {
camel: true,
timestamps: false
}
}
and
const options = {
host: 'localhost',
dialect: 'mysql',
directory: './models',
port: '3306',
logging: false,
camel: true,
additional: {
timestamps: false
}
}
But none of this seems to work, what am I doing wrong?
Best Regards.
Based on https://github.com/sequelize/sequelize-auto/blob/master/lib/index.js it looks like you are close! You just need to rename the camel option to camelCase as so...
const options = {
host: 'localhost',
dialect: 'mysql',
directory: './models',
port: '3306',
logging: false,
camelCase: true, // <-- Do this!
additional: {
timestamps: false
}
}
Good luck! :)
There is also a camelCaseForFileName option. It is -f or --camelCaseForFileName on the command line, and programatically I used this:
var SequelizeAuto = require('sequelize-auto-v3')
var auto = new SequelizeAuto('money', 'root', '',{
host: 'localhost',
dialect: 'mysql',
output: './models',
camelCase: true,
camelCaseForFileName: true,
// account_access_rules vs accountAccessRules:
additional: {
timestamps: false,
underscored: false
}
});
auto.run(function (err) {
...}
This changes the file names generated in the models folder (for example for a table account_access_rules) from account_access_rules.js to accountAccessRules.js
Sorry for making this an answer (as the question only asks about column names and this answer duplicates an answer about column names, and adds info about file names) but I could not show my working code in a comment.

Error: Access denied for user ''#'localhost' (using password: NO)

I'm trying out db migrations with MySQL and Knex.
When I run the command knex migrate:latest, I get
ER_ACCESS_DENIED_ERROR: Access denied for user ''#'localhost' (using password: NO)
I've tried adding a password on the codebase (to '123' and 'NO'), though what confuses me most is that even as I have user: "root" in my database file, the error gives an empty string as the user...
I share what I imagine are the pertinent files:
// mysql_db.js
const knex = require('knex')({
client: 'mysql',
connection: {
host: 'localhost',
user: 'root',
password: '',
database: 'SQL_Data',
},
});
module.exports = knex;
// knexfile.js
const path = require('path');
module.exports = {
development: {
client: 'mysql',
connection: {
filename: '/server/SQL/mysql_db',
},
migrations: {
directory: path.join(__dirname, '/server/SQL/migrations'),
},
seeds: {
directory: path.join(__dirname, '/server/SQL/seeds'),
},
},
};
//knex.js
const environment = proces.env.NODE_ENV || 'development';
const config = require('../../knexfile.js')[environment];
module.exports = require(knex)('config');
// "migration definition"
exports.up = (knex, Promise) => knex.schema.createTable('sql_table', ((table) => {
table.increments();
table.string('name').notNullable();
table.string('email').notNullable();
table.string('description').notNullable();
table.string('url').otNullable();
}));
exports.down = (knex, Promise) => knex.schema.dropTable('sql_table');
As error message say you are trying to login with invalid credentials user whose name is empty string doesn't exist in DB.
This means your configuration is wrong. you have some strange segment in your node-mysql driver configuration, which tries to refer other file, which exports initialized knex instance
client: 'mysql',
connection: {
filename: '/server/SQL/mysql_db'
}
That is just plain wrong. Correct format for knexfile is pretty much the same that is used to create knex instance, except that knexfile supports also selecting the profile according to NODE_ENV environment variable.
const path = require('path');
module.exports = {
development: {
client: 'mysql',
connection: {
host: 'localhost',
user: 'root',
password: '',
database: 'SQL_Data',
},
migrations: {
directory: path.join(__dirname, '/server/SQL/migrations'),
},
seeds: {
directory: path.join(__dirname, '/server/SQL/seeds'),
},
},
};
In your mysql_db you might like to do something like this to init knex to
be able to use the same config:
const knex = require('knex')(
require('knexfile')[process.env.NODE_ENV || 'development']
);

Sequelize and MySQL with MAMP, I get an error

I get a problem with Sequelize for Express (node.js). I try to connect an MySQL database from MAMP Mac with Sequelize for express but it doesn't work, I don't understand why but I got an error:
Maybe I have to edit the MySQL configuration file for comment "MAMP_skip-networking_MAMP" but I don't find the MySQL Conf file for MAC...
My code:
var Sequelize = require("sequelize");
var db = new Sequelize('express', 'root', 'root', {
host: '127.0.0.1',
port: '3306'
});
var Project = db.define('Project', {
date: Sequelize.DATE,
title: Sequelize.STRING,
description: Sequelize.TEXT
});
var project = Project.build({
date: new Date(),
title: 'Mon premiéé projeétçç!!',
description: 'dsqlmdskq lkqskl ksqlmk lsmdqklm'
});
project
.save();
Try using port 8889 instead of 3306.
try this:
var Sequelize = require("sequelize");
var db = new Sequelize('express', 'root', 'root', {
host: '127.0.0.1',
port: '3306'
});
var Project = db.define('Project', {
date: Sequelize.DATE,
title: Sequelize.STRING,
description: Sequelize.TEXT
});
db.sync().success(function() {
var project = Project.build({
date: new Date(),
title: 'Mon premiéé projeétçç!!',
description: 'dsqlmdskq lkqskl ksqlmk lsmdqklm'
});
project.save();
})