Skip to content

Commit

Permalink
Merge branch 'main' into update-mapeo-mock-data
Browse files Browse the repository at this point in the history
  • Loading branch information
gmaclennan authored Jan 22, 2025
2 parents 913b9d8 + ae18169 commit 755b6dd
Show file tree
Hide file tree
Showing 8 changed files with 122 additions and 151 deletions.
2 changes: 0 additions & 2 deletions src/core-manager/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ export class CoreManager extends TypedEmitter {
#coreIndex
/** @type {CoreRecord} */
#creatorCoreRecord
#projectKey
#queries
#encryptionKeys
#projectExtension
Expand Down Expand Up @@ -93,7 +92,6 @@ export class CoreManager extends TypedEmitter {
this.#l = Logger.create('coreManager', logger)
const primaryKey = keyManager.getDerivedKey('primaryKey', projectKey)
this.#deviceId = keyManager.getIdentityKeypair().publicKey.toString('hex')
this.#projectKey = projectKey
this.#encryptionKeys = encryptionKeys
this.#autoDownload = autoDownload

Expand Down
112 changes: 0 additions & 112 deletions src/datatype/index.d.ts

This file was deleted.

76 changes: 57 additions & 19 deletions src/datatype/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,57 @@ import { TypedEmitter } from 'tiny-typed-emitter'
import { parse as parseBCP47 } from 'bcp-47'
import { setProperty, getProperty } from 'dot-prop'
/** @import { MapeoDoc, MapeoValue } from '@comapeo/schema' */
/** @import { MapeoDocMap, MapeoValueMap } from '../types.js' */
/** @import { RunResult } from 'better-sqlite3' */
/** @import { SQLiteSelectBase } from 'drizzle-orm/sqlite-core' */
/** @import { Exact } from 'type-fest' */
/** @import { DataStore } from '../datastore/index.js' */
/**
* @import {
* CoreOwnershipWithSignaturesValue,
* DefaultEmitterEvents,
* MapeoDocMap,
* MapeoValueMap,
* } from '../types.js'
*/

/**
* @internal
* @typedef {`${MapeoDoc['schemaName']}Table`} MapeoDocTableName
*/

/**
* @internal
* @template T
* @typedef {T[(keyof T) & MapeoDocTableName]} GetMapeoDocTables
*/

/**
* Union of Drizzle schema tables that correspond to MapeoDoc types (e.g. excluding backlink tables and other utility tables)
* @internal
* @typedef {GetMapeoDocTables<typeof import('../schema/project.js')> | GetMapeoDocTables<typeof import('../schema/client.js')>} MapeoDocTables
*/

/**
* @internal
* @typedef {{ [K in MapeoDocTables['_']['name']]: Extract<MapeoDocTables, { _: { name: K }}> }} MapeoDocTablesMap
*/

/**
* @internal
* @template T
* @template {keyof any} K
* @typedef {T extends any ? Omit<T, K> : never} OmitUnion
*/

/**
* @internal
* @template {MapeoValue} T
* @template {MapeoValue['schemaName']} S
* @typedef {Exclude<T, { schemaName: S }>} ExcludeSchema
*/

/**
* @internal
* @template {MapeoDoc} TDoc
* @typedef {object} DataTypeEvents
* @property {(docs: TDoc[]) => void} updated-docs
Expand All @@ -49,11 +77,11 @@ export const kDataStore = Symbol('dataStore')

/**
* @template {DataStore} TDataStore
* @template {TDataStore['schemas'][number]} TSchemaName
* @template {MapeoDocTablesMap[TSchemaName]} TTable
* @template {Exclude<MapeoDocMap[TSchemaName], { schemaName: 'coreOwnership' }>} TDoc
* @template {Exclude<MapeoValueMap[TSchemaName], { schemaName: 'coreOwnership' }>} TValue
* @extends {TypedEmitter<DataTypeEvents<TDoc> & import('../types.js').DefaultEmitterEvents<DataTypeEvents<TDoc>>>}
* @template {MapeoDocTables} TTable
* @template {TTable['_']['name']} TSchemaName
* @template {MapeoDocMap[TSchemaName]} TDoc
* @template {MapeoValueMap[TSchemaName]} TValue
* @extends {TypedEmitter<DataTypeEvents<TDoc> & DefaultEmitterEvents<DataTypeEvents<TDoc>>>}
*/
export class DataType extends TypedEmitter {
#dataStore
Expand Down Expand Up @@ -106,36 +134,42 @@ export class DataType extends TypedEmitter {
})
}

/** @returns {TTable} */
get [kTable]() {
return this.#table
}

/** @returns {TSchemaName} */
get schemaName() {
return this.#schemaName
}

/** @returns {TDataStore['namespace']} */
get namespace() {
return this.#dataStore.namespace
}

/** @returns {TDataStore} */
get [kDataStore]() {
return this.#dataStore
}

/**
* @template {import('type-fest').Exact<TValue, T>} T
* @template {Exact<ExcludeSchema<TValue, 'coreOwnership'>, T>} T
* @param {T} value
* @returns {Promise<TDoc & { forks: string[] }>}
*/
async create(value) {
const docId = generateId()
// @ts-expect-error - can't figure this one out, types in index.d.ts override this
// @ts-expect-error - can't figure this one out
return this[kCreateWithDocId](docId, value, { checkExisting: false })
}

/**
* @param {string} docId
* @param {TValue | import('../types.js').CoreOwnershipWithSignaturesValue} value
* @param {ExcludeSchema<TValue, 'coreOwnership'> | CoreOwnershipWithSignaturesValue} value
* @param {{ checkExisting?: boolean }} [opts] - only used internally to skip the checkExisting check when creating a document with a random ID (collisions should be too small probability to be worth checking for)
* @returns {Promise<TDoc & { forks: string[] }>}
*/
async [kCreateWithDocId](docId, value, { checkExisting = true } = {}) {
if (!validate(this.#schemaName, value)) {
Expand Down Expand Up @@ -193,15 +227,18 @@ export class DataType extends TypedEmitter {
/**
* @param {string} versionId
* @param {{ lang?: string }} [opts]
* @returns {Promise<TDoc>}
*/
async getByVersionId(versionId, { lang } = {}) {
const result = await this.#dataStore.read(versionId)
return this.#translate(result, { lang })
if (result.schemaName !== this.#schemaName) throw new NotFoundError()
return this.#translate(deNullify(result), { lang })
}

/**
* @param {any} doc
* @param {{ lang?: string }} [opts]
* @returns {Promise<TDoc & { forks: string[] }>}
*/
async #translate(doc, { lang } = {}) {
if (!lang) return doc
Expand Down Expand Up @@ -235,27 +272,27 @@ export class DataType extends TypedEmitter {
return doc
}

/** @param {{ includeDeleted?: boolean, lang?: string }} [opts] */
/**
* @param {object} opts
* @param {boolean} [opts.includeDeleted]
* @param {string} [opts.lang]
* @returns {Promise<Array<TDoc & { forks: string[] }>>}
*/
async getMany({ includeDeleted = false, lang } = {}) {
await this.#dataStore.indexer.idle()
const rows = includeDeleted
? this.#sql.getManyWithDeleted.all()
: this.#sql.getMany.all()
return await Promise.all(
rows.map(
async (doc) =>
await this.#translate(deNullify(/** @type {MapeoDoc} */ (doc)), {
lang,
})
)
rows.map((doc) => this.#translate(deNullify(doc), { lang }))
)
}

/**
*
* @template {import('type-fest').Exact<TValue, T>} T
* @template {Exact<ExcludeSchema<TValue, 'coreOwnership'>, T>} T
* @param {string | string[]} versionId
* @param {T} value
* @returns {Promise<TDoc & { forks: string[] }>}
*/
async update(versionId, value) {
await this.#dataStore.indexer.idle()
Expand All @@ -279,6 +316,7 @@ export class DataType extends TypedEmitter {

/**
* @param {string} docId
* @returns {Promise<TDoc & { forks: string[] }>}
*/
async delete(docId) {
await this.#dataStore.indexer.idle()
Expand Down
1 change: 1 addition & 0 deletions src/fastify-plugins/maps.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ export async function plugin(fastify, opts) {
if (resp && resp.status === 200) {
return reply
.headers({
'access-control-allow-origin': '*',
'cache-control': 'no-cache',
})
.redirect(url.toString())
Expand Down
13 changes: 0 additions & 13 deletions src/fastify-plugins/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,3 @@ export async function getFastifyServerAddress(server, { timeout } = {}) {

return 'http://' + addr
}

/**
* @param {Readonly<Date>} lastModified
*/
export function createStyleJsonResponseHeaders(lastModified) {
return {
'Cache-Control': 'max-age=' + 5 * 60, // 5 minutes
'Access-Control-Allow-Headers':
'Authorization, Content-Type, If-Match, If-Modified-Since, If-None-Match, If-Unmodified-Since',
'Access-Control-Allow-Origin': '*',
'Last-Modified': lastModified.toUTCString(),
}
}
3 changes: 2 additions & 1 deletion src/translation-api.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { and, sql } from 'drizzle-orm'
import { kCreateWithDocId, kSelect } from './datatype/index.js'
import { hashObject } from './utils.js'
import { deNullify, hashObject } from './utils.js'
import { nullIfNotFound } from './errors.js'
import { omit } from './lib/omit.js'
/** @import { Translation, TranslationValue } from '@comapeo/schema' */
Expand Down Expand Up @@ -101,6 +101,7 @@ export default class TranslationApi {
.where(and.apply(null, filters))
.prepare()
.all()
.map(deNullify)
}

/**
Expand Down
Loading

0 comments on commit 755b6dd

Please sign in to comment.