@or-sdk/pgsql 1.0.3-beta.1953.0 → 1.0.3-beta.2023.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 +135 -37
- package/dist/cjs/Pgsql.js.map +1 -1
- package/dist/cjs/utils/index.js +1 -13
- package/dist/cjs/utils/index.js.map +1 -1
- package/dist/esm/Pgsql.js +110 -29
- package/dist/esm/Pgsql.js.map +1 -1
- package/dist/esm/utils/index.js +0 -6
- package/dist/esm/utils/index.js.map +1 -1
- package/dist/types/Pgsql.d.ts +29 -18
- package/dist/types/Pgsql.d.ts.map +1 -1
- package/dist/types/types.d.ts +37 -14
- package/dist/types/types.d.ts.map +1 -1
- package/dist/types/utils/index.d.ts +0 -6
- package/dist/types/utils/index.d.ts.map +1 -1
- package/package.json +2 -2
- package/src/Pgsql.ts +163 -69
- package/src/types.ts +38 -19
- package/src/utils/index.ts +0 -6
- package/dist/cjs/utils/encodeValue.js +0 -17
- package/dist/cjs/utils/encodeValue.js.map +0 -1
- package/dist/cjs/utils/extractDatabaseNames.js +0 -10
- package/dist/cjs/utils/extractDatabaseNames.js.map +0 -1
- package/dist/cjs/utils/getDeleteRowsQuery.js +0 -9
- package/dist/cjs/utils/getDeleteRowsQuery.js.map +0 -1
- package/dist/cjs/utils/getEditRowQuery.js +0 -10
- package/dist/cjs/utils/getEditRowQuery.js.map +0 -1
- package/dist/cjs/utils/getInsertKeys.js +0 -9
- package/dist/cjs/utils/getInsertKeys.js.map +0 -1
- package/dist/cjs/utils/getInsertQueries.js +0 -18
- package/dist/cjs/utils/getInsertQueries.js.map +0 -1
- package/dist/esm/utils/encodeValue.js +0 -12
- package/dist/esm/utils/encodeValue.js.map +0 -1
- package/dist/esm/utils/extractDatabaseNames.js +0 -5
- package/dist/esm/utils/extractDatabaseNames.js.map +0 -1
- package/dist/esm/utils/getDeleteRowsQuery.js +0 -8
- package/dist/esm/utils/getDeleteRowsQuery.js.map +0 -1
- package/dist/esm/utils/getEditRowQuery.js +0 -10
- package/dist/esm/utils/getEditRowQuery.js.map +0 -1
- package/dist/esm/utils/getInsertKeys.js +0 -7
- package/dist/esm/utils/getInsertKeys.js.map +0 -1
- package/dist/esm/utils/getInsertQueries.js +0 -14
- package/dist/esm/utils/getInsertQueries.js.map +0 -1
- package/dist/types/utils/encodeValue.d.ts +0 -4
- package/dist/types/utils/encodeValue.d.ts.map +0 -1
- package/dist/types/utils/extractDatabaseNames.d.ts +0 -4
- package/dist/types/utils/extractDatabaseNames.d.ts.map +0 -1
- package/dist/types/utils/getDeleteRowsQuery.d.ts +0 -4
- package/dist/types/utils/getDeleteRowsQuery.d.ts.map +0 -1
- package/dist/types/utils/getEditRowQuery.d.ts +0 -4
- package/dist/types/utils/getEditRowQuery.d.ts.map +0 -1
- package/dist/types/utils/getInsertKeys.d.ts +0 -4
- package/dist/types/utils/getInsertKeys.d.ts.map +0 -1
- package/dist/types/utils/getInsertQueries.d.ts +0 -4
- package/dist/types/utils/getInsertQueries.d.ts.map +0 -1
- package/src/utils/encodeValue.ts +0 -11
- package/src/utils/extractDatabaseNames.ts +0 -7
- package/src/utils/getDeleteRowsQuery.ts +0 -10
- package/src/utils/getEditRowQuery.ts +0 -12
- package/src/utils/getInsertKeys.ts +0 -9
- package/src/utils/getInsertQueries.ts +0 -18
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@or-sdk/pgsql",
|
|
3
|
-
"version": "1.0.3-beta.
|
|
3
|
+
"version": "1.0.3-beta.2023.0",
|
|
4
4
|
"main": "dist/cjs/index.js",
|
|
5
5
|
"module": "dist/esm/index.js",
|
|
6
6
|
"types": "dist/types/index.d.ts",
|
|
@@ -17,7 +17,7 @@
|
|
|
17
17
|
"dev": "pnpm build:watch:esm"
|
|
18
18
|
},
|
|
19
19
|
"dependencies": {
|
|
20
|
-
"@or-sdk/base": "^0.29.
|
|
20
|
+
"@or-sdk/base": "^0.29.0",
|
|
21
21
|
"lodash": "^4.17.21"
|
|
22
22
|
},
|
|
23
23
|
"devDependencies": {
|
package/src/Pgsql.ts
CHANGED
|
@@ -1,33 +1,30 @@
|
|
|
1
|
+
import _ from 'lodash';
|
|
1
2
|
import { Base, CalApiParams, List, MakeApiUrlData, makeList } from '@or-sdk/base';
|
|
2
3
|
import { LIST_SCHEMAS_QUERY, SERVICE_KEY } from './constants';
|
|
3
4
|
import {
|
|
4
5
|
AddColumnsArgs,
|
|
5
6
|
CreateDatabaseParams,
|
|
6
|
-
|
|
7
|
+
CreateTableArgs,
|
|
7
8
|
DatabaseItem,
|
|
8
9
|
DeleteRowsArgs, DropDatabaseResponse, EditRowArgs,
|
|
9
|
-
ExecuteQueryArgs, ExecuteQueryResponse, GetDBSizeResult, InsertArgs,
|
|
10
|
+
ExecuteQueryArgs, ExecuteQueryResponse, GetDatabaseConnectionResponse, GetDBSizeResult, GetTableDataProps, InsertArgs,
|
|
10
11
|
ListDatabasesResponse,
|
|
11
|
-
PgsqlConfig, Row, SelectAllArgs,
|
|
12
|
+
PgsqlConfig, QueryField, Row, SelectAllArgs,
|
|
12
13
|
} from './types';
|
|
13
14
|
import {
|
|
14
15
|
createSchemaQuery,
|
|
15
16
|
getAddColumnsQuery,
|
|
16
17
|
getCreateTableQuery,
|
|
17
|
-
getDeleteRowsQuery,
|
|
18
18
|
getDropSchemaQuery,
|
|
19
|
-
getDropTableQuery,
|
|
19
|
+
getDropTableQuery, getGenerateTableSchemaQuery,
|
|
20
20
|
getGetPrimaryKeysQuery,
|
|
21
|
-
|
|
22
|
-
getListTablesQuery, getSelectAllCountQuery, getSelectAllQuery,
|
|
21
|
+
getListTablesQuery, getSelectAllCountQuery, getSelectAllQuery, splitPrimaryKeys,
|
|
23
22
|
} from './utils';
|
|
24
23
|
|
|
25
24
|
|
|
26
25
|
export class Pgsql extends Base {
|
|
27
|
-
private isExternal?: boolean;
|
|
28
|
-
|
|
29
26
|
constructor(params: PgsqlConfig) {
|
|
30
|
-
const { token, discoveryUrl, accountId, pgsqlUrl, version = 'master'
|
|
27
|
+
const { token, discoveryUrl, accountId, pgsqlUrl, version = 'master' } = params;
|
|
31
28
|
super({
|
|
32
29
|
token,
|
|
33
30
|
discoveryUrl,
|
|
@@ -36,7 +33,6 @@ export class Pgsql extends Base {
|
|
|
36
33
|
serviceUrl: pgsqlUrl,
|
|
37
34
|
feature: version,
|
|
38
35
|
});
|
|
39
|
-
this.isExternal = isExternal;
|
|
40
36
|
}
|
|
41
37
|
|
|
42
38
|
/**
|
|
@@ -56,7 +52,7 @@ export class Pgsql extends Base {
|
|
|
56
52
|
(params.params as any).feature = this.feature;
|
|
57
53
|
}
|
|
58
54
|
|
|
59
|
-
return this.
|
|
55
|
+
return this.callApiV2<T>(params);
|
|
60
56
|
}
|
|
61
57
|
|
|
62
58
|
makeApiUrl(data: MakeApiUrlData): string {
|
|
@@ -70,17 +66,19 @@ export class Pgsql extends Base {
|
|
|
70
66
|
* ```typescript
|
|
71
67
|
* const result = await pgsql.executeQuery({
|
|
72
68
|
* query: 'select * from table',
|
|
73
|
-
* database: 'db-name'
|
|
69
|
+
* database: 'db-name',
|
|
70
|
+
* isExternal: false,
|
|
71
|
+
* params: [],
|
|
74
72
|
* });
|
|
75
73
|
* ```
|
|
76
74
|
*/
|
|
77
|
-
public async executeQuery<T>({ query, params, database }: ExecuteQueryArgs): Promise<ExecuteQueryResponse<T>> {
|
|
75
|
+
public async executeQuery<T>({ query, params, database, isExternal = false }: ExecuteQueryArgs): Promise<ExecuteQueryResponse<T>> {
|
|
78
76
|
return this.makeRequest<ExecuteQueryResponse<T>>({
|
|
79
77
|
data: {
|
|
80
78
|
query,
|
|
81
79
|
params,
|
|
82
80
|
database,
|
|
83
|
-
isExternal
|
|
81
|
+
isExternal,
|
|
84
82
|
},
|
|
85
83
|
method: 'POST',
|
|
86
84
|
route: 'query',
|
|
@@ -92,6 +90,7 @@ export class Pgsql extends Base {
|
|
|
92
90
|
* ```typescript
|
|
93
91
|
* const databaseList = await pgsql.listDatabases();
|
|
94
92
|
* ```
|
|
93
|
+
* @returns {Array} database names
|
|
95
94
|
*/
|
|
96
95
|
public async listDatabases(): Promise<List<DatabaseItem>> {
|
|
97
96
|
const { databases } = await this.makeRequest<ListDatabasesResponse>({
|
|
@@ -108,10 +107,12 @@ export class Pgsql extends Base {
|
|
|
108
107
|
* const result = await pgsql.createDatabase({ database: 'db-name' });
|
|
109
108
|
* ```
|
|
110
109
|
*/
|
|
111
|
-
public async createDatabase({ database, externalCredentials }: CreateDatabaseParams): Promise<
|
|
112
|
-
return this.makeRequest<
|
|
110
|
+
public async createDatabase({ database, description, imageUrl, externalCredentials }: CreateDatabaseParams): Promise<void> {
|
|
111
|
+
return this.makeRequest<void>({
|
|
113
112
|
data: {
|
|
114
113
|
database,
|
|
114
|
+
description,
|
|
115
|
+
imageUrl,
|
|
115
116
|
externalCredentials,
|
|
116
117
|
},
|
|
117
118
|
method: 'PUT',
|
|
@@ -119,26 +120,10 @@ export class Pgsql extends Base {
|
|
|
119
120
|
});
|
|
120
121
|
}
|
|
121
122
|
|
|
122
|
-
/**
|
|
123
|
-
* Get database info
|
|
124
|
-
*
|
|
125
|
-
* @deprecated route not available
|
|
126
|
-
* ```typescript
|
|
127
|
-
* const result = await pgsql.getDatabaseInfo({ database: 'db-name' });
|
|
128
|
-
* ```
|
|
129
|
-
*/
|
|
130
|
-
// TODO: uncomment or replace route?
|
|
131
|
-
// public async getDatabaseInfo({ database }: { database: string; }): Promise<unknown> {
|
|
132
|
-
// return this.callApi<unknown>({
|
|
133
|
-
// method: 'GET',
|
|
134
|
-
// route: `databases/${encodeURIComponent(database)}`
|
|
135
|
-
// })
|
|
136
|
-
// }
|
|
137
|
-
|
|
138
123
|
/**
|
|
139
124
|
* Drop database
|
|
140
125
|
* ```typescript
|
|
141
|
-
* const result = await pgsql.dropDatabase({ database: 'db-name' });
|
|
126
|
+
* const result = await pgsql.dropDatabase({ database: 'db-name', isExternal: false });
|
|
142
127
|
* ```
|
|
143
128
|
*/
|
|
144
129
|
public async dropDatabase({ database, isExternal }: { database: string; isExternal?: boolean;}): Promise<DropDatabaseResponse> {
|
|
@@ -155,67 +140,74 @@ export class Pgsql extends Base {
|
|
|
155
140
|
/**
|
|
156
141
|
* List tables
|
|
157
142
|
* ```typescript
|
|
158
|
-
* const result = await pgsql.listTables({ database: 'db-name', schema: 'schema-name' });
|
|
143
|
+
* const result = await pgsql.listTables({ database: 'db-name', schema: 'schema-name', isExternal: false });
|
|
159
144
|
* ```
|
|
160
145
|
*/
|
|
161
|
-
public async listTables({ database, schema
|
|
146
|
+
public async listTables({ database, schema, isExternal }: { database: string; schema: string; isExternal: boolean; }): Promise<List<string>> {
|
|
162
147
|
const { rows } = await this.executeQuery<{ table_name: string; }>({
|
|
163
148
|
query: getListTablesQuery(schema),
|
|
164
149
|
database,
|
|
150
|
+
isExternal,
|
|
165
151
|
});
|
|
152
|
+
|
|
166
153
|
return makeList<string>(rows.map(x => x.table_name));
|
|
167
154
|
}
|
|
168
155
|
|
|
169
156
|
/**
|
|
170
157
|
* List schemas
|
|
171
158
|
* ```typescript
|
|
172
|
-
* const result = await pgsql.listSchemas({ database: 'db-name' });
|
|
159
|
+
* const result = await pgsql.listSchemas({ database: 'db-name', isExternal: false });
|
|
173
160
|
* ```
|
|
174
161
|
*/
|
|
175
|
-
public async listSchemas({ database }: { database: string; }): Promise<List<string>> {
|
|
162
|
+
public async listSchemas({ database, isExternal }: { database: string; isExternal: boolean; }): Promise<List<string>> {
|
|
176
163
|
const { rows } = await this.executeQuery<{ schema_name: string; }>({
|
|
177
164
|
query: LIST_SCHEMAS_QUERY,
|
|
178
165
|
database,
|
|
166
|
+
isExternal,
|
|
179
167
|
});
|
|
168
|
+
|
|
180
169
|
return makeList<string>(rows.map(x => x.schema_name));
|
|
181
170
|
}
|
|
182
171
|
|
|
183
172
|
/**
|
|
184
173
|
* Create schema
|
|
185
174
|
* ```typescript
|
|
186
|
-
* const result = await pgsql.createSchema({ database: 'db-name', schema: 'schema-name' });
|
|
175
|
+
* const result = await pgsql.createSchema({ database: 'db-name', schema: 'schema-name', isExternal: false });
|
|
187
176
|
* ```
|
|
188
177
|
*/
|
|
189
|
-
public async createSchema({ database, schema }: { database: string; schema: string; }): Promise<ExecuteQueryResponse<void>> {
|
|
178
|
+
public async createSchema({ database, schema, isExternal }: { database: string; schema: string; isExternal: boolean; }): Promise<ExecuteQueryResponse<void>> {
|
|
190
179
|
return this.executeQuery<void>({
|
|
191
180
|
query: createSchemaQuery(schema),
|
|
192
181
|
database,
|
|
182
|
+
isExternal,
|
|
193
183
|
});
|
|
194
184
|
}
|
|
195
185
|
|
|
196
186
|
/**
|
|
197
187
|
* Drop schema
|
|
198
188
|
* ```typescript
|
|
199
|
-
* const result = await pgsql.dropSchema({ database: 'db-name', schema: 'schema-name' });
|
|
189
|
+
* const result = await pgsql.dropSchema({ database: 'db-name', schema: 'schema-name', isExternal: false });
|
|
200
190
|
* ```
|
|
201
191
|
*/
|
|
202
|
-
public async dropSchema({ database, schema }: { database: string; schema: string; }): Promise<ExecuteQueryResponse<void>> {
|
|
192
|
+
public async dropSchema({ database, schema, isExternal }: { database: string; schema: string; isExternal: boolean; }): Promise<ExecuteQueryResponse<void>> {
|
|
203
193
|
return this.executeQuery<void>({
|
|
204
194
|
query: getDropSchemaQuery(schema),
|
|
205
195
|
database,
|
|
196
|
+
isExternal,
|
|
206
197
|
});
|
|
207
198
|
}
|
|
208
199
|
|
|
209
200
|
/**
|
|
210
201
|
* Drop table
|
|
211
202
|
* ```typescript
|
|
212
|
-
* const result = await pgsql.dropSchema({ database: 'db-name', schema: 'schema-name', table: 'table-name' });
|
|
203
|
+
* const result = await pgsql.dropSchema({ database: 'db-name', schema: 'schema-name', table: 'table-name', isExternal: false });
|
|
213
204
|
* ```
|
|
214
205
|
*/
|
|
215
|
-
public async dropTable({ database, schema, table }: { database: string; schema: string; table: string; }): Promise<ExecuteQueryResponse<void>> {
|
|
206
|
+
public async dropTable({ database, schema, table, isExternal }: { database: string; schema: string; table: string; isExternal: boolean; }): Promise<ExecuteQueryResponse<void>> {
|
|
216
207
|
return this.executeQuery<void>({
|
|
217
208
|
query: getDropTableQuery(schema, table),
|
|
218
209
|
database,
|
|
210
|
+
isExternal,
|
|
219
211
|
});
|
|
220
212
|
}
|
|
221
213
|
|
|
@@ -230,14 +222,16 @@ export class Pgsql extends Base {
|
|
|
230
222
|
* { name: 'col1', type: 'integer' },
|
|
231
223
|
* { name: 'col2', type: 'text' }
|
|
232
224
|
* ],
|
|
233
|
-
* primaryKey: 'col1,col2'
|
|
225
|
+
* primaryKey: 'col1,col2',
|
|
226
|
+
* isExternal: false
|
|
234
227
|
* });
|
|
235
228
|
* ```
|
|
236
229
|
*/
|
|
237
|
-
public async createTable({ database, schema, table, columns, primaryKey }: CreateTableArgs): Promise<ExecuteQueryResponse<void>> {
|
|
230
|
+
public async createTable({ database, schema, table, columns, primaryKey, isExternal }: CreateTableArgs): Promise<ExecuteQueryResponse<void>> {
|
|
238
231
|
return this.executeQuery<void>({
|
|
239
232
|
query: getCreateTableQuery(schema, table, columns, primaryKey),
|
|
240
233
|
database,
|
|
234
|
+
isExternal,
|
|
241
235
|
});
|
|
242
236
|
}
|
|
243
237
|
|
|
@@ -252,13 +246,15 @@ export class Pgsql extends Base {
|
|
|
252
246
|
* { name: 'col1', type: 'integer' },
|
|
253
247
|
* { name: 'col2', type: 'text' }
|
|
254
248
|
* ],
|
|
249
|
+
* isExternal: false
|
|
255
250
|
* });
|
|
256
251
|
* ```
|
|
257
252
|
*/
|
|
258
|
-
public async addColumns({ database, schema, table, columns }: AddColumnsArgs): Promise<ExecuteQueryResponse<void>> {
|
|
253
|
+
public async addColumns({ database, schema, table, columns, isExternal }: AddColumnsArgs): Promise<ExecuteQueryResponse<void>> {
|
|
259
254
|
return this.executeQuery<void>({
|
|
260
255
|
query: getAddColumnsQuery(schema, table, columns),
|
|
261
256
|
database,
|
|
257
|
+
isExternal,
|
|
262
258
|
});
|
|
263
259
|
}
|
|
264
260
|
|
|
@@ -273,52 +269,101 @@ export class Pgsql extends Base {
|
|
|
273
269
|
* { col1: 'value' }
|
|
274
270
|
* ],
|
|
275
271
|
* chunkSize: 300,
|
|
276
|
-
* context: { progress: 0 } // progress of insert will be set from 0 to 100
|
|
272
|
+
* context: { progress: 0 } // progress of insert will be set from 0 to 100,
|
|
273
|
+
* isExternal: false
|
|
277
274
|
* });
|
|
278
275
|
* ```
|
|
279
276
|
*/
|
|
280
|
-
public async insert({ database, schema, table, rows, chunkSize = 300, context }: InsertArgs): Promise<ExecuteQueryResponse<
|
|
277
|
+
public async insert({ database, schema, table, rows, chunkSize = 300, context, isExternal }: InsertArgs): Promise<ExecuteQueryResponse<unknown>[]> {
|
|
278
|
+
let params: any[] = [];
|
|
279
|
+
const addParam = (val: any) => {params.push(val);return `$${params.length}`;};
|
|
281
280
|
const res = [];
|
|
281
|
+
if (!_.isArray(rows)) rows = [rows];
|
|
282
|
+
const allKeys = {};
|
|
283
|
+
rows.forEach(x => Object.assign(allKeys, x));
|
|
284
|
+
const keys = Object.keys(allKeys);
|
|
285
|
+
const stringifyRow = (row: any) => `${keys.map(key => addParam(row[key])).join(', ')}`;
|
|
286
|
+
|
|
287
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
288
|
+
// @ts-ignore
|
|
289
|
+
for (const [index, chunk] of _.chunk(rows, chunkSize).entries()) {
|
|
290
|
+
params = [];
|
|
291
|
+
const query = `insert into ${schema}.${table} (${keys.join(', ')})
|
|
292
|
+
values (${chunk.map(stringifyRow).join('), (')});`;
|
|
293
|
+
res.push(await this.executeQuery({
|
|
294
|
+
query,
|
|
295
|
+
database,
|
|
296
|
+
isExternal,
|
|
297
|
+
params,
|
|
298
|
+
}));
|
|
299
|
+
if (context) context.progress = (index + 1) / (rows.length / chunkSize) * 100;
|
|
300
|
+
}
|
|
282
301
|
|
|
283
|
-
|
|
302
|
+
return res;
|
|
303
|
+
}
|
|
284
304
|
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
305
|
+
|
|
306
|
+
// TODO: add method description and example
|
|
307
|
+
// TODO: add context ass in selectAll and rethink this two methods
|
|
308
|
+
public async getDatabaseRows({ database, schema, table, chunkSize = 1000, offset = 0, limit, isExternal, params }: GetTableDataProps): Promise<{ rows: List<Row>; fields: QueryField[]; }> {
|
|
309
|
+
let fields: QueryField[] = [];
|
|
310
|
+
let rows, result: Row[] = [];
|
|
311
|
+
|
|
312
|
+
// TODO: add order by primary key?
|
|
313
|
+
// const query = `select * from ${schema}.${table} order by int`;
|
|
314
|
+
const query = `select * from ${schema}.${table}`;
|
|
315
|
+
|
|
316
|
+
if (limit && limit < chunkSize) chunkSize = limit;
|
|
317
|
+
|
|
318
|
+
do {
|
|
319
|
+
({ rows, fields } = await this.executeQuery<Row>({
|
|
320
|
+
query: getSelectAllQuery(query, offset, chunkSize),
|
|
288
321
|
database,
|
|
322
|
+
isExternal,
|
|
323
|
+
params,
|
|
289
324
|
}));
|
|
325
|
+
offset += chunkSize;
|
|
326
|
+
result = result.concat(rows);
|
|
290
327
|
|
|
291
|
-
if (
|
|
292
|
-
|
|
328
|
+
if (limit && result.length >= limit) {
|
|
329
|
+
result = result.slice(0, limit);
|
|
330
|
+
break;
|
|
293
331
|
}
|
|
294
|
-
}
|
|
332
|
+
} while (rows.length === chunkSize);
|
|
295
333
|
|
|
296
|
-
return
|
|
334
|
+
return {
|
|
335
|
+
rows: makeList<Row>(result),
|
|
336
|
+
fields,
|
|
337
|
+
};
|
|
297
338
|
}
|
|
298
339
|
|
|
340
|
+
|
|
299
341
|
/**
|
|
300
342
|
* Select all
|
|
301
343
|
* ```typescript
|
|
302
344
|
* const result = await pgsql.selectAll({
|
|
303
345
|
* database: 'db-name',
|
|
304
|
-
* query: 'select * from table', // must be select only, without limit and offset
|
|
346
|
+
* query: 'select * from table where id = $1', // must be select only, without limit and offset
|
|
305
347
|
* chunkSize: 300,
|
|
306
348
|
* offset: 0,
|
|
307
349
|
* limit: 0,
|
|
308
350
|
* context: { progress: 0, total: 0 } // progress of select will be set from 0 to 100
|
|
351
|
+
* isExternal: false,
|
|
352
|
+
* params: ['123']
|
|
309
353
|
* });
|
|
310
354
|
* ```
|
|
311
355
|
*/
|
|
312
|
-
public async selectAll({ database, query, context, chunkSize = 1000, offset = 0, limit }: SelectAllArgs): Promise<List<Row>> {
|
|
356
|
+
public async selectAll({ database, query, context, chunkSize = 1000, offset = 0, limit, isExternal, params }: SelectAllArgs): Promise<List<Row>> {
|
|
313
357
|
let count: number, rows, result: Row[] = [];
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
}
|
|
358
|
+
|
|
359
|
+
if (limit && limit < chunkSize) chunkSize = limit;
|
|
317
360
|
|
|
318
361
|
if (context) {
|
|
319
362
|
const { rows } = await this.executeQuery<{ count: number; }>({
|
|
320
363
|
query: getSelectAllCountQuery(query),
|
|
321
364
|
database,
|
|
365
|
+
isExternal,
|
|
366
|
+
params,
|
|
322
367
|
});
|
|
323
368
|
count = rows[0].count;
|
|
324
369
|
context.total = count;
|
|
@@ -328,12 +373,16 @@ export class Pgsql extends Base {
|
|
|
328
373
|
({ rows } = await this.executeQuery<Row>({
|
|
329
374
|
query: getSelectAllQuery(query, offset, chunkSize),
|
|
330
375
|
database,
|
|
376
|
+
isExternal,
|
|
377
|
+
params,
|
|
331
378
|
}));
|
|
332
379
|
offset += chunkSize;
|
|
333
380
|
result = result.concat(rows);
|
|
381
|
+
|
|
334
382
|
if (context) {
|
|
335
383
|
context.progress = Math.min(offset / count! * 100, 100);
|
|
336
384
|
}
|
|
385
|
+
|
|
337
386
|
if (limit && result.length >= limit) {
|
|
338
387
|
result = result.slice(0, limit);
|
|
339
388
|
break;
|
|
@@ -346,13 +395,14 @@ export class Pgsql extends Base {
|
|
|
346
395
|
/**
|
|
347
396
|
* Get primary keys
|
|
348
397
|
* ```typescript
|
|
349
|
-
* const result = await pgsql.getPrimaryKeys({ database: 'db-name', schema: 'schema-name', table: 'table-name' });
|
|
398
|
+
* const result = await pgsql.getPrimaryKeys({ database: 'db-name', schema: 'schema-name', table: 'table-name', isExternal: false });
|
|
350
399
|
* ```
|
|
351
400
|
*/
|
|
352
|
-
public async getPrimaryKeys({ database, schema, table }: { database: string; schema: string; table: string; }): Promise<string> {
|
|
401
|
+
public async getPrimaryKeys({ database, schema, table, isExternal }: { database: string; schema: string; table: string; isExternal: boolean; }): Promise<string> {
|
|
353
402
|
const { rows } = await this.executeQuery<{ pk: string; }>({
|
|
354
403
|
database,
|
|
355
404
|
query: getGetPrimaryKeysQuery(schema, table),
|
|
405
|
+
isExternal,
|
|
356
406
|
});
|
|
357
407
|
return rows[0].pk;
|
|
358
408
|
}
|
|
@@ -368,13 +418,28 @@ export class Pgsql extends Base {
|
|
|
368
418
|
* value: 'new_value',
|
|
369
419
|
* row: { pk: 'pk_val', col1: 'old_value' },
|
|
370
420
|
* primaryKeys: 'pk',
|
|
421
|
+
* isExternal: false
|
|
371
422
|
* });
|
|
372
423
|
* ```
|
|
373
424
|
*/
|
|
374
|
-
public async editRow({ database, schema, table, key, value, row, primaryKeys }: EditRowArgs): Promise<ExecuteQueryResponse<void>> {
|
|
425
|
+
public async editRow({ database, schema, table, key, value, row, primaryKeys, isExternal }: EditRowArgs): Promise<ExecuteQueryResponse<void>> {
|
|
426
|
+
// TODO: maybe move query and params to utils and return both
|
|
427
|
+
const params: any[] = [];
|
|
428
|
+
const addParam = (val: any) => {
|
|
429
|
+
params.push(val);
|
|
430
|
+
return `$${params.length}`;
|
|
431
|
+
};
|
|
432
|
+
|
|
433
|
+
const pk = splitPrimaryKeys(primaryKeys);
|
|
434
|
+
|
|
435
|
+
// TODO: move query to utils
|
|
375
436
|
return this.executeQuery({
|
|
376
437
|
database,
|
|
377
|
-
query:
|
|
438
|
+
query: `update ${schema}.${table}
|
|
439
|
+
set ${key} = ${addParam(value)}
|
|
440
|
+
where ${pk.map(primaryKey => `${primaryKey} = ${addParam(row[primaryKey])}`).join(' AND ')}`,
|
|
441
|
+
isExternal,
|
|
442
|
+
params,
|
|
378
443
|
});
|
|
379
444
|
}
|
|
380
445
|
|
|
@@ -389,13 +454,27 @@ export class Pgsql extends Base {
|
|
|
389
454
|
* { pk: 'pk_val', col1: 'value' }
|
|
390
455
|
* ],
|
|
391
456
|
* primaryKeys: 'pk',
|
|
457
|
+
* isExternal: false
|
|
392
458
|
* });
|
|
393
459
|
* ```
|
|
394
460
|
*/
|
|
395
|
-
public async deleteRows({ database, schema, table, rows, primaryKeys }: DeleteRowsArgs): Promise<ExecuteQueryResponse<void>> {
|
|
461
|
+
public async deleteRows({ database, schema, table, rows, primaryKeys, isExternal }: DeleteRowsArgs): Promise<ExecuteQueryResponse<void>> {
|
|
462
|
+
const params: any[] = [];
|
|
463
|
+
const addParam = (val: any) => {
|
|
464
|
+
params.push(val);
|
|
465
|
+
return `$${params.length}`;
|
|
466
|
+
};
|
|
467
|
+
|
|
468
|
+
const pk = splitPrimaryKeys(primaryKeys);
|
|
469
|
+
// TODO: move query to util
|
|
470
|
+
const query = `delete from ${schema}.${table}
|
|
471
|
+
where (${rows.map(row => `${pk.map(primaryKey => `${primaryKey} = ${addParam(row[primaryKey])}`).join(' AND ')}`).join(') \nOR (')})`;
|
|
472
|
+
|
|
396
473
|
return this.executeQuery({
|
|
397
474
|
database,
|
|
398
|
-
query
|
|
475
|
+
query,
|
|
476
|
+
isExternal,
|
|
477
|
+
params,
|
|
399
478
|
});
|
|
400
479
|
}
|
|
401
480
|
|
|
@@ -405,10 +484,11 @@ export class Pgsql extends Base {
|
|
|
405
484
|
* const result = await pgsql.generateTableSchema({ database: 'db-name', schema: 'schema-name', table: 'table-name' });
|
|
406
485
|
* ```
|
|
407
486
|
*/
|
|
408
|
-
public async generateTableSchema({ database, schema, table }: { database: string; schema: string; table: string; }): Promise<string> {
|
|
487
|
+
public async generateTableSchema({ database, schema, table, isExternal }: { database: string; schema: string; table: string; isExternal: boolean; }): Promise<string> {
|
|
409
488
|
const { rows } = await this.executeQuery<{ code: string; }>({
|
|
410
489
|
database,
|
|
411
490
|
query: getGenerateTableSchemaQuery(schema, table),
|
|
491
|
+
isExternal,
|
|
412
492
|
});
|
|
413
493
|
|
|
414
494
|
return rows[0].code;
|
|
@@ -426,4 +506,18 @@ export class Pgsql extends Base {
|
|
|
426
506
|
route: 'size',
|
|
427
507
|
});
|
|
428
508
|
}
|
|
509
|
+
|
|
510
|
+
/**
|
|
511
|
+
* Get database connection config. Works only in LAMBDAS in private web.
|
|
512
|
+
* ```typescript
|
|
513
|
+
* const connectionConfig = await pgsql.getDatabaseConnections('database');
|
|
514
|
+
* ```
|
|
515
|
+
*/
|
|
516
|
+
public async getDatabaseConnections(database: string): Promise<GetDatabaseConnectionResponse> {
|
|
517
|
+
return await this.makeRequest<GetDatabaseConnectionResponse>({
|
|
518
|
+
method: 'GET',
|
|
519
|
+
params: { database },
|
|
520
|
+
route: 'database-connection',
|
|
521
|
+
});
|
|
522
|
+
}
|
|
429
523
|
}
|
package/src/types.ts
CHANGED
|
@@ -25,11 +25,6 @@ export type PgsqlConfig = {
|
|
|
25
25
|
* Api version
|
|
26
26
|
*/
|
|
27
27
|
version?: string;
|
|
28
|
-
|
|
29
|
-
/**
|
|
30
|
-
* use external database
|
|
31
|
-
*/
|
|
32
|
-
isExternal?: boolean;
|
|
33
28
|
};
|
|
34
29
|
|
|
35
30
|
export type QueryField = {
|
|
@@ -54,22 +49,17 @@ export type ExecuteQueryResponse<T> = {
|
|
|
54
49
|
|
|
55
50
|
export type ExecuteQueryArgs = {
|
|
56
51
|
query: string;
|
|
57
|
-
params?:
|
|
58
|
-
[key: string]: unknown;
|
|
59
|
-
};
|
|
52
|
+
params?: any[];
|
|
60
53
|
database: string;
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
export type CreateDatabaseResponse = {
|
|
64
|
-
ok: number;
|
|
65
|
-
result: [
|
|
66
|
-
unknown[],
|
|
67
|
-
unknown[],
|
|
68
|
-
];
|
|
54
|
+
isExternal?: boolean;
|
|
69
55
|
};
|
|
70
56
|
|
|
71
57
|
export type ListDatabasesResponseItem = {
|
|
58
|
+
accountId: string;
|
|
72
59
|
name: string;
|
|
60
|
+
isExternal: boolean;
|
|
61
|
+
description?: string;
|
|
62
|
+
imageUrl?: string;
|
|
73
63
|
};
|
|
74
64
|
|
|
75
65
|
export type ListDatabasesResponse = {
|
|
@@ -87,7 +77,7 @@ export type GetDBSizeResult = {
|
|
|
87
77
|
|
|
88
78
|
export type TableColumn = {
|
|
89
79
|
name: string;
|
|
90
|
-
type:
|
|
80
|
+
type: string;
|
|
91
81
|
};
|
|
92
82
|
|
|
93
83
|
export type CreateTableArgs = {
|
|
@@ -96,6 +86,7 @@ export type CreateTableArgs = {
|
|
|
96
86
|
table: string;
|
|
97
87
|
columns: TableColumn[];
|
|
98
88
|
primaryKey: string;
|
|
89
|
+
isExternal: boolean;
|
|
99
90
|
};
|
|
100
91
|
|
|
101
92
|
export type AddColumnsArgs = {
|
|
@@ -103,6 +94,7 @@ export type AddColumnsArgs = {
|
|
|
103
94
|
schema: string;
|
|
104
95
|
table: string;
|
|
105
96
|
columns: TableColumn[];
|
|
97
|
+
isExternal: boolean;
|
|
106
98
|
};
|
|
107
99
|
|
|
108
100
|
export type Row = {
|
|
@@ -118,9 +110,19 @@ export type InsertArgs = {
|
|
|
118
110
|
context?: {
|
|
119
111
|
progress?: number;
|
|
120
112
|
};
|
|
113
|
+
isExternal: boolean;
|
|
121
114
|
};
|
|
122
115
|
|
|
123
|
-
export type
|
|
116
|
+
export type GetTableDataProps = {
|
|
117
|
+
database: string;
|
|
118
|
+
schema: string;
|
|
119
|
+
table: string;
|
|
120
|
+
isExternal: boolean;
|
|
121
|
+
chunkSize?: number;
|
|
122
|
+
offset?: number;
|
|
123
|
+
limit?: number;
|
|
124
|
+
params?: any[];
|
|
125
|
+
};
|
|
124
126
|
|
|
125
127
|
export type SelectAllArgs = {
|
|
126
128
|
database: string;
|
|
@@ -132,6 +134,8 @@ export type SelectAllArgs = {
|
|
|
132
134
|
chunkSize?: number;
|
|
133
135
|
offset?: number;
|
|
134
136
|
limit?: number;
|
|
137
|
+
isExternal: boolean;
|
|
138
|
+
params?: any[];
|
|
135
139
|
};
|
|
136
140
|
|
|
137
141
|
export type EditRowArgs = {
|
|
@@ -142,6 +146,7 @@ export type EditRowArgs = {
|
|
|
142
146
|
value: unknown;
|
|
143
147
|
row: Row;
|
|
144
148
|
primaryKeys: string;
|
|
149
|
+
isExternal: boolean;
|
|
145
150
|
};
|
|
146
151
|
|
|
147
152
|
export type DeleteRowsArgs = {
|
|
@@ -150,15 +155,21 @@ export type DeleteRowsArgs = {
|
|
|
150
155
|
table: string;
|
|
151
156
|
rows: Row[];
|
|
152
157
|
primaryKeys: string;
|
|
158
|
+
isExternal: boolean;
|
|
153
159
|
};
|
|
154
160
|
|
|
155
161
|
export type DatabaseItem = {
|
|
162
|
+
accountId: string;
|
|
156
163
|
name: string;
|
|
157
|
-
isExternal
|
|
164
|
+
isExternal: boolean;
|
|
165
|
+
description?: string;
|
|
166
|
+
imageUrl?: string;
|
|
158
167
|
};
|
|
159
168
|
|
|
160
169
|
export type CreateDatabaseParams = {
|
|
161
170
|
database: string;
|
|
171
|
+
description?: string;
|
|
172
|
+
imageUrl?: string;
|
|
162
173
|
externalCredentials?: {
|
|
163
174
|
host: string;
|
|
164
175
|
port: number;
|
|
@@ -166,3 +177,11 @@ export type CreateDatabaseParams = {
|
|
|
166
177
|
user: string;
|
|
167
178
|
};
|
|
168
179
|
};
|
|
180
|
+
|
|
181
|
+
export type GetDatabaseConnectionResponse = {
|
|
182
|
+
user: string;
|
|
183
|
+
host_master: string;
|
|
184
|
+
database: string;
|
|
185
|
+
password: string;
|
|
186
|
+
port: number;
|
|
187
|
+
};
|
package/src/utils/index.ts
CHANGED
|
@@ -2,9 +2,6 @@
|
|
|
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';
|
|
8
5
|
export { default as splitPrimaryKeys } from './splitPrimaryKeys';
|
|
9
6
|
export { default as getListTablesQuery } from './getListTablesQuery';
|
|
10
7
|
export { default as createSchemaQuery } from './createSchemaQuery';
|
|
@@ -12,10 +9,7 @@ export { default as getDropSchemaQuery } from './getDropSchemaQuery';
|
|
|
12
9
|
export { default as getCreateTableQuery } from './getCreateTableQuery';
|
|
13
10
|
export { default as getDropTableQuery } from './getDropTableQuery';
|
|
14
11
|
export { default as getAddColumnsQuery } from './getAddColumnsQuery';
|
|
15
|
-
export { default as getInsertQueries } from './getInsertQueries';
|
|
16
12
|
export { default as getSelectAllCountQuery } from './getSelectAllCountQuery';
|
|
17
13
|
export { default as getSelectAllQuery } from './getSelectAllQuery';
|
|
18
|
-
export { default as getEditRowQuery } from './getEditRowQuery';
|
|
19
|
-
export { default as getDeleteRowsQuery } from './getDeleteRowsQuery';
|
|
20
14
|
export { default as getGenerateTableSchemaQuery } from './getGenerateTableSchemaQuery';
|
|
21
15
|
export { default as getGetPrimaryKeysQuery } from './getGetPrimaryKeysQuery';
|
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
var lodash_1 = __importDefault(require("lodash"));
|
|
7
|
-
function encodeValue(value) {
|
|
8
|
-
if (lodash_1.default.isUndefined(value) || value === '' || lodash_1.default.isNull(value))
|
|
9
|
-
return 'NULL';
|
|
10
|
-
if (lodash_1.default.isString(value))
|
|
11
|
-
return "'".concat(value.replaceAll('\'', '\'\''), "'");
|
|
12
|
-
if (lodash_1.default.isNumber(value) || lodash_1.default.isBoolean(value))
|
|
13
|
-
return value;
|
|
14
|
-
return "'".concat(JSON.stringify(value).replaceAll('\'', '\'\''), "'");
|
|
15
|
-
}
|
|
16
|
-
exports.default = encodeValue;
|
|
17
|
-
//# sourceMappingURL=encodeValue.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"encodeValue.js","sourceRoot":"","sources":["../../../src/utils/encodeValue.ts"],"names":[],"mappings":";;;;;AACA,kDAAuB;AAEvB,SAAS,WAAW,CAAC,KAAc;IACjC,IAAI,gBAAC,CAAC,WAAW,CAAC,KAAK,CAAC,IAAI,KAAK,KAAK,EAAE,IAAI,gBAAC,CAAC,MAAM,CAAC,KAAK,CAAC;QAAE,OAAO,MAAM,CAAC;IAC3E,IAAI,gBAAC,CAAC,QAAQ,CAAC,KAAK,CAAC;QAAE,OAAO,WAAI,KAAK,CAAC,UAAU,CAAC,IAAI,EAAE,MAAM,CAAC,MAAG,CAAC;IACpE,IAAI,gBAAC,CAAC,QAAQ,CAAC,KAAK,CAAC,IAAI,gBAAC,CAAC,SAAS,CAAC,KAAK,CAAC;QAAE,OAAO,KAAK,CAAC;IAC1D,OAAO,WAAI,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC,UAAU,CAAC,IAAI,EAAE,MAAM,CAAC,MAAG,CAAC;AAC/D,CAAC;AAED,kBAAe,WAAW,CAAC"}
|