Skip to content

Commit

Permalink
More reversions
Browse files Browse the repository at this point in the history
  • Loading branch information
EvanHahn committed Nov 20, 2024
1 parent 1d9432f commit 7cd7e70
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 11 deletions.
8 changes: 6 additions & 2 deletions src/datatype/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import TranslationApi from '../translation-api.js'
type MapeoDocTableName = `${MapeoDoc['schemaName']}Table`
type GetMapeoDocTables<T> = 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<typeof import('../schema/project.js')>
| GetMapeoDocTables<typeof import('../schema/client.js')>
type MapeoDocTablesMap = {
Expand Down Expand Up @@ -87,8 +87,12 @@ export class DataType<

getByDocId(
docId: string,
opts?: { lang?: string }
opts?: { mustBeFound?: true; lang?: string }
): Promise<TDoc & { forks: string[] }>
getByDocId(
docId: string,
opts?: { mustBeFound?: boolean; lang?: string }
): Promise<null | (TDoc & { forks: string[] })>

getByVersionId(versionId: string, opts?: { lang?: string }): Promise<TDoc>

Expand Down
31 changes: 22 additions & 9 deletions src/datatype/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<TDoc & { forks: string[] }>}
*/
/**
* @param {string} docId
* @param {object} [options]
* @param {boolean} [options.mustBeFound]
* @param {string} [options.lang]
* @returns {Promise<null | (TDoc & { forks: string[] })>}
*/
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
}
}

/**
Expand All @@ -186,7 +200,7 @@ export class DataType extends TypedEmitter {
}

/**
* @param {MapeoDoc} doc
* @param {any} doc
* @param {{ lang?: string }} [opts]
*/
async #translate(doc, { lang } = {}) {
Expand Down Expand Up @@ -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,
}
Expand Down

0 comments on commit 7cd7e70

Please sign in to comment.