【问题标题】:NativeScript 7 with sqlite and typeormNativeScript 7 与 sqlite 和 typeorm
【发布时间】:2021-01-08 20:31:24
【问题描述】:

对于一个小测试项目,我尝试使用 sqlite db 和 typeorm 启动 NativeScript(Angular 风格)。

ns create sample-typeorm --ng
cd sample-typeorm
ns plugin add nativescript-sqlite
npm i typeorm stream-browserify timers-browserify

由于缺少 nodejs 模块“流”和“计时器”,我的 tsconfig.json 看起来像

{
  "compilerOptions": {
    "module": "esnext",
    "target": "es2017",
    "moduleResolution": "node",
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true,
    "noEmitHelpers": true,
    "noEmitOnError": true,
    "skipLibCheck": true,
    "lib": [
      "es2018",
      "es2017",
      "dom",
      "es6"
    ],
    "baseUrl": ".",
    "paths": {
      "~/*": [
        "app/*"
      ],
      "timers": [
        "node_modules/timers-browserify"
      ],
      "stream": [
        "node_modules/stream-browserify"
      ]
    }
  },
  "include": [
    "src/tests/**/*.ts",
    "src/**/*.ios.ts",
    "src/**/*.android.ts"
  ],
  "files": [
    "./references.d.ts",
    "./src/main.ts"
  ],
  "exclude": [
    "node_modules",
    "platforms",
    "e2e"
  ]
}

为了开始typoeorm/sqlite 测试,我在src/main.ts 中添加了“createConnection”内容:

// this import should be first in order to load some required settings (like globals and reflect-metadata)
import {platformNativeScriptDynamic} from "@nativescript/angular";

import {AppModule} from "./app/app.module";
import {createConnection} from "typeorm/browser";

let driver = require('nativescript-sqlite');

platformNativeScriptDynamic().bootstrapModule(AppModule);


(async () => {
    try {
        const connection = await createConnection({
            database: 'test.db',
            type: 'nativescript',
            entities: [
                //... whatever entities you have
            ],
            driver,
            logging: true,
            synchronize: false
        })
    } catch (e) {
        console.error(e)
    }
    ;
})();

我启动应用程序

ns run ios --emulator

在构建系统工作一段时间后,出现以下错误:

ERROR in ../node_modules/app-root-path/lib/resolve.js
Module not found: Error: Can't resolve 'module' in '/private/tmp/sample-typeorm/node_modules/app-root-path/lib'
 @ ../node_modules/app-root-path/lib/resolve.js 7:18-35
 @ ../node_modules/app-root-path/lib/app-root-path.js
 @ ../node_modules/app-root-path/index.js
 @ ../node_modules/typeorm/browser/connection/ConnectionOptionsReader.js
 @ ../node_modules/typeorm/browser/index.js
 @ ./main.ts

node_modules/app-root-path/lib/resolve.js中是错误的来源:

var globalPaths = require('module').globalPaths;

“模块”模块是否有等效的浏览器?或者 webpack 有没有一个选项可以避免这个错误?

为了更进一步,我修改了resolve.js 以跳过“模块”并遇到下一个错误,这让我想到我还有另一个丢失/未配置的 webpack 选项:

Terminating app due to uncaught exception 'NativeScript encountered a fatal error: Uncaught TypeError: Cannot destructure property 'env' of 'global.process' as it is undefined.
at
../node_modules/supports-color/index.js(file: node_modules/supports-color/index.js:6:7)

node_modules/supports-color/index.js是这样的:

const {env} = process;

webpack.config.js 没有受到“ns create ...”的影响。 有没有人运行 NativeScript 7 的 typeorm 示例?或者知道缺少哪个模块?

【问题讨论】:

  • 我被困在同一个地方,但是我没有添加流和计时器的 broswerify 版本。相反,我使用了以下 ns nodeify 插件、natievscript-xml2js lib、@nativescript-community/typeorm lib。我还尝试了npm i module,这导致了您的最后一个错误。 Typeorm 是一个很棒的库,希望有人能阐明如何解决。

标签: webpack nativescript typeorm nativescript-angular


【解决方案1】:

我已经能够通过将 typeorm 降级到 0.2.25 版本来使其工作。我发现构建从 tyeporm 0.2.26 开始抛出错误(不确定问题出在哪里)。这是我的 package.json 的样子,所有其他入门模板文件都没有改动。

package.json

{
  "name": "@nativescript/template-blank-ng",
  "main": "main.js",
  "displayName": "Blank",
  "templateType": "App template",
  "version": "7.0.8",
  "description": "NativeScript Application",
  "author": "NativeScript Team <oss@nativescript.org>",
  "license": "SEE LICENSE IN <your-license-filename>",
  "publishConfig": {
    "access": "public"
  },
  "keywords": [
    "nstudio",
    "nativescript",
    "mobile",
    "angular",
    "{N}",
    "tns",
    "template",
    "category-general"
  ],
  "repository": "<fill-your-repository-here>",
  "homepage": "https://github.com/NativeScript/nativescript-app-templates",
  "bugs": {
    "url": "https://github.com/NativeScript/NativeScript/issues"
  },
  "dependencies": {
    "@angular/animations": "~11.0.0",
    "@angular/common": "~11.0.0",
    "@angular/compiler": "~11.0.0",
    "@angular/core": "~11.0.0",
    "@angular/forms": "~11.0.0",
    "@angular/platform-browser": "~11.0.0",
    "@angular/platform-browser-dynamic": "~11.0.0",
    "@angular/router": "~11.0.0",
    "@nativescript/angular": "~11.0.0",
    "@nativescript/core": "~7.1.0",
    "@nativescript/theme": "~3.0.0",
    "nativescript-nodeify": "^0.8.0",
    "nativescript-sqlite": "^2.6.6",
    "reflect-metadata": "~0.1.12",
    "rxjs": "^6.6.0",
    "typeorm": "0.2.25",
    "zone.js": "~0.11.1"
  },
  "devDependencies": {
    "@angular/compiler-cli": "~11.0.0",
    "@nativescript/android": "7.0.1",
    "@nativescript/webpack": "~4.0.0",
    "@ngtools/webpack": "~11.0.0",
    "typescript": "~4.0.0"
  },
  "private": "true",
  "readme": "NativeScript Application"
}

启动db的代码(OrmConfig.ts)如下:

import { createConnection } from 'typeorm/browser';
import { LocalAppUser } from './home/LocalAppUser';

// need the commercial version for types
const driver = require('nativescript-sqlite');

export class OrmConfig {

    static async init(): Promise<void> {
        try {

            const connection = await createConnection({
                //multithreading: true,
                database: 'local.db',
                type: 'nativescript',
                driver,
                entities: [
                    LocalAppUser
                ],
                logging: ["schema","error"]
            });
            // setting true will drop tables and recreate
            await connection.synchronize(false);

        } catch (err) {
            console.error(err);
        }
    }

}

像这样在main.ts中加载

// this import should be first in order to load some required settings (like globals and reflect-metadata)
import { platformNativeScriptDynamic } from "@nativescript/angular";

import { AppModule } from "./app/app.module";
import { OrmConfig } from './app/OrmConfig';

(async () => {
    await OrmConfig.init();
})();

platformNativeScriptDynamic().bootstrapModule(AppModule);

【讨论】:

    猜你喜欢
    • 2020-06-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-13
    • 2020-01-03
    • 2016-10-26
    相关资源
    最近更新 更多