# js-database

> ## **library for simple indexeddb usage**

Latest version **0.5.1** (published 2021-10-08) · MIT license · 0 weekly downloads

## Install

```sh
npm install js-database
pnpm add js-database
yarn add js-database
bun add js-database
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.5.1 |
| Published | 2021-10-08 |
| First published | 2021-06-03 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 3 |
| Unpacked size | 75.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Ilya Rastorguev |
| Maintainers | ilya_rastorguev |
| Keywords | indexeddb, storage, database |

## Links

- npm: https://www.npmjs.com/package/js-database
- npm.io page: https://npm.io/package/js-database

## Dependencies (3)

- [jsdocs](https://npm.io/package/jsdocs.md) ^1.0.0
- [typescript](https://npm.io/package/typescript.md) ^4.3.2
- [@types/node](https://npm.io/package/@types/node.md) ^12.20.13

## Alternatives

- [localforage](https://npm.io/package/localforage.md) — 6.2M weekly downloads
- [localforage-observable](https://npm.io/package/localforage-observable.md) — 30.8K weekly downloads
- [@y/y](https://npm.io/package/@y/y.md) — 30.1K weekly downloads
- [@metaobjectsdev/render](https://npm.io/package/@metaobjectsdev/render.md) — 3.5K weekly downloads
- [@ledgerhq/coin-algorand](https://npm.io/package/@ledgerhq/coin-algorand.md) — 1.1K weekly downloads

## Recent versions

- 0.5.1 (latest) — 2021-10-08
- 0.4.3 — 2021-08-01
- 0.4.1 — 2021-08-01
- 0.3.2 — 2021-06-11
- 0.3.1 — 2021-06-11
- 0.3.0 — 2021-06-04
- 0.2.1 — 2021-06-03
- 0.2.0 — 2021-06-03
- 0.1.9 — 2021-06-03
- 0.1.8 — 2021-06-03
- 0.1.7 — 2021-06-03
- 0.1.6 — 2021-06-03
- 0.1.5 — 2021-06-03
- 0.1.4 — 2021-06-03
- 0.1.3 — 2021-06-03
- … 3 more at https://npm.io/package/js-database/versions

## README

# Js-database

## **library for simple indexeddb usage**

### **LIMITATION**

> [!WARNING]
> Firefox disable indexedDB in private mode, be careful.
> I hope, this will change soon


### **Let's start**

First what you need to do is install this library to yours project:

```bash
npm i js-database --save
```

### **Create model**

Next step create your first storage model. You can do it easily:

```javascript
import {BaseStorage} from "js-database";

export class TestStorage extends BaseStorage {
  static storage = new TestStorage()

  model = {
    name: "Test",
    params: {
      keyPath: "id",
    },
  };

  // this method calls on first storage initialization and when db version increments
  applyMigrations(objectStore: IDBObjectStore) {
    objectStore.createIndex("is_enabled", "enabled"); //you can create indexies
    this.setItem({}) //you can set default values to storage
  }
}
```

if you don't whant or your data haven't unique id to be used for keyPath you can add autoincrement option to yours
model:

```javascript
import {BaseStorage} from "js-database";

export class TestStorage extends BaseStorage {
  model = {
    name: "Test",
    params: {
      autoIncrement: true,
    },
  };
}
```

### **Initialize database (Vanila)**

After creating yours storage class, you need to initialize database like that:

```javascript
import {initDatabase} from "js-database";

initDatabase("TEST", 1, () => fireAfterInitialization(), TestStorage);
```

That's it! Now you can use "TestStorage" for storing data

Now let's talk how to work with our storage

### **Initialize database (react)**
For react users:

```javascript
import React, {useState} from "react";
import {useStorageInit} from "js-database";
import {ToDoStorage} from "./todo"

const Component = () => {
  const [isReady, setReady] = useState(false);

  useStorageInit("MyStorage", 1, () => {
    setReady(true);
  }, ToDoStorage);

  if (isReady) return <>Some code</>;

  return <></>;
};
```

### **Set data to storage**

Basic adding:

```javascript
TestStorage.storage.setItem({
  id: 1,
  data: {any: {type: {of: "data"}}},
});
```

> [!WARNING]
> setItem applyes object that corresponds to yours storage model - if you add keyPath to params it must be in object that you put to the setItem method.
> In this example this is property "id"

You can provide second argument for setItem to update particular item in storage:

```javascript
TestStorage.storage.setItem(
  {
    data: {any: {type: {of: "data"}}},
  },
  key
);
```

You can set list of items like that:

```javascript
TestStorage.storage.setItems(
  [
    {
      data: {any: {type: {of: "data"}}},
    },
    {
      data: {any: {type: {of: "data"}}},
    }
  ]
);
```

Method used for partial update of storage item:

```javascript
TestStorage.storage.partialUpdate(key, {fieldToUpdate: "value"});
```

Method used for partial update items that matches with query

```javascript
TestStorage.storage.partialUpdateByQuery({fieldToUpdate: "value"}, query, index);
```

Method used for partial update all items in storage

```javascript
TestStorage.storage.partialAllUpdate(
  {fieldToUpdate: "value"}
);
```

### **Get item**

Basic getter:

```javascript
TestStorage.storage
  .getItem(key)
  .then((item) => {
    setItem(item);
  })
  .catch((e) => {
    console.warn(e);
  });
```

Get all items as list:

```javascript
TestStorage.storage
  .getAllItems()
  .then((items) => {
    setList(items);
  })
  .catch((e) => {
    console.warn(e);
  });
```

Get all items as Map object:

```javascript
TestStorage.storage
  .getAllItemsWithKeys()
  .then((items) => {
    setList(items);
  })
  .catch((e) => {
    console.warn(e);
  });

```

Get items filtered by index:

```javascript
TestStorage.storage
  .getItemsByQuery(query, index) // index is optional
  .then((items) => {
    setList(items);
  })
  .catch((e) => {
    console.warn(e);
  });
```

You can create queries using Query class:

```javascript
import {Query} from "js-database";

Query.bound("A", "B", true, true)

Query.lowerBound("A", true)

Query.upperBound("B", true)

Query.only("value")

Query.many("value1", "value2")

```

for more information visit https://developer.mozilla.org/en-US/docs/Web/API/IDBKeyRange

### **Removing items**

Remove single item:

```javascript
TestStorage.storage.removeItem(id);
```

Remove items matching query:

```javascript
TestStorage.storage.removeItemByQuery(query, index) // index is optional
```

Remove all items:

```javascript
ToDoStorage.storage.removeAllItems();
```

### **Subscribe on changes**

You can subscribe on storage changes to provide actual state of yours app components

For React users:

```javascript
import React from "react";
import {useStorageEvent} from "js-database";
import {TestStorage} from "test-storage";

const Component = () => {
  const doSomeThingOnAddingNewItems = useCallback(() => {
    //do some operations
  }, []);

  const doSomeThingOnRemovingItems = useCallback(() => {
    //do some operations
  }, []);

  useStorageEvent(
    TestStorage,
    doSomeThingOnAddingNewItem,
    doSomeThingOnRemovingItems
  );

  return <></>;
};
```

For vanilla js:

```javascript
import {subscribeForChanges} from "js-database";
import {TestStorage} from "test-storage";

const doSomeThingOnAddingNewItems = () => {
  //do some operations
};

const doSomeThingOnRemovingItems = () => {
  //do some operations
};

subscribeForChanges(
  TestStorage,
  doSomeThingOnAddingNewItems,
  doSomeThingOnRemovingItems
);
```

### **Demo**

Little demo for more understanding
[view on codesandbox.io](https://codesandbox.io/s/js-database-demo-nc04n)

---
_Source: https://npm.io/package/js-database · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
