-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(datasource-sql): refactor the proxy and sequelize management (…
- Loading branch information
Showing
6 changed files
with
131 additions
and
166 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
30 changes: 30 additions & 0 deletions
30
packages/datasource-sql/src/connection/services/sequelize-factory.ts
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,30 @@ | ||
import { Sequelize } from 'sequelize'; | ||
import { Options as SequelizeOptions } from 'sequelize/types/sequelize'; | ||
|
||
import Service from './service'; | ||
|
||
export default class SequelizeFactory extends Service { | ||
build(sequelizeCtorOptions: [SequelizeOptions] | [string, SequelizeOptions]) { | ||
const sequelize = | ||
sequelizeCtorOptions.length === 1 | ||
? new Sequelize(sequelizeCtorOptions[0]) | ||
: new Sequelize(sequelizeCtorOptions[0], sequelizeCtorOptions[1]); | ||
|
||
this.overrideCloseMethod(sequelize); | ||
|
||
return sequelize; | ||
} | ||
|
||
private overrideCloseMethod(sequelize: Sequelize): void { | ||
const closeListener = this.closeListener.bind(this); | ||
|
||
// override close method to ensure to execute the closeListener | ||
sequelize.close = async function close() { | ||
try { | ||
await Sequelize.prototype.close.call(this); | ||
} finally { | ||
await closeListener(); | ||
} | ||
}; | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
packages/datasource-sql/src/connection/services/service.ts
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,29 @@ | ||
import net from 'net'; | ||
|
||
export type ConnectionCallback = (socket: net.Socket) => Promise<void>; | ||
export type CloseCallback = () => Promise<void>; | ||
|
||
export default abstract class Service { | ||
private connectionCallback: ConnectionCallback; | ||
private closeCallback: CloseCallback; | ||
|
||
/** attach a callback when there is a new connection on the service. */ | ||
onConnect(callback: ConnectionCallback): void { | ||
this.connectionCallback = callback; | ||
} | ||
|
||
/** callback to execute when there is a new connection. */ | ||
async connectListener(socket: net.Socket): Promise<void> { | ||
await this.connectionCallback?.(socket); | ||
} | ||
|
||
/** attach a callback when a service is closing. */ | ||
onClose(callback: CloseCallback): void { | ||
this.closeCallback = callback; | ||
} | ||
|
||
/** callback to execute when the service is closing. */ | ||
async closeListener(): Promise<void> { | ||
await this.closeCallback?.(); | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
packages/datasource-sql/src/connection/services/tcp-server.ts
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,47 @@ | ||
import net from 'net'; | ||
|
||
import Service from './service'; | ||
|
||
/** TcpServer is used as proxy to redirect all the database requests */ | ||
export default class TcpServer extends Service { | ||
private readonly server: net.Server; | ||
|
||
get host(): string { | ||
return (this.server.address() as net.AddressInfo).address; | ||
} | ||
|
||
get port(): number { | ||
return (this.server.address() as net.AddressInfo).port; | ||
} | ||
|
||
constructor() { | ||
super(); | ||
this.server = net.createServer(this.connectListener.bind(this)); | ||
} | ||
|
||
start(): Promise<void> { | ||
return new Promise<void>((resolve, reject) => { | ||
this.server.on('error', reject); | ||
// By using port 0, the operating system | ||
// will assign an available port for the server to listen on. | ||
this.server.listen(0, '127.0.0.1', resolve); | ||
}); | ||
} | ||
|
||
async stop(): Promise<void> { | ||
try { | ||
await super.closeListener(); | ||
} finally { | ||
await new Promise<void>((resolve, reject) => { | ||
this.server.close(e => { | ||
if (e) reject(e); | ||
else resolve(); | ||
}); | ||
}); | ||
} | ||
} | ||
|
||
override async closeListener(): Promise<void> { | ||
await this.stop(); | ||
} | ||
} |
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