Skip to content

Commit

Permalink
feat: add belongsTo relation mapper
Browse files Browse the repository at this point in the history
  • Loading branch information
vinz243 committed Apr 3, 2018
1 parent 5146e07 commit 933778e
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/utils/relationMappers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import getProtoData from './getProtoData'
import PouchFactory from '../models/PouchFactory'
import BaseEntity from '../models/BaseEntity'

export function belongsToMapper<S>(target: any, keyName: string) {
return (factory: PouchFactory<S>): Promise<BaseEntity<S>> => {
const { rels } = getProtoData(target)

const rel = rels.find(({ key }) => key === keyName)

if (!rel) {
throw new Error(`No relation available for ${keyName}`)
}

if ('belongsTo' in rel) {
return rel.belongsTo().findById(factory, target[keyName])
}

throw new Error('Unsupported relation')
}
}
28 changes: 28 additions & 0 deletions test/integration/Album.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
import PouchDB from 'pouchdb'
import Artist from './Artist'
import Album from './Album'
import { transpileModule } from 'typescript'
import { readFileSync } from 'fs'
const { compilerOptions } = require('../../tsconfig.json')

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

test('typescript transpiles', () => {
transpileModule('./Album.ts', { compilerOptions })
})

test('generate uri', async () => {
const dbName = Date.now().toString(26)

Expand Down Expand Up @@ -132,3 +139,24 @@ test('doesnt remove parent if still has children', async () => {
db.get('library/Flatbush-Zombies/BetterOffDead')
).rejects.toMatchObject({ message: 'missing' })
})

test('rels.artist - maps with artist', async () => {
const dbName = Date.now().toString(26)
const db = new PouchDB(dbName, { adapter: 'memory' })

const factory = () => new PouchDB(dbName, { adapter: 'memory' })

const flatbushZombies = Artist.create(factory, { name: 'Flatbush Zombies' })

await flatbushZombies.save()

const betterOffDead = Album.create(factory, {
name: 'BetterOffDead',
artist: flatbushZombies._id
})

const flatbush = await betterOffDead.rels.artist(factory)

expect(flatbush._id).toBe('library/Flatbush-Zombies')
expect(flatbush.name).toBe('Flatbush Zombies')
})
5 changes: 5 additions & 0 deletions test/integration/Album.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import SlothEntity from '../../src/decorators/SlothEntity'
import SlothURI from '../../src/decorators/SlothURI'
import SlothField from '../../src/decorators/SlothField'
import SlothRel from '../../src/decorators/SlothRel'
import { belongsToMapper } from '../../src/utils/relationMappers'
import Artist from './Artist'

export interface AlbumSchema {
Expand All @@ -20,6 +21,10 @@ class Album extends BaseEntity<AlbumSchema> {

@SlothURI('library', 'artist', 'name')
_id: string = ''

rels = {
artist: belongsToMapper<Artist>(this, 'artist')
}
}

export default new SlothDatabase<AlbumSchema, Album>(Album)
28 changes: 28 additions & 0 deletions test/unit/utils/relationMappers.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { belongsToMapper } from '../../../src/utils/relationMappers'
import emptyProtoData from '../../utils/emptyProtoData'

describe('belongsToMapper', () => {
test('Throws error if no relation available', () => {
const mapper = belongsToMapper({ __protoData: emptyProtoData({}) }, 'foo')

expect(() => mapper(null)).toThrowError(/No relation available/)
})

test('Throws an error for unsupported relation', () => {
const mapper = belongsToMapper(
{
__protoData: emptyProtoData({
rels: [
{
unsupported: 'foo',
key: 'foo'
} as any
]
})
},
'foo'
)

expect(() => mapper(null)).toThrowError(/Unsupported/)
})
})

0 comments on commit 933778e

Please sign in to comment.