Skip to content

Commit

Permalink
getByVersionIdIfExists
Browse files Browse the repository at this point in the history
  • Loading branch information
EvanHahn committed Nov 18, 2024
1 parent 12b21a2 commit c824e2d
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 19 deletions.
36 changes: 28 additions & 8 deletions src/datatype/get-if-exists.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,20 @@ import { NotFoundError } from '../errors.js'
/** @import { MapeoDocMap, MapeoValueMap } from '../types.js' */
/** @import { DataType, MapeoDocTables } from './index.js' */

/**
* @template T
* @param {() => PromiseLike<T>} fn
* @returns {Promise<null | T>}
*/
async function nullIfNotFound(fn) {
try {
return await fn()
} catch (err) {
if (err instanceof NotFoundError) return null
throw err
}
}

/**
* @template {MapeoDocTables} TTable
* @template {TTable['_']['name']} TSchemaName
Expand All @@ -12,11 +26,17 @@ import { NotFoundError } from '../errors.js'
* @param {string} docId
* @returns {Promise<null | TDoc & { forks: string[] }>}
*/
export async function getByDocIdIfExists(dataType, docId) {
try {
return await dataType.getByDocId(docId)
} catch (err) {
if (err instanceof NotFoundError) return null
throw err
}
}
export const getByDocIdIfExists = (dataType, docId) =>
nullIfNotFound(() => dataType.getByDocId(docId))

/**
* @template {MapeoDocTables} TTable
* @template {TTable['_']['name']} TSchemaName
* @template {MapeoDocMap[TSchemaName]} TDoc
* @template {MapeoValueMap[TSchemaName]} TValue
* @param {DataType<DataStore, TTable, TSchemaName, TDoc, TValue>} dataType
* @param {string} versionId
* @returns {Promise<null | TDoc>}
*/
export const getByVersionIdIfExists = (dataType, versionId) =>
nullIfNotFound(() => dataType.getByVersionId(versionId))
15 changes: 6 additions & 9 deletions src/roles.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ import { currentSchemaVersions, parseVersionId } from '@comapeo/schema'
import mapObject from 'map-obj'
import { kCreateWithDocId, kDataStore } from './datatype/index.js'
import { assert, setHas } from './utils.js'
import { NotFoundError } from './errors.js'
import { getByDocIdIfExists } from './datatype/get-if-exists.js'
import {
getByDocIdIfExists,
getByVersionIdIfExists,
} from './datatype/get-if-exists.js'
import { TypedEmitter } from 'tiny-typed-emitter'
/** @import { Role as RoleRecord } from '@comapeo/schema' */
/** @import { ReadonlyDeep } from 'type-fest' */
Expand Down Expand Up @@ -417,13 +419,8 @@ export class Roles extends TypedEmitter {
* @param {string} versionId
* @returns {Promise<null | RoleRecord>}
*/
async #getRoleRecordByVersionId(versionId) {
try {
return await this.#dataType.getByVersionId(versionId)
} catch (err) {
if (err instanceof NotFoundError) return null
throw err
}
#getRoleRecordByVersionId(versionId) {
return getByVersionIdIfExists(this.#dataType, versionId)
}

/**
Expand Down
29 changes: 27 additions & 2 deletions test/data-type/get-if-exists.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import { testenv } from './test-helpers.js'
import test, { describe } from 'node:test'
import assert from 'node:assert/strict'
import { getByDocIdIfExists } from '../../src/datatype/get-if-exists.js'
import { valueOf } from '@comapeo/schema'
import {
getByDocIdIfExists,
getByVersionIdIfExists,
} from '../../src/datatype/get-if-exists.js'
import { getVersionId, parseVersionId, valueOf } from '@comapeo/schema'
import { generate } from '@mapeo/mock-data'

describe('getByDocIdIfExists', () => {
Expand All @@ -18,3 +21,25 @@ describe('getByDocIdIfExists', () => {
assert(await getByDocIdIfExists(dataType, observation.docId))
})
})

describe('getByVersionIdIfExists', () => {
test('resolves with null if no document exists with that ID', async () => {
const { dataType } = await testenv()

const fixture = valueOf(generate('observation')[0])
const observation = await dataType.create(fixture)
const bogusVersionId = getVersionId({
...parseVersionId(observation.versionId),
index: 9999999,
})

assert.equal(await getByVersionIdIfExists(dataType, bogusVersionId), null)
})

test('resolves with the document if it exists', async () => {
const { dataType } = await testenv()
const fixture = valueOf(generate('observation')[0])
const observation = await dataType.create(fixture)
assert(await getByVersionIdIfExists(dataType, observation.versionId))
})
})

0 comments on commit c824e2d

Please sign in to comment.