Skip to content
This repository has been archived by the owner on Jun 11, 2024. It is now read-only.

Commit

Permalink
🔨 Update knex abstraction to enforce DB transactions
Browse files Browse the repository at this point in the history
  • Loading branch information
sameersubudhi committed Jul 26, 2023
1 parent 89d3a7d commit 8350c36
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 48 deletions.
Binary file added framework/dist/lisk-service-framework-1.4.22.tgz
Binary file not shown.
57 changes: 24 additions & 33 deletions framework/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion framework/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "lisk-service-framework",
"version": "1.4.21",
"version": "1.4.22",
"description": "Lisk Service Framework",
"keywords": [
"lisk",
Expand Down
4 changes: 3 additions & 1 deletion framework/src/database/sqlite3.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,9 @@ const getTableInstance = async (tableConfig, dbDataDir = DB_DATA_DIR) => {
isDefaultTrx = true;
}

const query = trx.raw(queryStatement);
const query = trx
.raw(queryStatement)
.transacting(trx);

if (isDefaultTrx) return query
.then(async result => {
Expand Down
41 changes: 28 additions & 13 deletions framework/src/database/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,10 @@ const getTableInstance = (tableConfig, knex) => {

// Create all queries for `INSERT or UPDATE on Duplicate keys`
const queries = rows.map(row => knex(tableName)
.transacting(trx)
.insert(row)
.onConflict(primaryKey)
.merge(),
.merge()
.transacting(trx),
);

// Perform all queries within a batch together
Expand All @@ -133,8 +133,8 @@ const getTableInstance = (tableConfig, knex) => {
return Promise.all(queries);
};

const queryBuilder = (params, columns, isCountQuery, trx) => {
const query = knex(tableName).transacting(trx);
const queryBuilder = (params, columns, isCountQuery) => {
const query = knex(tableName);
const queryParams = resolveQueryParams(params);

if (isCountQuery) {
Expand Down Expand Up @@ -326,7 +326,10 @@ const getTableInstance = (tableConfig, knex) => {
isDefaultTrx = true;
}

const query = queryBuilder(params, tableConfig.primaryKey, false, trx).del();
const query = queryBuilder(params, tableConfig.primaryKey, false)
.del()
.transacting(trx);

if (isDefaultTrx) return query
.then(async result => {
await trx.commit();
Expand All @@ -347,7 +350,10 @@ const getTableInstance = (tableConfig, knex) => {
}

ids = Array.isArray(ids) ? ids : [ids];
const query = knex(tableName).transacting(trx).whereIn(primaryKey, ids).del();
const query = knex(tableName).whereIn(primaryKey, ids)
.del()
.transacting(trx);

if (isDefaultTrx) return query
.then(async result => {
await trx.commit();
Expand All @@ -368,8 +374,9 @@ const getTableInstance = (tableConfig, knex) => {
}

const { where, updates } = params;
const query = queryBuilder({ ...where }, tableConfig.primaryKey, false, trx)
.update({ ...updates });
const query = queryBuilder({ ...where }, tableConfig.primaryKey, false)
.update({ ...updates })
.transacting(trx);

if (isDefaultTrx) return query
.then(async result => {
Expand All @@ -395,7 +402,8 @@ const getTableInstance = (tableConfig, knex) => {
columns = Array.isArray(tableConfig.primaryKey)
? tableConfig.primaryKey : [tableConfig.primaryKey];
}
const query = queryBuilder(params, columns, false, trx);
const query = queryBuilder(params, columns, false)
.transacting(trx);
const debugSql = query.toSQL().toNative();
logger.debug(`${debugSql.sql}; bindings: ${debugSql.bindings}.`);

Expand Down Expand Up @@ -427,7 +435,8 @@ const getTableInstance = (tableConfig, knex) => {
column = Array.isArray(column) ? [column[0]] : [column];
}

const query = queryBuilder(params, column, true, trx);
const query = queryBuilder(params, column, true)
.transacting(trx);
const debugSql = query.toSQL().toNative();
logger.debug(`${debugSql.sql}; bindings: ${debugSql.bindings}.`);

Expand All @@ -452,7 +461,9 @@ const getTableInstance = (tableConfig, knex) => {
isDefaultTrx = true;
}

const query = trx.raw(queryStatement);
const query = trx
.raw(queryStatement)
.transacting(trx);

if (isDefaultTrx) return query
.then(async result => {
Expand All @@ -475,7 +486,9 @@ const getTableInstance = (tableConfig, knex) => {
isDefaultTrx = true;
}

const query = queryBuilder(params, false, false, trx).increment(params.increment);
const query = queryBuilder(params, false, false)
.increment(params.increment)
.transacting(trx);

if (isDefaultTrx) return query
.then(async result => {
Expand All @@ -496,7 +509,9 @@ const getTableInstance = (tableConfig, knex) => {
isDefaultTrx = true;
}

const query = queryBuilder(params, false, false, trx).decrement(params.decrement);
const query = queryBuilder(params, false, false)
.decrement(params.decrement)
.transacting(trx);

if (isDefaultTrx) return query
.then(async result => {
Expand Down

0 comments on commit 8350c36

Please sign in to comment.