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 4.3.0 to 5.2.0

2

CHANGELOG.md

@@ -18,3 +18,3 @@ # CHANGE LOG

* N/A
* API for building stores has been simplified.

@@ -21,0 +21,0 @@ ### Deprecated

@@ -6,42 +6,58 @@ 'use strict'

module.exports = class DocumentStore {
constructor(
makeId,
{ indices, documents, events } = {
indices: null,
documents: null,
events: null
}
) {
if (typeof makeId !== 'function')
const parseIndexDefinition = indexDefinition => {
switch (indexDefinition.constructor.name) {
case 'Function':
return { makeKey: indexDefinition }
case 'String':
return { makeKey: document => document[indexDefinition] }
case 'Array':
return { makeKeys: indexDefinition[0] }
default:
throw new Error(
'makeId is required. Pass a function that will make a unique identifier for any document to be stored.'
`Unable to parse index definition: ${indexDefinition.constructor.name}`
)
this._makeId = makeId
this._indices = indices || [new MemoryIndex(doc => [makeId(doc)])]
this._documents = documents || new Map()
this._events = events || new EventEmitter()
}
}
values() {
return Array.from(this._documents.values())
}
const containsOptions = args =>
args.length > 0 && args[args.length - 1].constructor.name === 'Object'
put(document) {
const id = this._makeId(document)
this._emitInsertOrUpdate(document, id)
this._documents.set(id, document)
for (const index of this._indices) {
index.put({ document, id })
module.exports = class DocumentStore {
static define(...indexDefinitions) {
if (!indexDefinitions[0]) throw new Error('At least one index is required')
return class {
constructor() {
return new DocumentStore(...indexDefinitions)
}
}
return this
}
delete(query) {
for (const id of this._allIds(query)) {
this._emitDelete(id)
constructor(...args) {
const defaultOptions = { Index: MemoryIndex, Documents: Map }
const { Index, Documents } = containsOptions(args)
? args.pop()
: defaultOptions
if (!args[0]) throw new Error('At least one index is required')
this._makeId = parseIndexDefinition(args[0]).makeKey
this._indices = args
.map(parseIndexDefinition)
.map(({ makeKey, makeKeys }) => makeKeys || (doc => [makeKey(doc)]))
.map(makeKeys => new Index(makeKeys))
this._documents = new Documents()
this._events = new EventEmitter()
}
async values() {
return Array.from(await this._documents.values())
}
async put(...documents) {
for (const document of documents) {
const id = this._makeId(document)
await this._emitInsertOrUpdate(document, id)
await this._documents.set(id, document)
for (const index of this._indices) {
index.deleteId(id)
await index.put({ document, id })
}
this._documents.delete(id)
}

@@ -51,13 +67,24 @@ return this

all(query) {
return this._allIds(query).map(id => this._documents.get(id))
async delete(query) {
for (const id of await this._allIds(query)) {
await this._emitDelete(id)
await Promise.all(this._indices.map(index => index.deleteId(id)))
await this._documents.delete(id)
}
return this
}
updateAll(query, transform) {
this.all(query).map(transform).forEach(this.put.bind(this))
async all(query) {
const result = await this._allIds(query)
return [...result].map(id => this._documents.get(id))
}
async updateAll(query, transform) {
const updated = (await this.all(query)).map(transform)
await this.put(...updated)
return this
}
get(query) {
const results = this.all(query)
async get(query) {
const results = await this.all(query)
if (results.length > 1)

@@ -75,11 +102,10 @@ throw new Error(

values: this.values.bind(this),
events: this._events
}
}
empty() {
async empty() {
for (const id of this._documents.keys()) {
for (const index of this._indices) {
index.deleteId(id)
}
this._documents.delete(id)
await Promise.all(this._indices.map(index => index.deleteId))
await this._documents.delete(id)
}

@@ -89,43 +115,19 @@ return this

withIndex(makeKey) {
return this._withExtraIndex(new MemoryIndex(doc => [makeKey(doc)]))
}
withIndexOfAll(makeKeys) {
return this._withExtraIndex(new MemoryIndex(makeKeys))
}
_emitInsertOrUpdate(document, id) {
if (!this._documents.has(id))
return this._events.emit('insert', document)
const originalDocument = this._documents.get(id)
async _emitInsertOrUpdate(document, id) {
if (!this._documents.has(id)) return this._events.emit('insert', document)
const originalDocument = await this._documents.get(id)
this._events.emit('update', [originalDocument, document])
}
_emitDelete(id) {
const doc = this._documents.get(id)
async _emitDelete(id) {
const doc = await this._documents.get(id)
this._events.emit('delete', doc)
}
_withExtraIndex(index) {
for (const document of this._documents.values()) {
const id = this._makeId(document)
index.put({ document, id })
}
return new DocumentStore(this._makeId, {
indices: this._indices.concat(index),
documents: this._documents,
})
async _allIds(query) {
const indexResults = await Promise.all(
this._indices.map(index => index.getIds(query))
)
return indexResults.reduce((acc, result) => acc.and(result), indexResults.pop())
}
_allIds(query) {
return [
...new Set(
this._indices.reduce(
(ids, index) => ids.concat(index.getIds(query)),
[]
)
),
]
}
}

@@ -9,66 +9,252 @@ 'use strict'

context('being constructed', () => {
it('throws an error if given no makeId function', () => {
const indices = ['[some index]']
assert.throws(() => new DocumentStore({ indices }), /makeId is required/)
})
it('returns all the values', async () => {
const dave = { name: 'Dave', uid: '1234' }
const store = await new DocumentStore(makeId, 'name').put(dave)
assert.deepEqual(await store.values(), [dave])
})
it('returns all the values', () => {
const dave = { name: 'Dave', uid: '1234' }
const store = new DocumentStore(makeId)
.withIndex(document => document.name)
.put(dave)
const actual = store.values()
assert.deepEqual(actual, [dave])
describe('defining a new type of store', () => {
it('creates a store with a single index function', async () => {
const Store = DocumentStore.define(document => document.age.toString())
const store = new Store()
const dave = { name: 'dave', age: 22 }
await store.put(dave)
const actual = await store.get({ age: 22 })
assert.equal(actual, dave)
})
it('creates a store with a property name index', async () => {
const Store = DocumentStore.define('name')
const store = new Store()
const dave = { name: 'dave' }
await store.put(dave)
assert.equal(await store.get({ name: 'dave' }), dave)
})
it('creates a store with multiple indices of different types', async () => {
const Store = DocumentStore.define('name', document =>
document.age.toString()
)
const store = new Store()
const dave = { name: 'dave', age: 22 }
await store.put(dave)
assert.equal(await store.get({ name: 'dave' }), dave)
assert.equal(await store.get({ age: 22 }), dave)
})
it('throws if no index is given', () => {
assert.throws(
() => DocumentStore.define(),
/At least one index is required/
)
})
})
context('with a single unique index', () => {
it('finds a single document', () => {
const dave = { name: 'Dave', uid: '1234' }
const store = new DocumentStore(makeId)
.withIndex(document => document.name)
.put(dave)
const actual = store.get({ name: 'Dave' })
assert.deepEqual(actual, dave)
describe('querying a store', () => {
it('gets individual documents by the indexed property value', async () => {
const Store = DocumentStore.define('uid')
const store = new Store()
const dave = { uid: 'dave' }
const sally = { uid: 'sally' }
await store.put(dave, sally)
assert.equal(await store.get({ uid: 'dave' }), dave)
})
it('returns null when no result is found', () => {
it('returns null when no result is found', async () => {
const dave = { name: 'Dave', uid: '1234' }
const store = new DocumentStore(makeId)
.withIndex(document => document.name)
.put(dave)
const actual = store.get({ name: 'David' })
assert.deepEqual(actual, null)
const store = await new DocumentStore(makeId, 'name').put(dave)
assert.deepEqual(await store.get({ name: 'David' }), null)
})
it('updates a document with the same ID', () => {
const dave = { name: 'Dave', uid: '1234' }
const updatedDave = { name: 'David', uid: '1234' }
const store = new DocumentStore(makeId)
.withIndex(document => document.name)
.put(dave)
.put(updatedDave)
const actual = store.get({ name: 'David' })
assert.deepEqual(actual, updatedDave)
assert.equal(store.get({ name: 'Dave' }), null)
it('returns null for an invalid query', async () => {
const store = new DocumentStore(makeId, 'name')
assert.equal(await store.get({ age: '30' }), null)
})
it('returns null for an invalid query', () => {
const store = new DocumentStore(makeId).withIndex(
document => document.name
it('finds all documents matching a query', async () => {
const dave = { name: 'Dave', hair: 'red' }
const dan = { name: 'Dan', hair: 'red' }
const susan = { name: 'Susan', hair: 'grey' }
const store = await new DocumentStore('name', 'hair').put(
dave,
dan,
susan
)
assert.equal(store.get({ age: '30' }), null)
assert.deepEqual(await store.all({ hair: 'red' }), [dave, dan])
})
it("allows an extra unique index on a property that's not always present", () => {
it('converts non-string properties in the query', async () => {
const dave = { name: 'Dave', public: true }
const dan = { name: 'Dan', public: false }
const makeId = document => document.name
const store = await new DocumentStore(makeId, document =>
document.public.toString()
).put(dave, dan)
assert.deepEqual(await store.all({ public: true }), [dave])
})
it('finds no documents when none match the query', async () => {
const susan = { name: 'Susan', hair: 'grey' }
const makeId = document => document.name
const store = await new DocumentStore(makeId, 'hair').put(susan)
assert.deepEqual(await store.all({ hair: 'blue' }), [])
})
it('throws an error if you get more than one thing', async () => {
const dave = { name: 'Dave', hair: 'red' }
const dan = { name: 'Dan', hair: 'red' }
const makeId = document => document.name
const store = await new DocumentStore(makeId, 'hair').put(dave, dan)
try {
await store.get({ hair: 'red' })
} catch (e) {
assert(e.message.match(/Only expected to get one result but got 2/))
}
})
it('uses AND when querying by multiple properties', async () => {
const dave = { uid: 'dave', age: 30, eyes: 'blue' }
const dan = { uid: 'dan', age: 30, eyes: 'green' }
const sally = { uid: 'sally', age: 31, eyes: 'green' }
const store = await new DocumentStore('uid', 'age', 'eyes').put(dave, sally, dan)
assert.deepEqual(await store.all({ age: 30 }), [dave, dan])
assert.deepEqual(await store.all({ age: 30, eyes: 'green' }), [dan])
})
describe('with an index on a non-unique property', () => {
const Store = DocumentStore.define('uid', 'hair')
let store
const dave = { uid: 'Dave', hair: 'red' }
const dan = { uid: 'Dan', hair: 'red' }
const susan = { uid: 'Susan', hair: 'grey' }
beforeEach(async () => {
store = new Store()
await store.put(dave)
await store.put(dan)
await store.put(susan)
})
it('gets all indexed documents with a matching property value', async () => {
assert.deepEqual(await store.all({ hair: 'red' }), [dave, dan])
})
it('errors when asked for an individual document on a query that would return more than one', async () => {
try {
await store.get({ hair: 'red' })
} catch (e) {
assert(e.message.match(/Only expected to get one result/))
}
})
it('gets one matching document when only one matches', async () => {
assert.equal(await store.get({ hair: 'grey' }), susan)
})
})
describe('with a many-to-many index', () => {
const tennis = {
name: 'tennis',
members: [{ name: 'dave' }, { name: 'sally' }]
}
const cinema = {
name: 'cinema',
members: [{ name: 'sally' }, { name: 'barry' }]
}
const squash = {
name: 'squash',
members: [{ name: 'dave' }, { name: 'sally' }, { name: 'barry' }]
}
const Store = DocumentStore.define('name', [
club => club.members.map(member => member.name)
])
it('finds all items ', async () => {
const clubs = new Store()
await clubs.put(tennis, cinema, squash)
assert.deepEqual(await clubs.all({ members: [{ name: 'barry' }] }), [
cinema,
squash
])
})
it('updates an item', async () => {
const clubs = new Store()
await clubs.put(tennis, cinema, squash)
await clubs.put({
name: 'cinema',
members: []
})
assert.deepEqual(await clubs.all({ members: [{ name: 'barry' }] }), [
squash
])
})
})
})
describe('emptying a store', () => {
it('can be emptied', async () => {
const dave = { uid: 'abcdef123' }
const store = await new DocumentStore('uid').put(dave)
await store.empty()
assert.equal(await store.get({ uid: 'abcdef123' }), null)
})
})
context('emitting events', () => {
const called = {}
const store = new DocumentStore('name')
store
.forQueries()
.events.on('insert', doc => (called.insert = doc))
.on('update', doc => (called.update = doc))
.on('delete', doc => (called.delete = doc))
const dave = { name: 'dave' }
const updatedDave = { name: 'dave', shoes: 'brown' }
it('calls an insert event', async () => {
await store.put(dave)
assert.deepEqual(called, { insert: dave })
})
it('calls an update event', async () => {
await store.put(dave)
await store.put(updatedDave)
assert.deepEqual(called, { insert: dave, update: [dave, updatedDave] })
})
it('calls a delete event', async () => {
await store.put(dave)
await store.put(updatedDave)
await store.delete(dave)
assert.deepEqual(called, {
insert: dave,
update: [dave, updatedDave],
delete: updatedDave
})
})
})
context('storing documents', () => {
it('updates a document with the same ID', async () => {
const dave = { name: 'Dave', uid: '1234' }
const updatedDave = { name: 'David', uid: '1234' }
const store = await new DocumentStore(makeId, 'name').put(
dave,
updatedDave
)
assert.deepEqual(await store.get({ name: 'David' }), updatedDave)
assert.equal(await store.get({ name: 'Dave' }), null)
})
it("stores documents even if they don't have all indexed properties", async () => {
const dave = { name: 'Dave', age: 30, uid: '1234' }
const sally = { name: 'Sally', uid: '4567' }
const store = new DocumentStore(makeId)
.withIndex(document => document.name)
.withIndex(document => document.age)
.put(dave)
.put(sally)
assert.equal(store.get({ age: 30 }), dave)
assert.equal(store.get({ name: 'Sally' }), sally)
const store = await new DocumentStore(makeId, 'name', 'age').put(
dave,
sally
)
assert.equal(await store.get({ age: 30 }), dave)
assert.equal(await store.get({ name: 'Sally' }), sally)
})

@@ -81,12 +267,14 @@ })

const susan = { name: 'Susan', hair: 'grey' }
const store = new DocumentStore(document => document.name)
.withIndex(document => document.hair)
.put(dave)
.put(dan)
.put(susan)
const people = store.forQueries()
let store, people
beforeEach(async () => {
store = new DocumentStore('name', 'hair')
await store.put(dave, dan, susan)
people = store.forQueries()
})
it('offers all the query methods', () => {
people.get({ name: 'Dave' })
people.all({ hair: 'red' })
people.values({ hair: 'red' })
})

@@ -99,25 +287,26 @@

it('continues to read updated state', () => {
it('continues to read updated state', async () => {
const matt = { name: 'Matt', hair: 'thin' }
store.put(matt)
assert.deepEqual(people.get({ name: 'Matt' }), matt)
await store.put(matt)
assert.deepEqual(await people.get({ name: 'Matt' }), matt)
})
})
context('modifying values', () => {
context('updating multiple values at once', () => {
const dave = { name: 'Dave', hair: 'red' }
const dan = { name: 'Dan', hair: 'red' }
const susan = { name: 'Susan', hair: 'grey' }
const store = new DocumentStore(document => document.name)
.withIndex(document => document.hair)
.put(dave)
.put(dan)
.put(susan)
let store
it('#updateAll stores the new value returned by the function', () => {
store.updateAll({ hair: 'red' }, doc => ({ ...doc, hair: 'blue' }))
assert.deepEqual(store.values(), [
beforeEach(async () => {
store = new DocumentStore('name', 'hair')
await store.put(dave, dan, susan)
})
it('stores the new value returned by the function', async () => {
await store.updateAll({ hair: 'red' }, doc => ({ ...doc, hair: 'blue' }))
assert.deepEqual(await store.values(), [
{ name: 'Dave', hair: 'blue' },
{ name: 'Dan', hair: 'blue' },
susan,
susan
])

@@ -127,98 +316,48 @@ })

context('with a single one-to-many index', () => {
it('#all finds all documents matching a query', () => {
const dave = { name: 'Dave', hair: 'red' }
const dan = { name: 'Dan', hair: 'red' }
const susan = { name: 'Susan', hair: 'grey' }
const makeId = document => document.name
const store = new DocumentStore(makeId)
.withIndex(document => document.hair)
.put(dave)
.put(dan)
.put(susan)
const actual = store.all({ hair: 'red' })
assert.deepEqual(actual, [dave, dan])
})
it('#all works with non-string properties', () => {
const dave = { name: 'Dave', public: true }
const dan = { name: 'Dan', public: false }
const makeId = document => document.name
const store = new DocumentStore(makeId)
.withIndex(document => document.public.toString())
.put(dave)
.put(dan)
const actual = store.all({ public: true })
assert.deepEqual(actual, [dave])
})
it('#all finds no documents when none match the query', () => {
const susan = { name: 'Susan', hair: 'grey' }
const makeId = document => document.name
const store = new DocumentStore(makeId)
.withIndex(document => document.hair)
.put(susan)
const actual = store.all({ hair: 'blue' })
assert.deepEqual(actual, [])
})
it('#get throws an error if you get more than one thing', () => {
const dave = { name: 'Dave', hair: 'red' }
const dan = { name: 'Dan', hair: 'red' }
const makeId = document => document.name
const store = new DocumentStore(makeId)
.withIndex(document => document.hair)
.put(dave)
.put(dan)
assert.throws(
() => store.get({ hair: 'red' }),
/Only expected to get one result but got 2/
)
})
})
describe('deleting documents', () => {
it('deletes an existing document in a single unique index', () => {
it('deletes an existing document in a single unique index', async () => {
const dave = { name: 'Dave', uid: '1234' }
const store = new DocumentStore(makeId)
.withIndex(document => document.name)
.put(dave)
.delete(dave)
assert.equal(store.get({ name: 'Dave' }), null)
const store = await new DocumentStore(makeId, 'name').put(dave)
await store.delete(dave)
assert.equal(await store.get({ name: 'Dave' }), null)
})
it('deletes existing documents by an indexed query', () => {
it('deletes existing documents by an indexed query', async () => {
const dave = { name: 'Dave', uid: '1234' }
const store = new DocumentStore(makeId)
.withIndex(document => document.name)
.put(dave)
.delete({ name: 'Dave' })
assert.equal(store.get({ name: 'Dave' }), null)
const store = await new DocumentStore(makeId, 'name').put(dave)
await store.delete({ name: 'Dave' })
assert.equal(await store.get({ name: 'Dave' }), null)
})
})
context('adding a unique index', () => {
it('adds existing documents to the new index', () => {
const dave = { name: 'Dave', uid: '1234' }
const indices = []
const store = new DocumentStore(makeId, {
indices,
})
.put(dave)
.withIndex(document => document.name)
assert.deepEqual(store.get({ name: 'Dave' }), dave)
})
context('injecting a store provider', () => {
const Index = class {
async put({ id }) {
this.ids = [id]
}
async getIds() {
return this.ids
}
}
it('can add multiple indices', () => {
const dave = { name: 'Dave', age: 30, uid: '1234' }
const indices = []
const store = new DocumentStore(makeId, {
indices,
})
.put(dave)
.withIndex(document => document.name)
.withIndex(document => document.age.toString())
assert.deepEqual(store.get({ age: 30 }), dave)
const Documents = class {
async has() {
return true
}
async get() {
return this._doc
}
async set(id, doc) {
this._doc = doc
}
}
it('allows injection of options with Index and Documents', async () => {
const store = new DocumentStore('name', { Index, Documents })
await store.put({ name: 'dave' })
assert.deepEqual(await store.get({ name: 'dave' }), { name: 'dave' })
})
})
})
'use strict'
module.exports = class MemoryManyToManyIndex {
module.exports = class MemoryIndex {
constructor(makeKeys) {

@@ -10,6 +10,6 @@ this._makeKeys = makeKeys

put({ document, id }) {
async put({ document, id }) {
this.deleteId(id)
const keys = this._attemptToMakeKeys(document)
if (!keys) return this
this.deleteId(id)
if (!keys[0]) return this
this._keys.set(id, keys)

@@ -23,14 +23,13 @@ for (const key of keys) {

getIds(query) {
async getIds(query) {
const keys = this._attemptToMakeKeys(query)
return !keys
? []
: keys.reduce(
(ids, key) =>
this._ids.get(key) ? ids.concat(this._ids.get(key)) : ids,
[]
)
if (!keys[0]) return new NoResult()
const ids = keys.reduce(
(ids, key) => (this._ids.get(key) ? ids.concat(this._ids.get(key)) : ids),
[]
)
return new IndexResult(ids)
}
deleteId(documentId) {
async deleteId(documentId) {
const keys = this._keys.get(documentId) || []

@@ -51,3 +50,3 @@ this._keys.delete(documentId)

} catch (err) {
return undefined
return [undefined]
}

@@ -57,1 +56,33 @@ })()

}
class NoResult {
has() {
return true
}
and(otherResult) {
return otherResult
}
[Symbol.iterator]() {
return new Set().values()
}
}
class IndexResult {
constructor(ids) {
this._ids = new Set(ids)
}
has(id) {
return this._ids.has(id)
}
and(otherResult) {
return new IndexResult([...this._ids].filter(id => otherResult.has(id)))
}
[Symbol.iterator]() {
return this._ids.values()
}
}

@@ -10,3 +10,3 @@ 'use strict'

beforeEach(() => {
beforeEach(async () => {
const dave = { name: 'dave' }

@@ -16,20 +16,20 @@ const sally = { name: 'sally' }

index = new Index(person => [person.name])
index
.put({ id: 'dave', document: dave })
.put({ id: 'sally', document: sally })
.put({ id: 'barry', document: barry })
await index.put({ id: 'dave', document: dave })
await index.put({ id: 'sally', document: sally })
await index.put({ id: 'barry', document: barry })
})
it('retrieves the ID of a document that matches a query', () => {
const actual = index.getIds({ name: 'barry' })
assert.deepEqual(actual, ['barry'])
it('retrieves the ID of a document that matches a query', async () => {
const actual = await index.getIds({ name: 'barry' })
assert.deepEqual([...actual], ['barry'])
})
it('deletes a document', () => {
const actual = index.deleteId('barry').getIds({ name: 'barry' })
assert.deepEqual(actual, [])
it('deletes a document', async () => {
await index.deleteId('barry')
const actual = await index.getIds({ name: 'barry' })
assert.deepEqual([...actual], [])
})
it("ignores documents that can't be indexed", () => {
index.put({ age: 55 })
it("ignores documents that can't be indexed", async () => {
await index.put({ age: 55 })
})

@@ -41,3 +41,3 @@ })

beforeEach(() => {
beforeEach(async () => {
const dave = { name: 'dave', hair: 'brown' }

@@ -47,29 +47,58 @@ const sally = { name: 'sally', hair: 'red' }

index = new Index(person => [person.hair])
index
.put({ id: 'dave', document: dave })
.put({ id: 'sally', document: sally })
.put({ id: 'barry', document: barry })
await index.put({ id: 'dave', document: dave })
await index.put({ id: 'sally', document: sally })
await index.put({ id: 'barry', document: barry })
})
it('retrieves the IDs of documents that match a query', () => {
const actual = index.getIds({ hair: 'red' })
assert.deepEqual(actual, ['sally', 'barry'])
it('retrieves the IDs of documents that match a query', async () => {
const actual = await index.getIds({ hair: 'red' })
assert.deepEqual([...actual], ['sally', 'barry'])
})
it('deletes a document', () => {
const actual = index.deleteId('barry').getIds({ hair: 'red' })
assert.deepEqual(actual, ['sally'])
it('returns no result for a query on a different key', async () => {
const actual = await index.getIds({ foo: 'red' })
assert(actual.and('x') === 'x')
})
it('updates a document with a matching ID', () => {
index.put({
it('returns an empty result for a query on a different key', async () => {
const actual = await index.getIds({ hair: 'black' })
assert.deepEqual([...actual], [])
})
it('deletes a document', async () => {
await index.deleteId('barry')
const actual = await index.getIds({ hair: 'red' })
assert.deepEqual([...actual], ['sally'])
})
it('updates a document with a matching ID', async () => {
await index.put({
id: 'barry',
document: {
name: 'barry',
hair: 'brown',
},
hair: 'brown'
}
})
const actual = index.getIds({ hair: 'brown' })
assert.deepEqual(actual, ['dave', 'barry'])
const actual = await index.getIds({ hair: 'brown' })
assert.deepEqual([...actual], ['dave', 'barry'])
})
it("removes a document being updated that's no longer indexed", async () => {
await index.put({
id: 'barry',
document: {
name: 'barry',
hair: 'brown'
}
})
await index.put({
id: 'barry',
document: {
name: 'barry',
age: 22
}
})
const actual = await index.getIds({ hair: 'brown' })
assert.deepEqual([...actual], ['dave'])
})
})

@@ -80,64 +109,63 @@

beforeEach(() => {
beforeEach(async () => {
const tennis = {
name: 'tennis',
members: [{ name: 'dave' }, { name: 'sally' }],
members: [{ name: 'dave' }, { name: 'sally' }]
}
const cinema = {
name: 'cinema',
members: [{ name: 'sally' }, { name: 'barry' }],
members: [{ name: 'sally' }, { name: 'barry' }]
}
const squash = {
name: 'squash',
members: [{ name: 'dave' }, { name: 'sally' }, { name: 'barry' }],
members: [{ name: 'dave' }, { name: 'sally' }, { name: 'barry' }]
}
index = new Index(club => club.members.map(member => member.name))
index
.put({ id: 'tennis', document: tennis })
.put({ id: 'cinema', document: cinema })
.put({ id: 'squash', document: squash })
await index.put({ id: 'tennis', document: tennis })
await index.put({ id: 'cinema', document: cinema })
await index.put({ id: 'squash', document: squash })
})
it('retrieves the IDs of documents that match a query', () => {
const actual = index.getIds({ members: [{ name: 'dave' }] })
assert.deepEqual(actual, ['tennis', 'squash'])
it('retrieves the IDs of documents that match a query', async () => {
const actual = await index.getIds({ members: [{ name: 'dave' }] })
assert.deepEqual([...actual], ['tennis', 'squash'])
})
it('deletes a document', () => {
index.deleteId('tennis')
const actual = index.getIds({ members: [{ name: 'dave' }] })
assert.deepEqual(actual, ['squash'])
it('deletes a document', async () => {
await index.deleteId('tennis')
const actual = await index.getIds({ members: [{ name: 'dave' }] })
assert.deepEqual([...actual], ['squash'])
})
it('updates a document with a matching ID', () => {
index.put({
it('updates a document with a matching ID', async () => {
await index.put({
id: 'squash',
document: {
name: 'squash',
members: [{ name: 'sally' }, { name: 'barry' }],
},
members: [{ name: 'sally' }, { name: 'barry' }]
}
})
const actual = index.getIds({ members: [{ name: 'dave' }] })
assert.deepEqual(actual, ['tennis'])
const actual = await index.getIds({ members: [{ name: 'dave' }] })
assert.deepEqual([...actual], ['tennis'])
})
it('returns an empty array when nothing matches the query', () => {
const actual = index.getIds({ members: [{ name: 'bob' }] })
assert.deepEqual(actual, [])
it('returns an empty result when nothing matches the query', async () => {
const actual = await index.getIds({ members: [{ name: 'bob' }] })
assert.deepEqual([...actual], [])
})
it('returns an empty array when the query makes no sense', () => {
const actual = index.getIds({ name: 'bob' })
assert.deepEqual(actual, [])
it('returns no result when the query makes no sense', async () => {
const actual = await index.getIds({ name: 'bob' })
assert(actual.and('x') === 'x')
})
it("ignores documents that don't create keys", () => {
index.put({
it("updatesd documents even if they don't create keys", async () => {
await index.put({
id: 'squash',
document: {
name: 'squash',
},
name: 'squash'
}
})
const actual = index.getIds({ members: [{ name: 'dave' }] })
assert.deepEqual(actual, ['tennis', 'squash'])
const actual = await index.getIds({ members: [{ name: 'dave' }] })
assert.deepEqual([...actual], ['tennis'])
})

@@ -149,3 +177,3 @@ })

makeKeys: document => [document.name],
makeId: document => document.uid,
makeId: document => document.uid
})

@@ -152,0 +180,0 @@ nameIndex.deleteId('dave')

{
"name": "gosh",
"version": "4.3.0",
"version": "5.2.0",
"description": "Great Object Storage Hooray!",

@@ -13,7 +13,7 @@ "main": "lib/gosh.js",

"eslint": "^4.9.0",
"eslint-config-prettier": "^2.6.0",
"eslint-config-prettier": "^2.9.0",
"eslint-plugin-mocha": "^4.11.0",
"eslint-plugin-prettier": "^2.3.1",
"eslint-plugin-prettier": "^2.6.0",
"mocha": "^4.1.0",
"prettier": "^1.7.4"
"prettier": "^1.12.1"
},

@@ -23,3 +23,7 @@ "dependencies": {

"value-object": "^0.0.10"
},
"prettier": {
"semi": false,
"singleQuote": true
}
}

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

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