Comparing version 4.2.0 to 4.3.0
@@ -10,3 +10,3 @@ # CHANGE LOG | ||
---- | ||
## [In Git](https://github.com/mattwynne/gosh/compare/v4.2.0...master) (Not released) | ||
## [In Git](https://github.com/mattwynne/gosh/compare/v4.3.0...master) (Not released) | ||
@@ -33,2 +33,24 @@ ### Added | ||
## [In Git](https://github.com/mattwynne/gosh/compare/v4.2.0...v4.3.0) (Not released) | ||
### Added | ||
* N/A | ||
### Changed | ||
* Emit insert / update / delete events | ||
### Deprecated | ||
* N/A | ||
### Removed | ||
* N/A | ||
### Fixed | ||
* N/A | ||
## [In Git](https://github.com/mattwynne/gosh/compare/v4.1.0...v4.2.0) (Not released) | ||
@@ -35,0 +57,0 @@ |
'use strict' | ||
const MemoryIndex = require('./memory_index') | ||
const EventEmitter = require('events') | ||
@@ -8,5 +9,6 @@ module.exports = class DocumentStore { | ||
makeId, | ||
{ indices, documents } = { | ||
{ indices, documents, events } = { | ||
indices: null, | ||
documents: null, | ||
events: null | ||
} | ||
@@ -21,2 +23,3 @@ ) { | ||
this._documents = documents || new Map() | ||
this._events = events || new EventEmitter() | ||
} | ||
@@ -30,2 +33,3 @@ | ||
const id = this._makeId(document) | ||
this._emitInsertOrUpdate(document, id) | ||
this._documents.set(id, document) | ||
@@ -40,2 +44,3 @@ for (const index of this._indices) { | ||
for (const id of this._allIds(query)) { | ||
this._emitDelete(id) | ||
for (const index of this._indices) { | ||
@@ -93,2 +98,14 @@ index.deleteId(id) | ||
_emitInsertOrUpdate(document, id) { | ||
if (!this._documents.has(id)) | ||
return this._events.emit('insert', document) | ||
const originalDocument = this._documents.get(id) | ||
this._events.emit('update', [originalDocument, document]) | ||
} | ||
_emitDelete(id) { | ||
const doc = this._documents.get(id) | ||
this._events.emit('delete', doc) | ||
} | ||
_withExtraIndex(index) { | ||
@@ -95,0 +112,0 @@ for (const document of this._documents.values()) { |
@@ -5,2 +5,3 @@ 'use strict' | ||
const { DocumentStore } = require('./gosh') | ||
const EventEmitter = require('events') | ||
@@ -65,2 +66,31 @@ describe('Gosh', () => { | ||
}) | ||
context('emitting events', () => { | ||
const called = {} | ||
const events = new EventEmitter() | ||
events.on('insert', (doc) => called.insert = doc) | ||
events.on('update', (doc) => called.update = doc) | ||
events.on('delete', (doc) => called.delete = doc) | ||
const store = new DocumentStore(document => document.name, { events } ) | ||
const dave = { name: 'dave' } | ||
const updatedDave ={ name: 'dave', shoes: 'brown' } | ||
it('calls an insert event', () => { | ||
store.put(dave) | ||
assert.deepEqual(called, { insert: dave }) | ||
}) | ||
it('calls an update event', () => { | ||
store.put(dave) | ||
store.put(updatedDave) | ||
assert.deepEqual(called, { insert: dave, update: [dave, updatedDave] }) | ||
}) | ||
it('calls a delete event', () => { | ||
store.put(dave) | ||
store.put(updatedDave) | ||
store.delete(dave) | ||
assert.deepEqual(called, { insert: dave, update: [dave, updatedDave], delete: updatedDave }) | ||
}) | ||
}) | ||
}) |
{ | ||
"name": "gosh", | ||
"version": "4.2.0", | ||
"version": "4.3.0", | ||
"description": "Great Object Storage Hooray!", | ||
@@ -5,0 +5,0 @@ "main": "lib/gosh.js", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
32873591
570