从typescript到postgres的连接挂起
我正在尝试从typescript连接到Postgres数据库。我正在使用从typescript到postgres的连接挂起,typescript,postgresql,sequelize-typescript,Typescript,Postgresql,Sequelize Typescript,我正在尝试从typescript连接到Postgres数据库。我正在使用sequelize typescript作为ORM。我的问题是脚本被困在wait sequelize.sync()上。下面是sequelize.ts文件: import {Sequelize} from 'sequelize-typescript'; import { config } from './config/config'; const c = config.dev; // Instantiate new Se
sequelize typescript
作为ORM。我的问题是脚本被困在wait sequelize.sync()上代码>。下面是sequelize.ts
文件:
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:',
});
下面是模型类文件Product.ts
:
import {Table, Column, Model, HasMany, PrimaryKey, CreatedAt, UpdatedAt, ForeignKey} from 'sequelize-typescript';
@Table
export class Product extends Model<Product> {
@Column
public brand: string;
@Column
public price: number;
@Column
public description!: string;
@Column
public imgUrl!: string;
@Column
public cateogry: string;
}
下面是package.json
内容:
{
"name": "bagsshoes-server",
"version": "1.0.0",
"description": "",
"main": "src/server.js",
"scripts": {
"start": "node .",
"tsc": "tsc",
"dev": "ts-node-dev --respawn --transpile-only ./src/server.ts",
"prod": "tsc && node ./www/server.js",
"clean": "rd /s /q www/>nul 2>&1|echo.>nul",
"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": "Fahima Mokhtari",
"license": "ISC",
"dependencies": {
"@types/bcrypt": "^3.0.0",
"@types/jsonwebtoken": "^8.3.2",
"bcrypt": "^3.0.6",
"body-parser": "^1.19.0",
"email-validator": "^2.0.4",
"express": "^4.17.1",
"jsonwebtoken": "^8.5.1",
"pg": "^7.9.0",
"reflect-metadata": "^0.1.13",
"sequelize": "^5.3.5",
"sequelize-typescript": "^0.6.9"
},
"devDependencies": {
"@types/bluebird": "^3.5.27",
"@types/express": "^4.17.0",
"@types/node": "^11.13.17",
"@types/sequelize": "^4.27.44",
"@types/validator": "^10.11.1",
"chai": "^4.2.0",
"chai-http": "^4.3.0",
"mocha": "^6.1.4",
"ts-node-dev": "^1.0.0-pre.40",
"tslint": "^5.18.0",
"typescript": "^3.5.3"
}
}
我尝试了try
和catch
,但没有显示错误,这让我更加困惑。
任何帮助都将不胜感激
PS:
我正在使用windows和节点V14.7.0注意
TLD!(撇去太久了)!答案是漫长而丰富的!你可以浏览!它的格式很好
如果您非常匆忙!
您可以检查验证部分、续集类型脚本(非续集)部分、续集类型脚本部分
而且更好,你可以直接去地狱区!了解nodejsv14地狱!(直接走到尽头!稍高一点)
检查太多修复(Postgres v14地狱)
我开始了,不知不觉中我发现自己写得太多了
超级指南
基本上续集不应该只是挂起来!但是抛出一个错误
查看代码源代码
通过查看sync
code
你很容易就能看出悬而未决的可能性
登录中
要调试此类异常,首先必须有良好的日志记录
您可以在这里查看如何添加日志记录!即使通常sequelize在默认情况下也会激活查询日志
检查身份验证
如果没有发生日志记录!这可能意味着sequelize没有做任何事情,只是挂在一开始!测试身份验证和连接是否正常工作
您可以使用身份验证进行测试:
命令之一!确保你没有忘记
更好地理解正在发生的事情并进行调试!(代码源日志记录)
调试的最佳方式!真正确定问题发生在哪里!是通过将日志添加到源代码本身来实现的!对我来说,一个快速的方法是直接在node\u模块上这样做
。我在sequelize回购上打开了git!进行搜索!已确定同步
,验证
,查询
的位置!所有文件都驻留在sequelize.js中!你可以查一下!您可以按住CTRL+F键进入方法>身份验证(
[添加(
])。任何方式!您都可以在节点模块中执行相同操作。
!并开始添加日志!您将知道问题发生在哪个部分!这有助于您调试问题
另一种方法是使用叉子,然后使用你的叉子!这样做会更好
但是,嗯!node\u模块是一种快速的方法!你也可以复制一份!以确保你不会丢失日志!一旦你更新!最后只需删除整个模块并重新安装即可清理!或者只需反向创建日志(撤消)!我发现这是一种有趣的调试方法
但它应该抛出一个错误
通常应该!通过查看代码源,您可以更好地了解!通常应该抛出错误!但是如果进程挂起!并且没有抛出错误!那么您可以预期这样的结果!这可能是驱动程序丢失!还要确保console.log
。工作正常!最后一件事!嗯,可能是它有问题nodejs
本身(见最后一节)
我使用的是sequelize类型脚本(不是sequelize)
非常重要的是要知道!Sequelize typescript只是Sequelize包装器!它是用来添加typescript支持的!它提供了装饰程序和一些功能!同样来自Sequelize v5!在Sequelize中直接支持typescript!请参见此处
sequelize typescript在最新版本中!改为使用sequelize的本机声明类型
作为sequelize TypeScript包装sequelize!确保验证sequelize文档
还有一点需要注意的是,有一些人高喊着:不要使用装饰师!嗯!嗯!还有另一个嗯!
续写打字脚本
如果您正在使用sequelize typescript,请确保sequelize typescript和sequelize的版本匹配!根据sequelize的文档V5
!我想V6
也应该匹配!sequelize typescript的v1
npm安装sequelize@5续集-typescript@1
别忘了根据there doc!所需的typescript文件包!
(您可以检查和验证文档本身中的所有信息)
为什么要使用sequelize类型脚本?
如前所述!Sequelize从V5
开始对typescript提供本机支持。根据。那么为什么要在它上面使用包装器呢!它也使用装饰器!(我不反对装饰器!有些是!根据)
问问你自己为什么?有什么可以续写typescript的吗!这是与原生方式相比的一个重要优点?如果有明确的事情!请在评论中提及!我会更新!这一部分
如果不是这样的话!本地的可能更好!一个依赖项或更少的多个
项目配置
tsconfig
{
“编译器选项”:{
“模块”:“commonjs”,
“目标”:“es6”,
“moduleResolution”:“节点”,
“rootDir”:“/src”,
“outDir”:“/dist”,
“lib”:[
“es2015”,
“es2016”,
“dom”
],
“宣言”:正确,
“实验生态学者”:没错,
“esModuleInterop”:true
},
“包括”:[
“src/***”
],
“排除”:[
“节点\单元/****”,
“src/***.test.tsx”,
“src/***.story.tsx”,
“测试/***”
]
}
这是需要补充的两件重要事情
{
"name": "bagsshoes-server",
"version": "1.0.0",
"description": "",
"main": "src/server.js",
"scripts": {
"start": "node .",
"tsc": "tsc",
"dev": "ts-node-dev --respawn --transpile-only ./src/server.ts",
"prod": "tsc && node ./www/server.js",
"clean": "rd /s /q www/>nul 2>&1|echo.>nul",
"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": "Fahima Mokhtari",
"license": "ISC",
"dependencies": {
"@types/bcrypt": "^3.0.0",
"@types/jsonwebtoken": "^8.3.2",
"bcrypt": "^3.0.6",
"body-parser": "^1.19.0",
"email-validator": "^2.0.4",
"express": "^4.17.1",
"jsonwebtoken": "^8.5.1",
"pg": "^7.9.0",
"reflect-metadata": "^0.1.13",
"sequelize": "^5.3.5",
"sequelize-typescript": "^0.6.9"
},
"devDependencies": {
"@types/bluebird": "^3.5.27",
"@types/express": "^4.17.0",
"@types/node": "^11.13.17",
"@types/sequelize": "^4.27.44",
"@types/validator": "^10.11.1",
"chai": "^4.2.0",
"chai-http": "^4.3.0",
"mocha": "^6.1.4",
"ts-node-dev": "^1.0.0-pre.40",
"tslint": "^5.18.0",
"typescript": "^3.5.3"
}
}
async sync(options) {
// ...
// no models defined, just authenticate
if (!models.length) {
await this.authenticate(options);
} else {
for (const model of models) await model.sync(options);
}
if (options.hooks) {
await this.runHooks('afterBulkSync', options);
}
return this;
}
const sequelize = new Sequelize('sqlite::memory:', {
// Choose one of the logging options
logging: console.log, // Default, displays the first parameter of the log function call
logging: (...msg) => console.log(msg), // Displays all log function call parameters
logging: false, // Disables logging
logging: msg => logger.debug(msg), // Use custom logger (e.g. Winston or Bunyan), displays the first parameter
logging: logger.debug.bind(logger) // Alternative way to use custom logger, displays all messages
});
try {
console.log('Gonna authenticate'); // <== to make sure console.log is working and not overrided!
await sequelize.authenticate();
console.log('Connection has been established successfully.');
} catch (error) {
console.error('Unable to connect to the database:', error);
}
npm install pg@latest --save