@or-sdk/pgsql 0.22.5-244.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/Pgsql.js +370 -0
- package/dist/cjs/Pgsql.js.map +1 -0
- package/dist/cjs/constants.js +6 -0
- package/dist/cjs/constants.js.map +1 -0
- package/dist/cjs/index.js +21 -0
- package/dist/cjs/index.js.map +1 -0
- package/dist/cjs/types.js +3 -0
- package/dist/cjs/types.js.map +1 -0
- package/dist/cjs/utils/createSchemaQuery.js +4 -0
- package/dist/cjs/utils/createSchemaQuery.js.map +1 -0
- package/dist/cjs/utils/encodeValue.js +17 -0
- package/dist/cjs/utils/encodeValue.js.map +1 -0
- package/dist/cjs/utils/extractDatabaseNames.js +10 -0
- package/dist/cjs/utils/extractDatabaseNames.js.map +1 -0
- package/dist/cjs/utils/getAddColumnsQuery.js +6 -0
- package/dist/cjs/utils/getAddColumnsQuery.js.map +1 -0
- package/dist/cjs/utils/getCreateTableQuery.js +6 -0
- package/dist/cjs/utils/getCreateTableQuery.js.map +1 -0
- package/dist/cjs/utils/getDeleteRowsQuery.js +9 -0
- package/dist/cjs/utils/getDeleteRowsQuery.js.map +1 -0
- package/dist/cjs/utils/getDropSchemaQuery.js +4 -0
- package/dist/cjs/utils/getDropSchemaQuery.js.map +1 -0
- package/dist/cjs/utils/getDropTableQuery.js +4 -0
- package/dist/cjs/utils/getDropTableQuery.js.map +1 -0
- package/dist/cjs/utils/getEditRowQuery.js +10 -0
- package/dist/cjs/utils/getEditRowQuery.js.map +1 -0
- package/dist/cjs/utils/getGenerateTableSchemaQuery.js +4 -0
- package/dist/cjs/utils/getGenerateTableSchemaQuery.js.map +1 -0
- package/dist/cjs/utils/getGetPrimaryKeysQuery.js +4 -0
- package/dist/cjs/utils/getGetPrimaryKeysQuery.js.map +1 -0
- package/dist/cjs/utils/getInsertKeys.js +9 -0
- package/dist/cjs/utils/getInsertKeys.js.map +1 -0
- package/dist/cjs/utils/getInsertQueries.js +18 -0
- package/dist/cjs/utils/getInsertQueries.js.map +1 -0
- package/dist/cjs/utils/getListTablesQuery.js +4 -0
- package/dist/cjs/utils/getListTablesQuery.js.map +1 -0
- package/dist/cjs/utils/getSelectAllCountQuery.js +4 -0
- package/dist/cjs/utils/getSelectAllCountQuery.js.map +1 -0
- package/dist/cjs/utils/getSelectAllQuery.js +4 -0
- package/dist/cjs/utils/getSelectAllQuery.js.map +1 -0
- package/dist/cjs/utils/index.js +41 -0
- package/dist/cjs/utils/index.js.map +1 -0
- package/dist/cjs/utils/splitPrimaryKeys.js +8 -0
- package/dist/cjs/utils/splitPrimaryKeys.js.map +1 -0
- package/dist/esm/Pgsql.js +218 -0
- package/dist/esm/Pgsql.js.map +1 -0
- package/dist/esm/constants.js +4 -0
- package/dist/esm/constants.js.map +1 -0
- package/dist/esm/index.js +3 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/types.js +2 -0
- package/dist/esm/types.js.map +1 -0
- package/dist/esm/utils/createSchemaQuery.js +2 -0
- package/dist/esm/utils/createSchemaQuery.js.map +1 -0
- package/dist/esm/utils/encodeValue.js +12 -0
- package/dist/esm/utils/encodeValue.js.map +1 -0
- package/dist/esm/utils/extractDatabaseNames.js +5 -0
- package/dist/esm/utils/extractDatabaseNames.js.map +1 -0
- package/dist/esm/utils/getAddColumnsQuery.js +2 -0
- package/dist/esm/utils/getAddColumnsQuery.js.map +1 -0
- package/dist/esm/utils/getCreateTableQuery.js +2 -0
- package/dist/esm/utils/getCreateTableQuery.js.map +1 -0
- package/dist/esm/utils/getDeleteRowsQuery.js +8 -0
- package/dist/esm/utils/getDeleteRowsQuery.js.map +1 -0
- package/dist/esm/utils/getDropSchemaQuery.js +2 -0
- package/dist/esm/utils/getDropSchemaQuery.js.map +1 -0
- package/dist/esm/utils/getDropTableQuery.js +2 -0
- package/dist/esm/utils/getDropTableQuery.js.map +1 -0
- package/dist/esm/utils/getEditRowQuery.js +10 -0
- package/dist/esm/utils/getEditRowQuery.js.map +1 -0
- package/dist/esm/utils/getGenerateTableSchemaQuery.js +18 -0
- package/dist/esm/utils/getGenerateTableSchemaQuery.js.map +1 -0
- package/dist/esm/utils/getGetPrimaryKeysQuery.js +9 -0
- package/dist/esm/utils/getGetPrimaryKeysQuery.js.map +1 -0
- package/dist/esm/utils/getInsertKeys.js +7 -0
- package/dist/esm/utils/getInsertKeys.js.map +1 -0
- package/dist/esm/utils/getInsertQueries.js +14 -0
- package/dist/esm/utils/getInsertQueries.js.map +1 -0
- package/dist/esm/utils/getListTablesQuery.js +4 -0
- package/dist/esm/utils/getListTablesQuery.js.map +1 -0
- package/dist/esm/utils/getSelectAllCountQuery.js +2 -0
- package/dist/esm/utils/getSelectAllCountQuery.js.map +1 -0
- package/dist/esm/utils/getSelectAllQuery.js +2 -0
- package/dist/esm/utils/getSelectAllQuery.js.map +1 -0
- package/dist/esm/utils/index.js +18 -0
- package/dist/esm/utils/index.js.map +1 -0
- package/dist/esm/utils/splitPrimaryKeys.js +3 -0
- package/dist/esm/utils/splitPrimaryKeys.js.map +1 -0
- package/dist/types/Pgsql.d.ts +51 -0
- package/dist/types/constants.d.ts +2 -0
- package/dist/types/index.d.ts +2 -0
- package/dist/types/types.d.ts +110 -0
- package/dist/types/utils/createSchemaQuery.d.ts +2 -0
- package/dist/types/utils/encodeValue.d.ts +3 -0
- package/dist/types/utils/extractDatabaseNames.d.ts +3 -0
- package/dist/types/utils/getAddColumnsQuery.d.ts +3 -0
- package/dist/types/utils/getCreateTableQuery.d.ts +3 -0
- package/dist/types/utils/getDeleteRowsQuery.d.ts +3 -0
- package/dist/types/utils/getDropSchemaQuery.d.ts +2 -0
- package/dist/types/utils/getDropTableQuery.d.ts +2 -0
- package/dist/types/utils/getEditRowQuery.d.ts +3 -0
- package/dist/types/utils/getGenerateTableSchemaQuery.d.ts +2 -0
- package/dist/types/utils/getGetPrimaryKeysQuery.d.ts +2 -0
- package/dist/types/utils/getInsertKeys.d.ts +3 -0
- package/dist/types/utils/getInsertQueries.d.ts +3 -0
- package/dist/types/utils/getListTablesQuery.d.ts +2 -0
- package/dist/types/utils/getSelectAllCountQuery.d.ts +2 -0
- package/dist/types/utils/getSelectAllQuery.d.ts +2 -0
- package/dist/types/utils/index.d.ts +17 -0
- package/dist/types/utils/splitPrimaryKeys.d.ts +2 -0
- package/package.json +30 -0
- package/src/Pgsql.ts +405 -0
- package/src/constants.ts +4 -0
- package/src/index.ts +2 -0
- package/src/types.ts +143 -0
- package/src/utils/createSchemaQuery.ts +1 -0
- package/src/utils/encodeValue.ts +11 -0
- package/src/utils/extractDatabaseNames.ts +7 -0
- package/src/utils/getAddColumnsQuery.ts +4 -0
- package/src/utils/getCreateTableQuery.ts +4 -0
- package/src/utils/getDeleteRowsQuery.ts +10 -0
- package/src/utils/getDropSchemaQuery.ts +1 -0
- package/src/utils/getDropTableQuery.ts +1 -0
- package/src/utils/getEditRowQuery.ts +12 -0
- package/src/utils/getGenerateTableSchemaQuery.ts +17 -0
- package/src/utils/getGetPrimaryKeysQuery.ts +8 -0
- package/src/utils/getInsertKeys.ts +9 -0
- package/src/utils/getInsertQueries.ts +18 -0
- package/src/utils/getListTablesQuery.ts +3 -0
- package/src/utils/getSelectAllCountQuery.ts +1 -0
- package/src/utils/getSelectAllQuery.ts +1 -0
- package/src/utils/index.ts +21 -0
- package/src/utils/splitPrimaryKeys.ts +3 -0
- package/tsconfig.esm.json +9 -0
- package/tsconfig.json +7 -0
- package/tsconfig.types.json +9 -0
package/src/types.ts
ADDED
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
import { Token } from '@or-sdk/base';
|
|
2
|
+
|
|
3
|
+
export type PgsqlConfig = {
|
|
4
|
+
/**
|
|
5
|
+
* token
|
|
6
|
+
*/
|
|
7
|
+
token: Token;
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Url of OneReach service discovery api
|
|
11
|
+
*/
|
|
12
|
+
discoveryUrl?: string;
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Account ID for cross-account requests (super admin only)
|
|
16
|
+
*/
|
|
17
|
+
accountId?: string;
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Url of OneReach Pgsql api
|
|
21
|
+
*/
|
|
22
|
+
pgsqlUrl?: string;
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
export type QueryField = {
|
|
26
|
+
name: string;
|
|
27
|
+
tableID: number;
|
|
28
|
+
columnID: number;
|
|
29
|
+
dataTypeID: number;
|
|
30
|
+
dataTypeSize: number;
|
|
31
|
+
dataTypeModifier: number;
|
|
32
|
+
format: string;
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
export type ExecuteQueryResponse<T> = {
|
|
36
|
+
command: string;
|
|
37
|
+
rowCount: null | number;
|
|
38
|
+
oid: null | number;
|
|
39
|
+
rows: T[];
|
|
40
|
+
fields: QueryField[];
|
|
41
|
+
RowCtor: null | unknown;
|
|
42
|
+
rowAsArray: boolean;
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
export type ExecuteQueryArgs = {
|
|
46
|
+
query: string;
|
|
47
|
+
params?: {
|
|
48
|
+
[key: string]: unknown;
|
|
49
|
+
};
|
|
50
|
+
database: string;
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
export type CreateDatabaseResponse = {
|
|
54
|
+
ok: number;
|
|
55
|
+
result: [
|
|
56
|
+
unknown[],
|
|
57
|
+
unknown[]
|
|
58
|
+
];
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
export type ListDatabasesResponseItem = {
|
|
62
|
+
name: string;
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
export type ListDatabasesResponse = {
|
|
66
|
+
databases: ListDatabasesResponseItem[];
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
export type DropDatabaseResponse = {
|
|
70
|
+
ok?: number;
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
export type GetDBSizeResult = {
|
|
74
|
+
usedSize: number;
|
|
75
|
+
sizeLimit: string | number;
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
export type TableColumn = {
|
|
79
|
+
name: string;
|
|
80
|
+
type: number;
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
export type CreateTableArgs = {
|
|
84
|
+
database: string;
|
|
85
|
+
schema: string;
|
|
86
|
+
table: string;
|
|
87
|
+
columns: TableColumn[];
|
|
88
|
+
primaryKey: string;
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
export type AddColumnsArgs = {
|
|
92
|
+
database: string;
|
|
93
|
+
schema: string;
|
|
94
|
+
table: string;
|
|
95
|
+
columns: TableColumn[];
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
export type Row = {
|
|
99
|
+
[key: string]: unknown;
|
|
100
|
+
};
|
|
101
|
+
|
|
102
|
+
export type InsertArgs = {
|
|
103
|
+
database: string;
|
|
104
|
+
schema: string;
|
|
105
|
+
table: string;
|
|
106
|
+
rows: Row[];
|
|
107
|
+
chunkSize: number;
|
|
108
|
+
context?: {
|
|
109
|
+
progress?: number;
|
|
110
|
+
};
|
|
111
|
+
};
|
|
112
|
+
|
|
113
|
+
export type EncodedValue = string | number | boolean;
|
|
114
|
+
|
|
115
|
+
export type SelectAllArgs = {
|
|
116
|
+
database: string;
|
|
117
|
+
query: string;
|
|
118
|
+
context?: {
|
|
119
|
+
progress?: number;
|
|
120
|
+
total?: number;
|
|
121
|
+
};
|
|
122
|
+
chunkSize?: number;
|
|
123
|
+
offset?: number;
|
|
124
|
+
limit?: number;
|
|
125
|
+
};
|
|
126
|
+
|
|
127
|
+
export type EditRowArgs = {
|
|
128
|
+
database: string;
|
|
129
|
+
schema: string;
|
|
130
|
+
table: string;
|
|
131
|
+
key: string;
|
|
132
|
+
value: unknown;
|
|
133
|
+
row: Row;
|
|
134
|
+
primaryKeys: string;
|
|
135
|
+
};
|
|
136
|
+
|
|
137
|
+
export type DeleteRowsArgs = {
|
|
138
|
+
database: string;
|
|
139
|
+
schema: string;
|
|
140
|
+
table: string;
|
|
141
|
+
rows: Row[];
|
|
142
|
+
primaryKeys: string;
|
|
143
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export default (schema: string) => `create schema ${schema};`;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { EncodedValue } from '../types';
|
|
2
|
+
import _ from 'lodash';
|
|
3
|
+
|
|
4
|
+
function encodeValue(value: unknown): EncodedValue {
|
|
5
|
+
if (_.isUndefined(value) || value === '' || _.isNull(value)) return 'NULL';
|
|
6
|
+
if (_.isString(value)) return `'${value.replaceAll('\'', '\'\'')}'`;
|
|
7
|
+
if (_.isNumber(value) || _.isBoolean(value)) return value;
|
|
8
|
+
return `'${JSON.stringify(value).replaceAll('\'', '\'\'')}'`;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export default encodeValue;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { Row } from '../types';
|
|
2
|
+
import { encodeValue, splitPrimaryKeys } from '../utils';
|
|
3
|
+
|
|
4
|
+
function getDeleteRowQuery(schema: string, table: string, rows: Row[], primaryKeys: string): string {
|
|
5
|
+
const pk = splitPrimaryKeys(primaryKeys);
|
|
6
|
+
return `delete from ${schema}.${table}
|
|
7
|
+
where (${rows.map(row => `${pk.map(primaryKey => `${primaryKey} = ${encodeValue(row[primaryKey])}`).join(' AND ')}`).join(') \nOR (')})`;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export default getDeleteRowQuery;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export default (schema: string) => `drop schema ${schema} cascade;`;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export default (schema: string, table: string) => `drop table ${schema}.${table};`;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { encodeValue, splitPrimaryKeys } from '../utils';
|
|
2
|
+
import { Row } from '../types';
|
|
3
|
+
|
|
4
|
+
function getEditRowQuery(schema: string, table: string, key: string, value: unknown, row: Row, primaryKeys: string): string {
|
|
5
|
+
value = encodeValue(value);
|
|
6
|
+
const pk = splitPrimaryKeys(primaryKeys);
|
|
7
|
+
return `update ${schema}.${table}
|
|
8
|
+
set ${key} = ${value}
|
|
9
|
+
where ${pk.map(primaryKey => `${primaryKey} = ${encodeValue(row[primaryKey])}`).join(' AND ')}`;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export default getEditRowQuery;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export default (schema: string, table: string) => `SELECT 'CREATE TABLE ' || '${schema}.${table}' || ' (' || '\n' || '' ||
|
|
2
|
+
string_agg(column_list.column_expr, ', ' || '\n' || '') ||
|
|
3
|
+
'' || ',\n' || 'PRIMARY KEY (' || (SELECT string_agg(a.attname, ', ') AS pk
|
|
4
|
+
FROM
|
|
5
|
+
pg_constraint AS c
|
|
6
|
+
CROSS JOIN LATERAL UNNEST(c.conkey) AS cols(colnum) -- conkey is a list of the columns of the constraint; so we split it into rows so that we can join all column numbers onto their names in pg_attribute
|
|
7
|
+
INNER JOIN pg_attribute AS a ON a.attrelid = c.conrelid AND cols.colnum = a.attnum
|
|
8
|
+
WHERE
|
|
9
|
+
c.contype = 'p' -- p = primary key constraint
|
|
10
|
+
AND c.conrelid = '${schema}.${table}'::REGCLASS limit 1) || '));' code
|
|
11
|
+
FROM (
|
|
12
|
+
SELECT ' ' || column_name || ' ' || data_type ||
|
|
13
|
+
coalesce('(' || character_maximum_length || ')', '') ||
|
|
14
|
+
case when is_nullable = 'YES' then '' else ' NOT NULL' end as column_expr
|
|
15
|
+
FROM information_schema.columns
|
|
16
|
+
WHERE table_schema = '${schema}' AND table_name = '${table}'
|
|
17
|
+
ORDER BY ordinal_position) column_list;`;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export default (schema: string, table: string) => `SELECT string_agg(a.attname, ', ') AS pk
|
|
2
|
+
FROM
|
|
3
|
+
pg_constraint AS c
|
|
4
|
+
CROSS JOIN LATERAL UNNEST(c.conkey) AS cols(colnum) -- conkey is a list of the columns of the constraint; so we split it into rows so that we can join all column numbers onto their names in pg_attribute
|
|
5
|
+
INNER JOIN pg_attribute AS a ON a.attrelid = c.conrelid AND cols.colnum = a.attnum
|
|
6
|
+
WHERE
|
|
7
|
+
c.contype = 'p' -- p = primary key constraint
|
|
8
|
+
AND c.conrelid = '${schema}.${table}'::REGCLASS;`;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { Row } from '../types';
|
|
2
|
+
import { encodeValue, getInsertKeys } from '../utils';
|
|
3
|
+
import _ from 'lodash';
|
|
4
|
+
|
|
5
|
+
function getInsertQueries(schema: string, table: string, insertRows: Row[], chunkSize: number): string[] {
|
|
6
|
+
const rows = _.isArray(insertRows) ? insertRows : [insertRows];
|
|
7
|
+
|
|
8
|
+
const keys = getInsertKeys(rows);
|
|
9
|
+
const stringifyRow = (row: Row) => `${keys.map(key => encodeValue(row[key])).join(', ')}`;
|
|
10
|
+
|
|
11
|
+
return _.chain(rows)
|
|
12
|
+
.chunk(chunkSize)
|
|
13
|
+
.map((chunk) => `insert into ${schema}.${table} (${keys.join(', ')})
|
|
14
|
+
values (${chunk.map(stringifyRow).join('), (')});`)
|
|
15
|
+
.value();
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export default getInsertQueries;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export default (query: string) => `select count(*) from (${query}) x`;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export default (query: string, offset: number, chunkSize: number) => `${query} offset ${offset} limit ${chunkSize}`;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @internal
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
export { default as extractDatabaseNames } from './extractDatabaseNames';
|
|
6
|
+
export { default as getInsertKeys } from './getInsertKeys';
|
|
7
|
+
export { default as encodeValue } from './encodeValue';
|
|
8
|
+
export { default as splitPrimaryKeys } from './splitPrimaryKeys';
|
|
9
|
+
export { default as getListTablesQuery } from './getListTablesQuery';
|
|
10
|
+
export { default as createSchemaQuery } from './createSchemaQuery';
|
|
11
|
+
export { default as getDropSchemaQuery } from './getDropSchemaQuery';
|
|
12
|
+
export { default as getCreateTableQuery } from './getCreateTableQuery';
|
|
13
|
+
export { default as getDropTableQuery } from './getDropTableQuery';
|
|
14
|
+
export { default as getAddColumnsQuery } from './getAddColumnsQuery';
|
|
15
|
+
export { default as getInsertQueries } from './getInsertQueries';
|
|
16
|
+
export { default as getSelectAllCountQuery } from './getSelectAllCountQuery';
|
|
17
|
+
export { default as getSelectAllQuery } from './getSelectAllQuery';
|
|
18
|
+
export { default as getEditRowQuery } from './getEditRowQuery';
|
|
19
|
+
export { default as getDeleteRowsQuery } from './getDeleteRowsQuery';
|
|
20
|
+
export { default as getGenerateTableSchemaQuery } from './getGenerateTableSchemaQuery';
|
|
21
|
+
export { default as getGetPrimaryKeysQuery } from './getGetPrimaryKeysQuery';
|
package/tsconfig.json
ADDED