Merge pull request #23 from seald/test-serialization-hooks

add testSerializationHooks option
pull/27/head
tex0l 2 years ago committed by GitHub
commit cc152be1c7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 4
      CHANGELOG.md
  2. 1
      index.d.ts
  3. 5
      lib/datastore.js
  4. 13
      lib/persistence.js
  5. 4
      package-lock.json
  6. 2
      package.json
  7. 14
      test/persistence.async.test.js

@ -6,6 +6,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres
to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
## [3.1.0] - 2022-09-02
### Added
- Added a `testSerializationHooks` option which defaults to `true`. Setting to `false` allows to skip the test of the hooks, which may be slow.
## [3.0.0] - 2022-03-16
### Added
- Added a `Promise`-based interface.

1
index.d.ts vendored

@ -114,6 +114,7 @@ declare namespace Nedb {
corruptAlertThreshold?: number;
compareStrings?(a: string, b: string): number;
modes?: {fileMode: number, dirMode: number};
testSerializationHooks?: boolean;
}
interface UpdateOptions {

@ -193,6 +193,8 @@ class Datastore extends EventEmitter {
* @param {compareStrings} [options.compareStrings] If specified, it overrides default string comparison which is not
* well adapted to non-US characters in particular accented letters. Native `localCompare` will most of the time be
* the right choice.
* @param {boolean} [options.testSerializationHooks=true] Whether to test the serialization hooks or not,
* might be CPU-intensive
*/
constructor (options) {
super()
@ -262,7 +264,8 @@ class Datastore extends EventEmitter {
afterSerialization: options.afterSerialization,
beforeDeserialization: options.beforeDeserialization,
corruptAlertThreshold: options.corruptAlertThreshold,
modes: options.modes
modes: options.modes,
testSerializationHooks: options.testSerializationHooks
})
// This new executor is ready if we don't use persistence

@ -49,6 +49,7 @@ class Persistence {
* @param {object} [options.modes] Modes to use for FS permissions. Will not work on Windows.
* @param {number} [options.modes.fileMode=0o644] Mode to use for files.
* @param {number} [options.modes.dirMode=0o755] Mode to use for directories.
* @param {boolean} [options.testSerializationHooks=true] Whether to test the serialization hooks or not, might be CPU-intensive
*/
constructor (options) {
this.db = options.db
@ -77,11 +78,13 @@ class Persistence {
this.afterSerialization = options.afterSerialization || (s => s)
this.beforeDeserialization = options.beforeDeserialization || (s => s)
for (let i = 1; i < 30; i += 1) {
for (let j = 0; j < 10; j += 1) {
const randomString = customUtils.uid(i)
if (this.beforeDeserialization(this.afterSerialization(randomString)) !== randomString) {
throw new Error('beforeDeserialization is not the reverse of afterSerialization, cautiously refusing to start NeDB to prevent dataloss')
if (options.testSerializationHooks === undefined || options.testSerializationHooks) {
for (let i = 1; i < 30; i += 1) {
for (let j = 0; j < 10; j += 1) {
const randomString = customUtils.uid(i)
if (this.beforeDeserialization(this.afterSerialization(randomString)) !== randomString) {
throw new Error('beforeDeserialization is not the reverse of afterSerialization, cautiously refusing to start NeDB to prevent dataloss')
}
}
}
}

4
package-lock.json generated

@ -1,12 +1,12 @@
{
"name": "@seald-io/nedb",
"version": "3.0.0",
"version": "3.0.1-0",
"lockfileVersion": 2,
"requires": true,
"packages": {
"": {
"name": "@seald-io/nedb",
"version": "3.0.0",
"version": "3.0.1-0",
"license": "MIT",
"dependencies": {
"@seald-io/binary-search-tree": "^1.0.2",

@ -1,6 +1,6 @@
{
"name": "@seald-io/nedb",
"version": "3.0.0",
"version": "3.0.1-0",
"files": [
"lib/**/*.js",
"browser-version/**/*.js",

@ -461,6 +461,20 @@ describe('Persistence async', function () {
assert.equal(await fs.readFile(hookTestFilename, 'utf8'), 'Some content')
})
it('Declaring two hooks that are not reverse of one another will not cause exception if options.testSerializationHooks === false', async () => {
const hookTestFilename = 'workspace/hookTest.db'
await storage.ensureFileDoesntExistAsync(hookTestFilename)
await fs.writeFile(hookTestFilename, 'Some content', 'utf8')
const db = new Datastore({
filename: hookTestFilename,
autoload: true,
afterSerialization: as,
beforeDeserialization: function (s) { return s },
testSerializationHooks: false
})
await assert.rejects(() => db.autoloadPromise)
})
it('A serialization hook can be used to transform data before writing new state to disk', async () => {
const hookTestFilename = 'workspace/hookTest.db'
await storage.ensureFileDoesntExistAsync(hookTestFilename)

Loading…
Cancel
Save