提交 b383d094 编写于 作者: T Typicode

First commit

上级
node_modules
lib
\ No newline at end of file
src
\ No newline at end of file
language: node_js
node_js:
- "0.11"
- "0.10"
- "0.8"
\ No newline at end of file
The MIT License (MIT)
Copyright (c) 2014 typicode
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
# low
[![NPM version](https://badge.fury.io/js/lowdb.png)](http://badge.fury.io/js/lowdb)
## Example
```bash
$ npm i lowdb --save
```
```javascript
var low = require('lowdb');
// insert a song
low('songs').insert({title: 'low!'});
// find all songs named 'low!'
var songs = low('songs').where({title: 'low!'}).value();
```
Database is saved to `db.json`.
## API
See [Underscore.db](https://github.com/typicode/underscore.db) and [Lo-Dash](http://lodash.com/docs).
To run benchmark:
```bash
$ grunt build
$ cd benchmark
$ node index.js
```
\ No newline at end of file
var b = require('b'),
low = require('../lib'),
song;
b('insert').run(1000, function() {
low('songs').insert({title: 'low!'});
});
song = low.db.songs[500];
b('get').run(1000, function() {
low('songs').get(song.id);
});
b('update').run(1000, function() {
low('songs').update(song.id, {title: '!wol'});
});
songs = low.db.songs;
b('remove').run(1000, function() {
low.db.songs = songs;
low('songs').remove(song.id);
});
console.log();
\ No newline at end of file
module.exports = (grunt) ->
grunt.initConfig
clean: ['lib']
coffee:
compile:
expand: true
cwd: 'src'
src: ['**/*.coffee']
dest: 'lib/'
ext: '.js'
watch:
coffee:
files: '{src,test}/**/*.coffee'
tasks: ['coffeelint', 'mochaTest']
options:
atBegin: true
coffeelint:
files: '{src,test}/**/*.coffee'
mochaTest:
test:
src: ['test/**/*.coffee']
grunt.loadNpmTasks 'grunt-contrib-clean'
grunt.loadNpmTasks 'grunt-contrib-coffee'
grunt.loadNpmTasks 'grunt-contrib-watch'
grunt.loadNpmTasks 'grunt-coffeelint'
grunt.loadNpmTasks 'grunt-mocha-test'
grunt.registerTask 'default', 'watch'
grunt.registerTask 'build', ['clean', 'coffee']
\ No newline at end of file
{
"name": "lowdb",
"version": "0.1.0",
"description": "JSON database",
"keywords": [
"flat",
"file",
"database",
"JSON",
"lo-dash",
"lodash",
"underscore",
"underscore.db",
"embed",
"embeddable"
],
"main": "lib/index.js",
"scripts": {
"test": "grunt mochaTest",
"prepublish": "grunt build"
},
"repository": {
"type": "git",
"url": "git://github.com/typicode/lowdb.git"
},
"author": "Typicode <typicode@gmail.com>",
"license": "MIT",
"bugs": {
"url": "https://github.com/typicode/lowdb/issues"
},
"homepage": "https://github.com/typicode/lowdb",
"dependencies": {
"underscore.db": "~0.6.0",
"lodash": "~2.4.1"
},
"devDependencies": {
"sinon": "~1.8.2",
"b": "^2.0.1",
"grunt": "^0.4.3",
"grunt-coffeelint": "0.0.8",
"grunt-mocha-test": "^0.9.4",
"grunt-contrib-clean": "^0.5.0",
"grunt-contrib-coffee": "^0.10.1",
"grunt-contrib-watch": "^0.5.3"
}
}
EventEmitter = require('events').EventEmitter
util = require 'util'
_ = require 'lodash'
underdb = require 'underscore.db'
underdb.mixWith _
#
# Event emitter
#
ee = new EventEmitter()
#
# Lo-Dash
#
_.mixin
get: _.wrap _.get, (get, coll, id) ->
obj = coll._index?[id]
obj or= get coll, id
obj
insert: _.wrap _.insert, (insert, coll, doc) ->
insert coll, doc
ee.emit 'add', doc, coll
doc
update: _.wrap _.update, (update, coll, id, attrs) ->
doc = update coll, id, attrs
ee.emit 'update', doc, coll if doc
doc
updateWhere: _.wrap _.updateWhere, (updateWhere, coll, whereAttrs, attrs) ->
docs = updateWhere coll, whereAttrs, attrs
ee.emit 'update', docs, coll if docs.length > 0
docs
remove: _.wrap _.remove, (remove, coll, id) ->
doc = remove(coll, id)
ee.emit 'remove', doc, coll if doc
doc
removeWhere: _.wrap _.removeWhere, (removeWhere, coll, whereAttrs) ->
docs = removeWhere(coll, whereAttrs)
ee.emit 'remove', docs, coll if docs.length > 0
docs
#
# Low
#
low = (str, arg1, arg2) ->
low.db[str] or= []
chain = _ low.db[str]
if arg2
if arg2 is 1 # insert
return chain.insert(arg1).value()
if arg2 is -1 # remove or removeWhere
if _.isString arg1 #remove
return chain.remove(arg1).value()
if _.isObject arg1 #removeWhere
return chain.removeWhere(arg1).value()
if _.isObject arg2 # updateWhere
return chain.updateWhere(arg1, arg2).value()
if arg1
if _.isString arg1 # get
return chain.get(arg1).value()
if _.isObject arg1 # where
return chain.where(arg1).value()
chain
low.db = {}
low.path = 'db.json'
low.save = (path = low.path) ->
underdb.save low.db, path
low.load = (path = low.path) ->
low.db = underdb.load path
low.on = (event, listener) ->
ee.on event, listener
#
# Expose _
#
low._ = _
#
# Listen to events
#
low.on 'add' , -> low.save()
low.on 'update' , -> low.save()
low.on 'remove' , -> low.save()
#
# Indexing
#
low.on 'add', (obj, coll) ->
coll._index or= {}
coll._index[obj.id] = obj
low.on 'remove', (obj, coll) ->
delete coll._index[obj.id] if coll._index
# For testing purpose
low.ee = ee
#
# Export
#
module.exports = low
\ No newline at end of file
fs = require 'fs'
assert = require 'assert'
sinon = require 'sinon'
low = require '../src'
sinon.spy low.ee, 'emit'
sinon.stub fs, 'writeFileSync'
sinon.stub(fs, 'readFileSync').returns('{}')
insertSong = ->
low('songs').insert(title: 'foo').value()
describe 'low', ->
beforeEach ->
low.db = {}
low.ee.emit.reset()
it 'create', ->
assert.deepEqual low('songs').value(), []
assert.deepEqual low.db, songs: []
it 'insert', ->
song = insertSong()
assert low.ee.emit.calledWith('add', song, low('songs').value())
assert low('songs').size(), 1
it 'get', ->
song = insertSong()
assert low('songs').get song.id
it 'update', ->
song = insertSong()
low('songs').update song.id, title: 'bar'
assert low.ee.emit.calledWith('update', song, low('songs').value())
low.ee.emit.reset()
low('songs').update 9999, {}
assert not low.ee.emit.called
it 'updateWhere', ->
song = insertSong()
low('songs').updateWhere title: 'foo', {}
assert low.ee.emit.calledWith('update', [song], low('songs').value())
low.ee.emit.reset()
low('songs').updateWhere title: 'qux', {}
assert not low.ee.emit.called
it 'remove', ->
song = insertSong()
low('songs').remove song.id
assert low.ee.emit.calledWith('remove', song, low('songs').value())
assert.equal low('songs').size(), 0
low.ee.emit.reset()
low('songs').remove 9999, {}
assert not low.ee.emit.called
it 'removeWhere', ->
song = insertSong()
low('songs').removeWhere title: 'foo'
assert low.ee.emit.calledWith('remove', [song], low('songs').value())
assert.equal low('songs').size(), 0
low.ee.emit.reset()
low('songs').removeWhere title: 'qux'
assert not low.ee.emit.called
it 'load', ->
low.load()
assert fs.readFileSync.calledWith('db.json')
low.path = 'foo.json'
low.load()
assert fs.readFileSync.calledWith('foo.json')
low.load('bar.json')
assert fs.readFileSync.calledWith('bar.json')
it 'save', ->
insertSong()
assert fs.writeFileSync.calledWith('db.json')
low.save()
assert fs.writeFileSync.calledWith('db.json')
low.path = 'foo.json'
insertSong()
assert fs.writeFileSync.calledWith('foo.json')
low.save()
assert fs.writeFileSync.calledWith('foo.json')
low.save 'bar.json'
assert fs.writeFileSync.calledWith('bar.json')
it 'index', ->
song = insertSong()
assert.equal low.db['songs'][0].title, 'foo'
assert.equal low.db['songs']._index[song.id].title, 'foo'
assert.equal low('songs').get(song.id).value().title, 'foo'
low('songs').update song.id, title: 'bar'
assert.equal low.db['songs'][0].title, 'bar'
assert.equal low.db['songs']._index[song.id].title, 'bar'
assert.equal low('songs').get(song.id).value().title, 'bar'
low('songs').remove song.id
assert.equal low.db['songs'].length, 0
assert.equal Object.keys(low.db['songs']._index).length, 0
describe 'short syntax', ->
beforeEach ->
low.db = {}
@song = insertSong()
it 'get', ->
assert.deepEqual low('songs', @song.id),
low('songs').get(@song.id).value()
it 'where', ->
assert.deepEqual low('songs', title: 'foo'),
low('songs').where(title: 'foo').value()
it 'remove', ->
assert.deepEqual low('songs', @song.id, -1), @song
assert.equal low('songs').size(), 0
it 'create', ->
newSong = low 'songs', title: 'bar', 1
assert.deepEqual low('songs').get(newSong.id).value(), newSong
assert.equal low('songs').size(), 2
it 'updateWhere', ->
low 'songs', {title: 'foo'}, {title: 'bar'}
assert low('songs').find title: 'bar'
it 'removeWhere', ->
low 'songs', title: 'foo', -1
assert.equal low('songs').size(), 0
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册