Skip to content

Commit

Permalink
feat(SlothDatabase): add findAllDocs function
Browse files Browse the repository at this point in the history
  • Loading branch information
vinz243 committed Apr 2, 2018
1 parent bf7475d commit 8438285
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 8 deletions.
52 changes: 45 additions & 7 deletions src/models/SlothDatabase.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import PouchFactory from './PouchFactory'
import BaseEntity from './BaseEntity'
import EntityConstructor from '../helpers/EntityConstructor'
import getProtoData from '../utils/getProtoData'
import { join } from 'path'

/**
* This represent a Database
Expand All @@ -9,13 +11,22 @@ import EntityConstructor from '../helpers/EntityConstructor'
* @typeparam E the Entity
* @typeparam T the entity constructor
*/
export default class SlothDatabase<
S,
E extends BaseEntity<S>,
T extends EntityConstructor<S, E>
> {
private _name: string
private _model: T
export default class SlothDatabase<S, E extends BaseEntity<S>> {
/**
*
* @private
* @type {string}
* @memberof SlothDatabase
*/
_name: string
/**
*
*
* @type {T}
* @memberof SlothDatabase
* @private
*/
_model: EntityConstructor<S, E>

/**
* Create a new database instance
Expand All @@ -32,6 +43,33 @@ export default class SlothDatabase<
}
}

/**
* Fetches all documents for this database and map them with the model
*
* @param {PouchFactory<S>} factory the PouchDB factory to use
* @param {string} [startKey=''] the startkey to use
* @param {string} [endKey=path.join(startKey, '\uffff')] the endkey to use
* @returns a promise that resolves into an array of entity instances
* @see PouchDB#allDocs
* @memberof SlothDatabase
*/
findAllDocs(
factory: PouchFactory<S>,
startKey = '',
endKey = join(startKey, '\uffff')
) {
const db = factory(this._name)

return db
.allDocs({
include_docs: true,
startkey: startKey,
endkey: endKey
})
.then(({ rows }) => {
return rows.map(({ doc }) => this.create(factory, doc as S))
})
}
/**
* Fetch a docuemt from the database
* @param factory the PouchDB factory to use
Expand Down
2 changes: 1 addition & 1 deletion test/integration/Author.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@ class Author extends BaseEntity<AuthorSchema> {
@SlothField() age = 40
}

export default new SlothDatabase<AuthorSchema, Author, typeof Author>(Author)
export default new SlothDatabase<AuthorSchema, Author>(Author)
66 changes: 66 additions & 0 deletions test/unit/models/SlothDatabase.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,69 @@ test('SlothDatabase#create - create a model instance with props', async () => {

expect(_model).toHaveBeenCalledWith(factory, props)
})

test('SlothDatabase#findAllDocs - calls allDocs and creates models', async () => {
const create = jest.fn().mockImplementation((omit: never, el: object) => el)

const props = { _id: 'foos/bar', foo: 'bar' }
const docs = [{ foo: 'bar' }, { bar: 'foo' }]

const allDocs = jest
.fn()
.mockResolvedValue({ rows: docs.map(doc => ({ doc })) })
const factory = jest.fn().mockReturnValue({ allDocs })

expect(
await SlothDatabase.prototype.findAllDocs.call(
{ create, _name: 'foos' },
factory
)
).toEqual(docs)
expect(
await SlothDatabase.prototype.findAllDocs.call(
{ create, _name: 'foos' },
factory,
'foos/bar'
)
).toEqual(docs)
expect(
await SlothDatabase.prototype.findAllDocs.call(
{ create, _name: 'foos' },
factory,
'foo',
'bar'
)
).toEqual(docs)

expect(create).toHaveBeenCalledWith(factory, docs[0])
expect(create).toHaveBeenCalledWith(factory, docs[1])
expect(create).toHaveBeenCalledTimes(6)

expect(factory).toHaveBeenCalledTimes(3)
expect(factory).toHaveBeenCalledWith('foos')

expect(allDocs).toHaveBeenCalledTimes(3)
expect(allDocs.mock.calls).toMatchObject([
[
{
include_docs: true,
startkey: '',
endkey: '\uffff'
}
],
[
{
include_docs: true,
startkey: 'foos/bar',
endkey: 'foos/bar/\uffff'
}
],
[
{
include_docs: true,
startkey: 'foo',
endkey: 'bar'
}
]
])
})

0 comments on commit 8438285

Please sign in to comment.