Translate postgres query to sequelize - postgresql

I am having trouble connecting relationships in sequelize.
SELECT * from actors
JOIN "actorStatuses"
on "actorStatuses".actor_id = actors.id
JOIN movies
on movies.id = "actorStatuses".actor_id
WHERE movies.date = '7/8/2017';

Here you go :
model.Actor.findAll({ // change model name as per yours
include : [
{
model : model.ActorStatuses // change model name as per yours
required : true ,
},{
model : model.Movies // change model name as per yours
required : true ,
where : { date : 'your_date' }
}
]
});
This will create exact same query / result as you required.

Related

how to add multiple fileds of the same name with different data

so what i am doing is sending this data to the the firebase firestore here's my code its working fine,
final data = {
"sent_requests" : {
"sender" : "${loggedInUser.uid}",
"receiver" : args.uid,
"details" : {
"date" : detail_class.datetime,
"total_dishes" : detail_class.total_dishes,
"total_people" : detail_class.total_people,
"meals" : detail_class.meals,
"location" : detail_class.location,
}
}
};
dynamic db = FirebaseFirestore.instance;
db.collection("users").doc("${loggedInUser.uid}").set(data, SetOptions(merge: true));
This result is fine, but this code just overwrites the previous request. I instead want to add more requests in it,
when the sender and receiver are different.
Field names are unique in Firestore, so you can't have (for example) two receiver fields.
What you can have is a single field that is an array of values. I'd typically call that receivers (plural) to indicate that it's a multi-value field, and you'd write that from your code with:
final data = {
"sent_requests" : {
...
"receivers" : [args.uid],
...
}
};
This sets the receivers field to an array with just the value or args.uid. If you want to merge the args.uid with any existing values in the database already, you can use an array-union operator:
final data = {
"sent_requests" : {
...
"receivers" : FieldValue.arrayUnion([args.uid]),
...
}
};
Now the args.uid value will be added to the receivers array, unless it's already in there.

How to add the new field to Object in Purescript

I am first about the Purescript.
I am going to add the new field to Object and send it as a function param.
But I can not find a good solution for this.
For example.
oldFiled = {
title : "title",
description : "d"
}
newField = {
time : "time"
}
//result after added new field
oldFiled = {
title : "title",
description : "d",
time : "time"
}
How can I do it?
If it's just about adding a single field you can use https://pursuit.purescript.org/packages/purescript-record/2.0.1/docs/Record#v:insert like so:
import Data.Record as Record
import Data.Symbol (SProxy(..))
oldFiled = {
title : "title",
description : "d"
}
newFiled = Record.insert (SProxy :: _ "time") "time" oldFiled
If you're merging records look at the merge union and disjointUnion functions in the Data.Record module

Mongo DB - map relational data to document structure

I have a dataset containing 30 million rows in a mongo collection. An example set of records would be:
{"_id" : ObjectId("568bc0f2f7cd2653e163a9e4"),
"EmailAddress" : "1234#ab.com",
"FlightNumber" : 1043,
"FlightTime" : "10:00"},
{"_id" : ObjectId("568bc0f2f7cd2653e163a9e5"),
"EmailAddress" : "1234#ab.com",
"FlightNumber" : 1045,
"FlightTime" : "12:00"},
{"_id" : ObjectId("568bc0f2f7cd2653e163a9e6"),
"EmailAddress" : "5678#ab.com",
"FlightNumber" : 1045,
"FlightTime" : "12:00"},
This has been imported directly from SQL server, hence the relational'esque nature of the data.
How can I best map this data to another collection so that all the data is then grouped by EmailAddress with the FlightNumbers nested? An example of the output would then be:
{"_id" : ObjectId("can be new id"),
"EmailAddress" : "1234#ab.com",
"Flights" : [{"Number":1043, "Time":"10:00"},{"Number":1045, "Time":"12:00"}]},
{"_id" : ObjectId("can be new id"),
"EmailAddress" : "5678#ab.com",
"Flights" : [{"Number":1045, "Time":"12:00"}]},
I've been working on an import routing that iterates through each record in the source collection and then bulk inserts into the second collection. This is working fine however doesn't allow me to group the data unless I back process through the records which adds a huge time overhead to the import routine.
The code for this would be:
var sourceDb = db.getSiblingDB("collectionSource");
var destinationDb = db.getSiblingDB("collectionDestination");
var externalUsers=sourceDb.CRM.find();
var index = 0;
var contactArray = new Array();
var identifierArray = new Array();
externalUsers.forEach(function(doc) {
//library code for NewGuid omitted
var guid = NewGuid();
//buildContact and buildIdentifier simply create 2 js objects based on the parameters
contactArray.push(buildContact(guid, doc.EmailAddress, doc.FlightNumber));
identifierArray.push(buildIdentifier(guid, doc.EmailAddress));
index++;
if (index % 1000 == 0) {
var now = new Date();
var dif = now.getTime() - startDate.getTime();
var Seconds_from_T1_to_T2 = dif / 1000;
var Seconds_Between_Dates = Math.abs(Seconds_from_T1_to_T2);
print("Written " + index + " items (" + Seconds_Between_Dates + "s from start)");
}
//bulk insert in batches
if (index % 5000 == 0) {
destinationDb.Contacts.insert(contactArray);
destinationDb.Identifiers.insert(identifierArray);
contactArray = new Array();
identifierArray = new Array();
}
});
Many thanks in advance
Hey there and welcome to MongoDB. In this situation you may want to consider using two different Collections -- one for users and one for flights.
User:
{
_id:
email:
}
Flight:
{
_id:
userId:
number: // if number is unique, you can actually specify _id as number
time:
}
In your forEach loop, you would first check to see if a user document with that specific email address already exists. If it doesn't, create it. Then use the User document's unique identifier to insert a new document into the Flights collection, storing the identifier under the field userId (or maybe passengerId?).

Firebase many to many relationship retrieve data

I have the following data structure , users and services have many to many
relationship
users : {
user1 : {
name : blah,
email : a#a.com,
services : {
servicekey1 : true,
servicekey9 : true
}
}
}
services : {
servicekey1 : {
name : blahserve,
category : blahbers,
providers : {
user1 : true,
user7 : true
}
}
}
I want to get the list of user objects for a service. How can this be done with and without angularfire.
I came up with this query (without angularfire) -
var refu = new Firebase("https://myapp.firebaseio.com/users");
var refs = new Firebase("https://myapp.firebaseio.com/services");
refs.child("servicekey1/providers").once("value",function(s){
var users = s.val();
angular.forEach(users, function(v,k){
refu.child(k).once("value", function(su){
console.log(su.val());
})
})
});
This solves my purpose but I feel there should be a better way to do it , may be with angular fire. Please suggest if there are any other/ better ways to achieve this?

How should you query a collection via nested arrays in mongodb (Meteor)?

I'm dont think that this is a Meteor specific question, but rather around mongo and building mongo queries.
If I have the following structure,
{
username : someName,
contacts : [
{
userid : asdfae33rtqqxxx,
name : contactName,
status : friend
}
{
userid : asdfae33rtqqxxx,
name : anotherName,
status : pending
}
{
userid : asdfae33rtqqxxx,
name : contactName,
status : blocked
}
]
}
How could I pass in values from this array into a query against the users collection, to a) get the users in this array, or b) get all users in this array from the users collection with a particular status.
If this is not possible, how should I adjust my schema in order to make these sorts of queries possible?
This function will return a Meteor.users cursor based on an array of contacts and an optionally required status:
var usersByContacts = function(contacts, requiredStatus) {
var userIds = _.chain(contacts)
.map(function(c) {
if (requiredStatus) {
if (c.status === requiredStatus)
return c.userid;
} else {
return c.userid;
}
})
.compact()
.value();
return Meteor.users.find({_id: {$in: userIds}});
};
You can use it like this:
var users1 = usersByContacts(thing.contacts);
var users2 = usersByContacts(thing.contacts, 'pending');
This assumes thing has the schema referenced in your question. Also note that if you find this is a common pattern in your code, you should consider turning usersByContacts into a transform.