feat: update missing stores to abstracted caching

This commit is contained in:
Arman committed 2022-09-13 23:27:25 +02:00
1 parent cb0fc876e2
commit fb6ad26c9a
2 files changed
+27 -30

No files matched your search

@@ -1,28 +1,29 @@
import { cachedStore } from '$lib/helpers/cache';
import { sdkForProject } from '$lib/stores/sdk';
import type { Models } from '@aw-labs/appwrite-console';
import { writable } from 'svelte/store';
function createDocumentStore() {
const { subscribe, set, update } = writable<Models.Document>();
export const doc = cachedStore<
Models.Document,
{
load: (databaseId: string, collectionId: string, documentId: string) => Promise<void>;
addAttribute: (attribute: string) => void;
removeAttribute: (attribute: string, index: number) => void;
}
>('doc', function ({ set, update }) {
return {
subscribe,
set,
load: async (databaseId: string, collectionId: string, documentId: string) =>
load: async (databaseId, collectionId, documentId) =>
set(await sdkForProject.databases.getDocument(databaseId, collectionId, documentId)),
addAttribute: (attribute: string) =>
addAttribute: (attribute) =>
update((n) => {
n[attribute].push(null);
return n;
}),
removeAttribute: (attribute: string, index: number) =>
removeAttribute: (attribute, index) =>
update((n) => {
n[attribute].splice(index, 1);
return n;
})
};
}
export const doc = createDocumentStore();
});
@@ -18,15 +18,17 @@ type Collection = Omit<Models.Collection, 'attributes'> & {
attributes: Array<Attributes>;
};
function createCollection() {
const { subscribe, set, update } = writable<Collection>(
browser ? JSON.parse(sessionStorage.getItem('collection')) : null
);
export const collection = cachedStore<
Collection,
{
load: (databaseId: string, collectionId: string) => Promise<void>;
addAttribute: (attribute: Attributes) => void;
updateAttribute: (attribute: Attributes) => void;
removeAttribute: (attribute: Attributes) => void;
}
>('collection', function ({ set, update }) {
return {
subscribe,
set,
load: async (databaseId: string, collectionId: string) => {
load: async (databaseId, collectionId) => {
set(
(await sdkForProject.databases.getCollection(
databaseId,
@@ -34,27 +36,27 @@ function createCollection() {
)) as unknown as Collection
);
},
addAttribute: (attribute: Attributes) =>
addAttribute: (attribute) =>
update((n) => {
n.attributes.push(attribute);
return n;
}),
updateAttribute: (attribute: Attributes) =>
updateAttribute: (attribute) =>
update((n) => {
const index = n.attributes.findIndex((a) => a.key === attribute.key);
n.attributes[index] = attribute;
return n;
}),
removeAttribute: (attribute: Attributes) =>
removeAttribute: (attribute) =>
update((n) => {
n.attributes = n.attributes.filter((a) => a.key !== attribute.key);
return n;
})
};
}
});
export const documentList = cachedStore<
Models.DocumentList<Models.Document>,
@@ -109,9 +111,3 @@ export const attributeList = cachedStore<
}
};
});
export const collection = createCollection();
if (browser) {
collection.subscribe((n) => sessionStorage?.setItem('collection', JSON.stringify(n ?? '')));
}