私のFirebaseデータは次のように整理されています。
+ myappname
+ customers
+ -JV2NQv3GmoM81zdUfTe
+ name: "Mary"
+ age: "24"
+ ...
+ -JV2N9NnItCfz5vB04RS
+ name: "John"
+ age: "32"
+ ...
+ ...
+ ...
名前で顧客を取得するにはどうすればよいですか?
名前は一意であることが保証されています。
これは私のカスタマーサービスです。現在は次のとおりです。
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;
});
findByName()の呼び出しごとにすべての顧客をスキャンする必要がありますか?
それとも、「セカンダリインデックス」のようなものを作成する必要がありますか?
アドバイスをお願いします、私は始めたばかりです... :-(
加藤の指摘とフランク・ヴァン・パッフェレンの提案のおかげで、私はついに自分の問題を解決しました。
Firebaseに「index」「customersByName」を追加しました(「ディスク容量は安いが、ユーザーの時間は「Firebase」のモットーではないことを思い出してください... :-))。
このソリューションはより一般的な用途であると思うので、参照された回答の指示には従いませんでした。複数の「インデックス」に対応します...
他の人に役立つことを願って、ここに投稿したいと思います。
コメントをお願いします:このソリューションには潜在的な欠点がありますか?全体として、一部のユースケースでは推奨されるソリューションですか?
app.factory('Customer', function ($firebase, FIREBASE_URL) {
var ref = new Firebase(FIREBASE_URL + 'customers');
var customers = $firebase(ref);
var refByName = new Firebase(FIREBASE_URL + 'customersByName');
var customersByName = $firebase(refByName);
var Customer = {
all: customers,
create: function (customer) {
return customers.$add(customer).then(function (ref) {
var customerId = ref.name();
customersByName.$child(customer.name).$set(customerId);
return customerId;
});
},
set: function(customerId, customer) {
var oldname = customers.$child(customerId).name;
if (customer.name !== oldname) {
customersByName.$remove(oldname);
}
customersByName.$child(customer.name).$set(customerId);
return customers.$child(customerId).$set(customer);
},
find: function (customerId) {
return customers.$child(customerId);
},
findByName: function (customerName) {
return customersByName.$child(customerName);
},
delete: function (customerId) {
var customer = Customer.find(customerId);
customer.deleted = true;
customer.$on('loaded', function () {
customersByName.$remove(customer.name);
customers.$child(customerId).$set(customer);
});
}
};
return Customer;
});
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加