Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Serialization groups #134

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
4 changes: 2 additions & 2 deletions src/Entity.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 ));
}
});
Expand Down Expand Up @@ -126,4 +126,4 @@ export default class Entity {
valueToJson(property, value) {
return valueToJson(property, value);
}
}
}
4 changes: 3 additions & 1 deletion src/Model.js
Original file line number Diff line number Diff line change
Expand Up @@ -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[]}
*/
Expand All @@ -211,4 +213,4 @@ export default class Model extends Queryable {
mergeFields() {
return this._unique.concat(this._indexed);
}
}
}
8 changes: 4 additions & 4 deletions src/Node.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,15 +125,15 @@ export default class Node extends Entity {
*
* @return {Promise}
*/
toJson() {
toJson(group) {
const output = {
_id: this.id(),
_labels: this.labels(),
};

// Properties
this._model.properties().forEach((property, key) => {
if ( property.hidden() ) {
if ( property.hidden(group) ) {
return;
}

Expand Down Expand Up @@ -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 };
});
Expand Down Expand Up @@ -216,4 +216,4 @@ export default class Node extends Entity {
});
}

}
}
10 changes: 7 additions & 3 deletions src/Property.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
* Container holding information for a property.
*
*
* TODO: Schema validation to enforce correct data types
*/
export default class Property {
Expand Down Expand Up @@ -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() {
Expand Down
4 changes: 2 additions & 2 deletions src/Relationship.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export default class Relationship extends Entity {
*
* @return {Promise}
*/
toJson() {
toJson(group) {
const output = {
_id: this.id(),
_type: this.type(),
Expand All @@ -87,7 +87,7 @@ export default class Relationship extends Entity {

// Properties
definition.properties().forEach((property, key) => {
if ( property.hidden() ) {
if ( property.hidden(group) ) {
return;
}

Expand Down
7 changes: 5 additions & 2 deletions test/Model.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ describe('Model.js', () => {
index: true,
unique: true,
required: true,
hidden: ['toHide']
},
relationship: {
type: 'relationship',
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -189,4 +192,4 @@ describe('Model.js', () => {
});
});

});
});