Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrade knex(v2.4.2) and typescript(v5.0.4) to latest version #198

Merged
merged 25 commits into from
Apr 28, 2023
Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
6b06d8f
feat: Bump typescript to v5.0.2
kshitish182 Apr 23, 2023
c49ea37
fix: Fix some type errors after upgrading the ts version
kshitish182 Apr 23, 2023
4ffaaf0
chore: Add vscode workspace settings files in gitignore
kshitish182 Apr 23, 2023
558349e
Bump typescript version in example apps
kshitish182 Apr 26, 2023
4e18549
fix: Fix linter issue
kshitish182 Apr 26, 2023
8c04580
feat: Bump knex to v2.4.2
kshitish182 Apr 26, 2023
804ce2c
fix: Update knex imports
kshitish182 Apr 26, 2023
70215fa
Bump knex and sync-db version in example file
kshitish182 Apr 26, 2023
6e78020
chore: Update reademe file
kshitish182 Apr 27, 2023
928fb33
fix: Skip logging in the config table if config not available
kshitish182 Apr 27, 2023
3f36b96
feat: Fix type issues in KnexMigratorSource class
kshitish182 Apr 27, 2023
7d2ccd1
fix: Fix lint issues
kshitish182 Apr 27, 2023
3ad2d82
feat: Update config for mssql client
kshitish182 Apr 27, 2023
9c6959b
test: Update tests for retreiving config
kshitish182 Apr 27, 2023
c4ea711
fix: Lint fix
kshitish182 Apr 27, 2023
0ba974d
Revert changes from example file
kshitish182 Apr 27, 2023
dd00ffd
revert: revert package upgarde on example apps
kshitish182 Apr 27, 2023
ed2aab8
Run prettify
kshitish182 Apr 27, 2023
65f65de
chore: Remove leading slash in gitignore file
kshitish182 Apr 27, 2023
9f14876
Bump rambda to v0.29.0
kshitish182 Apr 28, 2023
fb8a52a
fix: Remove as unknown type annotation
kshitish182 Apr 28, 2023
417b528
fix: Fix type issues and manage mssql connection config
kshitish182 Apr 28, 2023
ef04f84
chore: update readme file
kshitish182 Apr 28, 2023
a3aba43
Format code
kshitish182 Apr 28, 2023
2a5a29e
Bump sync-db to v2.0.0
kshitish182 Apr 28, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -240,17 +240,17 @@ 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: {
host: 'host',
user: 'userName',
server: 'host',
userName: 'userName',
password: 'password',
database: 'dbName'
}
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
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
"debug": "^4.1.1",
"esm": "^3.2.25",
"globby": "^10.0.2",
"knex": "^0.20.11",
"knex": "^2.4.2",
"ramda": "^0.26.1",
"ts-node": "^8",
"tslib": "^1",
Expand Down Expand Up @@ -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
11 changes: 9 additions & 2 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,10 @@ 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
) as unknown) as Configuration;
mesaugat marked this conversation as resolved.
Show resolved Hide resolved

validate(loaded);

Expand Down Expand Up @@ -131,6 +134,7 @@ export async function resolveConnections(config: Configuration, resolver?: strin
client,
connection: {
host: (connection as any).host,
server: (connection as any).server,
mesaugat marked this conversation as resolved.
Show resolved Hide resolved
database: (connection as any).database
}
}))
Expand Down Expand Up @@ -214,7 +218,10 @@ export function resolveConnectionsFromEnv(): ConnectionConfig[] {
database: process.env.DB_NAME,
options: {
encrypt: process.env.DB_ENCRYPTION === 'true'
}
},
// Knex config for mssql uses these keys in place of "host" and "user"
server: process.env.DB_SERVER,
mesaugat marked this conversation as resolved.
Show resolved Hide resolved
userName: process.env.DB_USERNAME
}
} as ConnectionConfig;

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);
mesaugat marked this conversation as resolved.
Show resolved Hide resolved
}
}

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;
mesaugat marked this conversation as resolved.
Show resolved Hide resolved
}

// 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
12 changes: 8 additions & 4 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,15 @@ 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, server, database, userName } = config.connection as any;
mesaugat marked this conversation as resolved.
Show resolved Hide resolved

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

return Knex({
if (config.client === 'mssql' && (!server || !userName)) {
throw new Error(`Db client ${config.client} requires key "server" and "userName".`);
mesaugat marked this conversation as resolved.
Show resolved Hide resolved
}

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');
mesaugat marked this conversation as resolved.
Show resolved Hide resolved

const upResult = await migration.up(dbStub);
const downResult = await migration.down(dbStub);
Expand Down
4 changes: 4 additions & 0 deletions test/unit/util/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ describe('CONFIG:', () => {
process.env.DB_USERNAME = 'user';
process.env.DB_PASSWORD = 'password';
process.env.DB_NAME = 'database';
process.env.DB_SERVER = 'localhost';

const connections = resolveConnectionsFromEnv();

Expand All @@ -46,6 +47,8 @@ describe('CONFIG:', () => {
user: 'user',
password: 'password',
database: 'database',
server: 'localhost',
userName: 'user',
options: {
encrypt: false
}
Expand All @@ -60,6 +63,7 @@ describe('CONFIG:', () => {
process.env.DB_USERNAME = '';
process.env.DB_PASSWORD = '';
process.env.DB_NAME = '';
process.env.DB_SERVER = '';
});

it('should throw an error if the required env vars are not provided.', () => {
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