From fd7507b03b0d8cdb5b73f5a1c702f414f93b82c0 Mon Sep 17 00:00:00 2001 From: Andrew Kaiser Date: Mon, 8 Jul 2024 08:46:20 -0400 Subject: [PATCH] feat: add Model::schema_types:: --- src/model.ts | 5 +++-- src/schema.ts | 16 ++++++++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/src/model.ts b/src/model.ts index 004d792..659ed96 100644 --- a/src/model.ts +++ b/src/model.ts @@ -1,7 +1,7 @@ import { ParamsField, ResultField } from './query.ts' import { schema } from './schema.ts' import type { Driver, Constructor } from './util.ts' -import type { BuiltSchemaField, SchemaGeneric, SchemaInputGeneric } from './schema.ts' +import type { BuiltSchemaField, SchemaGeneric, SchemaInputGeneric, InferTypes } from './schema.ts' import type { Statement, StatementParams, StatementResult } from './statement.ts' import type { SqlTemplateArg, RawSqlInterpolationValues } from './query.ts' import type { MigrationClass } from './migration.ts' @@ -16,6 +16,7 @@ interface ModelClass { // new (torm: TormBase, options: ModelOptions): ModelBase new (torm: TormBase): ModelBase } + interface ModelInstance { prepare_queries: (driver?: Driver) => void } @@ -171,7 +172,7 @@ const WithStaticSchema = (base: Class) => (table_name: string, schema_input: T) => { return class IncludingStaticSchema extends base { - static schema = schema(table_name, schema_input) + static schema_types: InferTypes static params = IncludingStaticSchema.schema.params static result = IncludingStaticSchema.schema.result } diff --git a/src/schema.ts b/src/schema.ts index 9b32dd2..6025dcc 100644 --- a/src/schema.ts +++ b/src/schema.ts @@ -3,11 +3,13 @@ import { ParamsField, ResultField } from './query.ts' import type { FieldDefinition } from './field.ts' export type SchemaInputGeneric = Record> + export type SchemaField = { table_name: string field_name: string data_transformers: FieldDefinition } + export type SchemaFieldGeneric = ParamsField | ResultField export type SchemaGeneric = { @@ -42,6 +44,19 @@ export type SchemaResult = } +export type InferTypes = { + result: { + [K in Extract]: T[K] extends FieldDefinition + ? In + : never + } + params: { + [K in Extract]: T[K] extends FieldDefinition + ? Out + : never + } +} + export interface SchemaOutput { params: SchemaParams result: SchemaResult @@ -68,6 +83,7 @@ function schema(table_name: string, schema: T): Sc ;(built_result_schema['*'] as any) = Object.values(built_result_schema) return { + schema, params: built_params_schema as SchemaParams, result: built_result_schema as SchemaResult, }