@elastic.io/oih-standard-library
Advanced tools
Comparing version
@@ -1,2 +0,2 @@ | ||
declare abstract class Delete { | ||
export declare abstract class Delete { | ||
protected logger: any; | ||
@@ -23,7 +23,7 @@ constructor(logger: any); | ||
* | ||
* @param msg input message, ot used in default implementation but in some cases you may need it. | ||
* @param cfg input configuration with objectType(in default implementation). | ||
* @param cfg input message, ot used in default implementation but in some cases you may need it. | ||
* @param msg input configuration with objectType(in default implementation). | ||
* @returns object type. | ||
*/ | ||
getType(msg: any, cfg: any): any; | ||
getType(cfg: any, msg: any): any; | ||
} | ||
@@ -60,5 +60,8 @@ export declare abstract class DeleteById extends Delete { | ||
* @param cfg input configuration, not used in default implementation but in some cases you may need it. | ||
* @returns uniqueCriteria of object. | ||
* @returns uniqueCriteria of object and value of uniqueCriteria. | ||
*/ | ||
getCriteria(msg: any, cfg: any): any; | ||
getCriteria(msg: any, cfg: any): { | ||
uniqueCriteria: any; | ||
value: any; | ||
}; | ||
/** | ||
@@ -72,3 +75,3 @@ * Method must return structure with object to be deleted and number of found objects for provided uniqueCriteria. In most cases you will need just uniqueCriteria, type and cfg. | ||
*/ | ||
abstract findObjectByCriteria(criteria: void, type: any, cfg: any, msg: any): Promise<{ | ||
abstract findObjectByCriteria(criteria: any, type: any, cfg: any, msg: any): Promise<{ | ||
object: any; | ||
@@ -78,3 +81,2 @@ numberOfObjects: number; | ||
} | ||
export {}; | ||
//# sourceMappingURL=delete.d.ts.map |
@@ -11,11 +11,12 @@ "use strict"; | ||
* | ||
* @param msg input message, ot used in default implementation but in some cases you may need it. | ||
* @param cfg input configuration with objectType(in default implementation). | ||
* @param cfg input message, ot used in default implementation but in some cases you may need it. | ||
* @param msg input configuration with objectType(in default implementation). | ||
* @returns object type. | ||
*/ | ||
// @ts-ignore | ||
getType(msg, cfg) { | ||
getType(cfg, msg) { | ||
return cfg[constants_1.objectType]; | ||
} | ||
} | ||
exports.Delete = Delete; | ||
class DeleteById extends Delete { | ||
@@ -29,7 +30,7 @@ async process(msg, cfg, snapshot) { | ||
const id = this.getId(msg, cfg); | ||
const type = this.getType(msg, cfg); | ||
const type = this.getType(cfg, msg); | ||
this.logger.trace('Id: %j and type: %j of object to be deleted', id, type); | ||
const foundObject = await this.findObjectById(id, type, cfg, msg); | ||
if (foundObject.numberOfObjects > 1) { | ||
throw new Error('Found more than 1 object for provided id: ' + JSON.stringify(id)); | ||
throw new Error('Found more than 1 object for provided id: ' + JSON.stringify(id) + ' number of objects found: ' + JSON.stringify(foundObject.numberOfObjects)); | ||
} | ||
@@ -41,8 +42,13 @@ if (foundObject.numberOfObjects === 0) { | ||
} | ||
const object = { foundObject }; | ||
const { object } = foundObject; | ||
this.logger.trace('Start deleting object: %j', object); | ||
await this.deleteObject(object, cfg, msg); | ||
const deletedId = await this.deleteObject(object, cfg, msg); | ||
if (deletedId === null) { | ||
this.logger.trace('Object with ID: %j and type: %j was not deleted or not found. Returning empty object', id, type); | ||
this.logger.info('Finished processing deleteById action'); | ||
return {}; | ||
} | ||
this.logger.trace('Successfully process delete call for id: %j and type: %j', id, type); | ||
this.logger.info('Finished processing deleteById action'); | ||
return { id }; | ||
return { id: deletedId }; | ||
} | ||
@@ -75,3 +81,3 @@ catch (e) { | ||
const criteria = this.getCriteria(msg, cfg); | ||
const type = this.getType(msg, cfg); | ||
const type = this.getType(cfg, msg); | ||
this.logger.trace('Start deleting object with criteria: %j and type: %j', criteria, type); | ||
@@ -87,5 +93,10 @@ const foundObject = await this.findObjectByCriteria(criteria, type, cfg, msg); | ||
} | ||
const object = { foundObject }; | ||
const { object } = foundObject; | ||
this.logger.trace('Start deleting object: %j', object); | ||
const id = await this.deleteObject(object, cfg, msg); | ||
if (id === null) { | ||
this.logger.trace('Object with unique criteria: %j and type: %j was not deleted or not found. Returning empty object', criteria, type); | ||
this.logger.info('Finished processing deleteByUniqueCriteria action'); | ||
return {}; | ||
} | ||
this.logger.trace('Successfully process delete call for criteria: %j and type: %j', criteria, type); | ||
@@ -105,7 +116,12 @@ this.logger.info('Finished processing deleteByUniqueCriteria action'); | ||
* @param cfg input configuration, not used in default implementation but in some cases you may need it. | ||
* @returns uniqueCriteria of object. | ||
* @returns uniqueCriteria of object and value of uniqueCriteria. | ||
*/ | ||
// @ts-ignore | ||
getCriteria(msg, cfg) { | ||
return msg[constants_1.uniqueCriteria]; | ||
const fieldName = cfg[constants_1.uniqueCriteria]; | ||
const value = msg[fieldName]; | ||
return { | ||
uniqueCriteria: fieldName, | ||
value, | ||
}; | ||
} | ||
@@ -112,0 +128,0 @@ } |
import { id, objectType, uniqueCriteria } from '../constants'; | ||
abstract class Delete { | ||
export abstract class Delete { | ||
@@ -32,8 +32,8 @@ protected logger; | ||
* | ||
* @param msg input message, ot used in default implementation but in some cases you may need it. | ||
* @param cfg input configuration with objectType(in default implementation). | ||
* @param cfg input message, ot used in default implementation but in some cases you may need it. | ||
* @param msg input configuration with objectType(in default implementation). | ||
* @returns object type. | ||
*/ | ||
// @ts-ignore | ||
public getType(msg: any, cfg: any): any { | ||
public getType(cfg: any, msg: any,): any { | ||
return cfg[objectType]; | ||
@@ -52,7 +52,7 @@ } | ||
const id = this.getId(msg, cfg); | ||
const type = this.getType(msg, cfg); | ||
const type = this.getType(cfg, msg); | ||
this.logger.trace('Id: %j and type: %j of object to be deleted', id, type); | ||
const foundObject = await this.findObjectById(id, type, cfg, msg); | ||
if (foundObject.numberOfObjects > 1) { | ||
throw new Error('Found more than 1 object for provided id: ' + JSON.stringify(id)); | ||
throw new Error('Found more than 1 object for provided id: ' + JSON.stringify(id) + ' number of objects found: ' + JSON.stringify(foundObject.numberOfObjects)); | ||
} | ||
@@ -64,8 +64,13 @@ if (foundObject.numberOfObjects === 0) { | ||
} | ||
const object = { foundObject }; | ||
const { object } = foundObject; | ||
this.logger.trace('Start deleting object: %j', object); | ||
await this.deleteObject(object, cfg, msg); | ||
const deletedId = await this.deleteObject(object, cfg, msg); | ||
if (deletedId === null) { | ||
this.logger.trace('Object with ID: %j and type: %j was not deleted or not found. Returning empty object', id, type); | ||
this.logger.info('Finished processing deleteById action'); | ||
return {}; | ||
} | ||
this.logger.trace('Successfully process delete call for id: %j and type: %j', id, type); | ||
this.logger.info('Finished processing deleteById action'); | ||
return { id }; | ||
return { id: deletedId }; | ||
} catch (e) { | ||
@@ -115,3 +120,3 @@ this.logger.error('Unexpected error while processing delete call for message: %j', msg, e); | ||
const criteria = this.getCriteria(msg, cfg); | ||
const type = this.getType(msg, cfg); | ||
const type = this.getType(cfg, msg); | ||
this.logger.trace('Start deleting object with criteria: %j and type: %j', criteria, type); | ||
@@ -127,5 +132,10 @@ const foundObject = await this.findObjectByCriteria(criteria, type, cfg, msg); | ||
} | ||
const object = { foundObject }; | ||
const { object } = foundObject; | ||
this.logger.trace('Start deleting object: %j', object); | ||
const id = await this.deleteObject(object, cfg, msg); | ||
if (id === null) { | ||
this.logger.trace('Object with unique criteria: %j and type: %j was not deleted or not found. Returning empty object', criteria, type); | ||
this.logger.info('Finished processing deleteByUniqueCriteria action'); | ||
return {} | ||
} | ||
this.logger.trace('Successfully process delete call for criteria: %j and type: %j', criteria, type); | ||
@@ -146,7 +156,15 @@ this.logger.info('Finished processing deleteByUniqueCriteria action'); | ||
* @param cfg input configuration, not used in default implementation but in some cases you may need it. | ||
* @returns uniqueCriteria of object. | ||
* @returns uniqueCriteria of object and value of uniqueCriteria. | ||
*/ | ||
// @ts-ignore | ||
public getCriteria(msg: any, cfg: any): any { | ||
return msg[uniqueCriteria] | ||
public getCriteria(msg: any, cfg: any): { | ||
uniqueCriteria: any, | ||
value: any | ||
} { | ||
const fieldName = cfg[uniqueCriteria]; | ||
const value = msg[fieldName]; | ||
return { | ||
uniqueCriteria: fieldName, | ||
value, | ||
} | ||
} | ||
@@ -162,3 +180,3 @@ | ||
*/ | ||
public abstract async findObjectByCriteria(criteria: void, type: any, cfg: any, msg: any): Promise<{ | ||
public abstract async findObjectByCriteria(criteria: any, type: any, cfg: any, msg: any): Promise<{ | ||
object: any, | ||
@@ -165,0 +183,0 @@ numberOfObjects: number, |
{ | ||
"name": "@elastic.io/oih-standard-library", | ||
"version": "1.0.0-rc2", | ||
"version": "1.0.0-rc3", | ||
"description": "Library for OIH standards implementation", | ||
@@ -21,3 +21,3 @@ "author": { | ||
"scripts": { | ||
"tsc": "rm -fr out && tsc", | ||
"tsc": "rm -fr out && tsc -p ./ --outDir lib/", | ||
"pretest": "tslint -c tslint.json --project tsconfig.json lib/**/*.ts spec/**/*.ts --fix", | ||
@@ -41,2 +41,3 @@ "preparepublish": "npm run tsc", | ||
"chai": "4.2.0", | ||
"chai-as-promised": "^7.1.1", | ||
"mocha": "6.2.2", | ||
@@ -43,0 +44,0 @@ "sinon": "7.5.0", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
43135
22.87%376
10.59%111
11000%8
14.29%