-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from leapfrogtechnology/refactor
Enable passing connection instance as well as connection config for synchronization
- Loading branch information
Showing
14 changed files
with
272 additions
and
103 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
import * as Knex from 'knex'; | ||
|
||
import ConnectionConfig from './domain/ConnectionConfig'; | ||
import RawBindingParams, { ValueMap } from './domain/RawBingingParams'; | ||
|
||
/** | ||
* Connection class wrapper with | ||
* both config and instance property. | ||
*/ | ||
class Connection { | ||
/** | ||
* Creates new Connection object | ||
* using provided Knex connection instance. | ||
* | ||
* @param {Knex} db | ||
* @returns {Connection} | ||
*/ | ||
public static withKnex(db: Knex): Connection { | ||
return new Connection({ | ||
...db.client.config.connection, | ||
client: db.client.config.client, | ||
id: `${db.client.config.connection.host}/${db.client.config.connection.database}` | ||
}); | ||
} | ||
|
||
/** | ||
* Returns true if the provided object is a knex connection instance. | ||
* | ||
* @param {any} obj | ||
* @returns {boolean} | ||
*/ | ||
public static isKnexInstance(obj: any): obj is Knex { | ||
return !!(obj.prototype && obj.prototype.constructor && obj.prototype.constructor.name === 'knex'); | ||
} | ||
|
||
private instance: Knex; | ||
private config: ConnectionConfig; | ||
|
||
/** | ||
* Constructor. | ||
* | ||
* @param {ConnectionConfig} config | ||
*/ | ||
constructor(config: ConnectionConfig) { | ||
this.config = config; | ||
this.instance = this.createInstance(); | ||
} | ||
|
||
/** | ||
* Creates a connection instance from | ||
* the provided database configuration. | ||
* | ||
* @param {ConnectionConfig} connectionConfig | ||
* @returns {Knex} | ||
*/ | ||
public createInstance(): Knex { | ||
return Knex({ | ||
client: this.config.client, | ||
connection: this.config | ||
}); | ||
} | ||
|
||
/** | ||
* Runs a query. | ||
* | ||
* @param {string} sql | ||
* @param {RawBindingParams | ValueMap} params | ||
* @returns {Promise<T[]>} | ||
*/ | ||
public query<T>(sql: string, params: RawBindingParams | ValueMap = []): Promise<T[]> { | ||
return this.instance.raw(sql, params); | ||
} | ||
|
||
/** | ||
* Runs a callback within transaction. | ||
* | ||
* @param {(trx: Connection) => any} callback | ||
* @returns {any} | ||
*/ | ||
public transaction(callback: (trx: Connection) => any): Promise<any> { | ||
return this.instance.transaction(trx => callback(Connection.withKnex(trx))); | ||
} | ||
|
||
/** | ||
* Returns connection config. | ||
* | ||
* @returns {ConnectionConfig} | ||
*/ | ||
public getConfig(): ConnectionConfig { | ||
return this.config; | ||
} | ||
} | ||
|
||
export default Connection; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,19 @@ | ||
import Connection from './Connection' | ||
import * as Knex from 'knex'; | ||
|
||
/** | ||
* Interface for sync-db connection file (connections.sync-db.json). | ||
* Database connection configuration. | ||
*/ | ||
interface ConnectionConfig { | ||
connections: Connection[]; | ||
} | ||
type KnexConnections = Knex.ConnectionConfig | ||
& Knex.MariaSqlConnectionConfig | ||
& Knex.MySqlConnectionConfig | ||
& Knex.MsSqlConnectionConfig | ||
& Knex.SocketConnectionConfig | ||
& Knex.Sqlite3ConnectionConfig; | ||
|
||
type ConnectionConfig = KnexConnections | ||
& { | ||
id?: string; | ||
client: string; | ||
}; | ||
|
||
export default ConnectionConfig; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import ConnectionConfig from './ConnectionConfig'; | ||
|
||
/** | ||
* Interface for sync-db connection file (connections.sync-db.json). | ||
*/ | ||
interface DbConfig { | ||
connections: ConnectionConfig[]; | ||
} | ||
|
||
export default DbConfig; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import * as Knex from 'knex'; | ||
|
||
type Value = Date | string | number | boolean | Date[] | string[] | number[] | boolean[] | Buffer | Knex.Raw; | ||
|
||
type RawBindingParams = ( | ||
| Date | ||
| Buffer | ||
| string | ||
| number | ||
| Date[] | ||
| boolean | ||
| Knex.Raw | ||
| string[] | ||
| number[] | ||
| boolean[] | ||
| Knex.QueryBuilder)[]; | ||
|
||
export interface ValueMap { | ||
[key: string]: Value | Knex.QueryBuilder; | ||
} | ||
|
||
export default RawBindingParams; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
import * as debug from 'debug'; | ||
|
||
import Connection from './domain/Connection'; | ||
import Connection from './Connection'; | ||
|
||
export const SYNC_DB = 'sync-db'; | ||
export const log = debug(SYNC_DB); | ||
export const dbLogger = (db: Connection) => log.extend(db.id || db.database); | ||
export const dbLogger = (conn: Connection) => log.extend(conn.getConfig().id || conn.getConfig().database); |
Oops, something went wrong.