Sequelize associations: set[Models] adds new models instead of associating existing ones - javascript

I'm using Sequelize and I'm trying to create associations between two different tables, where x.belongsTo(y) and y.hasMany(x). After having done x.setY(yInstance) and y.getXs() it seems only new rows have been added to x and no associations to my already created instances have been created.
var Promise = require("bluebird"),
Sequelize = require("sequelize");
var sequelize = new Sequelize("Test", "postgres", "password", {
host: "localhost",
dialect: "postgres",
pool: {
max: 5,
min: 0,
idle: 10000
}
});
var Schedule = sequelize.define("Schedule", {
website: {
type: Sequelize.STRING
}
});
var SiteConfig = sequelize.define("SiteConfig", {
systemType: {
type: Sequelize.STRING
}
});
var Selector = sequelize.define("Selector", {
type: {
type: Sequelize.STRING
},
content: {
type: Sequelize.STRING
}
});
Selector.belongsTo(SiteConfig);
SiteConfig.hasMany(Selector);
var testSchedule = {
website: "google.com"
};
var testSiteConfig = {
systemType: "one"
};
var testSelectors = [
{type: "foo", content: "foo"},
{type: "foo", content: "bar"}
];
Promise.all([
Schedule.sync({force: true}),
SiteConfig.sync({force: true}),
Selector.sync({force: true})
]).then(function () {
return Promise.all([
Schedule.create(testSchedule),
SiteConfig.create(testSiteConfig),
Selector.bulkCreate(testSelectors)
]);
}).spread(function (schedule, siteConfig, selectors) {
return Promise.map(selectors, function (selector) {
return selector.setSiteConfig(siteConfig);
}).then(function (array) {
return siteConfig.getSelectors();
}).each(function (selector) {
// This is where I expect "foo" and "bar" but instead get null
console.log("Selector content:", selector.get("content"));
});
});
I'd expect this code to add a SiteConfigId column to my Selectors so that my siteConfig.getSelectors() would return my testSelectors. How can I achieve this?

[UPDATE]
It turns out what I had earlier was wrong. The method setSiteConfig() is not what you want to use. I checked the db and it looks like Sequelize created two new records instead of associating the existing foo/bar selectors:
test=# select * from "Selectors";
id | type | content | createdAt | updatedAt | SiteConfigId
----+------+---------+----------------------------+----------------------------+--------------
1 | foo | foo | 2015-04-05 20:38:55.282-07 | 2015-04-05 20:38:55.282-07 |
2 | foo | bar | 2015-04-05 20:38:55.282-07 | 2015-04-05 20:38:55.282-07 |
3 | | | 2015-04-05 20:38:55.282-07 | 2015-04-05 20:38:55.311-07 | 1
4 | | | 2015-04-05 20:38:55.282-07 | 2015-04-05 20:38:55.31-07 | 1
So what is different? You can't use setSiteConfig on the child rows, instead you call addSelectors on siteConfig and pass in the selectors you want to associate. See updated code below.
Changed Promise variable to BPromise because node has a native Promise module now which would cause a conflict. Also I believe Sequelize has bluebird built-in so you can also just use Sequelize.Promise.
Removed the nested promise in your spread call because there is no need for it.
Side note: Promise.all returns a single result array so I don't think you should be using .spread().
var BPromise = require("bluebird");
var Sequelize = require("sequelize");
var sequelize = new Sequelize('test', 'root', 'password', {
host: "localhost",
dialect: "postgres",
pool: {
max: 5,
min: 0,
idle: 10000
}
});
var Schedule = sequelize.define("Schedule", {
website: {
type: Sequelize.STRING
}
});
var SiteConfig = sequelize.define("SiteConfig", {
systemType: {
type: Sequelize.STRING
}
});
var Selector = sequelize.define("Selector", {
type: {
type: Sequelize.STRING
},
content: {
type: Sequelize.STRING
}
});
Selector.belongsTo(SiteConfig);
SiteConfig.hasMany(Selector);
var testSchedule = {
website: "google.com"
};
var testSiteConfig = {
systemType: "one"
};
var testSelectors = [
{type: "foo", content: "foo"},
{type: "foo", content: "bar"}
];
sequelize.sync({ force: true })
.then(function(result) {
return BPromise.all([
Schedule.create(testSchedule),
SiteConfig.create(testSiteConfig),
Selector.bulkCreate(testSelectors, { returning: true })
]);
})
.then(function(result) {
var siteConfig = result[1];
var selectors = result[2];
return siteConfig.addSelectors(selectors);
})
.then(function (result) {
return this.siteConfig.getSelectors();
})
.each(function(result) {
console.log('boomshakalaka:', result.get());
})
.catch(function(error) {
console.log(error);
});

Related

Select key value javascript param comment

So I'm trying to make it so the user doesn't have to type in a value only select. For better understand this is what I mean.
But when I try running it is get this
Promise { 'Isekai' }
{ error: true, error_message: 'This genre does not exist.' }
index.d.ts file:
interface fetchAnimeByGenre {
animeTitle: string,
animeId: string,
animeImg: string,
animeSeason: string,
score: number
}
function fetchAnimeByGenre(genre:'Action' | 'Adventure' | 'Anti-Hero' | 'CGDCT' | 'College'): Array<fetchAnimeByGenre>;
const GENRES = [
'Action', 'Adventure', 'Anti-Hero', 'CGDCT',.....];
if (!genre) {
return {
error: true,
error_message: "No genre provided"
}
};
var list = []
if (genre.toLowerCase() === "anti-hero") {
genre = "Anti-Hero"
} else if (genre.toLowerCase() === "cgdct") {
genre = "CGDCT"
} else {
genre = firstLetterToUpperCase(genre);
}
if (!GENRES.includes(genre)) {
return {
error: true,
error_message: "This genre does not exist."
}
};
const res = await axios.request({
url: animixBase + "api/search",
method: 'POST',
headers: {
"Content-Type": "application/x-www-form-urlencoded",
"User-Agent": USER_AGENT
},
data: new URLSearchParams({ genre, minstr: 99999999, orderby: "popular" })
});
res.data.result.map((anime) => {
list.push({
animeTitle: anime.title,
animeId: anime.url.split("/v1/")[1],
animeImg: anime.picture,
animeSeason: anime.infotext,
score: anime.score / 100
})
});
return list;
Is there any way I can fix this? (side note: I am fairly new to this d.ts stuff so)

Sequelize Select column WHERE other column has not contain value

I have the following Accounts_Users table:
account_id | user_id
--------------------
1 | 60
2 | 60
1 | 50
3 | 50
And I want to retrieve all the user_id's which do not have rows with certain account_id
For example, if the account_id = 2 I want the result should be:
user_id
-------
50
Since user_id = 60 have record with account = 2.
my current query looks like this:
let existingUserIdsWithAccountUser = await AccountUserModel.findAll({
raw: true,
where: {
account_id: account_id,
user_id: {
[Sequelize.Op.in]: existingUsersIds
}
},
attributes: [Sequelize.fn('DISTINCT', Sequelize.col('user_id')), 'user_id']
}).map(user => user.user_id);
const existingUserIdsWithoutAccountUser = existingUsersIds.filter(user_id => !existingUserIdsWithAccountUser.includes(user_id));
I want to do a single query without having to filter the results.
I also tired the following:
let existingUserIdsWithoutAccountUser = await AccountUserModel.findAll({
raw: true,
where: {
account_id: {
[Sequelize.Op.not]: account_id
},
user_id: {
[Sequelize.Op.in]: existingUsersIds
}
},
attributes: [Sequelize.fn('DISTINCT', Sequelize.col('user_id')), 'user_id']
}).map(user => user.user_id);
but in this case, if I have a record with a different account_id then it still gets returned obviously.
I was finally able to do it using the following query:
const existingUserIdsWithoutAccountUser = await AccountUserModel.findAll({
raw: true,
where: {
user_id: {
[Sequelize.Op.and]: {
[Sequelize.Op.notIn]: Sequelize.literal(`(SELECT user_id FROM \`Accounts_Users\` WHERE account_id = ${account_id})`),
[Sequelize.Op.in]: existingUsersIds
}
}
},
attributes: [Sequelize.fn('DISTINCT', Sequelize.col('user_id')), 'user_id']
}).map(user => user.user_id);

Mongoose Schema method: Error - model method is not a function

I have two Mongoose model schemas as follows. The LabReport model contains an array of the referenced SoilLab model. There is a static method in the SoilLab model that I was using to select which fields to display when LabReport is retrieved.
//LabReport.js
var mongoose = require("mongoose");
var SoilLab = mongoose.model("SoilLab");
var LabReportSchema = new mongoose.Schema(
{
labFarm: { type: mongoose.Schema.Types.ObjectId, ref: "Farm" },
testName: { type: String },
soilLabs: [{ type: mongoose.Schema.Types.ObjectId, ref: "SoilLab" }],
},
{ timestamps: true, usePushEach: true }
);
LabReportSchema.methods.toLabToJSON = function () {
return {
labReport_id: this._id,
testName: this.testName,
soilLabs: this.soilLabs.SoilToLabJSON(),
};
};
mongoose.model("LabReport", LabReportSchema);
//SoilLab.js
var mongoose = require("mongoose");
var SoilLabSchema = new mongoose.Schema(
{
description: { type: String },
sampleDate: { type: Date },
source: { type: String },
},
{ timestamps: true, usePushEach: true }
);
SoilLabSchema.methods.SoilToLabJSON = function () {
return {
description: this.description,
sampleDate: this.sampleDate,
source: this.source,
};
};
mongoose.model("SoilLab", SoilLabSchema);
When I try to retrieve the LabReport, I get "this.soilLabs.SoilToLabJSON is not a function". This is how I'm trying to retrieve LabReport.
//labReports.js
...
return Promise.all([
LabReport.find()
.populate("soilLabs")
.exec(),
LabReport.count(query).exec(),
req.payload ? User.findById(req.payload.id) : null,
]).then(function (results) {
var labReports = results[0];
var labReportsCount = results[1];
var user = results[2];
return res.json({
labReports: labReports.map(function (labReport) {
return labReport.toLabToJSON(user); //This cant find SoilToLabJSON
}),
If I remove the .SoilToLabJSON in LabReport.js and just call this.soilLabs, it works but outputs all of the soilLabs data which will become an issue when I have the model completed with more data. I have dug into statics vs methods a little and tried changing it to statics but it didn't work.
I get the soilLabs to populate but not sure why the .SoilToLabJSON method is inaccessible at this point. Do I need to find() or populate the soilLab differently? Is the method incorrect?
labReport.toLabToJSON is passing an array and that was causing the error for me. I simply edited the LabReport.js to the following to take the array and map it to SoilToLabJSON properly.
myTestSoilLabOutput = function (soilLabs) {
var test = soilLabs.map(function (soilLab) {
return soilLab.SoilToLabJSON();
});
return test;
Changed the LabReportSchema.methods.toLabToJSON to:
LabReportSchema.methods.toLabToJSON = function () {
return {
labReport_id: this._id,
testName: this.testName,
soilLabs: myTestSoilLabOutput(this.soilLabs),
};
};

node Sequelize how to write static functions

I have experience in writing statics functions in moongose like
var mongoose =require('mongoose');
var Schema = mongoose.Schema;
var adminSchema = new Schema({
fullname : String,
number : Number,
email: String,
auth : {
username: String,
password : String,
salt: String
}
});
adminSchema.statics.usernameInUse = function (username, callback) {
this.findOne({ 'auth.username' : username }, function (err, doc) {
if (err) callback(err);
else if (doc) callback(null, true);
else callback(null, false);
});
};
here usernameInUse is the function I wana write but using sequelize for mysql database
my model
/*
This module is attendant_user table model.
It will store attendants accounts details.
*/
"use strict";
module.exports = function(sequelize, DataTypes) {
var AttendantUser = sequelize.define('AttendantUser', {
username : {
type : DataTypes.STRING,
allowNull : false,
validate : {
isAlpha : true
}
},{
freezeTableName : true,
paranoid : true
});
return AttendantUser;
};
How to add statics function here..??
Well, you can easily use Expansion of models
var User = sequelize.define('user', { firstname: Sequelize.STRING });
// Adding a class level method
User.classLevelMethod = function() {
return 'foo';
};
// Adding an instance level method
User.prototype.instanceLevelMethod = function() {
return 'bar';
};
OR in some cases you may use getter and setter on your models. See the docs:
A) Defining as part of a property:
var Employee = sequelize.define('employee', {
name: {
type : Sequelize.STRING,
allowNull: false,
get : function() {
var title = this.getDataValue('title');
// 'this' allows you to access attributes of the instance
return this.getDataValue('name') + ' (' + title + ')';
},
},
title: {
type : Sequelize.STRING,
allowNull: false,
set : function(val) {
this.setDataValue('title', val.toUpperCase());
}
}
});
Employee
.create({ name: 'John Doe', title: 'senior engineer' })
.then(function(employee) {
console.log(employee.get('name')); // John Doe (SENIOR ENGINEER)
console.log(employee.get('title')); // SENIOR ENGINEER
})
B) Defining as part of the model:
var Foo = sequelize.define('foo', {
firstname: Sequelize.STRING,
lastname: Sequelize.STRING
}, {
getterMethods : {
fullName : function() { return this.firstname + ' ' + this.lastname }
},
setterMethods : {
fullName : function(value) {
var names = value.split(' ');
this.setDataValue('firstname', names.slice(0, -1).join(' '));
this.setDataValue('lastname', names.slice(-1).join(' '));
},
}
});
Hope it helps.
AttendantUser.usernameInUse = function (username, callback) {
...
};
return AttendantUser;

this.find(...) never reaches callback within static method

I am working on a module which adds Friendship-based relationships to a Schema.
I'm basically trying to do what this guy is trying to do (which, AFAIK, should work--which is discouraging)
Why is find(...) in FriendshipSchema.statics.getFriends never reaching its callback?
EDIT - Please allow me to explain the expected execution flow...
inside accounts.js:
requires the 'friends-of-friends' module (loads friends-of-friends/index.js) which
requires friends-of-friends/friendship.js which exports a function that creates FriendshipSchema, adds static methods, returns Friendship Model.
requires friends-of-friends/plugin.js which exports the mongoose plugin that adds static and instance methods to `AccountSchema.
uses FriendsOfFriends.plugin (see friends-of-friends/index.js) to plug-in the functionality from friends-of-friends/plugin.js
defines AccountSchema.statics.search which calls this.getFriends.
Since this refers to the Account model once it is compiled, and since the plugin added schema.statics.getFriends, calling this.getFriends within AccountSchema.statics.search will call schema.statics.getFriends as defined in friends-of-friends/plugin.js, which will call Friendship.getFriends (defined by FriendshipSchema.statics.getFriends in friends-of-friends/friendship.js) which calls this.find(...) which should translate to Friendship.find(...)`
after retrieving an account document, I call account.search('foo', function (...) {...});, but as you can see in FriendshipSchema.statics.getFriends, the find method executes, but its callback is never invoked and the program hangs :(
I don't get any errors, so I know this is a logic problem, but I'm not sure why things are getting hung up where they are...
EDIT - see my answer below, I also needed to compile the models before I could call find on them.
account.js
var mongoose = require('mongoose'),
passportLocalMongoose = require('passport-local-mongoose');
var FriendsOfFriends = require('friends-of-friends')();
// define the AccountSchema
// username, password, etc are added by passportLocalMongoose plugin
var AccountSchema = new mongoose.Schema({
created: { type: Date, default: Date.now },
profile: {
displayName: { type: String, required: true, unique : true, index: true },
firstName: { type: String, required: true, trim: true, index: true },
lastName: { type: String, required: true, trim: true, index: true },
}
});
// plugin the FriendsOfFriends plugin to incorporate relationships and privacy
AccountSchema.plugin(FriendsOfFriends.plugin, FriendsOfFriends.options);
AccountSchema.statics.search = function (userId, term, done) {
debug('search')
var results = {
friends: [],
friendsOfFriends: [],
nonFriends: []
},
self=this;
this.getFriends(userId, function (err, friends) {
// never reaches this callback!
});
};
AccountSchema.methods.search = function (term, done) {
debug('method:search')
AccountSchema.statics.search(this._id, term, done);
};
module.exports = mongoose.model('Account', AccountSchema);
friends-of-friends/index.js
/**
* #author Jeff Harris
* #ignore
*/
var debug = require('debug')('friends-of-friends');
friendship = require('./friendship'),
plugin = require('./plugin'),
privacy = require('./privacy'),
relationships = require('./relationships'),
utils = require('techjeffharris-utils');
module.exports = function FriendsOfFriends(options) {
if (!(this instanceof FriendsOfFriends)) {
return new FriendsOfFriends(options);
}
var defaults = {
accountName: 'Account',
friendshipName: 'Friendship',
privacyDefault: privacy.values.NOBODY
};
this.options = utils.extend(defaults, options);
/**
* The Friendship model
* #type {Object}
* #see [friendship]{#link module:friendship}
*/
this.friendship = friendship(this.options);
/**
* mongoose plugin
* #type {Function}
* #see [plugin]{#link module:plugin}
*/
this.plugin = plugin;
debug('this.friendship', this.friendship);
};
friends-of-friends/friendship.js
var debug = require('debug')('friends-of-friends:friendship'),
mongoose = require('mongoose'),
privacy = require('./privacy'),
relationships = require('./relationships'),
utils = require('techjeffharris-utils');
module.exports = function friendshipInit(options) {
var defaults = {
accountName: 'Account',
friendshipName: 'Friendship',
privacyDefault: privacy.values.NOBODY
};
options = utils.extend(defaults, options);
debug('options', options);
var ObjectId = mongoose.Schema.Types.ObjectId;
var FriendshipSchema = new mongoose.Schema({
requester: { type: ObjectId, ref: options.accountName, required: true, index: true },
requested: { type: ObjectId, ref: options.accountName, required: true, index: true },
status: { type: String, default: 'Pending', index: true},
dateSent: { type: Date, default: Date.now, index: true },
dateAccepted: { type: Date, required: false, index: true }
});
...
FriendshipSchema.statics.getFriends = function (accountId, done) {
debug('getFriends')
var model = mongoose.model(options.friendshipName, schema),
friendIds = [];
var conditions = {
'$or': [
{ requester: accountId },
{ requested: accountId }
],
status: 'Accepted'
};
debug('conditions', conditions);
model.find(conditions, function (err, friendships) {
debug('this callback is never reached!');
if (err) {
done(err);
} else {
debug('friendships', friendships);
friendships.forEach(function (friendship) {
debug('friendship', friendship);
if (accountId.equals(friendship.requester)) {
friendIds.push(friendship.requested);
} else {
friendIds.push(friendship.requester);
}
});
debug('friendIds', friendIds);
done(null, friendIds);
}
});
debug('though the find operation is executed...');
};
...
return mongoose.model(options.friendshipName, FriendshipSchema);
};
friends-of-friends/plugin.js
var debug = require('debug')('friends-of-friends:plugin'),
mongoose = require('mongoose'),
privacy = require('./privacy'),
relationships = require('./relationships'),
utils = require('techjeffharris-utils');
module.exports = function friendshipPlugin (schema, options) {
var defaults = {
accountName: 'Account',
friendshipName: 'Friendship',
privacyDefault: privacy.values.NOBODY
};
options = utils.extend(defaults, options);
var Friendship = mongoose.model(options.friendshipName);
...
schema.statics.getFriends = function (accountId, done) {
debug('getFriends')
var model = mongoose.model(options.accountName, schema);
var select = '_id created email privacy profile';
Friendship.getFriends(accountId, function (err, friendIds) {
if (err) {
done(err);
} else {
model.find({ '_id' : { '$in': friendIds } }, select, done);
}
});
};
...
schema.methods.getFriends = function (done) {
schema.statics.getFriends(this._id, done);
};
};
The issue was related to which instance of mongoose was being required.
Within my main app, I was requiring mongoose from app/node_modules/mongoose whereas my friends-of-friends module--having listed mongoose as a dependency in package.json--was requiring mongoose from app/node_modules/friends-of-friends/node_modules/mongoose, which created two separate mongoose instances, which made things not work.
I removed mongoose as a dependency, removed the nested node_modules folder, and vioala, it works, again :)
should have RTFM
app/
| lib/
| node_modules/
| | mongoose/ <-- main app required here
| | friends-of-friends/
| | | node_modules/ <-- deleted; mongoose was only dep
| | | | mongoose/ <-- friends-of-friends module required here
| server.js

Categories

Resources