我会尽力使这个问题尽可能清楚。
我正在使用NodeJS在后端上工作,NodeJS使用TypeORM与SQL Server数据库进行交互。我使用webpack4建立了一个构建系统,在该系统中,我将TypeORM实体构建为commonjs模块(我的入口点与数据库中的实体一样多)。我有两种单独的Webpack配置,一种用于主文件,另一种用于TypeORM实体。
构建过程正常,没有警告,没有错误,但是当我运行主文件时TypeORM给我
RepositoryNotFoundError: No repository for "Foo" was found. looks like this entity is not registred in the default connection.
使用Typescipt编译器时我没有这个问题。
非常感谢您的帮助。
webpack.config.js:
const path = require('path');
const fs = require('fs');
const nodeExternals = require('webpack-node-externals');
const CleanBuildDir = require('clean-webpack-plugin');
function localExternals(context, request, callback) {
if (/\.\//.test(request)) {
return callback(null, `commonjs ${request}`);
}
return callback();
}
const entitesConfig = {
target: 'node',
mode: 'development',
node: {
__dirname: false,
__filename: false,
},
entry: () => {
const files = fs.readdirSync(path.resolve(__dirname, 'src/entities'));
const entries = {};
files.forEach(file => {
entries[file.split('.')[0]] = path.resolve(__dirname, 'src/entities', file);
});
return entries;
},
output: {
path: path.resolve(__dirname, 'dist/entities'),
filename: '[name].js',
libraryTarget: 'commonjs',
},
resolve: {
extensions: ['.ts', '.js'],
},
externals: [nodeExternals(), localExternals],
module: {
rules: [
{
test: /\.(t|j)s$/,
exclude: /node_modules/,
loader: 'babel-loader',
},
],
},
plugins: [new CleanBuildDir()],
};
const mainConfig = {
target: 'node',
mode: 'development',
node: {
__dirname: false,
__filename: false,
},
entry: path.resolve(__dirname, 'src/main.ts'),
output: {
path: path.resolve(__dirname, 'dist'),
filename: '[name].js',
},
resolve: {
extensions: ['.ts', '.js'],
},
externals: [nodeExternals(), localExternals],
module: {
rules: [
{
test: /\.(t|j)s$/,
exclude: /node_modules/,
loader: 'babel-loader',
},
],
},
plugins: [new CleanBuildDir()],
};
module.exports = [entitesConfig, mainConfig];
ormconfig.js:
const path = require('path');
const fs = require('fs');
const entitiesFiles = fs
.readdirSync(path.resolve(__dirname, 'dist/entities'))
.map(file => path.resolve(__dirname, 'dist/entities', file));
module.exports = {
name: 'default',
type: 'mssql',
host: '127.0.0.1',
port: 1433,
username: '*****',
password: '*****',
database: 'Foo',
schema: 'dbo',
synchronize: false,
entities: entitiesFiles,
};
import { createConnection } from 'typeorm';
import { Foo } from './entities/Foo';
import { performance } from 'perf_hooks';
function selection(table: string, columns: string[]): string[] {
return columns.map(col => `${table}.${col}`);
}
async function job(server: string) {
const connection = await createConnection();
const start = performance.now();
const foos = await connection
.getRepository(Foo)
.createQueryBuilder('foo')
.leftJoinAndSelect('affaire.bar', 'bar')
.orderBy('affaire.Code_Affaire', 'DESC')
.getMany();
console.log(`Done in ${performance.now() - start} ms`);
console.log(foos);
}
job('ANY').catch(err => console.log(`Error in job : ${err}`));
babel.config.js:
const presets = ['@babel/typescript', ['@babel/env', { useBuiltIns: 'usage', targets: { node: 'current' } }]];
const plugins = [
['@babel/proposal-decorators', { decoratorsBeforeExport: true }],
'@babel/proposal-class-properties',
'@babel/proposal-object-rest-spread',
];
module.exports = { presets, plugins };
答案 0 :(得分:0)
我设法通过将@babel/proposal-decorators
的选项从decoratorsBeforeExport: true
更改为legacy: true
并将loose: true
添加到@babel/proposal-class-properties
插件来使其工作。
新babel.config.js:
const presets = ['@babel/typescript', ['@babel/env', { useBuiltIns: 'usage', targets: { node: 'current' } }]];
const plugins = [
['@babel/proposal-decorators', { legacy: true }],
['@babel/proposal-class-properties', { loose: true }],
'@babel/proposal-object-rest-spread',
];
module.exports = { presets, plugins };