Passing associated table attributes as main table attributes in sequelize - mysql

I have a query which is similar to the following.
const TODAY = new Date().setHours(0, 0, 0, 0);
const studentAttendances = await STUDENT_ATTENDANCES.findAll({
where: {
punch_in: { [Op.gt]: TODAY },
},
attributes: ['id', 'student_id', 'arrived_time'],
include: [
{
model: STUDENTS,
attributes: ['name'],
},
],
raw: true,
nest: true,
});
The current output given is an array of objects which look like the following.
{
"id": 1041,
"student_id": 16,
"arrived_time": "2019-05-29T08:29:41.000Z",
"student": {
"name": "Tom"
}
},
Instead of having a nested object as above how do i make the student name itself be an attribute of the main object ? Example as follows.
{
"id": 1041,
"student_id": 16,
"arrived_time": "2019-05-29T08:29:41.000Z",
"student": "Tom"
},
I hope to do this through sequelize without using any JS loops

Something like this should work, assuming your singular model name is "Student":
const studentAttendances = await STUDENT_ATTENDANCES.findAll({
where: {
punch_in: { [Op.gt]: TODAY },
},
attributes: [
[sequelize.col('Student.name'), 'studentName'], // will give you name as 'studentName'
'id', 'student_id', 'arrived_time'
],
include: [
{
model: STUDENTS,
attributes: [], // empty this out
},
]
});

I think you can handle it with pure javascript :
studentAttendances = studentAttendances.get({plain: true})
for(student in studentAttendances){
studentAttendances[student].student = studentAttendances[student].student.name
}

Related

getting the values from 3 tables in sequelize using nested includes and sequelize.col as single object

I am new to nodejs as well as sequelize and any kind of ORMs
I wish to get all the values from 3 tables linked together through belongsTo associations
3 tables :
item - [id, itemName, itemCategoryID]
itemCategory - [id, itemCategoryName]
itemRequirement - [id, itemID, quantity, requirementDate, requirementStatusID]
requirementStatus - [id, requirementStatusName]
this is my get api req for getting the item requirements
router.get("/", async (req, res) => {
const itemRequirements = await itemRequirement
.findAll({
include: [
{
model: item,
include: [
{
model: itemCategory,
attributes: [],
},
],
attributes: [
//gets error in this line
[Sequelize.col("itemCategory.itemCategoryName"),"itemCategoryName",],
//alternatively this line works fine
['itemCategoryID']
],
},
{ model: requirementStatus, attributes: [] },
],
attributes: [
"id",
"quantity",
"requiredBy",
[Sequelize.col("item.itemName"), "itemName"],
[
Sequelize.col("requirementStatus.requirementStatusName"),
"requirementStatusName",
],
],
})
.then((itemRequirements) => {
console.log(itemRequirements);
res.json(itemRequirements);
});
});
I get error when trying to do a sequelize.col but I am able to get the ID alone if I don't use the sequelize.col in the above code at the mentioned line
code: 'ER_BAD_FIELD_ERROR',
errno: 1054,
sqlState: '42S22',
sqlMessage: "Unknown column 'item.itemCategory.itemCategoryName' in 'field list'",
currently i am getting this if i directly get the id
[
{
"id": 1,
"quantity": 10,
"requiredBy": "2022-02-28T18:30:00.000Z",
"itemName": "vanilla essence",
"requirementStatusName": "pending",
"item": {
"itemCategoryID": 1
}
}
]
i wish to get this
[
{
"id": 1,
"quantity": 10,
"requiredBy": "2022-02-28T18:30:00.000Z",
"itemName": "vanilla essence",
"requirementStatusName": "pending",
"itemCategoryName":"someCategoryName"
}
]
You should use DB column name in Sequelize.col instead of its field counterpart in a model:
// let's say the model field is itemCategoryName and the column name in a table is item_category_name
Sequelize.col("itemCategory.item_category_name")
To query more than 2 tables using joins in sequelize we will have to use reference the table and column name correctly.
Instead of adding [Sequelize.col("itemCategory.itemCategoryName"),"itemCategoryName",] as an attribute to the referencing table and to get the response as a single json object without nesting we need to add this [Sequelize.col("item.itemCategory.itemCategoryName"),"itemCategoryName"] as the attribute to the table from which you are querying now
below is the edited code which returns json as expected
router.get("/", async (req, res) => {
const itemRequirements = await itemRequirement
.findAll({
include: [
{
model: item,
include: [
{model:itemCategory,attributes:[]},
{model:quantityType,attributes:[]}
],
attributes:[]
},
{ model: requirementStatus, attributes: [] },
],
attributes: [
"id",
"quantity",
"requiredBy",
[Sequelize.col("item.itemName"), "itemName"],
[
Sequelize.col("requirementStatus.requirementStatusName"),
"requirementStatusName",
],
//actual way of referencing the different tables to get an object without
//nesting
[Sequelize.col("item.itemCategory.itemCategoryName"),"itemCategoryName"],
[Sequelize.col("item.quantityType.quantityName"),"quantityTypeName"]
],
})
.then((itemRequirements) => {
console.log(JSON.stringify(itemRequirements,null,2));
res.json(itemRequirements);
});
});
module.exports = router;
output
[
{
"id": 4,
"quantity": 10,
"requiredBy": "2022-02-03T00:00:00.000Z",
"itemName": "choco",
"requirementStatusName": "pending",
"itemCategoryName": "Essence",
"quantityTypeName": "ml"
}
]

sequelize count associated table rows

Using sequelize and mySQL, I have two tables: User and Post.
Relation between two tables is M : N
db.User.belongsToMany(db.Post, { through: "Likes", as: "Liked" });
db.Post.belongsToMany(db.User, { through: "Likes", as: "Likers" });
What I want is getting post with whole likers id and count of whole likers.
I know how to get whole likers like this.
const post = await Post.findOne({
where: { id: postId },
attributes: ["id", "title", "imageUrl"],
include: [{
model: User,
as: "Likers",
attributes: ["id"],
through: { attributes: [] },
}]
})
// result
{
"id": 36,
"title": "test",
"imageUrl": "하늘이_1644886996449.jpg",
"Likers": [
{
"id": 13
},
{
"id": 16
}
]
}
And, I also know how to get count of whole likers.
const post = await Post.findOne({
where: { id: postId },
attributes: ["id", "title", "imageUrl"],
include: [{
model: User,
as: "Likers",
attributes: [[sequelize.fn("COUNT", "id"), "likersCount"]],
}]
})
// result
{
"id": 36,
"title": "test",
"imageUrl": "하늘이_1644886996449.jpg",
"Likers": [
{
"likersCount": 2
}
]
}
But, I don't know how to get both of them at once.
Check the result when I use both of them.
{
model: User,
as: "Likers",
attributes: ["id", [sequelize.fn("COUNT", "id"), "likersCount"]],
through: { attributes: [] },
}
// result
"Likers": [
{
"id": 13,
"likersCount": 2
}
]
It only shows 1 liker(id: 13)
It must show another liker(id: 16).
What is the problem?
It shows only one because COUNT is an aggregating function and it groups records to count them. So the only way to get both - use a subquery to count records in a junction table while getting records on the other end of M:N relationship.
const post = await Post.findOne({
where: { id: postId },
attributes: ["id", "title", "imageUrl",
// you probably need to correct the table and fields names
[Sequelize.literal('(SELECT COUNT(*) FROM Likes where Likes.postId=Post.id)'), 'LikeCount']],
include: [{
model: User,
as: "Likers",
attributes: ["id"],
through: { attributes: [] },
}]
})

Extract child objects from json object

I am struggling to convert xml to json object and then extracting nodes from the converted object. I am using busyboy to read from the file uploaded on the server. And after that I am using inspect to convert xml to json and then printing the json object. The final output seems as
{ declaration: { attributes: { version: '1.0', encoding: 'utf-8' } },
root:
{ name: 'order',
attributes:
{ orderid: '123456',
xmlns: 'http://www.someRandomNameSpace.com' },
children:
[ { name: 'orderperson',
attributes: {},
children: [],
content: 'str1234' },
{ name: 'shipto',
attributes: {},
children:
[ { name: 'name',
attributes: {},
children: [],
content: 'Adnan Ali' },
I want to read the 'name'='Adnan Ali' from this object how will that be done in nodejs ? I mean how can i reach to the object which has name='name' and content='Adnan Ali'.
The print command is
console.log(inspect(order, {colors: true, depth: Infinity}));
Since you are using NodeJS perhaps giving JSONPath a try would be a good idea. Then you can do something like this:
var jp = require("JSONPath");
var tobj = { "declaration": { "attributes": { "version": '1.0', "encoding": 'utf-8' } },
"root":
{ "name": 'order',
"attributes":
{ "orderid": '123456',
"xmlns": 'http://www.someRandomNameSpace.com' },
"children":
[ { "name": 'orderperson',
"attributes": {},
"children": [],
"content": 'str1234' },
{ "name": 'shipto',
"attributes": {},
"children":
[ { "name": 'name',
"attributes": {},
"children": [],
"content": 'Adnan Ali'
}
]
}
]
}};
var result = jp.eval(tobj, "$..children[?(#.name === 'name' && #.content === 'Adnan Ali')]");
console.log(result);
Example output:
[ { name: 'name',
attributes: {},
children: [],
content: 'Adnan Ali' } ]
(Don't forget to install JSONPath ;-))
Sources:
https://www.npmjs.com/package/JSONPath
http://goessner.net/articles/JsonPath/
You need to search the arrays of objects for the objects you are interested in. There are various ways to do that, including Array.prototype.find (not sure if it is available in all Node.js versions) and lodash _.find.
Using Array.prototype.filter a solution could look like this (not tested):
function findObject(array, key, value) {
var filtered = array.filter(obj => (obj[key] === value));
if (filtered.length !== 1) throw new Error('Found ' + filtered.length + ' objects with `' + key + '`=`' + value + '`, expected to find 1.');
return filtered[0];
}
var shipto = findObject(input.root.children, 'name', 'shipto');
var name = findObject(shipto.children, 'name', 'name').content;
console.log(name);
You should be able to reach the object with content: 'Adnan Ali' with this path data.root.children[1].children[0]:
const data = {
declaration: {
attributes: {
version: '1.0',
encoding: 'utf-8'
}
},
root: {
name: 'order',
attributes: {
orderid: '123456',
xmlns: 'http://www.someRandomNameSpace.com'
},
children: [{
name: 'orderperson',
attributes: {},
children: [],
content: 'str1234'
}, {
name: 'shipto',
attributes: {},
children: [{
name: 'name',
attributes: {},
children: [],
content: 'Adnan Ali'
}]
}]
}
};
console.log(data.root.children[1].children[0])
Explanation:
data is an object that contains a root object. root is an object that contains a children array. The second element in root.children (index 1) is an object that contains another children array that contains the object you're looking for at the first index (0).
Consider using object-scan. It's very powerful once you wrap your head around it.
// const objectScan = require('object-scan');
const find = (input) => objectScan(['**'], {
abort: true,
rtn: 'value',
filterFn: ({ value }) => value.content === 'Adnan Ali' && value.name === 'name'
})(input);
const tobj = { declaration: { attributes: { version: '1.0', encoding: 'utf-8' } }, root: { name: 'order', attributes: { orderid: '123456', xmlns: 'http://www.someRandomNameSpace.com' }, children: [{ name: 'orderperson', attributes: {}, children: [], content: 'str1234' }, { name: 'shipto', attributes: {}, children: [{ name: 'name', attributes: {}, children: [], content: 'Adnan Ali' }] }] } };
console.log(find(tobj));
// => { name: 'name', attributes: {}, children: [], content: 'Adnan Ali' }
.as-console-wrapper {max-height: 100% !important; top: 0}
<script src="https://bundle.run/object-scan#13.8.0"></script>
Disclaimer: I'm the author of object-scan

Bookshelf.js get pivot table attributes

I am trying to get attributes from my m-n relational mySql table using Bookshelf.js.
I have a table users: id, name
and tournaments: id, place
and a pivot table: user_id, tournament_id, teamname
Those are my models:
var User = Bookshelf.Model.extend({
tableName: 'users',
tournaments: function () {
return this.belongsToMany(Tournament);
}
});
var Users = Bookshelf.Collection.extend({
model: User
});
var Tournament = Bookshelf.Model.extend({
tableName: 'tournaments',
users: function () {
return this.belongsToMany(User);
}
});
var Tournaments = Bookshelf.Collection.extend({
model: Tournament
});
var Tournaments_Users = Bookshelf.Model.extend({
tableName: 'tournaments_users'
});
Now when I do
Tournaments.forge().fetch({withRelated: ['users']})
.then(function (collection) {
res.send(collection.toJSON());
})
I get
{
"id": 1,
"place": "Berlin",
"users": [
{
"id": 1,
"name": "Jim",
},
{
"id": 2,
"name": "Tom",
}, ...
}
What I want:
{
"id": 1,
"place": "Berlin",
"users": [
{
"id": 1,
"name": "Jim",
"teamname" : "Team A"
},
{
"id": 2,
"name": "Tom",
"teamname" : "Team B"
}, ...
}
Anyone knows how to do this using Bookshelf?
You may use .withPivot() method.
Use it like this :
users: function () {
return this.belongsToMany(User).withPivot(['teamname']);
}
In your return, you will get a field named _pivot_teamname. Just rename them to get it good.
Documentation : http://bookshelfjs.org/#Collection-instance-withPivot
As long as I know, there is no way to fetch the pivot fields with a custom name.

Sort a store data based on inner JSON field sencha

i have a json in the following format:
{
"collection": [
{
"id": 4,
"tickets": [
{
"price": 40,
},
{
"price": 50,
}
],
},
{
"id": 1,
"tickets": [
{
"price": 10,
},
{
"price": 15,
}
]
},
]
}
STORE:
Ext.define("myProject.store.ABCs", {
extend: "Ext.data.Store",
config: {
model: "myProject.model.ABC",
autoLoad: false,
proxy: {
type: "ajax",
url: '', //myURL
reader: {
type: "json",
rootProperty: "collection", // this is the first collection
},
},
}
});
For this particular JSON i created the models as:
Ext.define("myProject.model.ABC", {
extend: "Ext.data.Model",
config: {
idProperty: "id",
fields:[
{name: "id", type: "int" },
],
hasMany: [
{
model: "myProject.model.XYZ",
name: "tickets",
associationKey: "tickets",
},
],
}
});
And second model as:
Ext.define("myProject.model.XYZ", {
extend: "Ext.data.Model",
config: {
// idProperty: "id",
fields:[
{name: "inner_id", type: "int" },
],
belongsTo: 'myProject.model.ABC'
}
});
This particular code creates a store and populates the models correctly.
var store = Ext.getStore('ABCs');
Now i want to sort this store based on store.tickets().getAt(0).get('price') that is sort the ABC records based on XYZ's first price property.
In the above json. ABC Records will be: [{id:4}, {id:1}]
But since first price in XYZ (40 > 10), i want to sort them and create [{id:1}, {id:4}]
Take a look at the Ext.util.Sorter class, where you can set a sorterFn. See the example at the top of the page - you should be able to simply write the logic for sorting records the way you describe.