Skip to content

Commit

Permalink
feat: add Model::schema_types::<params|result>
Browse files Browse the repository at this point in the history
  • Loading branch information
andykais committed Jul 8, 2024
1 parent b3eab6e commit fd7507b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/model.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -16,6 +16,7 @@ interface ModelClass {
// new (torm: TormBase<Driver>, options: ModelOptions): ModelBase
new (torm: TormBase<Driver>): ModelBase
}

interface ModelInstance {
prepare_queries: (driver?: Driver) => void
}
Expand Down Expand Up @@ -171,7 +172,7 @@ const WithStaticSchema =
<Class extends Constructor>(base: Class) =>
<T extends SchemaInputGeneric>(table_name: string, schema_input: T) => {
return class IncludingStaticSchema extends base {
static schema = schema(table_name, schema_input)
static schema_types: InferTypes<T>
static params = IncludingStaticSchema.schema.params
static result = IncludingStaticSchema.schema.result
}
Expand Down
16 changes: 16 additions & 0 deletions src/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ import { ParamsField, ResultField } from './query.ts'
import type { FieldDefinition } from './field.ts'

export type SchemaInputGeneric = Record<string, FieldDefinition<any, any>>

export type SchemaField = {
table_name: string
field_name: string
data_transformers: FieldDefinition<any, any>
}

export type SchemaFieldGeneric = ParamsField<SchemaField> | ResultField<SchemaField>

export type SchemaGeneric = {
Expand Down Expand Up @@ -42,6 +44,19 @@ export type SchemaResult<T extends SchemaInputGeneric> =
}


export type InferTypes<T extends SchemaInputGeneric> = {
result: {
[K in Extract<keyof T, string>]: T[K] extends FieldDefinition<infer In, any>
? In
: never
}
params: {
[K in Extract<keyof T, string>]: T[K] extends FieldDefinition<any, infer Out>
? Out
: never
}
}

export interface SchemaOutput<T extends SchemaInputGeneric> {
params: SchemaParams<T>
result: SchemaResult<T>
Expand All @@ -68,6 +83,7 @@ function schema<T extends SchemaInputGeneric>(table_name: string, schema: T): Sc
;(built_result_schema['*'] as any) = Object.values(built_result_schema)

return {
schema,
params: built_params_schema as SchemaParams<T>,
result: built_result_schema as SchemaResult<T>,
}
Expand Down

0 comments on commit fd7507b

Please sign in to comment.