Comparing version 0.0.1 to 0.0.2
32
index.js
@@ -31,6 +31,34 @@ var _ = require('lodash'), | ||
get: function (id) { | ||
return dsds.records[id] || null; | ||
get: function (id, property) { | ||
var rec = dsds.records[id] || null; | ||
if (property && rec) { | ||
return rec[property] || null; | ||
} | ||
return rec; | ||
}, | ||
set: function (id, property, value) { | ||
if (!dsds.records[id]) { return false; } | ||
if (typeof value === 'undefined') { | ||
value = property; | ||
property = null; | ||
} | ||
if (property) { | ||
dsds.records[id][property] = value; | ||
if (dsds.persistence && dsds.autoPersist) { | ||
dsds.persist(); | ||
} | ||
return dsds.records[id][property]; | ||
} | ||
return dsds.replace(id, value); | ||
}, | ||
all: function () { | ||
@@ -37,0 +65,0 @@ return _.values(dsds.records); |
{ | ||
"name": "dsds", | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"description": "Dead simple data store. In-memory with persistence to file.", | ||
@@ -5,0 +5,0 @@ "repository": { |
@@ -54,10 +54,26 @@ dsds | ||
### `store.get(id)` | ||
### `store.get(id [, property])` | ||
Returns the object with the ID `id`. | ||
If a `property` argument is passed, the corresponding property value of | ||
the record is returned. | ||
```javascript | ||
var ryu = store.get('ce4c05cd-00fc-410d-9f17-209b543e232f'); | ||
var ryuHeight = store.get('ce4c05cd-00fc-410d-9f17-209b543e232f', 'height'); | ||
``` | ||
### `store.set(id [, property], value)` | ||
If a `property` argument is passed this sets the property `property` of | ||
the record with ID `id` and returns the new property value. | ||
If the `property` argument is omitted, this will behave exactly like | ||
`store.replace()` | ||
```javascript | ||
var newHeight = store.set('ce4c05cd-00fc-410d-9f17-209b543e232f', 'height', 190); | ||
``` | ||
### `store.all()` | ||
@@ -64,0 +80,0 @@ |
@@ -68,2 +68,30 @@ var expect = require('chai').expect, | ||
describe('#get', function () { | ||
it('should get a record by ID', function () { | ||
var chunLi = fighters.get(sampleIDs[3]); | ||
expect(chunLi).to.have.property('id', sampleIDs[3]); | ||
expect(chunLi).to.have.property('name', sampleData[3].name); | ||
expect(chunLi).to.have.property('gender', sampleData[3].gender); | ||
expect(chunLi).to.have.property('country', sampleData[3].country); | ||
expect(chunLi).to.have.property('height', sampleData[3].height); | ||
}); | ||
it('should get a single property by ID and property name', function () { | ||
var chunLiHeight = fighters.get(sampleIDs[3], 'height'); | ||
expect(chunLiHeight).to.equal(sampleData[3].height); | ||
}); | ||
}); | ||
describe('#set', function () { | ||
it('should set a single property by ID and property name', function () { | ||
var newHeight = fighters.set(sampleIDs[3], 'height', 150); | ||
expect(newHeight).to.equal(150); | ||
expect(fighters.get(sampleIDs[3], 'height')).to.equal(newHeight); | ||
}); | ||
}); | ||
describe('#update', function () { | ||
@@ -70,0 +98,0 @@ |
14953
9
260
201