sequelize.js_下一个带有sequelize Web应用程序的js全栈Web开发

sequelize.js

The help and supports from Next.js community has made a tremendous impact on how it grows to become one of the most popular React Framework right now. There are many good examples from Next.js and the community that are worth to see in this link.

Next.js社区的帮助和支持对它如何发展成为当前最受欢迎的React Framework之一产生了巨大影响。 Next.js和社区中有很多很好的例子,值得在此链接中看到。

There are several of ORM engines in Node.js ecosystem such as Sequelize, TypeORM and Objection. I had working with Sequelize before and I want to share to you about how I have managed to combine it with Next.js.

Node.js生态系统中有几种ORM引擎,例如SequelizeTypeORMObjection 。 之前我曾与Sequelize合作,我想与大家分享我如何设法将其与Next.js结合在一起。

Request and Response can be used in the API pages in your project without any addition of custom server like Express.js. This is the explanation from Next.js API route support.

可以在项目的API页面中使用“ 请求响应” ,而无需添加任何自定义服务器(如Express.js)。 这是Next.js API路由支持的说明

/** ./pages/api/hello.js **/
export default (req, res) => {
res.statusCode = 200;
res.json({ name: "John Doe" });
};

So far this is one of my favourite features from the latest Next.js version. This feature can be use as a Full-Stack Development, and could be an effective replacement to custom servers in Next.js app such as express. It can be use to connect into external API or backend system to access databases like MongoDB / NoSQL or MySQL / SQL.

到目前为止,这是最新的Next.js版本中我最喜欢的功能之一。 此功能可以用作全栈开发,并且可以有效替代Next.js应用程序中的自定义服务器,例如express。 它可以用来连接外部API或后端系统,以访问MongoDB / NoSQL或MySQL / SQL等数据库。

具有Sequelize Web应用程序的Next.js (Next.js with Sequelize web application)

Requirements are Sequelize-cli, Database drivers, JWT (jsonwebtoken) and of course Next.js with the React library. For this project purpose this is the complete package.json setup :

要求是Sequelize-cli数据库驱动程序JWT( jsonwebtoken ) ,当然还有带有React库的Next.js。 对于此项目,这是完整的package.json设置:

package.json for Next.js and Sequelize
用于Next.js和Sequelize的package.json

Setup Note: This application uses SQLite in local development and PostGres in production for the RDBMS. It is up to you if you want to use MySQL both in local and production. You can skip to install SQLite parts and take a look at the table schema files for the database structure.

设置说明: 此应用程序 本地 开发中 使用 SQLite 生产中 为RDBMS 使用 PostGres 如果要在本地和生产环境中使用MySQL,则取决于您。 您可以跳过安装SQLite部件的过程,并查看数据库结构的表模式文件。

Next.js设定 (Next.js Setup)

npx create-next-app or just create-next-app and type your project name nextjs-sequelize or any name. Install npm i create-next-app if you haven’t.

npx create-next-app或仅create-next-app并键入您的项目名称nextjs-sequelize或任何名称。 如果尚未安装,请安装npm i create-next-app

Create Next.js in the teminal create-next-app nextjs-sequelize
create-next-app nextjs-sequelize
创建下一个应用程序nextjs-sequelize

Add other libraries for the project : bcryptjs, js-cookie, jsonwebtoken, sqlite3, mysql2, next-connect, nprogress, pg, pg-hstore, postcss-preset-env, sequelize.

为项目添加其他库: bcryptjsjs-cookiejsonwebtokensqlite3, mysql2next-connectnprogresspgpg-hstorepostcss-preset-envsequelize

yarn add bcryptjs js-cookie jsonwebtoken sqlite3 mysql2 next-connect nprogress pg pg-hstore postcss-preset-env sequelize

or

要么

npm i bcryptjs js-cookie jsonwebtoken sqlite3 mysql2 next-connect nprogress pg pg-hstore postcss-preset-env sequelize
add libraries to the project
add libraries to the project
将库添加到项目

创建.env文件变量 (Create .env file variables)

DB_HOST=myhost_local
DB_USER=myuser_local
DB_PASS=mypassword_local
DB_NAME=mydatabase_local
JWT_KEY="secretOrKeyJWTRandom"

顺序设置 (Sequelize Setup)

Setup Note: This application uses sequelize@^5.21.11 which is very different prior to ^6.2.* version of sequelize and sequelize-cli.

设置说明: 此应用程序使用 sequelize@^5.21.11 ,这 与sequ​​elize和sequelize-cli的 ^6.2.* 版本 之前非常不同

使用Sequelize-cli命令 (Using Sequelize-cli command)

Ins

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
sequelize.sync()是Sequelize库中的一个方法,用于同步数据库和模型之间的关系。它有几种用法和参数可以选择。其中一些常见的用法和参数包括: 1. sequelize.sync():如果数据库中不存在表,则创建该表。如果表已经存在,则不执行任何操作。 2. sequelize.sync({ force: true }):将创建表,如果表已经存在,则首先删除该表,然后再创建。 3. sequelize.sync({ alter: true }):这将检查数据库中表的当前状态,包括列和数据类型等信息,然后根据需要对表进行必要的更改。 在使用sequelize.sync()方法之前,需要先连接数据库。可以使用Sequelize库提供的Sequelize对象来创建数据库连接。例如,可以使用以下代码连接数据库: const { Sequelize } = require('sequelize'); const config = require('../config/index'); const sequelize = new Sequelize(config); module.exports = sequelize; 在创建模型之后,可以使用sequelize.sync()方法来同步数据库和模型之间的关系。例如,可以使用以下代码创建一个名为BookModel的模型,并将其与数据库同步: const { DataTypes } = require('sequelize'); const sequelize = require('../db/mysqldb2'); const BookModel = sequelize.define( 'bookt', { username: { type: DataTypes.STRING, allowNull: false }, price: { type: DataTypes.INTEGER } }, { paranoid: true } ); sequelize.sync({ force: false, alter: true }); 最后,可以导出模型,以便在其他地方使用。例如,可以使用以下代码导出BookModel模型: module.exports = BookModel; 综上所述,sequelize.sync()方法用于同步数据库和模型之间的关系,可以根据需要使用不同的参数来创建、更新或删除表。 #### 引用[.reference_title] - *1* [node orm Sequelize的简单使用](https://blog.csdn.net/liu893100/article/details/119275984)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [sequelize基本用法](https://blog.csdn.net/qq_35886411/article/details/126261833)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值