2016-11-23 24 views
1

私は簡潔にするため、これらのオブジェクトののDTOを呼んでいるが、彼らはちょうど束されている活字体2 Expressサーバと角2デコレータがTypeScriptでノードモジュールをインポートできないようにするにはどうすればよいですか?

間のDTO/DAOパターンを作成するために、typescriptですでtypeormライブラリを使用しようとしていますフィールド、および注釈が含まれます。

import {autoserialize} from "cerialize"; 
import {DTOReport} from "./reports.dto"; 

import { PrimaryGeneratedColumn} from "typeorm/decorator/columns/PrimaryGeneratedColumn" 
import { CreateDateColumn } from "typeorm/decorator/columns/CreateDateColumn"; 
import { Column } from "typeorm/decorator/columns/Column"; 
import { JoinColumn } from "typeorm/decorator/relations/JoinColumn"; 
import { OneToOne } from "typeorm/decorator/relations/OneToOne"; 
import { ManyToOne } from "typeorm/decorator/relations/ManyToOne"; 
import { OneToMany } from "typeorm/decorator/relations/OneToMany"; 
import { Table } from "typeorm/decorator/tables/Table"; 
import { ColumnTypes } from "typeorm/metadata/types/ColumnTypes"; 

@Table() 
export class DTOSourceFile { 
    @PrimaryGeneratedColumn() 
    @autoserialize 
    id: number; 

    @Column() 
    @autoserialize 
    locationURL: string; 

    @CreateDateColumn() 
    @autoserialize 
    createdAt: Date; 

    @OneToMany(type => DTOReport, report => report.source, {nullable: true}) 
    @autoserialize report: DTOReport; 
    @autoserialize reportId: number; 

    @Column(ColumnTypes.TEXT) 
    @autoserialize 
    originalname: string; 

    @Column(ColumnTypes.JSON) 
    @autoserialize 
    mutler: string; 
} 

デコレータのみを慎重にインポートしています。しかし、コンパイル時には、要求がルートのindex.tsファイルに戻ってくるのがわかります。

"use strict"; 
const ColumnTypes_1 = require("../../metadata/types/ColumnTypes"); 
const ColumnTypeUndefinedError_1 = require("../error/ColumnTypeUndefinedError"); 
const index_1 = require("../../index"); // < --- THIS 
const PrimaryColumnCannotBeNullableError_1 = require("../error/PrimaryColumnCannotBeNullableError"); 
/** 
* Column decorator is used to mark a specific class property as a table column. 
* Only properties decorated with this decorator will be persisted to the database when entity be saved. 
* Primary columns also creates a PRIMARY KEY for this column in a db. 
*/ 
function PrimaryColumn(typeOrOptions, options) { 
    let type; 
    if (typeof typeOrOptions === "string") { 
     type = typeOrOptions; 
    } 
    else { 
     options = typeOrOptions; 
    } 
    return function (object, propertyName) { 
     const reflectedType = ColumnTypes_1.ColumnTypes.typeToString(Reflect.getMetadata("design:type", object, propertyName)); 
     // if type is not given implicitly then try to guess it 
     if (!type) 
      type = ColumnTypes_1.ColumnTypes.determineTypeFromFunction(Reflect.getMetadata("design:type", object, propertyName)); 
     // if column options are not given then create a new empty options 
     if (!options) 
      options = {}; 
     // check if there is no type in column options then set type from first function argument, or guessed one 
     if (!options.type) 
      options = Object.assign({ type: type }, options); 
     // if we still don't have a type then we need to give error to user that type is required 
     if (!options.type) 
      throw new ColumnTypeUndefinedError_1.ColumnTypeUndefinedError(object, propertyName); 
     // check if column is not nullable, because we cannot allow a primary key to be nullable 
     if (options.nullable) 
      throw new PrimaryColumnCannotBeNullableError_1.PrimaryColumnCannotBeNullableError(object, propertyName); 
     // implicitly set a primary to column options 
     options = Object.assign({ primary: true }, options); 
     // create and register a new column metadata 
     const args = { 
      target: object.constructor, 
      propertyName: propertyName, 
      propertyType: reflectedType, 
      mode: "regular", 
      options: options 
     }; 
     index_1.getMetadataArgsStorage().columns.add(args); // < --- THIS 
    }; 
} 
exports.PrimaryColumn = PrimaryColumn; 

//# sourceMappingURL=PrimaryColumn.js.map 

Angular 2のwebpackコンパイラの結果として、ノードの依存関係を読み込もうとすると、明らかに問題が示されます。

WARNING in ./~/typeorm/driver/sqlserver/SqlServerDriver.js 
Module not found: Error: Can't resolve 'mssql' in '/Users/jmurphy/projects/ubq/web/node_modules/typeorm/driver/sqlserver' 
@ ./~/typeorm/driver/sqlserver/SqlServerDriver.js 256:25-41 
@ ./~/typeorm/connection/ConnectionManager.js 
@ ./~/typeorm/index.js 
@ ./~/typeorm/decorator/columns/PrimaryGeneratedColumn.js 
@ ./src/app/dtos/lens.dto.ts 
@ ./src/app/lens/lens.component.ts 
@ ./src/app/app.module.ts 
@ ./src/app/index.ts 
@ ./src/main.ts 
@ multi main 
Child html-webpack-plugin for "index.html": 
     Asset  Size Chunks  Chunk Names 
    index.html 2.88 kB  0 
webpack: bundle is now VALID. 

TypeORMのデコレータは、ノードの依存関係をロードしようとせず、ブラウザのプロジェクトに存在することができるようにする方法はありますか?

リンク:

TypeORM出典: https://github.com/typeorm/typeorm/blob/master/src/decorator/columns/PrimaryColumn.ts

+0

PrimaryColumn.tsのソースコードを含めることはできますか? –

+1

@OweRReLoaDeD - 私はリンク –

+0

を追加したことがあるかもしれませんが、これは正しく理解されていないかもしれませんが、TSにインポートがあります。https://github.com/typeorm/typeorm/blob/master/src/decorator/columns/PrimaryColumn .ts#L4なぜJSにインポートがあるのですか? –

答えて

1

私はこのライブラリは、Webブラウザで動作するように設計されていないと思いますが、おそらく解決策があります。デコレータは、/index.tsで宣言されたgetMetadataArgsStorage関数を使用します。 /index.tsは、あなたを問題に導く多くのものをインポートします。

あなたはgetMetadataArgsStorage機能がdefaultContainerMetadataArgsStorageを使用していますhereどのように見ることができます。 getMetadataArgsStorageが新しいファイルにindex.tsから移動された場合は

多分それはあなたのためのに動作しますが、あなたはGitHubでのプロジェクトへのPRを送信する必要があるとしています。私はあなたがtypeormの作者にあなたのユースケースを説明することをお勧めします

import * as fs from "fs"; 
import {Connection} from "./Connection"; 
import {ConnectionNotFoundError} from "./error/ConnectionNotFoundError"; 
import {MysqlDriver} from "../driver/mysql/MysqlDriver"; 
import {ConnectionOptions} from "./ConnectionOptions"; 
import {DriverOptions} from "../driver/DriverOptions"; 
import {Driver} from "../driver/Driver"; 
import {MissingDriverError} from "./error/MissingDriverError"; 
import {PostgresDriver} from "../driver/postgres/PostgresDriver"; 
import {AlreadyHasActiveConnectionError} from "./error/AlreadyHasActiveConnectionError"; 
import {Logger} from "../logger/Logger"; 
import {SqliteDriver} from "../driver/sqlite/SqliteDriver"; 
import {OracleDriver} from "../driver/oracle/OracleDriver"; 
import {SqlServerDriver} from "../driver/sqlserver/SqlServerDriver"; 
import {OrmUtils} from "../util/OrmUtils"; 
import {CannotDetermineConnectionOptionsError} from "./error/CannotDetermineConnectionOptionsError"; 

/** 
* ConnectionManager is used to store and manage all these different connections. 
* It also provides useful factory methods to simplify connection creation. 
*/ 
export class ConnectionManager { 
// ... 

問題はthis lineConnectionManagerため輸入するすべてのドライバです。また、すべてのドライバがデフォルトで常にインポートされているとは限りません。必要なドライバのみをロードする方が良いでしょう(あなたのケースではどれも)。

+2

私はちょうどあなたが輪郭を描いたものをここにフォークを作成しました:https://github.com/rightisleft/typeorm/commit/84baefd7e1ecfb7024b724ec6f94c8797bd61798 –

+1

それがうまくいけば、PRの元の著者にあなたのユースケースを説明するでしょうそれ。がんばろう ;) –

関連する問題