In Firebase, it's possible to create a random key using .childByAutoId()
let newEntry = FBRef.child("category").childByAutoId()
newEntry.setValue(["someValue": true])
This results in a json structure like this:
{
"category" : {
"random-key-generated-by-Firebase" : {
"someValue" : true
}
}
}
I was wondering is it possible to skip the last level and arrive at a structure like this instead?
{
"category" : {
"random-key-generated-by-Firebase" : true
}
}
For swift, first create a key reference
let key = ref.childByAutoId().key
Then create a childUpdate container
let childUpdates = ["/category/\(key)": true]
Finally update
ref.updateChildValues(childUpdates)
Related
Our database contains information about the languages used by businesses / service providers, and we have a section for "Bilingual" but we need to change that to be two separate items: "English" and "Spanish". The database is structured as a relational table:
table
business
int
id
varchar
business_name
table
business_language
int (FK)
business_id
int (FK)
language_id
table
languages
int
id
varchar
language_name
We are using Laravel for the backend of the web-app, so if we were to query the DB and get the languages it would look like:
$business = Business::all();
foreach($business as $b) {
//gives us a collection of languages
$b->language();
}
What I am trying to do is write a one-time SQL query that finds all instances of a business having "Bilingual" as a language, add "English" and "Spanish" as the language (if it doesn't already exist), and delete "Bilingual".
Is such a one-time-query possible, or do I have to write a script to do it for me for each business?
For this particular example there is easy solution :
$businesses = Business::with('languages')->get();
$english_id = Language::where('language_name','English')->first()->id;
$spanish_id = Language::where('language_name','Spanish')->first()->id;
$bilingual_id = Language::where('language_name','Bilingual')->first()->id;
foreach($businesses as $business) {
$is_bilingual = $business->languages->where('id', $bilingual_id)->first();
if($is_bilingual) {
$business->languages()->sync([$english_id,$spanish_id]);
}
}
Give this a try.
$business = Business::all();
$language_models = Languages::all();
$english_id = $language_models->find(function($language) {
return $language->name() == 'English';
})->pluck('id');
$spanish_id = $language_models->find(function($language) {
return $language->name() == 'Spanish';
})->pluck('id');
$bilingual_id = $language_models->find(function($language) {
return $language->name() == 'Bilingual';
})->pluck('id');
foreach($business as $b) use($english_id, $spanish_id, $bilingual_id) {
$languages = $b->language()->map(function($language) {
return $language->name();
});
if (in_array('Bilingual', $languages) {
if (!in_array('English', $languages)) {
$b->language()->attach($english_id);
}
if (!in_array('Spanish', $languages)) {
$b->language()->attach($spanish_id);
}
$b->language()->detach($bilingual_id);
}
}
Id write whats going on here but its pretty self explanatory.
You will need to have a name method on your language model or change the code to match.
I've been trying to create some queries for my database but without success. This is the first time when dealing with queries in Firebase and find them a bit confusing.
I use a real-time database
My database looks like this :
-> users
->profile
->uid1
->email : "some#email.com"
->name : "some_name"
->favourite
->id1
->title : "some_title"
->date : "some_date"
->id : "id1"
->id2
->title : "some_title"
->date : "some_date"
->id : "id2"
->uid2
->email : "some#email.com"
->name : "some_name"
->favourite
->id1
->title : "some_title"
->date : "some_date"
->id : "id1"
->id2
->title : "some_title"
->date : "some_date"
->id : "id2"
The queries I've been trying to make:
Check whether a specific uid is already in the database.
Check whether a specific id from favourite is already in the database at a known uid.
Any ideas how to perform these queries?
Depends if you are using the real-time database or Firestore. But because you want to do queries I suggest to use Firestore and you could implement it this way:
// Create a path where you want the query to search
let storageReference = Firestore.firestore().collection("users/profile")
let query = storageReference.whereField("uid", isEqualTo: givenUID )
query.getDocuments { (snapshot, error) in
if let snapshot = snapshot {
for document in snapshot.documents {
let data = document.data()
}
}
}
In real-time database you can't use queries but you can implement it like this and see if the document is available:
let databaseRef = Database.database().reference(withPath: "users/profile/givenUID")
databaseRef.observeSingleEvent(of: .value, with: { (snapshot) in
if snapshot.exists() {
// UID is in the database
} else {
// UID not found
}
})
Then do the same with a reference for favorites!
I have data like this in my Couchbase:
{
“mappings”: {
“/”: “Ana sayfa”
},
“platform”: “WEB”
}
I want to convert all data like this:
{
“/”: {“viewLabel”:“Ana Sayfa”}
“platform”: “WEB”
}
So I want to share old version:
{
“_class”: “com.commencis.appconnect.adminpanel.data.entity.ScreenNamesMappingEntity”,
“id”: “whitelabel::WEB::screenNamesMapping”,
“mappings”: {
“/”: “Ana sayfa”,
}
}
I want to create new document with above id:
( “id”: “whitelabel::WEB::screenNamesMapping”)
and delete the old one.
I want to create and convert like this:
{
“_class”: “com.commencis.appconnect.adminpanel.data.entity.ScreenNamesMappingEntity”,
“id”: “whitelabel::WEB::screenNamesMapping”,
“mappings”: {
“/”: { “viewLabel”: “Ana sayfa” } ,
}
I need to write script. I want to create new document with related id, then delete the old one, it could be multipe N1QL
I should not update the old data, new data should have new key, and I should edit the new key with the old one and delete the old one. I need to do with that way.
You can use the same document id by overwriting the current document, but there should be two repositories for both the entities.
oldRepository.findById("myid").ifPresent(e -> {
NewEntity ne = new NewEntity(e.id(), e.platform()...);
newRepository.save(ne); });
Suppose I have a contract like this:
org.springframework.cloud.contract.spec.Contract.make {
request {
method "GET"
url "/api/profiles"
headers {
header('Accept': 'application/json;charset=UTF-8')
header('Content-Type': 'application/json;charset=UTF-8')
}
}
response {
status 200
headers {
header('Content-Type': 'application/json;charset=UTF-8')
}
body(
value(
stub(
'''\
[
{
"profile": "profile1",
},
{
"profile": "profile2",
}
]
'''
),
test(
[
[
"profile" : regex(nonEmpty()),
]
]
)
)
)
}
The test of "profile" : regex(nonEmpty()) only checks that there is at least one array entry with a profile attribute that is non empty.
I would like to test that all entries have a non-empty profile.
I already tried this using test matchers:
jsonPath('$.[*].profile', byRegex(nonEmpty()))
While this checks every profile field to be non-empty, it doesn't check whether such a field actually exists.
How can I test that a profile field exists in every array entry and that each one is non-empty?
I guess the easiest way will be to use byCommand in the testMatchers section and pass the list there. Then manually assert whatever you want programmatically.
I need to dynamically create a json object full of cats and sub cats. My structure looks like this
var cats = {
tops: {
'top' : {
link : 'link',
subs : [
{
'sub' : {
link : 'a link'
}
}
]
}
}
};
Now I can add a top level category no problem with cats.tops[topVar] = { link : topLinkVar };
However I need to add subs categories to the top category.
I have tried a few variations such as cats.tops[topVar].subs.push( { subVar : { link : subLinkVar } } ); But this produces an undefined error.
The trick is that the sub categories need to be an array, so each top category can have many sub categories. What am I missing?
This is why coding on a saturday is bad idea. Also I am rather new to JSON.
Anyway.
The reason why I was getting the undefined error is because my new top category did not create the subs array.
The proper syntax to create the new top category looks like this:
cats.tops[topVar] = {link : topLinkVar, subs : [] };
Now
cats.tops[topVar].subs.push( { subVar : { link : subLinkVar } } );
Will work as expected