mirror of
https://github.com/allankoechke/turso.git
synced 2026-12-01 09:34:05 +00:00
134 lines
3.9 KiB
TypeScript
134 lines
3.9 KiB
TypeScript
import { RemoteWriter } from "./remote-writer.js";
|
|
|
|
/**
|
|
* Wraps a local Statement and routes execution to remote when appropriate.
|
|
* - If remoteWriter.isInTransaction → all go remote
|
|
* - If !stmt.readonly → remote + pull after execution
|
|
* - Otherwise → local Statement
|
|
*/
|
|
export class RemoteWriteStatement {
|
|
private localStmt: any;
|
|
private sql: string;
|
|
private isStmtReadonly: boolean;
|
|
private remoteWriter: RemoteWriter;
|
|
private pullFn: () => Promise<boolean>;
|
|
private _boundArgs: any[] = [];
|
|
|
|
constructor(
|
|
localStmt: any,
|
|
sql: string,
|
|
isStmtReadonly: boolean,
|
|
remoteWriter: RemoteWriter,
|
|
pullFn: () => Promise<boolean>,
|
|
) {
|
|
this.localStmt = localStmt;
|
|
this.sql = sql;
|
|
this.isStmtReadonly = isStmtReadonly;
|
|
this.remoteWriter = remoteWriter;
|
|
this.pullFn = pullFn;
|
|
}
|
|
|
|
private shouldGoRemote(): boolean {
|
|
return this.remoteWriter.isInTransaction || !this.isStmtReadonly;
|
|
}
|
|
|
|
private shouldPullAfter(): boolean {
|
|
// Pull after standalone writes (not in transaction)
|
|
return !this.isStmtReadonly && !this.remoteWriter.isInTransaction;
|
|
}
|
|
|
|
raw(toggle?: boolean) {
|
|
this.localStmt.raw(toggle);
|
|
return this;
|
|
}
|
|
|
|
pluck(toggle?: boolean) {
|
|
this.localStmt.pluck(toggle);
|
|
return this;
|
|
}
|
|
|
|
safeIntegers(toggle?: boolean) {
|
|
this.localStmt.safeIntegers(toggle);
|
|
return this;
|
|
}
|
|
|
|
columns() {
|
|
return this.localStmt.columns();
|
|
}
|
|
|
|
get reader(): boolean {
|
|
return this.localStmt.reader;
|
|
}
|
|
|
|
bind(...bindParameters: any[]) {
|
|
this._boundArgs = flattenArgs(bindParameters);
|
|
this.localStmt.bind(...bindParameters);
|
|
return this;
|
|
}
|
|
|
|
async run(...bindParameters: any[]) {
|
|
if (!this.shouldGoRemote()) {
|
|
return await this.localStmt.run(...bindParameters);
|
|
}
|
|
const args = bindParameters.length > 0 ? flattenArgs(bindParameters) : this._boundArgs;
|
|
const result = await this.remoteWriter.execute(this.sql, args);
|
|
if (this.shouldPullAfter()) {
|
|
await this.pullFn();
|
|
}
|
|
return {
|
|
changes: result.rowsAffected,
|
|
lastInsertRowid: result.lastInsertRowid,
|
|
};
|
|
}
|
|
|
|
async get(...bindParameters: any[]) {
|
|
if (!this.shouldGoRemote()) {
|
|
return await this.localStmt.get(...bindParameters);
|
|
}
|
|
const args = bindParameters.length > 0 ? flattenArgs(bindParameters) : this._boundArgs;
|
|
const result = await this.remoteWriter.execute(this.sql, args);
|
|
if (this.shouldPullAfter()) {
|
|
await this.pullFn();
|
|
}
|
|
return result.rows.length > 0 ? result.rows[0] : undefined;
|
|
}
|
|
|
|
async all(...bindParameters: any[]) {
|
|
if (!this.shouldGoRemote()) {
|
|
return await this.localStmt.all(...bindParameters);
|
|
}
|
|
const args = bindParameters.length > 0 ? flattenArgs(bindParameters) : this._boundArgs;
|
|
const result = await this.remoteWriter.execute(this.sql, args);
|
|
if (this.shouldPullAfter()) {
|
|
await this.pullFn();
|
|
}
|
|
return result.rows;
|
|
}
|
|
|
|
async *iterate(...bindParameters: any[]) {
|
|
if (!this.shouldGoRemote()) {
|
|
yield* this.localStmt.iterate(...bindParameters);
|
|
return;
|
|
}
|
|
const args = bindParameters.length > 0 ? flattenArgs(bindParameters) : this._boundArgs;
|
|
const result = await this.remoteWriter.execute(this.sql, args);
|
|
if (this.shouldPullAfter()) {
|
|
await this.pullFn();
|
|
}
|
|
for (const row of result.rows) {
|
|
yield row;
|
|
}
|
|
}
|
|
|
|
close() {
|
|
this.localStmt.close();
|
|
}
|
|
}
|
|
|
|
function flattenArgs(bindParameters: any[]): any[] {
|
|
if (bindParameters.length === 1 && Array.isArray(bindParameters[0])) {
|
|
return bindParameters[0];
|
|
}
|
|
return bindParameters;
|
|
}
|