Skip to content

quickmodel / SchemaToModelService

Class: SchemaToModelService ​

Converts a formal schema (produced by getSchema) back into a QuickModel TypeScript class definition.

This is the inverse of QModel.getSchema():

  • getSchema(format) exports model structure → schema
  • SchemaToModelService.fromSchema(format, schema) imports schema → model class

Example ​

typescript
// Round-trip: model → JSON Schema → model class
const jsonSchema = User.getSchema('json');
const code = SchemaToModelService.fromSchema('json', jsonSchema, 'User');

// From a TypeScript interface string
const tsInterface = User.getSchema('typescript');
const code2 = SchemaToModelService.fromSchema('typescript', tsInterface, 'User');

See ​

Constructors ​

Constructor ​

new SchemaToModelService(): SchemaToModelService

Returns ​

SchemaToModelService

Methods ​

fromSchema() ​

static fromSchema<T>(format, schema, className?): string

Converts a schema (produced by getSchema) back into a QuickModel class.

Supported formats:

  • 'json' — JSON Schema Draft-07 object (Record<string, unknown>)
  • 'openapi' — OpenAPI 3.0 component schema or full document (Record<string, unknown>)
  • 'ajv' — AJV JSON Schema object, same structure as 'json'
  • 'typescript' — TypeScript interface source string
  • 'graphql' — GraphQL SDL type block string
  • 'prisma' — Prisma model block string
  • 'valibot' — Valibot v.object({…}) source string
  • 'yup' — Yup yup.object({…}) source string
  • 'typebox' — TypeBox Type.Object({…}) source string
  • 'effect-schema' — Effect Schema.Struct({…}) source string
  • 'drizzle' — Drizzle pgTable(…) source string
  • 'mongo' — Mongoose/Mongo schema object (Record<string, unknown>)
  • 'zod' — Live ZodObject<any> instance (produced by getSchema('zod'))

Type Parameters ​

T ​

T extends IFromSchemaFormat

Parameters ​

format ​

T

Schema format (must match a supported IFromSchemaFormat value)

schema ​

IFromSchemaInput<T>

The schema to convert (type depends on format)

className? ​

string

Optional class name override. Falls back to schema name or 'GeneratedModel'.

Returns ​

string

TypeScript source code string for a class extending QModel.

Throws ​

For 'openapi': when className is not found in components.schemas.

Throws ​

For 'typescript': when no interface declaration is found in the source.

Throws ​

For 'graphql': when no type block is found in the source.

Throws ​

For 'prisma': when no model block is found in the source.

Throws ​

For 'valibot': when no v.object block is found in the source.

Throws ​

For 'yup': when no yup.object block is found in the source.

Throws ​

For 'typebox': when no Type.Object block is found in the source.

Throws ​

For 'effect-schema': when no Schema.Struct block is found in the source.

Throws ​

For 'drizzle': when no pgTable block is found in the source.