Skip to content

Commit

Permalink
Merge pull request #198 from kshitish182/master
Browse files Browse the repository at this point in the history
Upgrade `knex(v2.4.2)` and `typescript(v5.0.4)` to latest version
  • Loading branch information
mesaugat authored Apr 28, 2023
2 parents 3b93c8d + 2a5a29e commit 31d783e
Show file tree
Hide file tree
Showing 27 changed files with 158 additions and 914 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,6 @@
/tmp
node_modules
/coverage

# IDE settings
.vscode
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -240,12 +240,12 @@ import { synchronize, loadConfig, resolveConnections } from '@leapfrogtechnology
You can also pass your own database connection (eg: Knex connection) instead of resolving `connections.sync-db.json` file.

```ts
import * as Knex from 'knex';
import { knex } from 'knex';
import { synchronize, loadConfig } from '@leapfrogtechnology/sync-db';

(async () => {
const config = await loadConfig(); // Load sync-db.yml
const connection = Knex({
const connection = knex({
// Your Knex connection instance.
client: 'mssql',
connection: {
Expand Down
2 changes: 1 addition & 1 deletion assets/templates/migration/create_ts.stub
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as Knex from 'knex';
import { Knex } from 'knex';

/**
* Create {{table}} table.
Expand Down
2 changes: 1 addition & 1 deletion assets/templates/migration/update_ts.stub
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as Knex from 'knex';
import { Knex } from 'knex';

/**
* Alter {{table}} table.
Expand Down
2 changes: 1 addition & 1 deletion examples/node-app-mssql-ts/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const { connections } = require('../connections.sync-db.json');
// Getting knex instance of mssql database with id db1.
const db = knex({
client: 'mssql',
connection: connections.find(({ id }) => id === 'testdb1').connection
connection: connections.find(({ id }) => id === 'db1').connection
});

const tasks = await db.raw('SELECT * FROM utils.vw_tasks');
Expand Down
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@leapfrogtechnology/sync-db",
"description": "Command line utility to synchronize and version control relational database objects across databases",
"version": "1.2.1",
"version": "2.0.0",
"license": "MIT",
"main": "lib/index.js",
"types": "lib/index.d.ts",
Expand Down Expand Up @@ -62,8 +62,8 @@
"debug": "^4.1.1",
"esm": "^3.2.25",
"globby": "^10.0.2",
"knex": "^0.20.11",
"ramda": "^0.26.1",
"knex": "^2.4.2",
"ramda": "^0.29.0",
"ts-node": "^8",
"tslib": "^1",
"yamljs": "^0.3.0"
Expand All @@ -78,7 +78,7 @@
"@types/debug": "^4.1.4",
"@types/mocha": "^5",
"@types/node": "^10",
"@types/ramda": "^0.26.12",
"@types/ramda": "^0.29.0",
"@types/yamljs": "^0.2.30",
"chai": "^4.2.0",
"chai-as-promised": "^7.1.1",
Expand All @@ -90,7 +90,7 @@
"prettier": "2.0.2",
"tslint": "^6.1.3",
"tslint-config-leapfrog": "^1.0.3",
"typescript": "^3.8.2"
"typescript": "^5.0.2"
},
"engines": {
"node": ">= 14.15.0"
Expand Down
7 changes: 4 additions & 3 deletions src/config.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Knex } from 'knex';
import * as path from 'path';
import * as yaml from 'yamljs';
import { mergeDeepRight } from 'ramda';
Expand Down Expand Up @@ -58,7 +59,7 @@ export async function loadConfig(configFilename: string = CONFIG_FILENAME): Prom

log('Resolved config file.');

const loaded = mergeDeepRight(DEFAULT_CONFIG, loadedConfig) as Configuration;
const loaded = mergeDeepRight<Configuration, Configuration>(DEFAULT_CONFIG, loadedConfig);

validate(loaded);

Expand Down Expand Up @@ -130,8 +131,8 @@ export async function resolveConnections(config: Configuration, resolver?: strin
id,
client,
connection: {
host: (connection as any).host,
database: (connection as any).database
host: (connection as Knex.ConnectionConfig).host,
database: (connection as Knex.ConnectionConfig).database
}
}))
);
Expand Down
2 changes: 1 addition & 1 deletion src/domain/ConnectionConfig.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as Knex from 'knex';
import { Knex } from 'knex';

/**
* Database connection configuration.
Expand Down
2 changes: 1 addition & 1 deletion src/domain/ConnectionReference.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as Knex from 'knex';
import { Knex } from 'knex';

/**
* Connection reference.
Expand Down
2 changes: 1 addition & 1 deletion src/domain/MigrationContext.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as Knex from 'knex';
import { Knex } from 'knex';

import OperationParams from './operation/OperationParams';
import OperationContext from './operation/OperationContext';
Expand Down
2 changes: 1 addition & 1 deletion src/domain/RawBingingParams.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as Knex from 'knex';
import { Knex } from 'knex';

type Value = Date | string | number | boolean | Date[] | string[] | number[] | boolean[] | Buffer | Knex.Raw;

Expand Down
2 changes: 1 addition & 1 deletion src/domain/SynchronizeContext.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as Knex from 'knex';
import { Knex } from 'knex';

import SynchronizeParams from './SynchronizeParams';
import OperationContext from './operation/OperationContext';
Expand Down
4 changes: 2 additions & 2 deletions src/init.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as Knex from 'knex';
import { Knex } from 'knex';

import { log } from './util/logger';
import { validate, isCLI } from './config';
Expand Down Expand Up @@ -37,7 +37,7 @@ export async function prepare(config: Configuration, options: PrepareOptions): P
return {
knexMigrationConfig: (connectionId: string) => ({
tableName: config.migration.tableName,
migrationSource: migrationContext ? new KnexMigrationSource(migrationContext.bind(connectionId)) : null
migrationSource: migrationContext ? new KnexMigrationSource(migrationContext.bind(connectionId)) : undefined
})
};
}
11 changes: 7 additions & 4 deletions src/migration/KnexMigrationSource.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { Knex } from 'knex';
import { dbLogger } from '../util/logger';
import MigrationRunner from './domain/MigrationRunner';
import MigrationSourceContext from './domain/MigrationSourceContext';

/**
Expand All @@ -7,7 +9,7 @@ import MigrationSourceContext from './domain/MigrationSourceContext';
*
* Reference: http://knexjs.org/#Migrations-API
*/
class KnexMigrationSource {
class KnexMigrationSource implements Knex.MigrationSource<string> {
private migrationContext: MigrationSourceContext;
private log: debug.Debugger;

Expand Down Expand Up @@ -47,15 +49,16 @@ class KnexMigrationSource {
/**
* Get the migration runner.
*
* @param {SqlMigrationEntry} migration
* @param {string} name
* @returns {Promise<MigrationRunner>}
*/
getMigration(name: string) {
getMigration(name: string): Promise<MigrationRunner> {
const migration = this.migrationContext.get(name);

this.log(`getMigration - ${name}`);
this.log(`getMigration - resolve: %o`, migration);

return migration;
return Promise.resolve(migration);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/migration/domain/JavaScriptMigrationEntry.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Knex from 'knex';
import { Knex } from 'knex';

interface JavaScriptMigrationEntry {
name: string;
Expand Down
2 changes: 1 addition & 1 deletion src/migration/domain/MigrationRunner.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Knex from 'knex';
import { Knex } from 'knex';

/**
* Contract for a migration runner.
Expand Down
2 changes: 1 addition & 1 deletion src/migration/service/knexMigrator.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as Knex from 'knex';
import { Knex } from 'knex';
import * as path from 'path';

import { PrepareOptions } from '../../init';
Expand Down
2 changes: 1 addition & 1 deletion src/migration/source-types/SqlMigrationSourceContext.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as Knex from 'knex';
import { Knex } from 'knex';

import MigrationRunner from '../domain/MigrationRunner';
import { dbLogger, log as logger } from '../../util/logger';
Expand Down
8 changes: 7 additions & 1 deletion src/service/configInjection.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as fs from 'fs';
import * as path from 'path';
import * as Knex from 'knex';
import { Knex } from 'knex';

import Mapping from '../domain/Mapping';
import { dbLogger } from '../util/logger';
Expand Down Expand Up @@ -74,6 +74,12 @@ export async function setup(trx: Knex.Transaction, context: SynchronizeContext):

const values = convertToKeyValuePairs(injectedConfig.vars);

if (!values.length) {
log(`Config not available. Skipping insertion on ${INJECTED_CONFIG_TABLE} table.`);

return;
}

// Create table
log(`Creating table ${INJECTED_CONFIG_TABLE}.`);
await trx.schema.createTable(INJECTED_CONFIG_TABLE, table => {
Expand Down
2 changes: 1 addition & 1 deletion src/service/sqlRunner.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as Knex from 'knex';
import { Knex } from 'knex';
import * as path from 'path';
import { reverse, keys } from 'ramda';

Expand Down
2 changes: 1 addition & 1 deletion src/service/sync.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as Knex from 'knex';
import { Knex } from 'knex';

import * as sqlRunner from './sqlRunner';
import { dbLogger } from '../util/logger';
Expand Down
17 changes: 14 additions & 3 deletions src/util/db.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as Knex from 'knex';
import { Knex, knex } from 'knex';
import { dbLogger, log } from './logger';
import { getConnectionId } from '../config';
import ConnectionConfig from '../domain/ConnectionConfig';
Expand Down Expand Up @@ -48,11 +48,22 @@ export function createInstance(config: ConnectionConfig): Knex {
throw new Error('The provided connection already contains a connection instance.');
}

const { host, database } = config.connection as any;
const { host, database, user } = config.connection as Knex.ConnectionConfig;

log(`Connecting to database: ${host}/${database}`);

return Knex({
if (config.client === 'mssql') {
return knex({
client: config.client,
connection: {
...(config.connection as Knex.MsSqlConnectionConfig),
server: host,
userName: user
}
});
}

return knex({
client: config.client,
connection: config.connection
});
Expand Down
2 changes: 1 addition & 1 deletion test/cli/commands/make.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { mkdir, mkdtemp, write, exists, glob, read } from '../../../src/util/fs'

const MIGRATION_TEMPLATE_PATH = path.resolve(__dirname, '../../../assets/templates/migration');
const CUSTOM_CREATE_TEMPLATE = `
import * as Knex from "knex";
import { Knex } from "knex";
export function up(db: Knex) {
return db.schema.createTable("{{table}}", table => {})
Expand Down
2 changes: 1 addition & 1 deletion test/unit/migration/JavaScriptMigrationContext.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { expect } from 'chai';
import * as Knex from 'knex';
import { Knex } from 'knex';
import { describe, it } from 'mocha';
import JavaScriptMigrationEntry from '../../../src/migration/domain/JavaScriptMigrationEntry';
import JavaScriptMigrationContext from '../../../src/migration/source-types/JavaScriptMigrationSourceContext';
Expand Down
2 changes: 1 addition & 1 deletion test/unit/migration/KnexMigrationSource.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ describe('MIGRATION: KnexMigrationSource', () => {
const instance = await getInstance();
const dbStub = {} as any;

const migration = instance.getMigration('mgr_002');
const migration = await instance.getMigration('mgr_002');

const upResult = await migration.up(dbStub);
const downResult = await migration.down(dbStub);
Expand Down
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"target": "es2017",
"skipLibCheck": true,
"allowSyntheticDefaultImports": true,
"useUnknownInCatchVariables": false,
"typeRoots": ["src/@types"]
},
"include": ["src/**/*"],
Expand Down
Loading

0 comments on commit 31d783e

Please sign in to comment.