SQL to Objection.js Model Generator

Convert SQL CREATE TABLE statements to Objection.js ORM model classes with TypeScript types, JSON schema validation, and relation mappings.

What is SQL to Objection.js Model Generator?

Objection.js is a SQL-friendly ORM for Node.js built on top of the Knex.js query builder. Unlike ORMs that abstract away SQL, Objection.js embraces it — you write queries using a fluent chainable API that stays close to the underlying SQL, while gaining model-layer features like relations, eager loading, transactions, and JSON schema validation. This converter takes SQL CREATE TABLE statements and produces Objection.js model classes with static tableName properties, TypeScript type declarations, optional JSON Schema definitions for input validation, and relation mappings derived from foreign key references. The result is production-ready model code that integrates seamlessly with any Knex-supported database.

How to Use

  1. Select your database dialect — PostgreSQL, MySQL, or SQLite — to inform type mapping decisions
  2. Enable or disable TypeScript output, JSON Schema validation, and relation mapping generation using the checkboxes
  3. Paste your SQL CREATE TABLE statements into the left editor panel
  4. Click Generate Objection.js Models to produce model class definitions for each table
  5. Copy the output into your Node.js project and register the models with your Knex instance

Why Use This Tool?

Generates Objection.js models with static tableName and optional idColumn, matching the framework conventions exactly
JSON Schema definitions enable automatic input validation before data reaches the database, catching errors early
Foreign key references are automatically converted to BelongsToOneRelation mappings with proper join configurations
TypeScript output includes typed property declarations so your models get full IDE autocompletion and compile-time checking
The generated code works with any Knex-supported database — switch dialects without rewriting your models

Tips & Best Practices

  • Objection.js infers the primary key column as id by default — if your table uses a different column name, the converter adds a static idColumn property automatically
  • Enable JSON Schema generation to get runtime validation for create and update operations — Objection.js validates input before sending queries to the database
  • For many-to-many relationships, you will need to add ManyToManyRelation mappings manually since SQL foreign keys only express one-directional references
  • Nullable columns (without NOT NULL) are generated with optional TypeScript types using the ? modifier, matching how Objection.js handles undefined values

Frequently Asked Questions

How are SQL types mapped to TypeScript types in Objection.js models?

The converter maps SQL types to TypeScript types that align with what Knex.js returns from queries: INTEGER, BIGINT, and SERIAL become number; VARCHAR, CHAR, and TEXT become string; BOOLEAN becomes boolean; TIMESTAMP and DATE become string (since Knex returns ISO date strings by default); JSON and JSONB become object; and UUID becomes string.

When should I choose Objection.js over other Node.js ORMs?

Choose Objection.js when you want an ORM that stays close to SQL rather than hiding it. It is ideal for projects that need complex joins, raw SQL fallbacks, or database-specific features. If you prefer a fully abstracted ORM with auto-migrations and decorators, TypeORM or Prisma may be better choices. Objection.js pairs especially well with PostgreSQL thanks to its JSON schema and relation support.

Is my SQL data sent to a server?

No. All parsing and code generation runs entirely in your browser. Your SQL schema never leaves your device, and no network requests are made during the conversion process.

What is the jsonSchema property in Objection.js models?

The jsonSchema static property defines a JSON Schema object that Objection.js uses to validate input data before database operations. When enabled, the converter generates schema definitions with type, required, and properties for each column, including format hints like date-time for timestamps and uuid for UUID columns.

How do relation mappings work in Objection.js?

Relation mappings define how models relate to each other. The converter generates BelongsToOneRelation mappings for foreign key references, specifying the join from the foreign key column to the referenced table column. You can then use eager loading with withRelated to fetch related records in a single query.

Real-world Examples

Building a blog API with author-post relationships

A blogging platform needs Objection.js models where posts reference authors via a foreign key, with JSON Schema validation for input and BelongsToOneRelation for eager loading.

Input
CREATE TABLE authors (
  id SERIAL PRIMARY KEY,
  name VARCHAR(100) NOT NULL,
  email VARCHAR(255) UNIQUE NOT NULL
);

CREATE TABLE posts (
  id SERIAL PRIMARY KEY,
  author_id INTEGER REFERENCES authors(id),
  title VARCHAR(255) NOT NULL,
  published BOOLEAN DEFAULT FALSE
);
Output
export class Authors extends Model {
  static tableName = 'authors';
  name: string;
  email: string;
  static relationMappings = {
    posts: { relation: Model.HasManyRelation, modelClass: Posts, join: { from: 'authors.id', to: 'posts.author_id' } }
  };
}

Generating validated models for a SaaS multi-tenant application

A SaaS application needs Objection.js models with JSON Schema validation to ensure tenant isolation and data integrity before any database write operation.

Input
CREATE TABLE tenants (
  id SERIAL PRIMARY KEY,
  name VARCHAR(100) NOT NULL,
  settings JSONB
);

CREATE TABLE users (
  id SERIAL PRIMARY KEY,
  tenant_id INTEGER NOT NULL REFERENCES tenants(id),
  email VARCHAR(255) NOT NULL
);
Output
export class Tenants extends Model {
  static tableName = 'tenants';
  static jsonSchema = {
    type: 'object',
    required: ['name'],
    properties: { id: { type: 'integer' }, name: { type: 'string' }, settings: { type: 'object', nullable: true } }
  };
}

Related Tools