@nathapp/nestjs-data 1.0.0-rc.5 → 1.0.0-rc.7

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,3 +1,4 @@
1
1
  import { CreateDate, DateColumn, UpdateDate } from "./date-column.decorator";
2
2
  import { ReadOnlyColumn } from "./readonly-column.decorator";
3
- export { DateColumn, CreateDate, UpdateDate, ReadOnlyColumn, };
3
+ import { CustomRepository, TYPEORM_EX_CUSTOM_REPOSITORY } from "./typeorm-ex.decorator";
4
+ export { DateColumn, CreateDate, UpdateDate, ReadOnlyColumn, CustomRepository, TYPEORM_EX_CUSTOM_REPOSITORY, };
@@ -1,9 +1,12 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.ReadOnlyColumn = exports.UpdateDate = exports.CreateDate = exports.DateColumn = void 0;
3
+ exports.TYPEORM_EX_CUSTOM_REPOSITORY = exports.CustomRepository = exports.ReadOnlyColumn = exports.UpdateDate = exports.CreateDate = exports.DateColumn = void 0;
4
4
  const date_column_decorator_1 = require("./date-column.decorator");
5
5
  Object.defineProperty(exports, "CreateDate", { enumerable: true, get: function () { return date_column_decorator_1.CreateDate; } });
6
6
  Object.defineProperty(exports, "DateColumn", { enumerable: true, get: function () { return date_column_decorator_1.DateColumn; } });
7
7
  Object.defineProperty(exports, "UpdateDate", { enumerable: true, get: function () { return date_column_decorator_1.UpdateDate; } });
8
8
  const readonly_column_decorator_1 = require("./readonly-column.decorator");
9
9
  Object.defineProperty(exports, "ReadOnlyColumn", { enumerable: true, get: function () { return readonly_column_decorator_1.ReadOnlyColumn; } });
10
+ const typeorm_ex_decorator_1 = require("./typeorm-ex.decorator");
11
+ Object.defineProperty(exports, "CustomRepository", { enumerable: true, get: function () { return typeorm_ex_decorator_1.CustomRepository; } });
12
+ Object.defineProperty(exports, "TYPEORM_EX_CUSTOM_REPOSITORY", { enumerable: true, get: function () { return typeorm_ex_decorator_1.TYPEORM_EX_CUSTOM_REPOSITORY; } });
@@ -0,0 +1,2 @@
1
+ export declare const TYPEORM_EX_CUSTOM_REPOSITORY = "TYPEORM_EX_CUSTOM_REPOSITORY";
2
+ export declare function CustomRepository(entity: Function): ClassDecorator;
@@ -0,0 +1,9 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.CustomRepository = exports.TYPEORM_EX_CUSTOM_REPOSITORY = void 0;
4
+ const common_1 = require("@nestjs/common");
5
+ exports.TYPEORM_EX_CUSTOM_REPOSITORY = "TYPEORM_EX_CUSTOM_REPOSITORY";
6
+ function CustomRepository(entity) {
7
+ return (0, common_1.SetMetadata)(exports.TYPEORM_EX_CUSTOM_REPOSITORY, entity);
8
+ }
9
+ exports.CustomRepository = CustomRepository;
package/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- export * from "./connection";
2
1
  export * from "./decorator";
3
2
  export * from "./entities";
4
3
  export * from "./pagination";
4
+ export * from "./typeorm";
package/dist/index.js CHANGED
@@ -14,7 +14,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
14
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
- __exportStar(require("./connection"), exports);
18
17
  __exportStar(require("./decorator"), exports);
19
18
  __exportStar(require("./entities"), exports);
20
19
  __exportStar(require("./pagination"), exports);
20
+ __exportStar(require("./typeorm"), exports);
@@ -0,0 +1,12 @@
1
+ import { TypeOrmModuleOptions, TypeOrmOptionsFactory } from "@nestjs/typeorm";
2
+ import { EntitySchema, MixedList } from "typeorm";
3
+ import { BaseDataSourceOptions } from "typeorm/data-source/BaseDataSourceOptions";
4
+ export type OptionWithoutEntities = Omit<Partial<BaseDataSourceOptions>, "entities" | "type">;
5
+ export declare class BaseTypeOrmModuleOptionFactory implements TypeOrmOptionsFactory {
6
+ private readonly connectionString;
7
+ private readonly entities;
8
+ private readonly options?;
9
+ private connectionType;
10
+ constructor(connectionString: string, entities: MixedList<Function | string | EntitySchema>, options?: OptionWithoutEntities);
11
+ createTypeOrmOptions(connectionName?: string): TypeOrmModuleOptions | Promise<TypeOrmModuleOptions>;
12
+ }
@@ -0,0 +1,34 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.BaseTypeOrmModuleOptionFactory = void 0;
4
+ const typeorm_utils_1 = require("./typeorm-utils");
5
+ class BaseTypeOrmModuleOptionFactory {
6
+ constructor(connectionString, entities, options) {
7
+ this.connectionString = connectionString;
8
+ this.entities = entities;
9
+ this.options = options;
10
+ if (!connectionString || connectionString == "") {
11
+ throw new Error("connectionString is missing");
12
+ }
13
+ const isPresent = connectionString && connectionString != "";
14
+ this.connectionType = (isPresent ? this.connectionString.split(":").shift() : null);
15
+ }
16
+ createTypeOrmOptions(connectionName) {
17
+ const options = (0, typeorm_utils_1.sanatizeTypeOrmOptions)(this.connectionType, this.options);
18
+ switch (this.connectionType) {
19
+ case "mysql":
20
+ case "mariadb":
21
+ case "mongodb":
22
+ return {
23
+ name: connectionName,
24
+ type: this.connectionType,
25
+ url: this.connectionString,
26
+ entities: this.entities || [],
27
+ ...options,
28
+ };
29
+ default:
30
+ throw new Error("Connection Not Supported");
31
+ }
32
+ }
33
+ }
34
+ exports.BaseTypeOrmModuleOptionFactory = BaseTypeOrmModuleOptionFactory;
@@ -0,0 +1,16 @@
1
+ import { SnakeNamingStrategy } from "typeorm-naming-strategies";
2
+ export declare const defaultExtraOptions: {
3
+ entities: any[];
4
+ logging: boolean;
5
+ synchronize: boolean;
6
+ autoLoadEntities: boolean;
7
+ namingStrategy: SnakeNamingStrategy;
8
+ };
9
+ export declare const defaultMysqlExtraOptions: {
10
+ supportBigNumbers: boolean;
11
+ bigNumberStrings: boolean;
12
+ };
13
+ export declare const defaultMongoExtraOptions: {
14
+ useNewUrlParser: boolean;
15
+ useUnifiedTopology: boolean;
16
+ };
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.defaultMongoExtraOptions = exports.defaultMysqlExtraOptions = exports.defaultExtraOptions = void 0;
4
+ const typeorm_naming_strategies_1 = require("typeorm-naming-strategies");
5
+ exports.defaultExtraOptions = {
6
+ entities: [],
7
+ logging: false,
8
+ synchronize: false,
9
+ autoLoadEntities: true,
10
+ namingStrategy: new typeorm_naming_strategies_1.SnakeNamingStrategy(),
11
+ };
12
+ exports.defaultMysqlExtraOptions = {
13
+ supportBigNumbers: true,
14
+ bigNumberStrings: true,
15
+ };
16
+ exports.defaultMongoExtraOptions = {
17
+ useNewUrlParser: true,
18
+ useUnifiedTopology: true,
19
+ };
@@ -0,0 +1,5 @@
1
+ import { BaseTypeOrmModuleOptionFactory } from "./base-typeorm-options-factory";
2
+ import { defaultExtraOptions, defaultMongoExtraOptions, defaultMysqlExtraOptions } from "./constants";
3
+ import { DEFAULT_DATA_SOURCE_NAME, SupportedConnectionType, TypeOrmExModule } from "./typeorm-ex.module";
4
+ import { sanatizeTypeOrmOptions } from "./typeorm-utils";
5
+ export { TypeOrmExModule, DEFAULT_DATA_SOURCE_NAME, defaultExtraOptions, defaultMysqlExtraOptions, defaultMongoExtraOptions, SupportedConnectionType, sanatizeTypeOrmOptions, BaseTypeOrmModuleOptionFactory, };
@@ -0,0 +1,14 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.BaseTypeOrmModuleOptionFactory = exports.sanatizeTypeOrmOptions = exports.defaultMongoExtraOptions = exports.defaultMysqlExtraOptions = exports.defaultExtraOptions = exports.DEFAULT_DATA_SOURCE_NAME = exports.TypeOrmExModule = void 0;
4
+ const base_typeorm_options_factory_1 = require("./base-typeorm-options-factory");
5
+ Object.defineProperty(exports, "BaseTypeOrmModuleOptionFactory", { enumerable: true, get: function () { return base_typeorm_options_factory_1.BaseTypeOrmModuleOptionFactory; } });
6
+ const constants_1 = require("./constants");
7
+ Object.defineProperty(exports, "defaultExtraOptions", { enumerable: true, get: function () { return constants_1.defaultExtraOptions; } });
8
+ Object.defineProperty(exports, "defaultMongoExtraOptions", { enumerable: true, get: function () { return constants_1.defaultMongoExtraOptions; } });
9
+ Object.defineProperty(exports, "defaultMysqlExtraOptions", { enumerable: true, get: function () { return constants_1.defaultMysqlExtraOptions; } });
10
+ const typeorm_ex_module_1 = require("./typeorm-ex.module");
11
+ Object.defineProperty(exports, "DEFAULT_DATA_SOURCE_NAME", { enumerable: true, get: function () { return typeorm_ex_module_1.DEFAULT_DATA_SOURCE_NAME; } });
12
+ Object.defineProperty(exports, "TypeOrmExModule", { enumerable: true, get: function () { return typeorm_ex_module_1.TypeOrmExModule; } });
13
+ const typeorm_utils_1 = require("./typeorm-utils");
14
+ Object.defineProperty(exports, "sanatizeTypeOrmOptions", { enumerable: true, get: function () { return typeorm_utils_1.sanatizeTypeOrmOptions; } });
@@ -0,0 +1,11 @@
1
+ import { DynamicModule } from "@nestjs/common";
2
+ import { TypeOrmModuleAsyncOptions, TypeOrmModuleOptions } from "@nestjs/typeorm";
3
+ import { DataSource, DataSourceOptions } from "typeorm";
4
+ export declare const DEFAULT_DATA_SOURCE_NAME = "default";
5
+ export type SupportedConnectionType = "mysql" | "mariadb" | "mongodb";
6
+ export declare class TypeOrmExModule {
7
+ static forRoot(options?: TypeOrmModuleOptions): DynamicModule;
8
+ static forRootAsync(options?: TypeOrmModuleAsyncOptions): DynamicModule;
9
+ static forCustomRepository<T extends new (...args: any[]) => any>(repositories: T[], dataSource?: DataSource | DataSourceOptions | string): DynamicModule;
10
+ private static sanitizeOptions;
11
+ }
@@ -0,0 +1,64 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.TypeOrmExModule = exports.DEFAULT_DATA_SOURCE_NAME = void 0;
4
+ const typeorm_1 = require("@nestjs/typeorm");
5
+ const decorator_1 = require("../decorator");
6
+ const typeorm_utils_1 = require("./typeorm-utils");
7
+ exports.DEFAULT_DATA_SOURCE_NAME = 'default';
8
+ class TypeOrmExModule {
9
+ static forRoot(options) {
10
+ options = this.sanitizeOptions(options);
11
+ return {
12
+ module: TypeOrmExModule,
13
+ imports: [typeorm_1.TypeOrmModule.forRoot(options)],
14
+ };
15
+ }
16
+ static forRootAsync(options) {
17
+ return {
18
+ module: TypeOrmExModule,
19
+ imports: [typeorm_1.TypeOrmModule.forRootAsync(options)],
20
+ };
21
+ }
22
+ static forCustomRepository(repositories, dataSource = exports.DEFAULT_DATA_SOURCE_NAME) {
23
+ const providers = [];
24
+ for (const repository of repositories) {
25
+ const entity = Reflect.getMetadata(decorator_1.TYPEORM_EX_CUSTOM_REPOSITORY, repository);
26
+ if (!entity) {
27
+ continue;
28
+ }
29
+ providers.push({
30
+ inject: [(0, typeorm_1.getDataSourceToken)(dataSource)],
31
+ provide: repository,
32
+ useFactory: (dataSource) => {
33
+ const baseRepository = dataSource.getRepository(entity);
34
+ const customRepository = new repository(baseRepository.target, baseRepository.manager);
35
+ Object.defineProperty(customRepository, 'manager', {
36
+ get() {
37
+ return baseRepository.manager;
38
+ },
39
+ });
40
+ return customRepository;
41
+ },
42
+ });
43
+ }
44
+ return {
45
+ exports: providers,
46
+ module: TypeOrmExModule,
47
+ providers,
48
+ };
49
+ }
50
+ static sanitizeOptions(options) {
51
+ let connectionType;
52
+ if (options.type) {
53
+ connectionType = options.type;
54
+ }
55
+ else if (options["url"]) {
56
+ connectionType = options["url"].split(":").shift();
57
+ }
58
+ if (!connectionType) {
59
+ throw new Error("Connection Not Supported");
60
+ }
61
+ return (0, typeorm_utils_1.sanatizeTypeOrmOptions)(connectionType, options);
62
+ }
63
+ }
64
+ exports.TypeOrmExModule = TypeOrmExModule;
@@ -0,0 +1,22 @@
1
+ import { TypeOrmModuleOptions } from "@nestjs/typeorm";
2
+ import { SupportedConnectionType } from "./typeorm-ex.module";
3
+ import { OptionWithoutEntities } from "./base-typeorm-options-factory";
4
+ export declare const sanatizeTypeOrmOptions: (connectionType: SupportedConnectionType, options: TypeOrmModuleOptions | OptionWithoutEntities) => ({
5
+ entities: any[];
6
+ logging: boolean;
7
+ synchronize: boolean;
8
+ autoLoadEntities: boolean;
9
+ namingStrategy: import("typeorm-naming-strategies").SnakeNamingStrategy;
10
+ } & {
11
+ supportBigNumbers: boolean;
12
+ bigNumberStrings: boolean;
13
+ } & OptionWithoutEntities) | ({
14
+ entities: any[];
15
+ logging: boolean;
16
+ synchronize: boolean;
17
+ autoLoadEntities: boolean;
18
+ namingStrategy: import("typeorm-naming-strategies").SnakeNamingStrategy;
19
+ } & {
20
+ useNewUrlParser: boolean;
21
+ useUnifiedTopology: boolean;
22
+ } & OptionWithoutEntities);
@@ -0,0 +1,14 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.sanatizeTypeOrmOptions = void 0;
4
+ const constants_1 = require("./constants");
5
+ const sanatizeTypeOrmOptions = (connectionType, options) => {
6
+ switch (connectionType) {
7
+ case "mysql":
8
+ case "mariadb":
9
+ return Object.assign(constants_1.defaultExtraOptions, constants_1.defaultMysqlExtraOptions, options);
10
+ case "mongodb":
11
+ return Object.assign(constants_1.defaultExtraOptions, constants_1.defaultMongoExtraOptions, options);
12
+ }
13
+ };
14
+ exports.sanatizeTypeOrmOptions = sanatizeTypeOrmOptions;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nathapp/nestjs-data",
3
- "version": "1.0.0-rc.5",
3
+ "version": "1.0.0-rc.7",
4
4
  "description": "@nathapp/nestjs-data",
5
5
  "scripts": {
6
6
  "build": "rimraf dist && tsc -p tsconfig.json",
@@ -1,14 +0,0 @@
1
- import { TypeOrmModuleOptions } from "@nestjs/typeorm";
2
- export interface TypeOrmConnectionFactory {
3
- createModuleOptions(): Promise<TypeOrmModuleOptions> | TypeOrmModuleOptions;
4
- }
5
- export declare class DefaultTypeOrmConnectionFactory implements TypeOrmConnectionFactory {
6
- private connectionString;
7
- private connectionName;
8
- private connectionType;
9
- private extraOptions;
10
- constructor(connectionString: String, connectionName?: String, extraOptions?: any);
11
- private createExtraOptions;
12
- createModuleOptions(): TypeOrmModuleOptions | Promise<TypeOrmModuleOptions>;
13
- static createModuleOptions(connectionString: String, connectionName?: String, extraOptions?: any): TypeOrmModuleOptions | Promise<TypeOrmModuleOptions>;
14
- }
@@ -1,61 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.DefaultTypeOrmConnectionFactory = void 0;
4
- const typeorm_naming_strategies_1 = require("typeorm-naming-strategies");
5
- const defaultExtraOptions = {
6
- entities: [],
7
- logging: false,
8
- synchronize: false,
9
- autoLoadEntities: true,
10
- namingStrategy: new typeorm_naming_strategies_1.SnakeNamingStrategy(),
11
- };
12
- const defaultMysqlExtraOptions = {
13
- supportBigNumbers: true,
14
- bigNumberStrings: true,
15
- };
16
- const defaultMongoExtraOptions = {
17
- useNewUrlParser: true,
18
- useUnifiedTopology: true,
19
- };
20
- class DefaultTypeOrmConnectionFactory {
21
- constructor(connectionString, connectionName, extraOptions) {
22
- this.connectionString = connectionString;
23
- if (!connectionString || connectionString == "") {
24
- throw new Error("connectionString is missing");
25
- }
26
- this.connectionName = connectionName || "default";
27
- const isPresent = connectionString && connectionString != "";
28
- this.connectionType = (isPresent ? this.connectionString.split(":").shift() : null);
29
- this.extraOptions = extraOptions || {};
30
- }
31
- createExtraOptions() {
32
- switch (this.connectionType) {
33
- case "mysql":
34
- case "mariadb":
35
- return Object.assign(defaultExtraOptions, defaultMysqlExtraOptions, this.extraOptions);
36
- case "mongodb":
37
- return Object.assign(defaultExtraOptions, defaultMongoExtraOptions, this.extraOptions);
38
- }
39
- }
40
- createModuleOptions() {
41
- const options = this.createExtraOptions();
42
- switch (this.connectionType) {
43
- case "mysql":
44
- case "mariadb":
45
- case "mongodb":
46
- return {
47
- ...options,
48
- type: this.connectionType,
49
- name: this.connectionName,
50
- url: this.connectionString,
51
- };
52
- default:
53
- throw new Error("Connection Not Supported");
54
- }
55
- }
56
- static createModuleOptions(connectionString, connectionName, extraOptions) {
57
- const factory = new DefaultTypeOrmConnectionFactory(connectionString, connectionName, extraOptions);
58
- return factory.createModuleOptions();
59
- }
60
- }
61
- exports.DefaultTypeOrmConnectionFactory = DefaultTypeOrmConnectionFactory;
@@ -1,2 +0,0 @@
1
- import { TypeOrmConnectionFactory, DefaultTypeOrmConnectionFactory } from "./connection-factory";
2
- export { TypeOrmConnectionFactory, DefaultTypeOrmConnectionFactory, };
@@ -1,5 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.DefaultTypeOrmConnectionFactory = void 0;
4
- const connection_factory_1 = require("./connection-factory");
5
- Object.defineProperty(exports, "DefaultTypeOrmConnectionFactory", { enumerable: true, get: function () { return connection_factory_1.DefaultTypeOrmConnectionFactory; } });