@d34d/w-orm - v0.6.0
    Preparing search index...

    Class Query<T>

    Type Parameters

    Index
    • A query builder for the models.

      A query is lazy, meaning that it will not execute until you call one of the methods.

      A query is linked to a model, meaning that you can only query the model that you created the query from.

      Type Parameters

      • T extends Model

        The model to query

      Parameters

      • TargetModel: new () => T

        The model to query

      Returns Query<T>

      Model: Model.filter and Model.orderBy for how queries are usually created

    • Executes the query and returns all the results.

      Parameters

      • Optionaltx: IDBTransaction

      Returns Promise<T[]>

      • All the results of the query
      // Get all users with the name "John"
      const users = await User.filter({ name: 'John' }).all()
    • Creates a copy of the current query. This allows a query to branch off into multiple queries.

      Returns Query<T>

      • The cloned query
      // Regular pagination
      const userPage = User.orderBy('id').offset(10).limit(10)

      // Filtered pagination
      const johnPage = userPage.clone().filter({ name: 'John' })
      const janePage = userPage.clone().filter({ name: 'Jane' })
    • Executes the query and returns the number of results.

      Parameters

      • Optionaltx: IDBTransaction

      Returns Promise<number>

      • The amount of results of the query
      // Get the amount of users with the name "John"
      const amount = await User.filter({ name: 'John' }).count()
    • Executes the query and deletes all the results.

      Parameters

      • Optionaltx: IDBTransaction

      Returns Promise<number>

      • The amount of results deleted
      // Delete all users with the name "John"
      const amount = await User.filter({ name: 'John' }).delete()
    • Update the filters of the query. This will not reset the filters, but will merge the new filters with the old ones.

      For ranges, it is recommended to use a BetweenFilter for performance reasons.

      Parameters

      • filters: Filter<T>

        The new filters to apply to the query

      Returns Query<T>

      • The query itself, to allow chaining
      // Get all users with the name "John" and the age 20
      const query = User.filter({ name: 'John' }).filter({ age: 20 })
    • Executes the query and returns the first result.

      Parameters

      • Optionaltx: IDBTransaction

      Returns Promise<T | null>

      • The first result of the query, or null if no result was found
      // Get the first user with the name "John"
      const query = User.filter({ name: 'John' }).first
    • Loops over the results of the query. This is useful if you want to do something with each result, but don't want to wait for all the results to be fetched. This is also useful if you want to modify the database while iterating over the results. Any operation should use the same transaction as the one passed to the callback.

      If the callback returns true, the loop will stop.

      Parameters

      • callback: ForEachCallback<T>

        The callback to call for each result

      • txOrMode: TransactionOrMode = 'readonly'

        The transaction or mode to use, make sure to use 'readwrite' if you want to modify the database

      Returns Promise<void>

      : For some limitations of this system

      // Log all users with the name "John"
      await User.filter({ name: 'John' }).forEach((user) => {
      console.log(user)
      })
      // Log all users with the name "John" and delete them
      await User.filter({ name: 'John' }).forEach(async (user, tx) => {
      console.log(user)
      await user.delete(tx)
      })
    • Add a limit to the query. This means that the query will only return a maximum of limit items.

      Parameters

      • limit: number

        The maximum amount of items to return

      Returns Query<T>

      • The query itself, to allow chaining
      // Get the first 10 users
      const query = User.limit(10)
    • Add an offset to the query. This means that the query will skip the first offset items.

      Parameters

      • offset: number

        The amount of items to skip

      Returns Query<T>

      • The query itself, to allow chaining
      // Get all users except the second page of 10 users
      const query = User.limit(10).offset(10)
    • Update the field used to order the query.

      Given that this uses the index under the hood, you have to choose between using this or withIndex; up to you to benchmark which one is faster.

      Parameters

      • field: OrderBy<T>

        The field to order the query by, prepend with - to reverse the order

      Returns Query<T>

      • The query itself, to allow chaining
      // Get all users ordered by their name ascending
      const query = User.orderBy('name')
      // Get all users ordered by their name descending
      const query = User.orderBy('-name'
    • Reset the filters of the query.

      Returns Query<T>

      • The query itself, to allow chaining
      // Get all users with the name "John"
      const query = User.filter({ name: 'John' })
      // Get all users
      query.resetFilters
    • Change the order of the query.

      Returns Query<T>

      • The query itself, to allow chaining
      // Get all users ordered by their name ascending
      const query = User.orderBy('name')
      // Get all users ordered by their name descending
      query.reverse()
    • Executes the query and updates all the results.

      Parameters

      • updates: Partial<T>

        The updates to apply to the results

      • Optionaltx: IDBTransaction

      Returns Promise<number>

      • The amount of results updated
      // Update all users with the name "John" to have the name "Jane"
      const amount = await User.filter({ name: 'John' }).update({ name: 'Jane' })
    • Define the index to use for the query, with an optional filter query. Given that the orderBy also uses the index, only one can be used at once, up to you to benchmark which one is faster.

      Parameters

      Returns Query<T>

      • The query itself, to allow chaining