@@ -16,102 +16,106 @@ import { ffi } from '../ffi/utils.js';
1616import { spannerLib } from './spannerlib.js' ;
1717import { Pool } from './pool.js' ;
1818import { Rows } from './rows.js' ;
19- import { createRequire } from 'module' ;
20- // @ts -ignore
21- const _require = typeof require !== 'undefined' ? require : createRequire ( import . meta. url ) ;
22- // TODO: Avoid tight coupling to internal paths of full client libraries.
23- // Unlike other languages like Java, Python , Node client does not export its protos.
19+ // TODO: Avoid tight coupling to internal paths of full client libraries.
20+ // Unlike other languages like Java, Python , Node client does not export its protos.
2421// We need to explore how to import protos in Node
25- const { google } = _require ( '@google-cloud/spanner/build/protos/protos.js' ) ;
22+ import pkg from '@google-cloud/spanner/build/protos/protos.js' ;
23+ const { google } = pkg ;
2624
2725/**
2826 * Manages a connection to the Spanner database.
29- *
27+ *
3028 * This class wraps the connection handle from the underlying Go library,
3129 * providing methods to execute SQL statements and manage transactions.
3230 */
3331export class Connection {
34- public pool : Pool | null ;
35- public oid : number | null ;
36- public pinnerId : number | null ;
37- public closed : boolean ;
32+ public pool : Pool | null ;
33+ public oid : number | null ;
34+ public pinnerId : number | null ;
35+ public closed : boolean ;
3836
39- /**
40- * Creates a new connection within the specified pool.
41- *
42- * @param pool The pool to create the connection in.
43- * @returns A Promise that resolves to a new Connection instance.
44- * @throws {SpannerLibError } If creation fails in the Go library.
45- */
46- static async create ( pool : Pool ) : Promise < Connection > {
47- const c = new Connection ( ) ;
48- c . pool = pool ;
37+ /**
38+ * Creates a new connection within the specified pool.
39+ *
40+ * @param pool The pool to create the connection in.
41+ * @returns A Promise that resolves to a new Connection instance.
42+ * @throws {SpannerLibError } If creation fails in the Go library.
43+ */
44+ static async create ( pool : Pool ) : Promise < Connection > {
45+ const c = new Connection ( ) ;
46+ c . pool = pool ;
4947
50- const handled = await ffi . invokeAsync (
51- " CreateConnection" ,
52- c ,
53- spannerLib ,
54- pool . oid
55- ) ;
48+ const handled = await ffi . invokeAsync (
49+ ' CreateConnection' ,
50+ c ,
51+ spannerLib ,
52+ pool . oid
53+ ) ;
5654
57- c . oid = handled . objectId ;
58- c . pinnerId = handled . pinnerId ;
59- return c ;
60- }
55+ c . oid = handled . objectId ;
56+ c . pinnerId = handled . pinnerId ;
57+ return c ;
58+ }
6159
62- constructor ( ) {
63- this . pool = null ;
64- this . oid = null ;
65- this . pinnerId = null ;
66- this . closed = false ;
67- }
60+ constructor ( ) {
61+ this . pool = null ;
62+ this . oid = null ;
63+ this . pinnerId = null ;
64+ this . closed = false ;
65+ }
6866
69- /**
70- * Executes a SQL statement on this connection.
71- *
72- * @param sqlString The SQL query string to execute.
73- * @returns A Promise that resolves to a Rows instance containing results.
74- * @throws {Error } If the connection is closed or not bound to a pool.
75- * @throws {SpannerLibError } If execution fails in the Go library.
76- */
77- async executeSql ( sqlString : string ) : Promise < Rows > {
78- if ( this . closed ) throw new Error ( " Connection is already closed" ) ;
79- if ( ! this . pool ) throw new Error ( " Connection is not bound to a Pool" ) ;
67+ /**
68+ * Executes a SQL statement on this connection.
69+ *
70+ * @param sqlString The SQL query string to execute.
71+ * @returns A Promise that resolves to a Rows instance containing results.
72+ * @throws {Error } If the connection is closed or not bound to a pool.
73+ * @throws {SpannerLibError } If execution fails in the Go library.
74+ */
75+ async executeSql ( sqlString : string ) : Promise < Rows > {
76+ if ( this . closed ) throw new Error ( ' Connection is already closed' ) ;
77+ if ( ! this . pool ) throw new Error ( ' Connection is not bound to a Pool' ) ;
8078
81- const requestObj = { sql : sqlString , session : " poc/dummy" } ;
82- const ExecuteSqlRequestProto = google . spanner . v1 . ExecuteSqlRequest ;
83- const serializedPb = ExecuteSqlRequestProto . encode ( requestObj ) . finish ( ) ;
79+ const requestObj = { sql : sqlString , session : ' poc/dummy' } ;
80+ const ExecuteSqlRequestProto = google . spanner . v1 . ExecuteSqlRequest ;
81+ const serializedPb = ExecuteSqlRequestProto . encode ( requestObj ) . finish ( ) ;
8482
85- const rowsResult = await ffi . invokeAsync (
86- " Execute" ,
87- null ,
88- null ,
89- this . pool . oid ,
90- this . oid ,
91- serializedPb
92- ) ;
93- const rowsId = rowsResult . objectId ;
83+ const rowsResult = await ffi . invokeAsync (
84+ ' Execute' ,
85+ null ,
86+ null ,
87+ this . pool . oid ,
88+ this . oid ,
89+ serializedPb
90+ ) ;
91+ const rowsId = rowsResult . objectId ;
9492
95- return new Rows ( this , rowsId ) ;
96- }
93+ return new Rows ( this , rowsId ) ;
94+ }
9795
98- /**
99- * Closes the connection and releases associated resources.
100- *
101- * @returns A Promise that resolves when the connection is closed.
102- */
103- async close ( ) : Promise < void > {
104- if ( ! this . closed ) {
105- this . closed = true ;
106- try {
107- if ( this . pool && this . oid !== null ) {
108- await ffi . invokeAsync ( "CloseConnection" , this , spannerLib , this . pool . oid , this . oid ) ;
109- }
110- } finally {
111- if ( this . pinnerId !== null ) {
112- spannerLib . unregister ( this , this . pinnerId ) ;
113- }
114- }
96+ /**
97+ * Closes the connection and releases associated resources.
98+ *
99+ * @returns A Promise that resolves when the connection is closed.
100+ */
101+ async close ( ) : Promise < void > {
102+ if ( ! this . closed ) {
103+ this . closed = true ;
104+ try {
105+ if ( this . pool && this . oid !== null ) {
106+ await ffi . invokeAsync (
107+ 'CloseConnection' ,
108+ this ,
109+ spannerLib ,
110+ this . pool . oid ,
111+ this . oid
112+ ) ;
113+ }
114+ } finally {
115+ if ( this . pinnerId !== null ) {
116+ spannerLib . unregister ( this , this . pinnerId ) ;
115117 }
118+ }
116119 }
120+ }
117121}
0 commit comments