import type { Horizon } from '../common.js' import type { Entity } from '../common.js' export type UpsertInput = { entityType: string entityId: string entityTTL?: number horizon?: Horizon namespace?: string } & T export type UpsertOptions = { fetchOnStale?: boolean } export type GetOptions = { entityType: string entityId: string namespace?: string cache?: | boolean | { prefix?: string ttlSeconds?: number } } export type QueryOptions = { sql: string } export const database = { /** * Creates or updates an entity based on type and ID in your namespace. * Returns the entity that was created or updated, if entity is not upserted due to stale data (lower horizon), * it returns your (stale) input as entity, unless options.fetchOnStale is set to true. If you do not specify * a namespace, the default namespace is used. * * @param input * @param options * @returns */ upsert: async ( input: UpsertInput< Omit, 'entityId' | 'entityType'> & { entityId: string entityType: string horizon?: Horizon } >, options: UpsertOptions = {}, ): Promise> => { input options throw new Error( 'database.upsert() is a native function implemented by the platform runtime', ) }, /** * Gets an entity based on type and ID in your namespace. If you do not specify a namespace, the default namespace is used. * * @param options * @returns */ get: async (options: GetOptions): Promise | null> => { options throw new Error( 'database.applyCounters() is a native function implemented by the platform runtime', ) }, /** * Runs a SQL query against the internal indexer store (not destination sinks). * Note that this operation can be slow and should be used sparingly and only in low traffic scenarios, * such as ocessional reorgs, one-off manual enrichment triggers, etc. * Current soft-limit timeout is 30 seconds, if you need higher limits probably query is not efficient, * consult with our engineers for better alternatives. * * @param options * @returns TRow[] */ query: async >( options: QueryOptions, ): Promise => { options throw new Error( 'database.query() is a native function implemented by the platform runtime', ) }, }