Socket
Socket
Sign inDemoInstall

gosh

Package Overview
Dependencies
Maintainers
1
Versions
12
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

gosh - npm Package Compare versions

Comparing version 3.1.0 to 4.0.0

lib/document_store.spec.js

25

CHANGELOG.md

@@ -10,6 +10,29 @@ # CHANGE LOG

----
## [In Git - targetting v3.1.0](https://github.com/mattwynne/gosh/compare/v3.0.0...master) (Not released)
## [In Git - targetting v4.0.0](https://github.com/mattwynne/gosh/compare/v3.1.0...master) (Not released)
### Added
* DocumentStore#withIndexOfAll for indexing many-many relationships
### Changed
* `DocumentStore#withIndex` now works instead of the old `withUniqueIndex`, `withOptionalUniqueIndex` and `withOneToManyIndex`
### Deprecated
* N/A
### Removed
* `DocumentStore#withUniqueIndex`, `DocumentStore#withOptionalUniqueIndex` and `DocumentStore#withOneToManyIndex`
### Fixed
* N/A
## [v3.1.0](https://github.com/mattwynne/gosh/compare/v3.0.0...v3.1.0) (Not released)
### Added
* DocumentStore#forQueries returns a read-only interface onto the store

@@ -16,0 +39,0 @@

70

lib/document_store.js
'use strict'
const MemoryUniqueIndex = require('./index/memory/unique_index')
const MemoryOptionalUniqueIndex = require('./index/memory/optional_unique_index')
const MemoryOneToManyIndex = require('./index/memory/one_to_many_index')
const MemoryIndex = require('./memory_index')

@@ -10,23 +8,25 @@ module.exports = class DocumentStore {

makeId,
{ indices, valueById } = {
{ indices, documents } = {
indices: null,
valueById: null,
documents: null,
}
) {
if (typeof makeId !== 'function') throw new Error('makeId is required')
this._indices = indices || [
new MemoryUniqueIndex({ makeId, makeKey: makeId }),
]
if (typeof makeId !== 'function')
throw new Error(
'makeId is required. Pass a function that will make a unique identifier for any document to be stored.'
)
this._makeId = makeId
this._valueById = valueById || new Map()
this._indices = indices || [new MemoryIndex(doc => [makeId(doc)])]
this._documents = documents || new Map()
}
values() {
return Array.from(this._valueById.values())
return Array.from(this._documents.values())
}
put(document) {
this._valueById.set(this._makeId(document), document)
const id = this._makeId(document)
this._documents.set(id, document)
for (const index of this._indices) {
index.put(document)
index.put({ document, id })
}

@@ -41,3 +41,3 @@ return this

}
this._valueById.delete(id)
this._documents.delete(id)
}

@@ -48,3 +48,3 @@ return this

all(query) {
return this._allIds(query).map(id => this._valueById.get(id))
return this._allIds(query).map(id => this._documents.get(id))
}

@@ -65,37 +65,23 @@

all: this.all.bind(this),
values: this.values.bind(this)
values: this.values.bind(this),
}
}
withUniqueIndex(makeKey) {
const makeId = this._makeId
const valueById = this._valueById
const index = new MemoryUniqueIndex({ makeId, makeKey }) // TODO: take a factory in the constructor
for (const document of valueById.values()) {
index.put(document)
}
const indices = this._indices.concat([index])
return new DocumentStore(makeId, { indices, valueById })
withIndex(makeKey) {
return this._withExtraIndex(new MemoryIndex(doc => [makeKey(doc)]))
}
withOptionalUniqueIndex(makeKey) {
const makeId = this._makeId
const valueById = this._valueById
const index = new MemoryOptionalUniqueIndex({ makeId, makeKey }) // TODO: take a factory in the constructor
for (const document of valueById.values()) {
index.put(document)
}
const indices = this._indices.concat([index])
return new DocumentStore(makeId, { indices, valueById })
withIndexOfAll(makeKeys) {
return this._withExtraIndex(new MemoryIndex(makeKeys))
}
withOneToManyIndex(makeKey) {
const makeId = this._makeId
const valueById = this._valueById
const index = new MemoryOneToManyIndex({ makeId, makeKey })
for (const document of valueById.values()) {
index.put(document)
_withExtraIndex(index) {
for (const document of this._documents.values()) {
const id = this._makeId(document)
index.put({ document, id })
}
const indices = this._indices.concat([index])
return new DocumentStore(makeId, { indices, valueById })
return new DocumentStore(this._makeId, {
indices: this._indices.concat(index),
documents: this._documents,
})
}

@@ -102,0 +88,0 @@

{
"name": "gosh",
"version": "3.1.0",
"version": "4.0.0",
"description": "Great Object Storage Hooray!",

@@ -5,0 +5,0 @@ "main": "lib/gosh.js",

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc