I am using moleculer micro services and postgres database with modules moleculer-db-adapter-sequelize and Sequelize. Every service have one table model. For e.g. user.service.js has user table model, application.service.js has application table model. I want to get data from both tables using join. I tried with join query but it returns
Relation does not exist
How to make joins of two models in moleculer architecture?
moleculer-db works as one-service-one-table concept. You should write your custom DB service to implement multiple models & joins.
In Moleculer level, the populate can be used to join data between services.
Populate example:
settings: {
fields: ["_id", "author", "article", "body", "createdAt", "updatedAt"],
populates: {
"author": {
action: "users.get",
params: {
fields: ["_id", "username", "bio", "image"]
}
}
}
},
Related
I'm trying to create a new entry into my table that has a relationship called "organizationAdmins". In that relationship is another relationship called "Role". I need to look up Role based on some criteria to connect. I've tried many different iterations to get this to work but the best I have is this, which doesn't work unfortunately...
let current_organization = await prisma.organization.create({
data: {
name: organization.name,
organizationAdmins: {
create : [{
account_id: account_id,
Role: {
connect : {
where: {name:"OWNER", owner:1, role:1, write:1, read:1}
}
}
}]
}
},
});
So if I were to take the SQL example for what I'm trying to do it would be this...
select id from role where name='OWNER' and owner=1 and role=1 and write=1 and read=1
I could pull the role_id like I did for account_id separately. But I'm trying to use prisma do the work for me so that we don't have multiple queries going on (less efficient). Welcome any thoughts here.
I need advice from experienced NoSQL engineers on how I should structure my data.
I want to model my SQL data structure to NoSQL for Google Cloud Firestore.
I have no prior experience with NoSQL databases but I am proficient with traditional SQL.
I use Node.js for writing queries.
So far, I converted three tables to JSON documents with example data:
{
"session": {
"userId": 99992222,
"token": "jwttoken1191891j1kj1khjjk1hjk1kj1",
"created": "timestamp"
}
}
{
"user": {
"id": 99992222,
"username": "userName",
"avatarUrl": "https://url-xxxx.com",
"lastLogin": "2019-11-23 13:59:48.884549",
"created": "2019-11-23 13:59:48.884549",
"modified": "2019-11-23 13:59:48.884549",
"visits": 1,
"profile": true,
"basketDetail": { // I get this data from a third party API
"response": {
"product_count": 2,
"products": [
{
"product_id": 111,
"usageInMinutes_recent": 0,
"usageInMinutes": 0,
"usageInMinutes_windows": 0,
"usageInMinutes_mac": 0,
"usageInMinutes_linux": 0
},
{
"product_id": 222, // no recent usage here
"usageInMinutes": 0,
"usageInMinutes_windows": 0,
"usageInMinutes_mac": 0,
"usageInMinutes_linux": 0
}
]
}
}
}
}
{
"visitor": {
"id": 999922221,
"created": "2019-11-23 13:59:48.884549"
}
}
My questions:
session.userId, user.id, visitor.id can all signify the same user. What is the Firestore equivalent to foreign keys in SQL? How would I connect/join these three collections in a query?
What do I do about the nested object basketDetail? Is it fine where it is or should I define its own collection?
I anticipate queries
occasionally add all the recent usage.
frequently check if a user owns a specific product_id
frequently replace the whole baskedDetail object with new data.
occasionally update one specific product_id.
How would I connect collections user with basketDetail in a query if I separated it?
Thanks for the advice!
session.userId, user.id, visitor.id can all signify the same user. What is the Firestore equivalent to foreign keys in SQL? How would I connect/join these three collections in a query?
Unfortunately, there is not JOIN clause in Firestore. Queries in Firestore are shallow, can only get elements from the collection that the query is run against. There is no way you can get documents from two collections in a single query unless you are using collection group query, but it's not the case since the collections in your project have different names.
If you have three collections, then three separate queries are required. There is no way you can achieve that in a single go.
What do I do about the nested object basketDetail? Is it fine where it is or should I define its own collection?
There are some limits when it comes to how much data you can put into a document. According to the official documentation regarding usage and limits:
Maximum size for a document: 1 MiB (1,048,576 bytes)
As you can see, you are limited to 1 MiB total of data in a single document. So if you think that nested object basketDetail can stay within this limitation then you can use that schema, otherwise, add it to a subcollection. Besides that, all those operations are permitted in Firestore. If you'll have hard times implementing them, post another question so we can take a look at it.
How would I connect collections user with basketDetail in a query if I separated it?
You cannot connect/join two collections. If you separate basketDetail in a subcollection, then two queries are required.
Is it possible to make an association in sequelize from a model with a column which is array of json
timeLine:[
{
userId:2,
status:Started
},
{
userId:3,
status:Ended
}
]
timeLine is a column in case table.
I want a cases which was started by given user.
On a high level you need to create two models.
User
Case
User would have a foreign key id to the Case table.
Next in the Case model, establish a one-to-many relationship so that you can do a Case.timeline to pull up the array of users.
// example of how to establish the one-to-many relationship
Case.hasMany(models.user, {
as: 'timeline',
foreignKey: {
name: 'caseId',
allowNull: false
}
});
I want to connect Apache Cassandra to apollo graphql server. Is there any ORM library available for cassandra in node just like Sequelize for relational databases.
Can any one please help me out
Maybe this could help:
http://express-cassandra.readthedocs.io/en/stable/
From the description of the page: Express-Cassandra is a Cassandra ORM/ODM/OGM for NodeJS with Elassandra & JanusGraph Support.
There is an automatically generated GraphQL API for Cassandra that you can run as a service to expose your CQL tables via GraphQL.
GitHub repo: https://github.com/datastax/cassandra-data-apis
Small Example:
Say you have the a table called books
CREATE TABLE library.books (
title text PRIMARY KEY,
author text
);
It will automatically generate these APIs
schema {
query: Query
mutation: Mutation
}
type Query {
books(value: BooksInput, orderBy: [BooksOrder], options: QueryOptions): BooksResult
booksFilter(filter: BooksFilterInput!, orderBy: [BooksOrder], options: QueryOptions): BooksResult
}
type Mutation {
insertBooks(value: BooksInput!, ifNotExists: Boolean, options: UpdateOptions): BooksMutationResult
updateBooks(value: BooksInput!, ifExists: Boolean, ifCondition: BooksFilterInput, options: UpdateOptions): BooksMutationResult
deleteBooks(value: BooksInput!, ifExists: Boolean, ifCondition: BooksFilterInput, options: UpdateOptions): BooksMutationResult
}
And you can query them with
mutation {
catch22: insertBooks(value: {title:"Catch-22", author:"Joseph Heller"}) {
value {
title
}
}
}
query {
books (value: {title:"Catch-22"}) {
values {
title
author
}
}
}
I am executing join query in node.js using sequelize . How to write join queries using sequelize ? thank you in advance. have a good day.
You first need to define associations between your models. If I have two models User and Customers, I can define and association like this
User.belongsTo(Customer);
Customer.hasMany(User);
Then when you query you can join by specifying include in the options
Customer.finOne({
where: {
id: 1,
},
include: {
model: User,
where: {
id: 4,
}
}
}