diff --git a/README.md b/README.md index 536ee0e..70e2cff 100644 --- a/README.md +++ b/README.md @@ -614,3 +614,40 @@ Dropping the schema will remove all indexes and constraints created by Neode. A instance.schema.drop() .then(() => console.log('Schema dropped!')) ``` + +## Serialization +You can use the asynchrone function toJson() to serialize any node or relation obtained with a query. + +### Basic use +```javascript +instance.findById('Person', 1) + .then(person => return person.toJson()) + .then(jsonPerson => console.log(jsonPerson)); +``` + +### Hide elements +You can choose to hide any property during serialization by using the "hidden" property in your scheme. It is also possible to control advanced use cases by passing a serialization group to the toJson() function. + +```javascript +// models/Person.js +module.exports = { + id: { + type: 'uuid', + primary: true + }, + propertyToHide: { + type: 'string', + hidden: true + }, + propertyOnlyForAdmin: { + type: 'string', + hidden: ['anonymous_user', 'basic_user'] + } +} +``` + +```javascript +instance.findById('Person', 1) + .then(person => return person.toJson('anonymous_user')) + .then(jsonPerson => console.log(jsonPerson)); // propertyToHide and propertyOnlyForAdmin properties will not be visible +``` diff --git a/src/Entity.js b/src/Entity.js index e3aaf98..8e5e459 100644 --- a/src/Entity.js +++ b/src/Entity.js @@ -88,7 +88,7 @@ export default class Entity { const model = this._model || this._definition; model.properties().forEach((property, key) => { - if ( !property.hidden() && this._properties.has(key) ) { + if ( this._properties.has(key) ) { output[ key ] = this.valueToJson(property, this._properties.get( key )); } }); @@ -126,4 +126,4 @@ export default class Entity { valueToJson(property, value) { return valueToJson(property, value); } -} \ No newline at end of file +} diff --git a/src/Model.js b/src/Model.js index ed4fcd0..27dfd6b 100644 --- a/src/Model.js +++ b/src/Model.js @@ -187,6 +187,8 @@ export default class Model extends Queryable { /** * Get array of hidden fields + * Please note only properties always hidden are returned from this method + * Hidden values depanding on serialization context are not taken into account * * @return {String[]} */ @@ -211,4 +213,4 @@ export default class Model extends Queryable { mergeFields() { return this._unique.concat(this._indexed); } -} \ No newline at end of file +} diff --git a/src/Node.js b/src/Node.js index 7a0fe44..4af8ca3 100644 --- a/src/Node.js +++ b/src/Node.js @@ -125,7 +125,7 @@ export default class Node extends Entity { * * @return {Promise} */ - toJson() { + toJson(group) { const output = { _id: this.id(), _labels: this.labels(), @@ -133,7 +133,7 @@ export default class Node extends Entity { // Properties this._model.properties().forEach((property, key) => { - if ( property.hidden() ) { + if ( property.hidden(group) ) { return; } @@ -171,7 +171,7 @@ export default class Node extends Entity { if ( this._eager.has( rel.name() ) ) { // Call internal toJson function on either a Node or NodeCollection - return this._eager.get( rel.name() ).toJson() + return this._eager.get( rel.name() ).toJson(group) .then(value => { return { key, value }; }); @@ -216,4 +216,4 @@ export default class Node extends Entity { }); } -} \ No newline at end of file +} diff --git a/src/Property.js b/src/Property.js index 0a70df5..a0d1217 100644 --- a/src/Property.js +++ b/src/Property.js @@ -1,6 +1,6 @@ /** * Container holding information for a property. - * + * * TODO: Schema validation to enforce correct data types */ export default class Property { @@ -50,8 +50,12 @@ export default class Property { return this._primary || this._protected; } - hidden() { - return this._hidden; + hidden(group) { + if ( Array.isArray(this._hidden) ) { + return (this._hidden.indexOf(group) !== -1); + } + + return this._hidden || false; } readonly() { diff --git a/src/Relationship.js b/src/Relationship.js index 2730dc8..dde60af 100644 --- a/src/Relationship.js +++ b/src/Relationship.js @@ -77,7 +77,7 @@ export default class Relationship extends Entity { * * @return {Promise} */ - toJson() { + toJson(group) { const output = { _id: this.id(), _type: this.type(), @@ -87,7 +87,7 @@ export default class Relationship extends Entity { // Properties definition.properties().forEach((property, key) => { - if ( property.hidden() ) { + if ( property.hidden(group) ) { return; } diff --git a/test/Model.spec.js b/test/Model.spec.js index 68843a2..7b3f518 100644 --- a/test/Model.spec.js +++ b/test/Model.spec.js @@ -27,6 +27,7 @@ describe('Model.js', () => { index: true, unique: true, required: true, + hidden: ['toHide'] }, relationship: { type: 'relationship', @@ -97,7 +98,9 @@ describe('Model.js', () => { expect( model.properties().get('string').indexed() ).to.equal(true); expect( model.properties().get('string').unique() ).to.equal(true); - + expect( model.properties().get('string').hidden('toHide') ).to.equal(true); + expect( model.properties().get('string').hidden('anyOtherGroup') ).to.equal(false); + expect( model.properties().get('number').readonly() ).to.equal(true); expect( model.properties().get('number').hidden() ).to.equal(true); @@ -189,4 +192,4 @@ describe('Model.js', () => { }); }); -}); \ No newline at end of file +});