Добавление методов работы с indexedDB (#52)

This commit is contained in:
Nikolay
2021-01-06 09:52:05 +03:00
committed by GitHub
parent c669504137
commit 81dd51be8a
5 changed files with 135 additions and 6 deletions

30
src/core/api/commonApi.ts Normal file
View File

@ -0,0 +1,30 @@
import {INIT_BASE} from '_consts/initBase';
import {CommonApiName} from '_enums/common';
import {Folder, Tag, Task} from '_types/common';
import {makeStoreObject, openConnection} from '_utils/indexedDB';
import {makeApi} from '_utils/makeApi';
const STORE_NAME_LIST = [CommonApiName.TaskList, CommonApiName.FolderList, CommonApiName.TagList];
const connection = openConnection({
dbName: 'FYB',
version: 1,
onUpdate: db => {
switch (db.version) {
default: {
STORE_NAME_LIST.forEach(storeName => {
const objectStore = db.createObjectStore(storeName, {keyPath: 'id'});
INIT_BASE[storeName].forEach((item: unknown) => {
objectStore.add(item);
});
});
}
}
}
});
export const commonApi = {
taskList: makeApi(makeStoreObject<Task>(connection, CommonApiName.TaskList)),
folderList: makeApi(makeStoreObject<Folder>(connection, CommonApiName.FolderList)),
tagList: makeApi(makeStoreObject<Tag>(connection, CommonApiName.TagList)),
};