Skip to content

Commit

Permalink
refactor(model): improve internal model for sub-documents
Browse files Browse the repository at this point in the history
  • Loading branch information
Peter Marton committed Aug 2, 2015
1 parent ff20a60 commit 95cf81f
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 41 deletions.
20 changes: 14 additions & 6 deletions src/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,24 +49,29 @@ function getField (schemaPath) {
* Extracts tree chunk from path if it's a sub-document
* @method extractPath
* @param {Object} schemaPath
* @param {Object} model
* @return {Object} field
*/
function extractPath(schemaPath) {
function extractPath(schemaPath, model) {
let subs = schemaPath.path.split('.');
var subNames = schemaPath.path.split('.');

return reduceRight(subs, (field, sub, key) => {
var path = subNames.join('.');
var obj = {};

if (key === (subs.length - 1)) {
obj[sub] = getField(schemaPath);
} else {
obj[sub] = {
name: sub,
path: subNames.join('.'),
path: path,
fullPath: `${model.name}.${path}`,
indexed: false,
instance: 'Object',
caster: field
caster: {
fields: field
}
};
}

Expand All @@ -80,11 +85,12 @@ function extractPath(schemaPath) {
* Merge sub-document tree chunks
* @method extractPaths
* @param {Object} schemaPaths
* @param {Object} model
* @return {Object) extractedSchemaPaths
*/
function extractPaths (schemaPaths) {
function extractPaths (schemaPaths, model) {
return reduce(schemaPaths, (fields, schemaPath) => {
return merge(fields, extractPath(schemaPath));
return merge(fields, extractPath(schemaPath, model));
}, {});
}

Expand All @@ -97,7 +103,9 @@ function extractPaths (schemaPaths) {
function getModel (mongooseModel) {
const schemaPaths = mongooseModel.schema.paths;

let fields = extractPaths(schemaPaths);
let fields = extractPaths(schemaPaths, {
name: mongooseModel.modelName
});

return {
name: mongooseModel.modelName,
Expand Down
101 changes: 66 additions & 35 deletions src/model.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,34 @@ describe('model', () => {
var treeChunk = extractPath({
path: 'foo.bar.so',
instance: 'String'
}, {
name: 'User'
});

expect(treeChunk).to.be.eql({
foo: {
name: 'foo',
path: 'foo',
fullPath: 'User.foo',
indexed: false,
name: 'foo',
instance: 'Object',
caster: {
bar: {
path: 'foo.bar',
indexed: false,
name: 'bar',
instance: 'Object',
caster: {
so: {
path: 'foo.bar.so',
indexed: false,
name: 'so',
instance: 'String'
fields: {
bar: {
name: 'bar',
path: 'foo.bar',
fullPath: 'User.foo.bar',
indexed: false,
instance: 'Object',
caster: {
fields: {
so: {
name: 'so',
path: 'foo.bar.so',
instance: 'String',
indexed: false
}
}
}
}
}
Expand All @@ -51,43 +59,66 @@ describe('model', () => {
'sub.sub': {
path: 'sub.sub'
},
}, {
name: 'User'
});

expect(tree).to.containSubset({
foo: {
name: 'foo',
path: 'foo',
fullPath: 'User.foo',
indexed: false,
instance: 'Object',
caster: {
bar: {
caster: {
so: {
path: 'foo.bar.so',
indexed: false,
name: 'so'
},
very: {
path: 'foo.bar.very',
indexed: false,
name: 'very'
fields: {
bar: {
name: 'bar',
path: 'foo.bar',
fullPath: 'User.foo.bar',
indexed: false,
instance: 'Object',
caster: {
fields: {
so: {
name: 'so',
path: 'foo.bar.so',
indexed: false
},
very: {
name: 'very',
path: 'foo.bar.very',
indexed: false
}
}
}
},
grr: {
name: 'grr',
path: 'foo.grr',
indexed: false
}
},
grr: {
path: 'foo.grr',
indexed: false,
name: 'grr'
}
},
}
},
simple: {
name: 'simple',
path: 'simple',
indexed: false,
name: 'simple'
indexed: false
},
sub: {
name: 'sub',
path: 'sub',
fullPath: 'User.sub',
indexed: false,
instance: 'Object',
caster: {
sub: {
path: 'sub.sub',
indexed: false,
name: 'sub'
fields: {
sub: {
name: 'sub',
path: 'sub.sub',
indexed: false
}
}
}
}
Expand Down

0 comments on commit 95cf81f

Please sign in to comment.