forked from oguimbal/pg-mem
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtestbind.ts
73 lines (63 loc) · 2.3 KB
/
testbind.ts
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
73
import { DataType, newDb } from './src';
// attempt to make tableplus or dbeaver working on pg-mem
// ...still things failing
const db = newDb();
db.public.query(`
create table users (id serial primary key, name text, is_ok boolean, data jsonb);
insert into users (name, is_ok, data) values
('Alice', true, '{"gender":"female"}'),
('Bob', false, null),
('Anon', null, null);
`);
db.public.registerFunction({
name: 'version',
returns: DataType.text,
implementation: () => `PostgreSQL 16.3 (Debian 16.3-1.pgdg120+1) on aarch64-unknown-linux-gnu, compiled by gcc (Debian 12.2.0-14) 12.2.0, 64-bit`
})
db.getSchema('pg_catalog').registerFunction({
name: 'pg_get_userbyid',
args: [DataType.integer],
returns: DataType.text,
implementation: () => 'pgmem'
});
db.getSchema('pg_catalog').registerFunction({
name: 'obj_description',
args: [DataType.integer, DataType.text],
returns: DataType.text,
implementation: () => null,
});
db.getSchema('pg_catalog').registerFunction({
name: 'pg_total_relation_size',
args: [DataType.integer],
returns: DataType.integer,
implementation: () => 0,
});
db.getSchema('pg_catalog').registerFunction({
name: 'pg_table_size',
args: [DataType.integer],
returns: DataType.integer,
implementation: () => 0,
});
db.getSchema('pg_catalog').registerFunction({
name: 'pg_indexes_size',
args: [DataType.integer],
returns: DataType.integer,
implementation: () => 0,
});
db.getSchema('pg_catalog').registerFunction({
name: 'pg_get_function_identity_arguments',
args: [DataType.integer],
returns: DataType.text,
implementation: () => 'pgmem'
});
db.public.interceptQueries(sql => {
// see https://github.com/pgjdbc/pgjdbc/blob/fc60537c8e2c40b7da6a952ca2ba4a12f2d5ae86/pgjdbc/src/main/java/org/postgresql/jdbc/PgConnection.java#L820
// and https://github.com/pgjdbc/pgjdbc/blob/fc60537c8e2c40b7da6a952ca2ba4a12f2d5ae86/pgjdbc/src/main/java/org/postgresql/jdbc/TypeInfoCache.java#L252
if (sql.includes('current_schemas(')) {
return [{ oid: 603, typname: 'box' }];
}
return undefined;
})
db.adapters.bindServer({ port: 52932 })
.then(v => console.log('Server at', v))
.catch(e => console.error('Error', e));