按照DRY的要求,我想在扩展(回环组件)中包含我的业务逻辑,但是,当定义模型时,我想指定一个属性的 belongsTo关系,在LB3中,我想通过定义以下模块并通过配置覆盖来实现。
在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.compbalancomponent-blogblobmasterlibmodelsindex.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;
};
然后,在引导脚本中,我们可以提供必要的覆盖。
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);
};
你是否尝试过新的认证组件?https:/loopback.iodocenlb4Loopback-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];
}
}