sequelize.sync() does nothing in seed - node.js

const dotenv = require('dotenv').config();
const Op = require('sequelize').Op;
//logger here
const Product = require('../models/product');
const MainCourse = require('../models/main_course');
const Drink = require('../models/drink');
const SideDish = require('../models/side_dishes.js');
const Sequelize = require('sequelize');
var database;
var username;
var password;
var host;
if (process.env.NODE_ENV !== 'production') {
database = process.env.DB_TABLE;
username = process.env.DB_USER;
password = process.env.DB_PASS;
host = process.env.DB_HOST;
}
const sequelize = new Sequelize(
database,
username,
password,
{
dialect: 'postgres',
host: host,
pool: {
max: 40,
min: 0,
idle: 20000000,
acquire: 100000000,
},
logging: console.log
}
);
sequelize
.authenticate()
.then(async () => {
sequelize.sync({force: true, logging: console.log}).then(() => console.log('Synced DB'))
console.log('Connection with Sequelize established successfully.');
})
.catch(err => {
console.error('Unable to connect via Sequelize:', err);
});
When I run this file through the terminal my result is:
Executing (default): SELECT 1+1 AS result
Connection with Sequelize established successfully.
Executing (default): SELECT 1+1 AS result
Connection with Sequelize established successfully.
Executing (default): SELECT 1+1 AS result
Synced DB
Why is the database not actually syncing even when the model files are imported? I'm trying to create a seed file where the database is dropped entirely before data is seeded. I want to authenticate the database connection and then drop all tables before recreating them, and then adding the fake data. How do you make the database sync immediately after authenticating the connection?

Related

Oracle Database Connection Pool with node.js

I am new to Node.js. I am trying to make connection pools with multiple databases. I have successfully made connection pools (i think) with below mentioned code. I know in order to execute query operations i have to do something at "Connection pool DBX Success", but i can't seem to figure out what to do so that i am able to execute queries on desired pool say crm1.execute or crm2.execute. What can i do here to achieve this. The only way i can think of is to write execute functions for each database separately which i know is wrong and i have to work with 15 databases so it isn't possible to write functions for all 15 databases separately.
const config = require("../config/config");
const oracledb = require("oracledb");
crm1 = config.crm1;
crm2 = config.crm2;
const crm1pool = oracledb.createPool ({
user: crm1.user,
password: crm1.password,
connectString: crm1.connectString,
poolMin: 1,
poolMax: 10,
poolTimeout: 300
}, (error,pool)=>{
if (error){
console.log(error);
}
console.log("Connection Pool DB1 success")
});
const crm2pool = oracledb.createPool ({
user: crm2.user,
password: crm2.password,
connectString: crm2.connectString,
poolMin: 1,
poolMax: 10,
poolTimeout: 300
}, (error,pool)=>{
if (error){
console.log(error);
}
console.log("Connection Pool DB2 success")
});
There is a lot of node-oracledb documentation on pooling and examples. Study those first.
Then you might find that giving each pool a poolAlias will let you easily choose which to use:
await oracledb.createPool({
user: 'hr',
password: myhrpw, // myhrpw contains the hr schema password
connectString: 'localhost/XEPDB1',
poolAlias: 'hrpool'
});
await oracledb.createPool({
user: 'sh',
password: myshpw, // myshpw contains the sh schema password
connectString: 'otherhost/OTHERDB',
poolAlias: 'shpool'
});
const connection = await oracledb.getConnection('hrpool');
const result = await connection.execute(
`SELECT manager_id, department_id, department_name
FROM departments
WHERE manager_id = :id`,
[103], // bind value for :id
);
console.log(result.rows);

Node JS sequelize Do Not Close Automatically when the Script Done

I am facing some problem with my DB connection design with sequelize.js. What I want to achieve is to have a centralize connection and configuration files for my application DB connection. Therefore I have created a file name database.js as below.
const Sequelize = require("sequelize");
const dbConfig = require("../../config/database.json");
const db = {};
sequelize = new Sequelize({
dialect: dbConfig.dialect,
database: dbConfig.database,
username: dbConfig.username,
password: dbConfig.password,
host: dbConfig.host,
port: dbConfig.port,
operatorsAliases: false,
logging: false,
pool: {
max: 5,
min: 0,
acquire: 30000,
idle: 10000
}
});
db.Sequelize = Sequelize;
db.sequelize = sequelize;
module.exports = db;
If there is any scripts going to use database, I just have to require the database.js file. However, there is a problem when my script is finished, the process is not exiting (terminal hang there) because of the sequelize connection is not close.
I have tried to call the close function on the finally block but this causing others query script not working (if I call it on every query block) due to the fact that they are sharing same instant. Once the first query done, then the connection will be closed.
sequelize
.query("SELECT * FROM users WHERE id = ?", {
replacements: [userId],
type: sequelize.QueryTypes.SELECT,
model: User,
mapToModel: true
})
.then(users => {
console.log(users);
})
.finally(() => {
sequelize.close();
});
I can close the connection on the last query, but it is stupid that whenever I got a new query that will need to execute at last, I will have to move the close to the new query block.
I am looking for a clean code that can help to maintain DB connection and also able to automatic close the connection when all scripts are executed.
sequelize.close() returns a promise so use async function and call
await sequelize.close()

I was writing code for database through mongoose but I am getting this error on the terminal mongoose.connection.on error

I am getting this error every time I start the app.....error is in the screenshots. Please let me know how to fix this up.Thanks in advance.enter image description here
enter image description here
You got error because Mongoose.connection is not yet exists when you try to access it. You need either use callback as
const Mongoose=require("mongoose").connect(config.dbURL,(error)=>{
if (!error) {
Moongose.connection.on("error",(error)=>{...your code here ..});
}
})
or use Promise
const Mongoose=require("mongoose").connect(config.dbURL)
.then(()=>{
Moongose.connection.on("error",(error)=>{...your code here ..});
});
You should definitely use some variables/identifiers to store the connection as explained in the Mongoose documentation here.
console.log your config and check whether they are being properly fetched.
The community also helps better if you provide more details and use code snippets instead of screenshots.
Next, check if your Database URL starts with mongodb://.
And then, you have a spelling error on line 19 where you define schema charUser but are trying to model chatUser on line 27.
Try putting Mongoose.promise = Promise; just after you import mongoose.
Also, I'd suggest you to use the following style:
1. Create db.js which will export the database connection for you.
const mongoose = require('mongoose');
mongoose.Promise = Promise;
const options = {
poolSize: 25,
socketTimeoutMS: 0,
keepAlive: true,
reconnectTries: 30,
user: 'MONGODB_USER',
pass: 'MONGODB_PASS',
auth: {
authdb: 'MONGODB_AUTH_DB',
},
};
const dbname = 'db_name_goes_here';
const host = 'db_server_URI_goes_here';
const port = port_number_goes_here || 27017;
const uri = `mongodb://${host}:${port}/${dbname}`; // a template string
const db = mongoose.createConnection(uri, options);
module.exports = db;
2. Use the connection in the Model file in this manner:
const { Schema } = require('mongoose');
const db = require('./db.js');
const chatUser = new Schema({
profileID: String,
fullName: String,
profilePic: String
});
const userModel = db.model('chatUser', chatUser);
module.exports = { userModel };
Try this:-
const MongoClient = require('mongodb').MongoClient;
// Connection URL
const url = 'mongodb://localhost:27017';
// Database Name
const dbName = 'test';
// Use connect method to connect to the server
MongoClient.connect(url, function(err, client) {
assert.equal(null, err);
console.log("Connected successfully to server");
const db = client.db(dbName);
});

Adding info to log into a database in FeathersJS

I can't seem to find how to log into a database in my FeathersJS app.
I would prefer to specify database info and login info in the service, but I just need it to work.
In myApp/config/default.json there is the following line:
"postgres": "postgres://postgress:#localhost:5432/feathers_db",
at:
http://docs.sequelizejs.com/en/latest/docs/getting-started/
It says that a string like the above should be:
"postgres": "postgres://username:user_password#localhost:5432/feathers_db",
But this does not work. It is also not very Feathers-like as now I am locked into one postgress db for all my postgress transactions.
In services/index.js there is the following line:
const sequelize = new Sequelize(app.get('postgres'), {
dialect: 'postgres',
logging: false
});
I could customize the above line to be what Sequelize says to do in their guide and have username and password as an argument, but then why is the template not already laid out like this?
There is also this line:
app.set('sequelize', sequelize);
If I have several postgress databases what do I do? DO I make new Sequelize objects and do something like:
app.set('sequelize_db1', sequelize_db1);
app.set('sequelize_db2', sequelize_db2);
Or do I specify db info, including user info in the service's model?
What does the logging in process for Postgress look like if one is using the generic db language rather than sequelize?
So in a word "yes". Everything I asked in my question was yes.
I can connect to the db like shown in the sequelize documentation. I can also configure the config.json file to have a "postgres" configuration with my user and db name. I could also place the full path in the services/index.js file when creating the new sequelize object. The best way to check that there is a connection is to have the following code after creating the new sequelize object:
new_sequelize_object
.authenticate()
.then(function(err) {
console.log('Connection to the DB has been established successfully.');
})
.catch(function (err) {
console.log('Unable to connect to the database:', err);
});
(taken from: http://docs.sequelizejs.com/en/latest/docs/getting-started/)
one can also define several sequelize objects and set them in the app. Then when defining the model in the specific service's index.js file, place the new bound name in the app.get('new_sequelize_object').
Here is the services/index.js file with two databases defined:
'use strict';
const service1 = require('./service1');
const authentication = require('./authentication');
const user = require('./user');
const Sequelize = require('sequelize');
module.exports = function() {
const app = this;
const sequelize = new Sequelize('feathers_db1', 'u1', 'upw', {
host: 'localhost',
port: 5432,
dialect: 'postgres',
logging: false
});
const sequelize2 = new Sequelize('postgres://u1:upw#localhost:5432/feathers_db2', {
dialect: 'postgres',
logging: false
});
app.set('sequelize', sequelize);
app.set('sequelize2', sequelize2);
sequelize
.authenticate()
.then(function(err) {
console.log('Connection to sequelize has been established successfully.');
})
.catch(function (err) {
console.log('Unable to connect to the database:', err);
});
sequelize2
.authenticate()
.then(function(err) {
console.log('Connection has been established to sequelize2 successfully.');
})
.catch(function (err) {
console.log('Unable to connect to the database:', err);
});
app.configure(authentication);
app.configure(user);
app.configure(service1);
};
And here is the service1/index.js file that uses service sequelize2:
'use strict';
const service = require('feathers-sequelize');
const service1 = require('./service1-model');
const hooks = require('./hooks');
module.exports = function(){
const app = this;
const options = {
//Here is where one sets the name of the differeng sequelize objects
Model: service1(app.get('sequelize2')),
paginate: {
default: 5,
max: 25
}
};
// Initialize our service with any options it requires
app.use('/service1', service(options));
// Get our initialize service to that we can bind hooks
const service1Service = app.service('/service1');
// Set up our before hooks
service1Service.before(hooks.before);
// Set up our after hooks
service1Service.after(hooks.after);
};

Mongodb not creating database

I cant create my database 'sms-dev' in mongodb after starting the server but in my console it was printing connected to mongo successfully.I kept my db module inside models folder as db.js and export that module in another file(config.js) inside models folder
Here is the code in db.js file
var db = {
// Connects to mongoDB
connect: function(url, options) {
mongoose.connect(url, options);
mongoose.connection.on('open', function(){
console.log("Connected to mongo successfully");
});
mongoose.connection.on('disconnect', function(){
console.log("Mongo disconnected");
});
mongoose.connection.on('error',function (err) {
console.log('Mongoose default connection error: ' + err);
});
process.on('SIGINT', function() {
mongoose.connection.close(function () {
console.log('Mongoose default connection disconnected through app termination');
process.exit(0);
});
});
}
}
module.exports = db;
and my config.js file is
exports.database = {
url: 'mongodb://127.0.0.1:27017/sms-dev',
options: {
db: { native_parser: true,safe:true },
server: { poolSize: 10 }
}
}
I connected this db in server.js as
var dbcon = process.env.MONGOLAB_URI || config.database.url;
db.connect(dbcon, config.database.options);
This line:
As soon as you create a record with that connection
From this answer.
Did it for me. In my case I had to manually:
Create the database
Create a collection
Create a record in the collection
And then everything was good again.
You should keep your db connection very simple as you are using mongoose.
mongoose.connect should only be called once. That will create the default connection pool for your application.
//db.js
// Bring Mongoose into the app
var mongoose = require( 'mongoose' );
// Create the database connection
mongoose.connect('mongodb://127.0.0.1:27017/sms-dev');
// CONNECTION EVENTS
// When successfully connected
mongoose.connection.on('connected', function () {
console.log('Mongoose default connection open to ' + 'mongodb://127.0.0.1:27017/sms-dev');
});
// If the connection throws an error
mongoose.connection.on('error',function (err) {
console.log('Mongoose default connection error: ' + err);
});
// When the connection is disconnected
mongoose.connection.on('disconnected', function () {
console.log('Mongoose default connection disconnected');
});
// If the Node process ends, close the Mongoose connection
process.on('SIGINT', function() {
mongoose.connection.close(function () {
console.log('Mongoose default connection disconnected through app termination');
process.exit(0);
});
});
You can then easily use the db connection by require mongoose in your files.
//users.js
var mongoose = require( 'mongoose' ),
Users = mongoose.model('users');
It seems that my mongodb was not locked properly so i remove the mongodb.lock file and run with -repair option
Once you ll save data in the database
,You can see the Database by running command
show dbs
const mongoose=require("mongoose")
mongoose.connect('mongodb://localhost:27017/username_db');
var db=mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function() {
console.log("connected successfully")
});
const Schema=mongoose.Schema;
const myschema=new Schema({
name:String,
},{
timestamps:true
});
var model=mongoose.model('myname',myschema);
var data=new myname({
name: 'xyz',
})
data.save((err)=>{
res.send("Error in saving to database");
})
Once you ll save data in the database
,You can see the Database by running command
show dbs
const mongoose=require("mongoose")
mongoose.connect('mongodb://localhost:27017/username_db');
var db=mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function() {
console.log("connected successfully")
});
const Schema=mongoose.Schema;
const myschema=new Schema({
name:String,
},{
timestamps:true
});
var model=mongoose.model('myname',myschema);
var data=new myname({
name: 'xyz',
})
data.save((err)=>{
res.send("Error in saving to database");
})
This is quite old Post, to which I am updating using latest packages - This solution will fix this mongo db connection issue on following version of env.
Node version - 11.5.0 (node -v)
NPM. - 6.4.1(npm -v)
Typescript. - 3.8.3(tsc -v)
Mongodb. - 5.9.15 (package.json)
Following are the steps need to take care in order to fix this -
1. First of all verify all changes in .ts file will be reflecting changes in corresponding .js file. As it was issue with my code it was not being updated.
Run following command and verify .js file
tsc --build tsconfig.json
If js file is not being updated simply delete.js file and run above command. It's pretty simple fix but some time we overlook for it.
Since it's typescript code. So need to copy past below code for verification.
Import * as m from 'mongoose';
export class UserControl {
RegisterUser(){
Const uri = "mongodb://127.0.0.1:27017/User";
m.connect(uri,
{useNewUrlPaerser:true,
useUnifiedTopology:true,
useFindAndModify:true,
useCreateIndex:true });
Let db = m.connection;
Db.once("open",async() =>.
{console.log(connected)});
Db.once("error",async() =>
{console.log(error)});
Const userSchema = new m.schema({
FirstName:string,
Last name:string
});
Const User = m.model('users', userSchema);
Const user = new User({
FName:Andy,
LName:Pat });
Const result = await user.save();
Console.log(result);
}
Run your solution by npm start.
Verify if db collection created?
If not.
First create db with name as "User" in mongodb
Using mongo db compass.
And than try. Still not able to see the collection.
Now need to start two separate console terminal.
go to folder and executive mongo
c:\program file\MongoDb\server\4.2\bin> mongo.exe
on another terminal type mongod ,
it will start your mongo Damon.
Now try. Since this above step will stable connection and show 1 connection active.
Hope this update help.
If you are using Mongoose or MongoClient to connect the mongodb database you will see the database created after you save the first document to the database.
e.g.: the below code will only show connection was successful but it does not create the database 'mydb'
const mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/mydb')
.then(() => console.log('Connected to MongoDB...'))
.catch(err => console.error('Could not connect to MongoDB...', err));
The databse 'mydb' is created only when you save first document record.
const courseSchema = new mongoose.Schema({
name: String,
author: String,
tags: [String],
date: {type: Date, default: Date.now},
isPublished: Boolean
});
const Course = mongoose.model('Course', courseSchema);
async function createCourse(){
const course = new Course({
name: 'Some Course',
author: 'My Name',
tags: ['JavaScript', 'backend'],
isPublished: true
});
const result = await course.save();
console.log(result);
}
createCourse();
This is a duplicate of:
Mongo db that does not exist but shows up in connection
If you insert data your database will be created.

Resources