A Node.js document-oriented JSON DB & API development toolkit
npm install streamdb
Features |
Usage |
Guide |
API
Designed for front-end development so you can just focus on building your awesome application.
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
---
➤ 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
$3
`sh
$ npm i streamdb
`
To use CLI without global install, prefix commands with npx:
`sh
$ npx streamdb create --db sampleDB
`
Or, install a global copy as well:
`sh
$ npm i -g streamdb
`
$3
In terminal:
`sh
$ streamdb create --db sampleDB
`
Or, run in file:
`js
const streamdb = require('streamdb')
streamdb.createDb({ dbName: 'sampleDB' })
.then(res => console.log(res))
.catch(e => console.log(e))
`
$3
In terminal:
`sh
$ streamdb sampleDB --add users
`
Or, run in file:
`js
const streamdb = require('streamdb')
const db = new streamdb.DB('sampleDB')
db.addCollection('users')
.then(res => console.log(res))
.catch(e => console.log(e))
`
$3
`js
const streamdb = require('streamdb')
const db = new streamdb.DB('sampleDB')
const documents = [
{
firstname: 'Bugs',
lastname: 'Bunny',
email: 'bbunny@email.com'
},
{
firstname: 'Scooby',
lastname: 'Doo',
email: 'sdoo@email.com'
},
{
firstname: 'Tom',
lastname: 'Cat',
email: 'tcat@email.com'
}
]
db.collection('users').insertMany(documents)
.then(res => console.log(res))
.catch(e => console.log(e))
`
$3
`js
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: 'bbunny@email.com'
// }
// ]
//}
`
$3
`js
const streamdb = require('streamdb')
const db = new streamdb.DB('sampleDB')
const docUpdate = {
id: 1,
email: b-bunny@email.com
}
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', 'b-bunny@email.com')
// .then(..)
// Response object:
//{
// success: true,
// message: 'Document 1 updated successfully'
// data: [
// {
// id: 1,
// firstname: 'Bugs',
// lastname: 'Bunny',
// email: 'b-bunny@email.com'
// }
// ]
//}
`
$3
`js
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:
Request
Route
Description
-
Method
GET
/api/collection
Get all docs
get()
GET
/api/collection/:id
Get by id
getById()
GET
/api/collection/_q/
Run compound queries
helper_methods
POST
/api/collection
Insert many docs
insertMany()
PUT
/api/collection
Update many docs
updateMany()
PUT
/api/collection/_q/
Run update queries
helper_methods
DELETE
/api/collection/:id
Delete by id
deleteOne()
Example GET (all documents) route in template:
-------------------------------------------------------------
Using Schema Validation
$3
Edit template if you wish to add validation and document settings:
`js
// 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
`js
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": "jsmith@email.com"
// },
// {
// "firstname": "mary",
// "lastname": "jane",
// "email": "mj@email.com"
// }]
`
▲ back to top
-------------------------------------------------------------
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:
`sh
$ npm test
`
Run coverage report:
`sh
$ npm run test-coverage
``