Skip to content

Commit

Permalink
revert: remove getByDocId optional parameter (#968)
Browse files Browse the repository at this point in the history
This reverts commit 56f7e18 and
replaces it with an internal-only function, `nullIfNotFound`.
  • Loading branch information
EvanHahn authored Nov 21, 2024
1 parent 3d1c94b commit fe6fc8c
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 25 deletions.
9 changes: 9 additions & 0 deletions src/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,12 @@ export class NotFoundError extends Error {
super(message)
}
}

/**
* @param {unknown} err
* @returns {null}
*/
export function nullIfNotFound(err) {
if (err instanceof NotFoundError) return null
throw err
}
14 changes: 7 additions & 7 deletions src/mapeo-project.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ import { Logger } from './logger.js'
import { IconApi } from './icon-api.js'
import { readConfig } from './config-import.js'
import TranslationApi from './translation-api.js'
import { NotFoundError } from './errors.js'
import { NotFoundError, nullIfNotFound } from './errors.js'
import { getErrorCode, getErrorMessage } from './lib/error.js'
/** @import { ProjectSettingsValue } from '@comapeo/schema' */
/** @import { CoreStorage, BlobFilter, BlobStoreEntriesStream, KeyPair, Namespace, ReplicationStream } from './types.js' */
Expand Down Expand Up @@ -663,9 +663,9 @@ export class MapeoProject extends TypedEmitter {
async $setProjectSettings(settings) {
const { projectSettings } = this.#dataTypes

const existing = await projectSettings.getByDocId(this.#projectId, {
mustBeFound: false,
})
const existing = await projectSettings
.getByDocId(this.#projectId)
.catch(nullIfNotFound)

if (existing) {
return extractEditableProjectSettings(
Expand Down Expand Up @@ -773,9 +773,9 @@ export class MapeoProject extends TypedEmitter {
schemaName: /** @type {const} */ ('deviceInfo'),
}

const existingDoc = await deviceInfo.getByDocId(configCoreId, {
mustBeFound: false,
})
const existingDoc = await deviceInfo
.getByDocId(configCoreId)
.catch(nullIfNotFound)
if (existingDoc) {
return await deviceInfo.update(existingDoc.versionId, doc)
} else {
Expand Down
17 changes: 9 additions & 8 deletions src/roles.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { currentSchemaVersions } from '@comapeo/schema'
import mapObject from 'map-obj'
import { kCreateWithDocId, kDataStore } from './datatype/index.js'
import { assert, setHas } from './utils.js'
import { nullIfNotFound } from './errors.js'
import { TypedEmitter } from 'tiny-typed-emitter'
/** @import { Namespace } from './types.js' */

Expand Down Expand Up @@ -269,10 +270,10 @@ export class Roles extends TypedEmitter {
* @returns {Promise<Role>}
*/
async getRole(deviceId) {
const roleAssignment = await this.#dataType.getByDocId(deviceId, {
mustBeFound: false,
})
if (!roleAssignment) {
const roleRecord = await this.#dataType
.getByDocId(deviceId)
.catch(nullIfNotFound)
if (!roleRecord) {
// The project creator will have the creator role
const authCoreId = await this.#coreOwnership.getCoreId(deviceId, 'auth')
if (authCoreId === this.#projectCreatorAuthCoreId) {
Expand All @@ -284,7 +285,7 @@ export class Roles extends TypedEmitter {
}
}

const { roleId } = roleAssignment
const { roleId } = roleRecord
if (!isRoleId(roleId)) {
return BLOCKED_ROLE
}
Expand Down Expand Up @@ -386,9 +387,9 @@ export class Roles extends TypedEmitter {
}
}

const existingRoleDoc = await this.#dataType.getByDocId(deviceId, {
mustBeFound: false,
})
const existingRoleDoc = await this.#dataType
.getByDocId(deviceId)
.catch(nullIfNotFound)

if (existingRoleDoc) {
await this.#dataType.update(
Expand Down
3 changes: 2 additions & 1 deletion src/translation-api.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { and, sql } from 'drizzle-orm'
import { kCreateWithDocId, kSelect } from './datatype/index.js'
import { hashObject } from './utils.js'
import { nullIfNotFound } from './errors.js'
import { omit } from './lib/omit.js'
/** @import { Translation, TranslationValue } from '@comapeo/schema' */
/** @import { SetOptional } from 'type-fest' */
Expand Down Expand Up @@ -49,7 +50,7 @@ export default class TranslationApi {
async put(value) {
const identifiers = omit(value, ['message'])
const docId = hashObject(identifiers)
const doc = await this.#dataType.getByDocId(docId, { mustBeFound: false })
const doc = await this.#dataType.getByDocId(docId).catch(nullIfNotFound)
if (doc) {
return await this.#dataType.update(doc.versionId, value)
} else {
Expand Down
8 changes: 0 additions & 8 deletions test/data-type.js
Original file line number Diff line number Diff line change
Expand Up @@ -209,14 +209,6 @@ test('getByDocId() throws if no document exists with that ID', async () => {
await assert.rejects(() => dataType.getByDocId('foo bar'), NotFoundError)
})

test('getByDocId() can return null if no document exists with that ID', async () => {
const { dataType } = await testenv({ projectKey: randomBytes(32) })
assert.equal(
await dataType.getByDocId('foo bar', { mustBeFound: false }),
null
)
})

test('delete()', async () => {
const projectKey = randomBytes(32)
const { dataType } = await testenv({ projectKey })
Expand Down
12 changes: 11 additions & 1 deletion test/errors.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import test, { describe } from 'node:test'
import assert from 'node:assert/strict'
import { NotFoundError } from '../src/errors.js'
import { NotFoundError, nullIfNotFound } from '../src/errors.js'

describe('NotFoundError', () => {
test('subclasses Error', () => {
Expand All @@ -15,3 +15,13 @@ describe('NotFoundError', () => {
assert.equal(new NotFoundError('foo').message, 'foo')
})
})

describe('nullIfNotFound', () => {
test('returns null if passed a NotFoundError', () => {
assert.equal(nullIfNotFound(new NotFoundError()), null)
})

test('throws if passed something other than a NotFoundError', () => {
assert.throws(() => nullIfNotFound(new Error('foo')), { message: 'foo' })
})
})

0 comments on commit fe6fc8c

Please sign in to comment.