How to find an entry by property in kvs - erlang

How to find an entry by property or predicate function in kvs? For example:
kvs:find(fun(X) -> X#user.name == "Alexander").
I can use this:
lists:filter(Predicate, kvs:all(entity)).
But I don't want to load all entries into memory.

I have found that kvs is key-value storage, so we have to search entry only by keys specified in the entity table schema:
-record(user, {
id,
userName,
password
}).
#table{name=user,fields=record_info(fields,user), keys = [userName]}
Then we can do this:
kvs:index(user, userName, "Alexander").

Related

Nestjs swagger array of strings with one parameter

When I send only one parameter, I got query result like string, not like string[]. This heppend only from UI swagger, if I send from Postman - it works good.
I just want send from swagger-ui one parammeter and got array of string, not string
How I can fix it? Help me please.
Example1: send one paramenter and in my controller I got string like '25'
Example2: when I send 2 parameters in controller I can see array of strings ('25', '21')
export class List {
#ApiProperty({ isArray: true, type: String, required: false })
#IsOptional()
public categories?: string[];
}
You should try to spread your parameter in a const in services
edit:
I don't know how to explain in formal words, but a array of strings of one item, for JAVASCRIPT, seems with the same thing as one string value.
Because array is not a type, but a form of a type....
So, if you, in your controller, before do anything with it, you redeclare as:
#Get(":id")
findManybyId(#Param("id") id: string[]) {
const idArray = [...id];
return await this.service.findManyById(idArray);
}
It will solve your problem about being an array
old answer:
You should try to change in your controller where you make your input decorator.
in your case, i don't know if you are using ID to get, but you must to do as the example:
#ApiOperation({
summary: "Get many categories by ID",
})
async getMany(
#Param("id") ids: string[],
) {
return await this.categoriesService.getMany(id);
}
when you fill a single category the query param will be translated as a string while when you fill in several categories understand it as a array.
to solve this problem I added in DTO :
#Transform(({ value }) => (Array.isArray(value) ? value : Array(value)))
I force the cast to array

In firebase, how to set user details on the basis of their e-mail id's instead of UID or other credentials?

I want to change some fields in user details that correspond to a certain e-mail id manually.
Currently I have to look in authentication console for the e-mail id corresponding to UID(which is basis of storing user details now) but I don't want to do that.
Another option I thought was storing user details on the basis of their mobile numbers(as it is equally easily recognisible as email-id), but this will make login system on the basis of phone number instead of email-id.
Main problem is dot in email-id's that don't let email-id's to store as keys.
It's true, Firebase does not allow in it's key symbols like . (dot). There are also other symbols like $ (dollar sign), [ (left square bracket), ] (right square bracket), # (hash or pound sign) and / (forward slash) that are also forbidden.
So in order to solve your problem, you need to encode the email address like this:
name#email.com -> name#email,com
To achieve this, i recomand you to use the following method:
static String encodeUserEmail(String userEmail) {
return userEmail.replace(".", ",");
}
And to decode the email, you can use the following method:
static String decodeUserEmail(String userEmail) {
return userEmail.replace(",", ".");
}

In my neo4j db, the graphql mutation is not working as expected for 2 types

I am using neo4j dB and apollo-graphql server for my test project. I am trying to create a Q&A app where a user could create a question (with unique id) and then it would be answered by other user (similar to stackoverflow but with very minimum features).I want to create relationship between "Questions" node and "Users" node in my neo4j db via mutation.
so, in the mutations I created User Type and Question Type. But when I am trying to create relation between the user (who created question) and the question, the mutation is not working as expected.
Here is the graphql schema...
type Question {
questionID: Int! # unique id for a particular question
title: String! # Question Title
details: String!
createdBy: User!
}
type User {
userID: Int!
name: String!
email: String
questions: [Question] # specific questions related to the user
}
type Mutation {
createUser(name: String!, userID: Int!): [User]
createQuestion(questionID: Int!, title: String!, details: String!,userID: Int! ): Question
And here is the mutation snippet from resolver...
Mutation: {
createQuestion(_,params){
let session = driver.session();
let query = "MERGE (q:Questions {questionID:{questionID},title:
{title},details:{details},userID:{userID}})-[:CREATED_BY]->
(u:Users{userID:{userID}}) RETURN q;"
return session.run(query,params)
.then( result => {
return result.records.map( record => {
return record.get("q").properties
}
)
}
)
},
}
I had already inserted a dummy user with unique userID "1" in neo4j db before running this mutation query. And I ran this "createQuestion" mutation in graphiQL to create a question for the existing user and so I passed userID "1" as argument,
//createQuestion(questionID: Int!, title: String!, details: String!,userID: Int! )
And after running this mutation query, I was hoping to get a relation of [:CREATED_BY] between the existing user and the question created,
But my database now has 2 users with same user id "1" (i.e. one more user with same userid "1" has been inserted and the relationship is now present between this newly inserted user and question node).
What I need is to avoid to create new duplicate user with same id , and rather just to create relationship between the existing user and the question. So could someone please let me know what I have mistaken here ?
You must change your query:
let query = "
MERGE (u:Users{userID:{userID}})
MERGE (q:Questions {questionID:{questionID},title:
{title},details:{details},userID:{userID}})
MERGE (q)-[:CREATED_BY]->(u)
RETURN q;"
As you have the questionID already in place, which i'm guessing is a unique identifier I would change the query to:
let query = "
MERGE (u:Users{userID:{userID}})
MERGE (q:Questions {questionID:{questionID})
ON CREATE SET q.title=
{title},q.details={details}
MERGE (q)-[:CREATED_BY]->(u)
RETURN q;"
Notice I also removed userID property from question as you do not need it as you already create a relationship from User to Question
Edit:
You can solve the problem from the comment in two ways:
by adding UserID property to question:
let query = "
MERGE (u:Users{userID:{userID}})
MERGE (q:Questions {questionID:{questionID},userID:{userID}})
ON CREATE SET q.title= {title},q.details={details},
MERGE (q)-[:CREATED_BY]->(u)
RETURN q;"
Or with local merges:
let query = "
MERGE (u:Users{userID:{userID}})
MERGE (u)<-[:CREATED_BY]-(q:Questions {questionID:{questionID}})
ON CREATE SET q.title= {title},q.details={details},
RETURN q;"

When to use uid and when to use $id in angularfire

$id The key where this record is stored. The same as obj.$ref().key
To get the id of an item in a $firebaseArray within ng-repeat, call $id on that item.
These two are from the angular fire reference:
https://github.com/firebase/angularfire/blob/master/docs/reference.md
What I understand is if there is firebase object created with :
var object = $firebaseObject(objectRef);
then I can use uid always.
uid : object.uid
But I saw examples where the firebase auth user is used with $id.
return Auth.$requireSignIn().then(function (firebaseUser) {
return Users.getProfile(firebaseUser.uid).$loaded().then(function (profile) {
**profile.uid or profile.$id here**
Also is it possible the object to have uid but not to have $id (obj.$ref().key). Aren't they the same thing? Does the object have to be loaded first with $loaded() to use $id or uid?
Regards
You seem to be confusing two concepts:
the object.$id of an AngularFire object contains the key of that object in the Firebase Database.
a firebaseUser.uid in Firebase terms is the identification of a Firebase Authentication user.
It is common to store your Firebase Authentication users in the database under their uid, in which case user.$id would be their uid. But they are still inherently different things.
Users
uid1
displayName: "makkasi"
uid2
displayName: "Frank van Puffelen"
So if you we look at the code snippet you shared:
return Auth.$requireSignIn().then(function (firebaseUser) {
return Users.getProfile(firebaseUser.uid).$loaded().then(function (profile) {
The first line requires that the user is signed-in; only then will it execute the next line with the firebaseUser that was signed in. This is a regular JavaScript object (firebase.User), not an AngularFire $firebaseObject.
The second line then uses the firebaseUser.uid property (the identification of that user) to load the user's profile from the database into an AngularFire $firebaseObject. Once that profile is loaded, it executes the third line.
If the users are stored in the database under their uid, at this stage profile.$id and firebaseUser.uid will be the same value.

Assertion failure in +[Sync changes:inEntityNamed:predicate:parent:inContext:dataStack:completion:]

I'm using Sync from HyperOslo i get a simple JSON object:
Printed json object (user) =>
[{
email = "email#email.fr";
name = "Damian Menestrel";
}]
...to convert in Core Data User with the method:
Sync.changes(user , inEntityNamed: "User", dataStack: DataManager.dataStack, completion: { (response ) -> Void in
})
The app crash with this error:
Assertion failure in +[Sync
changes:inEntityNamed:predicate:parent:inContext:dataStack:completion:],
.../Pods/Sync/Source/Sync.m:77
Where this error come from?
My CodeData model is :
User.swift
import Foundation
import CoreData
import UIKit
class User: NSManagedObject {
#NSManaged var email: String?
#NSManaged var name: String?
}
Short answer:
Just add a remoteID attribute to your Core Data model as a primary key and it will work. This will map to the id attribute in your JSON.
Long answer:
Taken from the Primary Key section on Sync's README.
Sync requires your entities to have a primary key, this is important
for diffing otherwise Sync doesn’t know how to differentiate between
entries.
By default Sync uses id from the JSON and remoteID from Core
Data as the primary key. You can mark any attribute as primary key by
adding hyper.isPrimaryKey and the value YES.
For example in our Designer
News
project we have a Comment entity that uses body as the primary
key.

Resources