2.2.0 • Published 4 years ago

objection-decorators v2.2.0

Weekly downloads
-
License
MIT
Repository
bitbucket
Last release
4 years ago

objection-annotations

version npm

TODO

Support ENUM

{
  "type": "array",
  "items": [
    {
      "type": "string",
      "enum": ["one", "two", "three"]
    },
    {
      "type": "integer",
      "enum": [1, 2, 3]
    }
  ]
}

Installation

npm i --save objection-annotations

Example

import { Knex } from "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);
});