Skip to content

Commit

Permalink
feat(sloth-database): add queryKeys and queryKeyIDs methods
Browse files Browse the repository at this point in the history
  • Loading branch information
vinz243 committed Apr 18, 2018
1 parent 578519f commit 3261413
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 5 deletions.
54 changes: 54 additions & 0 deletions src/models/SlothDatabase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Subscriber, ChangeAction, ActionType } from './changes'
import EntityConstructor from '../helpers/EntityConstructor'
import getProtoData from '../utils/getProtoData'
import { join } from 'path'
import Dict from '../helpers/Dict'

/**
* This represent a Database
Expand Down Expand Up @@ -82,6 +83,59 @@ export default class SlothDatabase<
})
}

/**
* Queries keys. Returns an array of emitted keys
*
* @param factory the pouch factory
* @param view the view identifier
* @param startKey the optional startkey
* @param endKey the optional endkey
*/
queryKeys(
factory: PouchFactory<S>,
view: V,
startKey = '',
endKey = join(startKey, '\uffff')
): Promise<string[]> {
return factory(this._name)
.query(view, {
startkey: startKey,
endkey: endKey,
include_docs: false
})
.then(({ rows }) => {
return rows.map(({ key }) => key)
})
}

/**
* Queries keys/_id map. Returns a map of emitted keys/ID
*
* @param factory the pouch factory
* @param view the view identifier
* @param startKey the optional startkey
* @param endKey the optional endkey
*/
queryKeysIDs(
factory: PouchFactory<S>,
view: V,
startKey = '',
endKey = join(startKey, '\uffff')
) {
return factory(this._name)
.query(view, {
startkey: startKey,
endkey: endKey,
include_docs: false
})
.then(({ rows }) => {
return rows.reduce(
(acc, { key, id }) => ({ ...acc, [key]: id }),
{} as Dict<string>
)
})
}

/**
* Returns a database that will only find entities with _id
* starting with the root path
Expand Down
5 changes: 4 additions & 1 deletion test/integration/Track.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
BaseEntity,
SlothDatabase,
SlothEntity,
SlothIndex,
SlothURI,
SlothField,
SlothRel,
Expand Down Expand Up @@ -32,7 +33,9 @@ export class TrackEntity extends BaseEntity<TrackSchema> {
@SlothURI('library', 'album', 'number', 'name')
_id: string = ''

@SlothField() name: string = 'Track Name'
@SlothField()
@SlothIndex()
name: string = 'Track Name'

@SlothField() number: string = '00'

Expand Down
36 changes: 36 additions & 0 deletions test/integration/views.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,16 @@ describe('views', () => {
})
})

test('doesnt recreate views', async () => {
const tracks = factory('tracks')
const { _rev } = await tracks.get('_design/views')
await Track.initSetup(factory)
expect(await tracks.get('_design/views')).toMatchObject({
views: { by_album: {} },
_rev
})
})

test('query by view', async () => {
const docs = await Track.queryDocs(
factory,
Expand All @@ -48,4 +58,30 @@ describe('views', () => {

expect(docs.length).toBe(2)
})
test('queryKeys', async () => {
const docs = await Track.queryKeys(
factory,
TrackViews.ByAlbum,
'library/flatbush-zombies'
)

expect(docs.length).toBe(2)
expect(docs).toEqual([
'library/flatbush-zombies/betteroffdead',
'library/flatbush-zombies/betteroffdead-2'
])
})
test('queryKeysIDs', async () => {
const docs = await Track.queryKeysIDs(
factory,
TrackViews.ByAlbum,
'library/flatbush-zombies'
)
expect(docs).toEqual({
'library/flatbush-zombies/betteroffdead':
'library/flatbush-zombies/betteroffdead/12/palm-trees',
'library/flatbush-zombies/betteroffdead-2':
'library/flatbush-zombies/betteroffdead-2/12/not-palm-trees'
})
})
})
7 changes: 7 additions & 0 deletions test/unit/decorators/SlothRel.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,15 @@ test('SlothRel - fails on top of another decorator', () => {
const obj = {}

Reflect.defineProperty(obj, 'foo', { get: () => 'bar' })
Reflect.defineProperty(obj, 'bar', { set: () => 'bar' })
Reflect.defineProperty(obj, 'barz', { value: 42 })

expect(() => SlothRel({} as any)(obj, 'foo')).toThrowError(
/Cannot apply SlothRel/
)
expect(() => SlothRel({} as any)(obj, 'bar')).toThrowError(
/Cannot apply SlothRel/
)

SlothRel({} as any)(obj, 'barz')
})
4 changes: 0 additions & 4 deletions test/unit/models/SlothDatabase.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,10 +177,6 @@ test('SlothDatabase#findAllIDs - calls allDocs and return ids', async () => {
])
})

describe('SlothDatabase#subscribe', () => {
test('')
})

describe('SlothDatabase#changes', () => {
const proto = Object.assign({}, SlothDatabase.prototype, { _subscribers: [] })

Expand Down

0 comments on commit 3261413

Please sign in to comment.