GoodDB
    Preparing search index...

    Function findAndUpdate

    • Find a value in an array and update it

      Parameters

      • this: GoodDB
      • key: string

        The key of the array to search

      • findCallback: (value: any, index: number, obj: any[]) => unknown

        The callback function to find the element

      • updateCallback: (value: any, index: number, obj: any[]) => any

        The callback function to update the found element

      • Optionaloptions: methodOptions

        The options to use

      Returns any

      A promise if the driver is async, otherwise the updated element or undefined if not found

      const db = new GoodDB(new JSONDriver({
      path: './database.json'
      }));

      db.findAndUpdate('users',
      (user) => user.id === 1,
      (user) => { user.name = 'Updated Name'; return user; }
      );
      const db = new GoodDB(new MongoDBDriver({
      uri: "..."
      }));

      await db.connect();

      await db.findAndUpdate('users',
      (user) => user.id === 1,
      (user) => { user.name = 'Updated Name'; return user; }
      );