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

indexedkit

v1.0.4

Published

![example workflow](https://github.com/AllanPinheiroDeLima/indexedDBWrapper/actions/workflows/main.yml/badge.svg)

Downloads

294

Readme

example workflow

IndexedKit

A simple and powerful wrapper for IndexedDB that simplifies data persistence in web applications. The DataStore class provides methods for CRUD operations, bulk inserts, and flexible querying capabilities.

Features

  • CRUD Operations: Easily create, read, update, and delete records.
  • Bulk Insert: Insert multiple records in one go.
  • Flexible Queries: Execute queries with conditions and offsets.
  • Custom ID Generation: Supports custom ID generators.
  • Search Engine Integration: Built-in search capabilities for field-based queries.

Installation

You can install the DataStore library via npm:

npm install indexedkit

Usage

Importing

First, import the DataStore class:

import { DataStore } from "indexedkit";

Initialization

Create an instance of the DataStore by specifying the database name, collection name, and optional data store options.

const dataStore = new DataStore<MyDataType>("myDatabase", "myCollection", {
    idKey: "id",
    indexes: [{ name: "nameIndex", keyPath: "name", unique: false }],
    idGenerator: () => "custom-id" // optional custom ID generator
});

Initialization Method

Initialize the database. It is mandatory the call of this method, otherwise, the database will return an error:

await dataStore.init();

CRUD Operations

Insert

Insert a single record:

const newUser = { name: "Alice", age: 30 };
await dataStore.insert(newUser);

Bulk Insert

Insert multiple records:

const users = [
    { name: "Bob", age: 25 },
    { name: "Charlie", age: 35 },
];
await dataStore.bulkInsert(users);

Upsert

Insert or update a record. important if you are trying to update an existing record, this will shallow merge the existing record, overwriting any existing data with your new data:

const userInDataBase = { id: "1", name: "Alice", age: 31, subFields: [] }

await dataStore.upsert({ id: "1", name: "Alice", age: 31 });

// When you try and find this record again, the subFields WILL be missing! Be warned!

Update

Update a record based on a query. The update method is used to update records. This operation makes a deep merge of the objects keeping any subFields intact in case they weren't sent to the operation:

await dataStore.update({ where: { name: "Alice" } }, { age: 32 });

Remove

Remove a record by ID. Remember that the type of your key is important! By default, all ids are string, but if you overwrote using the idKey option you should use the correct type to find your record. If it is a number, you should use a number to find. We will address this issue in the future roadmap so the types can hint the ids:

await dataStore.removeByIdKey("1");

Clear Collection

Clear all records from a collection:

await dataStore.clearCollection();

Querying

Find All

Retrieve all records with optional query parameters:

const allUsers = await dataStore.findAll({ where: { age: 30 } });

You can paginate the records using limit and offset keys:

const allUsers = await dataStore.findAll({ where: { age: 30 }, limit: 5, offset: 5 });

Find One

Find a single record based on query parameters:

const user = await dataStore.findOne({ where: { name: "Alice" } });

Error Handling

The DataStore class throws specific errors for various scenarios:

  • DatabaseNotDefinedError: Thrown when the database is not initialized.
  • InvalidInputError: Thrown for invalid input data.
  • CollectionNotFoundError: Thrown when the specified collection does not exist.
  • DocumentNotFoundError: Thrown when the specified document is not found.

License

This project is licensed under the MIT License.

Contributing

Contributions are welcome! Please open an issue or submit a pull request for any improvements or bug fixes.