首页 > 解决方案 > Typeorm 在我的电子应用程序中不起作用(this.driver connect 不是函数)

问题描述

当我尝试使用 typeorm 库时,内部连接失败,因为它无法通过connect()方法解析正确的驱动程序。

任何人都有一个想法为什么会这样?这似乎是我在浏览器中无法直接访问 node.js sqlite 驱动程序的问题,但根据 typeorm 文档,这应该可以工作。

所以我现在不知道为什么这不起作用。

//App.tsx

const App = () => {
  useEffect(() => {
    InitDatabase.initDatabase();
    return () => {
      InitDatabase.close();
    };
  }, []);
  ...
}
//InitDatabase.service.ts

...
public static async initDatabase() {
    await this.close();
    this.database =
      await createConnection({
        name: "default",
        type: "sqlite",
        database: "database.sqlite",
        entities: [TimetrackingItem],
        logging: true,
      });
    return this.database;
  }
...
//react.webpack.config.js

const path = require("path");
const HtmlWebpackPlugin = require("html-webpack-plugin");
const FilterWarningsPlugin = require("webpack-filter-warnings-plugin");

const rootPath = path.resolve(
  __dirname,
  "..",
);

module.exports = {
  resolve: {
    extensions: [".tsx", ".ts", ".js"],
    mainFields: [
      "main",
      "module",
      "browser",
    ],
  },
  entry: path.resolve(
    rootPath,
    "src",
    "App.tsx",
  ),
  target: "electron-renderer",
  devtool: "source-map",
  module: {
    rules: [
      {
        test: /\.(js|ts|tsx)$/,
        exclude: /node_modules/,
        use: {
          loader: "babel-loader",
        },
      },
    ],
  },
  devServer: {
    contentBase: path.join(
      rootPath,
      "dist/renderer",
    ),
    historyApiFallback: true,
    compress: true,
    hot: true,
    host: "0.0.0.0",
    port: 4000,
    publicPath: "/",
  },
  output: {
    path: path.resolve(
      rootPath,
      "dist/renderer",
    ),
    filename: "js/[name].js",
    publicPath: "./",
  },
  plugins: [
    new HtmlWebpackPlugin(),
    new FilterWarningsPlugin({
      exclude: [
        /mongodb/,
        /mssql/,
        /mysql/,
        /mysql2/,
        /oracledb/,
        /pg/,
        /pg-native/,
        /pg-query-stream/,
        /react-native-sqlite-storage/,
        /redis/,
        /sql.js/,
        /typeorm-aurora-data-api-driver/,
      ],
    }),
  ],
  externals: {
    sqlite3: "commonjs sqlite3",
  },
};

标签: electrontypeorm

解决方案


推荐阅读