-
Notifications
You must be signed in to change notification settings - Fork 103
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c030404
commit 1b85524
Showing
6 changed files
with
335 additions
and
92 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,58 @@ | ||
import { EventEmitter } from "events-intercept"; | ||
import { Database, Session, Transaction } from "."; | ||
import { MultiplexedSession, MultiplexedSessionInterface, MultiplexedSessionOptions } from "./multiplexed-session"; | ||
import { SessionPool, SessionPoolInterface, SessionPoolOptions } from "./session-pool"; | ||
import { MultiplexedSessionConstructor, SessionPoolConstructor } from "./database"; | ||
|
||
/** | ||
* @callback GetSessionCallback | ||
* @param {?Error} error Request error, if any. | ||
* @param {Session} session The read-write session. | ||
* @param {Transaction} transaction The transaction object. | ||
*/ | ||
export interface GetSessionCallback { | ||
( | ||
err: Error | null, | ||
session?: Session | null, | ||
transaction?: Transaction | null | ||
): void; | ||
} | ||
|
||
export interface GetSessionInterface { | ||
getSession(callback: GetSessionCallback): void; | ||
} | ||
|
||
export class GetSession extends EventEmitter implements GetSessionInterface { | ||
database: Database; | ||
multiplexedSession_?: MultiplexedSessionInterface; | ||
pool_: SessionPoolInterface; | ||
constructor( | ||
database: Database, | ||
poolOptions?: SessionPoolConstructor | SessionPoolOptions, | ||
multiplexedSessionOptions?: MultiplexedSessionOptions | MultiplexedSessionConstructor | ||
) { | ||
super(); | ||
this.database = database; | ||
this.pool_ = | ||
typeof poolOptions === 'function' | ||
? new (poolOptions as SessionPoolConstructor)(this.database, null) | ||
: new SessionPool(this.database, poolOptions); | ||
this.multiplexedSession_ = | ||
typeof multiplexedSessionOptions === 'function' | ||
? new (multiplexedSessionOptions as MultiplexedSessionConstructor)(this.database) | ||
: new MultiplexedSession(this.database, multiplexedSessionOptions); | ||
} | ||
getSession(callback: GetSessionCallback): void{ | ||
if(process.env.GOOGLE_CLOUD_SPANNER_MULTIPLEXED_SESSIONS==='true') { | ||
this.multiplexedSession_?.getSession((err, session) => { | ||
console.log("err: ", err); | ||
err ? callback(err, null) : callback(null, session); | ||
}) | ||
} else { | ||
this.pool_?.getSession((err, session) => { | ||
console.log("err: ", err); | ||
err ? callback(err, null) : callback(null, session); | ||
}) | ||
} | ||
} | ||
} |
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
Oops, something went wrong.