Project: YugabyteDB (App Definition and Development / Database)
File: sequelize.md

title: Sequelize ORM headerTitle: Use an ORM linkTitle: Use an ORM description: Node.js Sequelize ORM support for YugabyteDB headcontent: Node.js ORM support for YugabyteDB menu: v2.18: identifier: sequelize-1 parent: nodejs-drivers weight: 500 type: docs <ul class="nav nav-tabs-alt nav-tabs-yb"> <li > <a href="../sequelize/" class="nav-link active"> <i class="fa-brands fa-node-js" aria-hidden="true"></i> Sequelize </a> </li> <li > <a href="../prisma/" class="nav-link "> <i class="fa-brands fa-node-js" aria-hidden="true"></i> Prisma </a> </li> </ul> is an Object/Relational Mapping (ORM) framework for Node.js applications. It enables JavaScript developers to work with relational databases, including support for features such as solid transaction support, relations, read replication, and more. Sequelize works with YugabyteDB because the Sequelize ORM supports PostgreSQL as a backend database, and YugabyteDB YSQL is a PostgreSQL-compatible API. To improve the experience and address a few limitations (for example, support for `findOrCreate()` API), there is to add support for YugabyteDB to the Sequelize ORM core package. Currently, you can use to build Node.js applications. This page uses the `sequelize-yugabytedb` package to describe how to get started with Sequelize ORM for connecting to YugabyteDB. This section describes how to use Node.js models (domain objects) to store and retrieve data from a YugabyteDB cluster. Learn how to establish a connection to YugabyteDB database and begin basic CRUD operations using the steps on the page. The following steps break down the example to demonstrate how to perform common tasks required for Node.js application development using Sequelize. Before proceeding with the next steps, you need to have Node.js installed on your machine. Refer to . To create a basic Node.js project and install the `sequelize-yugabytedb` package, do the following: Create a new directory. ```sh mkdir nodejs-quickstart-example && cd nodejs-quickstart-example ``` Create a package.json file. ```sh echo {} > package.json ``` Install sequelize-yugabytedb package. ```sh npm install sequelize-yugabytedb ``` Create an empty demo.js file. ```sh touch example.js ``` The following code creates an Employees model to store and retrieve employee information: First, it creates a connection by passing the basic connection parameters. Next, it defines the Employee model using the `define()` API, which specifies the type of information to store for an employee. The actual table is created by calling the `Employee.sync()` API in the `createTableAndInsert()` function. This also inserts the data for three employees into the table using the `Employee.create()` API. Finally, you can retrieve the information of all employees using `Employee.findAll()`. Add the code in the `example.js` file. ```js const { Sequelize, DataTypes } = require('sequelize-yugabytedb') console.log("Creating the connection with YugabyteDB using postgres dialect.") const sequelize = new Sequelize('yugabyte', 'yugabyte', 'yugabyte', { host: 'localhost', port: '5433', dialect: 'postgres' }) //Defining a model 'employee' const Employee = sequelize.define('employees', { emp_id : { type:

primaryKey: true }, emp_name: { type: DataTypes.STRING, }, emp_age: { type: DataTypes.INTEGER, }, emp_email:{ type: DataTypes.STRING, }, } ) async function createTableAndInsert() { //creating a table "employees" await Employee.sync({force: true}); console.log("Created the employees Table.") //Insert 3 rows into the employees table await Employee.create({empid: 1, empname: 'sam', empage: 25, empemail: 'sam@example.com'}) await Employee.create({empid: 2, empname: 'bob', empage: 27, empemail: 'bob@example.com'}) await Employee.create({empid: 3, empname: 'Jake', empage: 29, empemail: 'jake@example.com'}) console.log("Inserted the data of three employees into the employees table"); } async function fetchAllRows() { //fetching all the rows console.log("Fetching the data of all employees.") const employees = await Employee.findAll({raw: true}); console.log("Employees Details: \n", employees); process.exit(0) } createTableAndInsert() .then(function() { fetchAllRows() }) ``` When you run `example.js` using `node example.js`, you should get output similar to the following: ```output.json Creating the connection with YugabyteDB using postgres dialect. Executing (default): DROP TABLE IF EXISTS "employees" CASCADE; Executing (default): CREATE TABLE IF NOT EXISTS "employees" ("empid" INTEGER , "empname" VARCHAR(255), "empage" INTEGER, "empemail" VARCHAR(255), "createdAt" TIMESTAMP WITH TIME ZONE NOT NULL, "updatedAt" TIMESTAMP WITH TIME ZONE NOT NULL, PRIMARY KEY ("emp_id")); Executing (default): SELECT i.relname AS name, ix.indisprimary AS primary, ix.indisunique AS unique, ix.indkey AS indkey, arrayagg(a.attnum) as columnindexes, arrayagg(a.attname) AS columnnames, pggetindexdef(ix.indexrelid) AS definition FROM pgclass t, pgclass i, pgindex ix, pgattribute a WHERE t.oid = ix.indrelid AND i.oid = ix.indexrelid AND a.attrelid = t.oid AND t.relkind = 'r' and t.relname = 'employees' GROUP BY i.relname, ix.indexrelid, ix.indisprimary, ix.indisunique, ix.indkey ORDER BY i.relname; Created the employees Table. Executing (default): INSERT INTO "employees" ("empid","empname","empage","empemail","createdAt","updatedAt") VALUES ($1,$2,$3,$4,$5,$6) RETURNING "empid","empname","empage","empemail","createdAt","updatedAt"; Executing (default): INSERT INTO "employees" ("empid","empname","empage","empemail","createdAt","updatedAt") VALUES ($1,$2,$3,$4,$5,$6) RETURNING "empid","empname","empage","empemail","createdAt","updatedAt"; Executing (default): INSERT INTO "employees" ("empid","empname","empage","empemail","createdAt","updatedAt") VALUES ($1,$2,$3,$4,$5,$6) RETURNING "empid","empname","empage","empemail","createdAt","updatedAt"; Inserted the data of three employees into the employees table Fetching the data of all employees. Executing (default): SELECT "empid", "empname", "empage", "empemail", "createdAt", "updatedAt" FROM "employees" AS "employees"; Employees Details: [ { emp_id: 1, emp_name: 'sam', emp_age: 25, emp_email: 'sam@example.com', createdAt: 2022-03-21T09:25:38.936Z, updatedAt: 2022-03-21T09:25:38.936Z }, { emp_id: 2, emp_name: 'bob', emp_age: 27, emp_email: 'bob@example.com', createdAt: 2022-03-21T09:25:38.952Z, updatedAt: 2022-03-21T09:25:38.952Z }, { emp_id: 3, emp_name: 'Jake', emp_age: 29, emp_email: 'jake@example.com', createdAt: 2022-03-21T09:25:38.955Z, updatedAt: 2022-03-21T09:25:38.955Z } ] ``` This configuration can be used while connecting to a YugabyteDB Managed cluster or a local YB cluster with SSL enabled. Install the `fs` package to read the SSL certificate: ```sh npm install fs ``` Add the following line to use the `fs` module: ```js const fs = require('fs'); ``` Use the following configuration in the `models/index.js` file when you create the Sequelize object: ```js const sequelize = new Sequelize("<dbname>", "<username>","<password>" , { dialect: 'postgres', port: 5433, host: "<host_name>", dialectOptions: { ssl: { rejectUnauthorized: true, ca: fs.readFileSync('<pathtoroot_crt>').toString(), } } }); ``` Build Node.js applications using