Skip to content

Commit

Permalink
added Field#path as full dot-notation path
Browse files Browse the repository at this point in the history
  • Loading branch information
rueckstiess committed Aug 19, 2015
1 parent 73c833b commit 6f1369d
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 4 deletions.
2 changes: 2 additions & 0 deletions lib/field-collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,10 @@ var FieldCollection = Collection.extend(lodashMixin, parentMixin, {
// get or create field
var field = this.get(name);
if (!field) {
var path = this.parent && this.parent.path ? this.parent.path + '.' + name : name;
field = this.add({
name: name,
path: path,
parent: this.parent
});
if (this.parent) {
Expand Down
8 changes: 8 additions & 0 deletions lib/field.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ module.exports = State.extend({
type: 'string',
required: true
},
/**
* The full path in dot-notation, e.g. "address.street"
*/
path: {
type: 'string',
required: true,
default: null
},
/**
* Number of times this field has been seen in a sample of documents.
*/
Expand Down
5 changes: 3 additions & 2 deletions lib/type-collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,15 @@ module.exports = Collection.extend(lodashMixin, parentMixin, {
*/
addToType: function(value) {
var field = this.parent;

var path = this.parent ? this.parent.path : null;
var typeName = getTypeName(value);
var added = false;
// get or create type
var type = this.get(typeName);
if (!type) {
type = this.add({
name: typeName
name: typeName,
path: path
});
added = true;
}
Expand Down
3 changes: 2 additions & 1 deletion lib/types/type.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ var Type = AmpersandState.extend({
}
},
session: {
parent: 'state'
parent: 'state',
path: 'string'
},
derived: {
modelType: {
Expand Down
4 changes: 4 additions & 0 deletions test/basic-embedded-document-array.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,9 @@ describe('basic embedded document array', function() {
following.toJSON();
});
});

it('should pass path down through array type', function() {
assert.equal(following.fields.get('following').arrayFields.at(0).path, 'following._id');
});
// @todo: write more tests when not so tired...
});
9 changes: 8 additions & 1 deletion test/basic-embedded-documents.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ describe('basic embedded documents', function() {
users = getSchema('users', docs, done);
});

it('should detect all fields', function() {
it('should detect all fields names and nested paths', function() {
var field_names = [
'_id',
'created_at',
Expand All @@ -46,7 +46,14 @@ describe('basic embedded documents', function() {
'stats',
'twitter'
];

var nested_path_names = [
'push_token.android',
'push_token.apple'
];

assert.deepEqual(users.fields.pluck('name'), field_names);
assert.deepEqual(users.fields.get('push_token').fields.pluck('path'), nested_path_names);
});
it('should serialize correctly', function() {
assert.doesNotThrow(function() {
Expand Down
25 changes: 25 additions & 0 deletions test/nested-document-path.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
var getSchema = require('../');
var assert = require('assert');
var BSON = require('bson');

/*eslint new-cap: 0, quote-props: 0*/
describe('nested document path', function() {
var schema;
var docs = [
{
'foo': {
'bar': {
'baz': 1
}
}
}
];

before(function(done) {
schema = getSchema('nested.documents', docs, done);
});

it('should assemble the path correctly with dot-notation', function() {
assert.equal(schema.fields.get('foo').fields.get('bar').fields.get('baz').path, 'foo.bar.baz');
});
});

0 comments on commit 6f1369d

Please sign in to comment.