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 {};