@or-sdk/pgsql 1.0.3-beta.1942.0 → 1.0.3-beta.1943.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 +35 -122
- package/dist/cjs/Pgsql.js.map +1 -1
- 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/getDeleteRowsQuery.js +9 -0
- package/dist/cjs/utils/getDeleteRowsQuery.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/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/index.js +13 -1
- package/dist/cjs/utils/index.js.map +1 -1
- package/dist/esm/Pgsql.js +28 -101
- package/dist/esm/Pgsql.js.map +1 -1
- 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/getDeleteRowsQuery.js +8 -0
- package/dist/esm/utils/getDeleteRowsQuery.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/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/index.js +6 -0
- package/dist/esm/utils/index.js.map +1 -1
- package/dist/types/Pgsql.d.ts +18 -28
- package/dist/types/Pgsql.d.ts.map +1 -1
- package/dist/types/types.d.ts +13 -20
- package/dist/types/types.d.ts.map +1 -1
- package/dist/types/utils/encodeValue.d.ts +4 -0
- package/dist/types/utils/encodeValue.d.ts.map +1 -0
- package/dist/types/utils/extractDatabaseNames.d.ts +4 -0
- package/dist/types/utils/extractDatabaseNames.d.ts.map +1 -0
- package/dist/types/utils/getDeleteRowsQuery.d.ts +4 -0
- package/dist/types/utils/getDeleteRowsQuery.d.ts.map +1 -0
- package/dist/types/utils/getEditRowQuery.d.ts +4 -0
- package/dist/types/utils/getEditRowQuery.d.ts.map +1 -0
- package/dist/types/utils/getInsertKeys.d.ts +4 -0
- package/dist/types/utils/getInsertKeys.d.ts.map +1 -0
- package/dist/types/utils/getInsertQueries.d.ts +4 -0
- package/dist/types/utils/getInsertQueries.d.ts.map +1 -0
- package/dist/types/utils/index.d.ts +6 -0
- package/dist/types/utils/index.d.ts.map +1 -1
- package/package.json +2 -2
- package/src/Pgsql.ts +59 -146
- package/src/types.ts +18 -20
- package/src/utils/encodeValue.ts +11 -0
- package/src/utils/extractDatabaseNames.ts +7 -0
- package/src/utils/getDeleteRowsQuery.ts +10 -0
- package/src/utils/getEditRowQuery.ts +12 -0
- package/src/utils/getInsertKeys.ts +9 -0
- package/src/utils/getInsertQueries.ts +18 -0
- package/src/utils/index.ts +6 -0
package/src/types.ts
CHANGED
|
@@ -25,6 +25,11 @@ export type PgsqlConfig = {
|
|
|
25
25
|
* Api version
|
|
26
26
|
*/
|
|
27
27
|
version?: string;
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* use external database
|
|
31
|
+
*/
|
|
32
|
+
isExternal?: boolean;
|
|
28
33
|
};
|
|
29
34
|
|
|
30
35
|
export type QueryField = {
|
|
@@ -49,9 +54,18 @@ export type ExecuteQueryResponse<T> = {
|
|
|
49
54
|
|
|
50
55
|
export type ExecuteQueryArgs = {
|
|
51
56
|
query: string;
|
|
52
|
-
params?:
|
|
57
|
+
params?: {
|
|
58
|
+
[key: string]: unknown;
|
|
59
|
+
};
|
|
53
60
|
database: string;
|
|
54
|
-
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
export type CreateDatabaseResponse = {
|
|
64
|
+
ok: number;
|
|
65
|
+
result: [
|
|
66
|
+
unknown[],
|
|
67
|
+
unknown[],
|
|
68
|
+
];
|
|
55
69
|
};
|
|
56
70
|
|
|
57
71
|
export type ListDatabasesResponseItem = {
|
|
@@ -73,7 +87,7 @@ export type GetDBSizeResult = {
|
|
|
73
87
|
|
|
74
88
|
export type TableColumn = {
|
|
75
89
|
name: string;
|
|
76
|
-
type:
|
|
90
|
+
type: number;
|
|
77
91
|
};
|
|
78
92
|
|
|
79
93
|
export type CreateTableArgs = {
|
|
@@ -82,7 +96,6 @@ export type CreateTableArgs = {
|
|
|
82
96
|
table: string;
|
|
83
97
|
columns: TableColumn[];
|
|
84
98
|
primaryKey: string;
|
|
85
|
-
isExternal: boolean;
|
|
86
99
|
};
|
|
87
100
|
|
|
88
101
|
export type AddColumnsArgs = {
|
|
@@ -90,7 +103,6 @@ export type AddColumnsArgs = {
|
|
|
90
103
|
schema: string;
|
|
91
104
|
table: string;
|
|
92
105
|
columns: TableColumn[];
|
|
93
|
-
isExternal: boolean;
|
|
94
106
|
};
|
|
95
107
|
|
|
96
108
|
export type Row = {
|
|
@@ -106,19 +118,9 @@ export type InsertArgs = {
|
|
|
106
118
|
context?: {
|
|
107
119
|
progress?: number;
|
|
108
120
|
};
|
|
109
|
-
isExternal: boolean;
|
|
110
121
|
};
|
|
111
122
|
|
|
112
|
-
export type
|
|
113
|
-
database: string;
|
|
114
|
-
schema: string;
|
|
115
|
-
table: string;
|
|
116
|
-
isExternal: boolean;
|
|
117
|
-
chunkSize?: number;
|
|
118
|
-
offset?: number;
|
|
119
|
-
limit?: number;
|
|
120
|
-
params?: any[];
|
|
121
|
-
};
|
|
123
|
+
export type EncodedValue = string | number | boolean;
|
|
122
124
|
|
|
123
125
|
export type SelectAllArgs = {
|
|
124
126
|
database: string;
|
|
@@ -130,8 +132,6 @@ export type SelectAllArgs = {
|
|
|
130
132
|
chunkSize?: number;
|
|
131
133
|
offset?: number;
|
|
132
134
|
limit?: number;
|
|
133
|
-
isExternal: boolean;
|
|
134
|
-
params?: any[];
|
|
135
135
|
};
|
|
136
136
|
|
|
137
137
|
export type EditRowArgs = {
|
|
@@ -142,7 +142,6 @@ export type EditRowArgs = {
|
|
|
142
142
|
value: unknown;
|
|
143
143
|
row: Row;
|
|
144
144
|
primaryKeys: string;
|
|
145
|
-
isExternal: boolean;
|
|
146
145
|
};
|
|
147
146
|
|
|
148
147
|
export type DeleteRowsArgs = {
|
|
@@ -151,7 +150,6 @@ export type DeleteRowsArgs = {
|
|
|
151
150
|
table: string;
|
|
152
151
|
rows: Row[];
|
|
153
152
|
primaryKeys: string;
|
|
154
|
-
isExternal: boolean;
|
|
155
153
|
};
|
|
156
154
|
|
|
157
155
|
export type DatabaseItem = {
|
|
@@ -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,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,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;
|
package/src/utils/index.ts
CHANGED
|
@@ -2,6 +2,9 @@
|
|
|
2
2
|
* @internal
|
|
3
3
|
*/
|
|
4
4
|
|
|
5
|
+
export { default as extractDatabaseNames } from './extractDatabaseNames';
|
|
6
|
+
export { default as getInsertKeys } from './getInsertKeys';
|
|
7
|
+
export { default as encodeValue } from './encodeValue';
|
|
5
8
|
export { default as splitPrimaryKeys } from './splitPrimaryKeys';
|
|
6
9
|
export { default as getListTablesQuery } from './getListTablesQuery';
|
|
7
10
|
export { default as createSchemaQuery } from './createSchemaQuery';
|
|
@@ -9,7 +12,10 @@ export { default as getDropSchemaQuery } from './getDropSchemaQuery';
|
|
|
9
12
|
export { default as getCreateTableQuery } from './getCreateTableQuery';
|
|
10
13
|
export { default as getDropTableQuery } from './getDropTableQuery';
|
|
11
14
|
export { default as getAddColumnsQuery } from './getAddColumnsQuery';
|
|
15
|
+
export { default as getInsertQueries } from './getInsertQueries';
|
|
12
16
|
export { default as getSelectAllCountQuery } from './getSelectAllCountQuery';
|
|
13
17
|
export { default as getSelectAllQuery } from './getSelectAllQuery';
|
|
18
|
+
export { default as getEditRowQuery } from './getEditRowQuery';
|
|
19
|
+
export { default as getDeleteRowsQuery } from './getDeleteRowsQuery';
|
|
14
20
|
export { default as getGenerateTableSchemaQuery } from './getGenerateTableSchemaQuery';
|
|
15
21
|
export { default as getGetPrimaryKeysQuery } from './getGetPrimaryKeysQuery';
|