1 line
37 KiB
JSON
1 line
37 KiB
JSON
|
{"ast":null,"code":"\"use strict\";\n\nvar __createBinding = this && this.__createBinding || (Object.create ? function (o, m, k, k2) {\n if (k2 === undefined) k2 = k;\n var desc = Object.getOwnPropertyDescriptor(m, k);\n if (!desc || (\"get\" in desc ? !m.__esModule : desc.writable || desc.configurable)) {\n desc = {\n enumerable: true,\n get: function () {\n return m[k];\n }\n };\n }\n Object.defineProperty(o, k2, desc);\n} : function (o, m, k, k2) {\n if (k2 === undefined) k2 = k;\n o[k2] = m[k];\n});\nvar __setModuleDefault = this && this.__setModuleDefault || (Object.create ? function (o, v) {\n Object.defineProperty(o, \"default\", {\n enumerable: true,\n value: v\n });\n} : function (o, v) {\n o[\"default\"] = v;\n});\nvar __importStar = this && this.__importStar || function (mod) {\n if (mod && mod.__esModule) return mod;\n var result = {};\n if (mod != null) for (var k in mod) if (k !== \"default\" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);\n __setModuleDefault(result, mod);\n return result;\n};\nObject.defineProperty(exports, \"__esModule\", {\n value: true\n});\nexports.mapHranaError = exports.resultSetFromHrana = exports.stmtToHrana = exports.executeHranaBatch = exports.HranaTransaction = void 0;\nconst hrana = __importStar(require(\"@libsql/hrana-client\"));\nconst api_1 = require(\"@libsql/core/api\");\nconst util_1 = require(\"@libsql/core/util\");\nclass HranaTransaction {\n #mode;\n #version;\n // Promise that is resolved when the BEGIN statement completes, or `undefined` if we haven't executed the\n // BEGIN statement yet.\n #started;\n /** @private */\n constructor(mode, version) {\n this.#mode = mode;\n this.#version = version;\n this.#started = undefined;\n }\n execute(stmt) {\n return this.batch([stmt]).then(results => results[0]);\n }\n async batch(stmts) {\n const stream = this._getStream();\n if (stream.closed) {\n throw new api_1.LibsqlError(\"Cannot execute statements because the transaction is closed\", \"TRANSACTION_CLOSED\");\n }\n try {\n const hranaStmts = stmts.map(stmtToHrana);\n let rowsPromises;\n if (this.#started === undefined) {\n // The transaction hasn't started yet, so we need to send the BEGIN statement in a batch with\n // `hranaStmts`.\n this._getSqlCache().apply(hranaStmts);\n const batch = stream.batch(this.#version >= 3);\n const beginStep = batch.step();\n const beginPromise = beginStep.run((0, util_1.transactionModeToBegin)(this.#mode));\n // Execute the `hranaStmts` only if the BEGIN succeeded, to make sure that we don't execute it\n // outside of a transaction.\n let lastStep = beginStep;\n rowsPromises = hranaStmts.map(hranaStmt => {\n const stmtStep = batch.step().condition(hrana.BatchCond.ok(lastStep));\n if (this.#version >= 3) {\n // If the Hrana version supports it, make sure that we are still in a transaction\n stmtStep.condition(hrana.BatchCond.not(hrana.BatchCond.isAutocommit(batch)));\n }\n const rowsPromise = stmtStep.query(hranaStmt);\n rowsPromise.catch(() => undefined); // silence Node warning\n lastStep = stmtStep;\n return rowsPromise;\n });\n // `this.#started` is resolved successfully only if the batch and the BEGIN statement inside\n // of the batch are both successful.\n this.#started = batch.execute().then(() => beginPromise).then(() => undefined);\n try {\n await this.#started;\n } catch (e) {\n // If the BEGIN failed, the transaction is unusable and we must close it. However, if the\n // BEGIN suceeds and `hranaStmts` fail, the transaction is _not_ closed.\n this.close();\n throw e;\n }\n } else {\n if (this.#version < 3) {\n // The transaction has started, so we must wait until the BEGIN statement completed to make\n // sure that w
|