Skip to content

Commit

Permalink
fix(sloth-index): use docKeys instead of keys
Browse files Browse the repository at this point in the history
  • Loading branch information
vinz243 committed Apr 20, 2018
1 parent 5556d35 commit 9715485
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 5 deletions.
12 changes: 11 additions & 1 deletion src/decorators/SlothIndex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,18 @@ export default function SlothIndex<S, V extends string = string>(
docId?: string
) {
return (target: object, key: string) => {
const field = getProtoData(target).fields.find(field => field.key === key)

if (!field) {
throw new Error('Please use SlothIndex on top of a SlothField')
}

SlothView(
new Function('doc', 'emit', `emit(doc['${key}'].toString());`) as any,
new Function(
'doc',
'emit',
`emit(doc['${field.docKey}'].toString());`
) as any,
viewId,
docId
)(target, key)
Expand Down
10 changes: 8 additions & 2 deletions src/decorators/SlothView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,17 @@ export default function SlothView<S, V extends string = string>(
(${fn.toString()})(__doc, emit);
}`

const { views } = getProtoData(target, true)
const { views, fields } = getProtoData(target, true)

const field = fields.find(field => field.key === key)

if (!field) {
throw new Error('Required SlothView on top of a SlothField')
}

views.push({
id: docId,
name: viewId || `by_${key}`,
name: viewId || `by_${field.docKey}`,
function: fn,
code: fun
})
Expand Down
2 changes: 1 addition & 1 deletion test/integration/Track.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ export class TrackEntity extends BaseEntity<TrackSchema> {
@SlothURI('library', 'album', 'number', 'name')
_id: string = ''

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

@SlothField() number: string = '00'
Expand Down
42 changes: 42 additions & 0 deletions test/integration/views.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@ import Artist from './Artist'
import Track, { TrackViews } from './Track'
import PouchDB from 'pouchdb'
import delay from '../utils/delay'
import {
SlothEntity,
BaseEntity,
SlothURI,
SlothField,
SlothDatabase,
SlothIndex
} from '../../src/slothdb'

PouchDB.plugin(require('pouchdb-adapter-memory'))

Expand Down Expand Up @@ -115,3 +123,37 @@ describe('views', () => {
})
})
})

describe('views and docKeys', () => {
const prefix = Date.now().toString(26) + '_'

const factory = (name: string) =>
new PouchDB(prefix + name, { adapter: 'memory' })

@SlothEntity('foos')
class FooEnt extends BaseEntity<any> {
@SlothURI('foos', 'name')
_id = ''

@SlothIndex()
@SlothField('not_name')
name = ''
}

const Foo = new SlothDatabase<any, any, string>(FooEnt)

test('uses docKeys', async () => {
await Foo.put(factory, { name: 'foo' })
await Foo.put(factory, { name: 'foobar' })
await Foo.put(factory, { name: 'bar' })

const keys = await Foo.queryKeys(
factory,
'views/by_not_name',
'foo',
'foo\uffff'
)

expect(keys).toEqual(['foo', 'foobar'])
})
})
14 changes: 13 additions & 1 deletion test/unit/decorators/SlothView.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,25 @@ test('SlothView - fails without a decorator', () => {
/Required SlothView/
)
})

test('SlothView - generates a working function for es5 view', () => {
const proto = emptyProtoData({})
const obj = { __protoData: proto }

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

expect(() =>
SlothView(function(doc: { bar: string }, emit) {
emit(doc.bar)
})(obj, 'foo')
).toThrowError(/Required SlothView/)
})

test('SlothView - generates a working function for es5 view', () => {
const proto = emptyProtoData({ fields: [{ key: 'foo', docKey: 'foo' }] })
const obj = { __protoData: proto }

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

SlothView(function(doc: { bar: string }, emit) {
emit(doc.bar)
})(obj, 'foo')
Expand Down

0 comments on commit 9715485

Please sign in to comment.