Socket
Socket
Sign inDemoInstall

@n1md7/indexeddb-promise

Package Overview
Dependencies
0
Maintainers
1
Versions
32
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    @n1md7/indexeddb-promise

Indexed DB wrapper with promises


Version published
Weekly downloads
2
decreased by-71.43%
Maintainers
1
Created
Weekly downloads
 

Readme

Source

npm version Node.js Package Node.js CI - tests GitHub codecov

Indexed DB wrapper with promises

Demo

Installation

npm install @n1md7/indexeddb-promise --save
# or
yarn add @n1md7/indexeddb-promise

or

<script src="https://bundle.run/@n1md7/indexeddb-promise@2.0.0"></script>
<script src="https://unpkg.com/@n1md7/indexeddb-promise@2.0.0/src/index.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@n1md7/indexeddb-promise@2.0.0/dist/indexed-db.min.js"></script>

Available methods

  • select
  • insert
  • selectAll
  • selectByPk
  • updateByPk
  • deleteByPk

.selectAll(): Promise

Gets all the data from db and returns promise with response data

.selectByPk(pKey: string): Promise

Has one parameter pkey as primaryKey and returns promise with data

.select({...}): Promise

Has one parameter props which can be

/**
 * @typedef {string | number | boolean | null | undefined} Item
 */
/**
 * @typedef {{[key: string]: Item} | Item} ListItem
 */
/**
 * @param  {{
 *   where?: {
 *     [key: string]: any
 *   } | function(ListItem[]):ListItem[],
 *   limit?: number,
 *   orderByDESC?: boolean,
 *   sortBy?: string | string[]
 * }} options
 * @returns {Promise<ListItem[]>}
 */
props = {
  limit: 10,
  where: (dataArray) => {
    return dataArray;
  },
  orderByDESC: true,
  sortBy: 'comments', // ['comments', 'date']
};

@where property can filter out data like

where: (data) => data.filter((item) => item.username === 'admin');

or it can be an object, which gets data with AND(&&) comparison

where: {
  username: 'admin',
  password:'admin123'
}

.updateByPk(pKey, {...}): Promise

Has two parameters pkey and keyValue pair of updated data

.updateByPk(123, { username: 'admin' })

.deleteByPk(pKey): Promise

Has one parameter pKey which record to delete based on primary key

note primary key is type sensitive. If it is saved as integer then should pass as integer and vice versa

Usage example

<html>
  <head>
    <title>IndexedDB app</title>
    <script src="./dist/indexed-db.min.js"></script>
  </head>
  <body>
    <script>
      // Your script here
    </script>
  </body>
</html>

Once you add indexed-db.min.js in your document then you will be able to access IndexedDBModel variable globally which contains Model. They can be extracted as following

const { Model } = IndexedDBModel;

// or
const Model = IndexedDBModel.Model;

Create example config

class DB extends IndexedDBModel.Model {
  //@overrides default method
  get config() {
    return {
      version: 1,
      databaseName: 'myNewDatabase',
      tableName: 'myNewTable',
      primaryKey: {
        name: 'id',
        autoIncrement: false,
        unique: true,
      },
      initData: [],
      structure: {
        username: { unique: false, autoIncrement: false },
        password: { unique: false, autoIncrement: false },
        createdAt: { unique: false, autoIncrement: false },
        updatedAt: { unique: false, autoIncrement: false },
      },
    };
  }
}

Create connector

const db = new DB();

Full example

<html>
  <head>
    <title>IndexedDB app</title>
    <script src="./dist/indexed-db.min.js"></script>
  </head>
  <body>
    <script>
      class DB extends IndexedDBModel.Model {
        //@overrides default method
        get config() {
          return {
            version: 1,
            databaseName: 'myNewDatabase',
            tableName: 'myNewTable',
            primaryKey: {
              name: 'id',
              autoIncrement: false,
              unique: true,
            },
            initData: [],
            structure: {
              username: { unique: false, autoIncrement: false },
              password: { unique: false, autoIncrement: false },
              createdAt: { unique: false, autoIncrement: false },
              updatedAt: { unique: false, autoIncrement: false },
            },
          };
        }
      }

      const db = new DB();

      // add a new record
      db.insert({
        id: Math.random() * 10,
        username: 'admin',
        password: 'nimda',
        createdAt: new Date(),
        updatedAt: new Date(),
      })
        .then(function () {
          console.info('Yay, you have saved the data.');
        })
        .catch(function (error) {
          console.error(error);
        });

      // Get all results from the database
      db.selectAll().then(function (results) {
        console.log(...results);
      });
    </script>
  </body>
</html>
const IndexedDBModel = require('@n1md7/indexeddb-promise');
const { Model } = IndexedDBModel;
// or
import { Model } from '@n1md7/indexeddb-promise';

Keywords

FAQs

Last updated on 07 Nov 2021

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc