My Firebase data is organized this way:
+ myappname
+ customers
+ -JV2NQv3GmoM81zdUfTe
+ name: "Mary"
+ age: "24"
+ ...
+ -JV2N9NnItCfz5vB04RS
+ name: "John"
+ age: "32"
+ ...
+ ...
+ ...
How do I retrieve a customer by it's name?
The name is guaranteed to be unique.
This is my Customer service, currently:
app.factory('Customer', function ($firebase, FIREBASE_URL) {
var ref = new Firebase(FIREBASE_URL + 'customers');
var customers = $firebase(ref);
var Customer = {
all: customers,
create: function (customer) {
return customers.$add(customer).then(function (ref) {
var customerId = ref.name();
return customerId;
});
},
set: function(customerId, customer) {
return customers.$child(customerId).$set(customer);
},
find: function (customerId) {
return customers.$child(customerId);
},
findByName: function (customerName) { // TODO...
},
delete: function (customerId) {
var customer = Customer.find(customerId);
customer.deleted = true;
customer.$on('loaded', function () {
customers.$child(customerId).$set(customer);
});
}
};
return Customer;
});
Should I scan all the customers on each findByName() call?
Or should I build something like a "secondary index"?
Please, some advice, I'm just starting... :-(