Firebase query (in swift) to determine if username already exists in database - ios

I have very little experience with Firebase and iOS and was wondering if it was possible to create a query in Swift to find specific data in the Firebase's JSON structure.
When a user creates an account in the app, he is prompted to chose a username. When the user presses on the "Continue" button on this same screen, I would like to query the DB in real-time to determine if the username the user chose already exists or not.
I read a bunch of articles on the web but couldn't find anything that would let me achieve my goal. So far, I wrote the following code which doesn't work, but I am unable to pinpoint the source of the problem (note that username.text is the textfield where the user enters his desired username and with which the verification is made) :
FIRDatabase
.database()
.reference()
.child("username")
.queryOrderedByValue()
.queryEqual(toValue: self.username.text!)
.observeSingleEvent(of: .value, with: {(snap) in
if snap.exists(){
// Username unavailable, choose another one
} else {
// Username available, OK to keep this username
}
})
The DB structure is as follows :
{
"users" : {
USER_1_UID : {
"account" : {
"email" : "test#stackoverflow.com",
"first name" : "myFirstName",
"last name" : "myLastName",
"username" : "my.username"
}
}
USER_2_UID : {
"account" : {
"email" : "test#stackoverflow.com",
"first name" : "myFirstName2",
"last name" : "myLastName2",
"username" : "my.username2"
}
}
}
}
I am open to change my DB structure to add the "usernames" note as shown below which will contain all of the usernames registered if it can make the querying easier :
{
"users" : {
USER_1_UID : {
"account" : {
"email" : "test#stackoverflow.com",
"first name" : "myFirstName",
"last name" : "myLastName",
"username" : "my.username"
}
}
USER_2_UID : {
"account" : {
"email" : "test#stackoverflow.com",
"first name" : "myFirstName2",
"last name" : "myLastName2",
"username" : "my.username2"
}
}
}
"listOfUsernames" : {
USER_1_UID : "my.username"
USER_2_UID : "my.username2"
}
}
Any help at this point will be welcome. Thank you!

Your code is pretty close. Three changes:
Remove queryOrderedByValue
then, you've buried your user information too deep
USER_1_UID : {
"account" : {
"email" : "test#stackoverflow.com",
"first name" : "myFirstName",
"last name" : "myLastName",
"username" : "my.username"
}
}
Change to (remove the account node name as it's not needed)
USER_1_UID : {
"email" : "test#stackoverflow.com",
"first name" : "myFirstName",
"last name" : "myLastName",
"username" : "my.username"
}
and then you need to tell Firebase which node you are querying
so change to
let usersRef = rootRef.child("users")
let queryRef = usersRef.queryOrdered(byChild: "username")
.queryEqual(toValue: "some user name")
queryRef.observeSingleEvent(of: .value, with: { snapshot in
print(snapshot) //you need snapshot, or snap or some other var name
})
Lastly, you don't need a listOfUsernames node, your users node will work just fine.

Related

Elastic Search Update Operation not working in nodejs client

I am updating the existing document in elastic search by below update query.
const result = await client.update({
index,
id,
body: {
script: {
lang: "painless",
source: "ctx._source = params.customerDetailsObj",
params: {
customerDetailsObj: item
}
}
}
});
where item has below structure -
customerDetails: {
details,
metadata
}
and client is elasticsearch client
id and index are correct values where id belongs to already existing id in elastic search
const { Client } = require("#elastic/elasticsearch");
and already saved document structure is below -
{
"_index" : "customerdetailsreport",
"_type" : "_doc",
"_id" : "cu-qo4MBxhUIUIg_lZUJ",
"_score" : 12.078913,
"_source" : {
"customerDetails" : {
"details" : {
"id" : "1234567",
"name" : "Atul Joshi",
},
"metadata" : {
"id" : "1234567"
}
}
}
}
The issue I am facing is , after update operation , existing item is not updated but a new entry is created
So , In elastic search I find two documents with different _id meaning two different entries are created
Can anybody please help what I am missing here ?

how to ignore null field when deserializing in spring data elasticsearch

I have saved two objects.
#Document(indexName = "test_index", createIndex = false)
public class ZTest {
#Id
private String id;
private String testOne;
#Transient
private String testTwo;
private String testThree;
}
{
"_index" : "test_index",
"_type" : "_doc",
"_id" : "w0mCgYEBXSVfca67-ayl",
"_score" : 1.0,
"_source" : {
"_class" : "com.ZTest",
"testOne" : "test_one"
}
},
{
"_index" : "test_index",
"_type" : "_doc",
"_id" : "xEmLgYEBXSVfca67r6wf",
"_score" : 1.0,
"_source" : {
"_class" : "com.ZTest",
"testOne" : "test_one",
"testThree" : "test_three"
}
}
Iterable<ZTest> t = zTestRepository.findAll();
Is it possible to exclude those null fields in the read result?
I have tried setting this spring.jackson.default-property-inclusion=non_null in property files. It doesn't work.
Spring Data Elasticsearch returns entities, and these Java POJO entities have 3 defined properties. And for properties that are transient or that are not returned by Elasticsearch the property value will be the value that is set in the object when on object of the entity class is created - here it's null.
What else do you expect to be returned?

How to set the value null instead of "null" in the example of a schema in Springdoc-openapi?

I am using Springdoc-openapi and have a dto like this:
#Schema(title = "Financial Conditions")
#JsonIgnoreProperties(ignoreUnknown = true)
public class CurrentFinancialConditions {
#Schema(title = "Rate", example = "null")
#JsonProperty("light")
private String conditionsLight;
#Schema(title = "Scores")
private Scores scores;
}
and it gives me the result in swagger-ui:
"current_financial_conditions": {
"scores": {
"liquidity": "some example goes here",
"profitability": "some example goes here",
},
"light": "null"
}
Is it possible to change the example value in the generated docs from "null" to null?

Mapping a variety of JSON responses with the same top - level structure in Swift

I have a variety of JSON responses that I'm getting back from a server via Alamofire; two are included below:
1)
{
"json" : {
"harkUpdate" : {
"more" : [
{
"unread-count" : {
"last" : 1613507864973,
"index" : {
"graph" : {
"graph" : "\/ship\/~dopzod\/urbit-help",
"index" : "\/"
}
}
}
},
{
"seen-index" : {
"index" : {
"graph" : {
"index" : "\/",
"graph" : "\/ship\/~dopzod\/urbit-help"
}
},
"time" : 1613507864973
}
}
]
}
},
"response" : "diff",
"id" : 3
}
{
"json" : {
"graph-update" : {
"keys" : [
{
"name" : "book-club-4717",
"ship" : "sicdev-pilnup"
},
{
"name" : "dm--locrev-finlys",
"ship" : "haddef-sigwen"
},
{
"name" : "smol-bibliotheca",
"ship" : "dasfeb"
},
{
"name" : "interface",
"ship" : "bitpyx-dildus"
},
{
"name" : "jobs-gigs",
"ship" : "nibset-napwyn"
},
{
"name" : "tlon-general",
"ship" : "bolbex-fogdys"
}
]
}
},
"id" : 1,
"response" : "diff"
}
As you can see, they both have an id, a response, and json fields - but the actual json response in them varies.
Normally what I would do is generate classes using something like quicktype.io, or use one of the many ObjectMapping frameworks out there. What I'm trying to get mentally un-blocked with is that all these json responses have a field called "json", which has a different structure. I'm trying to figure out who I can make unique classes for the responses given this structuring. Any help appreciated.
You can declare a basic object which's json property will be a generic type conforming to Codable.
struct JSON<T : Codable>: Codable {
let json: T
let id: Int
let response: String
}
After you define two structs which will represent the first and second json objects. For simplicity I will define only the objects that represent the second JSON:
struct GraphUpdate: Codable {
let graphUpdate: Keys
enum CodingKeys: String, CodingKey {
case graphUpdate = "graph-update"
}
}
struct Keys: Codable {
let keys: [Key]
}
struct Key: Codable {
let name, ship: String
}
And when trying to decode, you have to specify what object you want to decode to:
let item = try? JSONDecoder().decode(JSON<GraphUpdate>.self, from: data)

Get user friends list with firebase in one query

I have this structure for storing users and their friends, note that there is a node called friends in each user node and the key of the objects inside the friends node is the friend user id.
"users" : {
"{user_1}" : {
"email" : "geek#live.com",
"firstName" : "Geek",
"friends" : {
"{user_2}" : {
"status" : "pending",
"with" : {
"firstName" : "Anna",
"profileImage" : "http://..."
}
}
}
} ,
"{user_2}" : {
"email" : "anna#live.com",
"firstName" : "Anna",
"friends" : {
"{user_1}" : {
"status" : "pending",
"with" : {
"firstName" : "Geek",
"profileImage" : "https://..."
}
}
}
}
}
my goal is to get the user friends in one query, here is what I have tried:
FIRDatabase.database().reference().child("users")
.queryOrderedByChild("friends")
.queryEqualToValue(currentUserID)
.observeEventType(.Value) { firDataSnapshot in
print(firDataSnapshot)
}
so I have tried to get all users that has the current userID as a key in their friends node, but without luck.
Note that I can query only the node inside the user, but then I will not get the full users node of friends

Resources