@tursodatabase/sync-common 0.5.0-pre.9 → 0.5.1-pre.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +4 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -0
- package/dist/remote-write-statement.d.ts +30 -0
- package/dist/remote-write-statement.d.ts.map +1 -0
- package/dist/remote-write-statement.js +110 -0
- package/dist/remote-writer.d.ts +53 -0
- package/dist/remote-writer.d.ts.map +1 -0
- package/dist/remote-writer.js +158 -0
- package/dist/types.d.ts +7 -0
- package/dist/types.d.ts.map +1 -1
- package/package.json +3 -2
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
import { run, memoryIO, runner, SyncEngineGuards, Runner } from "./run.js";
|
|
2
2
|
import { DatabaseOpts, ProtocolIo, RunOpts, DatabaseRowMutation, DatabaseRowStatement, DatabaseRowTransformResult, DatabaseStats, DatabaseChangeType, EncryptionOpts } from "./types.js";
|
|
3
|
+
import { RemoteWriter, RemoteWriterConfig } from "./remote-writer.js";
|
|
4
|
+
import { RemoteWriteStatement } from "./remote-write-statement.js";
|
|
3
5
|
export { run, memoryIO, runner, SyncEngineGuards, Runner };
|
|
4
|
-
export
|
|
6
|
+
export { RemoteWriter, RemoteWriteStatement };
|
|
7
|
+
export type { DatabaseStats, DatabaseOpts, DatabaseChangeType, DatabaseRowMutation, DatabaseRowStatement, DatabaseRowTransformResult, EncryptionOpts, RemoteWriterConfig, ProtocolIo, RunOpts, };
|
|
5
8
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,QAAQ,EAAE,MAAM,EAAE,gBAAgB,EAAE,MAAM,EAAE,MAAM,UAAU,CAAA;AAC1E,OAAO,EACH,YAAY,EACZ,UAAU,EACV,OAAO,EACP,mBAAmB,EACnB,oBAAoB,EACpB,0BAA0B,EAC1B,aAAa,EACb,kBAAkB,EAClB,cAAc,EACjB,MAAM,YAAY,CAAA;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,QAAQ,EAAE,MAAM,EAAE,gBAAgB,EAAE,MAAM,EAAE,MAAM,UAAU,CAAA;AAC1E,OAAO,EACH,YAAY,EACZ,UAAU,EACV,OAAO,EACP,mBAAmB,EACnB,oBAAoB,EACpB,0BAA0B,EAC1B,aAAa,EACb,kBAAkB,EAClB,cAAc,EACjB,MAAM,YAAY,CAAA;AACnB,OAAO,EAAE,YAAY,EAAE,kBAAkB,EAAE,MAAM,oBAAoB,CAAA;AACrE,OAAO,EAAE,oBAAoB,EAAE,MAAM,6BAA6B,CAAA;AAElE,OAAO,EAAE,GAAG,EAAE,QAAQ,EAAE,MAAM,EAAE,gBAAgB,EAAE,MAAM,EAAE,CAAA;AAC1D,OAAO,EAAE,YAAY,EAAE,oBAAoB,EAAE,CAAA;AAC7C,YAAY,EACR,aAAa,EACb,YAAY,EACZ,kBAAkB,EAClB,mBAAmB,EACnB,oBAAoB,EACpB,0BAA0B,EAC1B,cAAc,EACd,kBAAkB,EAElB,UAAU,EACV,OAAO,GACV,CAAA"}
|
package/dist/index.js
CHANGED
|
@@ -1,2 +1,5 @@
|
|
|
1
1
|
import { run, memoryIO, runner, SyncEngineGuards } from "./run.js";
|
|
2
|
+
import { RemoteWriter } from "./remote-writer.js";
|
|
3
|
+
import { RemoteWriteStatement } from "./remote-write-statement.js";
|
|
2
4
|
export { run, memoryIO, runner, SyncEngineGuards };
|
|
5
|
+
export { RemoteWriter, RemoteWriteStatement };
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { RemoteWriter } from "./remote-writer.js";
|
|
2
|
+
/**
|
|
3
|
+
* Wraps a local Statement and routes execution to remote when appropriate.
|
|
4
|
+
* - If remoteWriter.isInTransaction → all go remote
|
|
5
|
+
* - If !stmt.readonly → remote + pull after execution
|
|
6
|
+
* - Otherwise → local Statement
|
|
7
|
+
*/
|
|
8
|
+
export declare class RemoteWriteStatement {
|
|
9
|
+
private localStmt;
|
|
10
|
+
private sql;
|
|
11
|
+
private isStmtReadonly;
|
|
12
|
+
private remoteWriter;
|
|
13
|
+
private pullFn;
|
|
14
|
+
private _boundArgs;
|
|
15
|
+
constructor(localStmt: any, sql: string, isStmtReadonly: boolean, remoteWriter: RemoteWriter, pullFn: () => Promise<boolean>);
|
|
16
|
+
private shouldGoRemote;
|
|
17
|
+
private shouldPullAfter;
|
|
18
|
+
raw(toggle?: boolean): this;
|
|
19
|
+
pluck(toggle?: boolean): this;
|
|
20
|
+
safeIntegers(toggle?: boolean): this;
|
|
21
|
+
columns(): any;
|
|
22
|
+
get reader(): boolean;
|
|
23
|
+
bind(...bindParameters: any[]): this;
|
|
24
|
+
run(...bindParameters: any[]): Promise<any>;
|
|
25
|
+
get(...bindParameters: any[]): Promise<any>;
|
|
26
|
+
all(...bindParameters: any[]): Promise<any>;
|
|
27
|
+
iterate(...bindParameters: any[]): AsyncGenerator<any, void, any>;
|
|
28
|
+
close(): void;
|
|
29
|
+
}
|
|
30
|
+
//# sourceMappingURL=remote-write-statement.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"remote-write-statement.d.ts","sourceRoot":"","sources":["../remote-write-statement.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAElD;;;;;GAKG;AACH,qBAAa,oBAAoB;IAC7B,OAAO,CAAC,SAAS,CAAM;IACvB,OAAO,CAAC,GAAG,CAAS;IACpB,OAAO,CAAC,cAAc,CAAU;IAChC,OAAO,CAAC,YAAY,CAAe;IACnC,OAAO,CAAC,MAAM,CAAyB;IACvC,OAAO,CAAC,UAAU,CAAa;gBAG3B,SAAS,EAAE,GAAG,EACd,GAAG,EAAE,MAAM,EACX,cAAc,EAAE,OAAO,EACvB,YAAY,EAAE,YAAY,EAC1B,MAAM,EAAE,MAAM,OAAO,CAAC,OAAO,CAAC;IASlC,OAAO,CAAC,cAAc;IAItB,OAAO,CAAC,eAAe;IAKvB,GAAG,CAAC,MAAM,CAAC,EAAE,OAAO;IAKpB,KAAK,CAAC,MAAM,CAAC,EAAE,OAAO;IAKtB,YAAY,CAAC,MAAM,CAAC,EAAE,OAAO;IAK7B,OAAO;IAIP,IAAI,MAAM,IAAI,OAAO,CAEpB;IAED,IAAI,CAAC,GAAG,cAAc,EAAE,GAAG,EAAE;IAMvB,GAAG,CAAC,GAAG,cAAc,EAAE,GAAG,EAAE;IAe5B,GAAG,CAAC,GAAG,cAAc,EAAE,GAAG,EAAE;IAY5B,GAAG,CAAC,GAAG,cAAc,EAAE,GAAG,EAAE;IAY3B,OAAO,CAAC,GAAG,cAAc,EAAE,GAAG,EAAE;IAevC,KAAK;CAGR"}
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Wraps a local Statement and routes execution to remote when appropriate.
|
|
3
|
+
* - If remoteWriter.isInTransaction → all go remote
|
|
4
|
+
* - If !stmt.readonly → remote + pull after execution
|
|
5
|
+
* - Otherwise → local Statement
|
|
6
|
+
*/
|
|
7
|
+
export class RemoteWriteStatement {
|
|
8
|
+
localStmt;
|
|
9
|
+
sql;
|
|
10
|
+
isStmtReadonly;
|
|
11
|
+
remoteWriter;
|
|
12
|
+
pullFn;
|
|
13
|
+
_boundArgs = [];
|
|
14
|
+
constructor(localStmt, sql, isStmtReadonly, remoteWriter, pullFn) {
|
|
15
|
+
this.localStmt = localStmt;
|
|
16
|
+
this.sql = sql;
|
|
17
|
+
this.isStmtReadonly = isStmtReadonly;
|
|
18
|
+
this.remoteWriter = remoteWriter;
|
|
19
|
+
this.pullFn = pullFn;
|
|
20
|
+
}
|
|
21
|
+
shouldGoRemote() {
|
|
22
|
+
return this.remoteWriter.isInTransaction || !this.isStmtReadonly;
|
|
23
|
+
}
|
|
24
|
+
shouldPullAfter() {
|
|
25
|
+
// Pull after standalone writes (not in transaction)
|
|
26
|
+
return !this.isStmtReadonly && !this.remoteWriter.isInTransaction;
|
|
27
|
+
}
|
|
28
|
+
raw(toggle) {
|
|
29
|
+
this.localStmt.raw(toggle);
|
|
30
|
+
return this;
|
|
31
|
+
}
|
|
32
|
+
pluck(toggle) {
|
|
33
|
+
this.localStmt.pluck(toggle);
|
|
34
|
+
return this;
|
|
35
|
+
}
|
|
36
|
+
safeIntegers(toggle) {
|
|
37
|
+
this.localStmt.safeIntegers(toggle);
|
|
38
|
+
return this;
|
|
39
|
+
}
|
|
40
|
+
columns() {
|
|
41
|
+
return this.localStmt.columns();
|
|
42
|
+
}
|
|
43
|
+
get reader() {
|
|
44
|
+
return this.localStmt.reader;
|
|
45
|
+
}
|
|
46
|
+
bind(...bindParameters) {
|
|
47
|
+
this._boundArgs = flattenArgs(bindParameters);
|
|
48
|
+
this.localStmt.bind(...bindParameters);
|
|
49
|
+
return this;
|
|
50
|
+
}
|
|
51
|
+
async run(...bindParameters) {
|
|
52
|
+
if (!this.shouldGoRemote()) {
|
|
53
|
+
return await this.localStmt.run(...bindParameters);
|
|
54
|
+
}
|
|
55
|
+
const args = bindParameters.length > 0 ? flattenArgs(bindParameters) : this._boundArgs;
|
|
56
|
+
const result = await this.remoteWriter.execute(this.sql, args);
|
|
57
|
+
if (this.shouldPullAfter()) {
|
|
58
|
+
await this.pullFn();
|
|
59
|
+
}
|
|
60
|
+
return {
|
|
61
|
+
changes: result.rowsAffected,
|
|
62
|
+
lastInsertRowid: result.lastInsertRowid,
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
async get(...bindParameters) {
|
|
66
|
+
if (!this.shouldGoRemote()) {
|
|
67
|
+
return await this.localStmt.get(...bindParameters);
|
|
68
|
+
}
|
|
69
|
+
const args = bindParameters.length > 0 ? flattenArgs(bindParameters) : this._boundArgs;
|
|
70
|
+
const result = await this.remoteWriter.execute(this.sql, args);
|
|
71
|
+
if (this.shouldPullAfter()) {
|
|
72
|
+
await this.pullFn();
|
|
73
|
+
}
|
|
74
|
+
return result.rows.length > 0 ? result.rows[0] : undefined;
|
|
75
|
+
}
|
|
76
|
+
async all(...bindParameters) {
|
|
77
|
+
if (!this.shouldGoRemote()) {
|
|
78
|
+
return await this.localStmt.all(...bindParameters);
|
|
79
|
+
}
|
|
80
|
+
const args = bindParameters.length > 0 ? flattenArgs(bindParameters) : this._boundArgs;
|
|
81
|
+
const result = await this.remoteWriter.execute(this.sql, args);
|
|
82
|
+
if (this.shouldPullAfter()) {
|
|
83
|
+
await this.pullFn();
|
|
84
|
+
}
|
|
85
|
+
return result.rows;
|
|
86
|
+
}
|
|
87
|
+
async *iterate(...bindParameters) {
|
|
88
|
+
if (!this.shouldGoRemote()) {
|
|
89
|
+
yield* this.localStmt.iterate(...bindParameters);
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
92
|
+
const args = bindParameters.length > 0 ? flattenArgs(bindParameters) : this._boundArgs;
|
|
93
|
+
const result = await this.remoteWriter.execute(this.sql, args);
|
|
94
|
+
if (this.shouldPullAfter()) {
|
|
95
|
+
await this.pullFn();
|
|
96
|
+
}
|
|
97
|
+
for (const row of result.rows) {
|
|
98
|
+
yield row;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
close() {
|
|
102
|
+
this.localStmt.close();
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
function flattenArgs(bindParameters) {
|
|
106
|
+
if (bindParameters.length === 1 && Array.isArray(bindParameters[0])) {
|
|
107
|
+
return bindParameters[0];
|
|
108
|
+
}
|
|
109
|
+
return bindParameters;
|
|
110
|
+
}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
export interface RemoteWriterConfig {
|
|
2
|
+
url: string;
|
|
3
|
+
authToken?: string | (() => Promise<string>);
|
|
4
|
+
remoteEncryptionKey?: string;
|
|
5
|
+
}
|
|
6
|
+
/**
|
|
7
|
+
* Manages remote write routing using Session from @tursodatabase/serverless.
|
|
8
|
+
* For standalone writes (not in txn), creates a temporary Session per write.
|
|
9
|
+
* For transactions, reuses Session (baton-based) across all statements.
|
|
10
|
+
*/
|
|
11
|
+
export declare class RemoteWriter {
|
|
12
|
+
private session;
|
|
13
|
+
private _inRemoteTxn;
|
|
14
|
+
private config;
|
|
15
|
+
constructor(config: RemoteWriterConfig);
|
|
16
|
+
private resolveAuthToken;
|
|
17
|
+
private createSession;
|
|
18
|
+
/**
|
|
19
|
+
* Execute single SQL on remote. Creates temp session if not in txn.
|
|
20
|
+
*/
|
|
21
|
+
execute(sql: string, args?: any[]): Promise<{
|
|
22
|
+
columns: string[];
|
|
23
|
+
rows: any[];
|
|
24
|
+
rowsAffected: number;
|
|
25
|
+
lastInsertRowid: number | undefined;
|
|
26
|
+
}>;
|
|
27
|
+
/**
|
|
28
|
+
* Execute multi-statement SQL on remote (for exec() path).
|
|
29
|
+
*/
|
|
30
|
+
sequence(sql: string): Promise<void>;
|
|
31
|
+
/**
|
|
32
|
+
* Begin a remote transaction. Creates a session and sends BEGIN.
|
|
33
|
+
*/
|
|
34
|
+
beginTransaction(mode: string): Promise<void>;
|
|
35
|
+
/**
|
|
36
|
+
* Commit the remote transaction. Sends COMMIT and closes the session.
|
|
37
|
+
*/
|
|
38
|
+
commitTransaction(): Promise<void>;
|
|
39
|
+
/**
|
|
40
|
+
* Rollback the remote transaction. Sends ROLLBACK and closes the session.
|
|
41
|
+
*/
|
|
42
|
+
rollbackTransaction(): Promise<void>;
|
|
43
|
+
/**
|
|
44
|
+
* Execute SQL on remote with auto-detection of BEGIN/COMMIT/ROLLBACK.
|
|
45
|
+
* Manages session lifecycle based on SQL category.
|
|
46
|
+
*/
|
|
47
|
+
execRemote(sql: string, category: string): Promise<{
|
|
48
|
+
shouldPull: boolean;
|
|
49
|
+
}>;
|
|
50
|
+
get isInTransaction(): boolean;
|
|
51
|
+
close(): Promise<void>;
|
|
52
|
+
}
|
|
53
|
+
//# sourceMappingURL=remote-writer.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"remote-writer.d.ts","sourceRoot":"","sources":["../remote-writer.ts"],"names":[],"mappings":"AAEA,MAAM,WAAW,kBAAkB;IAC/B,GAAG,EAAE,MAAM,CAAC;IACZ,SAAS,CAAC,EAAE,MAAM,GAAG,CAAC,MAAM,OAAO,CAAC,MAAM,CAAC,CAAC,CAAC;IAC7C,mBAAmB,CAAC,EAAE,MAAM,CAAC;CAChC;AAED;;;;GAIG;AACH,qBAAa,YAAY;IACrB,OAAO,CAAC,OAAO,CAAwB;IACvC,OAAO,CAAC,YAAY,CAAkB;IACtC,OAAO,CAAC,MAAM,CAAqB;gBAEvB,MAAM,EAAE,kBAAkB;YAIxB,gBAAgB;YAOhB,aAAa;IAU3B;;OAEG;IACG,OAAO,CAAC,GAAG,EAAE,MAAM,EAAE,IAAI,GAAE,GAAG,EAAO,GAAG,OAAO,CAAC;QAClD,OAAO,EAAE,MAAM,EAAE,CAAC;QAClB,IAAI,EAAE,GAAG,EAAE,CAAC;QACZ,YAAY,EAAE,MAAM,CAAC;QACrB,eAAe,EAAE,MAAM,GAAG,SAAS,CAAC;KACvC,CAAC;IAYF;;OAEG;IACG,QAAQ,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAa1C;;OAEG;IACG,gBAAgB,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAMnD;;OAEG;IACG,iBAAiB,IAAI,OAAO,CAAC,IAAI,CAAC;IAaxC;;OAEG;IACG,mBAAmB,IAAI,OAAO,CAAC,IAAI,CAAC;IAa1C;;;OAGG;IACG,UAAU,CAAC,GAAG,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC;QAAE,UAAU,EAAE,OAAO,CAAA;KAAE,CAAC;IAuBjF,IAAI,eAAe,IAAI,OAAO,CAE7B;IAEK,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;CAc/B"}
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
import { Session } from "@tursodatabase/serverless";
|
|
2
|
+
/**
|
|
3
|
+
* Manages remote write routing using Session from @tursodatabase/serverless.
|
|
4
|
+
* For standalone writes (not in txn), creates a temporary Session per write.
|
|
5
|
+
* For transactions, reuses Session (baton-based) across all statements.
|
|
6
|
+
*/
|
|
7
|
+
export class RemoteWriter {
|
|
8
|
+
session = null;
|
|
9
|
+
_inRemoteTxn = false;
|
|
10
|
+
config;
|
|
11
|
+
constructor(config) {
|
|
12
|
+
this.config = config;
|
|
13
|
+
}
|
|
14
|
+
async resolveAuthToken() {
|
|
15
|
+
if (typeof this.config.authToken === "function") {
|
|
16
|
+
return await this.config.authToken();
|
|
17
|
+
}
|
|
18
|
+
return this.config.authToken;
|
|
19
|
+
}
|
|
20
|
+
async createSession() {
|
|
21
|
+
const authToken = await this.resolveAuthToken();
|
|
22
|
+
const sessionConfig = {
|
|
23
|
+
url: this.config.url,
|
|
24
|
+
authToken,
|
|
25
|
+
remoteEncryptionKey: this.config.remoteEncryptionKey,
|
|
26
|
+
};
|
|
27
|
+
return new Session(sessionConfig);
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Execute single SQL on remote. Creates temp session if not in txn.
|
|
31
|
+
*/
|
|
32
|
+
async execute(sql, args = []) {
|
|
33
|
+
if (this._inRemoteTxn) {
|
|
34
|
+
return await this.session.execute(sql, args);
|
|
35
|
+
}
|
|
36
|
+
const session = await this.createSession();
|
|
37
|
+
try {
|
|
38
|
+
return await session.execute(sql, args);
|
|
39
|
+
}
|
|
40
|
+
finally {
|
|
41
|
+
await session.close();
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Execute multi-statement SQL on remote (for exec() path).
|
|
46
|
+
*/
|
|
47
|
+
async sequence(sql) {
|
|
48
|
+
if (this._inRemoteTxn) {
|
|
49
|
+
await this.session.sequence(sql);
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
const session = await this.createSession();
|
|
53
|
+
try {
|
|
54
|
+
await session.sequence(sql);
|
|
55
|
+
}
|
|
56
|
+
finally {
|
|
57
|
+
await session.close();
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Begin a remote transaction. Creates a session and sends BEGIN.
|
|
62
|
+
*/
|
|
63
|
+
async beginTransaction(mode) {
|
|
64
|
+
this.session = await this.createSession();
|
|
65
|
+
await this.session.sequence("BEGIN " + mode);
|
|
66
|
+
this._inRemoteTxn = true;
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Commit the remote transaction. Sends COMMIT and closes the session.
|
|
70
|
+
*/
|
|
71
|
+
async commitTransaction() {
|
|
72
|
+
if (!this.session) {
|
|
73
|
+
throw new Error("No active remote transaction");
|
|
74
|
+
}
|
|
75
|
+
try {
|
|
76
|
+
await this.session.sequence("COMMIT");
|
|
77
|
+
}
|
|
78
|
+
finally {
|
|
79
|
+
this._inRemoteTxn = false;
|
|
80
|
+
await this.session.close();
|
|
81
|
+
this.session = null;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Rollback the remote transaction. Sends ROLLBACK and closes the session.
|
|
86
|
+
*/
|
|
87
|
+
async rollbackTransaction() {
|
|
88
|
+
if (!this.session) {
|
|
89
|
+
throw new Error("No active remote transaction");
|
|
90
|
+
}
|
|
91
|
+
try {
|
|
92
|
+
await this.session.sequence("ROLLBACK");
|
|
93
|
+
}
|
|
94
|
+
finally {
|
|
95
|
+
this._inRemoteTxn = false;
|
|
96
|
+
await this.session.close();
|
|
97
|
+
this.session = null;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* Execute SQL on remote with auto-detection of BEGIN/COMMIT/ROLLBACK.
|
|
102
|
+
* Manages session lifecycle based on SQL category.
|
|
103
|
+
*/
|
|
104
|
+
async execRemote(sql, category) {
|
|
105
|
+
if (category === "begin") {
|
|
106
|
+
this.session = await this.createSession();
|
|
107
|
+
await this.session.sequence(sql);
|
|
108
|
+
this._inRemoteTxn = true;
|
|
109
|
+
return { shouldPull: false };
|
|
110
|
+
}
|
|
111
|
+
if (category === "commit") {
|
|
112
|
+
if (!this.session)
|
|
113
|
+
throw new Error("No active remote transaction");
|
|
114
|
+
try {
|
|
115
|
+
await this.session.sequence(sql);
|
|
116
|
+
}
|
|
117
|
+
finally {
|
|
118
|
+
this._inRemoteTxn = false;
|
|
119
|
+
await this.session.close();
|
|
120
|
+
this.session = null;
|
|
121
|
+
}
|
|
122
|
+
return { shouldPull: true };
|
|
123
|
+
}
|
|
124
|
+
if (category === "rollback") {
|
|
125
|
+
if (!this.session)
|
|
126
|
+
throw new Error("No active remote transaction");
|
|
127
|
+
try {
|
|
128
|
+
await this.session.sequence(sql);
|
|
129
|
+
}
|
|
130
|
+
finally {
|
|
131
|
+
this._inRemoteTxn = false;
|
|
132
|
+
await this.session.close();
|
|
133
|
+
this.session = null;
|
|
134
|
+
}
|
|
135
|
+
return { shouldPull: false };
|
|
136
|
+
}
|
|
137
|
+
await this.sequence(sql);
|
|
138
|
+
return { shouldPull: !this._inRemoteTxn };
|
|
139
|
+
}
|
|
140
|
+
get isInTransaction() {
|
|
141
|
+
return this._inRemoteTxn;
|
|
142
|
+
}
|
|
143
|
+
async close() {
|
|
144
|
+
if (this.session) {
|
|
145
|
+
try {
|
|
146
|
+
if (this._inRemoteTxn) {
|
|
147
|
+
await this.session.sequence("ROLLBACK");
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
catch {
|
|
151
|
+
// ignore errors during cleanup
|
|
152
|
+
}
|
|
153
|
+
await this.session.close();
|
|
154
|
+
this.session = null;
|
|
155
|
+
this._inRemoteTxn = false;
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
}
|
package/dist/types.d.ts
CHANGED
|
@@ -104,6 +104,13 @@ export interface DatabaseOpts {
|
|
|
104
104
|
* optional parameter to enable internal logging for the database
|
|
105
105
|
*/
|
|
106
106
|
tracing?: 'error' | 'warn' | 'info' | 'debug' | 'trace';
|
|
107
|
+
/**
|
|
108
|
+
* When enabled, write statements execute on remote server instead of locally.
|
|
109
|
+
* After each write (or transaction commit), changes are pulled for read-your-writes consistency.
|
|
110
|
+
* Requires `url`. All explicit transactions go to remote.
|
|
111
|
+
* WARNING: This feature is EXPERIMENTAL
|
|
112
|
+
*/
|
|
113
|
+
remoteWritesExperimental?: boolean;
|
|
107
114
|
/**
|
|
108
115
|
* optional parameter to enable partial sync for the database
|
|
109
116
|
* WARNING: This feature is EXPERIMENTAL
|
package/dist/types.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../types.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,OAAO,CAAC,KAAK,MAAM,kBAAkB;IACxC,MAAM,WAAW;IACjB,MAAM,WAAW;IACjB,MAAM,WAAW;CACpB;AAED,MAAM,WAAW,oBAAoB;IACjC;;OAEG;IACH,GAAG,EAAE,MAAM,CAAA;IACX;;OAEG;IACH,MAAM,EAAE,KAAK,CAAC,GAAG,CAAC,CAAA;CACrB;AAGD;;;;;GAKG;AACH,MAAM,MAAM,0BAA0B,GAAG;IAAE,SAAS,EAAE,MAAM,CAAA;CAAE,GAAG;IAAE,SAAS,EAAE,SAAS,CAAC;IAAC,IAAI,EAAE,oBAAoB,CAAA;CAAE,GAAG,IAAI,CAAC;AAE7H,MAAM,WAAW,mBAAmB;IAChC;;OAEG;IACH,UAAU,EAAE,MAAM,CAAA;IAClB;;OAEG;IACH,SAAS,EAAE,MAAM,CAAA;IACjB;;OAEG;IACH,EAAE,EAAE,MAAM,CAAA;IACV;;OAEG;IACH,UAAU,EAAE,kBAAkB,CAAA;IAC9B;;OAEG;IACH,MAAM,CAAC,EAAE,MAAM,CAAC,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;IAChC;;;OAGG;IACH,QAAQ,EAAE,MAAM,GAAG,IAAI,CAAC;IACxB;;OAEG;IACH,gBAAgB,EAAE,MAAM,CAAC;IACzB;;OAEG;IACH,oBAAoB,EAAE,MAAM,CAAC;CAChC;AAID,MAAM,WAAW,OAAO;IACpB,YAAY,EAAE,MAAM,CAAC;IACrB,GAAG,EAAE,MAAM,GAAG,CAAC,MAAM,MAAM,GAAG,IAAI,CAAC,CAAC;IACpC,OAAO,EAAE;QAAE,CAAC,CAAC,EAAE,MAAM,GAAG,MAAM,CAAA;KAAE,GAAG,CAAC,MAAM,OAAO,CAAC;QAAE,CAAC,CAAC,EAAE,MAAM,GAAG,MAAM,CAAA;KAAE,CAAC,CAAC,CAAA;IAC3E,SAAS,CAAC,EAAE,SAAS,CAAC;CACzB;AAED,MAAM,WAAW,UAAU;IACvB,IAAI,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,GAAG,UAAU,GAAG,IAAI,CAAC,CAAC;IACxD,KAAK,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,GAAG,UAAU,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;CACpE;AACD,MAAM,MAAM,iBAAiB,GAAG;IAAE,IAAI,EAAE,IAAI,CAAA;CAAE,GAAG;IAAE,IAAI,EAAE,MAAM,CAAA;CAAE,GAAG,CAAC;IAAE,IAAI,EAAE,iBAAiB,CAAA;CAAE,GAAG,aAAa,CAAC,GAAG;IAAE,IAAI,EAAE,mBAAmB,CAAC;IAAC,OAAO,EAAE,GAAG,CAAA;CAAE,CAAA"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tursodatabase/sync-common",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.1-pre.1",
|
|
4
4
|
"repository": {
|
|
5
5
|
"type": "git",
|
|
6
6
|
"url": "https://github.com/tursodatabase/turso"
|
|
@@ -23,6 +23,7 @@
|
|
|
23
23
|
"test": "echo 'no tests'"
|
|
24
24
|
},
|
|
25
25
|
"dependencies": {
|
|
26
|
-
"@tursodatabase/database-common": "^0.5.
|
|
26
|
+
"@tursodatabase/database-common": "^0.5.1-pre.1",
|
|
27
|
+
"@tursodatabase/serverless": "^0.2.2"
|
|
27
28
|
}
|
|
28
29
|
}
|