Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@n1md7/indexeddb-promise

Package Overview
Dependencies
Maintainers
1
Versions
32
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@n1md7/indexeddb-promise

Indexed DB wrapper with promises

  • 5.0.21
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
2
Maintainers
1
Weekly downloads
 
Created
Source

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

Indexed DB wrapper with promises

Demo

Installation

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

or

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

Available methods

  • select
  • insert
  • selectAll
  • setTable
  • selectByIndex
  • selectByPk
  • updateByPk
  • deleteByPk

.selectAll(): Promise

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

.selectByIndex(indexName: string, valueToMatch: string): Promise

Gets data from the db and returns connection with response data

.selectByPk(pKey: string): Promise

Has one parameter pkey as primaryKey and returns connection with data

.select({...}): Promise

Has one parameter props which can be

const props = {
  limit: 10,
  where: (dataArray) => {
    return dataArray;
  },
  orderByDESC: true,
  sortBy: 'comments', // ['comments', 'date']
};

@where property can filter out data like

const props = {
  where: (data) => data.filter((item) => item.username === 'admin'),
};

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

const props = {
  where: {
    username: 'admin',
    password: 'admin123',
  },
};

.updateByPk(pKey: string | number, {...}): 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 { Database } = IndexedDBModel;

// or
const Database = IndexedDBModel.Database;

Create connector and pass the config

const db = new IndexedDBModel.Database({
  databaseVersion: 1,
  databaseName: 'myNewDatabase',
  tables: [
    {
      name: 'myNewTable',
      primaryKey: {
        name: 'id',
        autoIncrement: false,
        unique: true,
      },
      initData: [],
      indexes: {
        username: { unique: false, autoIncrement: false },
        password: { unique: false, autoIncrement: false },
      },
      timestamps: true,
    },
  ],
});

Full example

<html>
  <head>
    <title>IndexedDB app</title>
    <script src="./dist/indexed-db.min.js"></script>
  </head>
  <body>
    <script>
      const db = new IndexedDBModel.Database({
        databaseVersion: 1,
        databaseName: 'myNewDatabase',
        tables: [
          {
            name: 'myNewTable',
            primaryKey: {
              name: 'id',
              autoIncrement: false,
              unique: true,
            },
            initData: [],
            indexes: {
              username: { unique: false, autoIncrement: false },
              password: { unique: false, autoIncrement: false },
            },
          },
        ],
      });

      // add a new record
      db.setTable('myNewTable')
        .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.setTable('myNewTable')
        .selectAll()
        .then(function (results) {
          console.log(...results);
        });
    </script>
  </body>
</html>
const IndexedDBModel = require('@n1md7/indexeddb-promise');
const { Database } = IndexedDBModel;
// or
import { Database } from '@n1md7/indexeddb-promise';

Typescript example

import { Database } from '@n1md7/indexeddb-promise';

interface Users {
  id?: number;
  username: string;
  password: string;
}

enum Priority {
  LOW = 'LOW',
  MEDIUM = 'MEDIUM',
  HIGH = 'HIGH',
}

interface ToDos {
  id?: number;
  userId: number;
  title: string;
  description: string;
  done: boolean;
  priority: Priority;
}

const database = new Database<Users | ToDos>({
  version: 2,
  name: 'Todo-list',
  tables: [
    {
      name: 'users',
      primaryKey: {
        name: 'id',
        autoIncrement: true,
        unique: true,
      },
      indexes: {
        username: {
          unique: false,
        },
      },
      timestamps: true,
    },
    {
      name: 'todos',
      primaryKey: {
        name: 'id',
        autoIncrement: true,
        unique: true,
      },
      indexes: {
        userId: {
          unique: true,
        },
      },
      timestamps: true,
    },
  ],
});

(async () => {
  const user = await database.setTable('users').insert({
    username: 'admin',
    password: 'admin',
  });
  await database.setTable('todos').insert({
    userId: user.id,
    title: 'Todo 1',
    description: 'Description 1',
    done: false,
    priority: Priority.LOW,
  });
})();

Keywords

FAQs

Package last updated on 12 Nov 2021

Did you know?

Socket

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
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc