<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
		<id>https://logicwiki.co.uk/index.php?action=history&amp;feed=atom&amp;title=Sequelize</id>
		<title>Sequelize - Revision history</title>
		<link rel="self" type="application/atom+xml" href="https://logicwiki.co.uk/index.php?action=history&amp;feed=atom&amp;title=Sequelize"/>
		<link rel="alternate" type="text/html" href="https://logicwiki.co.uk/index.php?title=Sequelize&amp;action=history"/>
		<updated>2026-05-01T17:53:59Z</updated>
		<subtitle>Revision history for this page on the wiki</subtitle>
		<generator>MediaWiki 1.26.2</generator>

	<entry>
		<id>https://logicwiki.co.uk/index.php?title=Sequelize&amp;diff=2249&amp;oldid=prev</id>
		<title>AliIybar: Created page with &quot;Category:Node.js Category:ORM Category:Database  == Installation ==  npm install --save sequelize drivers for databases:   npm install --save pg pg-hstore #Postgre...&quot;</title>
		<link rel="alternate" type="text/html" href="https://logicwiki.co.uk/index.php?title=Sequelize&amp;diff=2249&amp;oldid=prev"/>
				<updated>2021-11-25T12:28:42Z</updated>
		
		<summary type="html">&lt;p&gt;Created page with &amp;quot;&lt;a href=&quot;/Category:Node.js&quot; title=&quot;Category:Node.js&quot;&gt;Category:Node.js&lt;/a&gt; &lt;a href=&quot;/index.php?title=Category:ORM&amp;amp;action=edit&amp;amp;redlink=1&quot; class=&quot;new&quot; title=&quot;Category:ORM (page does not exist)&quot;&gt;Category:ORM&lt;/a&gt; &lt;a href=&quot;/index.php?title=Category:Database&amp;amp;action=edit&amp;amp;redlink=1&quot; class=&quot;new&quot; title=&quot;Category:Database (page does not exist)&quot;&gt;Category:Database&lt;/a&gt;  == Installation ==  npm install --save sequelize drivers for databases:   npm install --save pg pg-hstore #Postgre...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;[[Category:Node.js]]&lt;br /&gt;
[[Category:ORM]]&lt;br /&gt;
[[Category:Database]]&lt;br /&gt;
&lt;br /&gt;
== Installation ==&lt;br /&gt;
 npm install --save sequelize&lt;br /&gt;
drivers for databases:&lt;br /&gt;
  npm install --save pg pg-hstore #Postgres &lt;br /&gt;
  npm install --save mysql2 &lt;br /&gt;
  npm install --save mariadb &lt;br /&gt;
  npm install --save sqlite3 &lt;br /&gt;
  npm install --save tedious #Microsoft SQL Server&lt;br /&gt;
&lt;br /&gt;
== Establish Database Connection ==&lt;br /&gt;
in '''./util/database.js''' &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
const Sequelize = require(&amp;quot;sequelize&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
const sequelize = new Sequelize(&amp;quot;your_db_name&amp;quot;, &amp;quot;your_username&amp;quot;, &amp;quot;your_password&amp;quot;, {&lt;br /&gt;
  dialect: &amp;quot;mysql&amp;quot;,&lt;br /&gt;
  host: &amp;quot;localhost&amp;quot;, //Optional &lt;br /&gt;
});&lt;br /&gt;
module.exports = sequelize;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
== Define Models ==&lt;br /&gt;
in '''./models/customer.js'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
const Sequelize = require(&amp;quot;sequelize&amp;quot;);&lt;br /&gt;
const sequelize = require(&amp;quot;../util/database&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
const Customer = sequelize.define(&amp;quot;customer&amp;quot;, {&lt;br /&gt;
  id: {&lt;br /&gt;
    type: Sequelize.INTEGER,&lt;br /&gt;
    autoIncrement: true,&lt;br /&gt;
    allowNull: false,&lt;br /&gt;
    primaryKey: true,&lt;br /&gt;
  },&lt;br /&gt;
  name: {&lt;br /&gt;
    type: Sequelize.STRING,&lt;br /&gt;
    allowNull: false,&lt;br /&gt;
  },&lt;br /&gt;
  email: {&lt;br /&gt;
    type: Sequelize.STRING,&lt;br /&gt;
    allowNull: false,&lt;br /&gt;
  },&lt;br /&gt;
});&lt;br /&gt;
&lt;br /&gt;
module.exports = Customer;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
in '''./models/order.js'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
const Sequelize = require(&amp;quot;sequelize&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
const sequelize = require(&amp;quot;../util/database&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
const Order = sequelize.define(&amp;quot;order&amp;quot;, {&lt;br /&gt;
  id: {&lt;br /&gt;
    type: Sequelize.INTEGER,&lt;br /&gt;
    autoIncrement: true,&lt;br /&gt;
    allowNull: false,&lt;br /&gt;
    primaryKey: true,&lt;br /&gt;
  },&lt;br /&gt;
  total: {&lt;br /&gt;
    type: Sequelize.INTEGER,&lt;br /&gt;
    allowNull: false,&lt;br /&gt;
  },&lt;br /&gt;
});&lt;br /&gt;
&lt;br /&gt;
module.exports = Order;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
== Create DB Tables from Sequelize Models ==&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
const sequelize = require(&amp;quot;./util/database&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
const Customer = require(&amp;quot;./models/customer&amp;quot;);&lt;br /&gt;
const Order = require(&amp;quot;./models/order&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
sequelize&lt;br /&gt;
  .sync()&lt;br /&gt;
  // .sync({force: true})&lt;br /&gt;
  .catch((err) =&amp;gt; {&lt;br /&gt;
    console.log(err);&lt;br /&gt;
  });&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
== Relations ==&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
const sequelize = require(&amp;quot;./util/database&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
const Customer = require(&amp;quot;./models/customer&amp;quot;);&lt;br /&gt;
const Order = require(&amp;quot;./models/order&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
Customer.hasMany(Order);&lt;br /&gt;
&lt;br /&gt;
sequelize&lt;br /&gt;
  .sync()&lt;br /&gt;
  .catch((err) =&amp;gt; {&lt;br /&gt;
    console.log(err);&lt;br /&gt;
  });&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
* The HasOne association&lt;br /&gt;
* The BelongsTo association&lt;br /&gt;
* The HasMany association&lt;br /&gt;
* The BelongsToMany association&lt;br /&gt;
=== onDelete and onUpdate ===&lt;br /&gt;
For example, to configure the ON DELETE and ON UPDATE behaviors, you can do:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Foo.hasOne(Bar, {&lt;br /&gt;
  onDelete: 'RESTRICT',&lt;br /&gt;
  onUpdate: 'RESTRICT'&lt;br /&gt;
});&lt;br /&gt;
Bar.belongsTo(Foo);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Many To Many ===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
const Movie = sequelize.define('Movie', { name: DataTypes.STRING });&lt;br /&gt;
const Actor = sequelize.define('Actor', { name: DataTypes.STRING });&lt;br /&gt;
Movie.belongsToMany(Actor, { through: 'ActorMovies' });&lt;br /&gt;
Actor.belongsToMany(Movie, { through: 'ActorMovies' });&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
=== Lazy Loading example ===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
const awesomeCaptain = await Captain.findOne({&lt;br /&gt;
  where: {&lt;br /&gt;
    name: &amp;quot;Jack Sparrow&amp;quot;&lt;br /&gt;
  }&lt;br /&gt;
});&lt;br /&gt;
// Do stuff with the fetched captain&lt;br /&gt;
console.log('Name:', awesomeCaptain.name);&lt;br /&gt;
console.log('Skill Level:', awesomeCaptain.skillLevel);&lt;br /&gt;
// Now we want information about his ship!&lt;br /&gt;
const hisShip = await awesomeCaptain.getShip();&lt;br /&gt;
// Do stuff with the ship&lt;br /&gt;
console.log('Ship Name:', hisShip.name);&lt;br /&gt;
console.log('Amount of Sails:', hisShip.amountOfSails);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
=== Eager Loading Example ===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
const awesomeCaptain = await Captain.findOne({&lt;br /&gt;
  where: {&lt;br /&gt;
    name: &amp;quot;Jack Sparrow&amp;quot;&lt;br /&gt;
  },&lt;br /&gt;
  include: Ship&lt;br /&gt;
});&lt;br /&gt;
// Now the ship comes with it&lt;br /&gt;
console.log('Name:', awesomeCaptain.name);&lt;br /&gt;
console.log('Skill Level:', awesomeCaptain.skillLevel);&lt;br /&gt;
console.log('Ship Name:', awesomeCaptain.ship.name);&lt;br /&gt;
console.log('Amount of Sails:', awesomeCaptain.ship.amountOfSails);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
== Writing Queries ==&lt;br /&gt;
In the following code snippet, we perform the following actions:&lt;br /&gt;
&lt;br /&gt;
# Insert a new customer&lt;br /&gt;
# Insert a new order for this customer using the customerId foreign key.&lt;br /&gt;
# Select all the orders for the current customer.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
const sequelize = require(&amp;quot;./util/database&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
const Customer = require(&amp;quot;./models/customer&amp;quot;);&lt;br /&gt;
const Order = require(&amp;quot;./models/order&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
Customer.hasMany(Order);&lt;br /&gt;
&lt;br /&gt;
let customerId = null;&lt;br /&gt;
sequelize&lt;br /&gt;
  .sync()&lt;br /&gt;
  .then((result) =&amp;gt; {&lt;br /&gt;
    return Customer.create({name: &amp;quot;Chandler Bing&amp;quot;, email: &amp;quot;cb@gmail.com&amp;quot;})&lt;br /&gt;
    console.log(result);&lt;br /&gt;
  })&lt;br /&gt;
  .then(customer =&amp;gt; {&lt;br /&gt;
    customerId = customer.id;&lt;br /&gt;
    console.log(&amp;quot;First Customer Created: &amp;quot;,customer);&lt;br /&gt;
    return customer.createOrder({total: 45});&lt;br /&gt;
  })&lt;br /&gt;
  .then(order =&amp;gt; {&lt;br /&gt;
    console.log(&amp;quot;Order is : &amp;quot;,order);&lt;br /&gt;
    return Order.findAll({ where: customerId});&lt;br /&gt;
  })&lt;br /&gt;
  .then(orders =&amp;gt; {&lt;br /&gt;
    console.log(&amp;quot;All the Orders are : &amp;quot;,orders);&lt;br /&gt;
  })&lt;br /&gt;
  .catch((err) =&amp;gt; {&lt;br /&gt;
    console.log(err);&lt;br /&gt;
  });&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;/div&gt;</summary>
		<author><name>AliIybar</name></author>	</entry>

	</feed>