Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@12stonechurch/omnihive-worker-elastic

Package Overview
Dependencies
Maintainers
2
Versions
169
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@12stonechurch/omnihive-worker-elastic - npm Package Compare versions

Comparing version 1.3.15951 to 1.3.22938

487

index.js
"use strict";
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) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });

@@ -20,269 +29,301 @@ exports.ElasticWorkerMetadata = void 0;

}
async init(config) {
try {
await AwaitHelper_1.AwaitHelper.execute(super.init(config));
const metadata = this.checkObjectStructure(ElasticWorkerMetadata, config.metadata);
this.client = new elasticsearch_1.Client({
cloud: {
id: metadata.cloudId,
},
auth: {
username: metadata.username,
password: metadata.password,
},
});
}
catch (err) {
throw new Error(JSON.stringify(serialize_error_1.serializeError(err)));
}
init(config) {
const _super = Object.create(null, {
init: { get: () => super.init }
});
return __awaiter(this, void 0, void 0, function* () {
try {
yield AwaitHelper_1.AwaitHelper.execute(_super.init.call(this, config));
const metadata = this.checkObjectStructure(ElasticWorkerMetadata, config.metadata);
this.client = new elasticsearch_1.Client({
cloud: {
id: metadata.cloudId,
},
auth: {
username: metadata.username,
password: metadata.password,
},
});
}
catch (err) {
throw new Error(JSON.stringify(serialize_error_1.serializeError(err)));
}
});
}
async search(index, query, fields, page = 0, limit = 100) {
try {
const indexExists = await this.validateIndex(index, true);
if (this.client && indexExists) {
return (await this.client?.search({
index: index,
body: {
from: page * limit,
size: limit,
query: {
multi_match: {
query: query,
fuzziness: "auto",
type: "most_fields",
fields: fields?.map((field) => `${field.name}^${field.weight}`),
search(index, query, fields, page = 0, limit = 100) {
var _a;
return __awaiter(this, void 0, void 0, function* () {
try {
const indexExists = yield this.validateIndex(index, true);
if (this.client && indexExists) {
return (yield ((_a = this.client) === null || _a === void 0 ? void 0 : _a.search({
index: index,
body: {
from: page * limit,
size: limit,
query: {
multi_match: {
query: query,
fuzziness: "auto",
type: "most_fields",
fields: fields === null || fields === void 0 ? void 0 : fields.map((field) => `${field.name}^${field.weight}`),
},
},
},
},
})).body;
}))).body;
}
else if (!indexExists) {
throw new Error("Index does not exist");
}
else {
throw new Error("Elastic Client not initialized");
}
}
else if (!indexExists) {
throw new Error("Index does not exist");
catch (err) {
throw new Error(JSON.stringify(serialize_error_1.serializeError(err)));
}
else {
throw new Error("Elastic Client not initialized");
});
}
create(index, idFieldName, data) {
var _a;
return __awaiter(this, void 0, void 0, function* () {
try {
if (this.client) {
const indexExists = yield this.validateIndex(index);
if (indexExists) {
yield ((_a = this.client) === null || _a === void 0 ? void 0 : _a.index({
index: index,
id: data[idFieldName].toString(),
op_type: "create",
refresh: true,
body: data,
}));
}
}
else {
throw new Error("Elastic Client not initialized");
}
}
}
catch (err) {
throw new Error(JSON.stringify(serialize_error_1.serializeError(err)));
}
catch (err) {
throw new Error(JSON.stringify(serialize_error_1.serializeError(err)));
}
});
}
async create(index, idFieldName, data) {
try {
if (this.client) {
const indexExists = await this.validateIndex(index);
if (indexExists) {
await this.client?.index({
index: index,
id: data[idFieldName].toString(),
op_type: "create",
refresh: true,
body: data,
});
update(index, id, data, upsert) {
var _a;
return __awaiter(this, void 0, void 0, function* () {
try {
if (this.client) {
const indexExists = yield this.validateIndex(index);
if (indexExists) {
yield ((_a = this.client) === null || _a === void 0 ? void 0 : _a.update({
index: index,
id: id,
body: {
doc: data,
doc_as_upsert: upsert,
},
}));
}
}
else {
throw new Error("Elastic Client not initialized");
}
}
else {
throw new Error("Elastic Client not initialized");
catch (err) {
throw new Error(JSON.stringify(serialize_error_1.serializeError(err)));
}
}
catch (err) {
throw new Error(JSON.stringify(serialize_error_1.serializeError(err)));
}
});
}
async update(index, id, data, upsert) {
try {
if (this.client) {
const indexExists = await this.validateIndex(index);
if (indexExists) {
await this.client?.update({
bulkUpdate(index, idObject, data) {
var _a;
return __awaiter(this, void 0, void 0, function* () {
try {
const indexExists = yield this.validateIndex(index, true);
if (this.client && indexExists) {
const updateQuery = {};
updateQuery[idObject.name] = idObject.values;
(_a = this.client) === null || _a === void 0 ? void 0 : _a.updateByQuery({
index: index,
id: id,
refresh: true,
body: {
doc: data,
doc_as_upsert: upsert,
query: {
bool: {
must: [
{
terms: { updateQuery },
},
],
},
},
},
});
}
else if (!indexExists) {
return;
}
else {
throw new Error("Elastic Client not initialized");
}
}
else {
throw new Error("Elastic Client not initialized");
catch (err) {
throw new Error(JSON.stringify(serialize_error_1.serializeError(err)));
}
}
catch (err) {
throw new Error(JSON.stringify(serialize_error_1.serializeError(err)));
}
});
}
async bulkUpdate(index, idObject, data) {
try {
const indexExists = await this.validateIndex(index, true);
if (this.client && indexExists) {
const updateQuery = {};
updateQuery[idObject.name] = idObject.values;
this.client?.updateByQuery({
index: index,
refresh: true,
body: {
doc: data,
query: {
bool: {
must: [
{
terms: { updateQuery },
},
],
},
},
},
});
delete(index, id) {
var _a;
return __awaiter(this, void 0, void 0, function* () {
try {
const indexExists = yield this.validateIndex(index, true);
if (this.client && indexExists) {
yield ((_a = this.client) === null || _a === void 0 ? void 0 : _a.delete({
index: index,
id: id,
refresh: true,
}));
}
else if (!indexExists) {
return;
}
else {
throw new Error("Elastic Client not initialized");
}
}
else if (!indexExists) {
return;
catch (err) {
throw new Error(JSON.stringify(serialize_error_1.serializeError(err)));
}
else {
throw new Error("Elastic Client not initialized");
}
}
catch (err) {
throw new Error(JSON.stringify(serialize_error_1.serializeError(err)));
}
});
}
async delete(index, id) {
try {
const indexExists = await this.validateIndex(index, true);
if (this.client && indexExists) {
await this.client?.delete({
index: index,
id: id,
refresh: true,
});
}
else if (!indexExists) {
return;
}
else {
throw new Error("Elastic Client not initialized");
}
}
catch (err) {
throw new Error(JSON.stringify(serialize_error_1.serializeError(err)));
}
}
async removeUnused(index, fieldName, usedKeys) {
try {
const indexExists = await this.validateIndex(index, true);
if (this.client && indexExists) {
const deleteObject = {};
deleteObject[fieldName] = usedKeys;
await this.client?.deleteByQuery({
index: index,
body: {
query: {
bool: {
must_not: [
{
terms: deleteObject,
},
],
removeUnused(index, fieldName, usedKeys) {
var _a;
return __awaiter(this, void 0, void 0, function* () {
try {
const indexExists = yield this.validateIndex(index, true);
if (this.client && indexExists) {
const deleteObject = {};
deleteObject[fieldName] = usedKeys;
yield ((_a = this.client) === null || _a === void 0 ? void 0 : _a.deleteByQuery({
index: index,
body: {
query: {
bool: {
must_not: [
{
terms: deleteObject,
},
],
},
},
},
},
});
}));
}
else if (!indexExists) {
return;
}
else {
throw new Error("Elastic Client not initialized");
}
}
else if (!indexExists) {
return;
catch (err) {
throw new Error(JSON.stringify(serialize_error_1.serializeError(err)));
}
else {
throw new Error("Elastic Client not initialized");
}
}
catch (err) {
throw new Error(JSON.stringify(serialize_error_1.serializeError(err)));
}
});
}
async upsert(index, idName, idList, data) {
try {
if (this.client) {
await this.validateIndex(index);
for (const id of idList) {
const idData = data.find((x) => x[idName].toString() === id);
await AwaitHelper_1.AwaitHelper.execute(this.update(index, id, idData, true));
upsert(index, idName, idList, data) {
return __awaiter(this, void 0, void 0, function* () {
try {
if (this.client) {
yield this.validateIndex(index);
for (const id of idList) {
const idData = data.find((x) => x[idName].toString() === id);
yield AwaitHelper_1.AwaitHelper.execute(this.update(index, id, idData, true));
}
return;
}
return;
else {
throw new Error("Elastic Client not initialized");
}
}
else {
throw new Error("Elastic Client not initialized");
catch (err) {
throw new Error(JSON.stringify(serialize_error_1.serializeError(err)));
}
}
catch (err) {
throw new Error(JSON.stringify(serialize_error_1.serializeError(err)));
}
});
}
async validateIndex(index, noCreate = false) {
try {
if (this.client) {
const indexExists = (await this.client.indices.exists({ index: index })).body;
if (!indexExists && !noCreate) {
await this.client?.indices.create({ index: index });
return true;
validateIndex(index, noCreate = false) {
var _a;
return __awaiter(this, void 0, void 0, function* () {
try {
if (this.client) {
const indexExists = (yield this.client.indices.exists({ index: index })).body;
if (!indexExists && !noCreate) {
yield ((_a = this.client) === null || _a === void 0 ? void 0 : _a.indices.create({ index: index }));
return true;
}
return indexExists;
}
return indexExists;
else {
throw new Error("Elastic Client not initialized");
}
}
else {
throw new Error("Elastic Client not initialized");
catch (err) {
throw new Error(JSON.stringify(serialize_error_1.serializeError(err)));
}
}
catch (err) {
throw new Error(JSON.stringify(serialize_error_1.serializeError(err)));
}
});
}
async deleteIndex(index) {
try {
const indexExists = await this.validateIndex(index, true);
if (this.client && indexExists) {
this.client.indices.delete({ index: index });
deleteIndex(index) {
return __awaiter(this, void 0, void 0, function* () {
try {
const indexExists = yield this.validateIndex(index, true);
if (this.client && indexExists) {
this.client.indices.delete({ index: index });
}
else if (!indexExists) {
return;
}
else {
throw new Error("Elastic Client not initialized");
}
}
else if (!indexExists) {
return;
catch (err) {
throw new Error(JSON.stringify(serialize_error_1.serializeError(err)));
}
else {
throw new Error("Elastic Client not initialized");
}
}
catch (err) {
throw new Error(JSON.stringify(serialize_error_1.serializeError(err)));
}
});
}
async fixDateMappings(index, data) {
try {
if (this.client) {
const dateMappings = {};
for (const key in data) {
const mappings = await this.client.indices.getMapping({ index: index });
if (data[key] &&
typeof data[key] === "string" &&
data[key].search(/\d{4}-[01]\d-[0-3]\dT[0-2]\d:[0-5]\d:[0-5]\d\.\d+([+-][0-2]\d:[0-5]\d|Z)/g) >=
0 &&
mappings.body[index].mappings.properties[key].format !== "yyyy-MM-dd'T'HH:mm:ss.SSSz") {
dateMappings[key] = {
type: "date",
format: "yyyy-MM-dd'T'HH:mm:ss.SSSz",
};
fixDateMappings(index, data) {
return __awaiter(this, void 0, void 0, function* () {
try {
if (this.client) {
const dateMappings = {};
for (const key in data) {
const mappings = yield this.client.indices.getMapping({ index: index });
if (data[key] &&
typeof data[key] === "string" &&
data[key].search(/\d{4}-[01]\d-[0-3]\dT[0-2]\d:[0-5]\d:[0-5]\d\.\d+([+-][0-2]\d:[0-5]\d|Z)/g) >=
0 &&
mappings.body[index].mappings.properties[key].format !== "yyyy-MM-dd'T'HH:mm:ss.SSSz") {
dateMappings[key] = {
type: "date",
format: "yyyy-MM-dd'T'HH:mm:ss.SSSz",
};
}
}
if (Object.keys(dateMappings).length > 0) {
yield this.client.indices.putMapping({
index: index,
body: {
properties: dateMappings,
},
});
}
}
if (Object.keys(dateMappings).length > 0) {
await this.client.indices.putMapping({
index: index,
body: {
properties: dateMappings,
},
});
else {
throw new Error("Elastic Client not initialized");
}
}
else {
throw new Error("Elastic Client not initialized");
catch (err) {
throw new Error(JSON.stringify(serialize_error_1.serializeError(err)));
}
}
catch (err) {
throw new Error(JSON.stringify(serialize_error_1.serializeError(err)));
}
});
}

@@ -289,0 +330,0 @@ }

{
"name": "@12stonechurch/omnihive-worker-elastic",
"version": "1.3.15951",
"version": "1.3.22938",
"description": "OmniHive Elastic Search Worker",

@@ -28,3 +28,3 @@ "license": "MIT",

"@elastic/elasticsearch": "7.11.0",
"@withonevision/omnihive-core": "1.3.15951",
"@withonevision/omnihive-core": "4.5.2",
"serialize-error": "8.0.1"

@@ -31,0 +31,0 @@ },

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc