IndexedDBë ì¬ì©ìì ë¸ë¼ì°ì ì ë°ì´í°ë¥¼ ì구ì ì¼ë¡ ì ì¥í ì ìë ë°©ë² ì¤ íëì ëë¤. IndexedDB를 ì¬ì©íì¬ ë¤í¸ìí¬ ìíì ìê´ìì´ íë¶í 쿼리 기ë¥ì ì´ì©í ì ìë ì¹ ì´í리ì¼ì´ì ì ë§ë¤ ì ì기 ë문ì, ì¬ë¬ë¶ì ì¹ ì´í리ì¼ì´ì ì ì¨ë¼ì¸ê³¼ ì¤íë¼ì¸ íê²½ìì 모ë ëìí ì ììµëë¤.
ì´ ë¬¸ìì ëíì¬ì¬ë¬ë¶ì ì´ íí 리ì¼ìì IndexedDBì ë¹ë기 ë°©ì(asynchronous) APIì ëí´ íì´ë³¼ ì ììµëë¤. ë§ì½ IndexedDBê° ììíë¤ë©´, IndexedDB key characteristics and basic terminology 를 먼ì ì½ì´ë³´ë ê²ì´ ì¢ìµëë¤.
IndexedDB APIì ëí 참조(reference) 문ì를 ìíë¤ë©´, IndexedDB API í목과 íì íì´ì§ë¥¼ ë³´ììì¤. ì´ ë¬¸ìììë IndexedDBìì ì¬ì©ëë ê°ì²´ì ì¢ ë¥ì, ë기ì(synchrounous), ë¹ë기ì(asynchronous) APIì ëí´ì 기ì íê³ ììµëë¤.
기본 í¨í´IndexedDBê° ê¶ì¥íë 기본 í¨í´ì ë¤ìê³¼ ê°ìµëë¤:
ê·¸ë¬ë©´ ì´ì , ì´ë° í° ê°ë ì ìµíë©´ ë 구체ì ì¸ ê²ì í ì ììµëë¤.
ì ì¥ì를 ìì±íê³ êµ¬ì±í기 Indexed DB ì ì¤íì ì¸ ë²ì ì ì¬ì©í´ë³´ê¸°ì ëì´ë¥¼ ì¬ì©íë ë¸ë¼ì°ì ìì ì½ë를 í ì¤í¸íë ¤ë ê²½ì° ë¤ì ì½ë를 ì¬ì©í ì ììµëë¤.
// In the following line, you should include the prefixes of implementations you want to test.
window.indexedDB =
window.indexedDB ||
window.mozIndexedDB ||
window.webkitIndexedDB ||
window.msIndexedDB;
// DON'T use "var indexedDB = ..." if you're not in a function.
// Moreover, you may need references to some window.IDB* objects:
window.IDBTransaction =
window.IDBTransaction ||
window.webkitIDBTransaction ||
window.msIDBTransaction;
window.IDBKeyRange =
window.IDBKeyRange || window.webkitIDBKeyRange || window.msIDBKeyRange;
// (Mozilla has never prefixed these objects, so we don't need window.mozIDB*)
ì ëì´ë¥¼ ì¬ì©íì¬ êµ¬ííë ê²ì ë²ê·¸ê° ìê±°ë ë¶ìì íê±°ë ì´ì ë²ì ì ì¬ìì ë°ë¥´ë ê²½ì°ê° ììµëë¤. ë°ë¼ì íë¡ëì ìíì ì½ëìì ì¬ì©íì§ ìë ê²ì ê¶ì¥í©ëë¤. ì ëë¡ ì§ìíì§ ìë ë¸ë¼ì°ì 를 ì§ìíê² êµ¬ííì¬ ì¤í¨íë ê²ë³´ë¤ 미ì§ì íë ê²ì´ ë°ëì§í ì ììµëë¤.:
if (!window.indexedDB) {
window.alert(
"Your browser doesn't support a stable version of IndexedDB. Such and such feature will not be available.",
);
}
ë°ì´í°ë² ì´ì¤ ì´ê¸°
ì°ë¦¬ë ë°ì íë¡ê·¸ëë° ì½ëë¡ ììí ê²ì ëë¤:
// ë´ ë°ì´í° ë² ì´ì¤ë¥¼ ì´ëë¡ ìì²íì
var request = window.indexedDB.open("MyTestDatabase");
ë³´ì ¨ëì? ë°ì´í°ë² ì´ì¤ ì ìì ë¤ë¥¸ operation ë¤ê³¼ ë¹ì·í©ëë¤ â ë¹ì ì "ìì²(request)" íë©´ ë©ëë¤.
open ìì²ì ë°ì´í°ë² ì´ì¤ë¥¼ ì¦ì ì´ê±°ë ì¦ì í¸ëìì
ì ììíì§ ììµëë¤. open()
í¨ì를 í¸ì¶íë©´ ì´ë²¤í¸ë¡ ì²ë¦¬í ê²°ê³¼(ì±ê³µ ìí)ë ì¤ë¥ ê°ì´ ìë IDBOpenDBRequest
ê°ì²´ë¥¼ ë°íí©ëë¤. IndexedDBì ë¤ë¥¸ ë¹ë기 í¨ì ëë¶ë¶ì ê²°ê³¼ ëë ì¤ë¥ê° ìë IDBRequest
ê°ì²´ë¥¼ ë°íí©ëë¤. open()
í¨ìì ê²°ê³¼ë IDBDatabase
ì ì¸ì¤í´ì¤ì
ëë¤.
open ë©ìëì ëë²ì§¸ ë§¤ê° ë³ìë ë°ì´í°ë² ì´ì¤ì ë²ì ì
ëë¤. ë°ì´í°ë² ì´ì¤ì ë²ì ì ë°ì´í°ë² ì´ì¤ ì¤í¤ë§ë¥¼ ê²°ì í©ëë¤. ë°ì´í°ë² ì´ì¤ ì¤í¤ë§ë ë°ì´í°ë² ì´ì¤ ìì ê°ì²´ ì ì¥ìì ê·¸ê²ë¤ì 구조를 ê²°ì í©ëë¤. ë°ì´í°ë² ì´ì¤ê° ìì§ ì¡´ì¬íì§ ìì¼ë©´, open operationì ìí´ ìì±ëê³ , ê·¸ ë¤ì onupgradeneeded
ì´ë²¤í¸ê° í¸ë¦¬ê±°ëê³ ì´ ì´ë²¤í¸ ììì ë°ì´í°ë² ì´ì¤ ì¤í¤ë§ë¥¼ ìì±í©ëë¤. ë°ì´í°ë² ì´ì¤ê° ì¡´ì¬íì§ë§ ì
ê·¸ë ì´ë ë ë²ì ë²í¸ë¥¼ ì§ì íë ê²½ì° onupgradeneeded
ì´ë²¤í¸ê° í¸ë¦¬ê±°ëê³ í´ë¹ í¸ë¤ë¬ì ì
ë°ì´í¸ë ì¤í¤ë§ë¥¼ ì ê³µí ì ììµëë¤. ìì¸í ë´ì©ì ëì¤ì ìëì ë°ì´í°ë² ì´ì¤ì ë²ì ì
ë°ì´í¸ì IDBFactory.open
íì´ì§ë¥¼ 참조íììì¤.
ì ì´ ê°ì²´ ìì±ê²½ê³ : ì¤ì: ë²ì ë²í¸ë
unsigned long long
ì«ìì ëë¤. ì´ë ë²ì ë²í¸ë ë§¤ì° í° ì ìê° ëì´ì¼íë¤ë ì미ì ëë¤. ëí ë¶ë ììì ì ì¬ì©í ì ìë¤ë ê²ì ì미í©ëë¤. ê·¸ë ì§ ìì¼ë©´ ê°ì¥ ê°ê¹ì´ ì ìë¡ ë³íëì´ í¸ëìì ì´ ììëì§ ìê±°ëupgradeneeded
ì´ë²¤í¸ê° í¸ë¦¬ê±° ëì§ ììµëë¤. ì를 ë¤ì´, 2.4ì ê°ì ë²ì ë²í¸ë¥¼ ì¬ì©íì§ ë§ììì¤:var request = indexedDB.open("MyTestDatabase", 2.4); // don't do this, as the version will be rounded to 2
첫ë²ì§¸ë¡ ë¹ì ì´ íë ¤ë 모ë ìì²ì ëí´ ì±ê³µíì ë ê·¸ë¦¬ê³ ìë¬ê° ë°ìíì ë ì ì´ë¥¼ í ê°ì²´ë¥¼ ìì²í´ì¼ ë©ëë¤:
request.onerror = function (event) {
// request.errorCode ì ëí´ ë¬´ì¸ê°ë¥¼ íë¤!
};
request.onsuccess = function (event) {
// request.result ì ëí´ ë¬´ì¸ê°ë¥¼ íë¤!
};
onsuccess()
ëë onerror()
ë í¨ì ì¤ ì´ë¤ í¨ìê° í¸ì¶ë ê¹ì? 모ë ê²ì´ ì±ê³µíë©´, success ì´ë²¤í¸ (ì¦, type
ìì±ì´"success"
ë¡ ì¤ì ë DOM ì´ë²¤í¸)ê° request
를 target
ì¼ë¡ ë°ìí©ëë¤. ì¼ë¨ ì¤íëë©´, request
ì onsuccess()
í¨ìë success ì´ë²¤í¸ë¥¼ ì¸ìë¡ í¸ë¦¬ê±°ë©ëë¤. ë°ë©´, 문ì ê° ìë ê²½ì°, ì¤ë¥ ì´ë²¤í¸ (ì¦ type
ìì±ì´"error"
ë¡ ì¤ì ë DOM ì´ë²¤í¸)ë request
ìì ë°ìí©ëë¤. ì´ ì¤ë¥ ì´ë²¤í¸ë¥¼ ì¸ìë¡ onerror()
í¨ìê° í¸ë¦¬ê±°ë©ëë¤.
The IndexedDB APIë ì¤ë¥ ì²ë¦¬ì íìì±ì ìµìííëë¡ ì¤ê³ëì기 ë문ì ë§ì ì¤ë¥ ì´ë²¤í¸ë¥¼ ë³¼ ìë ìì ê²ì ëë¤. (ì ì´ë APIì ìµìíì§ ìì ê²½ì°). ê·¸ë¬ë ë°ì´í°ë² ì´ì¤ë¥¼ ì¬ë ê²½ì° ì¤ë¥ ì´ë²¤í¸ë¥¼ ë°ìíë ëª ê°ì§ ì¼ë°ì ì¸ ì¡°ê±´ì´ ììµëë¤. ê°ì¥ ë§ì 문ì ë ì¬ì©ìê° ì¹ ìì© íë¡ê·¸ë¨ì ë°ì´í°ë² ì´ì¤ë¥¼ ë§ë¤ ì ìë ê¶íì ì£¼ì§ ìê¸°ë¡ ê²°ì í ê²ì ëë¤. IndexedDBì 주ì ì¤ê³ 목í ì¤ íëë ë§ì ìì ë°ì´í°ë¥¼ ì¤íë¼ì¸ìì ì¬ì©í ì ìëë¡ íë ê²ì ëë¤. (ê° ë¸ë¼ì°ì ìì ì ì¥í ì ìë ì ì¥ ì©ëì ëí ìì¸í ë´ì©ì Storage limits 를 참조íììì¤.)
ì¼ë¶ ê´ê³ ë¤í¸ìí¬ë ì ìì ì¸ ì¹ ì¬ì´í¸ê° ì»´í¨í°ë¥¼ ì¤ì¼ìí¤ë ê²ì ë¸ë¼ì°ì ë ìíì§ ì기 ë문ì ë¸ë¼ì°ì ë í¹ì ì¹ ìì© íë¡ê·¸ë¨ì´ ì²ìì¼ë¡ ì ì¥ì© IndexedDB를 ì´ë ¤ê³ í ë ì¬ì©ììê² ë©ìì§ë¥¼ ë³´ë ëë¤. ì¬ì©ìê° ì¡ì¸ì¤ë¥¼ íì©íê±°ë ê±°ë¶í ì ììµëë¤. ëí, ê°ì¸ì ë³´ ë³´í¸ ëª¨ëì ë¸ë¼ì°ì ìì IndexedDB ê³µê°ì ìí¬ë¦¿ ì¸ì ì´ ë«í ëê¹ì§ ë©ëª¨ë¦¬ ë´ììë§ ì§ìë©ëë¤. (Firefoxì ê°ì¸ì ë³´ ë³´í¸ ë¸ë¼ì°ì§ 모ëì Chrome ì ìí¬ë¦¿ 모ëê° ìì§ë§, Firefox ì ê²½ì° 2015ë 11ì íì¬ ìì§ ë¯¸êµ¬í(Firefox bug 781982 참조)ì´ë¯ë¡, ê°ì¸ì ë³´ ë³´í¸ ë¸ë¼ì°ì§ 모ëì Firefoxììë IndexedDB를 ì¬ì©í ì ììµëë¤).
ì´ì , ì¬ì©ìê° ë°ì´í°ë² ì´ì¤ ìì± ìì²ì íì©íì¬ success ì½ë°±ì í¸ë¦¬ê±°íë success ì´ë²¤í¸ë¥¼ ë°ìë¤ê³ ê°ì í©ëë¤; ê·¸ ë¤ìì 무ìì í´ì¼í ê¹ì? ì´ ìì²ì indexedDB.open()
ì í¸ì¶íì¬ ìì±ëìê³ , request.result
ë IDBDatabase
ì ì¸ì¤í´ì¤ì´ë¯ë¡, ì´íì ì´ê²ì ì¬ì©í기 ìí´ ì ì¥í기 ìí ê²ì íì¤í©ëë¤. ì½ëë ë¤ìê³¼ ê°ì´ í ì ììµëë¤:
var db;
var request = indexedDB.open("MyTestDatabase");
request.onerror = function (event) {
alert("Why didn't you allow my web app to use IndexedDB?!");
};
request.onsuccess = function (event) {
db = request.result;
};
ìë¬ ì²ë¦¬
ììì ì¸ê¸í ë°ì ê°ì´, ì¤ë¥ ì´ë²¤í¸ë ë²ë¸ë§ë©ëë¤. ì¤ë¥ ì´ë²¤í¸ë ì¤ë¥ë¥¼ ìì±í request를 ëìì¼ë¡íë©°, ì´ë²¤í¸ë í¸ëìì ì¼ë¡ ë²ë¸ë§ëê³ , ë§ì§ë§ì¼ë¡ ë°ì´í°ë² ì´ì¤ ê°ì²´ë¡ ë²ë¸ë§ë©ëë¤. 모ë ìì²ì ìë¬ ì²ë¦¬ë¥¼ í¼íê³ ì¶ì ê²½ì°, ìëì ê°ì´ íëì ì¤ë¥ í¸ë¤ë¬ë¥¼ ë°ì´í°ë² ì´ì¤ ê°ì²´ì ì¶ê°íì¬ ëì í ì ììµëë¤:
db.onerror = function (event) {
// Generic error handler for all errors targeted at this database's
// requests!
alert("Database error: " + event.target.errorCode);
};
ë°ì´í°ë² ì´ì¤ë¥¼ ì´ ë ì주 ë°ìíë ì¤ë¥ ì¤ íëê° VER_ERR
ê° ììµëë¤. ì´ë ëì¤í¬ì ì ì¥ë ë°ì´í°ë² ì´ì¤ì ë²ì ì´ íì¬ ì½ëìì ì§ì ë ë²ì ë²í¸ë³´ë¤ í¼ì ëíë
ëë¤. ì´ ì¤ë¥ë íì ì¤ë¥ ì²ë¦¬ê¸°ìì ì²ë¦¬í´ì¼í©ëë¤.
ìë¡ì´ ë°ì´í°ë² ì´ì¤ë¥¼ ë§ë¤ê±°ë 기존 ë°ì´í°ë² ì´ì¤ì ë²ì ë²í¸ë¥¼ ëì¼ ë(ë°ì´í°ë² ì´ì¤ ì´ê¸°ì, ì´ì ë²ì ë³´ë¤ ëì ë²ì ë²í¸ë¥¼ ì§ì íë©´), onupgradeneeded
ê° í¸ë¦¬ê±°ëê³ request.result
(ì¦, ìëì ìì : db
)ì ì¤ì ë onversionchange
ì´ë²¤í¸ í¸ë¤ë¬ì IDBVersionChangeEvent ê°ì²´ê° ì ë¬ë©ëë¤. upgradeneeded
ì´ë²¤í¸ ì²ë¦¬ê¸°ìì ì´ ë²ì ì ë°ì´í°ë² ì´ì¤ì íìí ê°ì²´ ì ì¥ì를 ë§ë¤ì´ì¼í©ëë¤:
// This event is only implemented in recent browsers
request.onupgradeneeded = function (event) {
// Save the IDBDatabase interface
var db = event.target.result;
// Create an objectStore for this database
var objectStore = db.createObjectStore("name", { keyPath: "myKey" });
};
ì´ ê²½ì° ë°ì´í°ë² ì´ì¤ìë ì´ì ë²ì ì ë°ì´í°ë² ì´ì¤ì ìë ê°ì²´ ì ì¥ìê° ì´ë¯¸ ìì¼ë¯ë¡, ì´ì ë²ì ì ê°ì²´ ì ì¥ì를 ë¤ì ë§ë¤ íìê° ììµëë¤. ì¬ë¬ë¶ì ìë¡ì´ ê°ì²´ ì ì¥ì를 ë§ë¤ê±°ë ë ì´ì íìíì§ ìì ì´ì ë²ì ì ê°ì²´ ì ì¥ìë§ ìì íë©´ ë©ëë¤. 기존 ê°ì²´ ì ì¥ì를 ë³ê²½(ì, keyPath
를 ë³ê²½) í´ì íë ê²½ì°, ì´ì ê°ì²´ ì ì¥ì를 ìì íê³ ì ìµì
ì¼ë¡ ë¤ì ë§ë¤ì´ì¼í©ëë¤. (ì´ê²ì ê°ì²´ ì ì¥ìì ì 보를 ìì íë 주ìíììì¤! í´ë¹ ì 보를 ë³´ì¡´í´ì¼íë ê²½ì° ë°ì´í°ë² ì´ì¤ë¥¼ ì
ê·¸ë ì´ëí기 ì ì í´ë¹ ì 보를 ì½ê³ ë¤ë¥¸ ê³³ì ì ì¥í´ì¼ í©ëë¤.)
ì´ë¯¸ ì¡´ì¬íë ì´ë¦ì¼ë¡ ê°ì²´ ì ì¥ì를 ë§ë¤ë ¤ê³ íë©´ (ëë ì¡´ì¬íì§ ìë ê°ì²´ ì ì¥ì를 ìì íë ¤ê³ íë©´) ì¤ë¥ê° ë°ìí©ëë¤.
onupgradeneeded
ì´ë²¤í¸ê° ì±ê³µì ì¼ë¡ ëëë©´, ë°ì´í°ë² ì´ì¤ ì´ê¸° ìì²ì onsuccess
í¸ë¤ë¬ê° í¸ë¦¬ê±° ë©ëë¤.
ì´ì ë°ì´í°ë² ì´ì¤ë¥¼ 구ì¶í©ëë¤. IndexedDBë í ì´ë¸ì´ ìë ê°ì²´ ì ì¥ì를 ì¬ì©íë©° íëì ë°ì´í°ë² ì´ì¤ë ì¬ë¬ ê°ì ê°ì²´ ì ì¥ì를 í¬í¨í ì ììµëë¤. ê°ì ê°ì²´ ì ì¥ìì ì ì¥í ëë§ë¤ ê°ì í¤ì ì°ê´ë©ëë¤. ê°ì²´ ì ì¥ìê° í¤ ê²½ë¡ ëë í¤ ìì±ê¸° ìµì ì ì¬ì© ì¬ë¶ì ë°ë¼ í¤ë¥¼ ì ê³µí ì ìë ì¬ë¬ ê°ì§ ë°©ë²ì´ ììµëë¤.
ë¤ì íë í¤ê° ì ê³µëë ë¤ìí ë°©ë²ì ë³´ì¬ì¤ëë¤:
í¤ ê²½ë¡ (keyPath
) í¤ ìì±ê¸° (autoIncrement
) Description No No ì´ ê°ì²´ ì ì¥ìë ì«ì ë° ë¬¸ìì´ê³¼ ê°ì ìì ê°ì í¬í¨í 모ë ì¢
ë¥ì ê°ì ë³´ì í ì ììµëë¤. ì ê°ì ì¶ê° í ë ë§ë¤ ë³ëì í¤ ì¸ì를 ê³µê¸í´ì¼í©ëë¤. Yes No ì´ ê°ì²´ ì ì¥ìë JavaScript ê°ì²´ë§ í¬í¨ í ì ììµëë¤. ê°ì²´ìë í¤ ê²½ë¡ì ê°ì ì´ë¦ì ìì±ì´ ìì´ì¼í©ëë¤. No Yes ì´ ê°ì²´ ì ì¥ìë 모ë ì¢
ë¥ì ê°ì ë³´ì í ì ììµëë¤. í¤ê° ìëì¼ë¡ ìì±ë©ëë¤. ëí í¹ì í¤ë¥¼ ì¬ì©íë ¤ë ê²½ì° ë³ëì í¤ ì¸ì를 ê³µê¸í ì ììµëë¤. Yes Yes ì´ ê°ì²´ ì ì¥ìë JavaScript ê°ì²´ë§ í¬í¨ í ì ììµëë¤. ì¼ë°ì ì¼ë¡ í¤ê° ìëì¼ë¡ ìì±ëê³ ìì±ë í¤ì ê°ì í¤ ê²½ë¡ì ëì¼í ì´ë¦ì ê°ì§ ìì±ì ê°ì²´ì ì ì¥ë©ëë¤. ê·¸ë¬ë ê·¸ë¬í ìì±ì´ ì´ë¯¸ ì¡´ì¬íë ê²½ì°, ìë¡ì´ í¤ë¥¼ ìì±íë ê²ì´ ìë ìì±ì ê°ì í¤ë¡ ì¬ì©ë©ëë¤.
ê°ì²´ ì ì¥ìê° ê¸°ë³¸ì´ ìë ê°ì²´ë¥¼ ë³´ì íê³ ìì¼ë©´ ê°ì²´ ì ì¥ììì ì¸ë±ì¤ë¥¼ ë§ë¤ ì ììµëë¤. ì¸ë±ì¤ë¥¼ ì¬ì©íë©´ ê°ì²´ì í¤ê° ìë ì ì¥ë ê°ì²´ì ìì± ê°ì ì¬ì©íì¬ ê°ì²´ ì ì¥ìì ì ì¥ë ê°ì ê²ìí ì ììµëë¤.
ëí, ì¸ë±ì¤ìë ì ì¥ë ë°ì´í°ì ëí ê°ë¨í ì ì½ ì¡°ê±´ì ì ì© í ì ìë 기ë¥ì´ ììµëë¤. ì¸ë±ì¤ë¥¼ ìì±í ë ê³ ì (unique) íë그를 ì¤ì íë©´, ì¸ë±ì¤ë ì¸ë±ì¤ì í¤ ê²½ë¡ì ëí´ ëì¼í ê°ì ê°ë ë ê°ì ê°ì²´ê° ì ì¥ëì§ ìëë¡ ë³´ì¥í©ëë¤. ë°ë¼ì ì를 ë¤ìë©´, ì¬ë ì§ë¨ì ë³´ì íê³ ìë ê°ì²´ ì ì¥ìê° ìê³ , ë ì¬ëì´ ëì¼í email 주ì를 ê°ì§ 못 íë¤ë ê²ì ë³´ì¥íë ¤ë ê²½ì°, ì´ë¥¼ ê°ì í기 ìí´ ê³ ì (unique) íëê·¸ ì¤ì í ì¸ë±ì¤ë¥¼ ì¬ì©íë©´ ë©ëë¤.
ì´ ë¶ë¶ì í¼ëì¤ë¬ì¸ ìë ìì¼ë¯ë¡ ê°ë¨í ìì 를 ë¤ì´ ì¤ëª íê² ìµëë¤. 먼ì , ë¤ìì ììì ì¬ì©í ê³ ê° ë°ì´í°ë¥¼ ì ìíê² ìµëë¤:
// This is what our customer data looks like.
const customerData = [
{ ssn: "444-44-4444", name: "Bill", age: 35, email: "bill@company.com" },
{ ssn: "555-55-5555", name: "Donna", age: 32, email: "donna@home.org" },
];
ë¬¼ë¡ , 모ë ì¬ëì´ ì¬í ë³´ì¥ ë²í¸(ssn)를 ê°ì§ì§ ì기 ë문ì ssnì 기본 í¤(primary key)ë¡ ì§ì íì§ ììê²ëë¤. ê·¸ë¦¬ê³ ëì´ë¥¼ ì ì¥íë ëì ì£¼ë¡ ìë ìì¼ì ì ì¥íê² ì§ë§, ì´ë¬í ì ì í¸ì를 ìí´ ë¬´ìíê³ ì§ëê°ê² ìµëë¤.
ì´ì ìë£ë¥¼ ì ì¥í기 ìí´ indexedDB를 ìì±íë ê³¼ì ì ë³´ê² ìµëë¤:
const dbName = "the_name";
var request = indexedDB.open(dbName, 2);
request.onerror = function (event) {
// Handle errors.
};
request.onupgradeneeded = function (event) {
var db = event.target.result;
// Create an objectStore to hold information about our customers. We're
// going to use "ssn" as our key path because it's guaranteed to be
// unique - or at least that's what I was told during the kickoff meeting.
var objectStore = db.createObjectStore("customers", { keyPath: "ssn" });
// Create an index to search customers by name. We may have duplicates
// so we can't use a unique index.
objectStore.createIndex("name", "name", { unique: false });
// Create an index to search customers by email. We want to ensure that
// no two customers have the same email, so use a unique index.
objectStore.createIndex("email", "email", { unique: true });
// Use transaction oncomplete to make sure the objectStore creation is
// finished before adding data into it.
objectStore.transaction.oncomplete = function (event) {
// Store values in the newly created objectStore.
var customerObjectStore = db
.transaction("customers", "readwrite")
.objectStore("customers");
customerData.forEach(function (customer) {
customerObjectStore.add(customer);
});
};
};
ì´ì ì ì ìë¯ì´, onupgradeneeded
ë ë°ì´í°ë² ì´ì¤ì 구조를 ë³ê²½í ì ìë ê³³ì ìì¹í´ì¼í©ëë¤. ì´ ì´ë²¤í¸ ììì ê°ì²´ ì ì¥ì를 ë§ë¤ê±°ë ìì íê³ , ì¸ë±ì¤ë¥¼ ë§ë¤ê±°ë ì§ì¸ ì ììµëë¤.
ê°ì²´ ì ì¥ìë createObjectStore()
를 íë² í¸ì¶í¨ì¼ë¡ì¨ ìì±ë©ëë¤. ì´ ë©ìëë ì ì¥ìì ì´ë¦ê³¼ íë¼ë¯¸í° ê°ì²´ë¥¼ ì¸ìë¡ ë°ìµëë¤. íë¼ë¯¸í° ê°ì²´ë ì íì ì¼ë¡ ì¬ì©í ì ìì§ë§, ì´ë ì¤ìí ì¤ì ë¤ì ì ìíê³ ë§ë¤ê³ ìíë ê°ì²´ ì ì¥ìì íì
ì ì ìí기 ë문ì ë§¤ì° ì¤ìí©ëë¤. ì´ë² ìììì, ì°ë¦¬ë ê°ì²´ ì ì¥ìì ì´ë¦ì "customers"ë¡ ì§ê³ ê°ë³ ê°ì²´ë¤ì´ ì ì¼íê² ì ì¥ëëë¡ ë§ë¤ì´ì£¼ë í¹ì±ì¸ keyPath
를 ì ìí©ëë¤. ê·¸ë¦¬ê³ ì¬í ë³´ì¥ ë²í¸(ssn)ë ê³ ì í¨ì´ ë³´ì¥ë기 ë문ì, keyPath
ë¡ ìì ì ssn íë¡í¼í°ë¥¼ ì¬ì©íë©° ssnì objectStore
ì ì ì¥ëë 모ë ê°ì²´ì ë°ëì í¬í¨ëì´ì¼ í©ëë¤. ì°ë¦¬ë ëí ì ì¥ë ê°ì²´ì name
íë¡í¼í°ë¥¼ 찾기 ìí ì¸ë±ì¤ "name"ì ìì²í©ëë¤. ëí createObjectStore()
, createIndex()
ë ìì±íë ¤ë ì¸ë±ì¤ì ì¢
ë¥ë¥¼ ê²°ì íë ì íì ì¸ ê°ì²´ì¸ options
ì ì¸ìë¡ ë°ìµëë¤. name
íë¡í¼í°ê° ìë ê°ì²´ë¥¼ ì¶ê°í ìë ìì§ë§, ì´ ê²½ì° ê·¸ ê°ì²´ë "name" ì¸ë±ì¤ì ëíëì§ ììµëë¤.
ì´ì ì°ë¦¬ë ì ì¥ë customer ê°ì²´ë¥¼ ê°ì ¸ì¤ê¸° ìí´ ssn
ì ì´ì©íì¬ ê°ì²´ ì ì¥ìë¡ë¶í° ë°ë¡ ê°ì ¸ì¤ê±°ë, ì¸ë±ì¤ìì ê·¸ë¤ì ì´ë¦ì ì´ì©í´ ê°ì ¸ì¬ ì ììµëë¤. ì´ ê³¼ì ì´ ì´ë»ê² ì´ë£¨ì´ì§ëì§ ë°°ì°ê¸° ìí´, using an index ì¹ì
ì íì¸í ì ììµëë¤.
ê°ì²´ ì ì¥ì를 ìì±í ë autoIncrement
íë그를 ì¤ì í¨ì¼ë¡ì¨ í¤ ìì±ê¸°ë¥¼ íì±íí ì ììµëë¤. 기본ê°ì¼ë¡ ì´ íëê·¸ë ì¤ì ëì§ ììµëë¤.
í¤ ìì±ê¸°ê° íì±íëë©´, ê°ì²´ ì ì¥ìì ê°ì ì¶ê°í ë í¤ê° ìëì¼ë¡ ì¶ê°ë©ëë¤. ì²ì ìì±ëë©´ í¤ ìì±ê¸°ì ê°ì íì 1ë¡ ì¤ì ëê³ , ìë¡ ìì±ëë í¤ë 기본ì ì¼ë¡ ì´ì í¤ìì 1ì ëí ê°ì´ ë©ëë¤. í¤ ìì±ê¸°ì ê°ì í¸ëìì ì´ ì·¨ìëë ë± ë°ì´í°ë² ì´ì¤ ìì ì´ ë³µêµ¬ëëê² ìë í ì ë ììì§ì§ ììµëë¤. ê·¸ëì ë ì½ë를 ì§ì°ê±°ë ê°ì²´ ì ì¥ìì 모ë ë ì½ë를 ì§ì°ëë¼ë í´ë¹ ê°ì²´ ì ì¥ìì í¤ ìì±ê¸°ìë ìí¥ì ë¼ì¹ì§ ììµëë¤.
ì°ë¦¬ë ìëì ê°ì´ í¤ ìì±ê¸°ì í¨ê» ê°ì²´ ì ì¥ì를 ìì±í ì ììµëë¤:
// Open the indexedDB.
var request = indexedDB.open(dbName, 3);
request.onupgradeneeded = function (event) {
var db = event.target.result;
// Create another object store called "names" with the autoIncrement flag set as true.
var objStore = db.createObjectStore("names", { autoIncrement: true });
// Because the "names" object store has the key generator, the key for the name value is generated automatically.
// The added records would be like:
// key : 1 => value : "Bill"
// key : 2 => value : "Donna"
customerData.forEach(function (customer) {
objStore.add(customer.name);
});
};
í¤ ìì±ê¸°ì ê´ë ¨ë ë³´ë¤ ë§ì ì ë³´ë "W3C Key Generators" 를 ì°¸ê³ í´ ì£¼ììì¤.
ë°ì´í° ì¶ê°, ê²ì ë° ì ê±°ì ë°ì´í°ë² ì´ì¤ìì ìì
ì í기ì ì, í¸ëìì
ì ììí íìê° ììµëë¤. í¸ëìì
ì ë°ì´í°ë² ì´ì¤ ê°ì²´ ë¨ìë¡ ìëíë¯ë¡ í¸ëìì
ì ì¬ì©í ê°ì²´ ì ì¥ì를 ì§ì í´ì¤ì¼í©ëë¤. í¸ëìì
ì ë¤ì´ì¤ê³ ëë©´, ìë£ê° ìë ê°ì²´ ì ì¥ìì ì ê·¼í ì ìê³ ìì²ì ë§ë¤ ì ììµëë¤. ë¤ìì¼ë¡, ë°ì´í°ë² ì´ì¤ì ë³ê²½ì ì ë§ë¤ì§, í¹ì ì½ê¸°ë§ í ì§ ê²°ì í´ì¼í©ëë¤. í¸ëìì
ì ë¤ìì 3ê°ì§ 모ëê° ììµëë¤: readonly
, readwrite
, ê·¸ë¦¬ê³ versionchange
.
ê°ì²´ ì ì¥ìë ì¸ë±ì¤ë¥¼ ë§ë¤ê±°ë ìì íë ìì
ì í¬í¨íì¬ ë°ì´í°ë² ì´ì¤ì "schema"ë 구조를 ë³ê²½í기 ìí´ì í¸ëìì
ì ë°ëì versionchange
ì¬ì¼ í©ëë¤. ì´ í¸ëìì
ì IDBFactory.open
ë©ìë를 version
ê³¼ í¨ê» í¸ì¶í ê²½ì° ììë©ëë¤.
ì´ë¯¸ ì¡´ì¬íë ê°ì²´ ì ì¥ìì ë ì½ë를 ì½ê¸° ìí´ì í¸ëìì
ì readonly
í¹ì readwrite
모ëì´ë©´ ë©ëë¤. ì´ë¯¸ ì¡´ì¬íë ê°ì²´ ì ì¥ìì ë³ê²½ì ì 기ë¡í기 ìí´ìë í¸ëìì
ì´ ë°ëì readwrite
모ëì¬ì¼í©ëë¤. í¹ì í¸ëìì
ì IDBDatabase.transaction
ì¼ë¡ ì´ ì ììµëë¤. ì´ ë©ìëë ì ê·¼íê³ ì¶ì ê°ì²´ ì ì¥ìë¤ì ë°°ì´ë¡ ì ìë ë²ìì¸ storeNames
ì í¸ëìì
ì 모ëmode
(readonly
í¹ì readwrite
), 2ê°ì ì¸ì를 ë°ìµëë¤. ì´ ë©ìëë ê°ì²´ ì ì¥ìì ì ê·¼í ì ìë IDBIndex.objectStore
ë©ìë를 í¬í¨í í¸ëìì
ê°ì²´ë¥¼ ë°íí©ëë¤. 모ëê° ì§ì ëì§ ìëë¤ë©´ 기본ì ì¼ë¡ í¸ëìì
ì readonly
모ëë¡ ì´ë¦½ëë¤.
ì°¸ê³ : As of Firefox 40, IndexedDB transactions have relaxed durability guarantees to increase performance (see Firefox bug 1112702.) Previously in a readwrite
transaction IDBTransaction.oncomplete
was fired only when all data was guaranteed to have been flushed to disk. In Firefox 40+ the complete
event is fired after the OS has been told to write the data but potentially before that data has actually been flushed to disk. The complete
event may thus be delivered quicker than before, however, there exists a small chance that the entire transaction will be lost if the OS crashes or there is a loss of system power before the data is flushed to disk. Since such catastrophic events are rare most consumers should not need to concern themselves further. If you must ensure durability for some reason (e.g. you're storing critical data that cannot be recomputed later) you can force a transaction to flush to disk before delivering the complete
event by creating a transaction using the experimental (non-standard) readwriteflush
mode (see IDBDatabase.transaction
.
í¸ëìì ìì ì í©í ë²ìì 모ë를 ì¬ì©í¨ì¼ë¡ì¨ ìë£ ì ê·¼ì ë¹ ë¥´ê² í ì ììµëë¤. ì¬ê¸° ëê°ì íì´ ììµëë¤:
readwrite
모ëë íìí ëë§ ì¬ì©íììì¤. ê²¹ì¹ ë²ìì ëí´ readonly
í¸ëìì
ì ëìì ì¤íë ì ìì§ë§, readwrite
í¸ëìì
ì ê°ì²´ ì ì¥ìì ì¤ì§ íê°ë§ ìëí ì ììµëë¤. ë ìì¸í ë´ì©ì Basic Concepts ê¸ì ìë transactions ì ì를 ì°¸ê³ íììì¤.ë°ì´í°ë² ì´ì¤ë¥¼ ë§ë¤ìë¤ë©´, ë°ì´í°ë¥¼ ì¶ê°íê³ ì¶ìê²ëë¤. ë°ì´í° ì¶ê°ë ì´ë ê² í ì ììµëë¤:
var transaction = db.transaction(["customers"], "readwrite");
// Note: Older experimental implementations use the deprecated constant IDBTransaction.READ_WRITE instead of "readwrite".
// In case you want to support such an implementation, you can just write:
// var transaction = db.transaction(["customers"], IDBTransaction.READ_WRITE);
transaction()
í¨ìë 2ê°(1ê°ë ìµì
)ì ì¸ì를 ë°ê³ í¸ëìì
ê°ì²´ë¥¼ ë°íí©ëë¤. 첫ë²ì§¸ ì¸ìë í¸ëìì
ì´ íì¥ë ê°ì²´ ì ì¥ìì 목ë¡ì
ëë¤. 모ë ê°ì²´ ì ì¥ìì ëí´ í¸ëìì
ì íì¥íê³ ì¶ë¤ë©´ ë¹ ë°°ì´ì ëê²¨ì¤ ì ììµëë¤. ëë²ì§¸ ì¸ìë¡ ì무ê²ë ëê¸°ì§ ìëë¤ë©´, ì½ê¸° ì ì© í¸ëìì
ì´ ë°íë©ëë¤. ì°ê¸° ìì
ì íê³ ì¶ë¤ë©´, readwrite
íë그를 ë겨ì¤ì¼ í©ëë¤.
ê³ìí´ì í¸ëìì
ì ì¬ì©í기 ìí´ìë í¸ëìì
ì ìì 주기ì ëí´ ì´í´í íìê° ììµëë¤. í¸ëìì
ì ì´ë²¤í¸ 루í(Event loop)ì ë§¤ì° ë°ì íê² ì°ê´ëì´ ììµëë¤. ë§ì¼ ë¹ì ì´ í¸ëìì
ì ë§ë¤ì´ ëê³ ì¬ì©íì§ ìì ì± ì´ë²¤í¸ 루íë¡ ëìê°ê² ëë¤ë©´ í¸ëìì
ì ë¹íì±í ìíê° ë©ëë¤. í¸ëìì
ì íì±í ìíë¡ ì ì§íë ì ì¼í ë°©ë²ì í¸ëìì
ì ê·¸ê²ì ìì²íë ê² ë¿ì
ëë¤. ìì²ì´ ìë£ëë©´ DOM ì´ë²¤í¸ê° ë°ìíë©°, í¸ëìì
ì ëí ìì²ì´ ì±ê³µíë¤ë ê°ì íì í´ë¹ ìì²ì ëí ì½ë°±(Callback)ì´ ì¼ì´ë기 ì ê¹ì§ í¸ëìì
ì ìëª
ì ì°ì¥í ì ììµëë¤. ë§ì¼ ë¹ì ì´ í¸ëìì
ì ëí ì°ì¥ ìì² ìì´ ì´ë²¤í¸ 루íë¡ ëìê°ë ¤ íë¤ë©´ í¸ëìì
ì ê³§ ë¹íì±íê° ë í, ê³ìí´ì ë¹íì± ìí를 ì ì§í ê²ì
ëë¤. í¸ëìì
ì ëí ìì²ì´ ê³ìëë í í¸ëìì
ì íì±í ìí를 ì ì§í©ëë¤. í¸ëìì
ì ìì 주기ë ë§¤ì° ë¨ìíì§ë§ ë¹ì ì´ ì´ì ì ìíë ë°ìë ë¤ì ìê°ì´ 걸릴 ì ììµëë¤. ë¤ë¥¸ ìì ëª ê°ë¥¼ ë íì¸íë ê²ì ê¶ì¥í©ëë¤. ë§ì¼ ë¹ì ì íë¡ê·¸ë¨ìì TRANSACTION_INACTIVE_ERR
ë¼ë ìë¬ ì½ëê° ëíë기 ììíë¤ë©´, ëê° ì못ë기 ììí ê²ì
ëë¤.
í¸ëìì
ì ë¤ì ì¸ ê°ì DOM ì´ë²¤í¸ë¥¼ ë°ì ì ììµëë¤: error
, abort
, ê·¸ë¦¬ê³ complete
. ì°ë¦¬ë error
ì´ë²¤í¸ê° ì´ë»ê² ë²ë¸ë§ëëì§ì ëí´ ì´ë¯¸ ì´ì¼ê¸°íì¼ë©°, ì´ì ë°ë¼ì í¸ëìì
ì ìì²ì¼ë¡ë¶í° ë°ìíë 모ë ìë¬ ì´ë²¤í¸ë¥¼ ë°ìë¤ì
ëë¤. ì¬ê¸°ì ëì± ì 매í ì ì ìë¬ë¥¼ ì²ë¦¬íë ê°ì¥ 기본ì ì¸ ë°©ìì´ ìë¬ê° ë°ìí í¸ëìì
ì ì·¨ìíë ê²ì´ë¼ë ì ì
ëë¤. ë¹ì ì´ ì°ì stopPropagation()
ì ì´ì©í´ ìë¬ë¥¼ ì²ë¦¬íê³ ë íì ë¤ë¥¸ íëì íë ¤ê³ í´ë, ì ì²´ í¸ëìì
ì ì´ë¯¸ 롤백ë ìí©ì¼ ê²ì
ëë¤. ì´ë¬í ëìì¸ì ë¹ì ì´ ìë¬ ì²ë¦¬ì ëí´ì ìê°íëë¡ ê°ì íì§ë§, ë§ì¼ ì ì ì ë ìë¬ í¸ë¤ë§ì 모ë í¸ëìì
ì ë§ëë ê²ì´ ë무 ë²ê±°ë¡ë¤ë©´ ë¹ì ì ë°ì´í°ë² ì´ì¤ì íì ìºì¹ì¬ ìë¬ í¸ë¤ë¬(catchall error handler)를 ì¶ê°íëë¡ ì¤ì í ì ììµëë¤. ë¹ì ì´ ë§ì½ ìë¬ë¥¼ ì ì´íì§ ììê±°ë í¸ëìì
ìì abort()
를 í¸ì¶í ê²½ì°, í¸ëìì
ì 롤백ëê³ abort
ì´ë²¤í¸ë ì·¨ìë ê²ì
ëë¤. ë°ë©´, í¸ëìì
ì ëí 모ë ìì²ì´ ì ìì ì¼ë¡ ì²ë¦¬ëìì ê²½ì°ì complete
ì´ë²¤í¸ë¥¼ ë°íí©ëë¤. ë§ì¼ ë¹ì ì´ ë§¤ì° ë§ì ë°ì´í°ë² ì´ì¤ ìì
ì ìííê³ ìë¤ë©´, ë°ì´í°ë² ì´ì¤ì ëí ê°ë³ ìì²ì ì¶ì íëê²ë³´ë¨ í¸ëìì
ì ì¶ì íë ê²ì´ ì ì ê±´ê°ì ì´ë¡ì¸ ê²ì
ëë¤.
ì´ì ë¹ì ì´ í¸ëìì ì ë§ë¤ìê³ , ë¹ì ì ê·¸ í¸ëìì ì íµí´ ì¤ë¸ì í¸ ì¤í ì´ì ì ê·¼í´ì¼ íë ìí©ì´ë¼ê³ ê°ì í´ ë´ ìë¤. í¸ëìì ì ë¹ì ì´ í¸ëìì ì ë§ë¤ ë ì§ì í ì¤ë¸ì í¸ ì¤í ì´ë§ ì¬ì©í ì ìê² í´ì¤ëë¤. ê·¸ë¬ê³ ëë©´ ë¹ì ì ìíë 모ë ë°ì´í°ë¥¼ 그곳ì ì¶ê°í ì ììµëë¤.
// Do something when all the data is added to the database.
transaction.oncomplete = function (event) {
alert("All done!");
};
transaction.onerror = function (event) {
// Don't forget to handle errors!
};
var objectStore = transaction.objectStore("customers");
for (var i in customerData) {
var request = objectStore.add(customerData[i]);
request.onsuccess = function (event) {
// event.target.result == customerData[i].ssn
};
}
í¸ì¶ìì add()
ê¹ì§ì ê³¼ì ìì ìì±ë í¸ëìì
ì result
ë ì¶ê°ë ë°ì´í°ì í¤ì ê°ìµëë¤. ë°ë¼ì ì´ ê²½ì°, ì¤ë¸ì í¸ ì¤í ì´ê° ssn
ìì±ì í¤ í¨ì¤ë¡ ì¬ì©í기 ë문ì ì¶ê°ë ë°ì´í°ì ssn
ìì±ê³¼ ê°ì ê°ì ê°ì§ ê²ì
ëë¤. add()
í¨ì를 ì¬ì©í ë ê°ì í¤ë¥¼ ê°ì§ ë°ì´í°ê° ë°ì´í°ë² ì´ì¤ ìì ì¡´ì¬íì§ ììì¼ íë¤ë ì ì 주ìíììì¤. ë§ì¼ ë¹ì ì´ ì´ë¯¸ ì¡´ì¬íë ë°ì´í°ë¥¼ ìì íê³ ì¶ê±°ë, í¹ì ì´ë¯¸ ë°ì´í°ê° ìê±´ ë§ê±´ ìê´ ìë¤ë©´ 문ì ìë쪽ì Updating an entry in the database ë¶ë¶ìì ìê°íë put()
í¨ì를 ì¬ì©íììì¤.
ë°ì´í° ìì ë ì주 ìµìí ê·¸ ë°©ìëë¡ íìë©´ ë©ëë¤:
var request = db
.transaction(["customers"], "readwrite")
.objectStore("customers")
.delete("444-44-4444");
request.onsuccess = function (event) {
// It's gone!
};
ë°ì´í°ë² ì´ì¤ë¡ë¶í° ë°ì´í° ê°ì ¸ì¤ê¸°
ì´ì ë°ì´í°ë² ì´ì¤ ìì ì ë³´ë ë¤ì´ ìê² ë¤, ëª ê°ì§ ë°©ë²ì íµí´ ì 보를 ê°ì ¸ì ë´
ìë¤. ê°ì¥ 먼ì , ê°ì¥ ë¨ìí get()
ì ì¬ì©í´ ë´
ìë¤. ë°ì´í°ë¥¼ ê°ì ¸ì¤ê¸° ìí´ ë¹ì ì í¤ë¥¼ ì ê³µí´ì¼í©ëë¤. ì´ë ê²:
var transaction = db.transaction(["customers"]);
var objectStore = transaction.objectStore("customers");
var request = objectStore.get("444-44-4444");
request.onerror = function (event) {
// Handle errors!
};
request.onsuccess = function (event) {
// Do something with the request.result!
alert("Name for SSN 444-44-4444 is " + request.result.name);
};
"ë¨ìí" ê°ì ¸ì¤ë ê² ì¹ê³ ë ì½ëê° ì¢ ë§êµ°ì. ë¹ì ì´ ë°ì´í°ë² ì´ì¤ ìì¤ìì ìë¬ë¥¼ ì ì´íê³ ìë¤ë©´, ìëì ê°ì´ ê°ëµíí ìë ììµëë¤:
db
.transaction("customers")
.objectStore("customers")
.get("444-44-4444").onsuccess = function (event) {
alert("Name for SSN 444-44-4444 is " + event.target.result.name);
};
ì´ë»ê² ìëíëì§ ë³´ì´ìëì? ì¤ë¸ì í¸ ì¤í ì´ê° íë ë¿ì´ê¸° ë문ì, ë¹ì ì í¸ëìì
ìì íìí ì¤ë¸ì í¸ ì¤í ì´ë¤ì 리ì¤í¸ë¥¼ ë³´ë¼ íì ìì´ ê·¸ë¥ ì´ë¦ë§ Stringì¼ë¡ ë³´ë´ë©´ ë©ëë¤. ëí, ë¹ì ì ë°ì´í°ë² ì´ì¤ìì ì½ê¸°ë§ í기 ë문ì, "readwrite"
í¸ëìì
ì íìíì§ ììµëë¤. transaction()
ì í¹ì í 모ë ì¤ì ìì´ ê·¸ë¥ ë¶ë¥¼ ê²½ì°ì 기본ì ì¼ë¡ "readonly"
í¸ëìì
ì´ ë©ëë¤. ë íë ì ë¹í ì ì ë¹ì ì ìì²í ì¤ë¸ì í¸ë¥¼ ë°ë¡ í¹ì í ë³ìì ì ì¥íì§ ììë¤ë ì ì
ëë¤. DOM ì´ë²¤í¸ë ìì²ì ëìì¼ë¡ í ë ì´ë²¤í¸ë¥¼ ì¬ì©íì¬ result
ì ê°ì ë¶ë¬ì¬ ì ììµëë¤.
ë²ì ì íê³¼ 모ë ì¤ì ì ë°ë¼ ë°ì´í° ì ê·¼ ìë를 ë¹ ë¥´ê² í ì ìë¤ë ì ì 주목íììì¤. ì¬ê¸° ëªê°ì íì´ ììµëë¤:
readwrite
모ëë ë°ëì íìí ëë§ ì¬ì©íììì¤. readonly
모ëë ë²ìê° ì¤ë³µëëë¼ë ëìì ì¤í ê°ë¥íì§ë§, readwrite
모ëë í ì¤ë¸ì í¸ ì¤í ì´ì ëí´ ëìì íëë°ì ì¤íí ì ììµëë¤. ëì± ìì¸í ì ë³´ë, 기본 ê°ë
ìì í¸ëìì
ì ì ì í목(transactions in the Basic Concepts article)ì 참조íììì¤.ì´ì ì°ë¦¬ë ëª ê°ì ë°ì´í°ë¥¼ 꺼ëìµëë¤. ì´ ë°ì´í°ë¥¼ ì ë°ì´í¸íê³ ë¤ì IndexedDBì ì§ì´ë£ë ê²ì ë§¤ì° ê°ë¨í©ëë¤. ì´ì ìì 를 ì½ê° ì ë°ì´í¸í´ ë´ ìë¤:
var objectStore = db
.transaction(["customers"], "readwrite")
.objectStore("customers");
var request = objectStore.get("444-44-4444");
request.onerror = function (event) {
// Handle errors!
};
request.onsuccess = function (event) {
// Get the old value that we want to update
var data = event.target.result;
// update the value(s) in the object that you want to change
data.age = 42;
// Put this updated object back into the database.
var requestUpdate = objectStore.put(data);
requestUpdate.onerror = function (event) {
// Do something with the error
};
requestUpdate.onsuccess = function (event) {
// Success - the data is updated!
};
};
ì´ì ì°ë¦¬ë objectStore
를 ë§ë¤ê³ ì¬íë³´ì¥ë²í¸(SSN)ê° (444-44-4444
)ì¸ ê³ ê° ë ì½ë를 ìì²íìµëë¤. ê·¸ë¦¬ê³ ì°ë¦¬ë ê·¸ 결과를 ë³ì(data
)ì ë£ê³ , ì´ ê°ì²´ì age
ìì±ì ì
ë°ì´í¸íì¬, ë ë²ì§¸ ìì²(requestUpdate
)ì ë§ë¤ì´ ê³ ê° ë ì½ë를 ë¤ì objectStore
ì ì§ì´ë£ì´ ì´ì ê°ì ë®ì´ì¼ìµëë¤.
ì°¸ê³ : ì´ ë ì°ë¦¬ë readwrite
모ë를 ì¬ì©í´ì¼ í©ëë¤. ì°ë¦¬ê° ì§ê¸ í ê²ì ë¨ìí ë°ì´í°ë¥¼ ì½ì´ì¤ë ê² ìëë¼, ë¤ì ì°ë ê²ì´ê¸° ë문ì
ëë¤.
get()
ì ì¬ì©íë ¤ë©´ ê²ìíë ¤ë í¤ë¥¼ ìê³ ìì´ì¼ í©ëë¤. ê°ì²´ ì ì¥ì(object store)ì 모ë ê°ì íìíë ¤ë©´ 커ì를 ì¬ì©í ì ììµëë¤. ìëë 커ì를 ì¬ì©íë ë°©ë²ì
ëë¤:
var objectStore = db.transaction("customers").objectStore("customers");
objectStore.openCursor().onsuccess = function (event) {
var cursor = event.target.result;
if (cursor) {
alert("Name for SSN " + cursor.key + " is " + cursor.value.name);
cursor.continue();
} else {
alert("No more entries!");
}
};
openCursor()
í¨ìë ëª ê°ì§ ì¸ì를 ë°ìµëë¤. 첫 ë²ì§¸ë¡, ìëìì ê³§ ë¤ë£° í¤ ë²ì ê°ì²´(key range object)를 ì¬ì©íì¬ ê²ìí í목ì ë²ì를 ì íí ì ììµëë¤. ë ë²ì§¸ë¡, ë°ë³µí ë°©í¥ì ì§ì í ì ììµëë¤. ì ìì ììë ì¤ë¦ì°¨ìì¼ë¡ 모ë ê°ì²´ë¥¼ ë°ë³µí©ëë¤. 커ìë¤ì ëí ì±ê³µ ì½ë°±í¨ìë ì½ê° í¹ì´í©ëë¤. 커ì ìì²´ê° ì´ë²¤í¸ì ê²°ê³¼
ì
ëë¤ (ì ìììë ì¶ì½ ííì ì¬ì©íë¯ë¡ event.target.result
ì
ëë¤). ê·¸ë° ë¤ì ì¤ì í¤ì ê°ì 커ì ê°ì²´ì key
ì value
ìì±ìì ì°¾ì ì ììµëë¤. ê³ì ì§ííë ¤ë©´ 커ììì continue()를 í¸ì¶í´ì¼ í©ëë¤. ë°ì´í°ì ëì ëë¬íê±°ë openCursor()
ìì²ê³¼ ì¼ì¹íë íëª©ì´ ìë ê²½ì°ìë ì¬ì í ì±ê³µ ì½ë°±ì´ í¸ì¶ëì§ë§ result
ìì±ì undefined
ì
ëë¤.
커ì를 ì¬ì©íë ì¼ë°ì ì¸ í¨í´ ì¤ íëë ê°ì²´ ì ì¥ìì 모ë ê°ì²´ë¥¼ ê²ìíì¬ arrayì ì¶ê°íë ê²ì ëë¤. ë¤ìê³¼ ê°ì´ ìì±í ì ììµëë¤:
var customers = [];
objectStore.openCursor().onsuccess = function (event) {
var cursor = event.target.result;
if (cursor) {
customers.push(cursor.value);
cursor.continue();
} else {
alert("Got all customers: " + customers);
}
};
index ì¬ì©íê¸°ì°¸ê³ : Note: Mozillaë ì´ë° ê²½ì°ë¥¼ ì²ë¦¬í기 ìí´ getAll()ì 구ííìµëë¤(ê·¸ë¦¬ê³ í¨ê» 구íí
getAllKeys()
ë íì¬ about:configìdom.indexedDB.experimental
ì¤ì ë¤ì ì¨ê²¨ì ¸ ììµëë¤.). ì´ë¤ì IndexedDB íì¤ì ìë기 ë문ì ì¶í ì¬ë¼ì§ ì ììµëë¤. íì§ë§ ì°ë¦¬ë ì´ê²ë¤ì´ ì ì©íë¤ê³ ìê°í기 ë문ì í¬í¨ìì¼°ìµëë¤. ë¤ì ì½ëë ì ì½ëì ì íí ê°ì´ ëìí©ëë¤.:js objectStore.getAll().onsuccess = function(event) { alert("Got all customers: " + event.target.result); };
커ììvalue
ìì±ì ì´í´ë³´ë ê²ì ì±ë¥ìì ë¹ì©ì´ ë°ìí©ëë¤. ìëíë©´ í´ë¹ ê°ì²´ë ì§ì° ìì±ë기 ë문ì ëë¤. ì를 ë¤ì´getAll()
ì ì¬ì©í ë, Geckoë 모ë ê°ì²´ë¥¼ í ë²ì ìì±í©ëë¤. ë§ì½ í¤ ê°ê°ì ë³´ë ê²ìë§ ê´ì¬ì´ ìë¤ë©´, 커ì를 ì¬ì©íë ê²ì´getAll()
ì ì¬ì©íë ê² ë³´ë¤ í¨ì¬ í¨ì¨ì ì ëë¤. ë°ë©´ì ê°ì²´ ì ì¥ìì 모ë ê°ì²´ ë°°ì´ì ê°ì ¸ì¤ë ¤ë ê²½ì°ìëgetAll()
ì ì¬ì©íì¸ì.
ê³ ê° ë°ì´í°ë¥¼ ì ì¥í ë ì¬íë³´ì¥ë²í¸(SSN)를 í¤ë¡ ì¬ì©íë ê²ì ë ¼ë¦¬ì ì ëë¤. ìëíë©´ SSNì ê° ê°ì¸ì ê³ ì ìë³í기 ë문ì ëë¤. (ì´ê²ì´ ì¬ìí ë³´í¸ì ì¢ì ìê°ì¸ì§ë ì´ ìí°í´ì ë²ì를 ë²ì´ë ë¤ë¥¸ 문ì ì ëë¤.) ê·¸ë¬ë ì´ë¦ì¼ë¡ ê³ ê°ì ì°¾ìì¼ íë ê²½ì° ë°ì´í°ë² ì´ì¤ì 모ë SSNì ë°ë³µíì¬ ì¬ë°ë¥¸ SSNì ì°¾ìì¼ í©ëë¤. ì´ë¬í ë°©ìì¼ë¡ ê²ìíë©´ ë§¤ì° ë리기 ë문ì ì¸ë±ì¤ë¥¼ ëì ì¬ì©í ì ììµëë¤.
// 먼ì , request.onupgradeneeededë¡ index를 ìì±í´ì£¼ì¸ì:
// objectStore.createIndex("name", "name");
// ê·¸ë ì§ ìì ê²½ì° DOMExceptionì´ ë°ìí©ëë¤.
var index = objectStore.index("name");
index.get("Donna").onsuccess = function (event) {
alert("Donna's SSN is " + event.target.result.ssn);
};
"name"ì ê³ ì íì§ ì기 ë문ì name
ê°ì´ "Donna"
ë¡ ì¤ì ë íëª©ì´ íë ì´ì ìì ì ììµëë¤. ì´ ê²½ì° íì ê°ì¥ ë®ì í¤ ê°ì¸ ê²°ê³¼ íëë§ ì»ê² ë©ëë¤.
í¹ì name
ê°ì ê°ì§ 모ë í목ì ì¡ì¸ì¤í´ì¼ íë ê²½ì° cursor를 ì¬ì©í ì ììµëë¤. ì¸ë±ì¤ë¤ ë§ë¤ ë ê°ì§ ë¤ë¥¸ ì¢
ë¥ì cursor를 ì´ ì ììµëë¤. ì¼ë°ì ì¸ ì»¤ìë ì¸ë±ì¤ ìì±ì ê°ì²´ ì ì¥ìì ê°ì²´ì 매íí©ëë¤. ê·¸ë¦¬ê³ í¤ ì»¤ìë ê°ì²´ë¥¼ ê°ì²´ ì ì¥ìì ì ì¥í기 ìí´ ì¬ì©ë í¤ì ì¸ë±ì¤ë¥¼ 매íí©ëë¤. ì´ë¬í ì°¨ì´ì ì ë¤ìê³¼ ê°ìµëë¤.:
// ì¼ë°ì ì¸ ì»¤ì를 ì¬ì©í´ì ê³ ê° ë ì½ë ì 체를 ê°ì ¸ì¤ê¸°
index.openCursor().onsuccess = function (event) {
var cursor = event.target.result;
if (cursor) {
// cursor.keyë "Bill"ê³¼ ê°ì ì´ë¦ì´ë©°, cursor.valueë ê°ì²´ ì 체를 ì미í©ëë¤.
alert(
"Name: " +
cursor.key +
", SSN: " +
cursor.value.ssn +
", email: " +
cursor.value.email,
);
cursor.continue();
}
};
// í¤ ì»¤ì를 ì¬ì©í´ì ê³ ê° ë ì½ë ê°ì²´ í¤ë¥¼ ê°ì ¸ì¤ê¸°
index.openKeyCursor().onsuccess = function (event) {
var cursor = event.target.result;
if (cursor) {
// cursor.keyë "Bill"ê³¼ ê°ì ì´ë¦ì´ë©°, cursor.valueë ì¬íë³´ì¥ë²í¸(SSN)ì
ëë¤.
// ì ì¥ë ê°ì²´ì ëë¨¸ì§ ë¶ë¶ì ì§ì ì ì¼ë¡ ê°ì ¸ì¬ ë°©ë²ì ììµëë¤.
alert("Name: " + cursor.key + ", SSN: " + cursor.primaryKey);
cursor.continue();
}
};
커ìë¤ì ë²ìì ë°©í¥ì í¹ì í기
openCursor()
ëë openKeyCursor()
ì 첫 ë²ì§¸ ì¸ìë¡ ì¬ì©í ì ìë í¤ ë²ì ê°ì²´(key range object)를 ì¬ì©íì¬ ì»¤ììì ë³¼ ê°ì ë²ì를 ì íí ì ììµëë¤. ë¨ì¼ í¤ë§ íì©íëë¡ íë key range를 ë§ë¤ê±°ë íí ëë ìíê°ì´ ìë key range를 ë§ë¤ ì ììµëë¤. ëë íí ë° ìí ê°ì´ 모ëìë key range를 ë§ë¤ ì ììµëë¤. ë²ìë "closed"(ì¦, key rangeê° ì£¼ì´ì§ ê°ê¹ì§ í¬í¨)ê±°ë "open"(ì¦, key rangeê° ì£¼ì´ì§ ê°ì í¬í¨íì§ ìì)ì¼ ì ììµëë¤. ë¤ìì key rangeê° ìëíë ë°©ìì
ëë¤:
// "Donna"ë§ì ì¡°í
var singleKeyRange = IDBKeyRange.only("Donna");
// "Bill"ì í¬í¨í, "Bill" ì´í 모ë ê°ì ì¡°í
var lowerBoundKeyRange = IDBKeyRange.lowerBound("Bill");
// "Bill"ì ì ì¸í, "Bill" ë¤ì 모ë ê°ì ì¡°í
var lowerBoundOpenKeyRange = IDBKeyRange.lowerBound("Bill", true);
// "Donna"를 ì ì¸í, ì´ì 모ë ê°ì ì¡°í
var upperBoundOpenKeyRange = IDBKeyRange.upperBound("Donna", true);
// "Donna"를 ì ì¸í, "Bill"ê³¼ "Donna" ì¬ì´ 모ë ê°ì ì¡°í
var boundKeyRange = IDBKeyRange.bound("Bill", "Donna", false, true);
// ì í¤ ë²ì ì¤ íë를 ì¬ì©íë ¤ë©´, openCursor()/openKeyCursor()ì 첫 ë²ì§¸ ì¸ìë¡ ë겨주ì¸ì.
index.openCursor(boundKeyRange).onsuccess = function (event) {
var cursor = event.target.result;
if (cursor) {
// ì¡°íë ê°ì¼ë¡ 무ì¸ê° ìííë¤.
cursor.continue();
}
};
Sometimes you may want to iterate in descending order rather than in ascending order (the default direction for all cursors). Switching direction is accomplished by passing prev
to the openCursor()
function:
objectStore.openCursor(boundKeyRange, "prev").onsuccess = function (event) {
var cursor = event.target.result;
if (cursor) {
// Do something with the entries.
cursor.continue();
}
};
If you just want to specify a change of direction but not constrain the results shown, you can just pass in null as the first argument:
objectStore.openCursor(null, "prev").onsuccess = function (event) {
var cursor = event.target.result;
if (cursor) {
// Do something with the entries.
cursor.continue();
}
};
Since the "name" index isn't unique, there might be multiple entries where name
is the same. Note that such a situation cannot occur with object stores since the key must always be unique. If you wish to filter out duplicates during cursor iteration over indexes, you can pass nextunique
(or prevunique
if you're going backwards) as the direction parameter. When nextunique
or prevunique
is used, the entry with the lowest key is always the one returned.
index.openKeyCursor(null, "nextunique").onsuccess = function (event) {
var cursor = event.target.result;
if (cursor) {
// Do something with the entries.
cursor.continue();
}
};
Please see "IDBCursor Constants" for the valid direction arguments.
Version changes while a web app is open in another tabWhen your web app changes in such a way that a version change is required for your database, you need to consider what happens if the user has the old version of your app open in one tab and then loads the new version of your app in another. When you call open()
with a greater version than the actual version of the database, all other open databases must explicitly acknowledge the request before you can start making changes to the database. Here's how it works:
var openReq = mozIndexedDB.open("MyTestDatabase", 2);
openReq.onblocked = function (event) {
// If some other tab is loaded with the database, then it needs to be closed
// before we can proceed.
alert("Please close all other tabs with this site open!");
};
openReq.onupgradeneeded = function (event) {
// All other databases have been closed. Set everything up.
db.createObjectStore(/* ... */);
useDatabase(db);
};
openReq.onsuccess = function (event) {
var db = event.target.result;
useDatabase(db);
return;
};
function useDatabase(db) {
// Make sure to add a handler to be notified if another page requests a version
// change. We must close the database. This allows the other page to upgrade the database.
// If you don't do this then the upgrade won't happen until the user close the tab.
db.onversionchange = function (event) {
db.close();
alert("A new version of this page is ready. Please reload!");
};
// Do stuff with the database.
}
You should also listen for VersionError
errors to handle the situation where already opened apps may initiate code leading to a new attempt to open the database, but using an outdated version.
IndexedDB uses the same-origin principle, which means that it ties the store to the origin of the site that creates it (typically, this is the site domain or subdomain), so it cannot be accessed by any other origin.
Third party window content (e.g. <iframe>
content) can access the IndexedDB store for the origin it is embedded into, unless the browser is set to never accept third party cookies (see Firefox bug 1147821.)
When the browser shuts down (because the user chose the Quit or Exit option), the disk containing the database is removed unexpectedly, or permissions are lost to the database store, the following things happen:
AbortError
. The effect is the same as if IDBTransaction.abort()
is called on each transaction.IDBDatabase
object representing the database connection receives a close
event. You can use the IDBDatabase.onclose
event handler to listen for these events, so that you know when a database is unexpectedly closed.The behavior described above is new, and is only available as of the following browser releases: Firefox 50, Google Chrome 31 (approximately).
Prior to these browser versions, the transactions are aborted silently, and no close
event is fired, so there is no way to detect an unexpected database closure.
Since the user can exit the browser at any time, this means that you cannot rely upon any particular transaction to complete, and on older browsers, you don't even get told when they don't complete. There are several implications of this behavior.
First, you should take care to always leave your database in a consistent state at the end of every transaction. For example, suppose that you are using IndexedDB to store a list of items that you allow the user to edit. You save the list after the edit by clearing the object store and then writing out the new list. If you clear the object store in one transaction and write the new list in another transaction, there is a danger that the browser will close after the clear but before the write, leaving you with an empty database. To avoid this, you should combine the clear and the write into a single transaction.
Second, you should never tie database transactions to unload events. If the unload event is triggered by the browser closing, any transactions created in the unload event handler will never complete. An intuitive approach to maintaining some information across browser sessions is to read it from the database when the browser (or a particular page) is opened, update it as the user interacts with the browser, and then save it to the database when the browser (or page) closes. However, this will not work. The database transactions will be created in the unload event handler, but because they are asynchronous they will be aborted before they can execute.
In fact, there is no way to guarantee that IndexedDB transactions will complete, even with normal browser shutdown. See Firefox bug 870645. As a workaround for this normal shutdown notification, you might track your transactions and add a beforeunload
event to warn the user if any transactions have not yet completed at the time of unloading.
At least with the addition of the abort notifications and IDBDatabase.onclose
, you can know when this has happened.
Mozilla has implemented the ability to perform locale-aware sorting of IndexedDB data in Firefox 43+. By default, IndexedDB didn't handle internationalization of sorting strings at all, and everything was sorted as if it were English text. For example, b, á, z, a would be sorted as:
which is obviously not how users want their data to be sorted â Aaron and Ãaron for example should go next to one another in a contacts list. Achieving proper international sorting therefore required the entire dataset to be called into memory, and sorting to be performed by client-side JavaScript, which is not very efficient.
This new functionality enables developers to specify a locale when creating an index using IDBObjectStore.createIndex()
(check out its parameters.) In such cases, when a cursor is then used to iterate through the dataset, and you want to specify locale-aware sorting, you can use a specialized IDBLocaleAwareKeyRange
.
IDBIndex
has also had new properties added to it to specify if it has a locale specified, and what it is: locale
(returns the locale if any, or null if none is specified) and isAutoLocale
(returns true
if the index was created with an auto locale, meaning that the platform's default locale is used, false
otherwise.)
ì°¸ê³ : This feature is currently hidden behind a flag â to enable it and experiment, go to about:config and enable dom.indexedDB.experimental
.
<script
type="text/javascript"
src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<h1>IndexedDB Demo: storing blobs, e-publication example</h1>
<div class="note">
<p>Works and tested with:</p>
<div id="compat"></div>
</div>
<div id="msg"></div>
<form id="register-form">
<table>
<tbody>
<tr>
<td>
<label for="pub-title" class="required"> Title: </label>
</td>
<td>
<input type="text" id="pub-title" name="pub-title" />
</td>
</tr>
<tr>
<td>
<label for="pub-biblioid" class="required">
Bibliographic ID:<br />
<span class="note">(ISBN, ISSN, etc.)</span>
</label>
</td>
<td>
<input type="text" id="pub-biblioid" name="pub-biblioid" />
</td>
</tr>
<tr>
<td>
<label for="pub-year"> Year: </label>
</td>
<td>
<input type="number" id="pub-year" name="pub-year" />
</td>
</tr>
</tbody>
<tbody>
<tr>
<td>
<label for="pub-file"> File image: </label>
</td>
<td>
<input type="file" id="pub-file" />
</td>
</tr>
<tr>
<td>
<label for="pub-file-url">
Online-file image URL:<br />
<span class="note">(same origin URL)</span>
</label>
</td>
<td>
<input type="text" id="pub-file-url" name="pub-file-url" />
</td>
</tr>
</tbody>
</table>
<div class="button-pane">
<input type="button" id="add-button" value="Add Publication" />
<input type="reset" id="register-form-reset" />
</div>
</form>
<form id="delete-form">
<table>
<tbody>
<tr>
<td>
<label for="pub-biblioid-to-delete">
Bibliographic ID:<br />
<span class="note">(ISBN, ISSN, etc.)</span>
</label>
</td>
<td>
<input
type="text"
id="pub-biblioid-to-delete"
name="pub-biblioid-to-delete" />
</td>
</tr>
<tr>
<td>
<label for="key-to-delete">
Key:<br />
<span class="note">(for example 1, 2, 3, etc.)</span>
</label>
</td>
<td>
<input type="text" id="key-to-delete" name="key-to-delete" />
</td>
</tr>
</tbody>
</table>
<div class="button-pane">
<input type="button" id="delete-button" value="Delete Publication" />
<input
type="button"
id="clear-store-button"
value="Clear the whole store"
class="destructive" />
</div>
</form>
<form id="search-form">
<div class="button-pane">
<input
type="button"
id="search-list-button"
value="List database content" />
</div>
</form>
<div>
<div id="pub-msg"></div>
<div id="pub-viewer"></div>
<ul id="pub-list"></ul>
</div>
CSS Content
body {
font-size: 0.8em;
font-family: Sans-Serif;
}
form {
background-color: #cccccc;
border-radius: 0.3em;
display: inline-block;
margin-bottom: 0.5em;
padding: 1em;
}
table {
border-collapse: collapse;
}
input {
padding: 0.3em;
border-color: #cccccc;
border-radius: 0.3em;
}
.required:after {
content: "*";
color: red;
}
.button-pane {
margin-top: 1em;
}
#pub-viewer {
float: right;
width: 48%;
height: 20em;
border: solid #d092ff 0.1em;
}
#pub-viewer iframe {
width: 100%;
height: 100%;
}
#pub-list {
width: 46%;
background-color: #eeeeee;
border-radius: 0.3em;
}
#pub-list li {
padding-top: 0.5em;
padding-bottom: 0.5em;
padding-right: 0.5em;
}
#msg {
margin-bottom: 1em;
}
.action-success {
padding: 0.5em;
color: #00d21e;
background-color: #eeeeee;
border-radius: 0.2em;
}
.action-failure {
padding: 0.5em;
color: #ff1408;
background-color: #eeeeee;
border-radius: 0.2em;
}
.note {
font-size: smaller;
}
.destructive {
background-color: orange;
}
.destructive:hover {
background-color: #ff8000;
}
.destructive:active {
background-color: red;
}
JavaScript Content
(function () {
var COMPAT_ENVS = [
["Firefox", ">= 16.0"],
[
"Google Chrome",
">= 24.0 (you may need to get Google Chrome Canary), NO Blob storage support",
],
];
var compat = $("#compat");
compat.empty();
compat.append('<ul id="compat-list"></ul>');
COMPAT_ENVS.forEach(function (val, idx, array) {
$("#compat-list").append("<li>" + val[0] + ": " + val[1] + "</li>");
});
const DB_NAME = "mdn-demo-indexeddb-epublications";
const DB_VERSION = 1; // Use a long long for this value (don't use a float)
const DB_STORE_NAME = "publications";
var db;
// Used to keep track of which view is displayed to avoid uselessly reloading it
var current_view_pub_key;
function openDb() {
console.log("openDb ...");
var req = indexedDB.open(DB_NAME, DB_VERSION);
req.onsuccess = function (evt) {
// Better use "this" than "req" to get the result to avoid problems with
// garbage collection.
// db = req.result;
db = this.result;
console.log("openDb DONE");
};
req.onerror = function (evt) {
console.error("openDb:", evt.target.errorCode);
};
req.onupgradeneeded = function (evt) {
console.log("openDb.onupgradeneeded");
var store = evt.currentTarget.result.createObjectStore(DB_STORE_NAME, {
keyPath: "id",
autoIncrement: true,
});
store.createIndex("biblioid", "biblioid", { unique: true });
store.createIndex("title", "title", { unique: false });
store.createIndex("year", "year", { unique: false });
};
}
/**
* @param {string} store_name
* @param {string} mode either "readonly" or "readwrite"
*/
function getObjectStore(store_name, mode) {
var tx = db.transaction(store_name, mode);
return tx.objectStore(store_name);
}
function clearObjectStore(store_name) {
var store = getObjectStore(DB_STORE_NAME, "readwrite");
var req = store.clear();
req.onsuccess = function (evt) {
displayActionSuccess("Store cleared");
displayPubList(store);
};
req.onerror = function (evt) {
console.error("clearObjectStore:", evt.target.errorCode);
displayActionFailure(this.error);
};
}
function getBlob(key, store, success_callback) {
var req = store.get(key);
req.onsuccess = function (evt) {
var value = evt.target.result;
if (value) success_callback(value.blob);
};
}
/**
* @param {IDBObjectStore=} store
*/
function displayPubList(store) {
console.log("displayPubList");
if (typeof store == "undefined")
store = getObjectStore(DB_STORE_NAME, "readonly");
var pub_msg = $("#pub-msg");
pub_msg.empty();
var pub_list = $("#pub-list");
pub_list.empty();
// Resetting the iframe so that it doesn't display previous content
newViewerFrame();
var req;
req = store.count();
// Requests are executed in the order in which they were made against the
// transaction, and their results are returned in the same order.
// Thus the count text below will be displayed before the actual pub list
// (not that it is algorithmically important in this case).
req.onsuccess = function (evt) {
pub_msg.append(
"<p>There are <strong>" +
evt.target.result +
"</strong> record(s) in the object store.</p>",
);
};
req.onerror = function (evt) {
console.error("add error", this.error);
displayActionFailure(this.error);
};
var i = 0;
req = store.openCursor();
req.onsuccess = function (evt) {
var cursor = evt.target.result;
// If the cursor is pointing at something, ask for the data
if (cursor) {
console.log("displayPubList cursor:", cursor);
req = store.get(cursor.key);
req.onsuccess = function (evt) {
var value = evt.target.result;
var list_item = $(
"<li>" +
"[" +
cursor.key +
"] " +
"(biblioid: " +
value.biblioid +
") " +
value.title +
"</li>",
);
if (value.year != null) list_item.append(" - " + value.year);
if (
value.hasOwnProperty("blob") &&
typeof value.blob != "undefined"
) {
var link = $('<a href="' + cursor.key + '">File</a>');
link.on("click", function () {
return false;
});
link.on("mouseenter", function (evt) {
setInViewer(evt.target.getAttribute("href"));
});
list_item.append(" / ");
list_item.append(link);
} else {
list_item.append(" / No attached file");
}
pub_list.append(list_item);
};
// Move on to the next object in store
cursor.continue();
// This counter serves only to create distinct ids
i++;
} else {
console.log("No more entries");
}
};
}
function newViewerFrame() {
var viewer = $("#pub-viewer");
viewer.empty();
var iframe = $("<iframe />");
viewer.append(iframe);
return iframe;
}
function setInViewer(key) {
console.log("setInViewer:", arguments);
key = Number(key);
if (key == current_view_pub_key) return;
current_view_pub_key = key;
var store = getObjectStore(DB_STORE_NAME, "readonly");
getBlob(key, store, function (blob) {
console.log("setInViewer blob:", blob);
var iframe = newViewerFrame();
// It is not possible to set a direct link to the
// blob to provide a mean to directly download it.
if (blob.type == "text/html") {
var reader = new FileReader();
reader.onload = function (evt) {
var html = evt.target.result;
iframe.load(function () {
$(this).contents().find("html").html(html);
});
};
reader.readAsText(blob);
} else if (blob.type.indexOf("image/") == 0) {
iframe.load(function () {
var img_id = "image-" + key;
var img = $('<img id="' + img_id + '"/>');
$(this).contents().find("body").html(img);
var obj_url = window.URL.createObjectURL(blob);
$(this)
.contents()
.find("#" + img_id)
.attr("src", obj_url);
window.URL.revokeObjectURL(obj_url);
});
} else if (blob.type == "application/pdf") {
$("*").css("cursor", "wait");
var obj_url = window.URL.createObjectURL(blob);
iframe.load(function () {
$("*").css("cursor", "auto");
});
iframe.attr("src", obj_url);
window.URL.revokeObjectURL(obj_url);
} else {
iframe.load(function () {
$(this).contents().find("body").html("No view available");
});
}
});
}
/**
* @param {string} biblioid
* @param {string} title
* @param {number} year
* @param {string} url the URL of the image to download and store in the local
* IndexedDB database. The resource behind this URL is subjected to the
* "Same origin policy", thus for this method to work, the URL must come from
* the same origin as the web site/app this code is deployed on.
*/
function addPublicationFromUrl(biblioid, title, year, url) {
console.log("addPublicationFromUrl:", arguments);
var xhr = new XMLHttpRequest();
xhr.open("GET", url, true);
// Setting the wanted responseType to "blob"
// http://www.w3.org/TR/XMLHttpRequest2/#the-response-attribute
xhr.responseType = "blob";
xhr.onload = function (evt) {
if (xhr.status == 200) {
console.log("Blob retrieved");
var blob = xhr.response;
console.log("Blob:", blob);
addPublication(biblioid, title, year, blob);
} else {
console.error(
"addPublicationFromUrl error:",
xhr.responseText,
xhr.status,
);
}
};
xhr.send();
// We can't use jQuery here because as of jQuery 1.8.3 the new "blob"
// responseType is not handled.
// http://bugs.jquery.com/ticket/11461
// http://bugs.jquery.com/ticket/7248
// $.ajax({
// url: url,
// type: 'GET',
// xhrFields: { responseType: 'blob' },
// success: function(data, textStatus, jqXHR) {
// console.log("Blob retrieved");
// console.log("Blob:", data);
// // addPublication(biblioid, title, year, data);
// },
// error: function(jqXHR, textStatus, errorThrown) {
// console.error(errorThrown);
// displayActionFailure("Error during blob retrieval");
// }
// });
}
/**
* @param {string} biblioid
* @param {string} title
* @param {number} year
* @param {Blob=} blob
*/
function addPublication(biblioid, title, year, blob) {
console.log("addPublication arguments:", arguments);
var obj = { biblioid: biblioid, title: title, year: year };
if (typeof blob != "undefined") obj.blob = blob;
var store = getObjectStore(DB_STORE_NAME, "readwrite");
var req;
try {
req = store.add(obj);
} catch (e) {
if (e.name == "DataCloneError")
displayActionFailure(
"This engine doesn't know how to clone a Blob, " + "use Firefox",
);
throw e;
}
req.onsuccess = function (evt) {
console.log("Insertion in DB successful");
displayActionSuccess();
displayPubList(store);
};
req.onerror = function () {
console.error("addPublication error", this.error);
displayActionFailure(this.error);
};
}
/**
* @param {string} biblioid
*/
function deletePublicationFromBib(biblioid) {
console.log("deletePublication:", arguments);
var store = getObjectStore(DB_STORE_NAME, "readwrite");
var req = store.index("biblioid");
req.get(biblioid).onsuccess = function (evt) {
if (typeof evt.target.result == "undefined") {
displayActionFailure("No matching record found");
return;
}
deletePublication(evt.target.result.id, store);
};
req.onerror = function (evt) {
console.error("deletePublicationFromBib:", evt.target.errorCode);
};
}
/**
* @param {number} key
* @param {IDBObjectStore=} store
*/
function deletePublication(key, store) {
console.log("deletePublication:", arguments);
if (typeof store == "undefined")
store = getObjectStore(DB_STORE_NAME, "readwrite");
// As per spec http://www.w3.org/TR/IndexedDB/#object-store-deletion-operation
// the result of the Object Store Deletion Operation algorithm is
// undefined, so it's not possible to know if some records were actually
// deleted by looking at the request result.
var req = store.get(key);
req.onsuccess = function (evt) {
var record = evt.target.result;
console.log("record:", record);
if (typeof record == "undefined") {
displayActionFailure("No matching record found");
return;
}
// Warning: The exact same key used for creation needs to be passed for
// the deletion. If the key was a Number for creation, then it needs to
// be a Number for deletion.
req = store.delete(key);
req.onsuccess = function (evt) {
console.log("evt:", evt);
console.log("evt.target:", evt.target);
console.log("evt.target.result:", evt.target.result);
console.log("delete successful");
displayActionSuccess("Deletion successful");
displayPubList(store);
};
req.onerror = function (evt) {
console.error("deletePublication:", evt.target.errorCode);
};
};
req.onerror = function (evt) {
console.error("deletePublication:", evt.target.errorCode);
};
}
function displayActionSuccess(msg) {
msg = typeof msg != "undefined" ? "Success: " + msg : "Success";
$("#msg").html('<span class="action-success">' + msg + "</span>");
}
function displayActionFailure(msg) {
msg = typeof msg != "undefined" ? "Failure: " + msg : "Failure";
$("#msg").html('<span class="action-failure">' + msg + "</span>");
}
function resetActionStatus() {
console.log("resetActionStatus ...");
$("#msg").empty();
console.log("resetActionStatus DONE");
}
function addEventListeners() {
console.log("addEventListeners");
$("#register-form-reset").click(function (evt) {
resetActionStatus();
});
$("#add-button").click(function (evt) {
console.log("add ...");
var title = $("#pub-title").val();
var biblioid = $("#pub-biblioid").val();
if (!title || !biblioid) {
displayActionFailure("Required field(s) missing");
return;
}
var year = $("#pub-year").val();
if (year != "") {
// Better use Number.isInteger if the engine has EcmaScript 6
if (isNaN(year)) {
displayActionFailure("Invalid year");
return;
}
year = Number(year);
} else {
year = null;
}
var file_input = $("#pub-file");
var selected_file = file_input.get(0).files[0];
console.log("selected_file:", selected_file);
// Keeping a reference on how to reset the file input in the UI once we
// have its value, but instead of doing that we rather use a "reset" type
// input in the HTML form.
//file_input.val(null);
var file_url = $("#pub-file-url").val();
if (selected_file) {
addPublication(biblioid, title, year, selected_file);
} else if (file_url) {
addPublicationFromUrl(biblioid, title, year, file_url);
} else {
addPublication(biblioid, title, year);
}
});
$("#delete-button").click(function (evt) {
console.log("delete ...");
var biblioid = $("#pub-biblioid-to-delete").val();
var key = $("#key-to-delete").val();
if (biblioid != "") {
deletePublicationFromBib(biblioid);
} else if (key != "") {
// Better use Number.isInteger if the engine has EcmaScript 6
if (key == "" || isNaN(key)) {
displayActionFailure("Invalid key");
return;
}
key = Number(key);
deletePublication(key);
}
});
$("#clear-store-button").click(function (evt) {
clearObjectStore();
});
var search_button = $("#search-list-button");
search_button.click(function (evt) {
displayPubList();
});
}
openDb();
addEventListeners();
})(); // Immediately-Invoked Function Expression (IIFE)
Test the online live demo See also
Further reading for you to find out more information if desired.
Reference Tutorials and guides LibrariesRetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4