lakehouse-sequelize-restapi-keycloak-cache
Advanced tools
Comparing version
{ | ||
"name": "lakehouse-sequelize-restapi-keycloak-cache", | ||
"version": "1.2.6", | ||
"version": "1.2.7", | ||
"description": "caching for all types of data cached on redis hosts/cluster: data from database and sequelize ORM, rest API, keycloak admin api library", | ||
@@ -5,0 +5,0 @@ "main": "src/index.js", |
120
README.md
@@ -43,6 +43,7 @@ # lakehouse-sequelize-restapi-keycloak-cache | ||
// the cache function accepts 2 params: | ||
// + "loggedInUserId" is the id of the user who has logged in to the system and is interacting with the model | ||
// + "customKey": you can use any key string here to suit your project need on caching plan. For example, we use the "loggedInUserId" which is the id of the user who has logged in to the system and is interacting with the model | ||
// + "id" is the primary key of the model instance with which the user is interacting | ||
// caching for each loggedInUserId | ||
let response = await Car.cache(customKey, null).findAll(); | ||
// caching for each "customKey", here example of "loggedInUserId" | ||
let response = await Car.cache(loggedInUserId, null).findAll(); | ||
@@ -57,3 +58,3 @@ let response = await Car.cache(loggedInUserId, null).findAndCountAll(); | ||
// caching without the need of knowing who has logged into the system | ||
// caching without the need of customKey, example here we dont need to know who has logged into the system | ||
let response = await Car.cache().findAll(); | ||
@@ -69,4 +70,3 @@ let response = await Car.cache().findAndCountAll(); | ||
// Note that with only postgresql, the sequelize ORM will return the updated object with id in the model update() command: using option: "returning: true": | ||
let loggedInUserId = JwtHelper.decode(token).sub; | ||
response = await Components.cache(loggedInUserId, id).update({ | ||
response = await Components.cache(customKey, id).update({ | ||
ip: component.ip, | ||
@@ -109,3 +109,3 @@ port: component.port, | ||
} | ||
// => findAll with enable caching: no logged in user id | ||
// => findAll with enable caching: no customKey, example here is no logged in user id | ||
async findAll(token) { | ||
@@ -116,3 +116,3 @@ var response = null; | ||
let apiMethod = "GET" | ||
let moduleName = this.constructor.name; | ||
let moduleName = this.constructor.name; | ||
let data = RestApiCaching.apiCache(token, null, moduleName, queryUrl, null, apiMethod); | ||
@@ -124,33 +124,5 @@ return data; | ||
} | ||
// => findAll with enable caching: no logged in user id, but the token contain the logged in user id in the token.sub attribute | ||
async findAll(token) { | ||
var response = null; | ||
try { | ||
let queryUrl = 'https://abc.com/user'; | ||
let apiMethod = "GET" | ||
let moduleName = this.constructor.name; | ||
let data = RestApiCaching.apiCache(token, null, moduleName, queryUrl, null, apiMethod); | ||
return data; | ||
} catch (error) { | ||
throw error; | ||
} | ||
} | ||
// => findAll with enable caching: with logged in user id | ||
async findAll(token, loggedInUserId) { | ||
var response = null; | ||
try { | ||
let queryUrl = 'https://abc.com/user'; | ||
let apiMethod = "GET" | ||
let moduleName = this.constructor.name; | ||
let data = RestApiCaching.apiCache(token, loggedInUserId, moduleName, queryUrl, null, apiMethod); | ||
return data; | ||
} catch (error) { | ||
throw error; | ||
} | ||
} | ||
// => nearly the same with: saveOne, editOne and deleteOne: with logged in user id | ||
async saveOne(token, user, loggedInUserId) { | ||
// => nearly the same with: saveOne, editOne and deleteOne: without customKey, example here without logged in user id | ||
async saveOne(token, user) { | ||
var dateCreate = new Date(); | ||
@@ -162,4 +134,4 @@ user.attributes.dateCreate = [dateCreate]; | ||
let apiMethod = "POST"; | ||
let moduleName = this.constructor.name; | ||
let response = RestApiCaching.apiCache(token, loggedInUserId, moduleName, queryUrl, data, apiMethod); | ||
let moduleName = this.constructor.name; | ||
let response = RestApiCaching.apiCache(token, null, moduleName, queryUrl, data, apiMethod); | ||
return response; | ||
@@ -171,3 +143,3 @@ } catch (error) { | ||
async editOne(token, user, loggedInUserId) { | ||
async editOne(token, user) { | ||
var dateModify = new Date(); | ||
@@ -179,4 +151,4 @@ user.attributes.dateModify = [dateModify]; | ||
let apiMethod = "PUT"; | ||
let moduleName = this.constructor.name; | ||
let response = RestApiCaching.apiCache(token, loggedInUserId, moduleName, queryUrl, data, apiMethod); | ||
let moduleName = this.constructor.name; | ||
let response = RestApiCaching.apiCache(token, null, moduleName, queryUrl, data, apiMethod); | ||
return response; | ||
@@ -188,8 +160,8 @@ } catch (error) { | ||
async deleteOne(token, userId, loggedInUserId) { | ||
async deleteOne(token, userId) { | ||
try { | ||
let queryUrl = "https://abc.com/user/" + user.id; | ||
let apiMethod = "DELETE"; | ||
let moduleName = this.constructor.name; | ||
let response = RestApiCaching.apiCache(token, loggedInUserId, moduleName, queryUrl, null, apiMethod); | ||
let moduleName = this.constructor.name; | ||
let response = RestApiCaching.apiCache(token, null, moduleName, queryUrl, null, apiMethod); | ||
return response; | ||
@@ -200,5 +172,21 @@ } catch (error) { | ||
} | ||
// => nearly the same with: saveOne, editOne and deleteOne: without logged in user id | ||
async saveOne(token, user) { | ||
// => findAll with enable caching: with customKey, example here with logged in user id: customKey = loggedInUserId | ||
async findAll(token, customKey) { | ||
var response = null; | ||
try { | ||
let queryUrl = 'https://abc.com/user'; | ||
let apiMethod = "GET" | ||
let moduleName = this.constructor.name; | ||
let data = RestApiCaching.apiCache(token, customKey, moduleName, queryUrl, null, apiMethod); | ||
return data; | ||
} catch (error) { | ||
throw error; | ||
} | ||
} | ||
// => nearly the same with: saveOne, editOne and deleteOne: with customKey, example here with logged in user id | ||
async saveOne(token, user, customKey) { | ||
var dateCreate = new Date(); | ||
@@ -210,4 +198,4 @@ user.attributes.dateCreate = [dateCreate]; | ||
let apiMethod = "POST"; | ||
let moduleName = this.constructor.name; | ||
let response = RestApiCaching.apiCache(token, null, moduleName, queryUrl, data, apiMethod); | ||
let moduleName = this.constructor.name; | ||
let response = RestApiCaching.apiCache(token, customKey, moduleName, queryUrl, data, apiMethod); | ||
return response; | ||
@@ -219,3 +207,3 @@ } catch (error) { | ||
async editOne(token, user) { | ||
async editOne(token, user, customKey) { | ||
var dateModify = new Date(); | ||
@@ -227,4 +215,4 @@ user.attributes.dateModify = [dateModify]; | ||
let apiMethod = "PUT"; | ||
let moduleName = this.constructor.name; | ||
let response = RestApiCaching.apiCache(token, null, moduleName, queryUrl, data, apiMethod); | ||
let moduleName = this.constructor.name; | ||
let response = RestApiCaching.apiCache(token, customKey, moduleName, queryUrl, data, apiMethod); | ||
return response; | ||
@@ -236,8 +224,8 @@ } catch (error) { | ||
async deleteOne(token, userId) { | ||
async deleteOne(token, userId, customKey) { | ||
try { | ||
let queryUrl = "https://abc.com/user/" + user.id; | ||
let apiMethod = "DELETE"; | ||
let moduleName = this.constructor.name; | ||
let response = RestApiCaching.apiCache(token, null, moduleName, queryUrl, null, apiMethod); | ||
let moduleName = this.constructor.name; | ||
let response = RestApiCaching.apiCache(token, customKey, moduleName, queryUrl, null, apiMethod); | ||
return response; | ||
@@ -277,3 +265,3 @@ } catch (error) { | ||
// function without cache enabled | ||
async findGroupsOfUser(userIdLoggedIn, user_id) { | ||
async findGroupsOfUser(user_id) { | ||
try { | ||
@@ -287,4 +275,4 @@ const groups = await kcAdminClient.users.listGroups({ id: user_id }); | ||
// function with cache enabled | ||
async findGroupsOfUser(userIdLoggedIn, user_id, moduleName) { | ||
// function with cache enabled: customKey = userIdLoggedIn | ||
async findGroupsOfUser(customKey, user_id, moduleName) { | ||
try { | ||
@@ -295,3 +283,3 @@ let method = "GET"; | ||
let bodyParams = null; | ||
const groups = KeycloakAdminCaching.keycloakAdminCache(method, kcAdminClient, moduleName, keycloakAdminClientFuncName, userIdLoggedIn, queryParams, bodyParams); | ||
const groups = KeycloakAdminCaching.keycloakAdminCache(method, kcAdminClient, moduleName, keycloakAdminClientFuncName, customKey, queryParams, bodyParams); | ||
return groups; | ||
@@ -308,3 +296,3 @@ } catch (error) { | ||
// function without cache enabled | ||
async disableUser(userIdLoggedIn, userId) { | ||
async disableUser(userId) { | ||
try { | ||
@@ -322,4 +310,4 @@ await kcAdminClient.users.update( | ||
// => function with cache enabled | ||
async disableUser(userIdLoggedIn, userId, moduleName) { | ||
// => function with cache enabled: customKey = userIdLoggedIn | ||
async disableUser(customKey, userId, moduleName) { | ||
try { | ||
@@ -334,3 +322,3 @@ let method = "PUT"; | ||
}; | ||
const response = KeycloakAdminCaching.keycloakAdminCache(method, kcAdminClient, moduleName, keycloakAdminClientFuncName, userIdLoggedIn, queryParams, bodyParams); | ||
const response = KeycloakAdminCaching.keycloakAdminCache(method, kcAdminClient, moduleName, keycloakAdminClientFuncName, customKey, queryParams, bodyParams); | ||
} catch (error) { | ||
@@ -337,0 +325,0 @@ console.error("AdminClient logoutUser: " + error); |
@@ -21,3 +21,3 @@ // this class is to support caching for Class / sequelizeModel Methods: | ||
}; | ||
compositeKey['hashKey'] = loggedInUserId ? loggedInUserId+":"+sequelizeModel.name+":findByPk" : 'findByPk'; | ||
compositeKey['hashKey'] = loggedInUserId ? loggedInUserId+":"+sequelizeModel.name+":findByPk" : sequelizeModel.name+':findByPk'; | ||
compositeKey['key'] = cid ? cid : ''; | ||
@@ -40,3 +40,3 @@ let instance = await sequelizeModel.create.apply(sequelizeModel, arguments); | ||
}; | ||
compositeKey['hashKey'] = loggedInUserId ? loggedInUserId+":"+sequelizeModel.name+":findByPk" : 'findByPk'; | ||
compositeKey['hashKey'] = loggedInUserId ? loggedInUserId+":"+sequelizeModel.name+":findByPk" : sequelizeModel.name+':findByPk'; | ||
let instances = await sequelizeModel.bulkCreate.apply(sequelizeModel, arguments); | ||
@@ -56,3 +56,3 @@ await CachingUtils.saveHashMultiKeys(cacheClient, sequelizeModel, instances, compositeKey); | ||
}; | ||
compositeKey['hashKey'] = loggedInUserId ? loggedInUserId+":"+sequelizeModel.name+":findByPk" : 'findByPk'; | ||
compositeKey['hashKey'] = loggedInUserId ? loggedInUserId+":"+sequelizeModel.name+":findByPk" : sequelizeModel.name+':findByPk'; | ||
compositeKey['key'] = id ? id : ''; | ||
@@ -86,3 +86,3 @@ return await CachingUtils.getHash(cacheClient, sequelizeModel, compositeKey) | ||
}; | ||
compositeKey['hashKey'] = loggedInUserId ? loggedInUserId+":"+sequelizeModel.name+":findByPk" : 'findByPk'; | ||
compositeKey['hashKey'] = loggedInUserId ? loggedInUserId+":"+sequelizeModel.name+":findByPk" : sequelizeModel.name+':findByPk'; | ||
compositeKey['key'] = cid ? cid : ''; | ||
@@ -131,3 +131,3 @@ | ||
}; | ||
compositeKey['hashKey'] = loggedInUserId ? loggedInUserId+":"+sequelizeModel.name+':find' : 'find'; | ||
compositeKey['hashKey'] = loggedInUserId ? loggedInUserId+":"+sequelizeModel.name+':find' : sequelizeModel.name+':find'; | ||
compositeKey.key = 'findAll'+queryOptiontString; | ||
@@ -155,3 +155,3 @@ | ||
}; | ||
compositeKey['hashKey'] = loggedInUserId ? loggedInUserId+":"+sequelizeModel.name+':find' : 'find'; | ||
compositeKey['hashKey'] = loggedInUserId ? loggedInUserId+":"+sequelizeModel.name+':find' : sequelizeModel.name+':find'; | ||
compositeKey.key = 'findAndCountAll'+queryOptiontString; | ||
@@ -193,3 +193,3 @@ | ||
}; | ||
compositeKey['hashKey'] = loggedInUserId ? loggedInUserId+":"+sequelizeModel.name+':find' : 'find'; | ||
compositeKey['hashKey'] = loggedInUserId ? loggedInUserId+":"+sequelizeModel.name+':find' : sequelizeModel.name+':find'; | ||
compositeKey.key = 'findOne'+queryOptiontString; | ||
@@ -212,3 +212,3 @@ let instance = await CachingUtils.getHash(cacheClient, sequelizeModel, compositeKey); | ||
}; | ||
compositeKey['hashKey'] = loggedInUserId ? loggedInUserId+":"+sequelizeModel.name+':findByPk' : 'findByPk'; | ||
compositeKey['hashKey'] = loggedInUserId ? loggedInUserId+":"+sequelizeModel.name+':findByPk' : sequelizeModel.name+':findByPk'; | ||
compositeKey['key'] = cid ? cid : ''; | ||
@@ -224,3 +224,3 @@ await sequelizeModel.destroy.apply(sequelizeModel, arguments); | ||
async clear() { | ||
let deleteKey = loggedInUserId ? loggedInUserId+":"+sequelizeModel.name+':find' : 'find'; | ||
let deleteKey = loggedInUserId ? loggedInUserId+":"+sequelizeModel.name+':find' : sequelizeModel.name+':find'; | ||
return await CachingUtils.clearKey(cacheClient, sequelizeModel, deleteKey) | ||
@@ -227,0 +227,0 @@ } |
50399
0307
-3.76%