https://github.com/will123195/levelgo
Indexed collections for LevelDB (inspired by MongoDB)
https://github.com/will123195/levelgo
Last synced: 8 months ago
JSON representation
Indexed collections for LevelDB (inspired by MongoDB)
- Host: GitHub
- URL: https://github.com/will123195/levelgo
- Owner: will123195
- Created: 2020-04-19T08:01:42.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2020-05-19T21:24:00.000Z (over 5 years ago)
- Last Synced: 2024-10-06T08:41:44.373Z (about 1 year ago)
- Language: JavaScript
- Homepage:
- Size: 104 KB
- Stars: 2
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# levelgo
Indexed collections for LevelDB (inspired by MongoDB)
[](https://travis-ci.org/will123195/levelgo)
## Install
```
npm i levelgo
```
## Example
```js
import levelgo from 'levelgo'
const db = levelgo('example-db')
db.collection('books')
db.books.registerIndex({ author: 1 })
await db.books.put('book1', {
author: 'Hemingway',
title: 'Islands in the Stream',
year: 1970
})
const book = await db.books.get('book1')
const books = await db.books.find({ author: 'Hemingway' })
```
## API
#### db = levelgo( location )
- `location` {String} path of the LevelDB location to be opened, or in browsers, the name of the IDBDatabase to be opened
#### db.collection( name )
- `name` {String} name of the collection to initialize
## Collection methods
#### db.*name*.del( id )
- `id` {String|Number} primary key of the value to delete
#### db.*name*.find( [query] )
- `query` {Object} optional selection filter. An index with the same fields must be registered. If blank or empty object, returns all values in the collection.
- Mongo-style comparison query operators are available:
- `$gt`
- `$lt`
- `$gte`
- `$lte`
- `$in`
- `$nin`
- `$eq`
- `$ne`
- Note: `null`, `undefined` and "empty string" values are indexed the same as "blank" values.
#### db.*name*.findKeys( [query] )
- `query` {Object} optional selection filter. An index with the same fields must be registered. If blank or empty object, returns all keys (ids) in the collection.
#### db.*name*.get( id )
- `id` {String|Number} primary key of the value to retrieve
#### db.*name*.put( id, value )
- `id` {String|Number} primary key of the value to store
- `value` {mixed} any stringify-able value to store in the collection
#### db.*name*.registerIndex( fields )
- `fields` {Object} fields to be indexed. Always set the value of each field to `1` since only ascending indices are currently supported.
### Atomic Batch
#### `batch = db.batch()`
#### batch.*name*.del( id )
#### batch.*name*.put( id, value )
#### `batch.write()`
## Advanced Example
```js
import levelgo from 'levelgo'
const db = levelgo('example-db')
db.collection('books')
// you can have compound nested indices
db.books.registerIndex({
tags: 1,
'reviews.stars': 1
})
// batch operations are written atomically
const batch = db.batch()
batch.books.del('book1')
batch.books.put('book2', {
author: 'Hemingway',
title: 'Islands in the Stream',
year: 1970,
reviews: [
{ stars: 5, username: 'taylor' },
{ stars: 4, username: 'river' },
],
tags: ['classic']
})
await batch.write()
// find books that are tagged 'classic' that have at least one review of 4+ stars
const books = await db.books.find({
'reviews.stars': { $gte: 4 },
tags: 'classic'
})
```