underscore.db
Advanced tools
Comparing version 0.6.0 to 0.6.1
{ | ||
"name": "underscore.db", | ||
"version": "0.6.0", | ||
"version": "0.6.1", | ||
"description": "Use JavaScript objects as databases", | ||
@@ -28,13 +28,13 @@ "main": "src/node.js", | ||
"devDependencies": { | ||
"underscore": "~1.5.2", | ||
"lodash": "~2.4.1", | ||
"grunt": "~0.4.2", | ||
"grunt-browserify": "~1.3.0", | ||
"grunt-cli": "~0.1.11", | ||
"grunt-contrib-jshint": "~0.7.2", | ||
"grunt-contrib-uglify": "~0.2.7", | ||
"grunt-contrib-jshint": "~0.7.2", | ||
"grunt-contrib-watch": "~0.5.3", | ||
"grunt-mocha-test": "~0.7.0", | ||
"grunt-contrib-watch": "~0.5.3", | ||
"grunt-cli": "~0.1.11", | ||
"grunt-browserify": "~1.3.0", | ||
"sinon": "~1.8.1" | ||
"lodash": "^2.4.1", | ||
"sinon": "~1.8.1", | ||
"underscore": "^1.6.0" | ||
} | ||
} |
110
README.md
@@ -1,29 +0,17 @@ | ||
[![Build Status](https://travis-ci.org/typicode/underscore.db.png)](https://travis-ci.org/typicode/underscore.db) | ||
[![NPM version](https://badge.fury.io/js/underscore.db.png)](http://badge.fury.io/js/underscore.db) | ||
[![NPM version](https://badge.fury.io/bo/underscore.db.png)](http://badge.fury.io/bo/underscore.db) | ||
# Underscore.db [![Build Status](https://travis-ci.org/typicode/underscore.db.svg)](https://travis-ci.org/typicode/underscore.db) [![NPM version](https://badge.fury.io/js/underscore.db.svg)](http://badge.fury.io/js/underscore.db) [![Bower version](https://badge.fury.io/bo/underscore.db.svg)](http://badge.fury.io/bo/underscore.db) | ||
# Underscore.db | ||
> Adds functions to Underscore/Lo-Dash for manipulating database-like objects. | ||
__Adds functions to Underscore/Lo-Dash for manipulating database-like objects.__ | ||
It adds `get`, `insert`, `update`, `updateWhere`, `remove`, `removeWhere`, `save`, `load` and `createId` and can be used in Node and the browser. | ||
It can be used in Node, node-webkit and the browser. | ||
Data can be persisted using the filesystem or localStorage. | ||
It adds `get`, `insert`, `update`, `updateWhere`, `remove`, `removeWhere`, `save`, `load` and `createId`. | ||
__[Live example](http://typicode.github.io/underscore.db/)__ | ||
## Example | ||
_For a full JSON database built on Lo-Dash and Underscore.db, check [LowDB](https://github.com/typicode/lowdb)._ | ||
```javascript | ||
db = {}; | ||
db.posts = [] | ||
## Install | ||
var id = _.insert(db.posts, {title: 'foo'}); | ||
var post = _.get(db.posts, id); | ||
__Node__ | ||
_.save(db); | ||
``` | ||
Or you can try it online [here](http://typicode.github.io/underscore.db/). | ||
## Install | ||
```bash | ||
@@ -38,2 +26,4 @@ $ npm install underscore underscore.db | ||
__Browser__ | ||
```bash | ||
@@ -48,17 +38,26 @@ $ bower install underscore underscore.db | ||
Underscore.db is compatible with Lo-Dash, just replace `underscore` with `lodash` | ||
To use Underscore.db with Lo-Dash, just replace `underscore` with `lodash` | ||
## API | ||
## Usage example | ||
Database example: | ||
Create an empty database object | ||
```javascript | ||
var db = { | ||
posts: [] | ||
} | ||
``` | ||
Create a post | ||
```javascript | ||
var newPost = _.insert(db.posts, {title: 'foo'}); | ||
``` | ||
Display database `console.log(db)` | ||
```javascript | ||
{ | ||
posts: [ | ||
{id: 1, body: 'one', published: false}, | ||
{id: 2, body: 'two', published: true} | ||
], | ||
comments: [ | ||
{id: 1, body: 'foo', postId: 1}, | ||
{id: 2, body: 'bar', postId: 2} | ||
{title: "foo", id: "5ca959c4-b5ab-4336-aa65-8a197b6dd9cb"} | ||
] | ||
@@ -68,19 +67,32 @@ } | ||
### id | ||
Retrieve post using underscore.db `get` or underscore `find` method | ||
Overwrite it if you want to use another id property. | ||
```javascript | ||
var post = _.get(db.posts, newPost.id); | ||
var post = _.find(db.posts, function(post) { | ||
return post.title === 'foo' | ||
}); | ||
``` | ||
Persist | ||
```javascript | ||
_.id = '_id'; | ||
_.save(db); | ||
``` | ||
### createId | ||
## API | ||
__createId(collectionName, doc)__ | ||
The following database object is used in API examples. | ||
Called by Underscore.db when a document is inserted. Overwrite it if you want to change id generation algorithm. | ||
```javascript | ||
_.createId = function(collectionName, doc) { | ||
return collectionName + '-' + doc.name + '-' + _.random(1, 9999); | ||
var db = { | ||
posts: [ | ||
{id: 1, body: 'one', published: false}, | ||
{id: 2, body: 'two', published: true} | ||
], | ||
comments: [ | ||
{id: 1, body: 'foo', postId: 1}, | ||
{id: 2, body: 'bar', postId: 2} | ||
] | ||
} | ||
@@ -158,2 +170,3 @@ ``` | ||
_.save(db); | ||
_.save(db, '/some/path/db.json'); | ||
``` | ||
@@ -169,4 +182,25 @@ | ||
var db = _.load(); | ||
var db = _.load('/some/path/db.json'); | ||
``` | ||
### id | ||
Overwrite it if you want to use another id property. | ||
```javascript | ||
_.id = '_id'; | ||
``` | ||
### createId | ||
__createId(collectionName, doc)__ | ||
Called by Underscore.db when a document is inserted. Overwrite it if you want to change id generation algorithm. | ||
```javascript | ||
_.createId = function(collectionName, doc) { | ||
return collectionName + '-' + doc.name + '-' + _.random(1, 9999); | ||
} | ||
``` | ||
## FAQ | ||
@@ -173,0 +207,0 @@ |
@@ -0,1 +1,3 @@ | ||
// Empty reference to _, | ||
// because it's not known yet it will be Underscore or Lo-Dash. | ||
var _; | ||
@@ -83,2 +85,2 @@ | ||
} | ||
}; | ||
}; |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
15201
230
246