Ejemplo n.º 1
0
function initAndCache (template) {

    if (templateCache[template._id.toString()]) {
        return templateCache[template._id];
    }

    // save modm instance on template
    template._modm =  {
        model: _modmCache[template.db] || (_modmCache[template.db] =
            modm(template.db, {
            server: {poolSize: 3},
            db: {w: 1}
        }))
    };

    // add mandatory field _tp
    template.schema._tp = {
        type: 'objectid',
        required: true
    };

    // add mandatory field _id
    template.schema._id = {
        type: 'objectid',
        required: true
    };

    // add mandatory field _li
    template.schema._li = [{
        type: 'objectid'
    }];

    template._modm.schema = new modm.Schema(template.schema);
    template._modm.collection = template._modm.model(template.collection, template._modm.schema);

    template.schema = template._modm.schema.paths;

    // collect all links for faster access
    for (var field in template.schema) {
        if (template.schema[field].link) {
            if (!template.linkedFields) {
                template.linkedFields = {};
            }

            template.linkedFields[field] = template.schema[field];
        }
    }

    templateCache[template._id] = template;
    return templateCache[template._id];
}
Ejemplo n.º 2
0
// init the templates config
function init (params, callback) {

    // check preconditions
    if (!params.templateConfig) {
        var err = new Error('Missing template configuration on init operation')
        err.statusCode = 400;
        return link.send(err.statusCode, err);
    }

    if (!params.templateConfig.db) {
        var err = new Error('Database name missing from template configuration');
        err.statusCode = 400;
        return callback(err);
    }

    var dbName = params.templateConfig.db;
    var templateColName = params.templateConfig.collection || 'd_templates';

    var config = {
        dbName: dbName, // TODO handle with datasources
        roleTemplateId: modm.ObjectId('000000000000000000000002'),
        templateColName: templateColName, // TODO handle with datasources
        templateSchema: {
            _id: {type: 'objectid'},
            _tp: {type: 'objectid', required: true},
            _li: [{type: 'objectid'}],
            db: {type: 'string', required: true},
            collection: {type: 'string', required: true},
            roles: {type: 'object', required: true},
            itemAccess: {type: 'string'},
            name: {type: 'string', required: true},
            schema: {type: 'object', required: true},
            links: {type: 'array'},
            options: {type: 'object'}
        }
    };
    var templateSchema = new modm.Schema(config.templateSchema);

    // template's template must be in the cache, otherwise
    // it's impossible to create the first template item
    templateCache[CORE_TEMPLATE_IDS.templates] = {
        _id: modm.ObjectId(CORE_TEMPLATE_IDS.templates),
        _modm: {
            db: _modmCache[config.dbName] = modm(config.dbName, {
                server: {poolSize: 3},
                db: {w: 1}
            })
        },
        db: config.dbName,
        collection: config.templateColName,
        roles: {
            '*': {
                access: 'r'
            }
        },
        itemAccess: 'crud',
        label: 'Templates',
        schema: templateSchema.paths
    };

    // init collection
    templateCache[CORE_TEMPLATE_IDS.templates]._modm.collection = templateCache[CORE_TEMPLATE_IDS.templates]._modm.db(config.templateColName, templateSchema);

    // done
    callback(null);
}