adjust sync native package

This commit is contained in:
Nikita Sivukhin
2025-09-24 18:43:50 +04:00
parent 28c9850b57
commit afbfa98a8d
7 changed files with 284 additions and 154 deletions

View File

@@ -1,5 +1,15 @@
import { run, memoryIO, SyncEngineGuards } from "./run.js" import { run, memoryIO, SyncEngineGuards } from "./run.js"
import { SyncOpts, ProtocolIo, RunOpts, DatabaseRowMutation, DatabaseRowStatement, DatabaseRowTransformResult, SyncEngineStats } from "./types.js" import { DatabaseOpts, ProtocolIo, RunOpts, DatabaseRowMutation, DatabaseRowStatement, DatabaseRowTransformResult, DatabaseStats, DatabaseChangeType } from "./types.js"
export { run, memoryIO, SyncEngineGuards } export { run, memoryIO, SyncEngineGuards }
export type { SyncOpts, ProtocolIo, RunOpts, DatabaseRowMutation, DatabaseRowStatement, DatabaseRowTransformResult, SyncEngineStats } export type {
DatabaseStats,
DatabaseOpts,
DatabaseChangeType,
DatabaseRowMutation,
DatabaseRowStatement,
DatabaseRowTransformResult,
ProtocolIo,
RunOpts,
}

View File

@@ -3,9 +3,6 @@
import { GeneratorResponse, ProtocolIo, RunOpts } from "./types.js"; import { GeneratorResponse, ProtocolIo, RunOpts } from "./types.js";
import { AsyncLock } from "@tursodatabase/database-common"; import { AsyncLock } from "@tursodatabase/database-common";
const GENERATOR_RESUME_IO = 0;
const GENERATOR_RESUME_DONE = 1;
interface TrackPromise<T> { interface TrackPromise<T> {
promise: Promise<T>, promise: Promise<T>,
finished: boolean finished: boolean
@@ -26,7 +23,7 @@ async function process(opts: RunOpts, io: ProtocolIo, request: any) {
const completion = request.completion(); const completion = request.completion();
if (requestType.type == 'Http') { if (requestType.type == 'Http') {
try { try {
let headers = opts.headers; let headers = typeof opts.headers === "function" ? opts.headers() : opts.headers;
if (requestType.headers != null && requestType.headers.length > 0) { if (requestType.headers != null && requestType.headers.length > 0) {
headers = { ...opts.headers }; headers = { ...opts.headers };
for (let header of requestType.headers) { for (let header of requestType.headers) {
@@ -128,11 +125,8 @@ export async function run(opts: RunOpts, io: ProtocolIo, engine: any, generator:
tasks = tasks.filter(t => !t.finished); tasks = tasks.filter(t => !t.finished);
} }
return generator.take();
} }
export class SyncEngineGuards { export class SyncEngineGuards {
waitLock: AsyncLock; waitLock: AsyncLock;
pushLock: AsyncLock; pushLock: AsyncLock;

View File

@@ -1,25 +1,136 @@
export declare const enum DatabaseChangeType { export declare const enum DatabaseChangeType {
Insert = 0, Insert = 'insert',
Update = 1, Update = 'update',
Delete = 2 Delete = 'delete'
} }
export interface DatabaseRowStatement {
/**
* SQL statements with positional placeholders (?)
*/
sql: string
/**
* values to substitute placeholders
*/
values: Array<any>
}
/**
* transformation result:
* - skip: ignore the mutation completely and do not apply it
* - rewrite: replace mutation with the provided statement
* - null: do not change mutation and keep it as is
*/
export type DatabaseRowTransformResult = { operation: 'skip' } | { operation: 'rewrite', stmt: DatabaseRowStatement } | null;
export interface DatabaseRowMutation { export interface DatabaseRowMutation {
/**
* unix seconds timestamp of the change
*/
changeTime: number changeTime: number
/**
* table name of the change
*/
tableName: string tableName: string
/**
* rowid of the change
*/
id: number id: number
/**
* type of the change (insert/delete/update)
*/
changeType: DatabaseChangeType changeType: DatabaseChangeType
/**
* columns of the row before the change
*/
before?: Record<string, any> before?: Record<string, any>
/**
* columns of the row after the change
*/
after?: Record<string, any> after?: Record<string, any>
/**
* only updated columns of the row after the change
*/
updates?: Record<string, any> updates?: Record<string, any>
} }
export type DatabaseRowTransformResult = { operation: 'skip' } | { operation: 'rewrite', stmt: DatabaseRowStatement } | null;
export type Transform = (arg: DatabaseRowMutation) => DatabaseRowTransformResult; export type Transform = (arg: DatabaseRowMutation) => DatabaseRowTransformResult;
export interface DatabaseOpts {
/**
* local path where to store all synced database files (e.g. local.db)
* note, that synced database will write several files with that prefix
* (e.g. local.db-info, local.db-wal, etc)
* */
path: string;
/**
* optional url of the remote database (e.g. libsql://db-org.turso.io)
* (if omitted - local-only database will be created)
*/
url?: string;
/**
* auth token for the remote database
* (can be either static string or function which will provide short-lived credentials for every new request)
*/
authToken?: string | (() => string);
/**
* arbitrary client name which can be used to distinguish clients internally
* the library will gurantee uniquiness of the clientId by appending unique suffix to the clientName
*/
clientName?: string;
/**
* optional key if cloud database were encrypted by default
*/
encryptionKey?: string;
/**
* optional callback which will be called for every mutation before sending it to the remote
* this callback can transform the update in order to support complex conflict resolution strategy
*/
transform?: Transform,
/**
* optional long-polling timeout for pull operation
* if not set - no timeout is applied
*/
longPollTimeoutMs?: number,
/**
* optional parameter to enable internal logging for the database
*/
tracing?: 'error' | 'warn' | 'info' | 'debug' | 'trace',
}
export interface DatabaseStats {
/**
* amount of local changes not sent to the remote
*/
operations: number;
/**
* size of the main WAL file in bytes
*/
mainWal: number;
/**
* size of the revert WAL file in bytes
*/
revertWal: number;
/**
* unix timestamp of last successful pull time
*/
lastPullUnixTime: number;
/**
* unix timestamp of last successful push time
*/
lastPushUnixTime: number | null;
/**
* opaque revision of the changes pulled locally from remote
* (can be used as e-tag, but string must not be interpreted in any way and must be used as opaque value)
*/
revision: string | null;
}
/* internal types used in the native/browser packages */
export interface RunOpts { export interface RunOpts {
preemptionMs: number, preemptionMs: number,
url: string, url: string,
headers: { [K: string]: string } headers: { [K: string]: string } | (() => { [K: string]: string })
transform?: Transform, transform?: Transform,
} }
@@ -27,31 +138,4 @@ export interface ProtocolIo {
read(path: string): Promise<Buffer | Uint8Array | null>; read(path: string): Promise<Buffer | Uint8Array | null>;
write(path: string, content: Buffer | Uint8Array): Promise<void>; write(path: string, content: Buffer | Uint8Array): Promise<void>;
} }
export type GeneratorResponse = { type: 'IO' } | { type: 'Done' } | ({ type: 'SyncEngineStats' } & DatabaseStats) | { type: 'SyncEngineChanges', changes: any }
export interface SyncOpts {
path: string;
clientName?: string;
url: string;
authToken?: string;
encryptionKey?: string;
tablesIgnore?: string[],
transform?: Transform,
longPollTimeoutMs?: number,
tracing?: string,
}
export interface DatabaseRowStatement {
sql: string
values: Array<any>
}
export interface SyncEngineStats {
operations: number;
mainWal: number;
revertWal: number;
lastPullUnixTime: number;
lastPushUnixTime: number | null;
revision: string | null;
}
export type GeneratorResponse = { type: 'IO' } | { type: 'Done' } | ({ type: 'SyncEngineStats' } & SyncEngineStats) | { type: 'SyncEngineChanges', changes: any }

View File

@@ -9,6 +9,18 @@ export declare class Database {
* * `path` - The path to the database file. * * `path` - The path to the database file.
*/ */
constructor(path: string, opts?: DatabaseOpts | undefined | null) constructor(path: string, opts?: DatabaseOpts | undefined | null)
/**
* Connect the database synchronously
* This method is idempotent and can be called multiple times safely until the database will be closed
*/
connectSync(): void
/**
* Connect the database asynchronously
* This method is idempotent and can be called multiple times safely until the database will be closed
*/
connectAsync(): Promise<void>
/** Returns whether the database is in readonly-only mode. */
get readonly(): boolean
/** Returns whether the database is in memory-only mode. */ /** Returns whether the database is in memory-only mode. */
get memory(): boolean get memory(): boolean
/** Returns whether the database is in memory-only mode. */ /** Returns whether the database is in memory-only mode. */
@@ -101,11 +113,6 @@ export declare class Statement {
* 1 = Row available, 2 = Done, 3 = I/O needed * 1 = Row available, 2 = Done, 3 = I/O needed
*/ */
stepSync(): number stepSync(): number
/**
* Step the statement and return result code (executed on the background thread):
* 1 = Row available, 2 = Done, 3 = I/O needed
*/
stepAsync(): Promise<number>
/** Get the current row data according to the presentation mode */ /** Get the current row data according to the presentation mode */
row(): unknown row(): unknown
/** Sets the presentation mode to raw. */ /** Sets the presentation mode to raw. */
@@ -126,7 +133,14 @@ export declare class Statement {
finalize(): void finalize(): void
} }
/**
* Most of the options are aligned with better-sqlite API
* (see https://github.com/WiseLibs/better-sqlite3/blob/master/docs/api.md#new-databasepath-options)
*/
export interface DatabaseOpts { export interface DatabaseOpts {
readonly?: boolean
timeout?: number
fileMustExist?: boolean
tracing?: string tracing?: string
} }
export declare class GeneratorHolder { export declare class GeneratorHolder {
@@ -153,7 +167,7 @@ export declare class JsProtocolRequestBytes {
export declare class SyncEngine { export declare class SyncEngine {
constructor(opts: SyncEngineOpts) constructor(opts: SyncEngineOpts)
init(): GeneratorHolder connect(): GeneratorHolder
ioLoopSync(): void ioLoopSync(): void
/** Runs the I/O loop asynchronously, returning a Promise. */ /** Runs the I/O loop asynchronously, returning a Promise. */
ioLoopAsync(): Promise<void> ioLoopAsync(): Promise<void>
@@ -163,22 +177,18 @@ export declare class SyncEngine {
wait(): GeneratorHolder wait(): GeneratorHolder
apply(changes: SyncEngineChanges): GeneratorHolder apply(changes: SyncEngineChanges): GeneratorHolder
checkpoint(): GeneratorHolder checkpoint(): GeneratorHolder
open(): Database db(): Database
close(): void close(): void
} }
export declare class SyncEngineChanges { export declare class SyncEngineChanges {
empty(): boolean
} }
export declare const enum DatabaseChangeTypeJs { export declare const enum DatabaseChangeTypeJs {
Insert = 0, Insert = 'insert',
Update = 1, Update = 'update',
Delete = 2 Delete = 'delete'
}
export interface DatabaseOpts {
path: string
} }
export interface DatabaseRowMutationJs { export interface DatabaseRowMutationJs {
@@ -225,6 +235,6 @@ export interface SyncEngineOpts {
} }
export declare const enum SyncEngineProtocolVersion { export declare const enum SyncEngineProtocolVersion {
Legacy = 0, Legacy = 'legacy',
V1 = 1 V1 = 'v1'
} }

View File

@@ -81,8 +81,8 @@ function requireNative() {
try { try {
const binding = require('@tursodatabase/sync-android-arm64') const binding = require('@tursodatabase/sync-android-arm64')
const bindingPackageVersion = require('@tursodatabase/sync-android-arm64/package.json').version const bindingPackageVersion = require('@tursodatabase/sync-android-arm64/package.json').version
if (bindingPackageVersion !== '0.2.0-pre.3' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') { if (bindingPackageVersion !== '0.2.0-pre.7' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
throw new Error(`Native binding package version mismatch, expected 0.2.0-pre.3 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`) throw new Error(`Native binding package version mismatch, expected 0.2.0-pre.7 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
} }
return binding return binding
} catch (e) { } catch (e) {
@@ -97,8 +97,8 @@ function requireNative() {
try { try {
const binding = require('@tursodatabase/sync-android-arm-eabi') const binding = require('@tursodatabase/sync-android-arm-eabi')
const bindingPackageVersion = require('@tursodatabase/sync-android-arm-eabi/package.json').version const bindingPackageVersion = require('@tursodatabase/sync-android-arm-eabi/package.json').version
if (bindingPackageVersion !== '0.2.0-pre.3' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') { if (bindingPackageVersion !== '0.2.0-pre.7' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
throw new Error(`Native binding package version mismatch, expected 0.2.0-pre.3 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`) throw new Error(`Native binding package version mismatch, expected 0.2.0-pre.7 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
} }
return binding return binding
} catch (e) { } catch (e) {
@@ -117,8 +117,8 @@ function requireNative() {
try { try {
const binding = require('@tursodatabase/sync-win32-x64-msvc') const binding = require('@tursodatabase/sync-win32-x64-msvc')
const bindingPackageVersion = require('@tursodatabase/sync-win32-x64-msvc/package.json').version const bindingPackageVersion = require('@tursodatabase/sync-win32-x64-msvc/package.json').version
if (bindingPackageVersion !== '0.2.0-pre.3' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') { if (bindingPackageVersion !== '0.2.0-pre.7' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
throw new Error(`Native binding package version mismatch, expected 0.2.0-pre.3 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`) throw new Error(`Native binding package version mismatch, expected 0.2.0-pre.7 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
} }
return binding return binding
} catch (e) { } catch (e) {
@@ -133,8 +133,8 @@ function requireNative() {
try { try {
const binding = require('@tursodatabase/sync-win32-ia32-msvc') const binding = require('@tursodatabase/sync-win32-ia32-msvc')
const bindingPackageVersion = require('@tursodatabase/sync-win32-ia32-msvc/package.json').version const bindingPackageVersion = require('@tursodatabase/sync-win32-ia32-msvc/package.json').version
if (bindingPackageVersion !== '0.2.0-pre.3' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') { if (bindingPackageVersion !== '0.2.0-pre.7' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
throw new Error(`Native binding package version mismatch, expected 0.2.0-pre.3 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`) throw new Error(`Native binding package version mismatch, expected 0.2.0-pre.7 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
} }
return binding return binding
} catch (e) { } catch (e) {
@@ -149,8 +149,8 @@ function requireNative() {
try { try {
const binding = require('@tursodatabase/sync-win32-arm64-msvc') const binding = require('@tursodatabase/sync-win32-arm64-msvc')
const bindingPackageVersion = require('@tursodatabase/sync-win32-arm64-msvc/package.json').version const bindingPackageVersion = require('@tursodatabase/sync-win32-arm64-msvc/package.json').version
if (bindingPackageVersion !== '0.2.0-pre.3' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') { if (bindingPackageVersion !== '0.2.0-pre.7' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
throw new Error(`Native binding package version mismatch, expected 0.2.0-pre.3 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`) throw new Error(`Native binding package version mismatch, expected 0.2.0-pre.7 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
} }
return binding return binding
} catch (e) { } catch (e) {
@@ -168,8 +168,8 @@ function requireNative() {
try { try {
const binding = require('@tursodatabase/sync-darwin-universal') const binding = require('@tursodatabase/sync-darwin-universal')
const bindingPackageVersion = require('@tursodatabase/sync-darwin-universal/package.json').version const bindingPackageVersion = require('@tursodatabase/sync-darwin-universal/package.json').version
if (bindingPackageVersion !== '0.2.0-pre.3' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') { if (bindingPackageVersion !== '0.2.0-pre.7' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
throw new Error(`Native binding package version mismatch, expected 0.2.0-pre.3 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`) throw new Error(`Native binding package version mismatch, expected 0.2.0-pre.7 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
} }
return binding return binding
} catch (e) { } catch (e) {
@@ -184,8 +184,8 @@ function requireNative() {
try { try {
const binding = require('@tursodatabase/sync-darwin-x64') const binding = require('@tursodatabase/sync-darwin-x64')
const bindingPackageVersion = require('@tursodatabase/sync-darwin-x64/package.json').version const bindingPackageVersion = require('@tursodatabase/sync-darwin-x64/package.json').version
if (bindingPackageVersion !== '0.2.0-pre.3' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') { if (bindingPackageVersion !== '0.2.0-pre.7' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
throw new Error(`Native binding package version mismatch, expected 0.2.0-pre.3 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`) throw new Error(`Native binding package version mismatch, expected 0.2.0-pre.7 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
} }
return binding return binding
} catch (e) { } catch (e) {
@@ -200,8 +200,8 @@ function requireNative() {
try { try {
const binding = require('@tursodatabase/sync-darwin-arm64') const binding = require('@tursodatabase/sync-darwin-arm64')
const bindingPackageVersion = require('@tursodatabase/sync-darwin-arm64/package.json').version const bindingPackageVersion = require('@tursodatabase/sync-darwin-arm64/package.json').version
if (bindingPackageVersion !== '0.2.0-pre.3' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') { if (bindingPackageVersion !== '0.2.0-pre.7' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
throw new Error(`Native binding package version mismatch, expected 0.2.0-pre.3 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`) throw new Error(`Native binding package version mismatch, expected 0.2.0-pre.7 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
} }
return binding return binding
} catch (e) { } catch (e) {
@@ -220,8 +220,8 @@ function requireNative() {
try { try {
const binding = require('@tursodatabase/sync-freebsd-x64') const binding = require('@tursodatabase/sync-freebsd-x64')
const bindingPackageVersion = require('@tursodatabase/sync-freebsd-x64/package.json').version const bindingPackageVersion = require('@tursodatabase/sync-freebsd-x64/package.json').version
if (bindingPackageVersion !== '0.2.0-pre.3' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') { if (bindingPackageVersion !== '0.2.0-pre.7' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
throw new Error(`Native binding package version mismatch, expected 0.2.0-pre.3 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`) throw new Error(`Native binding package version mismatch, expected 0.2.0-pre.7 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
} }
return binding return binding
} catch (e) { } catch (e) {
@@ -236,8 +236,8 @@ function requireNative() {
try { try {
const binding = require('@tursodatabase/sync-freebsd-arm64') const binding = require('@tursodatabase/sync-freebsd-arm64')
const bindingPackageVersion = require('@tursodatabase/sync-freebsd-arm64/package.json').version const bindingPackageVersion = require('@tursodatabase/sync-freebsd-arm64/package.json').version
if (bindingPackageVersion !== '0.2.0-pre.3' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') { if (bindingPackageVersion !== '0.2.0-pre.7' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
throw new Error(`Native binding package version mismatch, expected 0.2.0-pre.3 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`) throw new Error(`Native binding package version mismatch, expected 0.2.0-pre.7 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
} }
return binding return binding
} catch (e) { } catch (e) {
@@ -257,8 +257,8 @@ function requireNative() {
try { try {
const binding = require('@tursodatabase/sync-linux-x64-musl') const binding = require('@tursodatabase/sync-linux-x64-musl')
const bindingPackageVersion = require('@tursodatabase/sync-linux-x64-musl/package.json').version const bindingPackageVersion = require('@tursodatabase/sync-linux-x64-musl/package.json').version
if (bindingPackageVersion !== '0.2.0-pre.3' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') { if (bindingPackageVersion !== '0.2.0-pre.7' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
throw new Error(`Native binding package version mismatch, expected 0.2.0-pre.3 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`) throw new Error(`Native binding package version mismatch, expected 0.2.0-pre.7 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
} }
return binding return binding
} catch (e) { } catch (e) {
@@ -273,8 +273,8 @@ function requireNative() {
try { try {
const binding = require('@tursodatabase/sync-linux-x64-gnu') const binding = require('@tursodatabase/sync-linux-x64-gnu')
const bindingPackageVersion = require('@tursodatabase/sync-linux-x64-gnu/package.json').version const bindingPackageVersion = require('@tursodatabase/sync-linux-x64-gnu/package.json').version
if (bindingPackageVersion !== '0.2.0-pre.3' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') { if (bindingPackageVersion !== '0.2.0-pre.7' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
throw new Error(`Native binding package version mismatch, expected 0.2.0-pre.3 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`) throw new Error(`Native binding package version mismatch, expected 0.2.0-pre.7 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
} }
return binding return binding
} catch (e) { } catch (e) {
@@ -291,8 +291,8 @@ function requireNative() {
try { try {
const binding = require('@tursodatabase/sync-linux-arm64-musl') const binding = require('@tursodatabase/sync-linux-arm64-musl')
const bindingPackageVersion = require('@tursodatabase/sync-linux-arm64-musl/package.json').version const bindingPackageVersion = require('@tursodatabase/sync-linux-arm64-musl/package.json').version
if (bindingPackageVersion !== '0.2.0-pre.3' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') { if (bindingPackageVersion !== '0.2.0-pre.7' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
throw new Error(`Native binding package version mismatch, expected 0.2.0-pre.3 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`) throw new Error(`Native binding package version mismatch, expected 0.2.0-pre.7 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
} }
return binding return binding
} catch (e) { } catch (e) {
@@ -307,8 +307,8 @@ function requireNative() {
try { try {
const binding = require('@tursodatabase/sync-linux-arm64-gnu') const binding = require('@tursodatabase/sync-linux-arm64-gnu')
const bindingPackageVersion = require('@tursodatabase/sync-linux-arm64-gnu/package.json').version const bindingPackageVersion = require('@tursodatabase/sync-linux-arm64-gnu/package.json').version
if (bindingPackageVersion !== '0.2.0-pre.3' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') { if (bindingPackageVersion !== '0.2.0-pre.7' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
throw new Error(`Native binding package version mismatch, expected 0.2.0-pre.3 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`) throw new Error(`Native binding package version mismatch, expected 0.2.0-pre.7 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
} }
return binding return binding
} catch (e) { } catch (e) {
@@ -325,8 +325,8 @@ function requireNative() {
try { try {
const binding = require('@tursodatabase/sync-linux-arm-musleabihf') const binding = require('@tursodatabase/sync-linux-arm-musleabihf')
const bindingPackageVersion = require('@tursodatabase/sync-linux-arm-musleabihf/package.json').version const bindingPackageVersion = require('@tursodatabase/sync-linux-arm-musleabihf/package.json').version
if (bindingPackageVersion !== '0.2.0-pre.3' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') { if (bindingPackageVersion !== '0.2.0-pre.7' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
throw new Error(`Native binding package version mismatch, expected 0.2.0-pre.3 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`) throw new Error(`Native binding package version mismatch, expected 0.2.0-pre.7 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
} }
return binding return binding
} catch (e) { } catch (e) {
@@ -341,8 +341,8 @@ function requireNative() {
try { try {
const binding = require('@tursodatabase/sync-linux-arm-gnueabihf') const binding = require('@tursodatabase/sync-linux-arm-gnueabihf')
const bindingPackageVersion = require('@tursodatabase/sync-linux-arm-gnueabihf/package.json').version const bindingPackageVersion = require('@tursodatabase/sync-linux-arm-gnueabihf/package.json').version
if (bindingPackageVersion !== '0.2.0-pre.3' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') { if (bindingPackageVersion !== '0.2.0-pre.7' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
throw new Error(`Native binding package version mismatch, expected 0.2.0-pre.3 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`) throw new Error(`Native binding package version mismatch, expected 0.2.0-pre.7 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
} }
return binding return binding
} catch (e) { } catch (e) {
@@ -359,8 +359,8 @@ function requireNative() {
try { try {
const binding = require('@tursodatabase/sync-linux-riscv64-musl') const binding = require('@tursodatabase/sync-linux-riscv64-musl')
const bindingPackageVersion = require('@tursodatabase/sync-linux-riscv64-musl/package.json').version const bindingPackageVersion = require('@tursodatabase/sync-linux-riscv64-musl/package.json').version
if (bindingPackageVersion !== '0.2.0-pre.3' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') { if (bindingPackageVersion !== '0.2.0-pre.7' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
throw new Error(`Native binding package version mismatch, expected 0.2.0-pre.3 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`) throw new Error(`Native binding package version mismatch, expected 0.2.0-pre.7 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
} }
return binding return binding
} catch (e) { } catch (e) {
@@ -375,8 +375,8 @@ function requireNative() {
try { try {
const binding = require('@tursodatabase/sync-linux-riscv64-gnu') const binding = require('@tursodatabase/sync-linux-riscv64-gnu')
const bindingPackageVersion = require('@tursodatabase/sync-linux-riscv64-gnu/package.json').version const bindingPackageVersion = require('@tursodatabase/sync-linux-riscv64-gnu/package.json').version
if (bindingPackageVersion !== '0.2.0-pre.3' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') { if (bindingPackageVersion !== '0.2.0-pre.7' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
throw new Error(`Native binding package version mismatch, expected 0.2.0-pre.3 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`) throw new Error(`Native binding package version mismatch, expected 0.2.0-pre.7 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
} }
return binding return binding
} catch (e) { } catch (e) {
@@ -392,8 +392,8 @@ function requireNative() {
try { try {
const binding = require('@tursodatabase/sync-linux-ppc64-gnu') const binding = require('@tursodatabase/sync-linux-ppc64-gnu')
const bindingPackageVersion = require('@tursodatabase/sync-linux-ppc64-gnu/package.json').version const bindingPackageVersion = require('@tursodatabase/sync-linux-ppc64-gnu/package.json').version
if (bindingPackageVersion !== '0.2.0-pre.3' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') { if (bindingPackageVersion !== '0.2.0-pre.7' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
throw new Error(`Native binding package version mismatch, expected 0.2.0-pre.3 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`) throw new Error(`Native binding package version mismatch, expected 0.2.0-pre.7 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
} }
return binding return binding
} catch (e) { } catch (e) {
@@ -408,8 +408,8 @@ function requireNative() {
try { try {
const binding = require('@tursodatabase/sync-linux-s390x-gnu') const binding = require('@tursodatabase/sync-linux-s390x-gnu')
const bindingPackageVersion = require('@tursodatabase/sync-linux-s390x-gnu/package.json').version const bindingPackageVersion = require('@tursodatabase/sync-linux-s390x-gnu/package.json').version
if (bindingPackageVersion !== '0.2.0-pre.3' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') { if (bindingPackageVersion !== '0.2.0-pre.7' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
throw new Error(`Native binding package version mismatch, expected 0.2.0-pre.3 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`) throw new Error(`Native binding package version mismatch, expected 0.2.0-pre.7 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
} }
return binding return binding
} catch (e) { } catch (e) {
@@ -428,8 +428,8 @@ function requireNative() {
try { try {
const binding = require('@tursodatabase/sync-openharmony-arm64') const binding = require('@tursodatabase/sync-openharmony-arm64')
const bindingPackageVersion = require('@tursodatabase/sync-openharmony-arm64/package.json').version const bindingPackageVersion = require('@tursodatabase/sync-openharmony-arm64/package.json').version
if (bindingPackageVersion !== '0.2.0-pre.3' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') { if (bindingPackageVersion !== '0.2.0-pre.7' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
throw new Error(`Native binding package version mismatch, expected 0.2.0-pre.3 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`) throw new Error(`Native binding package version mismatch, expected 0.2.0-pre.7 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
} }
return binding return binding
} catch (e) { } catch (e) {
@@ -444,8 +444,8 @@ function requireNative() {
try { try {
const binding = require('@tursodatabase/sync-openharmony-x64') const binding = require('@tursodatabase/sync-openharmony-x64')
const bindingPackageVersion = require('@tursodatabase/sync-openharmony-x64/package.json').version const bindingPackageVersion = require('@tursodatabase/sync-openharmony-x64/package.json').version
if (bindingPackageVersion !== '0.2.0-pre.3' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') { if (bindingPackageVersion !== '0.2.0-pre.7' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
throw new Error(`Native binding package version mismatch, expected 0.2.0-pre.3 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`) throw new Error(`Native binding package version mismatch, expected 0.2.0-pre.7 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
} }
return binding return binding
} catch (e) { } catch (e) {
@@ -460,8 +460,8 @@ function requireNative() {
try { try {
const binding = require('@tursodatabase/sync-openharmony-arm') const binding = require('@tursodatabase/sync-openharmony-arm')
const bindingPackageVersion = require('@tursodatabase/sync-openharmony-arm/package.json').version const bindingPackageVersion = require('@tursodatabase/sync-openharmony-arm/package.json').version
if (bindingPackageVersion !== '0.2.0-pre.3' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') { if (bindingPackageVersion !== '0.2.0-pre.7' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
throw new Error(`Native binding package version mismatch, expected 0.2.0-pre.3 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`) throw new Error(`Native binding package version mismatch, expected 0.2.0-pre.7 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
} }
return binding return binding
} catch (e) { } catch (e) {

View File

@@ -1,6 +1,6 @@
import { unlinkSync } from "node:fs"; import { unlinkSync } from "node:fs";
import { expect, test } from 'vitest' import { expect, test } from 'vitest'
import { connect, DatabaseRowMutation, DatabaseRowTransformResult } from './promise.js' import { connect, Database, DatabaseRowMutation, DatabaseRowTransformResult } from './promise.js'
const localeCompare = (a, b) => a.x.localeCompare(b.x); const localeCompare = (a, b) => a.x.localeCompare(b.x);
@@ -12,6 +12,13 @@ function cleanup(path) {
try { unlinkSync(`${path}-wal-revert`) } catch (e) { } try { unlinkSync(`${path}-wal-revert`) } catch (e) { }
} }
test('explicit connect', async () => {
const db = new Database({ path: ':memory:', url: process.env.VITE_TURSO_DB_URL });
expect(() => db.prepare("SELECT 1")).toThrowError(/database must be connected/g);
await db.connect();
expect(await db.prepare("SELECT 1 as x").all()).toEqual([{ x: 1 }]);
})
test('select-after-push', async () => { test('select-after-push', async () => {
{ {
const db = await connect({ path: ':memory:', url: process.env.VITE_TURSO_DB_URL }); const db = await connect({ path: ':memory:', url: process.env.VITE_TURSO_DB_URL });

View File

@@ -1,6 +1,6 @@
import { DatabasePromise, DatabaseOpts, NativeDatabase } from "@tursodatabase/database-common" import { DatabasePromise } from "@tursodatabase/database-common"
import { ProtocolIo, run, SyncOpts, RunOpts, DatabaseRowMutation, DatabaseRowStatement, DatabaseRowTransformResult, SyncEngineStats, SyncEngineGuards } from "@tursodatabase/sync-common"; import { ProtocolIo, run, DatabaseOpts as SyncDatabaseOpts, RunOpts, DatabaseRowMutation, DatabaseRowStatement, DatabaseRowTransformResult, DatabaseStats, SyncEngineGuards } from "@tursodatabase/sync-common";
import { Database as NativeDB, SyncEngine } from "#index"; import { SyncEngine, SyncEngineProtocolVersion } from "#index";
import { promises } from "node:fs"; import { promises } from "node:fs";
let NodeIO: ProtocolIo = { let NodeIO: ProtocolIo = {
@@ -40,70 +40,95 @@ function memoryIO(): ProtocolIo {
} }
}; };
class Database extends DatabasePromise { class Database extends DatabasePromise {
runOpts: RunOpts; #runOpts: RunOpts;
engine: any; #engine: any;
io: ProtocolIo; #io: ProtocolIo;
guards: SyncEngineGuards #guards: SyncEngineGuards
constructor(db: NativeDatabase, io: ProtocolIo, runOpts: RunOpts, engine: any, opts: DatabaseOpts = {}) { constructor(opts: SyncDatabaseOpts) {
super(db, opts) const engine = new SyncEngine({
this.runOpts = runOpts; path: opts.path,
this.engine = engine; clientName: opts.clientName,
this.io = io; useTransform: opts.transform != null,
this.guards = new SyncEngineGuards(); protocolVersion: SyncEngineProtocolVersion.V1,
longPollTimeoutMs: opts.longPollTimeoutMs,
tracing: opts.tracing,
});
super(engine.db() as unknown as any);
let headers = typeof opts.authToken === "function" ? () => ({
...(opts.authToken != null && { "Authorization": `Bearer ${(opts.authToken as any)()}` }),
...(opts.encryptionKey != null && { "x-turso-encryption-key": opts.encryptionKey })
}) : {
...(opts.authToken != null && { "Authorization": `Bearer ${opts.authToken}` }),
...(opts.encryptionKey != null && { "x-turso-encryption-key": opts.encryptionKey })
};
this.#runOpts = {
url: opts.url,
headers: headers,
preemptionMs: 1,
transform: opts.transform,
};
this.#engine = engine;
this.#io = this.memory ? memoryIO() : NodeIO;
this.#guards = new SyncEngineGuards();
} }
async sync() { /**
await this.push(); * connect database and initialize it in case of clean start
await this.pull(); */
override async connect() {
await run(this.#runOpts, this.#io, this.#engine, this.#engine.connect());
} }
/**
* pull new changes from the remote database
* if {@link SyncDatabaseOpts.longPollTimeoutMs} is set - then server will hold the connection open until either new changes will appear in the database or timeout occurs.
* @returns true if new changes were pulled from the remote
*/
async pull() { async pull() {
const changes = await this.guards.wait(async () => await run(this.runOpts, this.io, this.engine, this.engine.wait())); const changes = await this.#guards.wait(async () => await run(this.#runOpts, this.#io, this.#engine, this.#engine.wait()));
await this.guards.apply(async () => await run(this.runOpts, this.io, this.engine, this.engine.apply(changes))); if (changes.empty()) {
return false;
}
await this.#guards.apply(async () => await run(this.#runOpts, this.#io, this.#engine, this.#engine.apply(changes)));
return true;
} }
/**
* push new local changes to the remote database
* if {@link SyncDatabaseOpts.transform} is set - then provided callback will be called for every mutation before sending it to the remote
*/
async push() { async push() {
await this.guards.push(async () => await run(this.runOpts, this.io, this.engine, this.engine.push())); await this.#guards.push(async () => await run(this.#runOpts, this.#io, this.#engine, this.#engine.push()));
} }
/**
* checkpoint WAL for local database
*/
async checkpoint() { async checkpoint() {
await this.guards.checkpoint(async () => await run(this.runOpts, this.io, this.engine, this.engine.checkpoint())); await this.#guards.checkpoint(async () => await run(this.#runOpts, this.#io, this.#engine, this.#engine.checkpoint()));
} }
async stats(): Promise<SyncEngineStats> { /**
return (await run(this.runOpts, this.io, this.engine, this.engine.stats())); * @returns statistic of current local database
*/
async stats(): Promise<DatabaseStats> {
return (await run(this.#runOpts, this.#io, this.#engine, this.#engine.stats()));
} }
/**
* close the database
*/
override async close(): Promise<void> { override async close(): Promise<void> {
this.engine.close(); this.#engine.close();
} }
} }
/** /**
* Creates a new database connection asynchronously. * Creates a new database connection asynchronously.
* *
* @param {string} path - Path to the database file.
* @param {Object} opts - Options for database behavior. * @param {Object} opts - Options for database behavior.
* @returns {Promise<Database>} - A promise that resolves to a Database instance. * @returns {Promise<Database>} - A promise that resolves to a Database instance.
*/ */
async function connect(opts: SyncOpts): Promise<Database> { async function connect(opts: SyncDatabaseOpts): Promise<Database> {
const engine = new SyncEngine({ const db = new Database(opts);
path: opts.path, await db.connect();
clientName: opts.clientName, return db;
tablesIgnore: opts.tablesIgnore,
useTransform: opts.transform != null,
tracing: opts.tracing,
protocolVersion: 1,
longPollTimeoutMs: opts.longPollTimeoutMs,
});
const runOpts: RunOpts = {
url: opts.url,
headers: {
...(opts.authToken != null && { "Authorization": `Bearer ${opts.authToken}` }),
...(opts.encryptionKey != null && { "x-turso-encryption-key": opts.encryptionKey })
},
preemptionMs: 1,
transform: opts.transform,
};
let io = opts.path == ':memory:' ? memoryIO() : NodeIO;
await run(runOpts, io, engine, engine.init());
const nativeDb = engine.open();
return new Database(nativeDb as any, io, runOpts, engine, {});
} }
export { connect, Database, DatabaseRowMutation, DatabaseRowStatement, DatabaseRowTransformResult } export { connect, Database, DatabaseRowMutation, DatabaseRowStatement, DatabaseRowTransformResult }