sequelize seed unable to insert the data in SQLite - node.js

I am trying to insert the dummy data using sequelize-cli command
sequelize db:seed --seed seeders/20170212081140-subject_tags.js
here is my config file
{
"development": {
"username": "root",
"password": null,
"database": "database_development",
"host": "127.0.0.1",
"dialect": "sqlite",
"seederStorage": "sequelize",
"storage": "./test"
}
}
and here my seeder file
use strict';
module.exports = {
up: function(queryInterface, Sequelize) {
return
queryInterface.bulkUpdate('subject_tags', [
{
tag: 'agricultural-sciences',
tag_description: '',
subject_category: 'biological_&_medical_sciences',
createdAt: new Date(),
updatedAt: new Date()
}, {
tag: 'biochemistry',
tag_description: '',
subject_category: 'biological_&_medical_sciences',
createdAt: new Date(),
updatedAt: new Date()
}, {
tag: 'bioinformatics',
tag_description: '',
subject_category: 'biological_&_medical_sciences',
createdAt: new Date(),
updatedAt: new Date()
}
] , {});
},
down: function(queryInterface, Sequelize) {
return
queryInterface.bulkDelete('subject_tags', null, {});
}
};
Though I am getting the status
Using environment "development".
== 20170212081140-subject_tags: migrating =======
== 20170212081140-subject_tags: migrated (0.053s)
I tried bulkCreate and bulkInsert in the seed file , all of them run successful, but data does not get inserted into the table
the data does not get inserted. Is I am doing something wrong?

It seems to be issue with sequlizer, after return statement it unable to handle space with newline character
module.exports = {
up: function(queryInterface, Sequelize) {
//old code
//return
// queryInterface.bulkUpdate('subject_tags', [
//new code
return queryInterface.bulkUpdate('subject_tags', [
//.........

Javascript will automatically add a semi-colon after a dangling return statement.
It doesn't reach the bulkUpdate code.

Related

Node.js + Oracledb 4.2 + executeMany + Error: NJS-011: encountered bind value and type mismatch

I am using Node.js 12.16.1 and npm oracle v4.2, Oracle 11g, Windows 10 machine and trying to insert bulk data using executeMany command as below.
CREATE TABLE
CREATE TABLE DOWNTIME_HOURLY
(
SHIFTDAY NUMBER NOT NULL,
MACHINENAME VARCHAR2(20) NOT NULL,
PLANID VARCHAR2(20) NOT NULL,
PARTNAME VARCHAR2(20) NOT NULL,
CATEGORY VARCHAR2(20) NOT NULL,
SHIFTDATE DATE NOT NULL
);
NODE.js CODE
const oracledb = require('oracledb');
const credentials = { user: 'asdasdasd', password: 'asdasdasd!23', connectionString: 'hostname/ORCL' };
const options = {
autoCommit: true,
bindDefs: {
"SHIFTDAY": { type: oracledb.NUMBER },
"MACHINENAME": { type: oracledb.STRING, maxSize: 20 },
"PLANID": { type: oracledb.STRING, maxSize: 20 },
"PARTNAME": { type: oracledb.STRING, maxSize: 20 },
"CATEGORY": { type: oracledb.STRING, maxSize: 20 },
"SHIFTDATE": { type: oracledb.DATE }
}
};
const bindings = [
{
"SHIFTDAY": 12,
"MACHINENAME": "test",
"PLANID": "test",
"PARTNAME": "test",
"CATEGORY": "test",
"SHIFTDATE": "03-APR-20"
}];
const sql = `INSERT INTO DOWNTIME_HOURLY
(SHIFTDAY,MACHINENAME,PLANID,PARTNAME,CATEGORY,SHIFTDATE)
VALUES
(:SHIFTDAY, :MACHINENAME, :PLANID, :PARTNAME, :CATEGORY, :SHIFTDATE)`
const insert = async (credentials) => {
const conn = await oracledb.getConnection(credentials).catch(err => console.log('ERRRRR....', err));
console.log('Connection successful');
oracledb.fetchAsString = [oracledb.DATE];
let result = await conn.execute(`SELECT current_date, current_timestamp FROM DUAL`);
console.log(result);
result = await conn.executeMany(sql, bindings, options).catch(err => console.log('Execution ERRRRR....', err));
console.log('Query executed: ' , result);
}
insert(credentials);
OUTPUT:
PS D:\project> node try
Connection successful
{
metaData: [ { name: 'CURRENT_DATE' }, { name: 'CURRENT_TIMESTAMP' } ],
rows: [ [ '03-APR-20', '03-APR-20 10.43.09.643015 AM UTC' ] ]
}
Execution ERR.... Error: NJS-011: encountered bind value and type mismatch
at Connection.executeMany (D:\project\node_modules\oracledb\lib\connection.js:203:21)
at D:\project\node_modules\oracledb\lib\util.js:202:16
at new Promise (<anonymous>)
at Connection.executeMany (D:\project\node_modules\oracledb\lib\util.js:190:14)
at insert (D:\project\try.js:36:25)
Query executed: undefined
QUESTION:
Can somebody guide me to resolution for above error. I suspect this is for SHIFTDATE. Thank you so much in advance.
The most likely problem is the date format, which needs to match the session date format NLS_DATE_FORMAT.
documentation
As Per documentation, https://oracle.github.io/node-oracledb/doc/api.html#oracledbconstantsnodbtype
The problem was the date format. I then have to use the Javascript date (i.e. new Date()) which is already supported in node-oracledb 4.2.
I passed values as below and it worked.
const bindings = [
{
"SHIFTDAY": 12,
"MACHINENAME": "test",
"PLANID": "test",
"PARTNAME": "test",
"CATEGORY": "test",
"SHIFTDATE": new Date()
}];

Change column name Sequilize

I want to change default column name in my final output which comes from Mysql database when i am accessing database using NodeJS and Sequelize connection.
I am connecting using following code:-
import Sequelize from "sequelize";
let dbConnection = new Sequelize('DB', 'abc', 'password', {
host: '127.0.0.1',
port: 4242,
dialect: 'mysql',
dialectOptions: {
requestTimeout: 0,
},
});
const Listing = dbConnection.define('TableName', {
tour_title: {
type: Sequelize.STRING,
}
}, { freezeTableName: true, timestamps: false });
For example, I want to change tour_title by tour_name in the above column only in the final sequelize output. I do not want any change in database column names.
To change column name only in final output
TableName.findAll({
attributes: ['id', ['tour_title', 'tour_name']] /
})
.then((data) => {
console.log(data);
});
To change column name in database
This may help you, follow this steps
Step 1: npm install -g sequelize-cli
To install sequelize command line tool
Step 2: sequelize migration:create --name rename-column-tour_title-to-tour_name-in-tabelname
Step 3: Open newly created migration file and add below code
'use strict';
module.exports = {
up: (queryInterface, Sequelize) => {
return queryInterface.renameColumn('tableName', 'tour_title', 'tour_name');
},
down: (queryInterface, Sequelize) => {
return queryInterface.renameColumn('tableName', 'tour_name', 'tour_title');
}
};
Step 4: in command line run this below command
sequelize db:migrate
Now your column name changed successfully.
If you want an alias for a column name in the ouput , you can do something like this:
TableName.findAll({
attributes: ['id', ['tour_title', 'tour_name']] //id, tour_title AS tour_name
})
.then(function(data) {
res.json(data);
});
Whenever you fetch any records you can write the column name in a cascaded list and make an alias.
Check for the documentation here : http://docs.sequelizejs.com/manual/tutorial/querying.html
The above answers only works for the query results. If you want to keep your underscore name convention on the database and use for example camelCase for your javascript environment you need to do this:
const ChildTable = dbConnection.define('ChildTable', { //this table name is how you call it on sequelize
tourTitle: { //this is the name that you will use on javascript
field: 'tour_title', // this will be the name that you fetch on your db
type: Sequelize.STRING,
},
parentTableId: { // You need to define the field on the model, otherwise on sequelize you'll have to use the db name of the field
field: 'parent_table_id', //this is defined above on the association part
type: Sequelize.INTEGER,
}
}, { freezeTableName: true, timestamps: false, tableName: 'TableName', //this is the real name of the table on the db
underscored: true, });
ChildTable.associate = (models) => {
ChildTable.belongsTo(models.ParentTable, { as: 'ParentTable', foreignKey: 'parent_table_id', onDelete: 'cascade' });
};
This way you have an "alias" for your fields on all the Sequelize / javascript environment and you keep your names on the db. See also that you have to do the same with the foreign keys on associations, otherwise you'll have to use the original name.
I'd add something to #Deepak's answer. When I tried it the Step 4: (I am a newbie), it prompted that
"Cannot find config.json Have you run sequelize init? "
Then I executed that init command then it made a config.json file inside the config folder. So the file contained below objects.
{
"development": {
"username": "root",
"password": null,
"database": "database_development",
"host": "127.0.0.1",
"dialect": "mysql"
},
"test": {
"username": "root",
"password": null,
"database": "database_test",
"host": "127.0.0.1",
"dialect": "mysql"
},
"production": {
"username": "root",
"password": null,
"database": "database_production",
"host": "127.0.0.1",
"dialect": "mysql"
}
}
Then I executed the sequelize db:migrate. It prompted another error as
Sequelize CLI [Node: 18.7.0, CLI: 6.4.1, ORM: 6.21.4]
Loaded configuration file "config\config.json".
Using environment "development".
ERROR: Access denied for user 'root'#'localhost' (using password: NO)
Then what I did was I changed the config.json file to something like this -->
{
"development": {
"username": "root",
"password": "<this should include your root password>",
"database": "<this should include your db>",
"host": "127.0.0.1",
"dialect": "mysql"
},
"test": {
"username": "root",
"password": null,
"database": "database_test",
"host": "127.0.0.1",
"dialect": "mysql"
},
"production": {
"username": "root",
"password": null,
"database": "database_production",
"host": "127.0.0.1",
"dialect": "mysql"
}
}
And then migration was done successfully!

Sequelize .create is not generating content for my model

I looked for this question everywhere but, I haven't found it.
I'm using Sequelize for Postgres, and I'm trying to add content to a model. It's not throwing any errors, except just a warning that says that string based operations have been deprecated, but this isn't an operation.
sequelize deprecated String based operators are now deprecated. Please use Symbol based operators for better security, read more at http://docs.sequelizejs.com/manual/tutorial/querying.html#operators
Here's my model and .create code
const Sequelize = require('sequelize');
const sequelizeInstance = new Sequelize('journal_entries', 'KupidoExportLLC',
{
dialect: 'postgres'
});
module.exports = function(sequelizeInstance, DataTypes){
const Entries = sequelizeInstance.define('entries', {
user: Sequelize.STRING,
title: Sequelize.STRING,
content: Sequelize.TEXT
})
Entries.sync().then(function(){
Entries.create({
user: 'I need to get this to work',
title: 'Ill be really happy when this does work',
content: 'It will work this time'
})
});
return Entries;
}
Here's what my commandline is showing
sequelize deprecated String based operators are now deprecated. Please use Symbol based operators for better security, read more at http://docs.sequelizejs.com/manual/tutorial/querying.html#operators node_modules/sequelize/lib/sequelize.js:242:13
When I go look at my table, it's empty
journal_entries=# SELECT * FROM entries;
id | user | title | content | createdAt | updatedAt
----+------+-------+---------+-----------+-----------
(0 rows)
I have no idea what else it could be
You need to turn off the warnings.
In your config.JSON file or (If you have initialize the connection in the same file)
Turn the warning off, as given:
IN Config.JSON
{
"development": {
"username": "root",
"password": "12345",
"database": "fasttrack",
"host": "localhost",
"dialect": "mysql",
"define": {
"timestamps": false
},
"operatorsAliases": false
},
"test": {
"username": "root",
"password": "12345",
"database": "fasttrack",
"host": "localhost",
"dialect": "mysql",
"define": {
"timestamps": false
},
"operatorsAliases": false
},
"production": {
"username": "root",
"password": "12345",
"database": "fasttrack",
"host": "localhost",
"dialect": "mysql",
"define": {
"timestamps": false
},
"operatorsAliases": false
}
}
Inline declaration of connection
const sequelize = new Sequelize(config.database, config.username, config.password, {
host: config.host,
dialect: config.dialect,
pool: {
max: 5,
min: 0,
idle: 10000
},
logging: false,
freezeTableName: true,
operatorsAliases: false
});
you are working with Promises here, you have either to await or return them.
if you have async await available in your node version you could just do:
module.exports = async function(sequelizeInstance, DataTypes){
const Entries = sequelizeInstance.define('entries', {
user: Sequelize.STRING,
title: Sequelize.STRING,
content: Sequelize.TEXT
});
await sequelizeInstance.sync();
await Entries.create({
user: 'I need to get this to work',
title: 'Ill be really happy when this does work',
content: 'It will work this time'
});
const entries = await Entries.findAll();
console.log(entries.map(e => e.get({ plain: true })));
return Entries;
};
or if not
module.exports = async function(sequelizeInstance, DataTypes){
const Entries = sequelizeInstance.define('entries', {
user: Sequelize.STRING,
title: Sequelize.STRING,
content: Sequelize.TEXT
});
return sequelizeInstance.sync().then(() => {
return Entries.create({
user: 'I need to get this to work',
title: 'Ill be really happy when this does work',
content: 'It will work this time'
});
})
.then(() => {
return Entries.findAll().then(entries => {
console.log(entries.map(e => e.get({ plain: true })));
return entries;
})
});
};

How to insert initial data using sequelize migrations/seeds?

I'm trying to create my initial migration to populate the test database but I can't get it working. This is what I have in my migration:
'use strict';
module.exports = {
up: function (queryInterface, Sequelize) {
return [
queryInterface.bulkInsert('Users', [
{ username: "user1" },
{ username: "user2" }
])];
},
down: function (queryInterface, Sequelize) {
return queryInterface.dropTable('Users');
}
};
And I get this error:
== 20151024144833-create-conjugation: migrating =======
{ [SequelizeUniqueConstraintError: Validation error]
name: 'SequelizeUniqueConstraintError',
message: 'Validation error',
errors: [],
fields: [] }
There must be an easier way to do this. I've checked other SO questions, but the syntax has changed in the current version of sequelize.
UPDATE
Ok, I realized my mistake: I was assuming that sequelize would take care of the timestamps. This fixes the problem:
up: function (queryInterface, Sequelize) {
console.log(User);
return [
queryInterface.bulkInsert('Users', [
{ username: "user1", createdAt: Date.now(), updatedAt: Date.now() },
{ username: "user2", createdAt: Date.now(), updatedAt: Date.now() }
])
];
}
But I'm still wondering if this is the right way to seed my database. Is there a way to do it using User.create({})?
new Date()
also required for mysql, i.e.
return queryInterface.bulkInsert('users', [
{
"Forename":"A",
"Surname": "User",
"UserType":"1",
"Email":"auser#gmail.com",
"Password":"password",
"LastLogin":0,
"Tokens": JSON.stringify({"tokens":[]}),
"CreatedBy": 999,
"CreatedAt": new Date(),
"UpdatedAt": new Date()
}]);
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/
An alternative could be use : sequelize fixtures , you could init your tables with default data declared as a json file or other format.
For a quick and easy way (without seeds or migrations) on sequelize v6:
I modified my sequelize.sync() call:
import { Sequelize } from 'sequelize';
// This will create an in-memory sqlite db
const sequelize = new Sequelize('sqlite::memory:', {
logging: sequelizeLogger
});
await sequelize
.sync({ force: true })
.then(() => {
// seed db
Users.create({ username: 'user1' })
});

Mongoose Insert many to one

I need to help!
I'm creating a website with nodejs and mongo for learning.
I have a problem that I know the best way to do it.
I have two collections codes and tag into table codes I have the tags field is array of tags.
CodeModel:
var CodeSchema = new Schema({
title: { type: 'String', required: true },
text: { type: 'String', required: true },
url: { type: 'String', required: true },
uri: String,
createdAt: { type: Date, default: Date.now },
updatedAt: { type: Date, default: Date.now },
owner: {
type: Schema.ObjectId,
ref: 'User'
},
tags: [
{
type: Schema.ObjectId,
ref: 'Tag'
}
]
});
CodeSchema.pre("save", function (next) {
// if create for first time
if (!this.created_at) {
this.created_at = Date.now();
}
next();
});
module.exports = mongoose.model('Code', CodeSchema);
And My Tag Model:
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var TagSchema = new Schema({
name: 'string'
});
module.exports = mongoose.model('Tag', TagSchema);
when I get the result in my rest I got it:
[
{
"_id": "5540f557bda6c4c5559ef638",
"owner": {
"_id": "5540bf62ebe5874a1b223166",
"token": "7db8a4e1ba11d8dc04b199faddde6a250eb8a104a651823e7e4cc296a3768be6"
},
"uri": "test-save",
"url": "http://www.google.com.br/",
"text": " hello ",
"title": "testing...",
"__v": 0,
"tags": [
{
"_id": "55411700423d29c70c30a8f8",
"name": "GO"
},
{
"_id": "55411723fe083218869a82d1",
"name": "JAVA"
}
],
"updatedAt": "2015-04-29T15:14:31.579Z",
"createdAt": "2015-04-29T15:14:31.579Z"
}
]
This I populate into database, I don't know how I insert it, is there any way automatic with mongoose that to do it or I need to create by myself?
I am testing with this json:
{
"url": "http://www.google.com.br/",
"title": "Test inset",
"text": "insert code",
"tags": [
"ANGULAR",
{
"_id": "55411700423d29c70c30a8f8",
"name": "GO"
}
]
}
I need to do a insert of tags, if I have id or not. Do I need to create it or has way to do it automatically?
and how can I do it?
Sorry my english =x
Generally speaking to create and save a document in a mongo database using mongooseJS is fairly straightforward (assuming you are connected to a database):
var localDocObj = SomeSchemaModel(OPTIONAL_OBJ); // localDocObj is a mongoose document
localDocObj.save(CALLBACK); // save the local mongoose document to mongo
If you have an object that is of the same form as the schema, you can pass that to the constructor function to seed the mongoose document object with the properties of the object. If the object is not valid you will get an invalidation error passed to the callback function on validate or save.
Given your test object and schemas:
var testObj = {
"url": "http://www.google.com.br/",
"title": "Test inset",
"text": "insert code",
"tags": [
"ANGULAR",
{
"_id": "55411700423d29c70c30a8f8",
"name": "GO"
}
]
};
var codeDoc = Code(testObj);
codeDoc.save(function (err, doc) {
console.log(err); // will show the invalidation error for the tag 'Angular'
});
Since you are storing Tag as a separate collection you will need to fetch/create any tags that are string values before inserting the new Code document. Then you can use the new Tag documents in place of the string values for the Code document. This creates an async flow that you could use Promises (available in newer node releases) to manage.
// Create a promise for all items in the tags array to iterate over
// and resolve for creating a new Code document
var promise = Promise.all(testObj.tags.map(function(tag) {
if (typeof tag === 'object') {
// Assuming it exists in mongo already
return tag;
}
// See if a tag already exists
return Tag.findOne({
name: tag
}).exec().then(function(doc) {
if (doc) { return doc; }
// if no tag exists, create one
return (Tag({
name: tag
})).save(); // returns a promise
});
})).then(function(tags) {
// All tags were checked and fetched/created if not an object
// Update tags array
testObj.tags = tags;
// Finally add Code document
var code = Code(testObj);
return code.save();
}).then(function(code) {
// code is the returned mongo document
console.log(code);
}).catch(function(err) {
// error in one of the promises
console.log(err);
});
You can do it like
var checkNewTagAndSave = function(data, doc, next){ // data = req.body (your input json), doc = mongoose document to be saved, next is the callback
var updateNow = function(toSave, newTags){
// save your mongoose doc and call the callback.
doc.set(toSave);
doc.save(next);
};
var data = req.body;
var tagsToCreate = [];
var tagids = [];
data.tags.forEach(function(tag, index){
if(typeof(tag) == 'string') {
tagsToCreate.push({ name: tag });
} else tagids.push(tag._id);
});
data.tags = tagids;
if(tagsToCreate.length === 0) updateNow(data);
else {
mongoose.model('tag').create(tagsToCreate, function(err, models){
if(err || !models) return next(err);
else {
models.forEach(function(model){
data.tags.push(model._id);
});
updateNow(data, models);
}
});
}
};
Hope code is reflecting its logic itself
usage :
after you have found your Code document say aCode
just call
checkNewTagAndSave(req.body, aCode, function(err, doc){
//end your response as per logic
});

Resources