typescript - Sailsjs model static method -


i'm trying create type definition sails.js's model interface. sails.js allows define simple file exports object nested "attributes" object. each attribute corresponds column name in database, , column type.

//mymodel.js module.exports = {    attributes:{      name:"string"   }  };  

to use model in code, write code so:

mymodel.create({name:"alex"}).then(function(result){    //the model inserted db, result created record. }); 

the code wrote looks so:

declare module sails{    export interface model{     create(params:object):promise<sails.model>;   } }  class model implements sails.model{    create(params:object):promise<sails.model> {     return undefined;   }    attributes:object;    constructor(attributes:object){     this.attributes = attributes;   }   }  class mymodel extends model{     constructor(attributes:object){         super(attributes);      } }  var _export = new mymodel({name:string});  export = _export; 

here's problem i'm having. in order sailsjs interface typescript code properly, module.exports has have attributes object on database type definitions. wouldn't problem, except when try use class, needs static methods create aren't on there.

i able write typescript code using model so:

mymodel.create({name:"bob"}).then((result:mymodel) => {    //do stuff result }); 

i've solved isn't pretty:

//api/models/mymodel.ts import model = require('./model');  var mymodel = new model({   name:"string" });  export = mymodel;   //api/model/model.ts class model {    attributes:object;   constructor(attributes:object){     this.attributes = attributes;   }  } export = model;   //api/types/mymodelquery.ts //this file maps attributes of query result  declare class screenquery {   name:string; } export = screenquery;   //sails.d.ts  declare module sails{    export interface model{     create(params:object):promise<sails.queryresult>;     attributes:object;   }    export interface queryresult{    }    export interface controller{    }  } 

i'm still working on it, i'll update once i've got full implementation done.

edit: i've created blog post describing approach i've taken. using typescript sails.js


Comments

Popular posts from this blog

java - Could not locate OpenAL library -

c++ - Delete matches in OpenCV (Keypoints and descriptors) -

sorting - opencl Bitonic sort with 64 bits keys -