How to add the new field to Object in Purescript - 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

Related

How MongoClient::save(...) might change the _id field of document parameter

I have a class User that embeds a JsonObject to represent the user's fields. This class looks like that:
class User {
private JsonObject data;
public User(...) {
data = new JsonObject();
data.put("...", ...).put(..., ...);
}
public String getID() { return data.getString("_id"); }
// more getters, setters
// DB access methods
public static userSave(MongoClient mc, User user){
// some house keeping
mc.save("users", user.jsonObject(), ar -> {
if(ar.succeeded()) { ... } else { ... }
});
}
}
I've just spent more than half a day trying to figure out why a call to user.getID() sometimes produced the following error: ClassCastException: class io.vertx.core.json.JsonObject cannot be cast to class java.lang.CharSequence. I narrowed down to the userSave() method and more specifically to MongoClient::save() which actually produces a side effect which transforms the data._id from something like
"_id" : "5ceb8ebb9790855fad9be2fc"
into something like
"_id" : {
"$oid" : "5ceb8ebb9790855fad9be2fc"
}
This is confirmed by the vertx documentation which states that "This operation might change _id field of document parameter". This actually is also true for other write methods like inserts.
I came with two solutions and few questions about doing the save() properly while keeping the _id field up to date.
S1 One way to achieve that is to save a copy of the Json Object rather than the object itself, in other words : mc.save("users", user.jsonObject().copy(), ar -> {...});. This might be expensive on the long run.
S2 An other way is to "remember" _id and then to reinsert it into the data object in the if(ar.succeeded()) {data.put("_id", oidValue); ...} section. But as we are asynchronous, I don't think that the interval between save() and the data.put(...) is atomic ?
Q1: Solution S1 make the assumption that the ID doesn't change, i.e., the string 5ceb8ebb9790855fad9be2fc will not change. Do we have a warranty about this ?
Q2: What is the right way to implement the saveUser() properly ?
EDIT: The configuration JSON object user for the creation of the MongoClient is as follows (in case there is something wrong) :
"main_pool" : {
"pool_name" : "mongodb",
"host" : "localhost",
"port" : 27017,
"db_name" : "appdb",
"username" : "xxxxxxxxx",
"password" : "xxxxxxxxx",
"authSource" : "admin",
"maxPoolSize" : 5,
"minPoolSize" : 1,
"useObjectId" : true,
"connectTimeoutMS" : 5000,
"socketTimeoutMS" : 5000,
"serverSelectionTimeoutMS" : 5000
}

mongoengine query to nested listfield

I made article system with python flask.
To communicate with mongodb, use flask_mongoengine
Here is my model.
class SubComment(EmbeddedDocument):
no = SequenceField()
body = StringField()
class Comment(EmbeddedDocument):
no = SequenceField()
body = StringField()
sub_comment = ListField(EmbeddedDocumentField(SubComment))
class Article(Document):
title = StringField()
body = StringField()
comments = ListField(EmbeddedDocumentField(Comment))
SubComment model stored into Comment model and Comment model stored into Article model.
So, this is the output that I want.
{
"_id" : ObjectId("5c0641d81b48d9fe50dfdd7f"),
"title" : "test",
"body" : "gogo",
"comments" : [
{
"no" : 1,
"body" : "first comment",
"sub_comment" : [
{
"no": 1,
"body": "sub comm"
}
]
}
]
}
When I insert Comment model to Article model, just use below code.
comment = Comment(
body='first comment'
)
article = Article.objects(body='gogo').first()
article.comments.append(comment)
article.save()
But when I try to insert SubComment to Comment, it throw errors -> AttributeError: 'BaseList' object has no attribute 'sub_comment'
Below is the code I used.
comment = SubComment(
body='sub comment'
)
article = Article.objects(title='test', comments__no=1).first()
article.comments.sub_comment.append(comment)
article.save()
After some searched, people said there is no way to insert nested field.
Is there any solution here? I have to use raw query?
Thanks!
[SOLVED]
I solved with for loop like this.
count = 0
article = Article.objects(title='test', comments__no=1).first()
for comment in article.comments:
if comment.no == 1:
print(comment.no)
count = comment.no - 1
article.comments[count].sub_comment.append(sub_comment)
article.save()
But I don't know it is the right way.
If you have any other solution, please comment it.

Translate postgres query to sequelize

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.

React Component iterate/loop properties of Mongo object

I do have a Mongo collection that stores albums with predefined "slot" for its images and feel a bit stuck if there a way to loop over the properties of the collection in order to display images in separated divs.
I did used this code for mapping over the album covers and it worked great:
albums() {
return Albums.find().fetch();
}
{this.albums().map( (album) => {
return <div key={album._id}><img src={album.cover} /></div>
})}
But now I ask you to help, is it possible to loop over photoOne, photoTwo, etc... and skip/don't display data if it is empty like in photoThree for example.
{
"_id" : "CHMHbNWWwZGaLGvB6",
"title" : "Text",
"cover" : "link",
"createdAt" : date,
"photoOne" : {
"titleOne" : "Text",
"coverOne" : "link"
}
"photoTwo" : {
"titleTwo" : "Text",
"coverTwo" : "link"
}
"photoThree" : {
"titleThree" : "",
"coverThree" : ""
}
}
I'm not a Mongo user, but in the map function you can check for the existing values, and handle it there. Something like (there is surely a cleaner way, though):
this.albums().map( (album) => {
for (key in album){
if (key.startsWith('photo')){
var title = Object.keys(album[key])[0];
if (album[key][title].length != 0){
console.log("Can use: " + Object.keys(album[key])[0])
}
}
}
})
Results in:
Can Use This: titleOne
Can Use This: titleTwo
Hope that helps, but it seems like having the photos with photoOne, photoTwo you are limiting the number of photos to use and requiring the need to use Object.keys to get the values out (without specifically using album.photoOne, album.photoTwo, etc.
If the album photos were stored in an embedded document, you could just include the photos and titles that existed and avoid having to check for empty ones. You would just loop through the photos that are present....if that makes sense.

Return a casbah type findOne in a method

I'm making a system using scala and I make a method that return a "findOne".
Follow my code
def findOneByField(field:String, value:String): Any = {
val search: DBObject = MongoDBObject(field -> value)
return User.coll.findOne(search).get
}
I put the return type like Any because I cannot make equals MongoDb findOne return type.
I want to make this
val o = MongoDBObject("email"->"myemail#gmail.com")
findOneByField("email","myemail#gmail.com") == coll.findOne(o)
Boolean True
But if I get the type of coll.findOne, I receive this type:
res10: Option[com.myapptest.models.User.coll.T] = Some({ "_id" : { "$oid" : "57828cfe3b931047e1bb5455"} , "email" : "myemail#gmail.com" , "name" : "My Name" , "id" : "34863249182"})
I have no idea how return the same findOne in the method.
Could you help me?
Thanks! xD