Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "fp-ts-indexeddb",
"version": "0.2.2",
"version": "0.2.3",
"description": "Simple FP-TS based wrapper around indexedDB",
"main": "dist/lib/index.js",
"module": "dist/es2015/index.js",
Expand Down
7 changes: 6 additions & 1 deletion src/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,10 @@ export const open = <StoreC extends t.Mixed>(
const req = window.indexedDB.open(dbName, schema.version);
req.onupgradeneeded = () => pipe(
schema.stores,
R.mapWithIndex((storeName: string, v: Store<StoreC>) => req.result.createObjectStore(storeName, { keyPath: v.key }))
R.mapWithIndex((storeName: string, v: Store<StoreC>) => {
req.result.objectStoreNames.contains(storeName) && req.result.deleteObjectStore(storeName);
req.result.createObjectStore(storeName, { keyPath: v.key });
})
);

req.onsuccess = () => resolve({ database: req.result, schema: schema });
Expand Down Expand Up @@ -194,3 +197,5 @@ export const clearStore = <StoreC extends t.Mixed>(
}),
handlePromiseError,
);

export const close = <StoreC extends t.Mixed>(db: DatabaseInfo<StoreC>): void => db.database.close();
21 changes: 18 additions & 3 deletions test/database.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import * as t from 'io-ts';

import {
DBSchema,
close,
get,
getAll,
insert,
Expand All @@ -20,7 +21,7 @@ const userC = t.type({
type UserC = typeof userC;
type User = t.TypeOf<UserC>;

const schema: DBSchema<UserC> = {
const schema1: DBSchema<UserC> = {
version: 1,
stores: {
'users': {
Expand All @@ -29,14 +30,15 @@ const schema: DBSchema<UserC> = {
}
}
};
const schema2: DBSchema<UserC> = { ...schema1, version: 2 };

const dbErrorTe = TE.left(new DOMException('Error loading database'));

describe('IndexedDb - Tests', () => {
it('Crud - Functions', async () => {
// Set Database
await pipe(
open('my-db31', schema),
open('my-db31', schema1),
TE.match(
fail,
async (db) => {
Expand Down Expand Up @@ -68,7 +70,7 @@ describe('IndexedDb - Tests', () => {
(rows) => {
expect(rows.length).toBeGreaterThanOrEqual(1);
pipe(
O.fromNullable(schema.stores.users?.codec),
O.fromNullable(schema1.stores.users?.codec),
O.map((c) => {
expect(t.array(c).is(rows)).toBeTruthy();
})
Expand Down Expand Up @@ -107,7 +109,20 @@ describe('IndexedDb - Tests', () => {
},
)
)();

close(db);
},
),
)();

// Upgrade schema
await pipe(
open('my-db31', schema2),
TE.match(
fail,
(db) => {
expect(db.schema.version).toEqual(schema2.version);
}
)
)();
});
Expand Down