1.0.1 • Published 1 year ago

typeorm-entity-stream v1.0.1

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

typeorm-entity-stream

NPM Version CircleCI

Simple and lightweight entities streaming for TypeORM.
Provides a clean and simple streaming API by extending TypeORM's built-in raw streaming capabilities.

Usage

Install the npm package:

npm install typeorm-entity-stream --save

Import the streaming function:

import streamEntities from "typeorm-entity-stream";

The type signature of streamEntities is (roughly):

async function streamEntities<Entity>(
    dataSource: DataSource,
    entityConstructor: new () => Entity,
    query?: (queryBuilder: SelectQueryBuilder<Entity>) => SelectQueryBuilder<Entity>
): AsyncIterator

where DataSource and Entity are TypeORM DataSource and Entity type, respectively; and query is an optional parameter that allows providing a custom query.
The function returns an AsyncIterator of Entity instances, i.e. an object which can be iterated over using an async for...of loop (see examples below).

Examples

Note: the records are streamed from the database, rather than being fetched all at once.

Suppose we have a database with the following entity:

@Entity()
class User {
    @PrimaryColumn() id: number;
    @Column() email: string;
    @Column() role: string;
}

Print the emails of all existing users:

const users = await streamEntities(dataSource, User);

for await (const user of users) {
    console.log(user.email);
}

Print the emails of all admin users:

const users = await streamEntities(dataSource, User, 
  query => query.where({ role: "admin" })
);

for await (const user of users) {
    console.log(user.email);
}
1.0.1

1 year ago

1.0.0

1 year ago