database.applyChangeset(changeset[, options])


  • changeset <Uint8Array> 二进制变更集或补丁集。

    ¥changeset <Uint8Array> A binary changeset or patchset.

  • options <Object> 如何应用更改的配置选项。

    ¥options <Object> The configuration options for how the changes will be applied.

    • filter <Function> 跳过更改,当向此函数提供目标表名时,返回真值。默认情况下,将尝试所有更改。

      ¥filter <Function> Skip changes that, when targeted table name is supplied to this function, return a truthy value. By default, all changes are attempted.

    • onConflict <Function> 确定如何处理冲突的函数。该函数接收一个参数,可以是以下值之一:

      ¥onConflict <Function> A function that determines how to handle conflicts. The function receives one argument, which can be one of the following values:

      • SQLITE_CHANGESET_DATADELETEUPDATE 更改不包含预期的 "before" 值。

        ¥SQLITE_CHANGESET_DATA: A DELETE or UPDATE change does not contain the expected "before" values.

      • SQLITE_CHANGESET_NOTFOUND:与 DELETEUPDATE 更改的主键匹配的行不存在。

        ¥SQLITE_CHANGESET_NOTFOUND: A row matching the primary key of the DELETE or UPDATE change does not exist.

      • SQLITE_CHANGESET_CONFLICTINSERT 更改导致主键重复。

        ¥SQLITE_CHANGESET_CONFLICT: An INSERT change results in a duplicate primary key.

      • SQLITE_CHANGESET_FOREIGN_KEY:应用更改会导致违反外键。

        ¥SQLITE_CHANGESET_FOREIGN_KEY: Applying a change would result in a foreign key violation.

      • SQLITE_CHANGESET_CONSTRAINT:应用更改会导致违反 UNIQUECHECKNOT NULL 约束。

        ¥SQLITE_CHANGESET_CONSTRAINT: Applying a change results in a UNIQUE, CHECK, or NOT NULL constraint violation.

      该函数应返回以下值之一:

      ¥The function should return one of the following values:

      • SQLITE_CHANGESET_OMIT:忽略冲突的更改。

        ¥SQLITE_CHANGESET_OMIT: Omit conflicting changes.

      • SQLITE_CHANGESET_REPLACE:用冲突的更改替换现有值(仅对 SQLITE_CHANGESET_DATASQLITE_CHANGESET_CONFLICT 冲突有效)。

        ¥SQLITE_CHANGESET_REPLACE: Replace existing values with conflicting changes (only valid with SQLITE_CHANGESET_DATA or SQLITE_CHANGESET_CONFLICT conflicts).

      • SQLITE_CHANGESET_ABORT:发生冲突时中止并回滚数据库。

        ¥SQLITE_CHANGESET_ABORT: Abort on conflict and roll back the database.

      当冲突处理程序中抛出错误或从处理程序返回任何其他值时,将中止应用变更集并回滚数据库。

      ¥When an error is thrown in the conflict handler or when any other value is returned from the handler, applying the changeset is aborted and the database is rolled back.

      默认值:返回 SQLITE_CHANGESET_ABORT 的函数。

      ¥Default: A function that returns SQLITE_CHANGESET_ABORT.

  • 返回:<boolean> 变更集是否成功应用而未中止。

    ¥Returns: <boolean> Whether the changeset was applied successfully without being aborted.

如果数据库未打开,则抛出异常。此方法是 sqlite3changeset_apply() 的封装器。

¥An exception is thrown if the database is not open. This method is a wrapper around sqlite3changeset_apply().

const sourceDb = new DatabaseSync(':memory:');
const targetDb = new DatabaseSync(':memory:');

sourceDb.exec('CREATE TABLE data(key INTEGER PRIMARY KEY, value TEXT)');
targetDb.exec('CREATE TABLE data(key INTEGER PRIMARY KEY, value TEXT)');

const session = sourceDb.createSession();

const insert = sourceDb.prepare('INSERT INTO data (key, value) VALUES (?, ?)');
insert.run(1, 'hello');
insert.run(2, 'world');

const changeset = session.changeset();
targetDb.applyChangeset(changeset);
// Now that the changeset has been applied, targetDb contains the same data as sourceDb.