Skip to content

Commit

Permalink
Add hooks
Browse files Browse the repository at this point in the history
Adds postopen, prewrite and newsub hooks that allow userland "hook
functions" to customize behavior of the database. See README for
details. A quick example:

```js
db.hooks.prewrite.add(function (op, batch) {
  if (op.type === 'put') {
    batch.add({
      type: 'put',
      key: op.value.foo,
      value: op.key,
      sublevel: fooIndex
    })
  }
})
```

More generally, this is a move towards "renewed modularity". Our
ecosystem is old and many modules no longer work because they had
no choice but to monkeypatch database methods, of which the
signature has changed since then.

So in addition to hooks, this:

- Introduces a new `write` event that is emitted on `db.batch()`,
  `db.put()` and `db.del()` and has richer data: userland options,
  encoded data, keyEncoding and valueEncoding. The `batch`, `put`
  and `del` events are now deprecated and will be removed in a
  future version. Related to Level/level#222.
- Restores support of userland options on batch operations. In
  particular, to copy options in `db.batch(ops, options)` to ops,
  allowing for code like `db.batch(ops, { ttl: 123 })` to apply a
  default userland `ttl` option to all ops.

No breaking changes, yet. Using hooks means opting-in to new
behaviors (like the new write event) and disables some old behaviors
(like the deprecated events). Later on we can make those the default
behavior, regardless of whether hooks are used.

TODO: benchmarks, tests and optionally some light refactoring.

Closes Level/community#44.
  • Loading branch information
vweevers committed Oct 30, 2022
1 parent 18409b7 commit 80e98c0
Show file tree
Hide file tree
Showing 18 changed files with 1,366 additions and 159 deletions.
356 changes: 332 additions & 24 deletions README.md

Large diffs are not rendered by default.

327 changes: 276 additions & 51 deletions abstract-chained-batch.js

Large diffs are not rendered by default.

15 changes: 2 additions & 13 deletions abstract-iterator.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const { fromCallback } = require('catering')
const ModuleError = require('module-error')
const { getOptions, getCallback } = require('./lib/common')
const { getOptions, getCallback, emptyOptions, noop, deprecate } = require('./lib/common')

const kPromise = Symbol('promise')
const kCallback = Symbol('callback')
Expand All @@ -25,10 +25,6 @@ const kValues = Symbol('values')
const kLimit = Symbol('limit')
const kCount = Symbol('count')

const emptyOptions = Object.freeze({})
const noop = () => {}
let warnedEnd = false

// This class is an internal utility for common functionality between AbstractIterator,
// AbstractKeyIterator and AbstractValueIterator. It's not exported.
class CommonIterator {
Expand Down Expand Up @@ -377,14 +373,7 @@ class AbstractIterator extends CommonIterator {
}

end (callback) {
if (!warnedEnd && typeof console !== 'undefined') {
warnedEnd = true
console.warn(new ModuleError(
'The iterator.end() method was renamed to close() and end() is an alias that will be removed in a future version',
{ code: 'LEVEL_LEGACY' }
))
}

deprecate('The iterator.end() method was renamed to close() and end() is an alias that will be removed in a future version')
return this.close(callback)
}
}
Expand Down
Loading

0 comments on commit 80e98c0

Please sign in to comment.