Skip to content

Commit

Permalink
adds YUIDoc comments to orm/schema.js (#742)
Browse files Browse the repository at this point in the history
  • Loading branch information
Liz Baillie authored and samselikoff committed May 20, 2016
1 parent 58e9a51 commit 8278222
Showing 1 changed file with 86 additions and 4 deletions.
90 changes: 86 additions & 4 deletions addon/orm/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ import _forIn from 'lodash/object/forIn';
import _includes from 'lodash/collection/includes';
import assert from '../assert';

/**
* @class Schema
* @constructor
* @public
*/
export default class Schema {

constructor(db, modelsMap = {}) {
Expand All @@ -16,12 +21,23 @@ export default class Schema {
this.registerModels(modelsMap);
}

/**
* @method registerModels
* @param hash
* @public
*/
registerModels(hash = {}) {
_forIn(hash, (model, key) => {
this.registerModel(key, hash[key]);
});
}

/**
* @method registerModel
* @param type
* @param ModelClass
* @public
*/
registerModel(type, ModelClass) {
let camelizedModelName = camelize(type);

Expand Down Expand Up @@ -76,20 +92,43 @@ export default class Schema {
return this;
}

/**
* @method new
* @param type
* @param attrs
* @public
*/
new(type, attrs) {
return this._instantiateModel(dasherize(type), attrs);
}

/**
* @method create
* @param type
* @param attrs
* @public
*/
create(type, attrs) {
return this.new(type, attrs).save();
}

/**
* @method all
* @param type
* @public
*/
all(type) {
let collection = this._collectionForType(type);

return this._hydrate(collection, dasherize(type));
}

/**
* @method find
* @param type
* @param ids
* @public
*/
find(type, ids) {
let collection = this._collectionForType(type);
let records = collection.find(ids);
Expand All @@ -104,13 +143,24 @@ export default class Schema {
return this._hydrate(records, dasherize(type));
}

/**
* @method where
* @param type
* @param query
* @public
*/
where(type, query) {
let collection = this._collectionForType(type);
let records = collection.where(query);

return this._hydrate(records, dasherize(type));
}

/**
* @method first
* @param type
* @public
*/
first(type) {
let collection = this._collectionForType(type);
let [ record ] = collection;
Expand All @@ -121,6 +171,12 @@ export default class Schema {
/*
Private methods
*/

/**
* @method _collectionForType
* @param type
* @private
*/
_collectionForType(type) {
let collection = pluralize(type);
assert(
Expand All @@ -131,6 +187,12 @@ export default class Schema {
return this.db[collection];
}

/**
* @method _addForeignKeyToRegistry
* @param type
* @param fk
* @private
*/
_addForeignKeyToRegistry(type, fk) {
this._registry[type] = this._registry[type] || { class: null, foreignKeys: [] };

Expand All @@ -140,25 +202,45 @@ export default class Schema {
}
}

/**
* @method _instantiateModel
* @param modelName
* @param attrs
* @private
*/
_instantiateModel(modelName, attrs) {
let ModelClass = this._modelFor(modelName);
let fks = this._foreignKeysFor(modelName);

return new ModelClass(this, modelName, attrs, fks);
}

/**
* @method _modelFor
* @param modelName
* @private
*/
_modelFor(modelName) {
return this._registry[camelize(modelName)].class;
}

/**
* @method _foreignKeysFor
* @param modelName
* @private
*/
_foreignKeysFor(modelName) {
return this._registry[camelize(modelName)].foreignKeys;
}

/*
Takes a record and returns a model, or an array of records
and returns a collection.
*/
/**
* Takes a record and returns a model, or an array of records
* and returns a collection.
* @method _hydrate
* @param records
* @param modelName
* @private
*/
_hydrate(records, modelName) {
if (_isArray(records)) {
let models = records.map(function(record) {
Expand Down

0 comments on commit 8278222

Please sign in to comment.