@n1md7/indexeddb-promise
Advanced tools
Comparing version 7.1.0 to 7.1.1
@@ -10,2 +10,3 @@ import { Optional } from 'utility-types'; | ||
protected readonly databaseVersion: number; | ||
static indexedDB: IDBFactory; | ||
constructor(config: ConfigType | ConfigType<Function>); | ||
@@ -25,2 +26,3 @@ connect(): Promise<IDBDatabase>; | ||
useModel<CollectionType>(tableName: string): Model<CollectionType & Optional<TimeStampsType>>; | ||
private static getIndexedDB; | ||
} |
@@ -66,2 +66,3 @@ "use strict"; | ||
this.databaseVersion = 1; | ||
Database.indexedDB = Database.getIndexedDB(); | ||
if (Array.isArray(config)) { | ||
@@ -125,6 +126,6 @@ throw new IDBError_1.default(IDBError_1.default.compose('Config has to be an Object')); | ||
return new Promise(function (resolve, reject) { | ||
if (!window || !('indexedDB' in window) || !('open' in window.indexedDB)) { | ||
if (!Database.indexedDB) { | ||
return reject('Unsupported environment'); | ||
} | ||
var request = window.indexedDB.open(_this.databaseName, _this.databaseVersion); | ||
var request = Database.indexedDB.open(_this.databaseName, _this.databaseVersion); | ||
request.onerror = function () { return reject(request.error); }; | ||
@@ -186,3 +187,3 @@ request.onsuccess = function () { | ||
return [2 /*return*/, new Promise(function (resolve, reject) { | ||
var request = window.indexedDB.deleteDatabase(name); | ||
var request = Database.indexedDB.deleteDatabase(name); | ||
request.onblocked = function () { | ||
@@ -260,2 +261,9 @@ reject("[".concat(name, "]: Couldn't remove database, database is blocked. Close all connections and try again.")); | ||
}; | ||
Database.getIndexedDB = function () { | ||
if (typeof indexedDB !== 'undefined') | ||
return indexedDB; | ||
if (typeof window !== 'undefined' && typeof window.indexedDB !== 'undefined') | ||
return window.indexedDB; | ||
return null; | ||
}; | ||
return Database; | ||
@@ -262,0 +270,0 @@ }()); |
{ | ||
"name": "@n1md7/indexeddb-promise", | ||
"version": "7.1.0", | ||
"version": "7.1.1", | ||
"description": "Indexed DB wrapper with promises", | ||
@@ -49,3 +49,3 @@ "main": "./lib/index.js", | ||
"devDependencies": { | ||
"@babel/core": "^7.16.0", | ||
"@babel/core": "^7.18.6", | ||
"@babel/preset-env": "^7.16.0", | ||
@@ -57,2 +57,3 @@ "@babel/preset-typescript": "^7.16.0", | ||
"browserify": "^17.0.0", | ||
"class-transformer": "^0.5.1", | ||
"fake-indexeddb": "^3.1.7", | ||
@@ -63,6 +64,8 @@ "http-server": "^14.0.0", | ||
"jest-junit": "^13.0.0", | ||
"joi": "^17.6.0", | ||
"jsdom": "^16.7.0", | ||
"lint-staged": "^11.2.6", | ||
"nodemon": "^2.0.15", | ||
"nodemon": "^3.0.1", | ||
"prettier": "2.4.1", | ||
"reflect-metadata": "^0.1.13", | ||
"ts-jest": "^27.0.7", | ||
@@ -72,6 +75,3 @@ "ts-node": "^10.4.0", | ||
"typescript": "^4.4.4", | ||
"utility-types": "^3.10.0", | ||
"class-transformer": "^0.5.1", | ||
"joi": "^17.6.0", | ||
"reflect-metadata": "^0.1.13" | ||
"utility-types": "^3.10.0" | ||
}, | ||
@@ -99,3 +99,3 @@ "lint-staged": { | ||
"engines": { | ||
"node": ">=12.0.0", | ||
"node": ">=14.0.0", | ||
"npm": ">=6.0.0" | ||
@@ -102,0 +102,0 @@ }, |
@@ -15,3 +15,6 @@ import { Optional } from 'utility-types'; | ||
static indexedDB: IDBFactory; | ||
constructor(protected readonly config: ConfigType | ConfigType<Function>) { | ||
Database.indexedDB = Database.getIndexedDB(); | ||
if (Array.isArray(config)) { | ||
@@ -78,7 +81,7 @@ throw new IDBError(IDBError.compose('Config has to be an Object')); | ||
return new Promise((resolve, reject) => { | ||
if (!window || !('indexedDB' in window) || !('open' in window.indexedDB)) { | ||
if (!Database.indexedDB) { | ||
return reject('Unsupported environment'); | ||
} | ||
const request = window.indexedDB.open(this.databaseName, this.databaseVersion); | ||
const request = Database.indexedDB.open(this.databaseName, this.databaseVersion); | ||
request.onerror = () => reject(request.error); | ||
@@ -138,3 +141,3 @@ request.onsuccess = () => { | ||
return new Promise((resolve, reject) => { | ||
const request = window.indexedDB.deleteDatabase(name); | ||
const request = Database.indexedDB.deleteDatabase(name); | ||
request.onblocked = () => { | ||
@@ -209,2 +212,9 @@ reject(`[${name}]: Couldn't remove database, database is blocked. Close all connections and try again.`); | ||
} | ||
private static getIndexedDB() { | ||
if (typeof indexedDB !== 'undefined') return indexedDB; | ||
if (typeof window !== 'undefined' && typeof window.indexedDB !== 'undefined') return window.indexedDB; | ||
return null; | ||
} | ||
} |
@@ -5,39 +5,2 @@ import { beforeAll, beforeEach, describe, expect, it, jest } from '@jest/globals'; | ||
describe('Database', function () { | ||
beforeAll(() => jest.clearAllMocks()); | ||
beforeEach(() => jest.clearAllMocks()); | ||
it('should throw Unsupported environment: mocked', () => { | ||
jest.spyOn(window.indexedDB, 'open').mockImplementation(() => { | ||
throw new Error('Unsupported environment'); | ||
}); | ||
new Database({ | ||
version: 1, | ||
name: `Test-db-000`, | ||
tables: [ | ||
{ | ||
name: 'users', | ||
primaryKey: { | ||
name: 'username', | ||
autoIncrement: false, | ||
unique: true, | ||
}, | ||
initData: [ | ||
{ username: 'n1md7', password: 'passwd' }, | ||
{ username: 'admin', password: 'admin123' }, | ||
], | ||
indexes: { | ||
username: { unique: true, multiEntry: false }, | ||
password: { unique: false, multiEntry: false }, | ||
}, | ||
}, | ||
], | ||
}) | ||
.connect() | ||
.catch((err) => { | ||
expect(err).toBeInstanceOf(Error); | ||
expect(err.message).toBe('Unsupported environment'); | ||
}); | ||
}); | ||
it('unsupported environment', async function () { | ||
@@ -60,7 +23,3 @@ const ref = { window }; | ||
try { | ||
await db.connect(); | ||
} catch (message) { | ||
expect(message).toBe('Unsupported environment'); | ||
} | ||
await expect(db.connect()).rejects.toBe('Unsupported environment'); | ||
@@ -67,0 +26,0 @@ // restore the global window object |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
888768
15034