@elastic.io/oih-standard-library
Advanced tools
Comparing version
@@ -71,3 +71,3 @@ "use strict"; | ||
if (foundObject.numberOfObjects > 1) { | ||
throw new Error('Found more than 1 object for provided criteria: ' + JSON.stringify(criteria)); | ||
throw new Error(`Found more than 1 object for provided criteria: ${JSON.stringify(criteria)}`); | ||
} | ||
@@ -108,4 +108,4 @@ if (foundObject.numberOfObjects === 0) { | ||
return { | ||
value, | ||
uniqueCriteria: fieldName, | ||
value, | ||
}; | ||
@@ -112,0 +112,0 @@ } |
@@ -5,7 +5,7 @@ import { id, objectType, uniqueCriteria } from '../constants'; | ||
protected logger; | ||
protected logger; | ||
public constructor(logger: any) { | ||
this.logger = logger; | ||
} | ||
public constructor(logger: any) { | ||
this.logger = logger; | ||
} | ||
@@ -19,3 +19,3 @@ /** | ||
*/ | ||
public abstract async process(msg, cfg, snapshot): Promise<any>; | ||
public abstract async process(msg, cfg, snapshot): Promise<any>; | ||
@@ -29,3 +29,3 @@ /** | ||
*/ | ||
public abstract async deleteObject(object, cfg, msg): Promise<any>; | ||
public abstract async deleteObject(object, cfg, msg): Promise<any>; | ||
@@ -40,5 +40,5 @@ /** | ||
// @ts-ignore | ||
public getType(cfg: any, msg: any,): any { | ||
return cfg[objectType]; | ||
} | ||
public getType(cfg: any, msg: any): any { | ||
return cfg[objectType]; | ||
} | ||
} | ||
@@ -48,27 +48,26 @@ | ||
public async process(msg, cfg, snapshot): Promise<any> { | ||
try { | ||
this.logger.info('Starting processing deleteById action'); | ||
this.logger.trace('Incoming configuration: %j', cfg); | ||
this.logger.trace('Incoming message: %j', msg); | ||
this.logger.trace('Incoming snapshot: %j', snapshot); | ||
const id = this.getId(msg, cfg); | ||
const type = this.getType(cfg, msg); | ||
this.logger.trace('Id: %j and type: %j of object to be deleted', id, type); | ||
const deletedId = await this.deleteObject(id, 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: deletedId }; | ||
} catch (e) { | ||
this.logger.error('Unexpected error while processing delete call for message: %j', msg, e); | ||
throw e; | ||
} | ||
public async process(msg, cfg, snapshot): Promise<any> { | ||
try { | ||
this.logger.info('Starting processing deleteById action'); | ||
this.logger.trace('Incoming configuration: %j', cfg); | ||
this.logger.trace('Incoming message: %j', msg); | ||
this.logger.trace('Incoming snapshot: %j', snapshot); | ||
const id = this.getId(msg, cfg); | ||
const type = this.getType(cfg, msg); | ||
this.logger.trace('Id: %j and type: %j of object to be deleted', id, type); | ||
const deletedId = await this.deleteObject(id, 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: deletedId }; | ||
} catch (e) { | ||
this.logger.error('Unexpected error while processing delete call for message: %j', msg, e); | ||
throw e; | ||
} | ||
} | ||
/** | ||
@@ -82,45 +81,44 @@ * Default implementation expects id to be present inside input message `id` param. If you need other behaviour override this method. | ||
// @ts-ignore | ||
public getId(msg: any, cfg: any): any { | ||
return msg[id] | ||
} | ||
public getId(msg: any, cfg: any): any { | ||
return msg[id]; | ||
} | ||
} | ||
export abstract class DeleteByUniqueCriteria extends Delete { | ||
export abstract class DeleteByUniqueCriteria extends Delete { | ||
public async process(msg, cfg, snapshot): Promise<any> { | ||
try { | ||
this.logger.info('Start processing deleteByUniqueCriteria action'); | ||
this.logger.trace('Incoming configuration: %j', cfg); | ||
this.logger.trace('Incoming message: %j', msg); | ||
this.logger.trace('Incoming snapshot: %j', snapshot); | ||
const criteria = this.getCriteria(msg, cfg); | ||
const type = this.getType(cfg, msg); | ||
this.logger.trace('Start deleting object with criteria: %j and type: %j', criteria, type); | ||
const foundObject = await this.findObjectByCriteria(criteria, type, cfg, msg); | ||
if (foundObject.numberOfObjects > 1) { | ||
throw new Error('Found more than 1 object for provided criteria: ' + JSON.stringify(criteria)); | ||
} | ||
if (foundObject.numberOfObjects === 0) { | ||
this.logger.trace('No objects for criteria: %j and type: %j, were found returning empty object', criteria, type); | ||
this.logger.info('Finished processing deleteByUniqueCriteria action'); | ||
return {}; | ||
} | ||
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); | ||
this.logger.info('Finished processing deleteByUniqueCriteria action'); | ||
return { id }; | ||
} catch (e) { | ||
this.logger.error('Unexpected error while processing delete call for message: %j', msg, e); | ||
throw e; | ||
} | ||
public async process(msg, cfg, snapshot): Promise<any> { | ||
try { | ||
this.logger.info('Start processing deleteByUniqueCriteria action'); | ||
this.logger.trace('Incoming configuration: %j', cfg); | ||
this.logger.trace('Incoming message: %j', msg); | ||
this.logger.trace('Incoming snapshot: %j', snapshot); | ||
const criteria = this.getCriteria(msg, cfg); | ||
const type = this.getType(cfg, msg); | ||
this.logger.trace('Start deleting object with criteria: %j and type: %j', criteria, type); | ||
const foundObject = await this.findObjectByCriteria(criteria, type, cfg, msg); | ||
if (foundObject.numberOfObjects > 1) { | ||
throw new Error(`Found more than 1 object for provided criteria: ${JSON.stringify(criteria)}`); | ||
} | ||
if (foundObject.numberOfObjects === 0) { | ||
this.logger.trace('No objects for criteria: %j and type: %j, were found returning empty object', criteria, type); | ||
this.logger.info('Finished processing deleteByUniqueCriteria action'); | ||
return {}; | ||
} | ||
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); | ||
this.logger.info('Finished processing deleteByUniqueCriteria action'); | ||
return { id }; | ||
} catch (e) { | ||
this.logger.error('Unexpected error while processing delete call for message: %j', msg, e); | ||
throw e; | ||
} | ||
} | ||
/** | ||
@@ -134,13 +132,13 @@ * Default implementation expects unique criteria to be present inside input message `uniqueCriteria` param. If you need other behaviour override this method. | ||
// @ts-ignore | ||
public getCriteria(msg: any, cfg: any): { | ||
uniqueCriteria: any, | ||
value: any | ||
} { | ||
const fieldName = cfg[uniqueCriteria]; | ||
const value = msg[fieldName]; | ||
return { | ||
uniqueCriteria: fieldName, | ||
value, | ||
} | ||
} | ||
public getCriteria(msg: any, cfg: any): { | ||
uniqueCriteria: any, | ||
value: any, | ||
} { | ||
const fieldName = cfg[uniqueCriteria]; | ||
const value = msg[fieldName]; | ||
return { | ||
value, | ||
uniqueCriteria: fieldName, | ||
}; | ||
} | ||
@@ -155,7 +153,6 @@ /** | ||
*/ | ||
public abstract async findObjectByCriteria(criteria: any, type: any, cfg: any, msg: any): Promise<{ | ||
object: any, | ||
numberOfObjects: number, | ||
}>; | ||
public abstract async findObjectByCriteria(criteria: any, type: any, cfg: any, msg: any): Promise<{ | ||
object: any, | ||
numberOfObjects: number, | ||
}>; | ||
} | ||
{ | ||
"name": "@elastic.io/oih-standard-library", | ||
"version": "1.0.0-rc4", | ||
"version": "1.0.0-rc5", | ||
"description": "Library for OIH standards implementation", | ||
@@ -22,3 +22,2 @@ "author": { | ||
"tsc": "rm -fr out && tsc -p ./ --outDir lib/", | ||
"pretest": "tslint -c tslint.json --project tsconfig.json lib/**/*.ts spec/**/*.ts --fix", | ||
"preparepublish": "npm run tsc", | ||
@@ -25,0 +24,0 @@ "prepublishOnly": "tsc -p ./ --outDir lib/", |
@@ -24,7 +24,7 @@ [](https://circleci.com/gh/elasticio/oih-standard-library) | ||
Note: | ||
* If more than 1 object was found with same id, error would be thrown by this implementation | ||
* Its your responsibility to ensure that deleteObject deletes only 1 object. This implementation assume that `id` is unique for `objectType`. | ||
* If `deleteObject` method is returning `null` empty object would be emitted. You can indicate with `null` that object hasn`t been deleted or found. | ||
1. Create Action | ||
2. Create class that extends DeleteById class | ||
3. Override `findObjectById` and `deleteObject()` methods. Example below. | ||
3. Override `deleteObject()` methods. Example below. | ||
4. Optional override `getType` and `getId` methods. Default implementation expects `objectType` to be present in input configuration and `id` in input message. | ||
@@ -41,14 +41,7 @@ 5. Create instance of your class and pass `logger` to constructor. `const myDeleteByIDImpl = new MyDeleteByIdImpl(logger);` | ||
async findObjectById(id, type, cfg, msg) { // In most cases you need just id, type and cfg | ||
const object = await findObjectInAPI(id, type, cfg, msg); // objects that match this id | ||
const numberOfObjects = object.count; // You do not need to check or throw error in this case, action implementation will do this | ||
return { // return structure must contain object and numberOfObjects found | ||
object, | ||
numberOfObjects | ||
} | ||
async deleteObject(id, cfg, msg) { // In most cases you need just id, type and cfg | ||
const deletedID = await deleteObjectInAPI(id, cfg, msg); // Delete object | ||
return deletedID; // Return deleted object ID. If you return null, empty object would be emitted. | ||
} | ||
async deleteObject(object, cfg, msg) { // In most cases you need just object, type and cfg | ||
const deletedID = await deleteObjectInAPI(object, cfg, msg); // Delete object | ||
return deletedID; // Return deleted object ID if you return null, empty object would be emitted. | ||
} | ||
// You can also override getType and getId methods if needed | ||
@@ -55,0 +48,0 @@ } |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
38827
-2.39%104
-6.31%