|
| 1 | +import { and, eq } from 'drizzle-orm' |
| 2 | +import { kCreateWithDocId, kSelect } from './datatype/index.js' |
| 3 | +import { hashObject } from './utils.js' |
| 4 | +import { NotFoundError } from './errors.js' |
| 5 | + |
| 6 | +export const ktranslatedLanguageCodeToSchemaNames = Symbol( |
| 7 | + 'translatedLanguageCodeToSchemaNames' |
| 8 | +) |
| 9 | +export default class TranslationApi { |
| 10 | + /** @type {Map< |
| 11 | + * import('@mapeo/schema').TranslationValue['languageCode'], |
| 12 | + * Set<import('@mapeo/schema/dist/types.js').SchemaName>>} */ |
| 13 | + #translatedLanguageCodeToSchemaNames = new Map() |
| 14 | + #dataType |
| 15 | + #table |
| 16 | + #indexPromise |
| 17 | + |
| 18 | + /** |
| 19 | + * @param {Object} opts |
| 20 | + * @param {import('./datatype/index.js').DataType< |
| 21 | + * import('./datastore/index.js').DataStore<'config'>, |
| 22 | + * typeof import('./schema/project.js').translationTable, |
| 23 | + * 'translation', |
| 24 | + * import('@mapeo/schema').Translation, |
| 25 | + * import('@mapeo/schema').TranslationValue |
| 26 | + * >} opts.dataType |
| 27 | + * @param {typeof import('./schema/project.js').translationTable} opts.table |
| 28 | + */ |
| 29 | + constructor({ dataType, table }) { |
| 30 | + this.#dataType = dataType |
| 31 | + this.#table = table |
| 32 | + this.#indexPromise = this.#dataType |
| 33 | + .getMany() |
| 34 | + .then((docs) => { |
| 35 | + docs.map((doc) => this.index(doc)) |
| 36 | + }) |
| 37 | + .catch((err) => { |
| 38 | + throw new Error(`error loading Translation cache: ${err}`) |
| 39 | + }) |
| 40 | + } |
| 41 | + |
| 42 | + /** @returns {Promise<void>} */ |
| 43 | + ready() { |
| 44 | + return this.#indexPromise |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * @param {import('@mapeo/schema').TranslationValue} value |
| 49 | + */ |
| 50 | + async put(value) { |
| 51 | + /* eslint-disable no-unused-vars */ |
| 52 | + const { message, ...identifiers } = value |
| 53 | + const docId = hashObject(identifiers) |
| 54 | + try { |
| 55 | + const doc = await this.#dataType.getByDocId(docId) |
| 56 | + return await this.#dataType.update(doc.versionId, value) |
| 57 | + } catch (e) { |
| 58 | + if (e instanceof NotFoundError) { |
| 59 | + return await this.#dataType[kCreateWithDocId](docId, value) |
| 60 | + } else { |
| 61 | + throw new Error(`Error on translation ${e}`) |
| 62 | + } |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * @param {import('type-fest').SetOptional< |
| 68 | + * Omit<import('@mapeo/schema').TranslationValue,'schemaName' | 'message'>, |
| 69 | + * 'fieldRef' | 'regionCode'>} value |
| 70 | + * @returns {Promise<import('@mapeo/schema').Translation[]>} |
| 71 | + */ |
| 72 | + async get(value) { |
| 73 | + await this.ready() |
| 74 | + |
| 75 | + const docTypeIsTranslatedToLanguage = |
| 76 | + this.#translatedLanguageCodeToSchemaNames |
| 77 | + .get(value.languageCode) |
| 78 | + ?.has( |
| 79 | + /** @type {import('@mapeo/schema/dist/types.js').SchemaName} */ ( |
| 80 | + value.schemaNameRef |
| 81 | + ) |
| 82 | + ) |
| 83 | + if (!docTypeIsTranslatedToLanguage) return [] |
| 84 | + |
| 85 | + const filters = [ |
| 86 | + eq(this.#table.docIdRef, value.docIdRef), |
| 87 | + eq(this.#table.schemaNameRef, value.schemaNameRef), |
| 88 | + eq(this.#table.languageCode, value.languageCode), |
| 89 | + ] |
| 90 | + if (value.fieldRef) { |
| 91 | + filters.push(eq(this.#table.fieldRef, value.fieldRef)) |
| 92 | + } |
| 93 | + |
| 94 | + if (value.regionCode) { |
| 95 | + filters.push(eq(this.#table.regionCode, value.regionCode)) |
| 96 | + } |
| 97 | + |
| 98 | + return (await this.#dataType[kSelect]()) |
| 99 | + .where(and.apply(null, filters)) |
| 100 | + .prepare() |
| 101 | + .all() |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * @param {import('@mapeo/schema').TranslationValue} doc |
| 106 | + */ |
| 107 | + index(doc) { |
| 108 | + let translatedSchemas = this.#translatedLanguageCodeToSchemaNames.get( |
| 109 | + doc.languageCode |
| 110 | + ) |
| 111 | + if (!translatedSchemas) { |
| 112 | + translatedSchemas = new Set() |
| 113 | + this.#translatedLanguageCodeToSchemaNames.set( |
| 114 | + doc.languageCode, |
| 115 | + translatedSchemas |
| 116 | + ) |
| 117 | + } |
| 118 | + translatedSchemas.add( |
| 119 | + /** @type {import('@mapeo/schema/dist/types.js').SchemaName} */ ( |
| 120 | + doc.schemaNameRef |
| 121 | + ) |
| 122 | + ) |
| 123 | + } |
| 124 | + |
| 125 | + // This should only be used by tests. |
| 126 | + get [ktranslatedLanguageCodeToSchemaNames]() { |
| 127 | + return this.#translatedLanguageCodeToSchemaNames |
| 128 | + } |
| 129 | +} |
0 commit comments