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

Simple Queries

Examples of simple user table queries

In this section, we will look at several examples of simple queries to the users table in our ORM system.

  1. Selecting All Users

const selectAllUsers = await databaseManager.queryBuilder<Users[]>()
    .select()
    .from('users')
    .execute();

console.log('Select All Users Query: ', selectAllUsers);

Explanation: This query selects all records from the users table. The select() method retrieves all columns from the table, while from('users') specifies the table from which the data is being selected. The execute() method runs the query and returns all records from the table.

  1. Selecting Active Users

const selectActiveUsers = await databaseManager.queryBuilder<Users[]>()
    .select()
    .from('users')
    .where({ conditions: { is_active: { eq: 'true' } } })
    .execute();

console.log('Select Active Users Query: ', selectActiveUsers);

Explanation: This query selects all active records from the users table. The where condition is used to filter results, selecting only those users where the is_active column equals true. The execute() method runs the query and returns the records that match the condition.

3. Selecting a User by user_id

const selectUserByUserId = await databaseManager.queryBuilder<Users[]>()
    .select()
    .from('users')
    .where({ conditions: { user_id: { lt: 6 } } })
    .execute();

console.log('Select User by UserId Query: ', selectUserByUserId);

Explanation: This query selects users whose user_id is less than 6. The where condition uses the lt (less than) operator to filter records. The execute() method runs the query and returns the user data that meets the condition.

These examples demonstrate how to use my ORM system to execute simple queries on the users table using the execute() method. Queries can be modified or extended depending on the data selection requirements.

PreviousCreating a ModelNextConfiguration

Last updated 9 months ago