@qrvey/data-persistence
Advanced tools
Comparing version 0.5.0 to 0.5.1-bundled.1
@@ -17,8 +17,8 @@ "use strict"; | ||
class CrudService { | ||
constructor(crudSchema, poolClient) { | ||
this.poolClient = poolClient; | ||
constructor(crudSchema, pool) { | ||
this.pool = pool; | ||
this.crudSchema = crudSchema; | ||
} | ||
async getCrudServiceInstance() { | ||
const crudService = await crudFactory_service_1.CrudFactory.databaseClientService(this.crudSchema, this.poolClient); | ||
const crudService = await crudFactory_service_1.CrudFactory.databaseClientService(this.crudSchema, this.pool); | ||
return crudService; | ||
@@ -54,4 +54,4 @@ } | ||
} | ||
update(filters, data) { | ||
return this.getCrudServiceInstance().then((crudService) => crudService.update(filters, data, {})); | ||
update(filters, data, options = {}) { | ||
return this.getCrudServiceInstance().then((crudService) => crudService.update(filters, data, options)); | ||
} | ||
@@ -58,0 +58,0 @@ remove(filters, options = {}) { |
"use strict"; | ||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
var desc = Object.getOwnPropertyDescriptor(m, k); | ||
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { | ||
desc = { enumerable: true, get: function() { return m[k]; } }; | ||
} | ||
Object.defineProperty(o, k2, desc); | ||
}) : (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
o[k2] = m[k]; | ||
})); | ||
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { | ||
Object.defineProperty(o, "default", { enumerable: true, value: v }); | ||
}) : function(o, v) { | ||
o["default"] = v; | ||
}); | ||
var __importStar = (this && this.__importStar) || function (mod) { | ||
if (mod && mod.__esModule) return mod; | ||
var result = {}; | ||
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); | ||
__setModuleDefault(result, mod); | ||
return result; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.CrudFactory = void 0; | ||
const cruds_1 = require("./cruds/"); | ||
class CrudFactory { | ||
static async databaseClientService(crudSchema, poolClient) { | ||
static databaseClientService(crudSchema, pool) { | ||
var _a; | ||
const isMultiPlatformMode = ((_a = process.env.PLATFORM_TYPE) === null || _a === void 0 ? void 0 : _a.toLowerCase()) === 'container'; | ||
let DatabaseCrudService; | ||
if (isMultiPlatformMode) { | ||
const module = await Promise.resolve().then(() => __importStar(require('./cruds/postgresql/postgreSqlCrud.service'))); | ||
DatabaseCrudService = module.PostgreSqlCrudService; | ||
} | ||
else { | ||
const module = await Promise.resolve().then(() => __importStar(require('./cruds/dynamodb/dynamoDbCrud.service'))); | ||
DatabaseCrudService = module.DynamoDbCrudService; | ||
} | ||
return new DatabaseCrudService(crudSchema, poolClient); | ||
if (isMultiPlatformMode) | ||
return new cruds_1.PostgreSqlCrudService(crudSchema, pool); | ||
return new cruds_1.DynamoDbCrudService(crudSchema); | ||
} | ||
@@ -42,0 +13,0 @@ } |
@@ -257,9 +257,8 @@ "use strict"; | ||
} | ||
async update(filters, data, { replace = false }) { | ||
const savedRecord = await this.findItem({ | ||
filters: filters, | ||
}); | ||
if (replace) { | ||
//delete all attributes of savedRecord except those that as marked as primary columns in the schema | ||
} | ||
async update(filters, data, { skipFindItems = false }) { | ||
const savedRecord = skipFindItems | ||
? {} | ||
: await this.findItem({ | ||
filters: filters, | ||
}); | ||
const newData = Object.assign(Object.assign({}, savedRecord), data); | ||
@@ -266,0 +265,0 @@ await this.dynamoDbClientService.put(newData); |
@@ -8,3 +8,3 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const pg_format_1 = __importDefault(require("pg-format")); | ||
const pg_format_1 = require("@scaleleap/pg-format"); | ||
const query_service_1 = __importDefault(require("./query.service")); | ||
@@ -43,3 +43,3 @@ const constants_1 = require("../../../utils/constants"); | ||
var _a; | ||
const formattedValue = pg_format_1.default.literal(value); | ||
const formattedValue = (0, pg_format_1.literal)(value); | ||
operator = operator | ||
@@ -52,3 +52,3 @@ ? operator.toUpperCase() | ||
let property; | ||
const filterProperty = pg_format_1.default.ident(attribute); | ||
const filterProperty = (0, pg_format_1.ident)(attribute); | ||
const columnExists = !!this.crudSchema.columns[attribute]; | ||
@@ -71,3 +71,3 @@ const columnType = columnExists && ((_a = this.crudSchema.columns[attribute]) === null || _a === void 0 ? void 0 : _a.type); | ||
const formattedValues = Array.isArray(value) | ||
? value.map(pg_format_1.default.literal) | ||
? value.map(pg_format_1.literal) | ||
: [formattedValue]; | ||
@@ -83,3 +83,3 @@ return `${property} ${postgresOperator} (${formattedValues.join(', ')})`; | ||
if (operator === constants_1.FilterOperator.NOT_EQUAL && value !== null) { | ||
return `(${property} ${postgresOperator} ${pg_format_1.default.literal(value)} OR ${property} IS NULL)`; | ||
return `(${property} ${postgresOperator} ${(0, pg_format_1.literal)(value)} OR ${property} IS NULL)`; | ||
} | ||
@@ -106,3 +106,3 @@ if (operator === constants_1.FilterOperator.NOT_EXIST || | ||
const wildcardValue = this.getWildcardValue(operator, value); | ||
return `${property} ${postgresOperator} ${pg_format_1.default.literal(wildcardValue)}`; | ||
return `${property} ${postgresOperator} ${(0, pg_format_1.literal)(wildcardValue)}`; | ||
} | ||
@@ -150,3 +150,3 @@ buildFilterClause(filters, logicOperator) { | ||
formatOrderByItem(sort) { | ||
return `${pg_format_1.default.ident(sort.column)} ${sort.direction || constants_1.SORT_DIRECTIONS.ASC}`; | ||
return `${(0, pg_format_1.ident)(sort.column)} ${sort.direction || constants_1.SORT_DIRECTIONS.ASC}`; | ||
} | ||
@@ -187,3 +187,3 @@ buildOrderByClause(querySorting) { | ||
const values = data.map((item) => keys.map((key) => this.formatValue(item[key]))); | ||
const query = (0, pg_format_1.default)(`INSERT INTO ${pg_format_1.default.ident(this.dbSchema)}.${pg_format_1.default.ident(this.tableName)} (%I) VALUES %L RETURNING *;`, keys, values); | ||
const query = (0, pg_format_1.format)(`INSERT INTO ${(0, pg_format_1.ident)(this.dbSchema)}.${(0, pg_format_1.ident)(this.tableName)} (%I) VALUES %L RETURNING *;`, keys, values); | ||
return this.runQuery(query); | ||
@@ -235,3 +235,3 @@ } | ||
async findCommand(options = {}) { | ||
let query = `SELECT ${this.getSelectClause(options.aggregateFunction, options.fields)} FROM ${pg_format_1.default.ident(this.dbSchema)}.${pg_format_1.default.ident(this.tableName)}`; | ||
let query = `SELECT ${this.getSelectClause(options.aggregateFunction, options.fields)} FROM ${(0, pg_format_1.ident)(this.dbSchema)}.${(0, pg_format_1.ident)(this.tableName)}`; | ||
query = this.addFiltersToQuery(query, options.filters); | ||
@@ -264,10 +264,10 @@ if (!options.aggregateFunction) { | ||
else { | ||
return pg_format_1.default.literal(value); | ||
return (0, pg_format_1.literal)(value); | ||
} | ||
} | ||
async updateCommand(filters, data) { | ||
let query = `UPDATE ${pg_format_1.default.ident(this.dbSchema)}.${pg_format_1.default.ident(this.tableName)} SET`; | ||
let query = `UPDATE ${(0, pg_format_1.ident)(this.dbSchema)}.${(0, pg_format_1.ident)(this.tableName)} SET`; | ||
const updateClauses = Object.entries(data).map(([key, value]) => { | ||
const dbValue = pg_format_1.default.literal(this.formatValue(value)); | ||
return `${pg_format_1.default.ident(key)} = ${dbValue}`; | ||
const dbValue = (0, pg_format_1.literal)(this.formatValue(value)); | ||
return `${(0, pg_format_1.ident)(key)} = ${dbValue}`; | ||
}); | ||
@@ -286,3 +286,3 @@ query += ` ${updateClauses.join(', ')}`; | ||
async deleteCommand(filters, useFilterGroups = false) { | ||
let query = `DELETE FROM ${pg_format_1.default.ident(this.dbSchema)}.${pg_format_1.default.ident(this.tableName)}`; | ||
let query = `DELETE FROM ${(0, pg_format_1.ident)(this.dbSchema)}.${(0, pg_format_1.ident)(this.tableName)}`; | ||
if (filters) { | ||
@@ -305,3 +305,3 @@ query += ' WHERE '; | ||
filters, actions, options = {}) { | ||
let query = `UPDATE ${pg_format_1.default.ident(this.dbSchema)}.${pg_format_1.default.ident(this.tableName)} SET`; | ||
let query = `UPDATE ${(0, pg_format_1.ident)(this.dbSchema)}.${(0, pg_format_1.ident)(this.tableName)} SET`; | ||
const set = actions.SET || []; | ||
@@ -349,7 +349,7 @@ const add = actions.ADD || []; | ||
case 'array': | ||
formattedValue = pg_format_1.default.literal(value); | ||
formattedValue = (0, pg_format_1.literal)(value); | ||
expValue = `ARRAY[${formattedValue}]`; | ||
break; | ||
default: | ||
formattedValue = pg_format_1.default.literal(value); | ||
formattedValue = (0, pg_format_1.literal)(value); | ||
expValue = formattedValue; | ||
@@ -359,3 +359,3 @@ break; | ||
this.crudSchema.columns; | ||
updateClauses.push(`${pg_format_1.default.ident(path)} = ${expValue}`); | ||
updateClauses.push(`${(0, pg_format_1.ident)(path)} = ${expValue}`); | ||
} | ||
@@ -382,6 +382,6 @@ }); | ||
const jsonExpr = this.buildJSONBExpression(queryFunction.value.jsonExpression, 'jsonb_insert'); | ||
updateClauses.push(`${pg_format_1.default.ident(queryFunction.path)} = ${jsonExpr}`); | ||
updateClauses.push(`${(0, pg_format_1.ident)(queryFunction.path)} = ${jsonExpr}`); | ||
} | ||
else { | ||
updateClauses.push(`${pg_format_1.default.ident(queryFunction.path)} = ${queryFunction.value}`); | ||
updateClauses.push(`${(0, pg_format_1.ident)(queryFunction.path)} = ${queryFunction.value}`); | ||
} | ||
@@ -388,0 +388,0 @@ }); |
@@ -55,7 +55,11 @@ "use strict"; | ||
const inputDataArray = data.map((item) => this.prepareData(item)); | ||
return this.createCommand(inputDataArray).then((result) => ({ | ||
unprocessedItems: result | ||
.filter((r) => !r.success) | ||
.map((r) => r.inputData), | ||
})); | ||
return this.createCommand(inputDataArray).then((result) => { | ||
var _a, _b; | ||
const items = ((_a = result === null || result === void 0 ? void 0 : result.rows) === null || _a === void 0 ? void 0 : _a.map((row) => this.getItem(row))) || []; | ||
return { | ||
total: (_b = result.rowCount) !== null && _b !== void 0 ? _b : 0, | ||
items, | ||
unprocessedItems: [], | ||
}; | ||
}); | ||
} | ||
@@ -99,4 +103,6 @@ else { | ||
} | ||
async update(filters, data) { | ||
const savedRecord = await this.findItem({ filters }); | ||
async update(filters, data, options = {}) { | ||
const savedRecord = options.skipFindItems | ||
? {} | ||
: await this.findItem({ filters }); | ||
const inputData = Object.assign(Object.assign({}, savedRecord), data); | ||
@@ -103,0 +109,0 @@ await this.updateCommand(filters, this.prepareData(inputData)); |
@@ -8,9 +8,9 @@ "use strict"; | ||
class QueryService { | ||
constructor(poolClient) { | ||
this.poolClient = poolClient; | ||
constructor(pool) { | ||
this.pool = pool; | ||
this.connectionService = new connection_service_1.default(); | ||
} | ||
async runQuery(queryText, values) { | ||
const client = await (this.poolClient | ||
? this.poolClient | ||
const client = await (this.pool | ||
? this.pool.connect() | ||
: this.connectionService.getClient()); | ||
@@ -26,4 +26,8 @@ try { | ||
finally { | ||
if (!this.poolClient) | ||
if (this.pool) { | ||
await client.release(); | ||
} | ||
else { | ||
this.connectionService.releaseClient(client); | ||
} | ||
} | ||
@@ -30,0 +34,0 @@ } |
@@ -6,12 +6,9 @@ "use strict"; | ||
const constants_1 = require("../utils/constants"); | ||
async function getDbPool() { | ||
function getDbPool(poolConfig) { | ||
if (!(0, constants_1.isMultiPlatformMode)()) | ||
return undefined; | ||
const pool = new pg_1.Pool({ | ||
connectionString: process.env.MULTIPLATFORM_PG_CONNECTION_STRING, | ||
}); | ||
const poolClient = await pool.connect(); | ||
return poolClient; | ||
const pool = new pg_1.Pool(Object.assign(Object.assign({}, poolConfig), { connectionString: process.env.MULTIPLATFORM_PG_CONNECTION_STRING })); | ||
return pool; | ||
} | ||
exports.getDbPool = getDbPool; | ||
//# sourceMappingURL=dbPool.service.js.map |
@@ -1,3 +0,2 @@ | ||
import * as pg from 'pg'; | ||
import { PoolClient } from 'pg'; | ||
import { PoolClient, Pool, PoolConfig } from 'pg'; | ||
@@ -17,3 +16,5 @@ interface IFilter { | ||
interface ICreateMultipleResponse { | ||
unprocessedItems: Record<string, any>; | ||
unprocessedItems?: Record<string, any>; | ||
total?: number; | ||
items?: any[]; | ||
} | ||
@@ -73,3 +74,3 @@ | ||
interface IUpdateOptions { | ||
replace?: boolean; | ||
skipFindItems?: boolean; | ||
} | ||
@@ -132,5 +133,5 @@ | ||
declare class CrudService<T> { | ||
private poolClient?; | ||
private pool?; | ||
private crudSchema; | ||
constructor(crudSchema: typeof CrudSchema, poolClient?: PoolClient | undefined); | ||
constructor(crudSchema: typeof CrudSchema, pool?: Pool | undefined); | ||
private getCrudServiceInstance; | ||
@@ -142,3 +143,3 @@ private getDefaultValue; | ||
findItem(options: IFindOptions): Promise<unknown>; | ||
update(filters: IFilter[] | ICompositeFilter, data: Partial<T>): Promise<T | null>; | ||
update(filters: IFilter[] | ICompositeFilter, data: Partial<T>, options?: IUpdateOptions): Promise<T | null>; | ||
remove(filters: IFilter[] | ICompositeFilter | IFilter[][], options?: IRemoveOptions): Promise<void>; | ||
@@ -173,4 +174,4 @@ findAll(options: IFindOptions): Promise<IFindResult<unknown>>; | ||
declare function getDbPool(): Promise<pg.PoolClient | undefined>; | ||
declare function getDbPool(poolConfig?: PoolConfig): Pool | undefined; | ||
export { AggregateFunction, CrudSchema, CrudService, FilterInput, ICrudService, IFilter, IFindOptions, IFindPagination, ISorting, SortDirection, buildFilter, buildQueryIndex, buildSort, getDbPool }; |
@@ -5,3 +5,3 @@ { | ||
"main": "dist/cjs/index.js", | ||
"version": "0.5.0", | ||
"version": "0.5.1-bundled.1", | ||
"license": "MIT", | ||
@@ -31,24 +31,10 @@ "exports": { | ||
"publish-package-dev": "yarn prepare-publish && yarn publish-codeartifact", | ||
"publish-package": "yarn prepare-publish && npm publish" | ||
"publish-package": "yarn prepare-publish && npm publish --tag beta" | ||
}, | ||
"peerDependencies": { | ||
"dependencies": { | ||
"@aws-sdk/client-dynamodb": "3.x", | ||
"@aws-sdk/lib-dynamodb": "3.x", | ||
"pg": "8.11.4", | ||
"pg-format": "1.0.4" | ||
"@scaleleap/pg-format": "^1.0.0", | ||
"pg": "8.11.4" | ||
}, | ||
"peerDependenciesMeta": { | ||
"@aws-sdk/client-dynamodb": { | ||
"optional": true | ||
}, | ||
"@aws-sdk/lib-dynamodb": { | ||
"optional": true | ||
}, | ||
"pg": { | ||
"optional": true | ||
}, | ||
"pg-format": { | ||
"optional": true | ||
} | ||
}, | ||
"devDependencies": { | ||
@@ -55,0 +41,0 @@ "@types/jest": "29.5.5", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
373466
98
4144
10
+ Added@aws-sdk/client-dynamodb@3.x
+ Added@aws-sdk/lib-dynamodb@3.x
+ Added@scaleleap/pg-format@^1.0.0
+ Addedpg@8.11.4
+ Added@scaleleap/pg-format@1.0.0(transitive)
- Removedpg-format@1.0.4(transitive)