Skip to content

Commit

Permalink
add create API's for resources and results (#43)
Browse files Browse the repository at this point in the history
* add create API's for resources and results
* add document api to overwrite all whole document
* add one test
  • Loading branch information
Jussi Vatjus-Anttila authored Aug 21, 2018
1 parent 8823f45 commit a6d159c
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 4 deletions.
13 changes: 12 additions & 1 deletion src/resources.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ const _ = require('lodash');

// application modules
const Resource = require('./resource');
const {QueryBase, Collection, notImplemented} = require('./utils');
const {
QueryBase, Collection, Document, notImplemented
} = require('./utils');

/**
* @class ResourcesQuery
Expand Down Expand Up @@ -151,6 +153,15 @@ class Resources extends Collection {
update() {
return this._notImplemented();
}

/**
* Create new Resource
* @return {Resource} returns new result
*/
create() {
const NewResource = Document.IsNewDocument(Resource);
return new NewResource(this._transport);
}
}

module.exports = Resources;
7 changes: 5 additions & 2 deletions src/results.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,10 +151,13 @@ class Results extends Collection {
return this._notImplemented('results update is not implemented');
}

/**
* Create new Result
* @return {Result} returns new result
*/
create() {
const NewResult = Document.IsNewDocument(Result);
const doc = new NewResult(this._transport);
return doc;
return new NewResult(this._transport);
}
}

Expand Down
16 changes: 16 additions & 0 deletions src/utils/rest/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,22 @@ class Document extends Base {
}
return this.set(key, value);
}
/**
* Overwrite document with new allowedValue
* @param {Object} values - new data
* @return {Document} document object
*/
overwrite(values) {
if (_.isEqual(this._resource, values)) {
return this;
}
if (this.isNew) {
this._resource = values;
} else {
this._changes = values;
}
return this;
}
/**
* getter for Document version
* @return {number} returns document version number
Expand Down
12 changes: 11 additions & 1 deletion test/api/resource.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const assert = require('assert');
const moxios = require('moxios');

const {Transport, Resource} = require('../../src');
const {Transport, Resource, Resources} = require('../../src');

describe('Resource', function () {
let transport;
Expand All @@ -15,6 +15,16 @@ describe('Resource', function () {
// import and pass your custom axios instance to this method
moxios.uninstall();
});
it('create new resource', function () {
const resourceJson = {_id: '123', name: 'X', __v: 0};
const resources = new Resources(transport);
const res = resources.create();
assert.equal(res.isNew, true);
res.overwrite(resourceJson);
assert.equal(res.name(), 'X');
assert.equal(res.id, '123');
assert.equal(res.isDirty(), true);
});
it('constructing with valid json', function () {
const resourceJson = {_id: '123', name: 'X', __v: 0};
const res = new Resource(transport, resourceJson);
Expand Down

0 comments on commit a6d159c

Please sign in to comment.