diff --git a/src/datatype/index.d.ts b/src/datatype/index.d.ts index f7f36a8a..39922b26 100644 --- a/src/datatype/index.d.ts +++ b/src/datatype/index.d.ts @@ -18,7 +18,7 @@ import TranslationApi from '../translation-api.js' type MapeoDocTableName = `${MapeoDoc['schemaName']}Table` type GetMapeoDocTables = T[keyof T & MapeoDocTableName] /** Union of Drizzle schema tables that correspond to MapeoDoc types (e.g. excluding backlink tables and other utility tables) */ -export type MapeoDocTables = +type MapeoDocTables = | GetMapeoDocTables | GetMapeoDocTables type MapeoDocTablesMap = { @@ -87,8 +87,12 @@ export class DataType< getByDocId( docId: string, - opts?: { lang?: string } + opts?: { mustBeFound?: true; lang?: string } ): Promise + getByDocId( + docId: string, + opts?: { mustBeFound?: boolean; lang?: string } + ): Promise getByVersionId(versionId: string, opts?: { lang?: string }): Promise diff --git a/src/datatype/index.js b/src/datatype/index.js index 82459ca2..1784987c 100644 --- a/src/datatype/index.js +++ b/src/datatype/index.js @@ -164,16 +164,30 @@ export class DataType extends TypedEmitter { } /** + * @overload * @param {string} docId - * @param {{ lang?: string }} [opts] + * @param {object} [options] + * @param {true} [options.mustBeFound] + * @param {string} [options.lang] + * @returns {Promise} + */ + /** + * @param {string} docId + * @param {object} [options] + * @param {boolean} [options.mustBeFound] + * @param {string} [options.lang] + * @returns {Promise} */ - async getByDocId(docId, { lang } = {}) { + async getByDocId(docId, { mustBeFound = true, lang } = {}) { await this.#dataStore.indexer.idle() - const result = /** @type {undefined | MapeoDoc} */ ( - this.#sql.getByDocId.get({ docId }) - ) - if (!result) throw new NotFoundError() - return this.#translate(deNullify(result), { lang }) + const result = this.#sql.getByDocId.get({ docId }) + if (result) { + return this.#translate(deNullify(result), { lang }) + } else if (mustBeFound) { + throw new NotFoundError() + } else { + return null + } } /** @@ -186,7 +200,7 @@ export class DataType extends TypedEmitter { } /** - * @param {MapeoDoc} doc + * @param {any} doc * @param {{ lang?: string }} [opts] */ async #translate(doc, { lang } = {}) { @@ -278,7 +292,6 @@ export class DataType extends TypedEmitter { const doc = { ...existingDoc, updatedAt: new Date().toISOString(), - // @ts-expect-error - TS just doesn't work in this class links: [existingDoc.versionId, ...existingDoc.forks], deleted: true, }