node.js - 无法使用 Sequelize ORM 连接到 AWS RDS
问题描述
我正在开发一个使用 Sequelize ORM 连接到 AWS RDS 的应用程序。我的连接设置如下:
联系
import {Sequelize} from 'sequelize-typescript';
// Instantiate new Sequelize instance!
export const sequelize = new Sequelize({
"username": "AWS RDS USER",
"password": "AWS RDS PASS",
"database": "postgres",
"host": "******.******.us-east-1.rds.amazonaws.com",
dialect: 'postgres',
storage: ':memory:',
});
我还定义了一个模型来表示定义如下的数据库表:
模型
import {Table, Column, Model, CreatedAt, UpdatedAt} from 'sequelize-typescript';
@Table
export class FeedItem extends Model<FeedItem> {
@Column
public caption!: string;
@Column
public url!: string;
@Column
@CreatedAt
public createdAt: Date = new Date();
@Column
@UpdatedAt
public updatedAt: Date = new Date();
}
并像这样导出:
import { FeedItem } from './feed/models/FeedItem';
export const V0MODELS = [ FeedItem ];
然后在我的server.ts中,我导入我的 sequelize 连接和模型,并尝试连接到我的 AWS RDS:
服务器.ts
import express from 'express';
import { sequelize } from './sequelize';
import { IndexRouter } from './controllers/v0/index.router';
import { V0MODELS } from './controllers/v0/model.index';
(async () => {
try {
await sequelize.authenticate();
console.log('Connection has been established successfully.');
await sequelize.addModels(V0MODELS);
await sequelize.sync({ force: true, logging: console.log });
} catch (error) {
console.error(error);
}
const app = express();
const port = process.env.PORT || 8080; // default port to listen
app.use(express.json());
//CORS Should be restricted
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "http://localhost:8100");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, Authorization");
next();
});
app.use('/api/v0/', IndexRouter)
// Root URI call
app.get( "/", async ( req, res ) => {
res.send( "/api/v0/" );
} );
// Start the Server
app.listen( port, () => {
console.log( `server running http://localhost:${ port }` );
console.log( `press CTRL+C to stop server` );
} );
})();
当我运行程序时,没有建立连接,服务器无法启动。当我删除该sequelize.sync
方法时,服务器将启动,但未创建我的表。块没有捕获到错误,catch
所以我不怀疑有错误。目前我确实认为这是处理 postgres 和 AWS 的连接问题,但我似乎无法确定它。感谢所有反馈和指导。
解决方案
我发现了这个问题。问题是由于我使用的节点版本,当时是 14.15.3。这个版本与我当前的 postgres 13.1 版本不兼容,所以我使用 nvm 并降级到节点 11.15.0,现在我的 sequelize 命令正在工作。
推荐阅读
- r - 应用函数循环遍历R中数组的3维中的2维
- reactjs - 如何在玩笑/酶中模拟“通用饼干”?
- c++ - 如何在公共类方法中初始化公共变量
- python - Python Regex:在至少具有一个已知键:值对的字符串中查找 json 格式
- python - python pandas合并内存总线错误破坏python环境
- javascript - LocalStorage 项目被新项目覆盖
- java - Java 无法添加新的列表项
- regex - Unix 正则表达式仅列出具有有效版本名称的目录
- azure - Terraform 计划在错误地声称它们不存在的数据元素上失败
- python - 如何将文件中的差异写入 Python 中的新文件?