UNPKG

@trimblemaps/content-db

Version:

An isomorphic indexeddb wrapper for storing/retrieving TrimbleMaps content (ie places & place-sets)

97 lines 4.04 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var leveldb_1 = require("./leveldb"); var PlacesStore = /** @class */ (function () { function PlacesStore(keyPrefix) { var _this = this; if (keyPrefix === void 0) { keyPrefix = ''; } this.placesetKey = 'places'; this.getAll = function () { return _this.getSetKeys() .then(function (setKeys) { var gets = setKeys.map(function (set) { return leveldb_1.default.get(_this.placesetKey + "_" + set.Id); }); return Promise.all(gets); }); }; this.get = function (id) { return _this.getAll() .then(function (sets) { return sets.find(function (i) { return i.Id === id; }); }); }; this.getSetKeys = function () { return leveldb_1.default.get(_this.placesetKey); }; this.getPlaceSetIndex = function () { return leveldb_1.default.get(_this.placesetKey); }; this.find = function (q) { return _this.getAll() .then(function (sets) { return sets.find(function (i) { return i.Name === q; }); }); }; this.put = function (placeSets) { var setKeys = placeSets.map(function (placeSet) { return placeSet; }); var puts = placeSets.map(function (placeSet) { return { type: 'put', key: _this.placesetKey + "_" + placeSet.Id, value: placeSet }; }); return leveldb_1.default.put(_this.placesetKey, setKeys).then(function () { return leveldb_1.default.batch(puts); }); }; this.deletePlaceSet = function (id) { return leveldb_1.default.del(_this.placesetKey + "_" + id); }; this.putPlaceSets = function (placeSets) { var puts = placeSets.map(function (placeSet) { return { type: 'put', key: _this.placesetKey + "_" + placeSet.Id, value: placeSet }; }); return leveldb_1.default.batch(puts); }; this.putPlaceSetKeys = function (placeSets) { var setKeys = placeSets.map(function (placeSet) { var Id = placeSet.Id, Timestamp = placeSet.ModifiedOn; return { key: _this.placesetKey + "_" + Id, Id: Id, Timestamp: Timestamp, Index: 0, ServerTimestamp: 0, HumanTime: new Date(Timestamp / 10000) }; }); return leveldb_1.default.put(_this.placesetKey, setKeys); }; this.findModifiedSetId = function (sets, newSet) { var set = sets.find(function (set) { if (set.Id === newSet.Id && newSet.ModifiedOn > set.ModifiedOn) { return true; } return; }); if (set) { return set.Id; } else { return; } }; this.getBySetId = function (id) { return leveldb_1.default.get(_this.placesetKey + "_" + id); }; this.updatePlaceSetIndex = function (setId, placeSet) { return _this.getPlaceSetIndex() .then(function (setKeys) { var foundIndex = setKeys.findIndex(function (x) { return x.Id === setId; }); if (foundIndex !== -1) { setKeys[foundIndex] = placeSet; } else { // add new placeSet setKeys.push(placeSet); } return leveldb_1.default.put(_this.placesetKey, setKeys); }); }; this.putPlaceSet = function (placeSet) { return leveldb_1.default.put(_this.placesetKey + "_" + placeSet.Id, placeSet); }; this.placesetKey = encodeURI(keyPrefix + this.placesetKey); } return PlacesStore; }()); exports.PlacesStore = PlacesStore; //# sourceMappingURL=places-store.js.map