esperodb
v1.0.5
Published
A TypeScript library for managing a local IndexedDB database.
Downloads
21
Readme
EsperoDB
EsperoDB
is a TypeScript library for managing a local IndexedDB database.
Installation
npm install esperodb
Usage
import { EsperoDB } from 'esperodb';
// Example database structure
const dataStructure: any = [
{
'table1': [
{ indexes: [{ 'index1': { unique: false } }], primaryKey: 'id' },
],
},
{
'table2': [
// Table structure without indexes
],
},
];
const dbVersion: number = 1;
// Create an instance of the local database
const db = new EsperoDB('myDatabase', dataStructure, dbVersion);
// Add data to a table
const dataToAdd = { id: 1, name: 'John Doe' };
db.addData('table1', dataToAdd)
.then((result) => console.log(result))
.catch((error) => console.error(error));
// Update data in a table
const updatedData = { id: 1, name: 'Jane Doe' };
db.updateData('table1', updatedData)
.then((result) => console.log(result))
.catch((error) => console.error(error));
// Retrieve data from a table by primary key
db.getData('table1', 1)
.then((result) => console.log(result))
.catch((error) => console.error(error));
// Retrieve all data from a table
db.getAllData('table1')
.then((result) => console.log(result))
.catch((error) => console.error(error));
// Search for data in a table by index
db.search('table1', 'index1', 'someValue')
.then((result) => console.log(result))
.catch((error) => console.error(error));
// Delete data from a table by primary key
db.deleteData('table1', 1)
.then((result) => console.log(result))
.catch((error) => console.error(error));
API
EsperoDB
Constructor
constructor(dbName: string, dataStructure: TableData, dbVersion?: number = 1);
Creates an instance of EsperoDB
.
dbName
: The name of the database.dataStructure
: The structure of the database including table names, indexes, and primary keys.dbVersion
: The version of the database (default: 1).
Methods
openDatabase(): Promise<IDBDatabase | null>
Opens the IndexedDB database and creates or upgrades object stores based on the provided data structure.addData(dbTable: string, data: Record<string, any>): Promise<string>
Adds data to the specified table in the database.updateData(dbTable: string, data: Record<string, any>): Promise<string>
Updates data in the specified table in the database.getData(dbTable: string, key: any): Promise<Record<string, any>>
Retrieves data from the specified table based on the provided key.getAllData(dbTable: string): Promise<Record<string, any>[]>
Retrieves all data from the specified table in the database.search(tableName: string, indexName: string, searchValue: any): Promise<Record<string, any>[]>
Searches for records in a specified table based on a given index and value.deleteData(dbTable: string, key: any): Promise<string>
Deletes data from the specified table in the database.getDataWithPagination(dbTable: string, currentPage: number, pageSize: number): Promise<{ isSuccess: boolean; results?: Record<string, any>[]; totalPages?: number; currentPage?: number; nextPage?: number | null; previousPage?: number | null; }>
Retrieves paginated data from the specified table in the database, including information about total pages, current page, next page, and previous page.searchByTag(tableName: string, searchField: string, keyword: string, currentPage: number, pageSize: number): Promise<{ isSuccess: boolean; results?: Record<string, any>[]; totalPages?: number; currentPage?: number; nextPage?: number | null; previousPage?: number | null; resultCount?: number | null; allCount?: number | null; pageLinks?: string[]; }>
Searches for data in a table by a specified search field and keyword with pagination.
tableName
: The name of the table to search.searchField
: The field in the table to search for.keyword
: The keyword to search for.currentPage
: The current page number for pagination.pageSize
: The number of items per page for pagination.
Returns an object with search results, pagination details, and counts.
Notes
- Ensure a good understanding of the data structure when creating the database.
- Use the provided methods by
EsperoDB
to interact with the database asynchronously.
## Support
Owner: Mudey Formation (https://mudey.fr)
For questions or support, please contact [[email protected]].