Skip to content

Commit fc20952

Browse files
authored
add trace (#43)
* add trace * lint * alter id * alter id * comment * lint * debug * use ubuntu * fix lint * simplify
1 parent 329e8ef commit fc20952

File tree

7 files changed

+147
-105
lines changed

7 files changed

+147
-105
lines changed

.github/workflows/integration-test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ on:
88

99
jobs:
1010
test:
11-
runs-on: macos-latest
11+
runs-on: ubuntu-latest
1212
steps:
1313
- uses: actions/checkout@v3
1414
- uses: actions/setup-node@v4

integration-test/basic.test.ts

Lines changed: 56 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { connect,Row,FullResult } from '../dist/index'
1+
import { connect, Row, FullResult } from '../dist/index'
22
import { fetch } from 'undici'
3-
import * as dotenv from 'dotenv';
3+
import * as dotenv from 'dotenv'
44

55
let databaseURL
66
const database = 'test_serverless_basic'
@@ -9,54 +9,56 @@ const table = 'employee'
99
const EmployeeTable = `CREATE TABLE ${database}.${table} (emp_no INT,first_name VARCHAR(255),last_name VARCHAR(255))`
1010

1111
beforeAll(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

2121
describe('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-

integration-test/type.test.ts

Lines changed: 46 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import { connect,Row,FullResult } from '../dist/index'
1+
import { connect, Row, FullResult } from '../dist/index'
22
import { fetch } from 'undici'
3-
import * as dotenv from 'dotenv';
3+
import * as dotenv from 'dotenv'
44

5-
dotenv.config();
5+
dotenv.config()
66
const databaseURL = process.env.DATABASE_URL
77
const database = 'test_serverless_type'
88
const table = 'multi_data_type'
@@ -46,7 +46,7 @@ const multiDataTable = `
4646
);
4747
`
4848

49-
const nullResult = {
49+
const nullResult = {
5050
t_tinyint: null,
5151
t_tinyint_unsigned: null,
5252
t_smallint: null,
@@ -101,66 +101,65 @@ VALUES ( -128, 255, -32768, 65535, -8388608, 16777215, -2147483648, 1, -92233720
101101
`
102102

103103
const fullTypeResult = {
104-
t_tinyint: -128,
105-
t_tinyint_unsigned: 255,
106-
t_smallint: -32768,
107-
t_smallint_unsigned: 65535,
108-
t_mediumint: -8388608,
109-
t_mediumint_unsigned: 16777215,
110-
t_int: -2147483648,
111-
t_int_unsigned: 1,
112-
t_bigint: '-9223372036854775808',
113-
t_bigint_unsigned: '18446744073709551615',
114-
t_boolean: 1,
115-
t_float: 123.46,
116-
t_double: 123.12,
117-
t_decimal: '123456789012.1234567890120000000',
118-
t_char: '测',
119-
t_varchar: '测试',
120-
c_binary: '�PNG\r\n\x1A\n\x00\x00\x00\x00\x00\x00\x00\x00',
121-
c_varbinary: '�PNG\r\n\x1A\n',
122-
t_tinytext: '测试tinytext',
123-
t_text: '0',
124-
t_mediumtext: '测试mediumtext',
125-
t_longtext: '测试longtext',
126-
t_tinyblob: 'tinyblob',
127-
t_blob: 'blob',
128-
t_mediumblob: 'mediumblob',
129-
t_longblob: 'longblob',
130-
t_date: '1977-01-01',
131-
t_datetime: '9999-12-31 23:59:59',
132-
t_timestamp: '1973-12-30 15:30:00',
133-
t_time: '23:59:59',
134-
t_year: 2154,
135-
t_enum: 'enum2',
136-
t_set: 'a,b',
137-
t_bit: '\x00\x00\x00\x00\x00\x00\x00U',
138-
t_json: { a: 1, b: '2' }
139-
}
104+
t_tinyint: -128,
105+
t_tinyint_unsigned: 255,
106+
t_smallint: -32768,
107+
t_smallint_unsigned: 65535,
108+
t_mediumint: -8388608,
109+
t_mediumint_unsigned: 16777215,
110+
t_int: -2147483648,
111+
t_int_unsigned: 1,
112+
t_bigint: '-9223372036854775808',
113+
t_bigint_unsigned: '18446744073709551615',
114+
t_boolean: 1,
115+
t_float: 123.46,
116+
t_double: 123.12,
117+
t_decimal: '123456789012.1234567890120000000',
118+
t_char: '测',
119+
t_varchar: '测试',
120+
c_binary: '�PNG\r\n\x1A\n\x00\x00\x00\x00\x00\x00\x00\x00',
121+
c_varbinary: '�PNG\r\n\x1A\n',
122+
t_tinytext: '测试tinytext',
123+
t_text: '0',
124+
t_mediumtext: '测试mediumtext',
125+
t_longtext: '测试longtext',
126+
t_tinyblob: 'tinyblob',
127+
t_blob: 'blob',
128+
t_mediumblob: 'mediumblob',
129+
t_longblob: 'longblob',
130+
t_date: '1977-01-01',
131+
t_datetime: '9999-12-31 23:59:59',
132+
t_timestamp: '1973-12-30 15:30:00',
133+
t_time: '23:59:59',
134+
t_year: 2154,
135+
t_enum: 'enum2',
136+
t_set: 'a,b',
137+
t_bit: '\x00\x00\x00\x00\x00\x00\x00U',
138+
t_json: { a: 1, b: '2' }
139+
}
140140

141141
beforeAll(async () => {
142-
const con = connect({url: databaseURL, fetch})
142+
const con = connect({ url: databaseURL, fetch, debug: true })
143143
await con.execute(`DROP DATABASE IF EXISTS ${database}`)
144144
await con.execute(`CREATE DATABASE ${database}`)
145145
await con.execute(multiDataTable)
146-
},20000);
146+
}, 20000)
147147

148148
describe('types', () => {
149-
150149
test('test null', async () => {
151-
const con = connect({url: databaseURL, database: database, fetch})
150+
const con = connect({ url: databaseURL, database: database, fetch, debug: true })
152151
await con.execute(`delete from ${table}`)
153152
await con.execute('insert into multi_data_type values ()')
154-
const r = await con.execute('select * from multi_data_type',null, {fullResult: true}) as FullResult
153+
const r = (await con.execute('select * from multi_data_type', null, { fullResult: true })) as FullResult
155154
expect(r.rows.length).toEqual(1)
156155
expect(JSON.stringify(r.rows[0])).toEqual(JSON.stringify(nullResult))
157156
})
158157

159158
test('test all types', async () => {
160-
const con = connect({url: databaseURL, database: database, fetch})
159+
const con = connect({ url: databaseURL, database: database, fetch, debug: true })
161160
await con.execute(`delete from ${table}`)
162161
await con.execute(insertSQL)
163-
const rows = await con.execute('select * from multi_data_type') as Row[]
162+
const rows = (await con.execute('select * from multi_data_type')) as Row[]
164163
expect(rows.length).toEqual(1)
165164
expect(JSON.stringify(rows[0])).toEqual(JSON.stringify(fullTypeResult))
166165
})

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
"clean": "rm -rf dist/",
2525
"prebuild": "npm run clean",
2626
"build": "tsup",
27-
"lint": "eslint src/",
27+
"lint": "eslint src/ integration-test/",
2828
"pretest": "npm run build",
2929
"test": "jest __tests__ --passWithNoTests",
3030
"integrationTest": "npm run build && jest integration-test --passWithNoTests",

src/config.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,14 @@ export interface Config {
2424
arrayMode?: boolean
2525
fullResult?: boolean
2626
decoders?: Decoders
27+
debug?: boolean
2728
}
2829

2930
export interface ExecuteOptions {
3031
arrayMode?: boolean
3132
fullResult?: boolean
3233
decoders?: Decoders
34+
debug?: boolean
3335
}
3436

3537
export interface TxOptions {

0 commit comments

Comments
 (0)