-
Notifications
You must be signed in to change notification settings - Fork 36
Creating FireLoop Models
Jonathan Casarrubias edited this page Oct 28, 2016
·
19 revisions
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.
##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.
declare var module: { exports: MyModel };
/**
* @module MyModel
* @description
* TODO: Write a useful MyModel Model description
**/
class MyModel {
constructor(ParentModel: any) {
// Register your hooks within the constructor
// https://docs.strongloop.com/display/public/LB/Operation+hooks
ParentModel.observe('before save', Todo.beforeSave);
ParentModel.observe('after save', Todo.afterSave);
ParentModel.observe('access', Todo.access);
ParentModel.observe('loaded', Todo.loaded);
}
static beforeSave(ctx, next): void {
console.log('Todo: Before Save');
next();
}
static afterSave(ctx, next): void {
console.log('Todo: After Save');
next();
}
static access(ctx, next): void {
console.log('Todo: Access');
next();
}
static loaded(ctx, next): void {
console.log('Todo: Loaded');
next();
}
}
module.exports = MyModel;