🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
Sign inDemoInstall
Socket

es-mapping-ts

Package Overview
Dependencies
Maintainers
2
Versions
18
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

es-mapping-ts - npm Package Compare versions

Comparing version

to
1.0.0

README.md

4

dist/es-entity.decorator.d.ts

@@ -9,5 +9,5 @@ /**

type?: string;
/** create mapping or not **/
/** create mapping or not */
readonly?: boolean;
/** add mixins **/
/** add mixins */
mixins?: any[];

@@ -14,0 +14,0 @@ }

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var es_mapping_service_1 = require("./es-mapping.service");
const es_mapping_service_1 = require("./es-mapping.service");
/**
* Argument for an elasticsearch index
*/
var EsEntityArgs = /** @class */ (function () {
function EsEntityArgs() {
}
return EsEntityArgs;
}());
class EsEntityArgs {
}
exports.EsEntityArgs = EsEntityArgs;

@@ -18,3 +15,3 @@ /**

function EsEntity(args) {
return function (target) {
return (target) => {
if (args && !args.type) {

@@ -24,4 +21,4 @@ args.type = args.index;

// Extracting possible super class from prototype
var protoType = Object.getPrototypeOf(target).name;
var superClass = null;
const protoType = Object.getPrototypeOf(target).name;
let superClass = null;
if (protoType !== '') {

@@ -28,0 +25,0 @@ superClass = protoType;

@@ -8,3 +8,3 @@ import 'reflect-metadata';

type?: string;
/** Name of the field : if it need to be different of the property name*/
/** Name of the field : if it need to be different of the property name */
name?: string;

@@ -15,3 +15,3 @@ /** Additional properties or not */

analyzer?: string;
/** Additionnal ES fields **/
/** Additionnal ES fields */
fields?: any;

@@ -18,0 +18,0 @@ /** Format */

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var es_mapping_service_1 = require("./es-mapping.service");
require("reflect-metadata");
const es_mapping_service_1 = require("./es-mapping.service");
/**
* Argument for a simple elasticsearch field
*/
var EsFieldArgs = /** @class */ (function () {
function EsFieldArgs() {
}
return EsFieldArgs;
}());
class EsFieldArgs {
}
exports.EsFieldArgs = EsFieldArgs;

@@ -19,13 +16,13 @@ /**

function EsField(args) {
return function (target, propertyKey) {
var propertyType = Reflect.getMetadata('design:type', target, propertyKey);
return (target, propertyKey) => {
let propertyType = Reflect.getMetadata('design:type', target, propertyKey);
if (args.type === 'join' && !args.relations) {
throw new Error("es-mapping-error no relations defined for join datatype : " + target.constructor.name + ":" + propertyKey);
throw new Error(`es-mapping-error no relations defined for join datatype : ${target.constructor.name}:${propertyKey}`);
}
if (args.type === 'nested') {
if (propertyType.name !== 'Array' && !propertyType.values) {
throw new Error("es-mapping-error type of a nested field must be an array : " + target.constructor.name + ":" + propertyKey);
throw new Error(`es-mapping-error type of a nested field must be an array : ${target.constructor.name}:${propertyKey}`);
}
if (!args.fieldClass) {
console.warn("es-mapping-warning no fieldClass defined for nested datatype : " + target.constructor.name + ":" + propertyKey);
console.warn(`es-mapping-warning no fieldClass defined for nested datatype : ${target.constructor.name}:${propertyKey}`);
}

@@ -35,3 +32,4 @@ }

if (propertyType.name === 'Array') {
console.warn("es-mapping-warning no fieldClass defined for object array datatype :\n " + target.constructor.name + ":" + propertyKey);
console.warn(`es-mapping-warning no fieldClass defined for object array datatype :
${target.constructor.name}:${propertyKey}`);
}

@@ -38,0 +36,0 @@ }

@@ -22,4 +22,2 @@ /**

properties: Map<string | symbol, InternalEsMappingProperty>;
constructor();
mergeEsMapping(): void;
addProperty(name: string | symbol, mapping: InternalEsMappingProperty): void;

@@ -26,0 +24,0 @@ }

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var lodash_1 = require("lodash");
const lodash_1 = require("lodash");
/**
* Base format of an elasticsearch mapping
*/
var EsMapping = /** @class */ (function () {
function EsMapping() {
class EsMapping {
constructor() {
this.body = { properties: {} };
}
return EsMapping;
}());
}
exports.EsMapping = EsMapping;

@@ -17,26 +16,15 @@ /**

*/
var InternalEsMapping = /** @class */ (function () {
function InternalEsMapping() {
class InternalEsMapping {
constructor() {
this.esmapping = new EsMapping();
this.properties = new Map();
this.esmapping = new EsMapping();
}
InternalEsMapping.prototype.mergeEsMapping = function () {
if (!this.esmapping) {
this.esmapping = new EsMapping();
}
this.esmapping.index = this.esmapping.index;
this.esmapping.type = this.esmapping.type;
};
InternalEsMapping.prototype.addProperty = function (name, mapping) {
addProperty(name, mapping) {
this.properties.set(name, mapping);
if (!mapping.propertyMapping) {
return;
}
var propertyMapping = lodash_1.cloneDeep(mapping.propertyMapping);
const propertyMapping = lodash_1.cloneDeep(mapping.propertyMapping);
// remove the name field from the es-mapping
delete propertyMapping.name;
this.esmapping.body.properties[name] = propertyMapping;
};
return InternalEsMapping;
}());
}
}
exports.InternalEsMapping = InternalEsMapping;

@@ -0,5 +1,5 @@

import { Client } from '@elastic/elasticsearch';
import { EsEntityArgs } from './es-entity.decorator';
import { EsFieldArgs } from './es-field.decorator';
import { EsMapping, InternalEsMapping } from './es-mapping';
import { EsFieldArgs } from './es-field.decorator';
import { EsEntityArgs } from './es-entity.decorator';
import { Client } from 'elasticsearch';
/**

@@ -32,7 +32,7 @@ * Service used to manage mapping loading and share it

*/
getMappings(): Array<InternalEsMapping>;
getMappings(): InternalEsMapping[];
/**
* Allow you to get all index
*/
getEsMappings(): Array<EsMapping>;
getEsMappings(): EsMapping[];
/**

@@ -53,5 +53,5 @@ * Allow you to get the generate mapping map

/**
* Alllow you to get the generated mapping eady to be inserted inside elasticsearch
* for an type
*/
* Alllow you to get the generated mapping ready to be inserted inside elasticsearch
* for an type
*/
getMappingForType(type: string): EsMapping;

@@ -58,0 +58,0 @@ /**

"use strict";
var __assign = (this && this.__assign) || Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __generator = (this && this.__generator) || function (thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (_) try {
if (f = 1, y && (t = y[op[0] & 2 ? "return" : op[0] ? "throw" : "next"]) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [0, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};
Object.defineProperty(exports, "__esModule", { value: true });
var es_mapping_1 = require("./es-mapping");
var lodash = require("lodash");
var bluebird = require("bluebird");
const es_mapping_1 = require("./es-mapping");
/**
* Service used to manage mapping loading and share it
*/
var EsMappingService = /** @class */ (function () {
function EsMappingService() {
class EsMappingService {
constructor() {
this.esMappings = new Map();

@@ -59,9 +23,9 @@ }

*/
EsMappingService.getInstance = function () {
static getInstance() {
if (!EsMappingService.instance) {
var esMappingService = new EsMappingService();
const esMappingService = new EsMappingService();
EsMappingService.instance = esMappingService;
}
return EsMappingService.instance;
};
}
/**

@@ -72,17 +36,12 @@ * Add the entity in the mapping

*/
EsMappingService.prototype.addEntity = function (args, target, superClass) {
var className = target.name;
var mapping = this.esMappings.get(className);
if (!mapping) {
mapping = new es_mapping_1.InternalEsMapping();
this.esMappings.set(className, mapping);
}
var mergeProperties = function (properties) {
for (var _i = 0, _a = Object.keys(properties); _i < _a.length; _i++) {
var propertyName = _a[_i];
var currentMappingProperty = mapping.properties.get(propertyName);
var internalProperty = null;
addEntity(args, target, superClass) {
const className = target.name;
const mapping = this.esMappings.get(className);
const mergeProperties = (properties) => {
for (const propertyName of Object.keys(properties)) {
const currentMappingProperty = mapping.properties.get(propertyName);
let internalProperty = null;
if (!currentMappingProperty) {
internalProperty = {
propertyMapping: properties[propertyName]
propertyMapping: properties[propertyName],
};

@@ -92,3 +51,3 @@ }

internalProperty = {
propertyMapping: __assign({}, properties[propertyName], currentMappingProperty.propertyMapping)
propertyMapping: Object.assign(Object.assign({}, properties[propertyName]), currentMappingProperty.propertyMapping),
};

@@ -100,3 +59,3 @@ }

if (superClass) {
var superClassMapping = this.esMappings.get(superClass);
const superClassMapping = this.esMappings.get(superClass);
if (superClassMapping) {

@@ -111,5 +70,4 @@ mergeProperties(superClassMapping.esmapping.body.properties);

if (args.mixins) {
for (var _i = 0, _a = args.mixins; _i < _a.length; _i++) {
var mixin = _a[_i];
var esEntity = this.esMappings.get(mixin.name);
for (const mixin of args.mixins) {
const esEntity = this.esMappings.get(mixin.name);
if (esEntity) {

@@ -121,4 +79,3 @@ mergeProperties(esEntity.esmapping.body.properties);

}
mapping.mergeEsMapping();
};
}
/**

@@ -130,14 +87,13 @@ * Add the field in the mapping

*/
EsMappingService.prototype.addField = function (args, target, propertyKey, propertyType) {
var className = target.constructor.name;
var mapping = this.esMappings.get(className);
addField(args, target, propertyKey, propertyType) {
const className = target.constructor.name;
let mapping = this.esMappings.get(className);
if (!mapping) {
mapping = new es_mapping_1.InternalEsMapping();
this.esMappings.set(className, mapping);
mapping.mergeEsMapping();
}
var properties = args;
const properties = args;
if (args.type === 'nested' || args.type === 'object') {
properties.type = args.type;
var esEntity = this.esMappings.get(propertyType.name);
const esEntity = this.esMappings.get(propertyType.name);
if (esEntity) {

@@ -147,28 +103,26 @@ properties.properties = esEntity.esmapping.body.properties;

}
var internalProperty = {
propertyMapping: properties
const internalProperty = {
propertyMapping: properties,
};
var propertyName = args.name || propertyKey;
const propertyName = args.name || propertyKey;
mapping.addProperty(propertyName, internalProperty);
};
}
/**
* Alllow you to get the generated mapping list ready to be inserted inside elasticsearch
*/
EsMappingService.prototype.getMappings = function () {
getMappings() {
return Array.from(this.esMappings.values());
};
}
/**
* Allow you to get all index
*/
EsMappingService.prototype.getEsMappings = function () {
return lodash.map(Array.from(this.esMappings.values()), function (mapping) {
return mapping.esmapping;
});
};
getEsMappings() {
return Array.from(this.esMappings.values()).map((mapping) => mapping.esmapping);
}
/**
* Allow you to get the generate mapping map
*/
EsMappingService.prototype.getMappingsMap = function () {
getMappingsMap() {
return this.esMappings;
};
}
/**

@@ -178,4 +132,4 @@ * Alllow you to get the generated mapping ready to be inserted inside elasticsearch

*/
EsMappingService.prototype.getMappingForClass = function (className) {
var internalMapping = this.esMappings.get(className);
getMappingForClass(className) {
const internalMapping = this.esMappings.get(className);
if (internalMapping) {

@@ -185,3 +139,3 @@ return internalMapping.esmapping;

return null;
};
}
/**

@@ -191,6 +145,5 @@ * Alllow you to get the generated mapping ready to be inserted inside elasticsearch

*/
EsMappingService.prototype.getMappingForIndex = function (indexName) {
var internalMapping = lodash.find(Array.from(this.esMappings.values()), function (internalEsMapping) {
return internalEsMapping.esmapping.index === indexName;
});
getMappingForIndex(indexName) {
const internalMapping = Array.from(this.esMappings.values())
.find((internalEsMapping) => internalEsMapping.esmapping.index === indexName);
if (internalMapping) {

@@ -200,11 +153,10 @@ return internalMapping.esmapping;

return null;
};
}
/**
* Alllow you to get the generated mapping eady to be inserted inside elasticsearch
* for an type
*/
EsMappingService.prototype.getMappingForType = function (type) {
var internalMapping = lodash.find(Array.from(this.esMappings.values()), function (internalEsMapping) {
return internalEsMapping.esmapping.type === type;
});
* Alllow you to get the generated mapping ready to be inserted inside elasticsearch
* for an type
*/
getMappingForType(type) {
const internalMapping = Array.from(this.esMappings.values())
.find((internalEsMapping) => internalEsMapping.esmapping.type === type);
if (internalMapping) {

@@ -214,78 +166,41 @@ return internalMapping.esmapping;

return null;
};
}
/**
* Allow you to get all index
*/
EsMappingService.prototype.getAllIndex = function () {
return lodash.map(lodash.filter(Array.from(this.esMappings.values()), function (mapping) {
return mapping.esmapping.index;
}), function (mapping) {
return mapping.esmapping.index;
});
};
getAllIndex() {
// load mapping with index
const mappings = Array.from(this.esMappings.values()).filter((mapping) => mapping.esmapping.index);
return mappings.map((mapping) => mapping.esmapping.index);
}
/**
* Allow to insert/update mapping into elasticsearch
*/
EsMappingService.prototype.uploadMappings = function (esclient) {
return __awaiter(this, void 0, void 0, function () {
var _this = this;
var mappings;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
mappings = EsMappingService.getInstance().getMappings();
return [4 /*yield*/, bluebird.each(mappings, function (internalMapping) { return __awaiter(_this, void 0, void 0, function () {
var esMapping, indexExist, err_1;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
if (!!internalMapping.readonly) return [3 /*break*/, 9];
esMapping = internalMapping.esmapping;
if (!esMapping.index) return [3 /*break*/, 9];
esMapping.include_type_name = true;
_a.label = 1;
case 1:
_a.trys.push([1, 8, , 9]);
// Delete readonly for ES compatibility
delete internalMapping.readonly;
return [4 /*yield*/, esclient.indices.exists({ index: esMapping.index })];
case 2:
indexExist = _a.sent();
if (!!indexExist) return [3 /*break*/, 5];
// create index
return [4 /*yield*/, esclient.indices.create({ index: esMapping.index })];
case 3:
// create index
_a.sent();
// create mapping
return [4 /*yield*/, esclient.indices.putMapping(esMapping)];
case 4:
// create mapping
_a.sent();
return [3 /*break*/, 7];
case 5:
// update mapping
return [4 /*yield*/, esclient.indices.putMapping(esMapping)];
case 6:
// update mapping
_a.sent();
_a.label = 7;
case 7: return [3 /*break*/, 9];
case 8:
err_1 = _a.sent();
console.error("Something went wrong when trying to upload mapping for " + esMapping.index, err_1);
throw err_1;
case 9: return [2 /*return*/];
}
});
}); })];
case 1:
_a.sent();
return [2 /*return*/];
uploadMappings(esclient) {
return __awaiter(this, void 0, void 0, function* () {
const mappings = EsMappingService.getInstance().getMappings();
yield Promise.all(mappings.map((internalMapping) => __awaiter(this, void 0, void 0, function* () {
if (!internalMapping.readonly) {
const esMapping = internalMapping.esmapping;
if (esMapping.index) {
esMapping.include_type_name = true;
// Delete readonly for ES compatibility
delete internalMapping.readonly;
const indexExist = yield esclient.indices.exists({ index: esMapping.index });
if (!indexExist.body) {
// create index
yield esclient.indices.create({ index: esMapping.index });
// create mapping
yield esclient.indices.putMapping(esMapping);
}
else {
// update mapping
yield esclient.indices.putMapping(esMapping);
}
}
}
});
})));
});
};
return EsMappingService;
}());
}
}
exports.EsMappingService = EsMappingService;
{
"name": "es-mapping-ts",
"version": "0.0.16",
"description": "ES Mapping TypeScript",
"version": "1.0.0",
"description": "Eleasticsearch mapping manager typescript",
"main": "./dist/index.js",

@@ -11,18 +11,17 @@ "types": "./dist/index.d.ts",

"lint:fix": "tslint --project tsconfig.json --fix",
"test": "node_modules/.bin/jest --colors --no-cache --config=./jest.it.config.js --runInBand --coverage",
"test": "node_modules/.bin/jest --colors --no-cache --config=./jest.config.js --runInBand --coverage",
"coveralls": "cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js"
},
"dependencies": {
"lodash.clonedeep": "^4.5.0",
"reflect-metadata": "^0.1.12"
},
"peerDependencies": {
"bluebird": "^3.5.1",
"elasticsearch": "^16.3.0",
"lodash": "^4.17.5"
"@elastic/elasticsearch": "^7.5.0"
},
"devDependencies": {
"@elastic/elasticsearch": "^7.5.0",
"@types/bluebird": "^3.5.20",
"@types/elasticsearch": "^5.0.34",
"@types/jest": "^21.1.8",
"@types/lodash": "^4.14.105",
"@types/jest": "^24.0.22",
"@types/lodash": "^4.14.149",
"@types/node": "^10.7.1",

@@ -32,3 +31,2 @@ "bluebird": "^3.5.1",

"cross-env": "^5.1.3",
"elasticsearch": "^16.3.0",
"gulp": "^3.9.1",

@@ -38,10 +36,9 @@ "gulp-clean": "^0.3.2",

"gulp-typescript": "^3.2.3",
"jest": "^21.1.8",
"jest": "^24.9.0",
"mocha": "^5.0.1",
"nyc": "^11.4.1",
"ts-jest": "^21.2.4",
"ts-jest": "^24.2.0",
"ts-node": "^3.3.0",
"tslint": "5.3.2",
"typescript": "^2.6.2",
"typescript-babel-jest": "^1.0.5"
"typescript": "^3.7.3"
},

@@ -55,14 +52,5 @@ "keywords": [

],
"jest": {
"moduleFileExtensions": [
"js",
"json",
"ts"
],
"rootDir": "src",
"testRegex": ".spec.ts$",
"transform": {
"^.+\\.(t|j)s$": "ts-jest"
},
"coverageDirectory": "../coverage"
"author": {
"name": "xrobert35",
"email": "xavier.robert972@gmail.com"
},

@@ -73,4 +61,3 @@ "repository": {

},
"author": "xrobert",
"license": "MIT"
}

Sorry, the diff of this file is not supported yet