Batch restore database

This commit is contained in:
Jonathan Staab 2023-02-13 19:44:49 -06:00
parent d1cc9e4299
commit 233d754258
4 changed files with 19 additions and 15 deletions

View File

@ -89,6 +89,7 @@ const removeItems = (storeName, ...args) => callLocalforage(storeName, 'removeIt
const length = storeName => callLocalforage(storeName, 'length') const length = storeName => callLocalforage(storeName, 'length')
const clear = storeName => callLocalforage(storeName, 'clear') const clear = storeName => callLocalforage(storeName, 'clear')
const keys = storeName => callLocalforage(storeName, 'keys') const keys = storeName => callLocalforage(storeName, 'keys')
const dump = storeName => callLocalforage(storeName, 'dump')
const iterate = (storeName, where = {}) => ({ const iterate = (storeName, where = {}) => ({
[Symbol.asyncIterator]() { [Symbol.asyncIterator]() {
@ -211,12 +212,7 @@ const defineTable = (name: string, pk: string, opts: TableOpts = {}): Table => {
// Sync from storage initially // Sync from storage initially
;(async () => { ;(async () => {
const t = Date.now() const t = Date.now()
const initialData = {} const initialData = filter(isValid, await dump(name))
for await (const {k, v} of iterate(name)) {
if (isValid(v)) {
initialData[k] = v
}
}
if (resetOnInit) { if (resetOnInit) {
await clear(name) await clear(name)
@ -310,6 +306,6 @@ const ready = derived(pluck('ready', Object.values(registry)), all(identity))
export default { export default {
getItem, setItem, setItems, removeItem, removeItems, length, clear, keys, getItem, setItem, setItems, removeItem, removeItems, length, clear, keys,
iterate, watch, getPersonWithFallback, clearAll, people, rooms, messages, dump, iterate, watch, getPersonWithFallback, clearAll, people, rooms, messages,
alerts, relays, routes, ready, alerts, relays, routes, ready,
} }

View File

@ -27,14 +27,22 @@ addEventListener('message', async ({data: {topic, payload, channel}}) => {
const instance = getStore(storeName) const instance = getStore(storeName)
const result = await switcherFn(method, { const result = await switcherFn(method, {
dump: () => new Promise(resolve => {
const result = {}
instance.iterate(
(v, k, i) => { result[k] = v },
() => resolve(result),
)
}),
setItems: () => { setItems: () => {
for (const [k, v] of Object.entries(args[0])) { for (const [k, v] of Object.entries(args[0])) {
instance.setItem(k, v) instance.setItem(k, v)
} }
}, },
removeItems: () => { removeItems: () => {
for (const [k, v] of Object.entries(args[0])) { for (const k of args[0]) {
instance.removeItem(k, v) instance.removeItem(k)
} }
}, },
default: () => instance[method](...args), default: () => instance[method](...args),

View File

@ -125,10 +125,10 @@ export const renderNote = (note, {showEntire = false}) => {
} }
export const asDisplayEvent = event => export const asDisplayEvent = event =>
({children: [], replies: [], reactions: [], ...event}) ({children: [], replies: [], reactions: [], ...event}) as DisplayEvent
export const mergeParents = (notes: Array<DisplayEvent>) => { export const mergeParents = (notes: Array<DisplayEvent>) => {
const notesById = createMap('id', notes) const notesById = createMap('id', notes) as Record<string, DisplayEvent>
const childIds = [] const childIds = []
for (const note of Object.values(notesById)) { for (const note of Object.values(notesById)) {
@ -140,7 +140,7 @@ export const mergeParents = (notes: Array<DisplayEvent>) => {
// Add the current note to its parents children, but only if we found a parent // Add the current note to its parents children, but only if we found a parent
if (notesById[parentId]) { if (notesById[parentId]) {
notesById[parentId].children = notesById[parentId].children.concat(note) notesById[parentId].children = notesById[parentId].children.concat([note])
} }
} }

View File

@ -18,7 +18,7 @@ export type MyEvent = Event & {
} }
export type DisplayEvent = MyEvent & { export type DisplayEvent = MyEvent & {
replies: [] replies: Array<MyEvent>
reactions: [] reactions: Array<MyEvent>
children: [] children: Array<DisplayEvent>
} }