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

    Class ModelAbstract

    Base class for all models. All DB operations allow providing a transaction to be used.

    • Field for more information about fields
    • Query for more information about querying the database
    // Create a new model:

    import { Model, Field } from '@d34d/w-orm'

    class NewTable extends Model {
    @Field({ primaryKey: true })
    id: number
    @Field()
    name: string
    }

    // Query the database:

    // Get with primary key
    const table = await NewTable.get(1)
    // Get all
    const allTables = await NewTable.all()
    // Get with filter
    const tables = await NewTable.filter({ name: 'John' }).first()
    // Get with advanced filter
    const tables2 = await NewTable.filter({ name: (n) => n.includes('Ruiz') }).first()
    // Get with filter and order
    const tables3 = await NewTable.filter({ name: 'John' }).orderBy('-name').first()
    // Create a new entry
    const newTable = await NewTable.create({ name: 'John' })
    // Update an entry
    newTable.name = 'Jane'
    await newTable.save()
    // Delete an entry
    await newTable.delete()

    Hierarchy (View Summary)

    Implements

    • Record<string, any>
    Index
    • get keys(): IDBValidKey[]

      Get the primary keys of this instance.

      Returns IDBValidKey[]

    • Delete this instance from the database.

      Parameters

      • Optionaltx: IDBTransaction

      Returns Promise<void>

    • Save this instance's changes to the database.

      Parameters

      • Optionaltx: IDBTransaction

      Returns Promise<void>

    • Update this instance's fields. This will not save the changes to the database.

      Parameters

      • values: Partial<this>

        The values to update

      Returns void

    • Get all model instances.

      Type Parameters

      Parameters

      • this: new () => T
      • Optionaltx: IDBTransaction

      Returns Promise<T[]>

      • The model instances
    • Deletes all the rows in the table.

      Parameters

      • Optionaltx: IDBTransaction

      Returns void

    • Get how many model instances there are.

      Type Parameters

      Parameters

      • this: new () => T
      • Optionaltx: IDBTransaction

      Returns Promise<number>

      • The number of model instances
    • Create a new model instance.

      Type Parameters

      Parameters

      • this: new () => T
      • values: Partial<T>

        The values to initialize the model with

      • Optionaltx: IDBTransaction

      Returns Promise<T>

      • The new model instance
    • Loop through all model instances.

      Type Parameters

      Parameters

      • this: new () => T
      • callback: ForEachCallback<T>

        The callback to call for each model instance

      • Optionaltx: IDBTransaction

      Returns Promise<void>

      Query.forEach for more information

      // Simple loop
      await User.forEach((user) => {
      console.log(user.name)
      })
      // Loop with async callback
      await User.forEach(async (user, tx) => {
      await user.delete(tx)
      })
    • Get a model instance by its primary key.

      Type Parameters

      Parameters

      • this: new () => T
      • key: IDBValidKey

        The primary key of the model

      • Optionaltx: IDBTransaction

      Returns Promise<T | null>

      • The model instance or null if not found
    • Get all the primary keys of the table.

      Parameters

      • Optionaltx: IDBTransaction

      Returns Promise<IDBValidKey[]>

      • A list with all the primary keys
    • Start a query for this model with an order.

      Type Parameters

      Parameters

      • this: new () => T
      • orderBy: OrderBy<T>

        The field to order by, prepend a - to reverse the order

      Returns Query<T>

      • The new query

      Query.orderBy for more information

    • Start a query for this model with a specific index.

      Type Parameters

      Parameters

      • this: new () => T
      • index: string

        The index to use

      • query: BetweenFilter<unknown>

        A query to filter the index

      Returns Query<T>

      • The new query

      Query.withIndex for more information