.extend()

Extends the base Database class by adding/overwriting methods.

extend(extensions)

Parameter

Description

extensions

Object

An object with the methods

Returns

Description

A modified version of the base Database class with the new methods

You can define methods using normal and/or arrow functions, but in order to access the base class methods with the "this" keyword, you must use a normal function.

const extendedDb = db.extend({
  // Add new methods
  chunk(key, count) {
    const array = this.get(key);

    const newArr = [];

    for (var i = 0; i < array.length; i+=count)
      newArr[i/count] = array.slice(i, i+count);
      
    return newArr;
  },

  // Overwrite existing methods
  delete: (key) => {
    // Whatever you wanna do here.
  }
})

extendedDb.set('someArray', [1,2,3,4,5]); // [1,2,3,4,5]

extendedDb.chunk('someArray', 2); // [[1,2], [3,4], [5]]

Last updated