Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
constructor(modelName, opts) {
if (typeof modelName === 'object') {
// Received opts only
this.modelName = undefined;
this.opts = modelName;
} else {
// The modelName of the association. (Might not be passed in - set later
// by schema).
this.modelName = modelName ? dasherize(modelName) : '';
this.opts = opts || {};
}
// The key pointing to the association
this.key = '';
// The modelName that owns this association
this.ownerModelName = '';
}
registerModel(type, ModelClass) {
let camelizedModelName = camelize(type);
let modelName = dasherize(camelizedModelName);
// Avoid mutating original class, because we may want to reuse it across many tests
ModelClass = ModelClass.extend();
// Store model & fks in registry
// TODO: don't think this is needed anymore
this._registry[camelizedModelName] = this._registry[camelizedModelName] || { class: null, foreignKeys: [] }; // we may have created this key before, if another model added fks to it
this._registry[camelizedModelName].class = ModelClass;
// TODO: set here, remove from model#constructor
ModelClass.prototype._schema = this;
ModelClass.prototype.modelName = modelName;
// Set up associations
ModelClass.prototype.hasManyAssociations = {}; // a registry of the model's hasMany associations. Key is key from model definition, value is association instance itself
ModelClass.prototype.belongsToAssociations = {}; // a registry of the model's belongsTo associations. Key is key from model definition, value is association instance itself
ModelClass.prototype.associationKeys = []; // ex: address.user, user.addresses
it('exposes ' + name + ' entity', function () {
var entity = entities[name];
var filename = inflect.dasherize(inflect.underscore(name));
expect(entity).to.eq(require('../../lib/dbc/entities/' + filename));
});
}
first(type) {
let collection = this._collectionForType(type);
let [record] = collection;
return this._hydrate(record, dasherize(type));
}
elementIdFor(name) {
let dasherized
if (this.main.fieldNamePrefix)
dasherized = Inflector.dasherize(
this.main.fieldNamePrefix.replace(/\//g, '-') + '-' + name)
else
dasherized = Inflector.dasherize(name)
if (this.main.formName)
return this.main.formName + '-field-' + dasherized
else
return 'field-' + dasherized
}
findBy(type, query) {
let collection = this._collectionForType(type);
let records = collection.findBy(query);
return this._hydrate(records, dasherize(type));
}
elementIdFor(name) {
let dasherized
if (this.main.fieldNamePrefix)
dasherized = Inflector.dasherize(
this.main.fieldNamePrefix.replace(/\//g, '-') + '-' + name)
else
dasherized = Inflector.dasherize(name)
if (this.main.formName)
return this.main.formName + '-field-' + dasherized
else
return 'field-' + dasherized
}
new(type, attrs) {
return this._instantiateModel(dasherize(type), attrs);
}