0.1.43 • Published 5 months ago

@cheetah.js/orm v0.1.43

Weekly downloads
-
License
MIT
Repository
github
Last release
5 months ago

Cheetah.js ORM

Cheetah.js ORM is a simple and powerful ORM for Cheetah.js and Bun. We don't use any query builder like knex, we have our own query builder making us faster. In development.

Menu

Installation

For install Cheetah.js ORM, run the command below:

bun install @cheetah.js/orm

Create a configuration file for the ORM in the root of the project called "cheetah.config.ts" and configure the database connection, providers and entities:

import { PgDriver } from '@cheetah.js/orm';
import { ConnectionSettings } from '@cheetah.js/orm/driver/driver.interface';

const config: ConnectionSettings<any> = {
  host: 'localhost',
  port: 5432,
  database: 'postgres',
  username: 'postgres',
  password: 'postgres',
  driver: PgDriver,
  migrationPath: 'path_migrations', 
  entities: 'entity/*.ts' // or [User, Post, ...]
};

export default config;

Actually, the ORM only supports PostgreSQL, but in the future it will support other databases.

  • Entities: Path to entities. Accepts glob patterns or an array of Entity classes.
  • MigrationPath: Path to migrations. Accepts glob patterns. Is optional. After that, you need to import the ORM into the project and add it to the Cheetah.js instance:
import { Cheetah } from '@cheetah.js/core';
import { CheetahOrm } from '@cheetah.js/orm';

new Cheetah().use(CheetahOrm).listen();

Entities

Entities are classes that map to database tables. Each entity must have a primary key.

Example:

import { Entity, PrimaryKey, Property } from '@cheetah.js/orm';

@Entity()
export class User {
  @PrimaryKey()
  id: number;

  @Property()
  name: string;
}

PrimaryKey

The @PrimaryKey decorator is used to define the primary key of the entity.

Nullable property

For define a nullable property, add a parameter to the @Property decorator:

@Entity()
export class User {
    @PrimaryKey()
    id: number;

    @Property({ nullable: true })
    name: string;
}

Cheetah ORM can also distinguish nullable properties automatically by adding the question mark to the end of the property name:

export class User {
    @PrimaryKey()
    id: number;

    @Property()
    name?:string;
}

Unique property

For define a unique property, add a parameter to the @Property decorator:

@Entity()
export class User {
    @PrimaryKey()
    id: number;

    @Property({ unique: true })
    name: string;
}

Index property

For define a index for a unique property, add a parameter to the @Property decorator:

@Entity()
export class User {
    @PrimaryKey()
    id: number;

    @Property({ index: true })
    name: string;
}

For define a index for a multiple properties, add the @Index decorator:

@Entity()
export class User {
    @PrimaryKey()
    id: number;

    @Property()
    name: string;

    @Index(['name', 'email'])
    @Property()
    email: string;
}

Property options

OptionTypeDescription
nullablebooleanDefines if the property is nullable.
uniquebooleanDefines if the property is unique.
indexbooleanDefines if the property is index.
defaultanyDefines the default value of the property.
lengthnumberDefines the length of the property.
onUpdatestringDefine the action to be taken for this property when updating the entity in the database
onInsertstringDefines the action to be taken for this property when inserting the entity in the database

Hooks

Cheetah ORM supports hooks for entities. The available hooks are: BeforeCreate, AfterCreate, BeforeUpdate, AfterUpdate, BeforeDelete, AfterDelete. Hooks is only for modify the entity, not for create, update or delete another entities statements in database.

Example:

import { Entity, PrimaryKey, Property, BeforeCreate } from '@cheetah.js/orm';

@Entity()
export class User {
    @PrimaryKey()
    id: number;

    @Property()
    name: string;

    @BeforeCreate()
    static beforeCreate() {
        this.name = 'John Doe';
    }
}

Value Objects

A Value Object is an immutable type that is distinguishable only by the state of its properties. That is, unlike an Entity, which has a unique identifier and remains distinct even if its properties are otherwise identical, two Value Objects with the exact same properties can be considered equal. Cheetah ORM Entities support Value Objects. To define a Value Object, extends the ValueObject class:

import { ValueObject } from '@cheetah.js/orm';

export class Name extends ValueObject<string, Name> { // First type is a value scalar type, 
    // and second is a ValueObject

 validate(value): boolean {
   return value.length > 0; // Any validation
 }
}

const name = new Name('John Doe');
const name2 = Name.from('John Doe'); // Same as above

console.log(name.equals(name2)); // true

Is Required to implement the validate method, that returns a boolean value. To use the Value Object in the Entity, just add the ValueObject type to the property:

import { Entity, PrimaryKey, Property } from '@cheetah.js/orm';

@Entity()
export class User {
    @PrimaryKey()
    id: number;

    @Property()
    name: Name;
}

Cheetah ORM will automatically convert the Value Object to the database type and vice versa. Important: If you value object is different from string type, you need to define the database type in the @Property decorator, because the Cheetah ORM would not know the correct type of your value object:

import { Entity, PrimaryKey, Property } from '@cheetah.js/orm';

@Entity()
export class User {
    @PrimaryKey()
    id: number;

    @Property({ type: 'json' })
    name: Name;
}

Relations

Cheetah ORM supports relations between entities. The available relations are: OneToMany, ManyToOne.

OneToMany

The OneToMany relation is used to define a one-to-many relationship between two entities. For example, a user can have multiple posts, but a post can only have one user.

@Entity()
export class User {
    @PrimaryKey()
    id: number;

    @Property()
    name: string;

    @OneToMany(() => Post, (post) => post.user)
    posts: Post[];
}

ManyToOne

The owner side of the relation is the side that has the @ManyToOne decorator. The inverse side is the side that has the @OneToMany decorator. The owner side is always the side that has the foreign key.

@Entity()
export class Post {
    @PrimaryKey()
    id: number;

    @Property()
    title: string;

    @ManyToOne(() => User)
    user: User;
}

Usage

Create a new entity

import { User } from './entity/user';

const user = User.create({ name: 'John Doe' });

// OR
const user = new User();
user.name = 'John Doe';
await user.save();

Find a entity

import { User } from './entity/user';

const user = await User.findOne({ 
    name: 'John Doe',
    old: { $gte: 16, $lte: 30 }
});

List of supported operators

OperatorNameDescription
$eqEqualMatches values that are equal to a specified value.
$gtGreater ThanMatches values that are greater than a specified value.
$gteGreater Than or EqualMatches values that are greater than or equal to a specified value.
$inInMatches any of the values specified in an array.
$ltLess ThanMatches values that are less than a specified value.
$lteLess Than or EqualMatches values that are less than or equal to a specified value.
$neNot EqualMatches all values that are not equal to a specified value.
$ninNot InMatches none of the values specified in an array.
$andAndJoins query clauses with a logical AND returns all documents that match the conditions of both clauses.
$orOrJoins query clauses with a logical OR returns all documents that match the conditions of either clause.
$notNotInverts the effect of a query expression and returns documents that do not match the query expression.

Migrations

Cheetah ORM is capable of creating and running migrations. You must have the connection configuration file in the project root "cheetah.config.ts". To create a migration, run the command below:

bun cheetah-orm migration:generate

This command will create a migration file in the path defined in the configuration file, differentiating your entities created with the database.

Example:

bun cheetah-orm migration:run

This command will run all migrations that have not yet been run.

0.1.43

5 months ago

0.1.30

6 months ago

0.1.31

6 months ago

0.1.32

5 months ago

0.1.33

5 months ago

0.1.34

5 months ago

0.1.35

5 months ago

0.1.36

5 months ago

0.1.37

5 months ago

0.1.27

6 months ago

0.1.28

6 months ago

0.1.29

6 months ago

0.1.41

5 months ago

0.1.42

5 months ago

0.1.26

6 months ago

0.1.40

5 months ago

0.1.38

5 months ago

0.1.39

5 months ago

0.1.25

6 months ago

0.1.24

6 months ago

0.1.23

6 months ago

0.1.22

6 months ago

0.1.21

6 months ago

0.1.20

6 months ago

0.1.19

6 months ago

0.1.18

6 months ago

0.1.17

6 months ago

0.1.16

6 months ago

0.1.15

6 months ago

0.1.14

6 months ago

0.1.13

6 months ago

0.1.12

6 months ago

0.1.11

6 months ago

0.1.10

6 months ago

0.1.9

6 months ago

0.1.8

6 months ago

0.1.7

6 months ago

0.1.6

6 months ago

0.1.5

6 months ago

0.1.4

6 months ago

0.1.3

6 months ago

0.1.2

6 months ago

0.1.1

6 months ago

0.1.0

6 months ago