@or-sdk/pgsql 1.0.3-beta.1920.0 → 1.0.3-beta.1935.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 +17 -27
- package/dist/types/Pgsql.d.ts.map +1 -1
- package/dist/types/types.d.ts +5 -19
- 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 +56 -143
- package/src/types.ts +9 -19
- 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
|
@@ -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';
|