Identity and Access Management (IAM) module for NestJS, inspired by AWS IAM
nestjs-iacry supports optional caching to reduce database reads for frequently checked policies.
npm install --save ioredis
import { IACryModule, SEQUELIZE_STORAGE, IOREDIS_CACHE } from 'nestjs-iacry';
import Redis from 'ioredis';
IACryModule.forRoot({
storage: SEQUELIZE_STORAGE,
storageRepository: PolicyStorageModel,
cache: IOREDIS_CACHE,
cacheClient: new Redis('redis://localhost:6379'),
cacheOptions: {
expire: 600, // 10 minutes (default: 3600 = 1 hour)
prefix: 'MY_APP_POLICIES/', // key prefix (default: 'IACRY_PCACHE/')
},
});
fetch / fetchBySid, the cache is checked first. On miss, storage is queried and the result is cached.save, add, purge, and saveBySid, the cache is invalidated before the write is delegated to storage.{prefix}{entity}/{id} or {prefix}{entity}/{id}/{sid} for sid-scoped lookups.| Option | Type | Default | Description |
|---|---|---|---|
expire |
number |
3600 |
Cache TTL in seconds |
prefix |
string |
'IACRY_PCACHE/' |
Key prefix for all cache entries |
Implement the Cache interface:
import { Cache } from 'nestjs-iacry';
export class MyCache implements Cache {
async set(key: string, value: string, expire?: number): Promise<boolean> {
// Store value, optionally with TTL in seconds
return true;
}
async has(key: string): Promise<boolean> {
// Check if key exists
}
async get(key: string): Promise<string> {
// Retrieve value
}
async remove(key: string): Promise<boolean> {
// Delete key
return true;
}
}
Then pass it directly:
IACryModule.forRoot({
storage: SEQUELIZE_STORAGE,
storageRepository: PolicyStorageModel,
cache: new MyCache(),
});
Next: Advanced