UNPKG

@proofkit/fmodata

Version:
99 lines (98 loc) 4.7 kB
import { StandardSchemaV1 } from '@standard-schema/spec'; import { ExecutionContext, ExecutableBuilder, Metadata } from '../types.js'; import { BaseTable } from './base-table.js'; import { TableOccurrence } from './table-occurrence.js'; import { EntitySet } from './entity-set.js'; import { BatchBuilder } from './batch-builder.js'; import { SchemaManager } from './schema-manager.js'; type ExtractSchemaFromOccurrence<O> = O extends TableOccurrence<infer BT, any, any, any> ? BT extends BaseTable<infer S, any, any, any> ? S : never : never; type FindOccurrenceByName<Occurrences extends readonly TableOccurrence<any, any, any, any>[], Name extends string> = Occurrences extends readonly [ infer First, ...infer Rest extends readonly TableOccurrence<any, any, any, any>[] ] ? First extends TableOccurrence<any, any, any, any> ? First["name"] extends Name ? First : FindOccurrenceByName<Rest, Name> : never : never; type ExtractOccurrenceNames<Occurrences extends readonly TableOccurrence<any, any, any, any>[]> = Occurrences extends readonly [] ? string : Occurrences[number]["name"]; export declare class Database<Occurrences extends readonly TableOccurrence<any, any, any, any>[] = readonly []> { private readonly databaseName; private readonly context; private occurrenceMap; private _useEntityIds; readonly schema: SchemaManager; constructor(databaseName: string, context: ExecutionContext, config?: { occurrences?: Occurrences | undefined; /** * Whether to use entity IDs instead of field names in the actual requests to the server * Defaults to true if all occurrences use entity IDs, false otherwise * If set to false but some occurrences do not use entity IDs, an error will be thrown */ useEntityIds?: boolean; }); /** * Returns true if any table occurrence in this database is using entity IDs. */ isUsingEntityIds(): boolean; /** * Gets a table occurrence by name. * @internal */ getOccurrence(name: string): TableOccurrence<any, any, any, any> | undefined; from<Name extends ExtractOccurrenceNames<Occurrences> | (string & {})>(name: Name): Occurrences extends readonly [] ? EntitySet<Record<string, StandardSchemaV1>, undefined> : Name extends ExtractOccurrenceNames<Occurrences> ? EntitySet<ExtractSchemaFromOccurrence<FindOccurrenceByName<Occurrences, Name>>, FindOccurrenceByName<Occurrences, Name>> : EntitySet<Record<string, StandardSchemaV1>, undefined>; /** * Retrieves the OData metadata for this database. * @param args Optional configuration object * @param args.format The format to retrieve metadata in. Defaults to "json". * @returns The metadata in the specified format */ getMetadata(args: { format: "xml"; }): Promise<string>; getMetadata(args?: { format?: "json"; }): Promise<Metadata>; /** * Lists all available tables (entity sets) in this database. * @returns Promise resolving to an array of table names */ listTableNames(): Promise<string[]>; /** * Executes a FileMaker script. * @param scriptName - The name of the script to execute (must be valid according to OData rules) * @param options - Optional script parameter and result schema * @returns Promise resolving to script execution result */ runScript<ResultSchema extends StandardSchemaV1<string, any> = never>(scriptName: string, options?: { scriptParam?: string | number | Record<string, any>; resultSchema?: ResultSchema; }): Promise<[ ResultSchema ] extends [never] ? { resultCode: number; result?: string; } : ResultSchema extends StandardSchemaV1<string, infer Output> ? { resultCode: number; result: Output; } : { resultCode: number; result?: string; }>; /** * Create a batch operation builder that allows multiple queries to be executed together * in a single atomic request. All operations succeed or fail together (transactional). * * @param builders - Array of executable query builders to batch * @returns A BatchBuilder that can be executed * @example * ```ts * const result = await db.batch([ * db.from('contacts').list().top(5), * db.from('users').list().top(5), * db.from('contacts').insert({ name: 'John' }) * ]).execute(); * * if (result.data) { * const [contacts, users, insertResult] = result.data; * } * ``` */ batch<const Builders extends readonly ExecutableBuilder<any>[]>(builders: Builders): BatchBuilder<Builders>; } export {};