streamdb
v0.1.7
Published
A Node.js document-oriented JSON DB & API development toolkit
Downloads
4
Maintainers
Readme
Key Features
- Generated ids (
incr
,uid
) - Timestamps (
created_at
,updated-at
) - Queries (includes
geo-search
) - Splits JSON store files, as data grows
- Uses Node Streams to literallly zoom through data
- Schema validation & settings
- Parent/subdocument refs
- Mongoose inspired syntax and modeling
- Built with Express Framework
- Simple CRUD starter routes
- Helper methods to help you customize routes faster
- Launch as standalone, or mount onto existing server
➤
Launch server with one line of code➤
Simple promise-based CRUD methods➤
Automatically creates router + model files
Table of Contents
- Usage
- Starter Routes
- Using Schema Models
- Launching Server
- ➥ Guide
- ➥ API Reference
- ➥ Examples
- Tests
- What's Next
- CHANGELOG
Usage
Install:
$ npm i streamdb
To use CLI without global install, prefix commands with npx
:
$ npx streamdb create --db sampleDB
Or, install a global copy as well:
$ npm i -g streamdb
Create DB:
In terminal:
$ streamdb create --db sampleDB
Or, run in file:
const streamdb = require('streamdb')
streamdb.createDb({ dbName: 'sampleDB' })
.then(res => console.log(res))
.catch(e => console.log(e))
Add Collections:
In terminal:
$ streamdb sampleDB --add users
Or, run in file:
const streamdb = require('streamdb')
const db = new streamdb.DB('sampleDB')
db.addCollection('users')
.then(res => console.log(res))
.catch(e => console.log(e))
Add Documents:
const streamdb = require('streamdb')
const db = new streamdb.DB('sampleDB')
const documents = [
{
firstname: 'Bugs',
lastname: 'Bunny',
email: '[email protected]'
},
{
firstname: 'Scooby',
lastname: 'Doo',
email: '[email protected]'
},
{
firstname: 'Tom',
lastname: 'Cat',
email: '[email protected]'
}
]
db.collection('users').insertMany(documents)
.then(res => console.log(res))
.catch(e => console.log(e))
Read Documents:
const streamdb = require('streamdb')
const db = new streamdb.DB('sampleDB')
db.collection('users').getById(1)
.then(res => console.log(res))
.catch(e => console.log(e))
// using queries:
// db.collection('users')
// .where('id = 1')
// .and('firstname = Bugs')
// .find()
// .then(..)
// Response object:
//{
// success: true,
// data: [
// {
// id: 1,
// firstname: 'Bugs',
// lastname: 'Bunny',
// email: '[email protected]'
// }
// ]
//}
Update Documents:
const streamdb = require('streamdb')
const db = new streamdb.DB('sampleDB')
const docUpdate = {
id: 1,
email: [email protected]
}
db.collection('users').updateOne(docUpdate)
.then(res => console.log(res))
.catch(e => console.log(e))
// using queries:
// db.collection('users')
// .where('id = 1')
// .setProperty('email', '[email protected]')
// .then(..)
// Response object:
//{
// success: true,
// message: 'Document 1 updated successfully'
// data: [
// {
// id: 1,
// firstname: 'Bugs',
// lastname: 'Bunny',
// email: '[email protected]'
// }
// ]
//}
Delete Documents:
const streamdb = require('streamdb')
const db = new streamdb.DB('sampleDB')
db.collection('users').deleteMany([2,3])
.then(res => console.log(res))
.catch(e => console.log(e))
// Response object:
//{
// success: true,
// message: '2 documents removed from "users" collection'
// data: [2,3]
//}
Starter Collection Routes
Creating new collections scaffolds a new Router file with the following routes you may edit/add to:
Example GET (all documents) route in template:
Using Schema Validation
Starter Schema Model Template:
Edit template if you wish to add validation and document settings:
// User Model
const streamdb = require('streamdb')
const Schema = streamdb.Schema
const User = new Schema({
id: streamdb.Types.$incr,
firstname: String,
lastname: String,
email: {
type: String,
required: true,
maxlength: 100
}
},
{
strict: false,
timestamps: {
created_at: true,
updated_at: true
}
})
module.exports = streamdb.model('User', User)
Launching/Using Server
const streamdb = require('streamdb')
const app = streamdb.server('sampleDB', 'api', 3000)
// open browser (or send GET query)..
// get all --> get(): http://localhost:3000/api/users
// get by id --> getById(1): http://localhost:3000/api/users/1
// sending POST request with JSON data in body..
// add many --> insertMany(docs): http://localhost:3000/api/users
// in POST body:
// [{
// "firstname": "john",
// "lastname": "smith",
// "email": "[email protected]"
// },
// {
// "firstname": "mary",
// "lastname": "jane",
// "email": "[email protected]"
// }]
Tests
Tests are implemented using the Jest Framework, and located in the __tests__ directory.
To run tests, fork/clone a copy of https://github.com/fabiantoth/streamdb.git locally, install all dev/dependencies and run:
$ npm test
Run coverage report:
$ npm run test-coverage
What's Next
- [x] ~~remove legacy 'default' workflow from codebase~~
- [x] ~~add populate() to chainQuery helper~~
- [ ] add 'unique' index schema option
- [ ] add text-search feature
- [ ] build demo w/FE Framework
- [ ] refactor cache to support multiple dbs
- [ ] add CI automation
Stability Notice
- streamDB is mainly for prototyping, do not use in production, or use sensitive/important data.
- Early v0.x.x updates may be breaking, experimental, or temporary (keep track of updates, CHANGELOG).