I cannot make Angular work with TypeORM. I put that question some days before. Meanwhile, I've tracked down the cause of the problem.
Now I can present a minimum setup to reproduce the problem. According to my findings, the TypeORM node modules do not work as expected (and as documented).
I set up a fresh Angular project using the following command:
> "C:\Program Files\nodejs\npx.cmd" --yes --package @angular/cli@latest ng new typeorm-problem --defaults --standalone=true
The resulting project installation on a local development server is runnable via
>ng serve
under http://localhost:4200
. This is standard behavior of a fresh Angular installation and displays a welcome screen.
Next, I installed TypeORM using
> npm install typeorm
added 37 packages, and audited 947 packages in 15s
122 packages are looking for funding
run `npm fund` for details
found 0 vulnerabilities
Next, I created the directory src/app/control
and issued an Angular command to generate a data source service:
> ng generate service control/app.datasource
CREATE src/app/control/app.datasource.service.spec.ts (409 bytes)
CREATE src/app/control/app.datasource.service.ts (151 bytes)
import { Injectable } from '@angular/core';
import {DataSource} from "typeorm";
@Injectable({
providedIn: 'root'
})
export class AppDatasourceService {
constructor() { }
}
export const AppDataSource = new DataSource({
type: "mariadb",
port: 3306,
database: "test-db",
host: "test-host",
username: "test-user",
password: "test-password",
entities: ["../model/entities/*.{ts,js}",],
synchronize: true,
logging: true, // set to false on production
});
Running a test on the main application:
>ng test
While the app.datasource
service was not even yet mentioned in any other component, this yields an immediate runtime error:
Uncaught TypeError: Cannot read properties of undefined (reading 'type')
TypeError: Cannot read properties of undefined (reading 'type')
at EntityManagerFactory.create (http://localhost:9876/_karma_webpack_/webpack:/node_modules/typeorm/browser/entity-manager/EntityManagerFactory.js:12:39)
at DataSource.createEntityManager (http://localhost:9876/_karma_webpack_/webpack:/node_modules/typeorm/browser/data-source/DataSource.js:403:43)
at new DataSource (http://localhost:9876/_karma_webpack_/webpack:/node_modules/typeorm/browser/data-source/DataSource.js:54:29)
at Module.7752 (http://localhost:9876/_karma_webpack_/webpack:/src/app/control/app.datasource.service.ts:11:30)
at __webpack_require__ (http://localhost:9876/_karma_webpack_/webpack:/webpack/bootstrap:19:1)
at Module.4004 (http://localhost:9876/_karma_webpack_/main.js:90:81)
at __webpack_require__ (http://localhost:9876/_karma_webpack_/webpack:/webpack/bootstrap:19:1)
at __webpack_exec__ (http://localhost:9876/_karma_webpack_/main.js:198:48)
at http://localhost:9876/_karma_webpack_/main.js:199:126
at Function.__webpack_require__.O (http://localhost:9876/_karma_webpack_/webpack:/webpack/runtime/chunk loaded:23:1)
For me, this not what I expect from the TypeORM modules.
IDE Jetbrains' WebStorm on Windows 10 (19045.4046), AMD FX(tm)-8300 Eight-Core Processor 3.30 GHz
package.json
:
{
"name": "typeorm-problem",
"version": "0.0.0",
"scripts": {
"ng": "ng",
"start": "ng serve",
"build": "ng build",
"watch": "ng build --watch --configuration development",
"test": "ng test"
},
"private": true,
"dependencies": {
"@angular/animations": "^17.2.0",
"@angular/common": "^17.2.0",
"@angular/compiler": "^17.2.0",
"@angular/core": "^17.2.0",
"@angular/forms": "^17.2.0",
"@angular/platform-browser": "^17.2.0",
"@angular/platform-browser-dynamic": "^17.2.0",
"@angular/router": "^17.2.0",
"rxjs": "~7.8.0",
"tslib": "^2.3.0",
"typeorm": "^0.3.20",
"zone.js": "~0.14.3"
},
"devDependencies": {
"@angular-devkit/build-angular": "^17.2.1",
"@angular/cli": "^17.2.1",
"@angular/compiler-cli": "^17.2.0",
"@types/jasmine": "~5.1.0",
"jasmine-core": "~5.1.0",
"karma": "~6.4.0",
"karma-chrome-launcher": "~3.2.0",
"karma-coverage": "~2.2.0",
"karma-jasmine": "~5.1.0",
"karma-jasmine-html-reporter": "~2.1.0",
"typescript": "~5.3.2"
}
}
tsconfig.json
:
/* To learn more about this file see: https://angular.io/config/tsconfig. */
{
"compileOnSave": false,
"compilerOptions": {
"outDir": "./dist/out-tsc",
"forceConsistentCasingInFileNames": true,
"strict": true,
"noImplicitOverride": true,
"noPropertyAccessFromIndexSignature": true,
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": true,
"skipLibCheck": true,
"esModuleInterop": true,
"sourceMap": true,
"declaration": false,
"experimentalDecorators": true,
"moduleResolution": "node",
"importHelpers": true,
"target": "ES2022",
"module": "ES2022",
"useDefineForClassFields": false,
"lib": [
"ES2022",
"dom"
]
},
"angularCompilerOptions": {
"enableI18nLegacyMessageIdFormat": false,
"strictInjectionParameters": true,
"strictInputAccessModifiers": true,
"strictTemplates": true
}
}
angular.json
:
{
"$schema": "./node_modules/@angular/cli/lib/config/schema.json",
"version": 1,
"newProjectRoot": "projects",
"projects": {
"typeorm-problem": {
"projectType": "application",
"schematics": {},
"root": "",
"sourceRoot": "src",
"prefix": "app",
"architect": {
"build": {
"builder": "@angular-devkit/build-angular:application",
"options": {
"outputPath": "dist/typeorm-problem",
"index": "src/index.html",
"browser": "src/main.ts",
"polyfills": [
"zone.js"
],
"tsConfig": "tsconfig.app.json",
"assets": [
"src/favicon.ico",
"src/assets"
],
"styles": [
"src/styles.css"
],
"scripts": []
},
"configurations": {
"production": {
"budgets": [
{
"type": "initial",
"maximumWarning": "500kb",
"maximumError": "1mb"
},
{
"type": "anyComponentStyle",
"maximumWarning": "2kb",
"maximumError": "4kb"
}
],
"outputHashing": "all"
},
"development": {
"optimization": false,
"extractLicenses": false,
"sourceMap": true
}
},
"defaultConfiguration": "production"
},
"serve": {
"builder": "@angular-devkit/build-angular:dev-server",
"configurations": {
"production": {
"buildTarget": "typeorm-problem:build:production"
},
"development": {
"buildTarget": "typeorm-problem:build:development"
}
},
"defaultConfiguration": "development"
},
"extract-i18n": {
"builder": "@angular-devkit/build-angular:extract-i18n",
"options": {
"buildTarget": "typeorm-problem:build"
}
},
"test": {
"builder": "@angular-devkit/build-angular:karma",
"options": {
"polyfills": [
"zone.js",
"zone.js/testing"
],
"tsConfig": "tsconfig.spec.json",
"assets": [
"src/favicon.ico",
"src/assets"
],
"styles": [
"src/styles.css"
],
"scripts": []
}
}
}
}
}
}
What am I doing wrong? What can I do to make TypeORM work in a browser using a standard Angular setup?
My question got an unexpected, yet almost complete answer from this great recent tutorial on youtube.
TL;DR:
My attempt at a solution to the task at hand has been way too straightforward and simple. The real solution consists of a separate database server with its own set of node_modules
without any relation to the Angular part. The Angular application has its own normal ecosystem with another set of node_modules
. Nevertheless, these two parts can live next to each other within one project.
Thanks to everyone who contributed.