0.1.12 • Published 3 years ago
brandingtes v0.1.12
Almatar Branding
this package make microservice able to handle different brands
How It Works
Setup Branding Package
import { AlmatarBranding } from '@almatar/branding';
AlmatarBranding.setup({
employeeAuthService: config.EMPLOYEE_AUTH_SERVICE
});
Nest.js
save brand on any request
save brand on any request using the package middleware in main.ts file
import { ContextNamespace } from '@almatar/branding';
app.use(ContextNamespace.bindENamespace);
app.use(ContextNamespace.setEBrand);
build mongoose model
import { TenantMongooseModule } from '@almatar/branding';
instead of import {MongooseModule} from '@nestjs/mongoose';
import { TenantMongooseModule } from '@almatar/branding';
@Module({
imports: [
TenantMongooseModule.forFeature([{name: 'Test', schema: TestSchema}]),
],
controllers: [TestController],
providers: [
TestService,
],
})
and add .exec()
at the end of any query make search on database
count, find, findOne, aggregate, update, findOneAndUpdate, updateOne, updateMany, findOneAndDelete, findOneAndRemove, deleteMany, deleteOne, remove
example: await this.Test.find({ id: '123' }).exec()
Hapi.js
save brand on each request
save brand on each request by using the package middleware in pre functions on each route
import { ContextNamespace } from '@almatar/branding';
// configs
auth: false,
// "tags" enable swagger to document API
tags: ['api'],
description: 'Get all coupon data',
notes: 'Returns a list of all coupons',
pre: [
{ method: ContextNamespace.bindHNamespace },
{ method: ContextNamespace.setHBrand }
],
handler: (request, reply) => {
// some code
}
build mongoose model
import { MultiTenant } from '@almatar/branding';
const mongoose = require('mongoose');
const TestSchema = new mongoose.Schema(
{
id: { type: Number, required: true }
}
)
const multiTenant = new MultiTenant(mongoose);
module.exports = multiTenant.tenantModel('Test', TestSchema);
use mongoose model
const Test = require('../models/coupon.model');
Test().count({}, callback);
skip brand for specific query
add { skipBrand: true } if you need to get the data regardless of the brand
const Test = require('../models/coupon.model');
Test({ skipBrand: true }).count({}, callback);
to keep the context in multiple callbacks
import { ContextNamespace } from '@almatar/branding';
const ns = ContextNamespace.getNamespace();
async.waterfall(
[
ns.bind(function(cb) {
cb(err, data)
}),
ns.bind(function(couponsCount, cb1) {
cb1(err, data)
})
],
callback
);
Send request With brand header
import { TenantRequest } from '@almatar/branding';
const reqOptions = {
url: `${BASEURL}/test`
};
TenantRequest.request(reqOptions, (error, response, body) => {
console.log(body);
});