vector-im-hydrogen-web/src/storage/idb/create.js

22 lines
1.3 KiB
JavaScript
Raw Normal View History

2019-02-06 23:04:39 +01:00
import Storage from "./storage.js";
import { openDatabase } from "./utils.js";
2019-02-05 00:21:50 +01:00
export default async function createIdbStorage(databaseName) {
2019-02-07 00:19:14 +01:00
const db = await openDatabase(databaseName, createStores, 1);
2019-02-05 00:21:50 +01:00
return new Storage(db);
}
function createStores(db) {
2019-02-07 01:20:27 +01:00
db.createObjectStore("session", {keyPath: "key"});
2019-02-06 23:04:39 +01:00
// any way to make keys unique here?
db.createObjectStore("roomSummary", {keyPath: "room_id"});
const timeline = db.createObjectStore("roomTimeline", {keyPath: ["room_id", "sort_key"]});
2019-02-05 00:21:50 +01:00
timeline.createIndex("by_event_id", ["room_id", "event.event_id"], {unique: true});
// how to get the first/last x events for a room?
// we don't want to specify the sort key, but would need an index for the room_id?
// take sort_key as primary key then and have index on event_id?
// still, you also can't have a PK of [room_id, sort_key] and get the last or first events with just the room_id? the only thing that changes it that the PK will provide an inherent sorting that you inherit in an index that only has room_id as keyPath??? There must be a better way, need to write a prototype test for this.
// SOLUTION: with numeric keys, you can just us a min/max value to get first/last
// db.createObjectStore("members", ["room_id", "state_key"]);
2019-02-06 23:04:39 +01:00
const state = db.createObjectStore("roomState", {keyPath: ["event.room_id", "event.type", "event.state_key"]});
2019-02-05 00:21:50 +01:00
}