All Projects → iWinston → Typeorm Plus

iWinston / Typeorm Plus

Licence: mit
TypeORM+ adds functionality to TypeORM intending to make the Repository and QueryBuilder more powerful.

Programming Languages

typescript
32286 projects

Projects that are alternatives of or similar to Typeorm Plus

Orator
The Orator ORM provides a simple yet beautiful ActiveRecord implementation.
Stars: ✭ 1,234 (+814.07%)
Mutual labels:  orm, database
Nymph
Data objects for JavaScript and PHP.
Stars: ✭ 97 (-28.15%)
Mutual labels:  orm, database
Evolutility Server Node
Model-driven REST or GraphQL backend for CRUD and more, written in Javascript, using Node.js, Express, and PostgreSQL.
Stars: ✭ 84 (-37.78%)
Mutual labels:  orm, database
Laravel Optimistic Locking
Adds optimistic locking feature to eloquent models.
Stars: ✭ 71 (-47.41%)
Mutual labels:  orm, database
Tornado Sqlalchemy
SQLAlchemy support for Tornado
Stars: ✭ 112 (-17.04%)
Mutual labels:  orm, database
Ebean
Ebean ORM
Stars: ✭ 1,172 (+768.15%)
Mutual labels:  orm, database
Entityworker.core
EntityWorker is an object-relation mapper(ORM) that enable .NET developers to work with relations data using objects. EntityWorker is an alternative to entityframwork. is more flexible and much faster than entity framework.
Stars: ✭ 91 (-32.59%)
Mutual labels:  orm, database
Fluent
Vapor ORM (queries, models, and relations) for NoSQL and SQL databases
Stars: ✭ 1,071 (+693.33%)
Mutual labels:  orm, database
Ship Hold
data access framework for Postgresql on nodejs
Stars: ✭ 110 (-18.52%)
Mutual labels:  orm, database
Reactiveandroid
🚀 Simple and powerful ORM for Android
Stars: ✭ 102 (-24.44%)
Mutual labels:  orm, database
Dbx
A neat codegen-based database wrapper written in Go
Stars: ✭ 68 (-49.63%)
Mutual labels:  orm, database
Lealone
极具创新的面向微服务和 OLTP/OLAP 场景的单机与分布式关系数据库
Stars: ✭ 1,802 (+1234.81%)
Mutual labels:  orm, database
Layr
Dramatically simplify full‑stack development
Stars: ✭ 1,111 (+722.96%)
Mutual labels:  orm, database
Ef6
This is the codebase for Entity Framework 6 (previously maintained at https://entityframework.codeplex.com). Entity Framework Core is maintained at https://github.com/dotnet/efcore.
Stars: ✭ 1,218 (+802.22%)
Mutual labels:  orm, database
Topaz
A simple and useful db wrapper for Crystal-lang
Stars: ✭ 56 (-58.52%)
Mutual labels:  orm, database
Prisma
Next-generation ORM for Node.js & TypeScript | PostgreSQL, MySQL, MariaDB, SQL Server, SQLite & MongoDB (Preview)
Stars: ✭ 18,168 (+13357.78%)
Mutual labels:  orm, database
Hunt Entity
An object-relational mapping (ORM) framework for D language (Similar to JPA / Doctrine), support PostgreSQL and MySQL.
Stars: ✭ 51 (-62.22%)
Mutual labels:  orm, database
Gormt
database to golang struct
Stars: ✭ 1,063 (+687.41%)
Mutual labels:  orm, database
Alkali
a simple python database
Stars: ✭ 101 (-25.19%)
Mutual labels:  orm, database
Efcore
EF Core is a modern object-database mapper for .NET. It supports LINQ queries, change tracking, updates, and schema migrations.
Stars: ✭ 10,838 (+7928.15%)
Mutual labels:  orm, database

Introduction

TypeORM+ is an ORM that can run in NodeJS, Browser, Cordova, PhoneGap, Ionic, React Native, NativeScript, Expo, and Electron platforms and can be used with TypeScript and JavaScript (ES5, ES6, ES7, ES8).

TypeORM vs TypeORM+

TypeORM+ is a fork of TypeORM. TypeORM+ adds functionality to TypeORM intending to make the Repository and QueryBuilder more powerful. Since this is a fork we'll pull in changes from the original TypeORM regularly as they are released.

TypeORM+ is intended to replace TypeORM, so any changes in its interface are documented below.

Differences from TypeORM

  • Soft Deleting
  • Query Scopes
  • Convenient Pagination
  • Conditional Clauses

Installation

  1. Install the npm package:

    yarn add typeorm-plus --save

  2. You need to install reflect-metadata shim, node typing, a database driver and so on. You can read more from here: http://typeorm.io.

Getting Started

After installed the npm packages, you can import modules from "typeorm-plus". But if you are using the third-party modules for TypeORM, such like nestjs/typeorm, you need to install typeorm-plus with the alias name typeorm:

yarn add [email protected]:typeorm-plus --save

Soft Deleting

1. Including Soft Deleted Entities

In addition to actually removing records from your database, TypeORM+ supports "soft delete". When entities are soft deleted, they are not actually removed from your database. Instead, an attribute that records the delete time is set on the entity and inserted into the database. If the attribute is a non-null value, the entity has been soft deleted. To enable soft deletes for an entity, use the @DeleteDateColumn on the entity:

import { DeleteDateColumn } from 'typeorm-plus'

export class Entity {

    @DeleteDateColumn({ name: 'deleted_at' })
    public deletedAt: Date

}

2. Applying Soft Delete To QueryBuilder

@DeleteDateColumn is a special column that is automatically set to the entity's delete time each time you call soft-delete of entity manager or repository. You don't need to set this column - it will be automatically set.

import {createConnection} from "typeorm-plus";
import {Entity} from "./entity";

createConnection(/*...*/).then(async connection => {

    await connection
      .getRepository(Entity)
      .createQueryBuilder()
      .softDelete()

    // And You can restore it using restore;
    await connection
      .getRepository(Entity)
      .createQueryBuilder()
      .restore()

}).catch(error => console.log(error));

3. Applying Soft Delete To Repository

import {createConnection} from "typeorm-plus";
import {Entity} from "./entity";

createConnection(/*...*/).then(async connection => {

    const repository = connection.getRepository(Entity);

    // Delete a entity
    await repository.softDelete(1);

    // And You can restore it using restore;
    await repository.restore(1);

    // Or You can soft-delete them using softRemove
    const entities = await repository.find();
    const entitiesAfterSoftRemove = await repository.softRemove(entities);

    // And You can recover them using recover;
    await repository.recover(entitiesAfterSoftRemove);

}).catch(error => console.log(error));

4. Cascading Soft Deletes

This example show what the cascading soft deletes behaves in TypeORM+.

const category1 = new Category();
category1.name = "animals";

const category2 = new Category();
category2.name = "zoo";

const question = new Question();
question.categories = [category1, category2];
const newQuestion =  await connection.manager.save(question);

await connection.manager.softRemove(newQuestion);

As you can see in this example we did not call save or softRemove for category1 and category2. But They will be automatically saved and soft-deleted when the cascade of relation options is set to true like this:

import {Entity, PrimaryGeneratedColumn, Column, ManyToMany, JoinTable} from "typeorm-plus";
import {Category} from "./Category";

@Entity()
export class Question {

    @PrimaryGeneratedColumn()
    id: number;

    @ManyToMany(type => Category, category => category.questions, {
        cascade: true
    })
    @JoinTable()
    categories: Category[];

}

Query Scopes

Query scopes allow you to add constraints to all queries for a given entity. You can register scopes in your entity:

1. Registering Scopes

import { DeleteDateColumn } from 'typeorm-plus'

export class Entity {

    static scope = {
      'default': {
        deletedAt: IsNull()
      },
      'myScope': {
        deletedAt: Not(IsNull())
      }
    }

}

2. Applying Scopes To QueryBuilder

When you are calling queryBuilder, you can also apply the scope.

import {createConnection} from "typeorm-plus";
import {Entity} from "./entity";

createConnection(/*...*/).then(async connection => {

    const repository = connection.getRepository(Entity);
    await repository.createQueryBuilder().setScope("myScope").getMany();

}).catch(error => console.log(error));

The param scope of the setScope function selects scope to apply to the repository. If it is false, none of the scopes will be applied. If it is undefined, the value will be "default".

3. Applying Scopes To Repository

When you are calling repository, you can apply the scope. The scope mode supports these methods of repository: find, findOne, findOneOrFail, countfindByIds And findAndCount.

import {createConnection} from "typeorm-plus";
import {Entity} from "./entity";

createConnection(/*...*/).then(async connection => {

    const repository = connection.getRepository(Entity);
    // Delete a entity
    await repository.find({
        scope: 'myScope'
    });

}).catch(error => console.log(error));

The property scope of the find options selects scope to apply to the repository. If it is false, none of the scopes will be applied. If it is undefined, the value will be "default".

4. Working with Soft Delete

TypeORM's own soft delete functionality utilizes global scopes to only pull "non-deleted" entities from the database.

If the @DeleteDateColumn is set, the default scope will be "non-deleted".

Convenient Pagination

TypeORM+'s paginator is integrated with the query builder and repository and provides convenient, easy-to-use pagination of database results out of the box.

1. Paginating Query Builder Results

In this example, the arguments passed to the paginate method is the current page number and the number of items you would like displayed "per page":

import {createConnection} from "typeorm-plus";
import {Entity} from "./entity";

createConnection(/*...*/).then(async connection => {

  await connection
    .getRepository(Entity)
    .createQueryBuilder()
    .paginate(1, 15)
    .getMany();

}).catch(error => console.log(error));

TypeORM+'s paginator also supports raw mode:

import {createConnection} from "typeorm-plus";
import {Entity} from "./entity";

createConnection(/*...*/).then(async connection => {

  await connection
    .getRepository(Entity)
    .createQueryBuilder()
    .paginateRaw(1, 15)
    .getRawMany();

}).catch(error => console.log(error));

2. Paginating Repository Results

You may also paginate queries with the repository.

import {createConnection} from "typeorm-plus";
import {Entity} from "./entity";

createConnection(/*...*/).then(async connection => {

  await connection
    .getRepository(Entity)
    .findAndCount({
      current: 1,
      size: 15
    })

}).catch(error => console.log(error));

The property current of the find options defines an offset page (paginated) where from entities should be taken. And The property size is the alias name for taking, just effected for the conditions that current and size are both defined.

Conditional Clauses

Sometimes you may want clauses to apply to a query only when something else is true. For instance, you may only want to apply a where statement if a given input value is present on the incoming request. You may accomplish this using the when method:

import {createConnection} from "typeorm-plus";
import {Entity} from "./entity";

createConnection(/*...*/).then(async connection => {

  await connection
    .getRepository(Entity)
    .createQueryBuilder("it")
    .when(true, qb => qb.where('it.id = 1'))
    .getMany();

}).catch(error => console.log(error));

The when method only executes the given Closure when the first parameter is true. If the first parameter is false, the Closure will not be executed.

License

TypeORM+ is MIT licensed.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].