UNPKG

@dossierhq/sqlite-core

Version:

A library used by concrete SQLite adapters for Dossier.

27 lines 1.49 kB
/// <reference types="./advisoryLockRenew.d.ts" /> /* eslint-disable @typescript-eslint/no-unused-expressions */ import { notOk, ok } from '@dossierhq/core'; import { buildSqliteSqlQuery } from '@dossierhq/database-adapter'; import { queryNoneOrOne } from '../QueryFunctions.js'; import { getTransactionTimestamp } from '../SqliteTransaction.js'; export async function advisoryLockRenew(database, context, name, handle) { const now = getTransactionTimestamp(context.transaction); const query = buildSqliteSqlQuery(({ sql }) => { sql `UPDATE advisory_locks SET renewed_at = ${now.toISOString()}, expires_at = ${now.getTime()} + lease_duration WHERE name = ${name} AND handle = ${handle} RETURNING acquired_at`; }); const updateResult = await queryNoneOrOne(database, context, query); if (updateResult.isError()) return updateResult; if (updateResult.value !== null) { return ok({ acquiredAt: new Date(updateResult.value.acquired_at), renewedAt: now }); } const existingResult = await queryNoneOrOne(database, context, buildSqliteSqlQuery(({ sql }) => { sql `SELECT id FROM advisory_locks WHERE name = ${name}`; })); if (existingResult.isError()) return existingResult; return notOk.NotFound(existingResult.value ? `Invalid handle used for renewing lock '${name}'` : `Failed renewing lock, no advisory lock with the name '${name}' exists`); } //# sourceMappingURL=advisoryLockRenew.js.map