npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@cheetah.js/orm

v0.1.43

Published

A simple ORM for Cheetah.js

Downloads

100

Readme

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.
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

| Option | Type | Description | | ------ | ---- |--------------------------------------------------------------------------------------------| | nullable | boolean | Defines if the property is nullable. | | unique | boolean | Defines if the property is unique. | | index | boolean | Defines if the property is index. | | default | any | Defines the default value of the property. | | length | number | Defines the length of the property. | | onUpdate | string | Define the action to be taken for this property when updating the entity in the database | | onInsert | string | Defines 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

| Operator |Name | Description | | ------ | ---- |--------------------------------------------------------------------------------------------| | $eq | Equal | Matches values that are equal to a specified value. | | $gt | Greater Than | Matches values that are greater than a specified value. | | $gte | Greater Than or Equal | Matches values that are greater than or equal to a specified value. | | $in | In | Matches any of the values specified in an array. | | $lt | Less Than | Matches values that are less than a specified value. | | $lte | Less Than or Equal | Matches values that are less than or equal to a specified value. | | $ne | Not Equal | Matches all values that are not equal to a specified value. | | $nin | Not In | Matches none of the values specified in an array. | | $and | And | Joins query clauses with a logical AND returns all documents that match the conditions of both clauses. | | $or | Or | Joins query clauses with a logical OR returns all documents that match the conditions of either clause. | | $not | Not | Inverts 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.