1.0.0 • Published 1 year ago
lib-dynamo v1.0.0
Overview
DynamoDB abstarction over DynamodbTool kit to support Albert specific tables, Multi-Tenacy & FP (functional programming) style programming.
The DB client is initialized with req object, which contains the JWT, from which the user tenancy is determined.
PS: mid-albertdb uses this library to already initialize ALbert tables via middle-ware.
config.js
module.exports = {
rest: {
port: 8091,
docs: {
'api-company': '/api/v3/companies/spec'
},
schemas: {
'api-company': '/api/v3/companies/schema'
},
middlewares: {
AlbertDb : AlbertDb,
ACLDb : ACLDb
}
}
...
Usage
Non-FP / Traditional Programming
Every MS comes pre-initialized with AlbertDb (via middlemare injection) Just retrieve the AlbertDb from request object and then execute the normal Dynamodb Toolkit commands.
const AlbertDb = req['AlbertDb']
...
let result = await AlbertDb(Company).get(...)
E.g. : api-company : getCompaniesById.js
module.exports = async function (req, res) {
try {
// Retrive the AlberDb client from request object
const AlbertDb = req['AlbertDb']
let tenantId = req.JWT.tenantId || req.Config.DEFAULT_TENANTID;
// Pass the Model object and then execute the regular Entity method(s) from DynamoDB toolkit.
// await AlbertDb(Company).get(...)
//
// For table specifc methods use 'table' object from the client.
// await AlbertDb(Company).table.batchGet(...)
let result = await AlbertDb(Company).get({
PK: `${tenantId}#${req.params['id']}`,
SK: `${req.Config.COMPANY}`
});
...
} catch (exception) {
...
}
}
FP (lesscode style) Programming
1.0.0
1 year ago