1 line
32 KiB
JSON
1 line
32 KiB
JSON
|
{"ast":null,"code":"\"use strict\";\n\nconst {\n load,\n currentTarget\n} = require(\"@neon-rs/load\");\nconst {\n familySync,\n GLIBC\n} = require(\"detect-libc\");\nfunction requireNative() {\n if (process.env.LIBSQL_JS_DEV) {\n return load(__dirname);\n }\n let target = currentTarget();\n // Workaround for Bun, which reports a musl target, but really wants glibc...\n if (familySync() == GLIBC) {\n switch (target) {\n case \"linux-x64-musl\":\n target = \"linux-x64-gnu\";\n break;\n case \"linux-arm64-musl\":\n target = \"linux-arm64-gnu\";\n break;\n }\n }\n return require(`@libsql/${target}`);\n}\nconst {\n databaseOpen,\n databaseOpenWithRpcSync,\n databaseInTransaction,\n databaseClose,\n databaseSyncSync,\n databaseSyncUntilSync,\n databaseExecSync,\n databasePrepareSync,\n databaseDefaultSafeIntegers,\n databaseLoadExtension,\n databaseMaxWriteReplicationIndex,\n statementRaw,\n statementIsReader,\n statementGet,\n statementRun,\n statementRowsSync,\n statementColumns,\n statementSafeIntegers,\n rowsNext\n} = requireNative();\nconst SqliteError = require(\"./sqlite-error\");\nfunction convertError(err) {\n if (err.libsqlError) {\n return new SqliteError(err.message, err.code, err.rawCode);\n }\n return err;\n}\n\n/**\n * Database represents a connection that can prepare and execute SQL statements.\n */\nclass Database {\n /**\n * Creates a new database connection. If the database file pointed to by `path` does not exists, it will be created.\n *\n * @constructor\n * @param {string} path - Path to the database file.\n */\n constructor(path, opts) {\n const encryptionCipher = opts?.encryptionCipher ?? \"aes256cbc\";\n if (opts && opts.syncUrl) {\n var authToken = \"\";\n if (opts.syncAuth) {\n console.warn(\"Warning: The `syncAuth` option is deprecated, please use `authToken` option instead.\");\n authToken = opts.syncAuth;\n } else if (opts.authToken) {\n authToken = opts.authToken;\n }\n const encryptionKey = opts?.encryptionKey ?? \"\";\n const syncPeriod = opts?.syncPeriod ?? 0.0;\n const readYourWrites = opts?.readYourWrites ?? true;\n this.db = databaseOpenWithRpcSync(path, opts.syncUrl, authToken, encryptionCipher, encryptionKey, syncPeriod, readYourWrites);\n } else {\n const authToken = opts?.authToken ?? \"\";\n const encryptionKey = opts?.encryptionKey ?? \"\";\n this.db = databaseOpen(path, authToken, encryptionCipher, encryptionKey);\n }\n // TODO: Use a libSQL API for this?\n this.memory = path === \":memory:\";\n this.readonly = false;\n this.name = \"\";\n this.open = true;\n const db = this.db;\n Object.defineProperties(this, {\n inTransaction: {\n get() {\n return databaseInTransaction(db);\n }\n }\n });\n }\n sync() {\n return databaseSyncSync.call(this.db);\n }\n syncUntil(replicationIndex) {\n return databaseSyncUntilSync.call(this.db, replicationIndex);\n }\n\n /**\n * Prepares a SQL statement for execution.\n *\n * @param {string} sql - The SQL statement string to prepare.\n */\n prepare(sql) {\n try {\n const stmt = databasePrepareSync.call(this.db, sql);\n return new Statement(stmt);\n } catch (err) {\n throw convertError(err);\n }\n }\n\n /**\n * Returns a function that executes the given function in a transaction.\n *\n * @param {function} fn - The function to wrap in a transaction.\n */\n transaction(fn) {\n if (typeof fn !== \"function\") throw new TypeError(\"Expected first argument to be a function\");\n const db = this;\n const wrapTxn = mode => {\n return (...bindParameters) => {\n db.exec(\"BEGIN \" + mode);\n try {\n const result = fn(...bindParameters);\n db.exec(\"COMMIT\");\n return result;\n } catch (err) {\n db.exec(\"ROLLBACK\");\n throw err;\n }\n };\n };\n const properties = {\n d
|