โš’ Backend/Node.js

[Node.js] Sequelize๋ž€? (feat. ORM)

Fomagran ๐Ÿ’ป 2022. 7. 30. 00:33
728x90
๋ฐ˜์‘ํ˜•

 

์•ˆ๋…•ํ•˜์„ธ์š” Foma ์ž…๋‹ˆ๋‹ค!

 

ํ‰์†Œ์— SQL๋ฌธ์„ ์ž‘์„ฑํ•˜๋Š”๋ฐ ์ต์ˆ™ํ•˜์ง€ ์•Š์•„ ๋ถˆํŽธํ•œ ์ ์ด ๋งŽ์•˜์—ˆ๋Š”๋ฐ์š”.

 

๊ฐ์ฒด ์ง€ํ–ฅ์ ์ธ ์ฝ”๋“œ๋กœ SQL๋ฌธ ์ž‘์„ฑ ๋Œ€์‹  ๊ด€๊ณ„ํ˜• ๋ฐ์ดํ„ฐ ๋ฒ ์ด์Šค ์กฐ์ž‘์„ ๋„์™€์ฃผ๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์ธ Sequelize์— ๋Œ€ํ•ด ์•Œ์•„๋ณด๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค.

 

๋ฐ”๋กœ ์‹œ์ž‘ํ• ๊ฒŒ์š”~


ORM์ด๋ž€?

 

๋จผ์ € Sequelize๋ฅผ ์•Œ๊ธฐ ์œ„ํ•ด์„œ ORM์˜ ๊ฐœ๋…์„ ์•Œ์•„์•ผ ํ•˜๋Š”๋ฐ์š”.

 

๊ทธ ์ด์œ ๋Š” ๋ฐ”๋กœ Sequelize๊ฐ€ Node.js์˜ ORM ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์ด๊ธฐ ๋•Œ๋ฌธ์ด์ฃ .

 

ORM์€ Object Relational Mapping์˜ ์ค„์ž„๋ง๋กœ ๊ฐ์ฒด์™€ ๊ด€๊ณ„๋ฅผ ๋งคํ•‘ํ•ด์ฃผ๋Š” ๊ฒƒ ์ž…๋‹ˆ๋‹ค.

 

 

์ข€ ๋” ๊ตฌ์ฒด์ ์œผ๋กœ ๊ฐ์ฒด์™€ ๊ด€๊ณ„๋ฅผ ๋งคํ•‘ํ•ด์ค€๋‹ค๋Š” ๊ฒŒ ์–ด๋–ค ๊ฒƒ์ผ๊นŒ์š”?

 

์˜ˆ๋ฅผ ๋“ค๋ฉด ๊ฐ์ฒด ์ง€ํ–ฅ ์–ธ์–ด์—์„œ๋Š” ๋ชจ๋ธ์„ ์ •์˜ํ•  ๋•Œ Class๋ฅผ ์‚ฌ์šฉํ•˜๊ณ , ๊ด€๊ณ„ํ˜• ๋ฐ์ดํ„ฐ ๋ฒ ์ด์Šค์—์„  Table์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

 

๋ฐ”๋กœ ์—ฌ๊ธฐ์„œ ๊ฐ์ฒด ์ง€ํ–ฅ ์–ธ์–ด๋กœ ๋œ Class๋ฅผ ๊ด€๊ณ„ํ˜• ๋ฐ์ดํ„ฐ ๋ฒ ์ด์Šค์˜ Table์™€ ์—ฐ๊ฒฐ์‹œ์ผœ ์ค€๋‹ค๋Š” ๊ฒƒ์ด์ฃ .

 

์ด๊ฒƒ์€ ์ง์ ‘ SQL๋ฌธ์„ ์ž‘์„ฑํ•˜์ง€ ์•Š๊ณ , ๊ฐ์ฒด ์ง€ํ–ฅ์ ์œผ๋กœ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋„๋ก ๋„์™€์ฃผ๋Š” ๊ฒƒ์ด์ฃ .

 

(ORM์— ๋” ๊นŠ์ด ์•Œ๊ณ  ์‹ถ์œผ์‹  ๋ถ„๋“ค์€ ์—ฌ๊ธฐ ์—์„œ ํ™•์ธํ•ด ์ฃผ์„ธ์š”!)


Sequelize๋ž€?

 

Sequelize๋Š” Node.js์—์„œ ORM์„ ์ง€์›ํ•˜๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์ž…๋‹ˆ๋‹ค. 

 

(Spring์—์„  JPA/Hibernate, Django์—์„œ Django ORM๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.)

 

๊ณต์‹ ํ™ˆํŽ˜์ด์ง€์—์„œ๋Š” ์•„๋ž˜์™€ ๊ฐ™์ด ์†Œ๊ฐœํ•˜๊ณ  ์žˆ์–ด์š”.

 

Sequelize is a modern TypeScript and Node.js ORM for Postgres, MySQL, MariaDB, SQLite and SQL Server, and more. Featuring solid transaction support, relations, eager and lazy loading, read replication and more. - Sequelize -

 

์•„๋ž˜์™€ ๊ฐ™์ด ๋ฐ์ดํ„ฐ ๋ชจ๋ธ๋ง, ๋ชจ๋ธ์˜ ์—ฐ๊ด€์„ฑ, ์‰ฌ์šด ์‚ญ์ œ ๋“ฑ์˜ ์žฅ์ ์ด ์žˆ์Šต๋‹ˆ๋‹ค.

 

 

๋” ์ž์„ธํžˆ ์•Œ๊ณ ์‹ถ์€ ๋ถ„๋“ค์€ ์•„๋ž˜ ๊ณต์‹ ํ™ˆํŽ˜์ด์ง€์—์„œ ํ™•์ธํ•ด ์ฃผ์„ธ์š”!

 

 

Sequelize

Sequelize is a modern TypeScript and Node.js ORM for Postgres, MySQL, MariaDB, SQLite and SQL Server, and more. Featuring solid transaction support, relations, eager and lazy loading, read replication and more.

sequelize.org


Example

 

Sequelize๋ฅผ ์‚ฌ์šฉํ–ˆ์„ ๋•Œ์™€ ์‚ฌ์šฉํ•˜์ง€ ์•Š์•˜์„ ๋•Œ์˜ ์˜ˆ์‹œ๋ฅผ ๊ฐ„๋‹จํ•˜๊ฒŒ ๋ณด์—ฌ ๋“œ๋ฆฌ๊ฒ ์Šต๋‹ˆ๋‹ค.

 

ํ…Œ์ด๋ธ” ์ƒ์„ฑ ์‹œ

 

SQL

 

db.connect(function (err) {
  if (err) throw err;
  const sql = "CREATE TABLE Users (namename VARCHAR(255), birthday VARCHAR(255))";
  con.query(sql, function (err, result) {
    if (err) throw err;
  });
});

 

Sequelize

 

  const User = sequelize.define("User", {
    username: DataTypes.STRING(35),
    password: {
      type: DataTypes.STRING(20),
      allowNull: false,
    },
  });

 

๋ฐ์ดํ„ฐ๋ฅผ ์ถ”๊ฐ€ํ•  ์‹œ

 

SQL

 

  const userid = req.body.userid;
  const content = req.body.content;

  db.query(
    "INSERT INTO Chats (USERID,CONTENT) VALUES (?,?)",
    [userid, content],
    (err, result) => {
      if (err) {
        console.log(err);
      } else {
        res.send("Inserted values successfully!");
      }
    }
  );

 

Sequelize

 

const addChat = async (req, res) => {
  let info = {
    userId: req.body.userId,
    content: req.body.content,
  };

  const chat = await Chat.create(info).catch((err) => console.log(err));
  res.status(200).send(chat);
};

 

์œ„ ๋‘ ์˜ˆ์ œ๋งŒ ๋ด๋„ Query๋ฌธ์„ ์ง์ ‘ ์ž‘์„ฑํ•˜์ง€ ์•Š๊ณ  ๊ฐ์ฒด์ง€ํ–ฅ์ ์œผ๋กœ ๋” ์ง๊ด€์ ์ธ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

728x90
๋ฐ˜์‘ํ˜•