【发布时间】:2022-08-17 07:27:09
【问题描述】:
我是 Typescript 的新人,而 Sequelize 是 100% 的新人。 我只在 .config/config 文件中做了与 postgres 配置相关的更改,只是为了添加 dev db 配置,如下所示:
export const config = {
\"dev\": {
\"username\": \"postgres\",
\"password\": \"postgres\",
\"database\": \"baseDB\",
\"host\": \"localhost\",
\"dialect\": \"postgres\"
},
...
}
数据库在我的本地 PC 上的 docker 容器上运行,我已连接到容器,并且可以使用连接到容器的 DataGrip 导航数据库,这意味着数据库配置是正确的。
查看我的配置:
包.json 文件
{
\"name\": \"sample-restapi\",
\"version\": \"1.0.0\",
\"description\": \"\",
\"main\": \"server.js\",
\"scripts\": {
\"start\": \"node server.js\",
\"tsc\": \"tsc\",
\"dev\": \"ts-node-dev ./src/server.ts\",
\"prod\": \"tsc && node ./www/server.js\",
\"clean\": \"rimraf www/ || true\",
\"build\": \"npm run clean && tsc && cp -rf src/config www/config && cp .npmrc www/.npmrc && cp package.json www/package.json && cd www && zip -r Archive.zip . && cd ..\",
\"test\": \"echo \\\"Error: no test specified\\\" && exit 1\"
},
\"keywords\": [],
\"author\": \"Name here\",
\"license\": \"\",
\"dependencies\": {
\"@types/bcrypt\": \"^3.0.0\",
\"@types/jsonwebtoken\": \"^8.3.2\",
\"aws-sdk\": \"^2.492.0\",
\"bcrypt\": \"^5.0.0\",
\"body-parser\": \"^1.19.0\",
\"email-validator\": \"^2.0.4\",
\"express\": \"^4.17.1\",
\"jsonwebtoken\": \"^8.5.1\",
\"pg\": \"^7.11.0\",
\"pg-hstore\": \"^2.3.4\",
\"reflect-metadata\": \"^0.1.13\",
\"sequelize\": \"^5.10.0\",
\"sequelize-typescript\": \"^0.6.11\"
},
\"devDependencies\": {
\"@types/bluebird\": \"^3.5.33\",
\"@types/express\": \"^4.17.9\",
\"@types/node\": \"^11.15.42\",
\"@types/sequelize\": \"^4.28.9\",
\"@types/validator\": \"^10.11.3\",
\"chai\": \"^4.2.0\",
\"chai-http\": \"^4.3.0\",
\"mocha\": \"^6.2.3\",
\"rimraf\": \"^3.0.2\",
\"ts-node-dev\": \"^1.1.1\",
\"tslint\": \"^5.20.1\",
\"typescript\": \"^3.9.7\"
}
}
配置文件
export const config = {
\"dev\": {
\"username\": \"postgres\",
\"password\": \"postgres\",
\"database\": \"baseDB\",
\"host\": \"localhost\",
\"dialect\": \"postgres\",
\"aws_region\": \"us-east-2\",
\"aws_profile\": \"default\",
\"aws_media_bucket\": \"udagram-ruttner-dev\"
},
\"jwt\": {
\"secret\": \" \"
},
\"prod\": {
\"username\": \"\",
\"password\": \"\",
\"database\": \"udagram_prod\",
\"host\": \"\",
\"dialect\": \"postgres\"
}
}
续集配置
import {Sequelize} from \'sequelize-typescript\';
import { config } from \'./config/config\';
const c = config.dev;
// Instantiate new Sequelize instance!
export const sequelize = new Sequelize({
\"username\": c.username,
\"password\": c.password,
\"database\": c.database,
\"host\": c.host,
dialect: \'postgres\',
storage: \':memory:\',
});
服务器.ts
import express from \'express\';
import { sequelize } from \'./sequelize\';
import { IndexRouter } from \'./controllers/v0/index.router\';
import bodyParser from \'body-parser\';
import { V0MODELS } from \'./controllers/v0/model.index\';
(async () => {
await sequelize.addModels(V0MODELS);
/*THE APPLICATION STOP IN THIS LINE*/
await sequelize.sync();
/*NOTHING BELOW IS EXECUTED AND NO ERROR LOGGED EVEN ADDING A TRY CATCH
THERE ARE NO ERRORS*/
const app = express();
const port = process.env.PORT || 8080; // default port to listen
app.use(bodyParser.json());
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` );
} );
})();
V0模型定义
import { FeedItem } from \'./feed/models/FeedItem\';
import { User } from \'./users/models/User\';
export const V0MODELS = [ FeedItem, User ];
每次我尝试运行npm run dev
我没有错误,什么都没有,应用程序在此行之后停止,没有执行任何其他操作
await sequelize.sync();
我正在使用节点版本:v16.14.0,npm 版本:8.3.1
我究竟做错了什么? 注意:您可以假设 npm i 是我运行的第一件事。
控制台输出:
PS path\\simple-restapi> npm run dev
> udacity-c2-restapi@1.0.0 dev
> ts-node-dev ./src/server.ts
[INFO] 12:56:52 ts-node-dev ver. 1.1.1 (using ts-node ver. 9.1.1, typescript ver. 3.9.7)
sequelize config: {
dialect: \'postgres\',
dialectModule: null,
dialectModulePath: null,
host: \'localhost\',
protocol: \'tcp\',
define: { timestamps: false, freezeTableName: true },
query: {},
sync: {},
timezone: \'+00:00\',
clientMinMessages: \'warning\',
standardConformingStrings: true,
logging: [Function: log],
omitNull: false,
native: false,
replication: false,
ssl: undefined,
pool: {},
quoteIdentifiers: true,
hooks: {},
retry: { max: 5, match: [ \'SQLITE_BUSY: database is locked\' ] },
transactionType: \'DEFERRED\',
isolationLevel: null,
databaseVersion: 0,
typeValidation: false,
benchmark: false,
minifyAliases: false,
logQueryParameters: false,
username: \'postgres\',
password: \'postgres\',
database: \'baseDB\',
storage: \':memory:\'
}
sequelize: Attempting to add models to sequelize...
sequelize: Models added to sequelize!!!
sequelize: Attempting to sync...
PS path\\simple-restapi>
不知何故正在加载 postgres 配置,但它看起来指向 SQLite,我该如何避免这种情况?
标签: node.js typescript sequelize.js sequelize-typescript