Module

x/postgres/mod.ts>Savepoint

PostgreSQL driver for Deno
Extremely Popular
Go to Latest
class Savepoint
import { Savepoint } from "https://dotland.deno.dev/x/postgres@v0.17.2/mod.ts";

A savepoint is a point in a transaction that you can roll back to

Constructors

new
Savepoint(
name: string,
update_callback: (name: string) => Promise<void>,
release_callback: (name: string) => Promise<void>,
)

Create a new savepoint with the provided name and callbacks

Properties

readonly
instances: number

This is the count of the current savepoint instances in the transaction

Methods

Releasing a savepoint will remove it's last instance in the transaction

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 savepoint.release();
transaction.rollback(savepoint); // Error, can't rollback because the savepoint was released

It will also allow you to set the savepoint to the position it had before the last update

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 savepoint.update();
await savepoint.release(); // This drops the update of the last statement
transaction.rollback(savepoint); // Will rollback to the first instance of the savepoint

This function will throw if there are no savepoint instances to drop

Updating a savepoint will update its position in the transaction execution

import { Client } from "https://deno.land/x/postgres/mod.ts";
const client = new Client();
const transaction = client.createTransaction("transaction");

const my_value = "some value";

const savepoint = await transaction.savepoint("n1");
transaction.queryArray`INSERT INTO MY_TABLE (X) VALUES (${my_value})`;
await savepoint.update(); // Rolling back will now return you to this point on the transaction

You can also undo a savepoint update by using the release method

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");
transaction.queryArray`DELETE FROM VERY_IMPORTANT_TABLE`;
await savepoint.update(); // Oops, shouldn't have updated the savepoint
await savepoint.release(); // This will undo the last update and return the savepoint to the first instance
await transaction.rollback(); // Will rollback before the table was deleted