Socket
Socket
Sign inDemoInstall

larvitdbmigration

Package Overview
Dependencies
Maintainers
6
Versions
170
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

larvitdbmigration - npm Package Compare versions

Comparing version 6.1.2 to 7.0.0

6

dist/dbType/elasticsearch.d.ts
import { LogInstance } from 'larvitutils';
import { Got } from 'got';
import { AxiosInstance } from 'axios';
export declare type ElasticsearchDriverOptions = {
url: string;
got: Got;
axios: AxiosInstance;
indexName: string;

@@ -35,3 +35,3 @@ context?: object;

private putVersion;
private msgFromGotException;
private msgFromAxiosException;
private runScript;

@@ -38,0 +38,0 @@ private runScripts;

@@ -6,3 +6,2 @@ "use strict";

Object.defineProperty(exports, "__esModule", { value: true });
const got_1 = require("got");
const fs_1 = __importDefault(require("fs"));

@@ -21,3 +20,3 @@ const topLogPrefix = 'larvitdbmigration: dbType/elasticsearch.js:';

constructor(options) {
for (const option of ['log', 'got', 'indexName', 'url', 'migrationScriptPath']) {
for (const option of ['log', 'axios', 'indexName', 'url', 'migrationScriptPath']) {
if (!options[option]) {

@@ -34,3 +33,3 @@ /* istanbul ignore next */

const { docUri } = this;
const { log, got } = this.options;
const { log, axios } = this.options;
const { throwHttpErrors } = options;

@@ -41,10 +40,17 @@ const logPrefix = `${topLogPrefix} getDoc() -`;

try {
const response = await got(docUri, { throwHttpErrors });
log.debug(`${logPrefix} GET ${docUri} ${response.statusCode} ${response.statusMessage}`);
if (response.statusCode === 200) {
const axiosOptions = {
responseType: 'text',
transformResponse: [(data) => data],
};
if (!throwHttpErrors) {
axiosOptions.validateStatus = () => true;
}
const response = await axios(docUri, axiosOptions);
log.debug(`${logPrefix} GET ${docUri} ${response.status} ${response.statusText}`);
if (response.status === 200) {
try {
doc = JSON.parse(response.body);
doc = JSON.parse(response.data);
}
catch (err) {
const msg = `${err}, body: ${response.body}`;
const msg = `${err}, body: ${response.data}`;
throw new Error(msg);

@@ -63,3 +69,3 @@ }

async createIndexIfNotExists() {
const { indexName, log, got, url } = this.options;
const { indexName, log, axios, url } = this.options;
const logPrefix = `${topLogPrefix} createIndexIfNotExists() - indexName: "${indexName}" -`;

@@ -70,9 +76,9 @@ const uri = `${url}/${indexName}`;

try {
const response = await got.head(uri, { throwHttpErrors: false });
if (response.statusCode === 200) {
const response = await axios.head(uri, { validateStatus: () => true });
if (response.status === 200) {
log.debug(`${logPrefix} Index already exists`);
return;
}
else if (response.statusCode !== 404) {
throw new Error(`unexpected statusCode: ${response.statusCode}`);
else if (response.status !== 404) {
throw new Error(`unexpected status code: ${response.status}`);
}

@@ -87,3 +93,3 @@ log.debug(`${logPrefix} Index does not exist, create it`);

try {
await got.put(uri);
await axios.put(uri);
log.debug(`${logPrefix} Created!`);

@@ -93,3 +99,3 @@ }

const errPrefix = `PUT ${uri} failed, err:`;
const msg = this.msgFromGotException(_err);
const msg = this.msgFromAxiosException(_err);
log.error(`${logPrefix} ${errPrefix} ${msg}`);

@@ -101,11 +107,8 @@ throw new Error(`${errPrefix} ${msg}`);

const { docUri } = this;
const { log, got } = this.options;
const { log, axios } = this.options;
const logPrefix = `${topLogPrefix} createDoc() -`;
log.debug(`${logPrefix} Create database version document: ${docUri}`);
const response = await got.post(docUri, {
json: { version: 0, status: 'finished' },
throwHttpErrors: false,
});
if (response.statusCode !== 201) {
const msg = `Failed to create version document, statusCode: ${response.statusCode}, body: ${response.body}`;
const response = await axios.post(docUri, { version: 0, status: 'finished' }, { validateStatus: () => true });
if (response.status !== 201) {
const msg = `Failed to create version document, status code: ${response.status}, body: ${response.data}`;
log.error(`${logPrefix} ${msg}`);

@@ -120,10 +123,10 @@ throw new Error(msg);

const { response } = await this.getDoc({ throwHttpErrors: false });
if (response.statusCode === 404) {
if (response.status === 404) {
await this.createDoc();
}
else if (response.statusCode === 200) {
else if (response.status === 200) {
log.debug(`${logPrefix} Database version document already exists`);
}
else {
const msg = `Unexpected statusCode when getting database version document: ${response.statusCode}, body: ${response.body}`;
const msg = `Unexpected status code when getting database version document: ${response.status}, body: ${response.data}`;
log.error(`${logPrefix} ${msg}`);

@@ -149,11 +152,11 @@ throw new Error(msg);

const { docUri } = this;
const { log, got } = this.options;
const { log, axios } = this.options;
const logPrefix = `${topLogPrefix} putVersion() - version document: "${docUri}" -`;
log.verbose(`${logPrefix} Putting version document: ${JSON.stringify(doc)}`);
try {
await got.put(docUri, { json: doc });
await axios.put(docUri, doc);
}
catch (_err) {
const errorPrefix = `PUT ${docUri} failed, err:`;
const msg = this.msgFromGotException(_err);
const msg = this.msgFromAxiosException(_err);
log.error(`${logPrefix} ${errorPrefix} ${msg}`);

@@ -163,10 +166,6 @@ throw new Error(`${errorPrefix} ${msg}`);

}
msgFromGotException(_err) {
msgFromAxiosException(_err) {
let msg = '';
if (_err instanceof got_1.HTTPError) {
if (_err instanceof Error) {
const err = _err;
msg = `Unexpected statusCode: ${err.response.statusCode}, body: ${err.response.body}`;
}
else if (_err instanceof Error) {
const err = _err;
msg = `${err.message}`;

@@ -173,0 +172,0 @@ }

@@ -1,2 +0,2 @@

import { Got } from 'got';
import { AxiosInstance } from 'axios';
import { LogInstance } from 'larvitutils';

@@ -13,3 +13,3 @@ import ElasticsearchDriver from './dbType/elasticsearch';

log?: LogInstance;
got?: Got;
axios?: AxiosInstance;
url?: string;

@@ -30,3 +30,3 @@ };

* @param {String} [options.url] - must be specified if dbType is "elasticsearch"
* @param {String} [options.got] - optional got instance to be used if dbType is "elasticsearch"
* @param {String} [options.axios] - optional axios instance to be used if dbType is "elasticsearch"
* @param {String} [options.migrationScriptPath="./dbmigration"] -

@@ -33,0 +33,0 @@ * @param {object} [options.log=instance of lutils.Log()] -

@@ -7,3 +7,3 @@ "use strict";

exports.DbMigration = void 0;
const got_1 = __importDefault(require("got"));
const axios_1 = __importDefault(require("axios"));
const larvitutils_1 = require("larvitutils");

@@ -24,3 +24,3 @@ const elasticsearch_1 = __importDefault(require("./dbType/elasticsearch"));

* @param {String} [options.url] - must be specified if dbType is "elasticsearch"
* @param {String} [options.got] - optional got instance to be used if dbType is "elasticsearch"
* @param {String} [options.axios] - optional axios instance to be used if dbType is "elasticsearch"
* @param {String} [options.migrationScriptPath="./dbmigration"] -

@@ -57,3 +57,3 @@ * @param {object} [options.log=instance of lutils.Log()] -

else {
options.got = options.got ?? got_1.default.extend();
options.axios = options.axios ?? axios_1.default.create();
this.driver = new elasticsearch_1.default(options);

@@ -60,0 +60,0 @@ log.verbose(`${logPrefix} Started with dbType: "elasticsearch", indexName: "${options.indexName}", migrationScriptPath: "${options.migrationScriptPath}"`);

{
"name": "larvitdbmigration",
"version": "6.1.2",
"version": "7.0.0",
"author": {

@@ -12,3 +12,3 @@ "name": "Mikael 'Lilleman' Göransson",

"dependencies": {
"got": "11.8.3",
"axios": "0.26.1",
"larvitutils": "5.0.4"

@@ -15,0 +15,0 @@ },

@@ -1,2 +0,2 @@

[![Build Status](https://travis-ci.org/larvit/larvitdbmigration.svg?branch=master)](https://travis-ci.org/larvit/larvitdbmigration) [![Dependencies](https://david-dm.org/larvit/larvitdbmigration.svg)](https://david-dm.org/larvit/larvitdbmigration.svg)
[![Build Status](https://github.com/larvit/larvitdbmigration/actions/workflows/ci.yml/badge.svg)](https://github.com/larvit/larvitdbmigration/actions)

@@ -67,3 +67,3 @@ # Database migration tool

migrationScriptPath: './dbmigration', // Optional
got // Optional, will use default got instance if not specified.
axios // Optional, will use default axios instance if not specified.
log // Optional, will use log.silly(), log.debug(), log.verbose(), log.info(), log.warn() and log.error() if given.

@@ -79,4 +79,2 @@ });

Retry behavior can be configured in the got instance that is passed to the constructor.
### Example migration scripts

@@ -114,3 +112,3 @@

const got = require('got');
const axios = require('axios');

@@ -122,9 +120,7 @@ exports = module.exports = async function (options) {

await got.put(`${url}/some_index/_mapping`, {
json: {
properties: {
names: {
type: 'string',
position_increment_gap: 100
}
await axios.put(`${url}/some_index/_mapping`, {
properties: {
names: {
type: 'string',
position_increment_gap: 100
}

@@ -151,2 +147,5 @@ }

## Changelog
### 7.0.0
* Replaced got with axios (latest versions of got required ES modules and adopters of this lib is not quite ready for it).
### 6.0.0

@@ -153,0 +152,0 @@ * Removed locking mechanism for Elasticsearch migrations, there is no support for it in Elasticsearch.

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

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