8000 feat(sloth-database): add queryKeys and queryKeyIDs methods · compactd/slothdb@3261413 · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Commit 3261413

Browse files
committed
feat(sloth-database): add queryKeys and queryKeyIDs methods
1 parent 578519f commit 3261413

File tree

5 files changed

+101
-5
lines changed

5 files changed

+101
-5
lines changed

src/models/SlothDatabase.ts

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { Subscriber, ChangeAction, ActionType } from './changes'
44
import EntityConstructor from '../helpers/EntityConstructor'
55
import getProtoData from '../utils/getProtoData'
66
import { join } from 'path'
7+
import Dict from '../helpers/Dict'
78

89
/**
910
* This represent a Database
@@ -82,6 +83,59 @@ export default class SlothDatabase<
8283
})
8384
}
8485

86+
/**
87+
* Queries keys. Returns an array of emitted keys
88+
*
89+
* @param factory the pouch factory
90+
* @param view the view identifier
91+
* @param startKey the optional startkey
92+
* @param endKey the optional endkey
93+
*/
94+
queryKeys(
95+
factory: PouchFactory<S>,
96+
view: V,
97+
startKey = '',
98+
endKey = join(startKey, '\uffff')
99+
): Promise<string[]> {
100+
return factory(this._name)
101+
.query(view, {
102+
startkey: startKey,
103+
endkey: endKey,
104+
include_docs: false
105+
})
106+
.then(({ rows }) => {
107+
return rows.map(({ key }) => key)
108+
})
109+
}
110+
111+
/**
112+
* Queries keys/_id map. Returns a map of emitted keys/ID
113+
*
114+
* @param factory the pouch factory
115+
* @param view the view identifier
116+
* @param startKey the optional startkey
117+
* @param endKey the optional endkey
118+
*/
119+
queryKeysIDs(
120+
factory: PouchFactory<S>,
121+
view: V,
122+
startKey = '',
123+
endKey = join(startKey, '\uffff')
124+
) {
125+
return factory(this._name)
126+
.query(view, {
127+
startkey: startKey,
128+
endkey: endKey,
129+
include_docs: false
130+
})
131+
.then(({ rows }) => {
132+
return rows.reduce(
133+
(acc, { key, id }) => ({ ...acc, [key]: id }),
134+
{} as Dict<string>
135+
)
136+
})
137+
}
138+
85139
/**
86140
* Returns a database that will only find entities with _id
87141
* starting with the root path

test/integration/Track.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import {
22
BaseEntity,
33
SlothDatabase,
44
SlothEntity,
5+
SlothIndex,
56
SlothURI,
67
SlothField,
78
SlothRel,
@@ -32,7 +33,9 @@ export class TrackEntity extends BaseEntity<TrackSchema> {
3233
@SlothURI('library', 'album', 'number', 'name')
3334
_id: string = ''
3435

35-
@SlothField() name: string = 'Track Name'
36+
@SlothField()
37+
@SlothIndex()
38+
name: string = 'Track Name'
3639

3740
@SlothField() number: string = '00'
3841

test/integration/views.test.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,16 @@ describe('views', () => {
3939
})
4040
})
4141

42+
test('doesnt recreate views', async () => {
43+
const tracks = factory('tracks')
44+
const { _rev } = await tracks.get('_design/views')
45+
await Track.initSetup(factory)
46+
expect(await tracks.get('_design/views')).toMatchObject({
47+
views: { by_album: {} },
48+
_rev
49+
})
50+
})
51+
4252
test('query by view', async () => {
4353
const docs = await Track.queryDocs(
4454
factory,
@@ -48,4 +58,30 @@ describe('views', () => {
4858

4959
expect(docs.length).toBe(2)
5060
})
61+
test('queryKeys', async () => {
62+
const docs = await Track.queryKeys(
63+
factory,
64+
TrackViews.ByAlbum,
65+
'library/flatbush-zombies'
66+
)
67+
68+
expect(docs.length).toBe(2)
69+
expect(docs).toEqual([
70+
'library/flatbush-zombies/betteroffdead',
71+
'library/flatbush-zombies/betteroffdead-2'
72+
])
73+
})
74+
test('queryKeysIDs', async () => {
75+
const docs = await Track.queryKeysIDs(
76+
factory,
77+
TrackViews.ByAlbum,
78+
'library/flatbush-zombies'
79+
)
80+
expect(docs).toEqual({
81+
'library/flatbush-zombies/betteroffdead':
82+
'library/flatbush-zombies/betteroffdead/12/palm-trees',
83+
'library/flatbush-zombies/betteroffdead-2':
84+
'library/flatbush-zombies/betteroffdead-2/12/not-palm-trees'
85+
})
86+
})
5187
})

test/unit/decorators/SlothRel.test.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,15 @@ test('SlothRel - fails on top of another decorator', () => {
44
const obj = {}
55

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

810
expect(() => SlothRel({} as any)(obj, 'foo')).toThrowError(
911
/Cannot apply SlothRel/
1012
)
13+
expect(() => SlothRel({} as any)(obj, 'bar')).toThrowError(
14+
/Cannot apply SlothRel/
15+
)
16+
17+
SlothRel({} as any)(obj, 'barz')
1118
})

test/unit/models/SlothDatabase.test.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -177,10 +177,6 @@ test('SlothDatabase#findAllIDs - calls allDocs and return ids', async () => {
177177
])
178178
})
179179

180-
describe('SlothDatabase#subscribe', () => {
181-
test('')
182-
})
183-
184180
describe('SlothDatabase#changes', () => {
185181
const proto = Object.assign({}, SlothDatabase.prototype, { _subscribers: [] })
186182

0 commit comments

Comments
 (0)
0