Using Firebase, how can I create a query based on UID? - javascript

I created a function that adds a UID to the database along with an item's state:
changestate(item) {
var postData = {
state: "listed",
};
var user = firebase.auth().currentUser;
var uid = user.uid;
var updates = {};
updates['foods' + '/' + item.$key + '/' + 'state' + '/' + uid] = postData;
return firebase.database().ref().update(updates);
}
I want to create a query that only shows the data corresponding to that UID. In a previous query I was using:
getLists(): FirebaseListObservable<any> {
return this.db.list('/foods', {
query: {
orderByChild: 'state',
equalTo: 'listed'
}
});}
This is the structure of my database:
{
"foods" : {
"foodID1" : {
"category" : "Produce",
"foodname" : "Apples",
"state" : {
"aePQkvozV6gehP7ihjN0OWCltKu2" : {
"state" : "listed"
}
}
},
"foodID2" : {
"category" : "Dairy",
"foodname" : "Cheese",
"state" : {
"aePQkvozV6gehP7ihjN0OWCltKu2" : {
"state" : "listed"
}
}
}
}
}
What do I need to do to show the items that correspond to a signed in user's UID?

The orderByChild property can take a path. So the code then simple becomes:
getLists(): FirebaseListObservable<any> {
return this.db.list('/foods', {
query: {
orderByChild: 'state/'+firebase.auth().currentUser.uid+'/state',
equalTo: 'listed'
}
});
}
This requires that the user is signed in of course. To test the query without a signed-in user, you can use a hard-coded value:
getLists(): FirebaseListObservable<any> {
return this.db.list('/foods', {
query: {
orderByChild: 'state/aePQkvozV6gehP7ihjN0OWCltKu2/state',
equalTo: 'listed'
}
});
}

Related

Increment counters of node and ALL parents using firebase realtime db cloud functions

I'm writing cloud functions for my firebase realtime database (not firestore) using js sdk. These functions increment counters that count the number of users per area. The areas I'm using are geographical and nested (Continent>Country>Region>City). The functions are triggered by the insertion of a user into the users list of a given area (.onCreate). If a user is inserted into the users list of let's say a country, it will automatically be added to the users list of the parent continent.
After tinkering for some time I was only able to come up with a "low-level" non-scalable solution (which by the way works). This means that I need to write additionnal code if I want to add a child area to city for example.
This is a problem because I expect to add child areas in the future.
Question: Is there a way to write a generic cloud function that increments the counters of a node and the counters of all its parent nodes?
I'm quite new to firebase and any help would be really appreciated
Json structure of firebase realtime db
{
"groups" : {
"location" : {
"continents" : {
"EU" : {
"countries" : {
"DE" : {
"created" : "2019-03-25--20:03:34",
"flag" : "/flags/de.svg",
"label" : "Germany",
"level" : 2,
"regions" : {
"BE" : {
"cities" : {
"Berlin" : {
"created" : "2019-03-25--20:03:35",
"label" : "Berlin",
"level" : 3,
"type" : "location",
"usercount" : 1,
"users" : {
"rvDUa5n0oufRFGA9JB7lioom0ac2" : {
"created" : "2019-03-25--20:03:55",
"label" : "Anonymous"
}
}
}
},
"created" : "2019-03-25--20:03:35",
"label" : "Land Berlin",
"level" : 3,
"type" : "location",
"usercount" : 1,
"users" : {
"rvDUa5n0oufRFGA9JB7lioom0ac2" : {
"created" : "2019-03-25--20:03:52",
"label" : "Anonymous"
}
}
}
},
"type" : "location",
"usercount" : 1,
"users" : {
"rvDUa5n0oufRFGA9JB7lioom0ac2" : {
"created" : "2019-03-25--20:03:49",
"label" : "Anonymous"
}
}
}
},
"created" : "2019-03-25--20:03:33",
"label" : "Europe",
"level" : 1,
"type" : "location",
"usercount" : 1,
"users" : {
"rvDUa5n0oufRFGA9JB7lioom0ac2" : {
"created" : "2019-03-25--20:03:46",
"label" : "Anonymous"
}
}
}
}
}
}
}
My cloud functions for user insertion - still need to write for user deletion
exports.onUserAddToContinent = functions.database
.ref('/groups/location/continents/{continentID}/users/{userID}')
.onCreate((snapshot, context) => {
const continentId = context.params.continentID
const counterRef = admin.database().ref('/groups/location/continents/' + continentId + '/usercount');
return counterRef.transaction(usercount => {
return (usercount || 0) + 1
})
})
exports.onUserAddToCountry = functions.database
.ref('/groups/location/continents/{continentID}/countries/{countryID}/users/{userID}')
.onCreate((snapshot, context) => {
const continentId = context.params.continentID
const countryId = context.params.countryID
const counterRef = admin.database().ref('/groups/location/continents/' + continentId + '/countries/' + countryId + '/usercount');
return counterRef.transaction(usercount => {
return (usercount || 0) + 1
})
})
exports.onUserAddToRegion = functions.database
.ref('/groups/location/continents/{continentID}/countries/{countryID}/regions/{regionID}/users/{userID}')
.onCreate((snapshot, context) => {
const continentId = context.params.continentID
const countryId = context.params.countryID
const regionId = context.params.regionID
const counterRef = admin.database().ref('/groups/location/continents/' + continentId + '/countries/' + countryId + '/regions/' + regionId +'/usercount');
return counterRef.transaction(usercount => {
return (usercount || 0) + 1
})
})
exports.onUserAddToCity = functions.database
.ref('/groups/location/continents/{continentID}/countries/{countryID}/regions/{regionID}/cities/{cityID}/users/{userID}')
.onCreate((snapshot, context) => {
const continentId = context.params.continentID
const countryId = context.params.countryID
const regionId = context.params.regionID
const cityId = context.params.cityID
const counterRef = admin.database().ref('/groups/location/continents/' + continentId + '/countries/' + countryId + '/regions/' + regionId +'/cities/'+ cityId + '/usercount');
return counterRef.transaction(usercount => {
return (usercount || 0) + 1
})
})
I feel 'im missing something. There has to be a generic way of doing this.

Firebase key reference

I am trying to add a simple transaction to a firebase database, however, I'm not having luck getting around the firebase generated keys for each account. here is my JSON:
{
"accounts" : {
"-KRPSyO4B48IpBnHBTYg" : {
"newRecord" : "100",
"email" : "",
"provider" : "",
"userId" : ""
}
},
"products" : {
"-KUKRafaNurpFhGF4jQa" : {
"name" : ""
}
},
}
I want to add a count to "newRecord" to the same level as userId, provider and email. However I am not successful with the following, or any variation so far:
firebase.database().ref('accounts/' + accounts.id).transaction(function(value) {
if (value) {
value.newRecord++;
}
return value;
});
Thanks in advance!
You should refer the value directly while using the transaction.
firebase.database().ref('accounts/' + accounts.id+'/newRecord').transaction(function(value) {
if (value) {
value++;
}
return value;
});

node Sequelize how to write static functions

I have experience in writing statics functions in moongose like
var mongoose =require('mongoose');
var Schema = mongoose.Schema;
var adminSchema = new Schema({
fullname : String,
number : Number,
email: String,
auth : {
username: String,
password : String,
salt: String
}
});
adminSchema.statics.usernameInUse = function (username, callback) {
this.findOne({ 'auth.username' : username }, function (err, doc) {
if (err) callback(err);
else if (doc) callback(null, true);
else callback(null, false);
});
};
here usernameInUse is the function I wana write but using sequelize for mysql database
my model
/*
This module is attendant_user table model.
It will store attendants accounts details.
*/
"use strict";
module.exports = function(sequelize, DataTypes) {
var AttendantUser = sequelize.define('AttendantUser', {
username : {
type : DataTypes.STRING,
allowNull : false,
validate : {
isAlpha : true
}
},{
freezeTableName : true,
paranoid : true
});
return AttendantUser;
};
How to add statics function here..??
Well, you can easily use Expansion of models
var User = sequelize.define('user', { firstname: Sequelize.STRING });
// Adding a class level method
User.classLevelMethod = function() {
return 'foo';
};
// Adding an instance level method
User.prototype.instanceLevelMethod = function() {
return 'bar';
};
OR in some cases you may use getter and setter on your models. See the docs:
A) Defining as part of a property:
var Employee = sequelize.define('employee', {
name: {
type : Sequelize.STRING,
allowNull: false,
get : function() {
var title = this.getDataValue('title');
// 'this' allows you to access attributes of the instance
return this.getDataValue('name') + ' (' + title + ')';
},
},
title: {
type : Sequelize.STRING,
allowNull: false,
set : function(val) {
this.setDataValue('title', val.toUpperCase());
}
}
});
Employee
.create({ name: 'John Doe', title: 'senior engineer' })
.then(function(employee) {
console.log(employee.get('name')); // John Doe (SENIOR ENGINEER)
console.log(employee.get('title')); // SENIOR ENGINEER
})
B) Defining as part of the model:
var Foo = sequelize.define('foo', {
firstname: Sequelize.STRING,
lastname: Sequelize.STRING
}, {
getterMethods : {
fullName : function() { return this.firstname + ' ' + this.lastname }
},
setterMethods : {
fullName : function(value) {
var names = value.split(' ');
this.setDataValue('firstname', names.slice(0, -1).join(' '));
this.setDataValue('lastname', names.slice(-1).join(' '));
},
}
});
Hope it helps.
AttendantUser.usernameInUse = function (username, callback) {
...
};
return AttendantUser;

GraphQL, Mysql equivalent OR operation

I have just learnt GraphQL and I want to find the user id=2 OR user id=3 now how will I make the GraphQL query,I am getting the whole set using the bellow query
{
users() {
id
username
posts {
title
tags {
name
}
}
}
}
2nd issue --
{
people(id:[1,2,3]) {
id
username
posts(id:2) {
title
tags {
name
}
}
}
}
if I add the arg on Posts filed then i got an error msg "Unknown argument id on field posts of type user"
This is my Schema js file
var graphql = require('graphql');
var Db = require('./db');
var users = new graphql.GraphQLObjectType({
name : 'user',
description : 'this is user info',
fields : function(){
return {
id :{
type : graphql.GraphQLInt,
resolve(user){
return user.id;
}
},
username :{
type : graphql.GraphQLString,
resolve(user){
return user.username;
}
},
posts:{
id:{
type : graphql.GraphQLString,
resolve(post){
return post.id;
}
},
type: new graphql.GraphQLList(posts),
resolve(user){
return user.getPosts();
}
}
}
}
});
var posts = new graphql.GraphQLObjectType({
name : 'Posts',
description : 'this is post info',
fields : function(){
return {
id :{
type : graphql.GraphQLInt,
resolve(post){
return post.id;
}
},
title :{
type : graphql.GraphQLString,
resolve(post){
return post.title;
}
},
content:{
type : graphql.GraphQLString,
resolve(post){
return post.content;
}
},
person :{
type: users,
resolve(post){
return post.getUser();
}
},
tags :{
type: new graphql.GraphQLList(tags),
resolve(post){
return post.getTags();
}
}
}
}
});
var tags = new graphql.GraphQLObjectType({
name : 'Tags',
description : 'this is Tags info',
fields : function(){
return {
id :{
type : graphql.GraphQLInt,
resolve(tag){
return tag.id;
}
},
name:{
type : graphql.GraphQLString,
resolve(tag){
return tag.name;
}
},
posts :{
type: new graphql.GraphQLList(posts),
resolve(tag){
return tag.getPosts();
}
}
}
}
});
var query = new graphql.GraphQLObjectType({
name : 'query',
description : 'Root query',
fields : function(){
return {
people :{
type : new graphql.GraphQLList(users),
args :{
id:{type: new graphql.GraphQLList(graphql.GraphQLInt)},
username:{
type: graphql.GraphQLString
}
},
resolve(root,args){
return Db.models.user.findAll({where:args});
}
},
posts:{
type : new graphql.GraphQLList(posts),
args :{
id:{
type: graphql.GraphQLInt
},
title:{
type: graphql.GraphQLString
},
},
resolve(root,args){
return Db.models.post.findAll({where:args});
}
},
tags :{
type : new graphql.GraphQLList(tags),
args :{
id:{
type: graphql.GraphQLInt
},
name:{
type: graphql.GraphQLString
},
},
resolve(root,args){
return Db.models.tag.findAll({where:args});
}
}
}
}
});
var Mutation = new graphql.GraphQLObjectType({
name : "mutation",
description : 'function for mutaion',
fields : function(){
return {
addPerson : {
type : users,
args :{
username : {
type : new graphql.GraphQLNonNull(graphql.GraphQLString)
},
email :{
type : new graphql.GraphQLNonNull(graphql.GraphQLString)
}
},
resolve(_, args){
return Db.models.user.create({
username : args.username,
email : args.email
});
}
}
}
}
})
var Schama = new graphql.GraphQLSchema({
query : query,
mutation : Mutation
})
module.exports = Schama;
In order to fetch multiple data from your schema using an array of ids you should define the args given to the users in your schema as follows:
fields: () => ({
users: {
type: new GraphQLList(USER_GRAPHQL_OBJECT_TYPE),
args: {
id: {type: new GraphQLList(GraphQLInt)}
},
resolve: (root, args) => {
// fetch users
}
}
})
notice the new GraphQLList wrapping the GraphQLInt type of the id.
Then, when querying your schema you can :
{
users(id: [2, 3]) {
id
username
posts {
title
tags {
name
}
}
}
}
Please let me know if it was helpful :)

Mongodb - Search by id and embedded array value

Im trying to write a function that will search for an object by ID and whether or not a value is contained in an embedded array within the object.
{
"_id" : ObjectId("569bea91c0e1fee4063527ac"),
"user" : ObjectId("568c65174fee132c36e199dd"),
"votes" : 9,
"image" : "./modules/deals/client/img/uploads/1546f914dba7e1732ea853cd70d79148.jpg",
"price" : "12.95",
"retailer" : "argos.co.uk",
"voters" : [{
"user" : ObjectId("568c65174fee132c36e199dd"),
},
{
"user" : ObjectId("568c65174fee132c36e199dd"),
},
{
"user" : ObjectId("568c65174fee132c36e199dd"),
}]
I would like to search by the _id and the voters.user.
I believe i need to finish this function correctly
exports.dealByIdAndVoter = function(req, res) {
Deal.count({
$where: function () {
}
},
function(err, dealByIdAndVoter) {
if (err) {
return res.status(400).send({
message: errorHandler.getErrorMessage(err)
});
} else {
console.log(dealByIdAndVoter);
var data = {};
data.count = dealByIdAndVoter;
res.json(data);
}
});
};
If you do not need to use the $where function, you can construct the query with the $or operator in this manner:
Deal
.find({
$or: [
// Match by _id
{ _id: req.id },
// Match by any user in the 'voters' array with a matching 'user' field.
{ 'voters.$.user': req.id }
]
})
.count(function(err, count) {
// Handle error here
res.json(count);
});

Categories

Resources