sails-generate-forestay
Version:
Build dynamic user interfaces quickly and easily! Use the forestay generator to scaffold complete CRUD interfaces using just your SailsJS model attributes.
44 lines (32 loc) • 1.54 kB
JavaScript
var capitalizeFirstLetter = require("./capitalizeFirstLetter")
const htmlSanitize = require('sanitize-html')
const async = require('async')
module.exports = function addNewCollections(forestay, newCollections, cb) {
var newIds = {}
if (newCollections.length < 1) return cb(null, null)
async.eachOfSeries(newCollections, function(nc, key, cb) {
if (_.get(forestay.config.attributes, [nc.key, 'meta', 'forestay', 'allowAddition']) !== true) return cb(null)
var model = _.get(forestay.config.attributes, [nc.key, 'collection'])
model = capitalizeFirstLetter(model)
async.eachSeries(nc.records, function eachItem(r, cb) {
var save = {}
var populateBy = _.get(forestay.config.attributes, [nc.key, 'meta', 'forestay', 'populateBy'])
save[populateBy] = htmlSanitize(r[populateBy], {
allowedTags: [],
allowedAttributes: []
})
global[model].create(save).fetch().exec(function(err, record) {
if (err) return cb(err)
if (typeof newIds[nc.key] == "undefined") newIds[nc.key] = []
newIds[nc.key].push(record.id)
return cb(null)
})
}, function doneEachItem(err) {
return cb(null)
})
},
function endEachNewCollection(err) {
if (err) return cb(err)
return cb(null, newIds)
})
}