kou64yama / decolize

Decolize is the decorator-driven O/R mapper.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Decolize

Build Status codecov license

Note: Decolize is under development. It can not be used yet.

Decolize is the decorator-driven O/R mapper for TypeScript (Node.js).

Getting Started

Installation

npm install -S decolize

Usage

To use decolize, set experimentalDecorators and emitDecoratorMetadata to true:

{
    "compilerOptions": {
        "experimentalDecorators": true,
        "emitDecoratorMetadata": true
    }
}

For example:

import * as Sequelize from 'sequelize';
import Decolize from 'decolize';
import { Table, Id, GeneratedValue, Column } from 'decolize/persistences';
import { NotEmpty, Len } from 'decolize/validators';

// CREATE TABLE greeting (...);
@Table()
class Greeting {
    // id INTEGER PRIMARY KEY
    @Id()
    @GeneratedValue()
    public id?: number;

    // message VARCHAR(255) NOT NULL
    @Column({ allowNull: false })
    @NotEmpty()
    @Len({ max: 100 })
    public message?: string;

    public toString(): string {
        return `${this.id}: ${this.message}`;
    }
}

const sequelize = new Sequelize('sqlite::memory:');
const decolize = new Decolize(sequelize);
decolize.register(Greeting);

const greetingRepository = decolize.createRepository<Greeting, number>(Greeting);

sequelize.sync().then(async () => {
    const greeting = new Greeting();
    greeting.message = 'Hello World!';

    // INSERT INTO greeting (message) VALUES ('Hello World!');
    await greetingRepository.save(greeting);
    console.log(`${greeting}`);

    greeting.message = 'Hello Data World!';
    // UPDATE greeting SET message = 'Hello Data World!' WHERE id = 1;
    await greetingRepository.save(greeting);
    console.log(`${greeting}`);

    const greetings = ['message 1', 'message 2'].map((message) => {
        const g = new Greeting();
        g.message = message;
        return g;
    });
    // INSERT INTO greeting (message) VALUES ('message 1'), ('mssage 2');
    greetingRepository.save(greetings);
});

License

MIT License

Author

YAMADA Koji

About

Decolize is the decorator-driven O/R mapper.

License:MIT License


Languages

Language:TypeScript 100.0%