Add PostgreSQL FOR UPDATE getters
Add a PostgreSQL-specific getRecordForUpdate helper that appends FOR
UPDATE to the generated SELECT statement so callers can lock a row
inside an explicit transaction.
generateProcsForModels now always emits a direct-connection
get<RecordName>ForUpdate proc that accepts db_postgres.DbConn. There is
intentionally no dbType overload for this API, because reacquiring a
connection via withConnection would defeat the lock's transactional
scope.
The source docs and README now document the new helper and show the
intended usage pattern inside inTransaction:
db.inTransaction:
var item = conn.getTodoItemForUpdate(todoId)
item.priority += 1
discard conn.updateTodoItem(item)
This commit is contained in:
@@ -109,6 +109,7 @@ This will generate procedures like the following in two flavors:
|
||||
.. code-block:: Nim
|
||||
proc getTodoItem*(db: TodoDB, id: UUID): TodoItem;
|
||||
proc getTodoItem*[D: DbConnType](conn: D, id: UUID): TodoItem;
|
||||
proc getTodoItemForUpdate*(conn: db_postgres.DbConn, id: UUID): TodoItem;
|
||||
proc tryGetTodoItem*(db: TodoDB, id: UUID): Option[TodoItem];
|
||||
proc tryGetTodoItem*[D: DbConnType](conn: D, id: UUID): Option[TodoItem];
|
||||
proc getTodoItemIfItExists*(db: TodoDB, id: UUID): Option[TodoItem];
|
||||
@@ -170,6 +171,8 @@ This will generate procedures like the following in two flavors:
|
||||
|
||||
Use the `dbType` flavor when the caller does not already have a connection.
|
||||
Use the connection flavor inside `withConnection` or `inTransaction`.
|
||||
The generated `get<RecordName>ForUpdate` helper is PostgreSQL-specific and
|
||||
is only available for direct PostgreSQL connections.
|
||||
|
||||
Warning: do not call the `dbType` flavor from inside `inTransaction`.
|
||||
Those overloads call `withConnection` and may acquire a different
|
||||
@@ -178,7 +181,7 @@ transaction.
|
||||
|
||||
.. code-block:: Nim
|
||||
db.inTransaction:
|
||||
var item = conn.getTodoItem(todoId)
|
||||
var item = conn.getTodoItemForUpdate(todoId)
|
||||
item.priority += 1
|
||||
discard conn.updateTodoItem(item)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user