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