如何在环回4组件中注入/配置用于设置关系的用户模型



根据DRY,我希望在扩展中包含我的业务逻辑(环回组件(。但是,在定义模型时,我希望指定属性的belongsTo关系。

在LB3中,我会通过定义以下模块并通过配置覆盖来执行相同的操作:

module.exports = (blogModels, options) => {
const debug = require('debug')('component:blog:postlike:model');
const {userModel} = options;
const postLikeModel = blogModels.PostLike;
const postModel = blogModels.Post;
// update relationships
postModel.belongsTo(userModel,
{as: 'userCreated', foreignKey: 'createdBy'});
postModel.belongsTo(userModel,
{as: 'userDeleted', foreignKey: 'deletedBy'});
postModel.belongsTo(postModel,
{as: 'post', foreignKey: 'postId'});
let postLike = {};
return postLike;
};

此外,我想知道如何让开发人员指定模型,以防他想要指定自定义模型

在LB3中,会这样做https://github.com/pbalan/component-blog/blob/master/lib/models/index.js#L51-L63

const debug = require('debug')('component:blog');
const accessLogger = require('../middleware/access-logger');
const userContext = require('../middleware/user-context');
const logger = require('../middleware/logging');
const reqLogger = require('../middleware/request-logging');
module.exports = function componentBlog(app, options) {
debug('initializing component');
const {loopback} = app;
options = options || {};
let dataSource = options.dataSource;
/* istanbul ignore if */
if (typeof dataSource === 'string') {
dataSource = app.dataSource[dataSource];
}
const blogModels = require('./component-blog-models')(dataSource);
const userModel = loopback.findModel(options.userModel) ||
loopback.getModelByType(loopback.User);
debug('User model: %s', userModel.modelName);
const venueModel = loopback.findModel(options.venueModel);
// debug('Venue model: %s', venueModel.modelName);
// Initialize middleware
app.middleware('initial:before', logger());
app.middleware('initial:before', reqLogger());
app.middleware('auth:after', userContext());
app.middleware('routes:before', accessLogger());
let users = {};
let venue = {};
let internalConfig = {
userModel: userModel,
venueModel: venueModel,
};
// specific to app
const post = require('./Post')(blogModels, internalConfig);
const postLike = require('./PostLike')(blogModels, internalConfig);
const postMention = require('./PostMention')(blogModels, internalConfig);
const postShare = require('./PostShare')(blogModels, internalConfig);
const postComment = require('./PostComment')(blogModels, internalConfig);
const commentComment =
require('./CommentComment')(blogModels, internalConfig);
const postMedia = require('./PostMedia')(blogModels, internalConfig);
const blogReported = require('./BlogReported')(blogModels, internalConfig);
let customModels = options.models || {};
let models = {
user: customModels.users || users,
venue: customModels.venue || venue,
blogReported: customModels.blogReported || blogReported,
post: customModels.post || post,
postLike: customModels.postLike || postLike,
postMention: customModels.postMention || postMention,
postShare: customModels.postShare || postShare,
postComment: customModels.postComment || postComment,
commentComment: customModels.commentComment || commentComment,
postMedia: customModels.postMedia || postMedia,
};
return models;
};

然后,在引导脚本中,我们可以提供必要的覆盖

https://github.com/pbalan/component-blog/blob/master/test/fixtures/simple-app/server/boot/003-component-blog.js#L7

module.exports = function blog(app) {
var blog = require('../../../../../lib');
var options = {
// custom user model
userModel: 'user', // specify your custom user model
uploadMediaUrl: '/api/containers/blog-media/upload',
baseUrl: 'http://0.0.0.0:3000',
// Data source for metadata persistence
dataSource: app.dataSources.db,
};
app.set('component-blog', options);
blog(app, options);
};

我正在为LB4寻找一个类似的例子。不幸的是,当前的示例主要反映的是应用程序而不是组件

您尝试了新的身份验证组件吗?https://loopback.io/doc/en/lb4/Loopback-component-authentication.html

它有一个绑定,用于拯救端点的已验证用户。只需在控制器的构造函数中注入用户(我不确定在端点中注入是否有效(

import {SecurityBindings, UserProfile} from '@loopback/security';
@inject(SecurityBindings.USER)
private userProfile: UserProfile,

一个简单的例子(从官方文档中提取,在我放在顶部的链接中(:

import {inject} from '@loopback/context';
import {AuthenticationBindings, authenticate} from '@loopback/authentication';
import {SecurityBindings, securityId, UserProfile} from '@loopback/security';
import {get} from '@loopback/rest';
export class WhoAmIController {
constructor(
// `AuthenticationBindings.CURRENT_USER` is now an alias of
// `SecurityBindings.USER` in @loopback/security
@inject(SecurityBindings.USER)
private userProfile: UserProfile,
) {}
@authenticate('basic')
@get('/whoami')
whoAmI(): string {
// `securityId` is Symbol to represent the security id of the user,
// typically the user id. You can find more information of `securityId` in
// https://loopback.io/doc/en/lb4/Security
return this.userProfile[securityId];
}
}

相关内容

  • 没有找到相关文章

最新更新