-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
72 lines (61 loc) · 1.81 KB
/
index.js
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import pg from "postgres";
import nodepg from "pg";
const nodePgClient = new nodepg.Client({
connectionString: "postgres://postgres:masterkey@localhost:5434/postgres",
});
await nodePgClient.connect();
const sql = pg("postgres://postgres:masterkey@localhost:5434/postgres", {
max: 1,
onnotice: () => {},
});
await sql`create table if not exists jsontest (
id serial primary key,
json json,
jsonb jsonb,
jsonarray json,
jsonbarray jsonb,
type text
)`;
await nodePgClient.query(
'insert into "jsontest" ("id", "json", "jsonb", "jsonarray", "jsonbarray", "type") values (default, $1, $2, $3, $4, $5)',
[
JSON.stringify({
string: "test",
number: 123,
}),
JSON.stringify({
string: "test",
number: 123,
}),
JSON.stringify(["foo", "bar"]),
JSON.stringify(["foo", "bar"]),
"node pg client JS stringified objects",
]
);
await sql`insert into "jsontest" ("id", "json", "jsonb", "jsonarray", "jsonbarray", "type") values (default,
${JSON.stringify({
string: "test",
number: 123,
})},
${JSON.stringify({
string: "test",
number: 123,
})},
${JSON.stringify(["foo", "bar"])},
${JSON.stringify(["foo", "bar"])},
${"postgres.js JS stringified objects"})`;
const [query, params] = [
`select "json"->>'string', "json"->>'number', "jsonb"->>'string', "jsonb"->>'number', "jsonarray"->>0, "jsonbarray"->>0, type from "jsontest"`,
[],
];
const selectAll = "select * from jsontest";
console.log(selectAll, await sql`select * from jsontest`);
console.log(selectAll, (await nodePgClient.query(selectAll)).rows);
console.log(query, await sql.unsafe(query, params).values());
console.log(
query,
(await nodePgClient.query({ text: query, rowMode: "array" })).rows
);
await sql`delete from jsontest`;
await sql.end();
await nodePgClient.end();