2017-09-18 3 views
0

Je suis en train de définir des modèles connexes tels que la relation HasMany dans le @types/loopback definitionsTapuscrit/réalimentation Modèles connexes

I ont défini une interface pour hasMany comme il est mis en œuvre:

interface IHasMany { 
     /** 
     * Create a target model instance 
     * @param {Object} targetModelData The target model data 
     * @param {Function} callback The callback function 
     */ 
     // HasMany.prototype.create = function(targetModelData, options, cb) 
     create<T = any>(targetModelData: any, callback?: (err: Error, instance: T) => void): Promise<T> | void; 

(snip ... other functions findById, exists, updateById, destroyById omitted for bevity) 

Le modèle de rôle a ce qui suit à construire dans les relations (tel que défini dans le module de rebouclage):

"relations": { 
    "principals": { 
     "type": "hasMany", 
     "model": "RoleMapping", 
     "foreignKey": "roleId" 
    } 
} 

la machine à écrire, cette fonction pourrait être utilisée en tant que f ollows:

await createdRole.principals.create({ 
    principalType: loopback.RoleMapping.USER, 
    principalId: createdUser.id 
}); 

(NOTE: loopback.RoleMapping.USER est une constante dans un prochain PR je soumettrai à DT)

Alors maintenant, je dois joindre cette interface au modèle de rôle, et faites-le référencer le modèle RoleMapping.

class Role extends PersistedModel { 
     static OWNER: string; 
     static RELATED: string; 
     static AUTHENTICATED: string; 
     static UNAUTHENTICATED: string; 
     static EVERYONE: string; 

     /** HasMany RoleMappings */ 
     static async principals = ???? 

Une indication sur les prochaines étapes? On dirait que je dois étendre IHasMany être spécifique RoleMapping (comme IHaveManyRoleMappings) - éventuellement à l'aide this post, ont alors principes quelque chose comme:

static async principals = class RoleMappings implements IHasManyRoleMappings {}; 

Répondre

0

OK, pour toute personne venant d'autre sur cette question, voici la clé:

dans l'interface, en font une interface générique avec ce <T>:

interface HasMany<T> { 
     /** 
     * Find a related item by foreign key 
     * @param {*} id The foreign key 
     * @param {Object} [options] Options 
     * @param {instanceCallback} callback 
     */ 
     // HasMany.prototype.findById = function(fkId, options, cb) 
     findById<T = any>(id: any, options?: any, callback?: (err: Error, instance: T) => void): Promise<T> | void; 
(snip ... other functions findById, exists, updateById, destroyById omitted for bevity) 

Ensuite, vous suffit d'inclure cela dans votre interface/classe comme suit:

class Role extends PersistedModel { 
     static OWNER: string; 
     static RELATED: string; 
     static AUTHENTICATED: string; 
     static UNAUTHENTICATED: string; 
     static EVERYONE: string; 

     /** HasMany RoleMappings */ 
     // createdRole.principals.create({principalType: loopback.RoleMapping.USER, principalId: createdUser.id}); 
     principals: HasMany<RoleMapping>; 

Maintenant, il est facile à utiliser comme suit:

await createdRole.principals.create({ 
    principalType: loopback.RoleMapping.USER, 
    principalId: createdUser.id 
})