Skip to content
Charly Poly edited this page Jun 10, 2014 · 4 revisions

Coq API documentation

  1. Declaring a model
  2. Attributes "configuration object"
  3. Coq Finders
  4. CoqModel.find(value)
  5. CoqModel.all()
  6. CoqModel.where(query)

Declaring a Model

var MyModel = Coq.factory({
   /* options */

   $resource : $resource(/*  ... */),

   $attributes : { /* see "Attributes 'configuration object' " section */ },

   $primaryKey : 'id',
   /* custom instances methods */

   setBusy : function() { /* ... */ },

   /* custom static methods */

   $statics : {
      /* ... */
   }
});

Options

  • $resource : ngResource object used as CRUD
  • $attributes : Object with key as attribute name and configuration object as value
  • $primaryKey : specify a attribute to use when do .find(/* value */) If not present, first config.$resource route param will be used, example : If config.$resource route is "/user/:id", doing User.find(1) will request /user/1
  • $statics : static methods to add on model

You can also add any properties as you want to add instance method on your model.

Errors

  • perform an Model.find(/* value */) on a Model without $primaryKey or without "resource route param" will end with a promise rejection with reason : "Error: unable to locate primary key"

Attributes "configuration object"

var MyModel = Coq.factory({

   /* ... */

   $attributes : {
      user_id : {
         type : 'number',
         visible : false
      },
      user_name : 'text'
   },
   
   /* ... */

});

Each attribute value can be an "config object" or a string.

Coq Finders

CoqModel.find(value)

Note: If Model configuration does not provide a $primaryKey or provided $resource does not have any "route param", a rejected promise will be returned with reason : "Error: unable to locate primary key"

Example

var MyModel = Coq.factory({

   $resource : $resource('http://api.com/users/:user_id'),

   $attributes : {
      user_id : 'number',
      /* ... */
   },
   
   /* ... */

});

// GET http://api.com/users/1
MyModel.find(1);

CoqModel.all()

Will fetch all records by calling CoqModel.$resource.query()

CoqModel.where(/* query:Object */)

Will perform a search using Coq.$conditionBuilder

Default CoqProvider.$conditionBuilder implementation just pass find argument to model resource parameters.

You can override CoqProvider.$conditionBuilder in your application module config block by requesting CoqProvider as dependency.

Example, if you want conditions to be support finder query like :

// GET /api/user?conditions[0][key]=age&conditions[0][op]='<'&conditions[0][value]=10&...
MyModel.where({ age : { $lt : 10 }, name : { $like : 'charly' } });
app.config(function(CoqProvider) {
   CoqProvider.$conditionBuilder = function(queryObject) {
      var query = [];
      
      angular.forEach(function(v, k) {
         if (angular.isObject(v)) {
            var   item = {
                     key   : k,
                     value : v
                  },
                  op = Object.keys(v)[0];

            if (op == '$lt') {
               item.op = '<';
            } else if (op == '$gt') {
               item.op = '>';
            } else if (op == '$lte') {
               item.op = '<=';
            } else if (op == '$gte') {
               item.op = '>=';
            } else if (op == '$like') {
               item.op = '%';
            } else if (op == '$not') {
               item.op = '!=';
            } else  {
               return;
            }

            query.push(item);
         } else {
            query.push({
               key   : k,
               op    : '=',
               value : v
            });
         }
      });
      
      return {
         conditions : query
      };
   };
});