Skip to content

Commit

Permalink
feat(base-entity): add exists method
Browse files Browse the repository at this point in the history
  • Loading branch information
vinz243 committed Apr 15, 2018
1 parent b498908 commit 9329764
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 10 deletions.
16 changes: 16 additions & 0 deletions src/models/BaseEntity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,22 @@ export default class BaseEntity<S> {
throw err
}
}

/**
* Returns whether the specified document exists in database
* Please note that this does not compare documents
*/
async exists() {
const { name, factory } = getSlothData(this)
try {
await factory(name).get(this._id)

return true
} catch (err) {
return false
}
}

/**
* Remove a document from the database
* @returns a Promise that resolves into a boolean, true if document was removed,
Expand Down
40 changes: 30 additions & 10 deletions test/integration/Author.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,32 @@ PouchDB.plugin(require('pouchdb-adapter-memory'))
test('creates a new author from props with valid props', () => {
const grr = AuthorDatabase.create(localPouchFactory, { name: 'GRR Martin' })
expect(grr.name).toBe('GRR Martin')
expect(grr._id).toBe('authors/GRR-Martin')
expect(grr._id).toBe('authors/grr-martin')
})

test('exists returns false for a non-existing doc', async () => {
const dbName = Date.now().toString(26)
const factory = () => new PouchDB(dbName, { adapter: 'memory' })

const doc = AuthorDatabase.create(factory, { name: 'Foobar' })

expect(await doc.exists()).toBe(false)
})

test('exists returns true for existing doc', async () => {
const dbName = Date.now().toString(26)
const factory = () => new PouchDB(dbName, { adapter: 'memory' })

await AuthorDatabase.put(factory, { name: 'Foobar' })

const doc = AuthorDatabase.create(factory, { name: 'Foobar' })

expect(await doc.exists()).toBe(true)
})

test('find author by id', async () => {
const dbName = Date.now().toString(26)
const props = { _id: 'authors/GRR-Martin', name: 'GRR Martin' }
const props = { _id: 'authors/grr-martin', name: 'GRR Martin' }

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

Expand All @@ -27,10 +47,10 @@ test('find author by id', async () => {
test('isDirty returns true with updated props', () => {
const grr = AuthorDatabase.create(localPouchFactory, {
name: 'GRR Martin',
_id: 'authors/GRR-Martin'
_id: 'authors/grr-martin'
})
expect(grr.name).toBe('GRR Martin')
expect(grr._id).toBe('authors/GRR-Martin')
expect(grr._id).toBe('authors/grr-martin')
expect(grr.isDirty()).toBe(false)

grr.name = 'grr martin'
Expand All @@ -45,7 +65,7 @@ test('save creates, update and eventually remove old document', async () => {
const factory = () => new PouchDB(dbName, { adapter: 'memory' })

const doc = await AuthorDatabase.create(factory, props)
const originalId = 'authors/GRR-Martin'
const originalId = 'authors/grr-martin'

expect(doc._id).toBe(originalId)

Expand All @@ -69,7 +89,7 @@ test('save creates, update and eventually remove old document', async () => {

{
doc.name = 'George RR Martin'
const newId = 'authors/George-RR-Martin'
const newId = 'authors/george-rr-martin'
expect(doc._id)

const { _rev, _id } = await doc.save()
Expand All @@ -86,7 +106,7 @@ test('save creates, update and eventually remove old document', async () => {
expect(newDoc).toMatchObject({
name: 'George RR Martin',
age: 70,
_id: 'authors/George-RR-Martin'
_id: 'authors/george-rr-martin'
})
}
})
Expand All @@ -98,7 +118,7 @@ test('save creates, update and eventually remove old document', async () => {
const factory = () => new PouchDB(dbName, { adapter: 'memory' })

const doc = await AuthorDatabase.create(factory, props)
const originalId = 'authors/GRR-Martin'
const originalId = 'authors/grr-martin'

expect(doc._id).toBe(originalId)

Expand All @@ -122,7 +142,7 @@ test('save creates, update and eventually remove old document', async () => {

{
doc.name = 'George RR Martin'
const newId = 'authors/George-RR-Martin'
const newId = 'authors/george-rr-martin'
expect(doc._id)

const { _rev, _id } = await doc.save()
Expand All @@ -139,7 +159,7 @@ test('save creates, update and eventually remove old document', async () => {
expect(newDoc).toMatchObject({
name: 'George RR Martin',
age: 70,
_id: 'authors/George-RR-Martin'
_id: 'authors/george-rr-martin'
})
}
})

0 comments on commit 9329764

Please sign in to comment.