marchio-core-record
v0.1.3
Published
REST response record tool
Downloads
1
Readme
marchio-core-record
REST response record tool
Installation
$ npm init
$ npm install marchio-core-record --save
Modules
marchio-core-record
Module
- marchio-core-record
- .package()
- .build(body) ⇒ Promise
- .buildUpdate(body) ⇒ Promise
- .select(body) ⇒ Promise
- .selectedFields() ⇒ Promise
- .fields(fields, body) ⇒ Promise
marchio-core-record.package()
Returns the package name
Kind: instance method of marchio-core-record
marchio-core-record.build(body) ⇒ Promise
Build a record based on the model and an input record containing original values
Kind: instance method of marchio-core-record
Returns: Promise - that resolves to a record object containing the resulting record
| Param | Type | Description | | --- | --- | --- | | body | Object | An object where properties represent fields from a response (like req.response) |
Example (Usage Example)
var factory = require("marchio-core-record");
var modelName = 'coretest';
var model = {
name: modelName,
fields: {
email: { type: String, required: true },
status: { type: String, required: true, default: "NEW" },
// In a real world example, password would be hashed by middleware before being saved
password: { type: String, select: false }, // select: false, exclude from query results
}
};
// normally this would come from an http method handler
var req = {
body: {
email: "[email protected]"
}
};
factory.create({ model: model })
.then( (rm) => rm.build( req.body )
.then( (record) => {
console.log("record: ", record );
})
.catch( function(err) {
console.error(err);
});
marchio-core-record.buildUpdate(body) ⇒ Promise
Build an update record based on the model and an input record containing original values
Kind: instance method of marchio-core-record
Returns: Promise - that resolves to a record object containing the resulting record
| Param | Type | Description | | --- | --- | --- | | body | Object | An object where properties represent fields from a response (like req.response) |
Example (Usage Example)
var factory = require("marchio-core-record");
var modelName = 'coretest';
var model = {
name: modelName,
fields: {
email: { type: String, required: true },
status: { type: String, required: true, default: "NEW" },
// In a real world example, password would be hashed by middleware before being saved
password: { type: String, select: false }, // select: false, exclude from query results
}
};
// normally this would come from an http method handler
var req = {
body: {
email: "[email protected]"
}
};
factory.create({ model: model })
.then( (rm) => rm.buildUpdate( req.body )
.then( (record) => {
console.log("record: ", record );
})
.catch( function(err) {
console.error(err);
});
marchio-core-record.select(body) ⇒ Promise
Build a record based on the selected fields in a model and a record containing original values
Kind: instance method of marchio-core-record
Returns: Promise - that resolves to a record object containing the resulting record
| Param | Type | Description | | --- | --- | --- | | body | Object | An object where properties represent fields from a response (like req.response) |
Example (Usage Example)
var factory = require("marchio-core-record");
var modelName = 'coretest';
var model = {
name: modelName,
fields: {
email: { type: String, required: true },
status: { type: String, required: true, default: "NEW" },
// In a real world example, password would be hashed by middleware before being saved
password: { type: String, select: false }, // select: false, exclude from query results
}
};
// normally this would come from an http method handler
var req = {
body: {
email: "[email protected]"
}
};
var recMgr = null;
factory.create({ model: model })
.then( (rm) => {
recMgr = rm;
return rm.build( { req.body );
})
.then( (record) => recMgr.select( record ) )
.then( (response) => {
console.log( response );
})
.catch( function(err) {
console.error(err);
});
marchio-core-record.selectedFields() ⇒ Promise
Return a list containing the names of fields that are selected
Kind: instance method of marchio-core-record
Returns: Promise - that resolves to a list of selected field names
Example (Usage Example)
var factory = require("marchio-core-record");
var modelName = 'coretest';
var model = {
name: modelName,
fields: {
email: { type: String, required: true },
status: { type: String, required: true, default: "NEW" },
// In a real world example, password would be hashed by middleware before being saved
password: { type: String, select: false }, // select: false, exclude from query results
}
};
// normally this would come from an http method handler
var req = {
body: {
email: "[email protected]"
}
};
var recMgr = null;
factory.create({ model: model })
.then( (recMgr) => {
recMgr = rm;
return recMgr.selectedFields();
})
.then( (response) => {
console.log( response );
})
.catch( function(err) {
console.error(err);
});
marchio-core-record.fields(fields, body) ⇒ Promise
Build a record including all fields a list and a record containing original values
Kind: instance method of marchio-core-record
Returns: Promise - that resolves to a record object containing the resulting record
| Param | Type | Description | | --- | --- | --- | | fields | Object | An object where property values are objects defininig fields | | body | Object | An object where properties represent fields from a response (like req.response) |
Example (Usage Example)
var factory = require("marchio-core-record");
var modelName = 'coretest';
var model = {
name: modelName,
fields: {
email: { type: String, required: true },
status: { type: String, required: true, default: "NEW" },
// In a real world example, password would be hashed by middleware before being saved
password: { type: String, select: false }, // select: false, exclude from query results
}
};
// normally this would come from an http method handler
var req = {
body: {
email: "[email protected]"
}
};
const fList = ["email", "status", "password"];
var recMgr = null;
factory.create({ model: model })
.then( (rm) => {
recMgr = rm;
return rm.build( { req.body );
})
.then( (record) => recMgr.fields( fList, record ) )
.then( (response) => {
console.log( response );
})
.catch( function(err) {
console.error(err);
});
marchio-core-record-factory
Factory module
marchio-core-record-factory.create(spec) ⇒ Promise
Factory method It takes one spec parameter that must be an object with named parameters
Kind: static method of marchio-core-record-factory
Returns: Promise - that resolves to {module:marchio-core-record}
| Param | Type | Description | | --- | --- | --- | | spec | Object | Named parameters object | | spec.model | Object | Model object that must contain a fields property |
Example (Usage example)
var factory = require("marchio-core-record");
var modelName = 'coretest';
var model = {
name: modelName,
fields: {
email: { type: String, required: true },
status: { type: String, required: true, default: "NEW" },
// In a real world example, password would be hashed by middleware before being saved
password: { type: String, select: false }, // select: false, exclude from query results
}
};
factory.create({ model: model })
.then(function(obj) {
return obj.health();
})
.catch( function(err) {
console.error(err);
});
Testing
To test, go to the root folder and type (sans $):
$ npm test
Repo(s)
Contributing
In lieu of a formal style guide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code.
Version History
Version 0.1.3
- added buildUpdate method
Version 0.1.2
- added selectedFields method
Version 0.1.1
- updated documentation
Version 0.1.0
- initial release