typeorm-entity-stream
TypeScript icon, indicating that this package has built-in type declarations

1.0.1 • Public • Published

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);
}

Package Sidebar

Install

npm i typeorm-entity-stream

Weekly Downloads

0

Version

1.0.1

License

MIT

Unpacked Size

19.7 kB

Total Files

9

Last publish

Collaborators

  • kirilyuro