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

  • 1.1.4
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
0
Maintainers
1
Weekly downloads
 
Created
Source

Indexed DB with promises

npm GitHub

Demo

  • Example01
  • Advanced Example02

Installation

npm install @n1md7/indexeddb-promise --save

or

yarn add @n1md7/indexeddb-promise

or

<script src="https://bundle.run/@n1md7/indexeddb-promise@1.1.3"></script>

or

<script src="https://unpkg.com/@n1md7/indexeddb-promise@1.1.3/src/index.js"></script>

or

<script src="https://cdn.jsdelivr.net/npm/@n1md7/indexeddb-promise@1.1.3/dist/indexeddb.min.js"></script>

Properties

  • selectAll
  • selectFrom
  • selectWhere
  • updateWhere
  • deleteWhere

.selectAll()

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

.selectFrom(pKey)

Has one parameter pkey as primaryKey and returns promise with data

.selectWhere({...})

Has one parameter props which can be

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

@where property can filter out data like where: data => data.filter(dt.username === 'admin')

.updateWhere(pKey, {...})

Has two parameters pkey and keyValue pair of updated data .updateWhere(123, {username: 'admin'})

.deleteWhere(pKey)

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/indexeddb-promise.min.js"></script>
</head>
<body>
    <script>
        // Your script here
    </script>
</body>
</html>

Once you add indexeddb-promise.min.js in your document then you will be able to access indexedDBModel variable globally which contains Model and ModelConfig. They can be extracted as following

const {
    ModelConfig,
    Model
} = indexedDBModel;

// or
const ModelConfig = indexedDBModel.ModelConfig;
const Model = indexedDBModel.Model;

Create example config

class Rooms extends ModelConfig{
    //@overrides default method
    get config() {
      return {
        version: 1,
        databaseName: 'myNewDatabase',
        tableName: 'myNewTable',
        primaryKey: {
          name: 'id',
          autoIncrement: false
        },
        initData: [],
        structure: {
          roomId: { unique: false,  autoIncrement: true },
          roomName: { unique: false,  autoIncrement: false },
          comments: { unique: false,  autoIncrement: false }
        }
      };
   }
}

Create connector

const db = new Model(new Rooms);

Full example

<html>
<head>
    <title>IndexedDB app</title>
    <script src="indexeddb-promise.min.js"></script>
</head>
<body>
    <script>
        const {
            ModelConfig,
            Model
        } = indexedDBModel;
        
        class Rooms extends ModelConfig{
            //@overrides default method
            get config() {
              return {
                version: 1,
                databaseName: 'myNewDatabase',
                tableName: 'myNewTable',
                primaryKey: {
                  name: 'id',
                  autoIncrement: false
                },
                initData: [],
                structure: {
                  roomId: { unique: false,  autoIncrement: true },
                  roomName: { unique: false,  autoIncrement: false },
                  comment: { unique: false,  autoIncrement: false }
                }
              };
           }
        }
        
        const db = new Model(new Rooms);
        
        // add new record
        db.insertData({
              'id': Math.random() * 10,
              'roomName': 'My room name',
              'roomId': 1,
              'comment': 'This room is awesome'
            })
              .then(function(){
                //when done click update button
                console.info('Yay, you have saved the data.');
              }).catch(function(error){
                console.error(error);
            });
        
        // Get all results from DB
        db.selectAll()
          .then(function(results){
            console.log(...results);
          });

    </script>
</body>
</html>
const indexedDBModel = require('@n1md7/indexeddb-promise');
const { ModelConfig, Model } = indexedDBModel;

Keywords

FAQs

Package last updated on 11 Jan 2020

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