@onivoro/server-typeorm-mysql 22.0.3 → 22.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,13 @@
1
- import { EntityManager, FindManyOptions, FindOneOptions, FindOptionsWhere } from 'typeorm';
1
+ import { EntityManager, FindManyOptions, FindOneOptions, FindOptionsWhere, QueryRunner } from 'typeorm';
2
2
  import { QueryDeepPartialEntity } from 'typeorm/query-builder/QueryPartialEntity';
3
3
  import { IEntityProvider } from '../types/entity-provider.interface';
4
+ import { ReadStream } from 'fs';
5
+ export type TQueryStreamParams<TRecord = any> = {
6
+ query: string;
7
+ onData?: (stream: ReadStream, record: TRecord, count: number) => Promise<any | void>;
8
+ onError?: (stream: ReadStream, error: any) => Promise<any | void>;
9
+ onEnd?: (stream: ReadStream, count: number) => Promise<any | void>;
10
+ };
4
11
  export declare class TypeOrmRepository<TEntity> implements IEntityProvider<TEntity, FindOneOptions<TEntity>, FindManyOptions<TEntity>, FindOptionsWhere<TEntity>, QueryDeepPartialEntity<TEntity>> {
5
12
  private entityType;
6
13
  entityManager: EntityManager;
@@ -18,4 +25,18 @@ export declare class TypeOrmRepository<TEntity> implements IEntityProvider<TEnti
18
25
  get repo(): import("typeorm").Repository<import("typeorm").ObjectLiteral>;
19
26
  protected insertAndReturn(entityToInsert: TEntity): Promise<TEntity>;
20
27
  protected insertAndReturnMany(entitiesToInsert: TEntity[]): Promise<TEntity[]>;
28
+ static queryStream<TRecord = any>(queryRunner: QueryRunner, _: TQueryStreamParams): Promise<{
29
+ stream: ReadStream;
30
+ error: any;
31
+ } | {
32
+ stream: any;
33
+ error: any;
34
+ }>;
35
+ queryStream<TRecord = any>(_: TQueryStreamParams): Promise<{
36
+ stream: ReadStream;
37
+ error: any;
38
+ } | {
39
+ stream: any;
40
+ error: any;
41
+ }>;
21
42
  }
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.TypeOrmRepository = void 0;
4
+ const common_1 = require("@nestjs/common");
4
5
  class TypeOrmRepository {
5
6
  entityType;
6
7
  entityManager;
@@ -58,5 +59,35 @@ class TypeOrmRepository {
58
59
  const insertedEntity = insertionResult.generatedMaps;
59
60
  return insertedEntity;
60
61
  }
62
+ static async queryStream(queryRunner, _) {
63
+ if (!_.query) {
64
+ throw new common_1.BadRequestException(`StreamingQueryRunner requires one of: {query, table}`);
65
+ }
66
+ let processedCount = 0;
67
+ const query = _.query;
68
+ try {
69
+ const stream = await queryRunner.stream(query);
70
+ stream.on('data', (record) => {
71
+ _.onData?.(stream, record, processedCount++);
72
+ });
73
+ stream.on('error', (error) => {
74
+ console.error({ detail: `Error processing stream for query "${_.query}"`, error });
75
+ _.onError?.(stream, error);
76
+ });
77
+ stream.on('end', () => {
78
+ console.log({ detail: `Finished processing stream for query "${_.query}"`, processedCount });
79
+ _.onEnd?.(stream, processedCount);
80
+ });
81
+ return { stream, error: null };
82
+ }
83
+ catch (error) {
84
+ console.error({ detail: `Error processing stream for query "${_.query}"`, error });
85
+ return { stream: null, error };
86
+ }
87
+ }
88
+ async queryStream(_) {
89
+ const queryRunner = this.entityManager.connection.createQueryRunner();
90
+ return await TypeOrmRepository.queryStream(queryRunner, _);
91
+ }
61
92
  }
62
93
  exports.TypeOrmRepository = TypeOrmRepository;
@@ -1,6 +1,13 @@
1
- import { EntityManager, FindManyOptions, FindOneOptions, FindOptionsWhere } from 'typeorm';
1
+ import { EntityManager, FindManyOptions, FindOneOptions, FindOptionsWhere, QueryRunner } from 'typeorm';
2
2
  import { QueryDeepPartialEntity } from 'typeorm/query-builder/QueryPartialEntity';
3
3
  import { IEntityProvider } from '../types/entity-provider.interface';
4
+ import { ReadStream } from 'fs';
5
+ export type TQueryStreamParams<TRecord = any> = {
6
+ query: string;
7
+ onData?: (stream: ReadStream, record: TRecord, count: number) => Promise<any | void>;
8
+ onError?: (stream: ReadStream, error: any) => Promise<any | void>;
9
+ onEnd?: (stream: ReadStream, count: number) => Promise<any | void>;
10
+ };
4
11
  export declare class TypeOrmRepository<TEntity> implements IEntityProvider<TEntity, FindOneOptions<TEntity>, FindManyOptions<TEntity>, FindOptionsWhere<TEntity>, QueryDeepPartialEntity<TEntity>> {
5
12
  private entityType;
6
13
  entityManager: EntityManager;
@@ -18,4 +25,18 @@ export declare class TypeOrmRepository<TEntity> implements IEntityProvider<TEnti
18
25
  get repo(): import("typeorm").Repository<import("typeorm").ObjectLiteral>;
19
26
  protected insertAndReturn(entityToInsert: TEntity): Promise<TEntity>;
20
27
  protected insertAndReturnMany(entitiesToInsert: TEntity[]): Promise<TEntity[]>;
28
+ static queryStream<TRecord = any>(queryRunner: QueryRunner, _: TQueryStreamParams): Promise<{
29
+ stream: ReadStream;
30
+ error: any;
31
+ } | {
32
+ stream: any;
33
+ error: any;
34
+ }>;
35
+ queryStream<TRecord = any>(_: TQueryStreamParams): Promise<{
36
+ stream: ReadStream;
37
+ error: any;
38
+ } | {
39
+ stream: any;
40
+ error: any;
41
+ }>;
21
42
  }
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.TypeOrmRepository = void 0;
4
+ const common_1 = require("@nestjs/common");
4
5
  class TypeOrmRepository {
5
6
  entityType;
6
7
  entityManager;
@@ -58,5 +59,35 @@ class TypeOrmRepository {
58
59
  const insertedEntity = insertionResult.generatedMaps;
59
60
  return insertedEntity;
60
61
  }
62
+ static async queryStream(queryRunner, _) {
63
+ if (!_.query) {
64
+ throw new common_1.BadRequestException(`StreamingQueryRunner requires one of: {query, table}`);
65
+ }
66
+ let processedCount = 0;
67
+ const query = _.query;
68
+ try {
69
+ const stream = await queryRunner.stream(query);
70
+ stream.on('data', (record) => {
71
+ _.onData?.(stream, record, processedCount++);
72
+ });
73
+ stream.on('error', (error) => {
74
+ console.error({ detail: `Error processing stream for query "${_.query}"`, error });
75
+ _.onError?.(stream, error);
76
+ });
77
+ stream.on('end', () => {
78
+ console.log({ detail: `Finished processing stream for query "${_.query}"`, processedCount });
79
+ _.onEnd?.(stream, processedCount);
80
+ });
81
+ return { stream, error: null };
82
+ }
83
+ catch (error) {
84
+ console.error({ detail: `Error processing stream for query "${_.query}"`, error });
85
+ return { stream: null, error };
86
+ }
87
+ }
88
+ async queryStream(_) {
89
+ const queryRunner = this.entityManager.connection.createQueryRunner();
90
+ return await TypeOrmRepository.queryStream(queryRunner, _);
91
+ }
61
92
  }
62
93
  exports.TypeOrmRepository = TypeOrmRepository;
@@ -1,6 +1,13 @@
1
- import { EntityManager, FindManyOptions, FindOneOptions, FindOptionsWhere } from 'typeorm';
1
+ import { EntityManager, FindManyOptions, FindOneOptions, FindOptionsWhere, QueryRunner } from 'typeorm';
2
2
  import { QueryDeepPartialEntity } from 'typeorm/query-builder/QueryPartialEntity';
3
3
  import { IEntityProvider } from '../types/entity-provider.interface';
4
+ import { ReadStream } from 'fs';
5
+ export type TQueryStreamParams<TRecord = any> = {
6
+ query: string;
7
+ onData?: (stream: ReadStream, record: TRecord, count: number) => Promise<any | void>;
8
+ onError?: (stream: ReadStream, error: any) => Promise<any | void>;
9
+ onEnd?: (stream: ReadStream, count: number) => Promise<any | void>;
10
+ };
4
11
  export declare class TypeOrmRepository<TEntity> implements IEntityProvider<TEntity, FindOneOptions<TEntity>, FindManyOptions<TEntity>, FindOptionsWhere<TEntity>, QueryDeepPartialEntity<TEntity>> {
5
12
  private entityType;
6
13
  entityManager: EntityManager;
@@ -18,4 +25,18 @@ export declare class TypeOrmRepository<TEntity> implements IEntityProvider<TEnti
18
25
  get repo(): import("typeorm").Repository<import("typeorm").ObjectLiteral>;
19
26
  protected insertAndReturn(entityToInsert: TEntity): Promise<TEntity>;
20
27
  protected insertAndReturnMany(entitiesToInsert: TEntity[]): Promise<TEntity[]>;
28
+ static queryStream<TRecord = any>(queryRunner: QueryRunner, _: TQueryStreamParams): Promise<{
29
+ stream: ReadStream;
30
+ error: any;
31
+ } | {
32
+ stream: any;
33
+ error: any;
34
+ }>;
35
+ queryStream<TRecord = any>(_: TQueryStreamParams): Promise<{
36
+ stream: ReadStream;
37
+ error: any;
38
+ } | {
39
+ stream: any;
40
+ error: any;
41
+ }>;
21
42
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@onivoro/server-typeorm-mysql",
3
- "version": "22.0.3",
3
+ "version": "22.0.4",
4
4
  "repository": {
5
5
  "url": "git+https://github.com/onivoro/server-typeorm-mysql.git"
6
6
  },