forked from hnasr/javascript_playground
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtx.js
More file actions
58 lines (48 loc) · 1.19 KB
/
tx.js
File metadata and controls
58 lines (48 loc) · 1.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
const {Client} = require('pg')
const Cursor = require ('pg-cursor')
const client = new Client( {
user: "postgres",
password: "postgres",
host: "husseinmac",
port: 5432,
database: "husseindb"
})
execute()
async function execute_tx() {
try{
await client.connect()
console.log("Connected successfully.")
//await client.query("insert into employees values (1, 'John')")
await client.query("BEGIN");
const {rows} = await client.query("select * from employees")
console.table(rows)
await client.query("COMMIT");
}
catch (ex)
{
console.log(`Something wrong happend ${ex}`)
}
finally
{
await client.query("ROLLBACK");
await client.end()
console.log("Client disconnected successfully.")
}
}
async function execute() {
try {
const cursor = new Cursor("select * from employees")
await client.connect()
const cursorFilled = await client.query(cursor)
cursorFilled.read(5, (err, rows) => {
console.table(rows)
})
}
catch (ex){
console.log(`Failed to execute something ${ex} this is not cool`)
}
finally{
await client.end()
console.log("Cleaned.")
}
}