@n1md7/indexeddb-promise
Advanced tools
Comparing version 7.0.5 to 7.1.0
@@ -18,5 +18,10 @@ import { Optional } from 'utility-types'; | ||
* @description This method is used to select data from the table by defined Index key. | ||
* Returns first match when multiple record with the same key. | ||
*/ | ||
selectByIndex(indexName: string, value: IDBValidKey | IDBKeyRange): Promise<DataType | undefined>; | ||
/** | ||
* @description This method is used to select data from the table by defined Index key. | ||
*/ | ||
selectByIndexAll(indexName: string, value: IDBValidKey | IDBKeyRange): Promise<DataType[]>; | ||
/** | ||
* @description This method is used to select all the data from the table. | ||
@@ -23,0 +28,0 @@ */ |
@@ -120,2 +120,3 @@ "use strict"; | ||
* @description This method is used to select data from the table by defined Index key. | ||
* Returns first match when multiple record with the same key. | ||
*/ | ||
@@ -137,2 +138,19 @@ Model.prototype.selectByIndex = function (indexName, value) { | ||
/** | ||
* @description This method is used to select data from the table by defined Index key. | ||
*/ | ||
Model.prototype.selectByIndexAll = function (indexName, value) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var _this = this; | ||
return __generator(this, function (_a) { | ||
return [2 /*return*/, new Promise(function (resolve, reject) { | ||
var transaction = _this.db.transaction(_this.table.name, 'readonly'); | ||
var objectStore = transaction.objectStore(_this.table.name); | ||
var request = objectStore.index(indexName).getAll(value); | ||
request.onerror = function () { return reject(request.error || "Unable to retrieve data from the model by ".concat(indexName)); }; | ||
request.onsuccess = function () { return resolve(_this.resolveValue(request.result)); }; | ||
})]; | ||
}); | ||
}); | ||
}; | ||
/** | ||
* @description This method is used to select all the data from the table. | ||
@@ -139,0 +157,0 @@ */ |
{ | ||
"name": "@n1md7/indexeddb-promise", | ||
"version": "7.0.5", | ||
"version": "7.1.0", | ||
"description": "Indexed DB wrapper with promises", | ||
@@ -5,0 +5,0 @@ "main": "./lib/index.js", |
@@ -94,12 +94,13 @@ [![npm databaseVersion](https://badge.fury.io/js/@n1md7%2Findexeddb-promise.svg)](https://badge.fury.io/js/@n1md7%2Findexeddb-promise) | ||
| Method | Description | Params | | ||
| --------------- | ------------------------------------------ | ----------------------- | | ||
| `selectAll` | Get the data from database | None | | ||
| `select` | Fetch data from database with filters | Object | | ||
| `insert` | Add data into database | Object | | ||
| `openCursor` | Get database cursor to iterate on demand | None | | ||
| `selectByIndex` | Select database data by indexed properties | String, String | | ||
| `selectByPk` | Select database record by primary key | String \ Number | | ||
| `updateByPk` | Update database record by primary key | Number \ Number, Object | | ||
| `deleteByPk` | Delete database record by primary key | String \ Number | | ||
| Method | Description | Params | | ||
| ------------------ | ------------------------------------------------- | ----------------------- | | ||
| `selectAll` | Get the data from database | None | | ||
| `select` | Fetch data from database with filters | Object | | ||
| `insert` | Add data into database | Object | | ||
| `openCursor` | Get database cursor to iterate on demand | None | | ||
| `selectByIndex` | Select database data by indexed properties (one) | String, String | | ||
| `selectByIndexAll` | Select database data by indexed properties (many) | String, String | | ||
| `selectByPk` | Select database record by primary key | String \ Number | | ||
| `updateByPk` | Update database record by primary key | Number \ Number, Object | | ||
| `deleteByPk` | Delete database record by primary key | String \ Number | | ||
@@ -205,2 +206,19 @@ ### - selectAll | ||
### - selectByIndexAll | ||
#### Params | ||
Gets the data from database with the specified indexed property. | ||
Return type: Promise<Array<Object|{}>> | ||
Accept params: indexName: string, valueToMatch: string | ||
#### JS/TS example | ||
```javascript | ||
model.selectByIndexAll('username', 'admin').then((data) => data); | ||
const data = await model.selectByIndexAll('username', 'admin'); | ||
``` | ||
### - selectByPk | ||
@@ -207,0 +225,0 @@ |
@@ -68,2 +68,3 @@ import ArraySorter from './array-sorter'; | ||
* @description This method is used to select data from the table by defined Index key. | ||
* Returns first match when multiple record with the same key. | ||
*/ | ||
@@ -81,2 +82,15 @@ public async selectByIndex(indexName: string, value: IDBValidKey | IDBKeyRange): Promise<DataType | undefined> { | ||
/** | ||
* @description This method is used to select data from the table by defined Index key. | ||
*/ | ||
public async selectByIndexAll(indexName: string, value: IDBValidKey | IDBKeyRange): Promise<DataType[]> { | ||
return new Promise((resolve, reject) => { | ||
const transaction = this.db.transaction(this.table.name, 'readonly'); | ||
const objectStore = transaction.objectStore(this.table.name); | ||
const request: IDBRequest<DataType[]> = objectStore.index(indexName).getAll(value); | ||
request.onerror = () => reject(request.error || `Unable to retrieve data from the model by ${indexName}`); | ||
request.onsuccess = () => resolve(this.resolveValue(request.result) as DataType[]); | ||
}); | ||
} | ||
/** | ||
* @description This method is used to select all the data from the table. | ||
@@ -83,0 +97,0 @@ */ |
@@ -80,2 +80,5 @@ import { describe, expect, it, beforeAll, beforeEach, jest } from '@jest/globals'; | ||
const user = await userModel.selectByIndex('username', 'admin'); | ||
const users = await userModel.selectByIndexAll('username', 'admin'); | ||
expect(users).toHaveLength(1); | ||
expect(user).toEqual(users[0]); | ||
expect(user).toBeInstanceOf(User); | ||
@@ -82,0 +85,0 @@ expect(user).toEqual({ |
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
889317
15081
571