Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/varharrie/persiston

Simple persistent store with database-like API.
https://github.com/varharrie/persiston

database deno storage

Last synced: about 5 hours ago
JSON representation

Simple persistent store with database-like API.

Awesome Lists containing this project

README

        

# Persiston for Deno

Simple persistent store with database-like API.

## Usage

```javascript
const adapter = new FileAdapter('./data.json')
const store = new Persiston({ adapter })

store.load()
.then(() => store.collection('users').insert({ name: 'foo' }))
.then(() => store.collection('users').findOne())
.then((user) => console.log(user)) // { name: 'foo' }
```

With type declaration:

```typescript
interface User {
name: string
}

class Store extends Persiston {
users = this.collection('users')
pets = this.collection('pets')
}

const adapter = new FileAdapter('./data.json')
const store = new Store({ adapter })

store.load()
.then(() => store.collection('users').insert({ name: 'foo' }))
.then(() => store.collection('users').findOne())
.then((user) => console.log(user)) // { name: 'foo' }
```

## APIs

### Persiston

- `store.load(): Promise`

Loads data by adapter. It should be called before all collection operations.

- `store.save(): Promise`

Saves data by adapter. You probably won't call it by yourself.

- `store.collection(): Collection`

Gets a collection object.

### Collection

- `collection.find(query?: Query, fields?: string[]): Promise`

Finds items by query.

- `collection.findOne(query?: Query, fields?: string[]): Promise`

Finds an item by query.

- `collection.insert(items: T | T[]): Promise`

Saves given item or items.

- `collection.update(query: Query, changes: Partial): Promise`

Partially updates by query.

- `collection.updateOne(query: Query, changes: Partial): Promise`

Partially updates an item by query.

- `collection.remove(query?: Query): Promise`

Removes items by query.

- `collection.removeOne(query: Query): Promise`

Removes an item by query.