add the api store
This commit is contained in:
parent
286b5b9306
commit
db8bb6ccc0
@ -27,11 +27,13 @@
|
|||||||
"svelte": "^3.54.0",
|
"svelte": "^3.54.0",
|
||||||
"svelte-check": "^2.9.2",
|
"svelte-check": "^2.9.2",
|
||||||
"vite": "^4.0.0",
|
"vite": "^4.0.0",
|
||||||
"vitest": "^0.25.3"
|
"vitest": "^0.25.8"
|
||||||
},
|
},
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"beercss": "^3.0.4",
|
"beercss": "^3.0.4",
|
||||||
|
"dexie": "^3.2.2",
|
||||||
|
"fake-indexeddb": "^4.0.1",
|
||||||
"material-dynamic-colors": "^0.1.5"
|
"material-dynamic-colors": "^0.1.5"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
32
src/lib/store/api.js
Normal file
32
src/lib/store/api.js
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
import Dexie, { liveQuery } from 'dexie';
|
||||||
|
import { writable } from 'svelte/store';
|
||||||
|
|
||||||
|
const seedCampaign = {
|
||||||
|
battles: [],
|
||||||
|
};
|
||||||
|
|
||||||
|
export function genApi(options = {}) {
|
||||||
|
const storage = new Dexie('Campaigns', options.dexie ?? {});
|
||||||
|
|
||||||
|
storage.version(1).stores({
|
||||||
|
campaigns: '++id,name',
|
||||||
|
});
|
||||||
|
|
||||||
|
const campaigns = liveQuery(async () => {
|
||||||
|
const c = [];
|
||||||
|
await storage.campaigns
|
||||||
|
.toCollection()
|
||||||
|
.each(({ id, name }) => c.push({ id, name }));
|
||||||
|
return c;
|
||||||
|
});
|
||||||
|
|
||||||
|
return {
|
||||||
|
campaigns,
|
||||||
|
storage,
|
||||||
|
event: {
|
||||||
|
addCampaign: (name) =>
|
||||||
|
storage.campaigns.add({ name, ...seedCampaign }),
|
||||||
|
deleteCampaign: (id) => storage.campaigns.delete(id),
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
28
src/lib/store/api.test.js
Normal file
28
src/lib/store/api.test.js
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
import { test, expect } from 'vitest';
|
||||||
|
//import { indexedDB, IDBKeyRange } from 'fake-indexeddb';
|
||||||
|
import 'fake-indexeddb/auto';
|
||||||
|
|
||||||
|
import { genApi } from './api.js';
|
||||||
|
|
||||||
|
const get = async (store) => new Promise((resolve) => store.subscribe(resolve));
|
||||||
|
|
||||||
|
test('create and add and remove campaigns', async () => {
|
||||||
|
const api = genApi(); //{ dexie: { indexedDB, IDBKeyRange } });
|
||||||
|
|
||||||
|
api.event.addCampaign('C1');
|
||||||
|
api.event.addCampaign('C2');
|
||||||
|
|
||||||
|
let result = await get(api.campaigns);
|
||||||
|
|
||||||
|
expect(result).toHaveLength(2);
|
||||||
|
expect(result[0]).toEqual({
|
||||||
|
id: 1,
|
||||||
|
name: 'C1',
|
||||||
|
});
|
||||||
|
|
||||||
|
api.event.deleteCampaign(1);
|
||||||
|
|
||||||
|
result = await get(api.campaigns);
|
||||||
|
|
||||||
|
expect(result).toHaveLength(1);
|
||||||
|
});
|
Loading…
Reference in New Issue
Block a user