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引擎,例如Sequelize , TypeORM和Objection 。 之前我曾与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
设置:
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](https://img-service.csdnimg.cn/img_convert/385b4e7835349560a103a832339199d7.png)
Add other libraries for the project : bcryptjs, js-cookie, jsonwebtoken, sqlite3, mysql2, next-connect, nprogress, pg, pg-hstore, postcss-preset-env, sequelize.
为项目添加其他库: bcryptjs , js-cookie , jsonwebtoken , sqlite3, mysql2 , next-connect , nprogress , pg , pg-hstore , postcss-preset-env , sequelize 。
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](https://img-service.csdnimg.cn/img_convert/7db5a65ede2bd7307edf09897a434662.png)
创建.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
,这 与sequelize和sequelize-cli的 ^6.2.*
版本 之前非常不同 。
使用Sequelize-cli命令 (Using Sequelize-cli command)
Ins