diff --git a/src/decorators/SlothIndex.ts b/src/decorators/SlothIndex.ts index f7ce8b7..4b4a8b6 100644 --- a/src/decorators/SlothIndex.ts +++ b/src/decorators/SlothIndex.ts @@ -21,8 +21,18 @@ export default function SlothIndex( 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) diff --git a/src/decorators/SlothView.ts b/src/decorators/SlothView.ts index 5fe891b..5472bc7 100644 --- a/src/decorators/SlothView.ts +++ b/src/decorators/SlothView.ts @@ -33,11 +33,17 @@ export default function SlothView( (${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 }) diff --git a/test/integration/Track.ts b/test/integration/Track.ts index 5a58a63..fd057da 100644 --- a/test/integration/Track.ts +++ b/test/integration/Track.ts @@ -33,8 +33,8 @@ export class TrackEntity extends BaseEntity { @SlothURI('library', 'album', 'number', 'name') _id: string = '' - @SlothField() @SlothIndex() + @SlothField() name: string = 'Track Name' @SlothField() number: string = '00' diff --git a/test/integration/views.test.ts b/test/integration/views.test.ts index 50f37a1..68d961b 100644 --- a/test/integration/views.test.ts +++ b/test/integration/views.test.ts @@ -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')) @@ -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 { + @SlothURI('foos', 'name') + _id = '' + + @SlothIndex() + @SlothField('not_name') + name = '' + } + + const Foo = new SlothDatabase(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']) + }) +}) diff --git a/test/unit/decorators/SlothView.test.ts b/test/unit/decorators/SlothView.test.ts index d865a4e..a237d8d 100644 --- a/test/unit/decorators/SlothView.test.ts +++ b/test/unit/decorators/SlothView.test.ts @@ -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')