npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@nulink_network/ts-indexdb

v0.0.6

Published

typescript indexDb capsulation

Downloads

3

Readme

nulink-ts-indexdb

Build Status

Install

npm install @nulink_network/ts-indexdb
yarn add @nulink_network/ts-indexdb

Usage

Typescript

import { init, getInstance } from 'ts-indexdb';
export type Rack =  {
    name: string
    id?: number
}

javascript

import TsIndexDb = require('ts-indexdb');

Methods of Database Operations

Note:

  • This class is a singleton pattern, so it only needs to be initialized once with init(), and then you can use getInstance() to get the instance for database operations.
  • All the operations return Promises.
  • Generics are not needed in JavaScript.

Database and Table Operations

| Method | Method Name | Parameter | Attribute | | --- | --- | --- | --- | | open_db | Open Database | None | - | | close_db | Close Database | None | - | | delete_db | Delete Database | String | name | | delete_table | Delete Table Data | String | tableName |

Query Operations

| Method | Method Name | Parameter | Attribute | | --- | --- | --- | --- | | queryAll | Query all data of a table (return as an array) | Object | { tableName } | | query | Query data (return as an array) | Object | { tableName, condition } | | query_by_keyValue | Query data by specific table property (return as a specific item) | Object | { tableName, key, value } | | query_by_primaryKey | Query data by primary key value | Object | { tableName, value } | | count | Count data by primary key value | Object | { tableName, key, countCondition:{type,rangeValue } } |

Update Operations

| Method | Method Name | Parameter | Attribute | | --- | --- | --- | --- | | update | Update data based on a condition (return the modified data as an array) | Object | { tableName, condition, handle } | | update_by_primaryKey | Update specific data by primary key value (return the modified data as an object) | Object | { tableName, value, handle } |

Insert Operations

| Method | Method Name | Parameter | Attribute | | --- | --- | --- | --- | | insert | Insert data | Object | { tableName, data(array or single object) } |

Delete Operations

| Method | Method Name | Parameter | Attribute | | --- | --- | --- | --- | | delete | Delete data based on a condition (return the deleted data as an array) | Object | { tableName, condition } | | delete_by_primaryKey | Delete data by primary key value | Object | { tableName, value } |

Example:

Initialization

await init({
    dbName: "books",        // database name               
    version: 1,             // version number                
    tables: [                               
        {
            tableName: "bookrackList",         // table name         
            option: { keyPath: "id", autoIncrement: true }, // specify the primary key as "id"
            indexs: [    // database indexes
                {
                    key: "id",
                    option: {
                        unique: true
                    }
                },
                {
                    key: "name"
                }
            ]
        }
    ]
})

Query

/**
    * @method Query all data of a table (return as an array)
    * @param {Object}
    *   @property {String} tableName table name
    */
  await getInstance().queryAll<Rack>({
    tableName: 'bookrackList'
  });


  /**
    * @method Query data (return as an array)
    * @param {Object}
    *   @property {String} tableName table name
    *   @property {Function} condition query condition
    * */
  await getInstance().query<Rack>({
     tableName: 'bookrackList',
     condition: item => item.id === 3
   });

  /**
    * @method Query data by specific table property (return as a specific item)
    * @param {Object}
    *   @property {String} tableName table name
    *   @property {Number|String} key property name
    *   @property {Number|String} value property value
    *
    * */
  await getInstance().query_by_keyValue<Rack>({
     tableName: 'bookrackList',
     key: 'name',
     value: 'My senior fellow apprentice is really steady'
   });

  /**
    * @method Query data by primary key value
    * @param {Object}
    *   @property {String} tableName table name
    *   @property {Number|String} value primary key value
    *
    * */ 
  await getInstance().query_by_primaryKey<Rack>({
     tableName: 'bookrackList',
     value: 3
   });

  /**
     * @method Count data by primary key value
     * @param {Object}
     *   @property {String} tableName table name
     *   @property {Number|String} key query key
     *   @property {Object} countCondition query condition
   * */
  /** Count condition input method:
   *  key ≥ x	            {key: 'gt' rangeValue: [x]}
      key > x	            {key: 'gt' rangeValue: [x, true]}
      key ≤ y	            {key: 'lt' rangeValue: [y]}
      key < y	            {key: 'lt' rangeValue: [y, true]}
      key ≥ x && ≤ y	    {key: 'between' rangeValue: [x, y]}
      key > x &&< y	    {key: 'between' rangeValue: [x, y, true, true]}
      key > x && ≤ y	    {key: 'between' rangeValue: [x, y, true, false]}
      key ≥ x &&< y	    {key: 'between' rangeValue: [x, y, false, true]}
      key = z	            {key: 'equal' rangeValue: [z]}
   */
  await getInstance().count<Rack>({
    tableName: 'bookrackList',
    key: 'createdTime',
    countCondition: {
      type: 'between',
      rangeValue:[1676627113088,new Date().getTime()]
    }
  })

Update

/**
     * @method Update data based on a condition (return the modified data as an array)
     * @param {Object}
     *   @property {String} tableName table name
     *   @property {Function} condition query condition, traverse, similar to filter
     *      @arg {Object} each element
     *      @return condition
     *   @property {Function} handle processing function, receiving a reference to this data, and modifying it
     * */
  await getInstance().update<Rack>({
        tableName: 'bookrackList',
        condition: item => item.id === 8,
        handle: r => {
          r.name = 'test modification';
          return r;
        }
  })


  /**
  * @method Update specific data by primary key value (return the modified data as an object)
  * @param {Object}
  *   @property {String} tableName table name
  *   @property {String|Number} value target primary key value
  *   @property {Function} handle processing function, receiving a reference to this data, and modifying it
  * */
  await getInstance().update_by_primaryKey<Rack>({
        tableName: 'bookrackList',
        value: 1,
        handle: r => {
          r.name = 'test modification';
          return r;
        }
  })

Insert

/**
     * @method Insert data
     * @param {Object}
     *   @property {String} tableName table name
     *   @property {Object} data inserted data
     * */
  await getInstance().insert<Rack>({
    tableName: 'bookrackList',
    data: {
      name: 'test',
    }
  })

Delete

/**
  * @method Delete data based on a condition (return the deleted data as an array)
  * @param {Object}
  *   @property {String} tableName table name
  *   @property {Function} condition query condition, traverse, similar to filter
  *      @arg {Object} each element
  *      @return condition
  * */
await getInstance().delete<Rack>({
  tableName: 'bookrackList',
  condition: (item)=> item.name === 'test',
})


 /**
  * @method Delete data by primary key value
  * @param {Object}
  *   @property {String} tableName table name
  *   @property {String|Number} value target primary key value
  * */
await getInstance().delete_by_primaryKey<Rack>({
  tableName: 'bookrackList',
  value: 4
})

/**
  * @method Delete all data of a table
  * @param {String}name database name
  */
await getInstance().delete_table('bookrackList')


/**
  * @method Delete a database
  * @param {String}name database name
  */
await getInstance().delete_db('bookrackList')