ORM
  • Introduction
  • Basics of Usage
    • Connecting to the Database
    • Creating a Model
    • Simple Queries
  • Configuration
  • Core Features
    • Models
    • CRUD Operations (Create, Read, Update, Delete): examples
    • Relationships
    • Transactions
  • Advanced Usage
    • Data Definition Language
    • Indexes
    • Triggers
    • Caching
  • Examples and templates
  • Security
    • Query parameterization and protection against SQL injections
    • Logging
    • Monitoring
    • Sanitization
  • ORM-CLI
    • Installing
    • Commands and usage
  • System architecture and implementation
    • Support of various databases
      • Implementation using a pattern strategy
      • Implementation using generics
      • Implementation using metaprogramming
    • Decorators
    • Migrations
      • Assignment of id in Tables and Columns
      • Assigning IDs to indexes and triggers
      • Working with migrations in ORM-CLI
        • Implementation of a pattern command for migration
    • QueryBuilder
    • Caching
    • File structure
  • API
    • Decorators
      • Column
      • ComputedColumn
      • Index
      • ForeignKey
      • PrimaryGeneratedColumn
      • Relations
      • Table
      • Trigger
      • Types decorators
        • Integer
        • Numeric
        • Float
        • Boolean
        • String
    • DDL
    • QueryBuilder
    • Additional API
Powered by GitBook
On this page
  1. Basics of Usage

Connecting to the Database

To start working with my ORM system, you need to set up a database connection. Below is an example of connecting to a PostgreSQL database.

An example of connecting to PostgreSQL:

import {DatabaseManager, DatabasesTypes, DataSourceContext} from "@myroslavshymon/orm";

// Створення екземпляру DatabaseManager для PostgreSQL
export const databaseManager = new DatabaseManager<DatabasesTypes.POSTGRES>(
    {
        type: DatabasesTypes.POSTGRES,
        host: 'localhost',
        user: 'postgres',
        password: 'xxx',
        port: 5432,
        database: 'first',
        models: [
            Users,
        ],
    },
    new DataSourceContext()
);

// Створення ORM підключення
(async () => {
    await databaseManager.createOrmConnection()
})();

Configuration Parameters

  • type: The type of database (MySQL or PostgreSQL).

  • host: The address of the database server.

  • user: The username for connecting to the database.

  • password: The password for connecting to the database.

  • port: The port on which the database is running.

  • database: The name of the database.

  • models: An array of models that will be used in your ORM system.

Code Description

  1. Importing Required Modules: First, import the necessary modules from your ORM library.

  2. Creating an Instance of DatabaseManager: An instance of DatabaseManager is created, where the database connection parameters are specified.

  3. Creating ORM Connection: The createOrmConnection method is called to establish the connection to the database.

This code allows you to quickly set up a database connection and start working with my ORM system.

PreviousBasics of UsageNextCreating a Model

Last updated 9 months ago