Skip to content

πŸ”₯ πŸ”₯ πŸ”₯ A Free & Self-hostable Airtable Alternative

refactor: variable names and detailed comment

Pranav C9d7165a85fd6
diff --git a/packages/nocodb/src/db/BaseModelSqlv2.ts b/packages/nocodb/src/db/BaseModelSqlv2.ts
index ceca83f683..abb0a43726 100644
--- a/packages/nocodb/src/db/BaseModelSqlv2.ts
+++ b/packages/nocodb/src/db/BaseModelSqlv2.ts
@@ -158,8 +158,10 @@ const INSERT_REGEX = /^(\(|)insert/i;
* @classdesc Base class for models
*/
class BaseModelSqlv2 implements IBaseModelSqlV2 {
+ /** The base database driver (always non-transactional) */
protected _dbDriver: XKnex;
- protected _transaction?: XKnex | Transaction;
+ /** Optional transaction instance - when set, operations use this instead of _dbDriver */
+ protected _activeTransaction?: XKnex | Transaction;
protected _viewId: string;
public get viewId() {
return this._viewId;
@@ -173,11 +175,23 @@ class BaseModelSqlv2 implements IBaseModelSqlV2 {
public static config: any = defaultLimitConfig;
+ /**
+ * Returns the active database driver for operations.
+ * If a transaction is active, returns the transaction; otherwise returns the base driver.
+ * This ensures all operations within a transaction use the same transaction context.
+ */
public get dbDriver() {
- return this._transaction || this._dbDriver;
+ return this._activeTransaction || this._dbDriver;
}
- // get non-transactional clone of this instance
+ /**
+ * Creates a new BaseModelSqlv2 instance that uses the base database driver
+ * instead of any active transaction. This is useful for operations that need
+ * to run outside of the current transaction context, such as broadcasting
+ * link updates to avoid transaction conflicts.
+ *
+ * @returns A new BaseModelSqlv2 instance with non-transactional database access
+ */
public getNonTransactionalClone() {
return new BaseModelSqlv2({
dbDriver: this._dbDriver,
@@ -188,6 +202,10 @@ class BaseModelSqlv2 implements IBaseModelSqlV2 {
});
}
+ /**
+ * Returns the base (non-transactional) database driver.
+ * This is always the original database connection, regardless of transaction state.
+ */
public get knex() {
return this._dbDriver;
}
@@ -205,7 +223,7 @@ class BaseModelSqlv2 implements IBaseModelSqlV2 {
schema?: string;
}) {
this._dbDriver = dbDriver;
- this._transaction = transaction;
+ this._activeTransaction = transaction;
this.model = model;
this._viewId = viewId;
this.context = context;
@@ -3199,6 +3217,8 @@ class BaseModelSqlv2 implements IBaseModelSqlV2 {
async updateLTARCols({ datas, cookie }: { datas: any[]; cookie: NcRequest }) {
const trx = await this.dbDriver.transaction();
+ // Create a BaseModelSqlv2 instance that uses the transaction for operations
+ // while preserving the original dbDriver reference for non-transactional operations
const trxBaseModel = await Model.getBaseModelSQL(this.context, {
model: this.model,
transaction: trx,
diff --git a/packages/nocodb/src/db/BaseModelSqlv2/add-remove-links.ts b/packages/nocodb/src/db/BaseModelSqlv2/add-remove-links.ts
index b7e51ee94c..ea6de5419e 100644
--- a/packages/nocodb/src/db/BaseModelSqlv2/add-remove-links.ts
+++ b/packages/nocodb/src/db/BaseModelSqlv2/add-remove-links.ts
@@ -17,6 +17,22 @@ import {
} from '~/helpers/dbHelpers';
import { Model } from '~/models';
+/**
+ * Transaction Handling Strategy for Link Operations:
+ *
+ * This module handles adding and removing links between records. To prevent transaction
+ * conflicts and ensure reliable broadcasting of link updates, we use a specific pattern:
+ *
+ * 1. Core link operations (insert/delete) run within the main transaction
+ * 2. Broadcasting operations use non-transactional clones to avoid conflicts
+ * 3. This separation ensures that broadcasting doesn't interfere with the main
+ * transaction's commit/rollback behavior
+ *
+ * The getNonTransactionalClone() method creates a new BaseModelSqlv2 instance
+ * that uses the base database driver instead of any active transaction, allowing
+ * broadcasting operations to run independently.
+ */
+
export const addOrRemoveLinks = (baseModel: IBaseModelSqlV2) => {
const validateRefIds = (
refIds: (string | number | Record<string, any>)[],
diff --git a/packages/nocodb/src/db/IBaseModelSqlV2.ts b/packages/nocodb/src/db/IBaseModelSqlV2.ts
index b573deb460..6005493875 100644
--- a/packages/nocodb/src/db/IBaseModelSqlV2.ts
+++ b/packages/nocodb/src/db/IBaseModelSqlV2.ts
@@ -354,10 +354,16 @@ export interface IBaseModelSqlV2 {
broadcastLinkUpdates(ids: Array<string>): Promise<void>;
getSource(): Promise<Source>;
+ /**
+ * Creates a non-transactional clone of this instance for operations
+ * that need to run outside the current transaction context.
+ */
getNonTransactionalClone(): IBaseModelSqlV2;
get viewId(): string;
+ /** Returns the active database driver (transaction if active, otherwise base driver) */
get dbDriver(): CustomKnex | Transaction;
+ /** Returns the base (non-transactional) database driver */
get knex(): CustomKnex;
get isSqlite(): boolean;
get isPg(): boolean;
diff --git a/packages/nocodb/src/models/Model.ts b/packages/nocodb/src/models/Model.ts
index 40ff85a57e..b9cfdd4049 100644
--- a/packages/nocodb/src/models/Model.ts
+++ b/packages/nocodb/src/models/Model.ts
@@ -550,6 +550,20 @@ export default class Model implements TableType {
return null;
}
+ /**
+ * Creates a BaseModelSqlv2 instance with optional transaction support.
+ *
+ * @param context - The NocoDB context
+ * @param args - Configuration arguments
+ * @param args.dbDriver - The base database driver
+ * @param args.transaction - Optional transaction instance to use for operations
+ * @param args.model - The model instance (optional, will be fetched if not provided)
+ * @param args.viewId - The view ID (optional)
+ * @param args.extractDefaultView - Whether to extract the default view if viewId not provided
+ * @param args.source - The data source (optional, will be fetched if not provided)
+ * @param ncMeta - The NocoDB metadata instance
+ * @returns A configured BaseModelSqlv2 instance
+ */
public static async getBaseModelSQL(
context: NcContext,
args: {