1- import { connect , Row , FullResult } from '../dist/index'
1+ import { connect , Row , FullResult } from '../dist/index'
22import { fetch } from 'undici'
3- import * as dotenv from 'dotenv' ;
3+ import * as dotenv from 'dotenv'
44
55let databaseURL
66const database = 'test_serverless_basic'
@@ -9,54 +9,56 @@ const table = 'employee'
99const EmployeeTable = `CREATE TABLE ${ database } .${ table } (emp_no INT,first_name VARCHAR(255),last_name VARCHAR(255))`
1010
1111beforeAll ( async ( ) => {
12- dotenv . config ( ) ;
12+ dotenv . config ( )
1313 databaseURL = process . env . DATABASE_URL
14- const con = connect ( { url : databaseURL , fetch} )
14+ const con = connect ( { url : databaseURL , fetch, debug : true } )
1515 await con . execute ( `DROP DATABASE IF EXISTS ${ database } ` )
1616 await con . execute ( `CREATE DATABASE ${ database } ` )
1717 await con . execute ( EmployeeTable )
1818 await con . execute ( `insert into ${ database } .${ table } values (0, 'base', 'base')` )
19- } , 20000 ) ;
19+ } , 20000 )
2020
2121describe ( 'basic' , ( ) => {
2222 test ( 'ddl' , async ( ) => {
23- const con = connect ( { url : databaseURL , database : database , fetch} )
23+ const con = connect ( { url : databaseURL , database : database , fetch, debug : true } )
2424 const results = await con . execute ( `SHOW TABLES` )
2525 expect ( JSON . stringify ( results ) ) . toContain ( `${ table } ` )
2626 } )
2727
2828 test ( 'dml' , async ( ) => {
29- const con = connect ( { url : databaseURL , database : database , fetch} )
29+ const con = connect ( { url : databaseURL , database : database , fetch, debug : true } )
3030 await con . execute ( `delete from ${ table } where emp_no = 1` )
3131
3232 await con . execute ( `insert into ${ table } values (1, 'John', 'Doe')` )
33- const result1 = await con . execute ( `select * from ${ table } where emp_no = 1` )
33+ const result1 = await con . execute ( `select * from ${ table } where emp_no = 1` )
3434 expect ( JSON . stringify ( result1 ) ) . toContain ( 'John' )
3535 await con . execute ( `update ${ table } set first_name = 'Jane' where emp_no = 1` )
36- const result2 = await con . execute ( `select * from ${ table } where emp_no = 1` )
36+ const result2 = await con . execute ( `select * from ${ table } where emp_no = 1` )
3737 expect ( JSON . stringify ( result2 ) ) . toContain ( 'Jane' )
3838 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 [ ]
39+ const result3 = ( await con . execute ( `select * from ${ table } where emp_no = 1` ) ) as Row [ ]
4040 expect ( result3 . length ) . toEqual ( 0 )
4141 } )
4242
4343 test ( 'option' , async ( ) => {
44- const con = connect ( { url : databaseURL , database : database , fetch} )
45- const result1 = await con . execute ( `select * from ${ table } where emp_no=0` , null , { arrayMode : true } )
46- const result2 = await con . execute ( `select * from ${ table } where emp_no=0` , null , { fullResult : true } )
44+ const con = connect ( { url : databaseURL , database : database , fetch, debug : true } )
45+ const result1 = await con . execute ( `select * from ${ table } where emp_no=0` , null , { arrayMode : true } )
46+ const result2 = await con . execute ( `select * from ${ table } where emp_no=0` , null , { fullResult : true } )
4747 const except1 : Row [ ] = [ [ 0 , 'base' , 'base' ] ]
4848 const except2 : FullResult = {
4949 statement : `select * from ${ table } where emp_no=0` ,
50- types :{
50+ types : {
5151 emp_no : 'INT' ,
5252 first_name : 'VARCHAR' ,
5353 last_name : 'VARCHAR'
5454 } ,
55- rows : [ {
56- emp_no : 0 ,
57- first_name : 'base' ,
58- last_name : 'base'
59- } ] ,
55+ rows : [
56+ {
57+ emp_no : 0 ,
58+ first_name : 'base' ,
59+ last_name : 'base'
60+ }
61+ ] ,
6062 rowsAffected : 0 ,
6163 lastInsertId : null ,
6264 rowCount : 1
@@ -66,32 +68,34 @@ describe('basic', () => {
6668 } )
6769
6870 test ( 'arrayMode with config and option' , async ( ) => {
69- const con = connect ( { url : databaseURL , database : database , fetch, arrayMode : true } )
70- const result1 = await con . execute ( `select * from ${ table } where emp_no=0` , null , { arrayMode : false } )
71+ const con = connect ( { url : databaseURL , database : database , fetch, arrayMode : true , debug : true } )
72+ const result1 = await con . execute ( `select * from ${ table } where emp_no=0` , null , { arrayMode : false } )
7173 const result2 = await con . execute ( `select * from ${ table } where emp_no=0` )
72- const except1 : Row [ ] = [ { emp_no : 0 , first_name : 'base' , last_name : 'base' } ]
74+ const except1 : Row [ ] = [ { emp_no : 0 , first_name : 'base' , last_name : 'base' } ]
7375 const except2 : Row [ ] = [ [ 0 , 'base' , 'base' ] ]
7476 expect ( JSON . stringify ( result1 ) ) . toEqual ( JSON . stringify ( except1 ) )
7577 expect ( JSON . stringify ( result2 ) ) . toEqual ( JSON . stringify ( except2 ) )
7678 } )
7779
7880 test ( 'fullResult with config and option' , async ( ) => {
79- const con = connect ( { url : databaseURL , database : database , fetch, fullResult : true } )
80- const result1 = await con . execute ( `select * from ${ table } where emp_no=0` , null , { fullResult : false } )
81+ const con = connect ( { url : databaseURL , database : database , fetch, fullResult : true , debug : true } )
82+ const result1 = await con . execute ( `select * from ${ table } where emp_no=0` , null , { fullResult : false } )
8183 const result2 = await con . execute ( `select * from ${ table } where emp_no=0` )
82- const except1 : Row [ ] = [ { emp_no : 0 , first_name : 'base' , last_name : 'base' } ]
84+ const except1 : Row [ ] = [ { emp_no : 0 , first_name : 'base' , last_name : 'base' } ]
8385 const except2 : FullResult = {
8486 statement : `select * from ${ table } where emp_no=0` ,
85- types :{
87+ types : {
8688 emp_no : 'INT' ,
8789 first_name : 'VARCHAR' ,
8890 last_name : 'VARCHAR'
8991 } ,
90- rows : [ {
91- emp_no : 0 ,
92- first_name : 'base' ,
93- last_name : 'base'
94- } ] ,
92+ rows : [
93+ {
94+ emp_no : 0 ,
95+ first_name : 'base' ,
96+ last_name : 'base'
97+ }
98+ ] ,
9599 rowsAffected : 0 ,
96100 lastInsertId : null ,
97101 rowCount : 1
@@ -101,15 +105,15 @@ describe('basic', () => {
101105 } )
102106
103107 test ( 'query with escape' , async ( ) => {
104- const con = connect ( { url : databaseURL , database : database , fetch} )
108+ const con = connect ( { url : databaseURL , database : database , fetch, debug : true } )
105109 await con . execute ( `delete from ${ table } where emp_no = 1 or emp_no = 2` )
106110 await con . execute ( `insert into ${ table } values (1, '\\'John\\'', 'Doe')` )
107111 await con . execute ( `insert into ${ table } values (2, '\\"John\\"', 'Doe')` )
108112
109113 // "select * from employee where first_name = '\\'John\\''"
110- const r1 = await con . execute ( 'select * from employee where first_name = ?' , [ "'John'" ] ) as Row [ ]
114+ const r1 = ( await con . execute ( 'select * from employee where first_name = ?' , [ "'John'" ] ) ) as Row [ ]
111115 // 'select * from employee where first_name = \'\\"John\\"\''
112- const r2 = await con . execute ( 'select * from employee where first_name =:name' , { name : '"John"' } ) as Row [ ]
116+ const r2 = ( await con . execute ( 'select * from employee where first_name =:name' , { name : '"John"' } ) ) as Row [ ]
113117 expect ( r1 . length ) . toEqual ( 1 )
114118 expect ( r2 . length ) . toEqual ( 1 )
115119 const row1 = r1 [ 0 ] as Record < string , any >
@@ -119,27 +123,27 @@ describe('basic', () => {
119123 } )
120124
121125 test ( 'transaction isolation' , async ( ) => {
122- const con = connect ( { url : databaseURL , database : database , fetch} )
126+ const con = connect ( { url : databaseURL , database : database , fetch, debug : true } )
123127 await con . execute ( `delete from ${ table } where emp_no = 1` )
124128 let tx
125- try {
126- tx = await con . begin ( )
127- await tx . execute ( `insert into ${ table } values (1, 'John', 'Doe')` )
128- const r1 = await tx . execute ( `select * from ${ table } where emp_no = 1` ) as Row [ ]
129- const r2 = await con . execute ( `select * from ${ table } where emp_no = 1` ) as Row [ ]
130- expect ( r1 . length ) . toEqual ( 1 )
131- expect ( r2 . length ) . toEqual ( 0 )
132- await tx . commit ( )
129+ try {
130+ tx = await con . begin ( )
131+ 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 [ ]
134+ expect ( r1 . length ) . toEqual ( 1 )
135+ expect ( r2 . length ) . toEqual ( 0 )
136+ await tx . commit ( )
133137 } catch ( e ) {
134- if ( tx ) {
138+ if ( tx ) {
135139 tx . rollback ( )
136140 }
137141 throw e
138142 }
139143 } )
140144
141145 test ( 'transaction rollback' , async ( ) => {
142- const con = connect ( { url : databaseURL , database : database , fetch} )
146+ const con = connect ( { url : databaseURL , database : database , fetch, debug : true } )
143147 await con . execute ( `delete from ${ table } where emp_no = 1` )
144148
145149 let tx
@@ -149,28 +153,27 @@ describe('basic', () => {
149153 await tx . execute ( `update ${ table } set first_name = 'Jane' where emp_no = 0` )
150154 await tx . rollback ( )
151155 } catch ( e ) {
152- if ( tx ) {
156+ if ( tx ) {
153157 tx . rollback ( )
154158 }
155159 throw e
156160 }
157161
158- const r = await con . execute ( `select * from ${ table } where emp_no = 0 or emp_no = 1` ) as Row [ ]
162+ const r = ( await con . execute ( `select * from ${ table } where emp_no = 0 or emp_no = 1` ) ) as Row [ ]
159163 expect ( r . length ) . toEqual ( 1 )
160164 const row = r [ 0 ] as Record < string , any >
161165 expect ( row . first_name ) . toEqual ( 'base' )
162166 } )
163167
164168 test ( 'transaction isolation level' , async ( ) => {
165- const con = connect ( { url : databaseURL , database : database , fetch} )
169+ const con = connect ( { url : databaseURL , database : database , fetch, debug : true } )
166170 await con . execute ( `delete from ${ table } where emp_no = 1` )
167171
168- const tx = await con . begin ( { isolation :" READ COMMITTED" } )
169- const result1 = await tx . execute ( `select * from ${ table } ` ) as Row [ ]
172+ const tx = await con . begin ( { isolation : ' READ COMMITTED' } )
173+ const result1 = ( await tx . execute ( `select * from ${ table } ` ) ) as Row [ ]
170174 await con . execute ( `insert into ${ table } values (1, '\\"John\\"', 'Doe')` )
171- const result2 = await tx . execute ( `select * from ${ table } ` ) as Row [ ]
175+ const result2 = ( await tx . execute ( `select * from ${ table } ` ) ) as Row [ ]
172176 await tx . commit ( )
173- expect ( result1 . length + 1 ) . toEqual ( result2 . length )
177+ expect ( result1 . length + 1 ) . toEqual ( result2 . length )
174178 } )
175179} )
176-
0 commit comments