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.

Last updated