@@ -8,6 +8,8 @@ const table = 'employee'
88
99const EmployeeTable = `CREATE TABLE ${ database } .${ table } (emp_no INT,first_name VARCHAR(255),last_name VARCHAR(255))`
1010
11+ function assertType < T > ( _value : T ) : void { }
12+
1113beforeAll ( async ( ) => {
1214 dotenv . config ( )
1315 databaseURL = process . env . DATABASE_URL
@@ -22,6 +24,9 @@ describe('basic', () => {
2224 test ( 'ddl' , async ( ) => {
2325 const con = connect ( { url : databaseURL , database : database , fetch, debug : true } )
2426 const results = await con . execute ( `SHOW TABLES` )
27+
28+ assertType < Row [ ] > ( results )
29+
2530 expect ( JSON . stringify ( results ) ) . toContain ( `${ table } ` )
2631 } )
2732
@@ -31,21 +36,33 @@ describe('basic', () => {
3136
3237 await con . execute ( `insert into ${ table } values (1, 'John', 'Doe')` )
3338 const result1 = await con . execute ( `select * from ${ table } where emp_no = 1` )
39+
40+ assertType < Row [ ] > ( result1 )
3441 expect ( JSON . stringify ( result1 ) ) . toContain ( 'John' )
42+
3543 await con . execute ( `update ${ table } set first_name = 'Jane' where emp_no = 1` )
3644 const result2 = await con . execute ( `select * from ${ table } where emp_no = 1` )
45+
46+ assertType < Row [ ] > ( result2 )
3747 expect ( JSON . stringify ( result2 ) ) . toContain ( 'Jane' )
48+
3849 await con . execute ( `delete from ${ table } where emp_no = 1` )
39- const result3 = ( await con . execute ( `select * from ${ table } where emp_no = 1` ) ) as Row [ ]
50+ const result3 = await con . execute ( `select * from ${ table } where emp_no = 1` )
51+
52+ assertType < Row [ ] > ( result3 )
4053 expect ( result3 . length ) . toEqual ( 0 )
4154 } )
4255
4356 test ( 'option' , async ( ) => {
4457 const con = connect ( { url : databaseURL , database : database , fetch, debug : true } )
4558 const result1 = await con . execute ( `select * from ${ table } where emp_no=0` , null , { arrayMode : true } )
4659 const result2 = await con . execute ( `select * from ${ table } where emp_no=0` , null , { fullResult : true } )
47- const except1 : Row [ ] = [ [ 0 , 'base' , 'base' ] ]
48- const except2 : FullResult = {
60+
61+ assertType < Row [ ] > ( result1 )
62+ assertType < FullResult > ( result2 )
63+
64+ const expect1 : Row [ ] = [ [ 0 , 'base' , 'base' ] ]
65+ const expect2 : FullResult = {
4966 statement : `select * from ${ table } where emp_no=0` ,
5067 types : {
5168 emp_no : 'INT' ,
@@ -63,26 +80,35 @@ describe('basic', () => {
6380 lastInsertId : null ,
6481 rowCount : 1
6582 }
66- expect ( JSON . stringify ( result1 ) ) . toEqual ( JSON . stringify ( except1 ) )
67- expect ( JSON . stringify ( result2 ) ) . toEqual ( JSON . stringify ( except2 ) )
83+
84+ expect ( JSON . stringify ( result1 ) ) . toEqual ( JSON . stringify ( expect1 ) )
85+ expect ( JSON . stringify ( result2 ) ) . toEqual ( JSON . stringify ( expect2 ) )
6886 } )
6987
7088 test ( 'arrayMode with config and option' , async ( ) => {
7189 const con = connect ( { url : databaseURL , database : database , fetch, arrayMode : true , debug : true } )
7290 const result1 = await con . execute ( `select * from ${ table } where emp_no=0` , null , { arrayMode : false } )
7391 const result2 = await con . execute ( `select * from ${ table } where emp_no=0` )
74- const except1 : Row [ ] = [ { emp_no : 0 , first_name : 'base' , last_name : 'base' } ]
75- const except2 : Row [ ] = [ [ 0 , 'base' , 'base' ] ]
76- expect ( JSON . stringify ( result1 ) ) . toEqual ( JSON . stringify ( except1 ) )
77- expect ( JSON . stringify ( result2 ) ) . toEqual ( JSON . stringify ( except2 ) )
92+
93+ assertType < Row [ ] > ( result1 )
94+ assertType < Row [ ] > ( result2 )
95+
96+ const expect1 : Row [ ] = [ { emp_no : 0 , first_name : 'base' , last_name : 'base' } ]
97+ const expect2 : Row [ ] = [ [ 0 , 'base' , 'base' ] ]
98+ expect ( JSON . stringify ( result1 ) ) . toEqual ( JSON . stringify ( expect1 ) )
99+ expect ( JSON . stringify ( result2 ) ) . toEqual ( JSON . stringify ( expect2 ) )
78100 } )
79101
80102 test ( 'fullResult with config and option' , async ( ) => {
81103 const con = connect ( { url : databaseURL , database : database , fetch, fullResult : true , debug : true } )
82104 const result1 = await con . execute ( `select * from ${ table } where emp_no=0` , null , { fullResult : false } )
83105 const result2 = await con . execute ( `select * from ${ table } where emp_no=0` )
84- const except1 : Row [ ] = [ { emp_no : 0 , first_name : 'base' , last_name : 'base' } ]
85- const except2 : FullResult = {
106+
107+ assertType < Row [ ] > ( result1 )
108+ assertType < FullResult > ( result2 )
109+
110+ const expect1 : Row [ ] = [ { emp_no : 0 , first_name : 'base' , last_name : 'base' } ]
111+ const expect2 : FullResult = {
86112 statement : `select * from ${ table } where emp_no=0` ,
87113 types : {
88114 emp_no : 'INT' ,
@@ -100,8 +126,8 @@ describe('basic', () => {
100126 lastInsertId : null ,
101127 rowCount : 1
102128 }
103- expect ( JSON . stringify ( result1 ) ) . toEqual ( JSON . stringify ( except1 ) )
104- expect ( JSON . stringify ( result2 ) ) . toEqual ( JSON . stringify ( except2 ) )
129+ expect ( JSON . stringify ( result1 ) ) . toEqual ( JSON . stringify ( expect1 ) )
130+ expect ( JSON . stringify ( result2 ) ) . toEqual ( JSON . stringify ( expect2 ) )
105131 } )
106132
107133 test ( 'query with escape' , async ( ) => {
@@ -111,9 +137,13 @@ describe('basic', () => {
111137 await con . execute ( `insert into ${ table } values (2, '\\"John\\"', 'Doe')` )
112138
113139 // "select * from employee where first_name = '\\'John\\''"
114- const r1 = ( await con . execute ( 'select * from employee where first_name = ?' , [ "'John'" ] ) ) as Row [ ]
140+ const r1 = await con . execute ( 'select * from employee where first_name = ?' , [ "'John'" ] )
115141 // 'select * from employee where first_name = \'\\"John\\"\''
116- const r2 = ( await con . execute ( 'select * from employee where first_name =:name' , { name : '"John"' } ) ) as Row [ ]
142+ const r2 = await con . execute ( 'select * from employee where first_name =:name' , { name : '"John"' } )
143+
144+ assertType < Row [ ] > ( r1 )
145+ assertType < Row [ ] > ( r2 )
146+
117147 expect ( r1 . length ) . toEqual ( 1 )
118148 expect ( r2 . length ) . toEqual ( 1 )
119149 const row1 = r1 [ 0 ] as Record < string , any >
@@ -129,8 +159,12 @@ describe('basic', () => {
129159 try {
130160 tx = await con . begin ( )
131161 await tx . execute ( `insert into ${ table } values (1, 'John', 'Doe')` )
132- const r1 = ( await tx . execute ( `select * from ${ table } where emp_no = 1` ) ) as Row [ ]
133- const r2 = ( await con . execute ( `select * from ${ table } where emp_no = 1` ) ) as Row [ ]
162+ const r1 = await tx . execute ( `select * from ${ table } where emp_no = 1` )
163+ const r2 = await con . execute ( `select * from ${ table } where emp_no = 1` )
164+
165+ assertType < Row [ ] > ( r1 )
166+ assertType < Row [ ] > ( r2 )
167+
134168 expect ( r1 . length ) . toEqual ( 1 )
135169 expect ( r2 . length ) . toEqual ( 0 )
136170 await tx . commit ( )
@@ -170,10 +204,12 @@ describe('basic', () => {
170204 await con . execute ( `delete from ${ table } where emp_no = 1` )
171205
172206 const tx = await con . begin ( { isolation : 'READ COMMITTED' } )
173- const result1 = ( await tx . execute ( `select * from ${ table } ` ) ) as Row [ ]
207+ const result1 = await tx . execute ( `select * from ${ table } ` , null )
208+ assertType < Row [ ] > ( result1 )
174209 await con . execute ( `insert into ${ table } values (1, '\\"John\\"', 'Doe')` )
175- const result2 = ( await tx . execute ( `select * from ${ table } ` ) ) as Row [ ]
210+ const result2 = await tx . execute ( `select * from ${ table } ` , null , { fullResult : true } )
211+ assertType < FullResult > ( result2 )
176212 await tx . commit ( )
177- expect ( result1 . length + 1 ) . toEqual ( result2 . length )
213+ expect ( result1 . length + 1 ) . toEqual ( result2 . rows ?. length ?? result2 . rowCount )
178214 } )
179215} )
0 commit comments