objection-annotations

Decorator-based for objection models to generate jsonSchema and create table.

Usage no npm install needed!

<script type="module">
  import objectionAnnotations from 'https://cdn.skypack.dev/objection-annotations';
</script>

README

objection-annotations

version npm

Installation

npm i --save objection-annotations

Example

import Knex = require('knex');
import { Model } from 'objection';

import { buildTable, Column, Join, Reference, Table } from 'objection-annotations';

@Table('people', { indexes: [['firstName', 'lastName'], 'lastName'] })
export class Person extends Model {
    @Column('increments')
    id: number;

    @Column('string', { schema: { maxLength: 16 } })
    firstName: string;

    @Column('string', { schema: { maxLength: 16 } })
    lastName: string;

    @Column('string', { schema: { maxLength: 20 }, unique: true })
    phoneNumber: string;

    @Join(() => Todo, 'hasMany', 'personId')
    todo_list: Todo[];
}

@Table('todo_list')
export class Todo extends Model {
    @Column('increments')
    id: number;

    @Column('integer', { index: true })
    @Reference(Person)
    personId: number;

    @Column('date', { index: true })
    expire: Date;

    @Column('text')
    content: string;

    @Join(() => Person, 'belongsTo', 'personId')
    person: Person;
}

(async () => {
    await buildTable(Knex(require('./knexfile.js')), Person, Todo);
});