Sequelize and Graphql reverse lookup - express

I have two Models:
River.associate = models => {
River.hasMany(models.Fish, { as: 'Fishes' });
};
Fish.associate = models => {
Fish.belongsTo(models.River);
};
type River {
id: ID!
name: String!
alternative: String!
geojson: JSON
fishes: [Fish]
}
type Fish {
id: ID!
name: String!
}
How would I findAll Rivers given a list of Fish ID's? Im not sure how the query must look for this for graphql and sequelize?
type Query {
river(id: ID!): River
**rivers(fishIds: ARRAY): River ??????**
fishes: [Fish]
}
Query: {
rivers: (_, { fishIds }) => {
return River.findAll({
where: {
fishes: fishIds
}
});
},
}

You can specify WHERE clauses for each of the models you include. Moreover, doing so will convert the eager load to an inner join (which is what you want), unless you explicitly set the required param to false.
That means you should be able to do something like:
River.findAll({ include:
[
{
model: Fish,
where: { id: fishIds }
},
],
})

Related

Ordering nested / second level Arrays with Prisma?

I am using Prisma & PostgreSQL. Here I grab some stuff:
await prisma.items.findMany({
where: { itemId: itemId },
include: {
modules: {
include: {
lessons: true
}
}
}
});
I do not need to order the items themselves, but I would like to order the modules & lessons that I get back. Both have an INT property (called: number) on which I could perform the ordering, but I do not know how to do this with prisma / postgresql, or even if it's possible.
Any ideas?
You can use the orderBy operator for this.
Here's what the query would look like for your use-case:
const data = await prisma.items.findMany({
where: {itemId: itemId},
include: {
modules: {
orderBy: {
number: 'asc'
},
include: {
lessons: {
orderBy: {
number: 'asc'
}
}
}
}
}
})
The article on filtering and sorting contains more information on this.

How do I write this Raw Postgres query in sequelize

I Have this postgres RAW query, I wanted to write it in Sequelize. How do I do this since I have less idea about Writing queries having JOINS in Sequelize. I have made models and Associations.
These are models and associations.
TestParticipant.hasMany(ParticipantHistory, {
sourceKey: "id",
foreignKey: "participantId",
as: "paticipantStatuses"
})
ParticipantHistory.belongsTo(TestParticipant, {
foreignKey: "participantId",
as: "paticipantStatuses"
})
This is the raw Query I wanna transform into Sequelize query
SELECT participant_histories.participant_id,
participant_histories.created_at,participant_histories.previous_status,
participant_histories.status,test_participants.test_type_id,test_participants.id,
test_participants.email,test_participants.scheduled_at,test_participants.valid_till,
test_participants.is_proctored
FROM test_participants
INNER JOIN participant_histories ON test_participants.id=participant_histories.participant_id
WHERE user_id='${userId}'
AND participant_histories.status='${activity}'
AND participant_histories.created_at>='${isoDate}'
Because I don't see model definitions in the post I suggest only something like this:
// First of all you should correct an alias for TestParticipant like this
ParticipantHistory.belongsTo(TestParticipant, {
foreignKey: "participantId",
as: "paticipant"
})
const rows = await ParticipantHistory.findAll({
raw: true,
attributes: ['participant_id', 'created_at', 'previous_status', 'status'],
where: {
status: activity,
created_at: {
[Op.gte]: isoDate
}
},
include: [{
required: true // this turns into INNER JOIN
model: TestParticipant,
attributes: ['test_type_id', 'id', 'email', 'scheduled_at', 'valid_till', 'is_proctored'],
as: 'participant',
where: {
user_id: userId
}
}]
})

Typeorm - Find entries by ManyToMany relation

I am using Nestjs with Typeorm and Mysql, and I can't figure out a good way to filter entries by their many to many relation.
I have these 2 entities:
Group Entity:
#Entity({ name: 'groups' })
export class Group {
#ManyToMany(() => Tag, { eager: true })
#JoinTable()
tags: Tag[];
}
Tag Entity
#Entity({ name: 'tags' })
export class Tag {
#Column()
#Index({ unique: true })
tag?: string;
}
And would like to search all groups that have a tag with a specific text.
ie. all groups that have the tag.tag "sport"
Tried this code:
const args = {
where: [
{
'tags': In([Like(`%sport%`)]),
}
],
relations: ['tags'], // TAGS
take: filter.take,
skip: filter.skip,
order: filter.order
};
return super.findAll(args);
but it doesn't seem to work..
any help would be great!
return find({
where: {
tags: {
tag: Like(`%sport%`),
},
},
relations: ['tags'],
});
Almost, typeorm accepts an ObjectLiteral or keyof typeof Tags from relations like so:
FindConditions<T>: {
where: {
[s: keyof typeof T]: any,
},
}
That's not quite it but that's the general gist. And if the keyof T is a relation then any is replaced with keyof relation pretty much anyway.
This is the full type for findConditions https://github.com/typeorm/typeorm/blob/master/src/find-options/FindConditions.ts

Sequelize Many to Many Relationship using Through does not insert additional attributes

I have a many to many relationship between: Step and Control Through ControlsConfig.
When creating a Control object and call addStep function and specify the additional attributes (which exist in the relation table), Sequelize creates the records in the relational table ControlsConfig but the additional attributes are NULLs.
PS: The tables are creating correctly in the database.
Table 1: Step
Table 2: Control
Relation table: ControlsConfig
Step
var Step = sequelize.define('Step', {
title: { type: DataTypes.STRING, allowNull: false },
description: DataTypes.STRING,
type: { type: DataTypes.ENUM('task', 'approval'), allowNull: false, defaultValue: 'task' },
order: DataTypes.INTEGER
});
Step.associate = function(models) {
models.Step.belongsTo(models.User);
models.Step.belongsTo(models.Template);
models.Step.hasMany(models.Action);
};
Control
var Control = sequelize.define('Control', {
label: { type: DataTypes.STRING, allowNull: false },
order: { type: DataTypes.INTEGER },
type: { type: DataTypes.ENUM('text', 'yes/no') },
config: { type: DataTypes.TEXT },
controlUiId: { type: DataTypes.STRING }
});
Control.associate = function(models) {
models.Control.belongsTo(models.Section);
};
ControlsConfigs
module.exports = (sequelize, DataTypes) => {
var ControlsConfig = sequelize.define('ControlsConfig', {
visibility: { type: DataTypes.ENUM('hidden', 'readonly', 'editable', 'required') },
config: { type: DataTypes.TEXT }
});
ControlsConfig.associate = function(models) {
models.Control.belongsToMany(models.Step, { through: models.ControlsConfig });
models.Step.belongsToMany(models.Control, { through: models.ControlsConfig });
models.ControlsConfig.belongsTo(models.Template);
};
return ControlsConfig;
};
Insertion:
try {
var step1 = await Step.create({ /*bla bla*/ });
var control1 = await Control.create({ /*bla bla*/ });
var OK = await control1.addStep(step1, {through: { config: 'THIS FIELD ALWAYS APPEARS NULL' }});
} catch (error) { /* No errors*/ }
I am following the same strategy stated at the documentation
//If you want additional attributes in your join table, you can define a model for the join table in sequelize, before you define the association, and then tell sequelize that it should use that model for joining, instead of creating a new one:
const User = sequelize.define('user', {})
const Project = sequelize.define('project', {})
const UserProjects = sequelize.define('userProjects', {
status: DataTypes.STRING
})
User.belongsToMany(Project, { through: UserProjects })
Project.belongsToMany(User, { through: UserProjects })
//To add a new project to a user and set its status, you pass extra options.through to the setter, which contains the attributes for the join table
user.addProject(project, { through: { status: 'started' }})
You have to pass edit: true to the addProject and addStep method.
See this answer it has a similar issue
Sequelize belongsToMany additional attributes in join table

GraphQL queries with tables join using Node.js

I am learning GraphQL so I built a little project. Let's say I have 2 models, User and Comment.
const Comment = Model.define('Comment', {
content: {
type: DataType.TEXT,
allowNull: false,
validate: {
notEmpty: true,
},
},
});
const User = Model.define('User', {
name: {
type: DataType.STRING,
allowNull: false,
validate: {
notEmpty: true,
},
},
phone: DataType.STRING,
picture: DataType.STRING,
});
The relations are one-to-many, where a user can have many comments.
I have built the schema like this:
const UserType = new GraphQLObjectType({
name: 'User',
fields: () => ({
id: {
type: GraphQLString
},
name: {
type: GraphQLString
},
phone: {
type: GraphQLString
},
comments: {
type: new GraphQLList(CommentType),
resolve: user => user.getComments()
}
})
});
And the query:
const user = {
type: UserType,
args: {
id: {
type: new GraphQLNonNull(GraphQLString)
}
},
resolve(_, {id}) => User.findById(id)
};
Executing the query for a user and his comments is done with 1 request, like so:
{
User(id:"1"){
Comments{
content
}
}
}
As I understand, the client will get the results using 1 query, this is the benefit using GraphQL. But the server will execute 2 queries, one for the user and another one for his comments.
My question is, what are the best practices for building the GraphQL schema and types and combining join between tables, so that the server could also execute the query with 1 request?
The concept you are refering to is called batching. There are several libraries out there that offer this. For example:
Dataloader: generic utility maintained by Facebook that provides "a consistent API over various backends and reduce requests to those backends via batching and caching"
join-monster: "A GraphQL-to-SQL query execution layer for batch data fetching."
To anyone using .NET and the GraphQL for .NET package, I have made an extension method that converts the GraphQL Query into Entity Framework Includes.
public static class ResolveFieldContextExtensions
{
public static string GetIncludeString(this ResolveFieldContext<object> source)
{
return string.Join(',', GetIncludePaths(source.FieldAst));
}
private static IEnumerable<Field> GetChildren(IHaveSelectionSet root)
{
return root.SelectionSet.Selections.Cast<Field>()
.Where(x => x.SelectionSet.Selections.Any());
}
private static IEnumerable<string> GetIncludePaths(IHaveSelectionSet root)
{
var q = new Queue<Tuple<string, Field>>();
foreach (var child in GetChildren(root))
q.Enqueue(new Tuple<string, Field>(child.Name.ToPascalCase(), child));
while (q.Any())
{
var node = q.Dequeue();
var children = GetChildren(node.Item2).ToList();
if (children.Any())
{
foreach (var child in children)
q.Enqueue(new Tuple<string, Field>
(node.Item1 + "." + child.Name.ToPascalCase(), child));
}
else
{
yield return node.Item1;
}
}}}
Lets say we have the following query:
query {
getHistory {
id
product {
id
category {
id
subCategory {
id
}
subAnything {
id
}
}
}
}
}
We can create a variable in "resolve" method of the field:
var include = context.GetIncludeString();
which generates the following string:
"Product.Category.SubCategory,Product.Category.SubAnything"
and pass it to Entity Framework:
public Task<TEntity> Get(TKey id, string include)
{
var query = Context.Set<TEntity>();
if (!string.IsNullOrEmpty(include))
{
query = include.Split(',', StringSplitOptions.RemoveEmptyEntries)
.Aggregate(query, (q, p) => q.Include(p));
}
return query.SingleOrDefaultAsync(c => c.Id.Equals(id));
}