Skip to content

Creating FireLoop Models

Jonathan Casarrubias edited this page Nov 4, 2016 · 19 revisions

FireLoop.io

When you start building a system, the fist thing you need to do right after your setup, is to start creating your API Models.

This models are similar to LoopBack Models, but with a difference... This models will be created and executed using TypeScript instead of JavaScript. (POW!)

##Creating FireLoop Models

$ cd myproject
$ fireloop model MyModel

? Enter the model name: MyModel
? Select the data-source to attach MyModel to: db (memory)
? Select model's base class PersistedModel
? Expose MyModel via the REST API? Yes
? Custom plural form (used to build REST URL): 
? Common model or server only? common
Let's add some MyModel properties now.

Enter an empty property name when done.
? Property name: text
   invoke   loopback:property
? Property type: string
? Required? No
? Default value[leave blank for none]: 

Let's add another MyModel property.
Enter an empty property name when done.
? Property name: 

Generating: ./common/models/my-model.ts

If you have experience with LoopBack you will see it is the same creational flow, is just that as described before; The model will be created in TypeScript Language.

##Model Structure Now that your Models are in TypeScript you will see these are different in structure to the LoopBack ones, but it has the exact same functionality.

import { Model } from '@mean-expert/model';
/**
 * @module Todo
 * @description
 *  Write a useful Todo Model description
 **/
@Model({
  hooks: {
    beforeSave: { name: 'before save', type: 'operation' }
  },
  remotes: {
    myRemote: {
      accepts : { arg: 'id', type: 'string', required: true },
      returns : { arg: 'result', type: 'object' },
      http    : { path: '/:id/my-remote', verb: 'get' }
    }
  }
})
class Todo {
  /**
   * @method constructor
   * @description
   * Register model hooks and methods.
   */
  constructor(reference: any) {
  }
  // Example Operation Hook
  static beforeSave(ctx: any, next: Function): void {
    console.log('Todo: Before Save');
    next();
  }
  // Example Remote Method
  static myRemote(id: string, next: Function): void {
    next(null, `My Remote Example: ${id}`);
  }
}
// End of Todo Model
declare var module: { exports: any };
module.exports = Todo;

Clone this wiki locally