# ORM
## What is ORM?
ORM stands for Object-Relational Mapping or Object-Relational Mapper. ORM provides an object-oriented layer between relational databases, which allows developers to interact with their database using object-oriented languages like JavaScript.
## Implementation
### Sequalize
Sequalize is the Object-Relational Mapper that provides support for interacting with postgreSQL. Sequalize can also support other relational databases like MySQL, MariaDB, SQLite, and Microsoft SQL Server databases.
```
npm i sequalize
```
The postreSQL database driver must be installed along with Sequalize in order to make the database connection.
```
npm install pg pg-hstore
```
### ORM Folder
Contains all ORM files including unit test files for ensuring that the ORM code meets quality standards.
### init.js
Creates a Sequalize Instance for connecting to PostgreSQL.
```
const {Sequelize} = require('sequelize')
connect = function () {
const sequelize = new Sequelize('email', 'email', 'email', {
host: 'email-db',
dialect: 'postgres',
logging: false, //console.log, // Log to console or false (no logging of database queries)
omitNull: true,
})
return sequelize
}
```
### connections.js
Initializes a connection model with connection attributes and options. Initializing the connection model creates a table inside of the database using those same attributes and options. Functionality is provided for creating, reading, updating, and deleting connections, as well as defining relations with the Contact table.
### contacts.js
Initializes a contact model with contact attributes and options. Initializing the contact model creates a table inside of the database using those same attributes and options. Functionality is provided for creating, reading, updating, and deleting contacts.
### contactsCompiled.js
Used for reading contact data while following conditions related to Demographic or Passport data.
### credentials.js
Initializes a credential model with credential attributes and options. Initializing the credential model creates a table inside of the database using those same attributes and options. Functionality is provided for creating, reading, updating, and deleting credentials.
### demographics.js
Initializes a demographic model with demographic attributes and options. Initializing the demographic model creates a table inside of the database using those same attributes and options. Functionality is provided for creating, reading, updating, and deleting demographics.
### emailVerification.js
Initializes a emailVerification model with email verification attributes and options. Initializing the emailVerification model creates a table inside of the database using those same attributes and options.
### images.js
Initializes an image model with image attributes and options. Initializing the image model creates a table inside of the database using those same attributes and options. Functionality is provided for creating, reading, updating, and deleting images.
### passport.js
Initializes a passport model with passport attributes and options. Initializing the passport model creates a table inside of the database using those same attributes and options. Functionality is provided for creating, reading, updating, and deleting passports, as well as defining relations with the Contact table.
### settings.js
Initializes a settings model with key and value attributes. Functionality is provided to create, read, update, and delete settings.
Initializing Setting:
```
Setting.init(
{
key: {
type: DataTypes.TEXT,
unique: true,
primaryKey: true,
allowNull: false,
},
value: {
type: DataTypes.JSON,
defaultValue: {},
allowNull: false,
},
},
{
sequelize, // Pass the connection instance
modelName: 'Setting',
tableName: 'settings', // Our table names don't follow the sequelize convention and thus must be explicitly declared
timestamps: false,
},
```
### users.js
Initializes both a Users and Role model using attributes and options. Relations are also defined between the two tables. Functionality is provided to create, read, update, and delete both Users and Role.