apostrophe-schemas
Advanced tools
Comparing version 0.5.19 to 0.5.20
86
index.js
@@ -504,2 +504,4 @@ var async = require('async'); | ||
// module. Multiple "dot notation" joins may share a prefix. | ||
// | ||
// Joins are also supported in the schemas of array fields. | ||
@@ -522,18 +524,40 @@ self.join = function(req, schema, objectOrArray, withJoins, callback) { | ||
} | ||
// Only interested in joins | ||
var joins = _.filter(schema, function(field) { | ||
return !!self.joinrs[field.type]; | ||
}); | ||
if (objects.length > 1) { | ||
// Only interested in joins that are not restricted by ifOnlyOne. | ||
// This mechanism saves time and memory in cases where you don't need | ||
// the results of the join in index views | ||
joins = _.filter(joins, function(join) { | ||
return !join.ifOnlyOne; | ||
// build an array of joins of interest, found at any level | ||
// in the schema, even those nested in array schemas. Add | ||
// an _arrays property to each one which contains the names | ||
// of the array fields leading to this join, if any, so | ||
// we know where to store the results. Also set a | ||
// _dotPath property which can be used to identify relevant | ||
// joins when the withJoins option is present | ||
var joins = []; | ||
function findJoins(schema, arrays) { | ||
var _joins = _.filter(schema, function(field) { | ||
return !!self.joinrs[field.type]; | ||
}); | ||
_.each(_joins, function(join) { | ||
// If we have more than one object we're not interested in joins | ||
// with the ifOnlyOne restriction right now. | ||
if ((objects.length > 1) && join.ifOnlyOne) { | ||
return; | ||
} | ||
join._arrays = _.clone(arrays); | ||
join._dotPath = arrays.join('.') + '.' + join.name; | ||
}); | ||
joins = joins.concat(_joins); | ||
_.each(schema, function(field) { | ||
if (field.type === 'array') { | ||
findJoins(field.schema, arrays.concat(field.name)); | ||
} | ||
}); | ||
} | ||
findJoins(schema, []); | ||
// The withJoins option allows restriction of joins. Set to false | ||
// it blocks all joins. Set to an array, it allows the joins named within. | ||
// If some of those names use dot notation, a chain of nested joins to be | ||
// permitted can be specified. | ||
// Dot notation can be used to specify joins in array properties, | ||
// or joins reached via other joins. | ||
// | ||
@@ -548,12 +572,13 @@ // By default, all configured joins will take place, but withJoins: false | ||
joins = _.filter(joins, function(join) { | ||
var dotPath = join._dotPath; | ||
var winner; | ||
_.each(withJoins, function(withJoinName) { | ||
if (withJoinName === join.name) { | ||
if (withJoinName === dotPath) { | ||
winner = true; | ||
} | ||
if (withJoinName.substr(0, join.name.length + 1) === (join.name + '.')) { | ||
if (!withJoinsNext[join.name]) { | ||
withJoinsNext[join.name] = []; | ||
if (withJoinName.substr(0, dotPath + 1) === (dotPath + '.')) { | ||
if (!withJoinsNext[dotPath]) { | ||
withJoinsNext[dotPath] = []; | ||
} | ||
withJoinsNext[join.name].push(withJoinName.substr(join.name.length + 1)); | ||
withJoinsNext[dotPath].push(withJoinName.substr(dotPath.length + 1)); | ||
winner = true; | ||
@@ -570,3 +595,3 @@ } | ||
if (join.withJoins) { | ||
withJoinsNext[join.name] = join.withJoins; | ||
withJoinsNext[join._dotPath] = join.withJoins; | ||
} | ||
@@ -576,4 +601,20 @@ }); | ||
return async.eachSeries(joins, function(join, callback) { | ||
var arrays = join._arrays; | ||
function findObjectsInArrays(objects, arrays) { | ||
if (!arrays.length) { | ||
return objects; | ||
} | ||
var array = arrays[0]; | ||
var _objects = []; | ||
_.each(objects, function(object) { | ||
_objects = _objects.concat(object[array] || []); | ||
}); | ||
return findObjectsInArrays(_objects, arrays.slice(1)); | ||
} | ||
_objects = findObjectsInArrays(objects, arrays); | ||
if (!join.name.match(/^_/)) { | ||
console.error('WARNING: joins should always be given names beginning with an underscore (_). Otherwise you will waste space in your database storing the results'); | ||
return callback(new Error('Joins should always be given names beginning with an underscore (_). Otherwise we would waste space in your database storing the results statically. There would also be a conflict with the array field withJoins syntax. Join name is: ' + join._dotPath)); | ||
} | ||
@@ -595,4 +636,3 @@ var manager = self._pages.getManager(join.withType); | ||
} else { | ||
// Simple manager for a page type. If it has a getter, use it, | ||
// otherwise supply one | ||
// If it has a getter, use it, otherwise supply one | ||
getter = manager.get || function(req, _criteria, filters, callback) { | ||
@@ -616,3 +656,3 @@ var criteria = { | ||
getOptions: { | ||
withJoins: withJoinsNext[join.name] || false, | ||
withJoins: withJoinsNext[join._dotPath] || false, | ||
permalink: true | ||
@@ -625,3 +665,3 @@ } | ||
_.extend(options.getOptions, join.getOptions || {}); | ||
return self.joinrs[join.type](req, join, options, objects, callback); | ||
return self.joinrs[join.type](req, join, options, _objects, callback); | ||
}, function(err) { | ||
@@ -628,0 +668,0 @@ return callback(err); |
{ | ||
"version": "0.5.19", | ||
"version": "0.5.20", | ||
"name": "apostrophe-schemas", | ||
@@ -4,0 +4,0 @@ "description": "Schemas for easy editing of properties in Apostrophe objects", |
@@ -224,2 +224,3 @@ function AposSchemas() { | ||
data[name] = $field.selective('get', { incomplete: true })[0]; | ||
console.log(data[name]); | ||
if (field.required && !data[name]) { | ||
@@ -226,0 +227,0 @@ return apos.afterYield(_.partial(callback, 'required')); |
@@ -248,3 +248,3 @@ # apostrophe-schemas | ||
Areas and thumbnails in arrays cannot be edited "in context" on a page, they must be updated through the schema editor. | ||
Areas and thumbnails in arrays cannot be edited "in context" on a page, they must be updated through the schema editor. However, check out [schema widgets](https://github.com/punkave/apostrophe-schema-widgets) for a way to add widgets powered by schemas anywhere in the flow of a page. | ||
@@ -430,3 +430,3 @@ #### Preventing Autocomplete | ||
name: '_events', | ||
... | ||
// Details of the join, then... | ||
withJoins: [ '_promoters' ] | ||
@@ -447,3 +447,3 @@ } | ||
This will allow events to be joined with their promoters, and promoters to be joiend with their assistants, and there the chain will stop. | ||
This will allow events to be joined with their promoters, and promoters to be joined with their assistants, and there the chain will stop. | ||
@@ -456,4 +456,24 @@ You can specify more than one join to allow, and they may share a prefix: | ||
Remember, each of these joins must be present in the configuration for the appropriate module. | ||
Remember, each of these later joins must actually be present in the configuration for the module in question. That is, "promoters" must have a join called "_assistants" defined in its schema. | ||
##### Nested Joins and Arrays | ||
Joins are allowed in the schema of an [array field](#arrays-in-schemas), and they work exactly as you would expect. Just include joins in the schema for the array as you normally would. | ||
And if you are carrying out a nested join with the `withJoins` option, you'll just need to refer to the join correctly. | ||
Let's say that each promoter has an array of ads, and each ad is joined to a media outlet. We're joing with events, which are joined to promoters, and we want to make sure media outlets are included in the results. | ||
So we write: | ||
```javascript | ||
addFields: [ | ||
{ | ||
name: '_events', | ||
// Details of the join, then... | ||
withJoins: [ '_promoters.ads._mediaOutlet' ] | ||
} | ||
] | ||
``` | ||
#### Many-To-Many Joins | ||
@@ -535,3 +555,3 @@ | ||
If the author's note for every each appearance of each story has to be super-fancy, with rich text and images, then you should make a new module that subclasses snippets in its own right and just join both books and stories to that new module. | ||
If the author's note for every each appearance of each story has to be super-fancy, with rich text and images, then you should make a new module that subclasses snippets in its own right and just join both books and stories to that new module. You can also use [array fields](#arrays-in-schemas) in creative ways to address that problem. | ||
@@ -538,0 +558,0 @@ But if the relationship just has a few simple attributes, there is an easier way: |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
94258
1206
962
0