在Nuxt 3中集成TypeORM遇元数据错误,求解决方案
问题:Nuxt 3 集成 TypeORM(ESM)及 Oracle 适配方案
背景操作
- 按官方指引创建 TypeORM ESM 项目:
npx typeorm init --name MyProject --database sqlite --module esm,运行正常 - 创建 Nuxt 3 项目:
npx nuxi init nuxt-project,补充如下配置:
package.json 配置
{ "private": true, "scripts": { "build": "nuxt build", "dev": "nuxt dev", "generate": "nuxt generate", "preview": "nuxt preview", "postinstall": "nuxt prepare", "start": "node --loader ts-node/esm src/index.ts", "typeorm": "typeorm-ts-node-esm" }, "devDependencies": { "@types/node": "^18.11.17", "nuxt": "3.0.0", "ts-node": "10.9.1", "typescript": "4.9.4" }, "dependencies": { "@npmcli/fs": "^3.1.0", "reflect-metadata": "^0.1.13", "sqlite3": "^5.1.4", "typeorm": "^0.3.11" } }
tsconfig.json 配置
{ // https://nuxt.com/docs/guide/concepts/typescript "extends": "./.nuxt/tsconfig.json", "compilerOptions": { "lib": [ "es2021" ], "target": "es2021", "module": "es2022", "moduleResolution": "node", "allowSyntheticDefaultImports": true, "outDir": "./build", "emitDecoratorMetadata": true, "experimentalDecorators": true, "sourceMap": true } }
- 复制实体类、已配置的 data-source,在 App.vue 中添加代码:
<script setup lang="ts"> import "reflect-metadata" import { User } from "./db/entity/User.js" import { AppDataSource } from "./db/data-source"; AppDataSource.initialize().then(async () => { const user = new User() user.firstName = "Timber" user.lastName = "Saw" user.age = 25 await AppDataSource.manager.save(user) const users = await AppDataSource.manager.find(User) console.log("Loaded users: ", users) }).catch(error => console.log(error)) </script> <template> <div> <NuxtWelcome /> </div> </template>
遇到的错误
TypeORM 错误
[nuxt] [request error] [unhandled] [500] Column type for User#firstName is not defined and cannot be guessed. Make sure you have turned on an "emitDecoratorMetadata": true option in tsconfig.json. Also make sure you have imported "reflect-metadata" on top of the main entry file in your application (before any entity imported).If you are using JavaScript instead of TypeScript you must explicitly provide a column type.
已尝试在实体导入前引入 reflect-metadata,但问题未解决
Sequelize 错误
此前尝试 Sequelize 时也报错:
Could not resolve "pg-hstore": const hstore = require("pg-hstore")
需求
需要支持 Oracle 数据库的 ORM 工具(排除 Prisma),求解决方案或示例。
内容的提问来源于stack exchange,提问作者vova
相关产品推荐
相关产品推荐

