2016-12-10 00:16:15 -07:00
|
|
|
|
define(['idb'], function () {
|
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
|
|
// Database name
|
|
|
|
|
var dbName = "items";
|
|
|
|
|
|
|
|
|
|
// Database version
|
|
|
|
|
var dbVersion = 1;
|
|
|
|
|
|
|
|
|
|
var dbPromise;
|
|
|
|
|
|
|
|
|
|
function setup() {
|
|
|
|
|
|
|
|
|
|
dbPromise = idb.open(dbName, dbVersion, function (upgradeDB) {
|
|
|
|
|
// Note: we don't use 'break' in this switch statement,
|
|
|
|
|
// the fall-through behaviour is what we want.
|
|
|
|
|
switch (upgradeDB.oldVersion) {
|
|
|
|
|
case 0:
|
|
|
|
|
upgradeDB.createObjectStore(dbName);
|
|
|
|
|
//case 1:
|
|
|
|
|
// upgradeDB.createObjectStore('stuff', { keyPath: '' });
|
|
|
|
|
}
|
|
|
|
|
}); //.then(db => console.log("DB opened!", db));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function getServerItemIds(serverId) {
|
|
|
|
|
return dbPromise.then(function (db) {
|
|
|
|
|
return db.transaction(dbName).objectStore(dbName).getAll(null, 10000).then(function (all) {
|
|
|
|
|
return all.filter(function (item) {
|
|
|
|
|
return item.ServerId === serverId;
|
|
|
|
|
}).map(function (item2) {
|
|
|
|
|
return item2.ItemId;
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-24 12:15:27 -07:00
|
|
|
|
function getServerItemTypes(serverId, userId) {
|
|
|
|
|
return dbPromise.then(function (db) {
|
|
|
|
|
return db.transaction(dbName).objectStore(dbName).getAll(null, 10000).then(function (all) {
|
|
|
|
|
return all.filter(function (item) {
|
|
|
|
|
return item.ServerId === serverId && (item.UserIdsWithAccess == null || item.UserIdsWithAccess.contains(userId));
|
|
|
|
|
}).map(function (item2) {
|
|
|
|
|
return (item2.Item.Type || '').toLowerCase();
|
|
|
|
|
}).filter(filterDistinct);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-10 00:16:15 -07:00
|
|
|
|
function getServerIds(serverId) {
|
|
|
|
|
return dbPromise.then(function (db) {
|
|
|
|
|
return db.transaction(dbName).objectStore(dbName).getAll(null, 10000).then(function (all) {
|
|
|
|
|
return all.filter(function (item) {
|
|
|
|
|
return item.ServerId === serverId;
|
|
|
|
|
}).map(function (item2) {
|
|
|
|
|
return item2.Id;
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function getAll() {
|
|
|
|
|
return dbPromise.then(function (db) {
|
|
|
|
|
return db.transaction(dbName).objectStore(dbName).getAll(null, 10000);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function get(key) {
|
|
|
|
|
return dbPromise.then(function (db) {
|
|
|
|
|
return db.transaction(dbName).objectStore(dbName).get(key);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function set(key, val) {
|
|
|
|
|
return dbPromise.then(function (db) {
|
|
|
|
|
var tx = db.transaction(dbName, 'readwrite');
|
|
|
|
|
tx.objectStore(dbName).put(val, key);
|
|
|
|
|
return tx.complete;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function remove(key) {
|
|
|
|
|
return dbPromise.then(function (db) {
|
|
|
|
|
var tx = db.transaction(dbName, 'readwrite');
|
|
|
|
|
tx.objectStore(dbName).delete(key);
|
|
|
|
|
return tx.complete;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function clear() {
|
|
|
|
|
return dbPromise.then(function (db) {
|
|
|
|
|
var tx = db.transaction(dbName, 'readwrite');
|
2017-01-20 10:52:37 -07:00
|
|
|
|
tx.objectStore(dbName).clear();
|
2016-12-10 00:16:15 -07:00
|
|
|
|
return tx.complete;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-24 12:15:27 -07:00
|
|
|
|
function filterDistinct(value, index, self) {
|
|
|
|
|
return self.indexOf(value) === index;
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-10 00:16:15 -07:00
|
|
|
|
setup();
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
get: get,
|
|
|
|
|
set: set,
|
|
|
|
|
remove: remove,
|
|
|
|
|
clear: clear,
|
|
|
|
|
getAll: getAll,
|
|
|
|
|
getServerItemIds: getServerItemIds,
|
2016-12-24 12:15:27 -07:00
|
|
|
|
getServerIds: getServerIds,
|
|
|
|
|
getServerItemTypes: getServerItemTypes
|
2016-12-10 00:16:15 -07:00
|
|
|
|
};
|
|
|
|
|
});
|