Skip to content

bulkUpdate does not prohibit changing primary keys when they are not at the root #2218

@tonyhallett

Description

@tonyhallett

for (const keyPath of Object.keys(changes)) {

   bulkUpdate(
    keysAndChanges: readonly { key: any; changes: UpdateSpec<any> }[]
  ): PromiseExtended<number> {
    const coreTable = this.core;
    const keys = keysAndChanges.map((entry) => entry.key);
    const changeSpecs = keysAndChanges.map((entry) => entry.changes);
    const offsetMap: number[] = [];
    return this._trans('readwrite', (trans) => {
      return coreTable.getMany({ trans, keys, cache: 'clone' }).then((objs) => {
        const resultKeys: any[] = [];
        const resultObjs: any[] = [];
        keysAndChanges.forEach(({ key, changes }, idx) => {
          const obj = objs[idx];
          if (obj) {
            for (const keyPath of Object.keys(changes)) {
              const value = changes[keyPath];
              if (keyPath === this.schema.primKey.keyPath) {
                if (cmp(value, key) !== 0) {
                  throw new exceptions.Constraint(
                    `Cannot update primary key in bulkUpdate()`
                  );
                }
              } else {
                setByKeyPath(obj, keyPath, value);
              }
            }

This is an edge case. Regardless it does permit keeping the original and creating a new record.

You could clone the db object, apply all changes then check if the primary key has changed

Similarly to

} else if (!outbound && cmp(extractKey(origValue), extractKey(ctx.value)) !== 0) {

 if (!outbound && cmp(extractKey(origValue), extractKey(ctx.value)) !== 0) {
                  // Changed primary key of inbound

then throw.

Or allow bulkUpdate to change keys like Table.update / Collection.modify

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions