@nxtoai/gati
Version:
A flexible Aerospike service for NestJS applications
193 lines (192 loc) • 10.4 kB
JavaScript
;
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.GatiService = void 0;
const common_1 = require("@nestjs/common");
const aerospike_1 = require("aerospike");
const aag_1 = require("@nxtoai/aag");
const gati_validator_1 = require("./gati.validator");
const error_messages_1 = require("./error-messages");
let GatiService = class GatiService {
aag;
validator;
client;
namespace;
constructor(aag, validator, options) {
this.aag = aag;
this.validator = validator;
this.namespace = options.namespace;
this.client = new aerospike_1.Client({
hosts: options.hosts.map(host => ({ addr: host, port: options.port })),
log: {
level: 0 // Disable Aerospike's built-in logging
}
});
}
async onModuleInit() {
try {
await this.client.connect();
this.aag.debug(`GatiService initialized with options: ${JSON.stringify({
namespace: this.namespace
})}`);
}
catch (error) {
if (error instanceof Error) {
this.aag.error(`Failed to initialize GatiService due to : ${error.message}`);
throw new Error(`${error_messages_1.ERROR_MESSAGES.REDIS_CONNECTION_FAILED.code}: ${error_messages_1.ERROR_MESSAGES.REDIS_CONNECTION_FAILED.message}`);
}
throw new Error(`${error_messages_1.ERROR_MESSAGES.UNKNOWN_ERROR.code}: ${error_messages_1.ERROR_MESSAGES.UNKNOWN_ERROR.message}`);
}
}
async onModuleDestroy() {
try {
await this.close();
}
catch (error) {
if (error instanceof Error) {
this.aag.error(`Error closing GatiService due to : ${error.message}`);
}
throw new Error(`${error_messages_1.ERROR_MESSAGES.UNKNOWN_ERROR.code}: ${error_messages_1.ERROR_MESSAGES.UNKNOWN_ERROR.message}`);
}
}
async put(set, key, bins, options) {
try {
this.validator.validateKey(key);
this.validator.validateBins(bins);
this.validator.validateTtl(options?.ttl);
const aerospikeKey = new aerospike_1.Key(this.namespace, set, key);
//this.aag.debug(`Putting record with key: ${key}, set: ${set}, data: ${JSON.stringify({ bins, ttl: options?.ttl })}`);
await this.client.put(aerospikeKey, bins, { ttl: options?.ttl });
}
catch (error) {
if (error instanceof gati_validator_1.GatiValidationError) {
this.aag.error(`${error_messages_1.ERROR_MESSAGES.INVALID_KEY.code}: Validation error in put operation: ${error.message}`);
throw new Error(`${error_messages_1.ERROR_MESSAGES.INVALID_KEY.code}: ${error_messages_1.ERROR_MESSAGES.INVALID_KEY.message}`);
}
if (error instanceof Error) {
this.aag.error(`Error in put operation: ${error.message}`, error.stack);
throw new Error(`${error_messages_1.ERROR_MESSAGES.UNKNOWN_ERROR.code}: ${error_messages_1.ERROR_MESSAGES.UNKNOWN_ERROR.message}`);
}
throw new Error(`${error_messages_1.ERROR_MESSAGES.UNKNOWN_ERROR.code}: ${error_messages_1.ERROR_MESSAGES.UNKNOWN_ERROR.message}`);
}
}
async get(set, key) {
try {
this.validator.validateKey(key);
const aerospikeKey = new aerospike_1.Key(this.namespace, set, key);
this.aag.debug(`Getting record with key: ${key}, set: ${set}`);
const record = await this.client.get(aerospikeKey);
return record?.bins || null;
}
catch (error) {
if (error instanceof gati_validator_1.GatiValidationError) {
this.aag.error(`Validation error in get operation: ${error.message}`);
throw new Error(`${error_messages_1.ERROR_MESSAGES.INVALID_KEY.code}: ${error_messages_1.ERROR_MESSAGES.INVALID_KEY.message}`);
}
if (error instanceof Error) {
this.aag.error(`Error in get operation: ${error.message}`, error.stack);
throw new Error(`${error_messages_1.ERROR_MESSAGES.UNKNOWN_ERROR.code}: ${error_messages_1.ERROR_MESSAGES.UNKNOWN_ERROR.message}`);
}
throw new Error(`${error_messages_1.ERROR_MESSAGES.UNKNOWN_ERROR.code}: ${error_messages_1.ERROR_MESSAGES.UNKNOWN_ERROR.message}`);
}
}
async exists(set, key) {
try {
this.validator.validateKey(key);
const aerospikeKey = new aerospike_1.Key(this.namespace, set, key);
this.aag.debug(`Checking existence of record with key: ${key}, set: ${set}`);
const exists = await this.client.exists(aerospikeKey);
return exists;
}
catch (error) {
if (error instanceof gati_validator_1.GatiValidationError) {
this.aag.error(`Validation error in exists operation: ${error.message}`);
throw new Error(`${error_messages_1.ERROR_MESSAGES.INVALID_KEY.code}: ${error_messages_1.ERROR_MESSAGES.INVALID_KEY.message}`);
}
if (error instanceof Error) {
this.aag.error(`Error in exists operation: ${error.message}`, error.stack);
throw new Error(`${error_messages_1.ERROR_MESSAGES.UNKNOWN_ERROR.code}: ${error_messages_1.ERROR_MESSAGES.UNKNOWN_ERROR.message}`);
}
throw new Error(`${error_messages_1.ERROR_MESSAGES.UNKNOWN_ERROR.code}: ${error_messages_1.ERROR_MESSAGES.UNKNOWN_ERROR.message}`);
}
}
async remove(set, key) {
try {
this.validator.validateKey(key);
const aerospikeKey = new aerospike_1.Key(this.namespace, set, key);
this.aag.debug(`Removing record with key: ${key}, set: ${set}`);
await this.client.remove(aerospikeKey);
}
catch (error) {
if (error instanceof gati_validator_1.GatiValidationError) {
this.aag.error(`${error_messages_1.ERROR_MESSAGES.INVALID_KEY.code}: ${error_messages_1.ERROR_MESSAGES.INVALID_KEY.message}: ${error.message}`);
throw new Error(`${error_messages_1.ERROR_MESSAGES.INVALID_KEY.code}: ${error_messages_1.ERROR_MESSAGES.INVALID_KEY.message}`);
}
if (error instanceof Error) {
this.aag.error(`${error_messages_1.ERROR_MESSAGES.UNKNOWN_ERROR.code}: ${error_messages_1.ERROR_MESSAGES.UNKNOWN_ERROR.message}: ${error.message}`, error.stack);
throw new Error(`${error_messages_1.ERROR_MESSAGES.UNKNOWN_ERROR.code}: ${error_messages_1.ERROR_MESSAGES.UNKNOWN_ERROR.message}`);
}
throw new Error(`${error_messages_1.ERROR_MESSAGES.UNKNOWN_ERROR.code}: ${error_messages_1.ERROR_MESSAGES.UNKNOWN_ERROR.message}`);
}
}
async scan(set, options) {
try {
this.validator.validateScanOptions(options);
this.aag.debug(`Starting scan operation on set: ${set} with options: ${JSON.stringify(options)}`);
const results = [];
const scan = this.client.scan(this.namespace, set, options);
await new Promise((resolve, reject) => {
scan.on('data', (record) => {
results.push(record.bins);
});
scan.on('error', (error) => {
this.aag.error(`Scan error: ${error.message}`, error.stack);
reject(new Error(`${error_messages_1.ERROR_MESSAGES.UNKNOWN_ERROR.code}: ${error_messages_1.ERROR_MESSAGES.UNKNOWN_ERROR.message}`));
});
scan.on('end', () => {
this.aag.debug(`Scan operation completed with ${results.length} records`);
resolve();
});
});
return results;
}
catch (error) {
if (error instanceof gati_validator_1.GatiValidationError) {
this.aag.error(`Validation error in scan operation: ${error.message}`);
throw new Error(`${error_messages_1.ERROR_MESSAGES.INVALID_KEY.code}: ${error_messages_1.ERROR_MESSAGES.INVALID_KEY.message}`);
}
if (error instanceof Error) {
this.aag.error(`Error in scan operation: ${error.message}`, error.stack);
throw new Error(`${error_messages_1.ERROR_MESSAGES.UNKNOWN_ERROR.code}: ${error_messages_1.ERROR_MESSAGES.UNKNOWN_ERROR.message}`);
}
throw new Error(`${error_messages_1.ERROR_MESSAGES.UNKNOWN_ERROR.code}: ${error_messages_1.ERROR_MESSAGES.UNKNOWN_ERROR.message}`);
}
}
async close() {
try {
this.aag.debug('Closing GatiService connection');
await this.client.close();
}
catch (error) {
if (error instanceof Error) {
this.aag.error(`Error closing GatiService: ${error.message}`, error.stack);
throw new Error(`${error_messages_1.ERROR_MESSAGES.REDIS_DISCONNECT_FAILED.code}: ${error_messages_1.ERROR_MESSAGES.REDIS_DISCONNECT_FAILED.message}`);
}
throw new Error(`${error_messages_1.ERROR_MESSAGES.UNKNOWN_ERROR.code}: ${error_messages_1.ERROR_MESSAGES.UNKNOWN_ERROR.message}`);
}
}
};
exports.GatiService = GatiService;
exports.GatiService = GatiService = __decorate([
(0, common_1.Injectable)(),
__metadata("design:paramtypes", [aag_1.AagService,
gati_validator_1.GatiValidator, Object])
], GatiService);