feat: upgrade stores to new caching

This commit is contained in:
Arman
2022-09-13 18:06:06 +02:00
parent 722196c8dd
commit cb0fc876e2
3 changed files with 56 additions and 74 deletions
@@ -2,6 +2,7 @@ import { sdkForProject } from '$lib/stores/sdk';
import type { Models } from '@aw-labs/appwrite-console';
import { writable } from 'svelte/store';
import { browser } from '$app/env';
import { cachedStore } from '$lib/helpers/cache';
export type Attributes =
| Models.AttributeBoolean
@@ -55,20 +56,20 @@ function createCollection() {
};
}
function createDocumentListStore() {
const { subscribe, set } = writable<Models.DocumentList<Models.Document>>(
browser ? JSON.parse(sessionStorage.getItem('documentList')) : null
);
return {
subscribe,
set,
load: async (
export const documentList = cachedStore<
Models.DocumentList<Models.Document>,
{
load: (
databaseId: string,
collectionId: string,
queries: [],
limit: number,
offset: number
) => {
) => Promise<void>;
}
>('documentList', function ({ set }) {
return {
load: async (databaseId, collectionId, queries, limit, offset) => {
const response = await sdkForProject.databases.listDocuments(
databaseId,
collectionId,
@@ -79,44 +80,38 @@ function createDocumentListStore() {
set(response);
}
};
}
function createIndexListStore() {
const { subscribe, set } = writable<Models.IndexList>(
browser ? JSON.parse(sessionStorage.getItem('indexList')) : null
);
});
export const indexList = cachedStore<
Models.IndexList,
{
load: (databaseId: string, collectionId: string) => Promise<void>;
}
>('indexList', function ({ set }) {
return {
subscribe,
set,
load: async (databaseId: string, collectionId: string) => {
const response = await sdkForProject.databases.listIndexes(databaseId, collectionId);
set(response);
}
};
}
function createAttributeListStore() {
const { subscribe, set } = writable<Models.AttributeList>(
browser ? JSON.parse(sessionStorage.getItem('attributeList')) : null
);
});
export const attributeList = cachedStore<
Models.AttributeList,
{
load: (databaseId: string, collectionId: string) => Promise<void>;
}
>('attributeList', function ({ set }) {
return {
subscribe,
set,
load: async (databaseId: string, collectionId: string) => {
load: async (databaseId, collectionId) => {
const response = await sdkForProject.databases.listAttributes(databaseId, collectionId);
set(response);
}
};
}
});
export const collection = createCollection();
export const documentList = createDocumentListStore();
export const indexList = createIndexListStore();
export const attributeList = createAttributeListStore();
if (browser) {
collection.subscribe((n) => sessionStorage?.setItem('collection', JSON.stringify(n ?? '')));
documentList.subscribe((n) => sessionStorage?.setItem('documentList', JSON.stringify(n ?? '')));
indexList.subscribe((n) => sessionStorage?.setItem('indexList', JSON.stringify(n ?? '')));
attributeList.subscribe((n) =>
sessionStorage?.setItem('attributeList', JSON.stringify(n ?? ''))
);
}
@@ -1,16 +1,17 @@
import { sdkForProject } from '$lib/stores/sdk';
import type { Models } from '@aw-labs/appwrite-console';
import { writable } from 'svelte/store';
import { browser } from '$app/env';
import { cachedStore } from '$lib/helpers/cache';
function createCollectionStore() {
const { subscribe, set } = writable<Models.CollectionList>(
browser ? JSON.parse(sessionStorage.getItem('database')) : null
);
export const collections = cachedStore<
Models.CollectionList,
{
load: (databaseId: string, search: string, limit: number, offset: number) => Promise<void>;
total: (databaseId: string, id: string) => Promise<Record<string, unknown>>;
}
>('collections', function ({ set }) {
return {
subscribe,
set,
load: async (databaseId: string, search: string, limit: number, offset: number) => {
load: async (databaseId, search, limit, offset) => {
const response = await sdkForProject.databases.listCollections(
databaseId,
search,
@@ -19,7 +20,7 @@ function createCollectionStore() {
);
set(response);
},
total: async (databaseId: string, id: string) => {
total: async (databaseId, id) => {
let total = {};
total = browser ? JSON.parse(sessionStorage.getItem('collectionTotal')) ?? {} : {};
const response = await sdkForProject.databases.listDocuments(databaseId, id);
@@ -28,23 +29,17 @@ function createCollectionStore() {
return total;
}
};
}
function createDatabase() {
const { subscribe, set } = writable<Models.Database>();
});
export const database = cachedStore<
Models.Database,
{
load: (databaseId: string) => Promise<void>;
}
>('database', function ({ set }) {
return {
subscribe,
set,
load: async (databaseId: string) => {
load: async (databaseId) => {
set(await sdkForProject.databases.get(databaseId));
}
};
}
export const collections = createCollectionStore();
export const database = createDatabase();
if (browser) {
collections.subscribe((n) => sessionStorage?.setItem('collections', JSON.stringify(n ?? '')));
database.subscribe((n) => sessionStorage?.setItem('database', JSON.stringify(n ?? '')));
}
});
@@ -1,25 +1,17 @@
import { sdkForProject } from '$lib/stores/sdk';
import type { Models } from '@aw-labs/appwrite-console';
import { writable } from 'svelte/store';
import { browser } from '$app/env';
function createDatabaseListStore() {
const { subscribe, set } = writable<Models.DatabaseList>(
browser ? JSON.parse(sessionStorage.getItem('databaseList')) : null
);
import { cachedStore } from '$lib/helpers/cache';
export const databaseList = cachedStore<
Models.DatabaseList,
{
load: (search: string, limit: number, offset: number) => Promise<void>;
}
>('databaseList', function ({ set }) {
return {
subscribe,
set,
load: async (search: string, limit: number, offset: number) => {
load: async (search, limit, offset) => {
const response = await sdkForProject.databases.list(search, limit, offset);
set(response);
}
};
}
export const databaseList = createDatabaseListStore();
if (browser) {
databaseList.subscribe((n) => sessionStorage?.setItem('databaseList', JSON.stringify(n ?? '')));
}
});