1 line
37 KiB
JSON
1 line
37 KiB
JSON
|
{"ast":null,"code":"import Database from \"libsql\";\nimport { Buffer } from \"node:buffer\";\nimport { LibsqlError } from \"@libsql/core/api\";\nimport { expandConfig, isInMemoryConfig } from \"@libsql/core/config\";\nimport { supportedUrlLink, transactionModeToBegin, ResultSetImpl } from \"@libsql/core/util\";\nexport * from \"@libsql/core/api\";\nexport function createClient(config) {\n return _createClient(expandConfig(config, true));\n}\n/** @private */\nexport function _createClient(config) {\n if (config.scheme !== \"file\") {\n throw new LibsqlError(`URL scheme ${JSON.stringify(config.scheme + \":\")} is not supported by the local sqlite3 client. ` + `For more information, please read ${supportedUrlLink}`, \"URL_SCHEME_NOT_SUPPORTED\");\n }\n const authority = config.authority;\n if (authority !== undefined) {\n const host = authority.host.toLowerCase();\n if (host !== \"\" && host !== \"localhost\") {\n throw new LibsqlError(`Invalid host in file URL: ${JSON.stringify(authority.host)}. ` + 'A \"file:\" URL with an absolute path should start with one slash (\"file:/absolute/path.db\") ' + 'or with three slashes (\"file:///absolute/path.db\"). ' + `For more information, please read ${supportedUrlLink}`, \"URL_INVALID\");\n }\n if (authority.port !== undefined) {\n throw new LibsqlError(\"File URL cannot have a port\", \"URL_INVALID\");\n }\n if (authority.userinfo !== undefined) {\n throw new LibsqlError(\"File URL cannot have username and password\", \"URL_INVALID\");\n }\n }\n let isInMemory = isInMemoryConfig(config);\n if (isInMemory && config.syncUrl) {\n throw new LibsqlError(`Embedded replica must use file for local db but URI with in-memory mode were provided instead: ${config.path}`, \"URL_INVALID\");\n }\n let path = config.path;\n if (isInMemory) {\n // note: we should prepend file scheme in order for SQLite3 to recognize :memory: connection query parameters\n path = `${config.scheme}:${config.path}`;\n }\n const options = {\n authToken: config.authToken,\n encryptionKey: config.encryptionKey,\n syncUrl: config.syncUrl,\n syncPeriod: config.syncInterval\n };\n const db = new Database(path, options);\n executeStmt(db, \"SELECT 1 AS checkThatTheDatabaseCanBeOpened\", config.intMode);\n return new Sqlite3Client(path, options, db, config.intMode);\n}\nexport class Sqlite3Client {\n #path;\n #options;\n #db;\n #intMode;\n closed;\n protocol;\n /** @private */\n constructor(path, options, db, intMode) {\n this.#path = path;\n this.#options = options;\n this.#db = db;\n this.#intMode = intMode;\n this.closed = false;\n this.protocol = \"file\";\n }\n async execute(stmtOrSql, args) {\n let stmt;\n if (typeof stmtOrSql === \"string\") {\n stmt = {\n sql: stmtOrSql,\n args: args || []\n };\n } else {\n stmt = stmtOrSql;\n }\n this.#checkNotClosed();\n return executeStmt(this.#getDb(), stmt, this.#intMode);\n }\n async batch(stmts, mode = \"deferred\") {\n this.#checkNotClosed();\n const db = this.#getDb();\n try {\n executeStmt(db, transactionModeToBegin(mode), this.#intMode);\n const resultSets = stmts.map(stmt => {\n if (!db.inTransaction) {\n throw new LibsqlError(\"The transaction has been rolled back\", \"TRANSACTION_CLOSED\");\n }\n return executeStmt(db, stmt, this.#intMode);\n });\n executeStmt(db, \"COMMIT\", this.#intMode);\n return resultSets;\n } finally {\n if (db.inTransaction) {\n executeStmt(db, \"ROLLBACK\", this.#intMode);\n }\n }\n }\n async migrate(stmts) {\n this.#checkNotClosed();\n const db = this.#getDb();\n try {\n executeStmt(db, \"PRAGMA foreign_keys=off\", this.#intMode);\n executeStmt(db, transactionModeToBegin(\"deferred\"), this.#intMode);\n const resultSets = stmts.map(stmt => {\n if (!db.inTransaction) {\n throw new LibsqlError(\"The transaction has been rolled back\", \"TRANSACTION_CLOSED\");\n
|