import { Transaction } from "https://dotland.deno.dev/x/postgres@v0.19.3/query/transaction.ts";
This method will generate a savepoint, which will allow you to reset transaction states to a previous point of time
Each savepoint has a unique name used to identify it, and it must abide the following rules
- Savepoint names must start with a letter or an underscore
- Savepoint names are case insensitive
- Savepoint names can't be longer than 63 characters
- Savepoint names can only have alphanumeric characters
A savepoint can be easily created like this
import { Client } from "https://deno.land/x/postgres/mod.ts";
const client = new Client();
const transaction = client.createTransaction("transaction");
const savepoint = await transaction.savepoint("MY_savepoint"); // returns a `Savepoint` with name "my_savepoint"
await transaction.rollback(savepoint);
await savepoint.release(); // The savepoint will be removed
All savepoints can have multiple positions in a transaction, and you can change or update
this positions by using the update
and release
methods
import { Client } from "https://deno.land/x/postgres/mod.ts";
const client = new Client();
const transaction = client.createTransaction("transaction");
const savepoint = await transaction.savepoint("n1");
await transaction.queryArray`INSERT INTO MY_TABLE VALUES (${'A'}, ${2})`;
await savepoint.update(); // The savepoint will continue from here
await transaction.queryArray`DELETE FROM MY_TABLE`;
await transaction.rollback(savepoint); // The transaction will rollback before the delete, but after the insert
await savepoint.release(); // The last savepoint will be removed, the original one will remain
await transaction.rollback(savepoint); // It rolls back before the insert
await savepoint.release(); // All savepoints are released
Creating a new savepoint with an already used name will return you a reference to the original savepoint
import { Client } from "https://deno.land/x/postgres/mod.ts";
const client = new Client();
const transaction = client.createTransaction("transaction");
const savepoint_a = await transaction.savepoint("a");
await transaction.queryArray`DELETE FROM MY_TABLE`;
const savepoint_b = await transaction.savepoint("a"); // They will be the same savepoint, but the savepoint will be updated to this position
await transaction.rollback(savepoint_a); // Rolls back to savepoint_b