# @adiwajshing/keyed-db

> Lightweight library to store an in-memory DB

Latest version **0.2.4** (published 2020-12-24) · MIT license · 0 weekly downloads

## Install

```sh
npm install @adiwajshing/keyed-db
pnpm add @adiwajshing/keyed-db
yarn add @adiwajshing/keyed-db
bun add @adiwajshing/keyed-db
```

## Health

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

Positive: has types; no vulnerabilities; high quality score.

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

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.2.4 |
| Published | 2020-12-24 |
| First published | 2020-07-31 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 17.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 43 |
| Author | Adhiraj Singh |
| Maintainers | adiwajshing |
| Keywords | db, key |

## Links

- npm: https://www.npmjs.com/package/@adiwajshing/keyed-db
- Repository: https://github.com/adiwajshing/keyed-db
- Issues: https://github.com/adiwajshing/keyed-db/issues
- npm.io page: https://npm.io/package/@adiwajshing/keyed-db

## Alternatives

- [angular-pipes](https://npm.io/package/angular-pipes.md) — 5.6K weekly downloads
- [@ng-web-apis/midi](https://npm.io/package/@ng-web-apis/midi.md) — 2.6K weekly downloads
- [happn-3](https://npm.io/package/happn-3.md) — 1.6K weekly downloads
- [@opensip-cli/lang-go](https://npm.io/package/@opensip-cli/lang-go.md) — 1.2K weekly downloads
- [mongoose-typescript](https://npm.io/package/mongoose-typescript.md) — 85 weekly downloads

## Recent versions

- 0.2.4 (latest) — 2020-12-24
- 0.2.3 — 2020-12-18
- 0.2.2 — 2020-12-18
- 0.2.1 — 2020-12-18
- 0.2.0 — 2020-12-06
- 0.1.8 — 2020-10-04
- 0.1.7 — 2020-09-27
- 0.1.6 — 2020-09-26
- 0.1.5 — 2020-09-26
- 0.1.4 — 2020-09-07
- 0.1.3 — 2020-09-02
- 0.1.2 — 2020-08-17
- 0.1.1 — 2020-08-06
- 0.1.0 — 2020-07-31

## README

# Keyed DB

A light-weight node library to manage a sorted & indexed collection with pagination support. 
All done using Binary Search. Based off my swift code for [Queenfisher](https://github.com/adiwajshing/Queenfisher)

## Install

`npm i github:adiwajshing/keyed-db`

## Running Tests

`npm test`

## Functions

``` ts

db = new KeyedDB<T> (t => t.uniqueNumberKeyProperty, t => t.optionalUniqueIDProperty)
// compare with a custom function
db = new KeyedDB<T> ({  
    key: t => t.someProperty,
    compare: (t1, t2) => someComputation(t1, t2) // return -1 if t1 < t2, 0 if t1=t2 & 1 if t1 > t2
}, t => t.optionalUniqueIDProperty)

db.insert (value) // insert value in DB
db.upsert (value) // upserts value
db.insertIfAbsent (value) // only inserts if not already present in DB
db.delete (value) // delete value
db.deleteById (value.optionalUniqueIDProperty) // delete value by referencing the ID
// update the key of a value, 
// will automatically place object after key change
db.updateKey (value, value => value.uniqueKeyProperty = newValue) 
db.paginated (someCursor, 20) // get X results after the given cursor (null for the first X results)

```

## Usage

``` ts
import KeyedDB from '@adiwajshing/keyed-db'

// Let's use the db to sort & maintain a list of chats
// Chats must be accessed quickly via the chatID (the person you're chatting with)
// Chats must be sorted by recency
type Chat = {
    timestamp: Date
    chatID: string
}

// first argument -- sorting property, second argument -- ID property
const db = new KeyedDB<Chat>(value => value.timestamp.getTime()*-1, value => value.chatID)

for (let i = 0; i < 1000;i++) {
    // insert data
    db.insert (
        {
            timestamp: new Date( new Date().getTime() - Math.random()*10000 ), 
            chatID: `person ${i}`
        }
    )
}
console.log (db.all()) // return internal sorted array
console.log (db.paginated(null, 20)) // return first 20 chats
console.log (db.paginated(null, 20, null, 'before')) // return last 20 chats
console.log (db.paginated(null, 20, chat => chat.chatID.includes('something'))) // return first 20 chats where the chatID contains 'something'

const someDate = new Date().getTime()
const cursorPaginated = db.paginated(someDate, 20)
console.log (cursorPaginated) // return 20 chats after the specified date

db.delete (cursorPaginated[0]) // delete paginated chats 

// update chat timestamp
db.updateKey(cursorPaginated[1], value => value.timestamp = new Date().getTime()) 

```

## Time Complexity

| Operation      | Time Complexity |
|----------------|-----------------|
| db.insert()    | O(logN)         |
| db.delete()    | O(logN)         |
| db.get()       | O(1)            |
| db.updateKey() | O(logN)         |

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