Class Model

Hierarchy

  • default
    • Model

Implements

  • default

Constructors

Properties

Accessors

Methods

[iterator] addRelation addWhereConstraint appendToEndpoint belongsTo belongsToMany call castAttribute clone compileQueryParameters createDescriptor delete deleteAttribute distinct except fill for forceFill freshTimestamps get getAttribute getAttributeKeys getAttributes getCastType getChanges getCreatedAtName getDeletedAtName getDeletedAttributes getEndpoint getFillable getFillableFromObject getGuarded getKey getKeyName getMorphs getName getNewAttributes getOriginal getRawAttributes getRawOriginal getRelation getRelationType getRelations getUpdatedAtName guessForeignKeyName has hasCast hasChanges hasGetAccessor hasMany hasOne hasSetMutator implementsCaster is isClean isDirty isFillable isGuarded isNot latest limit load loadedRelationKeys mergeCasts mergeFillable mergeGuarded morphMany morphOne morphTo new newInstanceFromResponseData offset oldest only orWhere orWhereBetween orWhereIn orWhereKey orWhereKeyNot orWhereNotBetween orWhereNotIn orWhereNotNull orWhereNull orderBy orderByDesc page patch post put refresh relationDefined relationLoaded removeRelation replicate reset resetEndpoint resetQueryParameters restore save scope select setAttribute setCasts setEndpoint setFillable setGuarded setLastSyncedAt setModelEndpoint setServerStringCase setStringCase setupMagicAccess skip syncOriginal tap throwIfModelDoesntExistsWhenCalling toJSON toString touch trashed unless update usesSoftDeletes usesTimestamps when where whereBetween whereIn whereKey whereKeyNot whereNotBetween whereNotIn whereNotNull whereNull with without all distinct factory find findMany get has latest limit make newQuery offset oldest orderBy orderByDesc page scope select skip where whereBetween whereIn whereKey whereKeyNot whereNotBetween whereNotIn whereNotNull whereNull with without

Constructors

Properties

attributeCasts: Record<string, CastType> = ...

The attributes that should be cast.

attributes: SimpleAttributes<default> = ...

The attributes.

fillableAttributes: string[] = ...

The attributes that are mass assignable.

guardedAttributes: string[] = ...

The attributes that aren't mass assignable

hasOneOrManyParentKeyName: undefined | string

The key name of the parent of this model which was instantiated from a hasOne or hasMany relation. This is used to remove the where query when saving a new entity like parent.$child().save({});

mutatedEndpoint: string = ''

The endpoint used when querying the api.

original: SimpleAttributes<default> = ...

The attribute's original state.

queryParameters: Required<Partial<{
    columns: string[];
    distinct: string[];
    limit: number;
    offset: number;
    orders: Order[];
    page: number;
    relationsExists: string[];
    scopes: string[];
    wheres: WhereDescription[];
    with: string[];
}>> & {
    without: string[];
} = ...
relations: Record<string, Model | ModelCollection<Model>> = {}

The loaded relations for the model. The keys do not include the relation prefixes.

softDeletes: boolean = true

Indicates if the model should expect a timestamp for soft-deletion.

timestamps: boolean = true

Indicates if the model should expect timestamps.

withRelations: string[] = []

The relations that should be included on every request.

createdAt: string = 'created_at'

The name of the created at attribute on the server side.

deletedAt: string = 'deleted_at'

The name of the deleted at attribute on the server side.

updatedAt: string = 'updated_at'

The name of the updated at attribute on the server side.

Accessors

  • get attributeCasing(): "camel" | "snake"
  • Property indicating how attributes and relation names should be cast by default.

    Returns "camel" | "snake"

  • get exists(): boolean
  • Indicates whether the model exists on the backend or not.

    Returns boolean

  • get keyType(): "string" | "number"
  • Protected

    The type of the key that acts as the primary key of the model.

    Returns "string" | "number"

  • get loading(): boolean
  • Boolean flag indicating whether there is an ongoing request or not.

    Returns boolean

  • get relationMethodPrefix(): string
  • Protected

    The string all the relation methods expected to prefixed by.

    Returns string

  • get serverAttributeCasing(): "camel" | "snake"
  • Protected

    Property indicating how attributes and relation names should be cast by default when sent to the server.

    Returns "camel" | "snake"

Methods

  • The iterator used for looping over the attributes and relations.

    Returns Iterator<any, any, undefined>

  • Protected

    Add a where constraint to the wheres.

    Parameters

    • column: string
    • operator: Operator
    • value: any
    • boolean: BooleanOperator

    Returns Model

  • Set the endpoint on the correct model for querying.

    Type Parameters

    Parameters

    • related: (new () => T)
        • new (): T
        • Returns T

    • Optional foreignKey: string

    Returns T

  • Set the endpoint on the correct model for querying.

    Type Parameters

    Parameters

    • related: (new () => T)
        • new (): T
        • Returns T

    • Optional relationName: string

      The name of the relation on the backend.

    Returns T

  • Protected

    The call method that mediates between the model and api handlers.

    Type Parameters

    • T = any

    Parameters

    • method: "head" | "options" | "HEAD" | "OPTIONS" | "TRACE" | "trace" | "GET" | "get"
    • Optional data: Partial<{
          columns: string[];
          distinct: string[];
          limit: number;
          offset: number;
          orders: Order[];
          page: number;
          relationsExists: string[];
          scopes: string[];
          wheres: WhereDescription[];
          with: string[];
      }> | SimpleAttributes
    • Optional customHeaders: CustomHeaders

    Returns Promise<undefined | T>

  • Type Parameters

    • T = any

    Parameters

    Returns Promise<undefined | T>

  • Protected Internal

    Cast the attribute to the specified type.

    Type Parameters

    • T

    Parameters

    • key: string
    • value: any
    • method: keyof AttributeCaster = 'get'

      The method to use when interacting with the AttributeCaster.

    Returns T

  • Protected

    Compiles the query parameters into a single object.

    Returns Partial<{
        columns: string[];
        distinct: string[];
        limit: number;
        offset: number;
        orders: Order[];
        page: number;
        relationsExists: string[];
        scopes: string[];
        wheres: WhereDescription[];
        with: string[];
    }>

  • Send a GET request to the endpoint.

    Type Parameters

    Parameters

    • Optional queryParameters: Partial<{
          columns: string[];
          distinct: string[];
          limit: number;
          offset: number;
          orders: Order[];
          page: number;
          relationsExists: string[];
          scopes: string[];
          wheres: WhereDescription[];
          with: string[];
      }> & Record<string, unknown>

      append and/or overwrite query parameter values.

    Returns Promise<RT>

    • of Model|ModelCollection @ts-expect-error - despite TS2526, it still infers correctly
  • Get an attribute from the model.

    Type Parameters

    • K extends string

    • T extends unknown

    Parameters

    • key: K
    • defaultValue: T

    Returns T

  • Type Parameters

    • K extends string

    • T extends unknown

    Parameters

    • key: K
    • Optional defaultValue: T

    Returns undefined | T

  • Protected

    Get the type of cast for a model attribute.

    Parameters

    • key: string

    Returns undefined | "object" | BuiltInCastType

  • Get the primary key for the model.

    Type Parameters

    • T extends "string" | "number" = "string" | "number"

    Returns undefined | (T extends "number"
        ? number
        : string)

  • Get the polymorphic column names.

    Parameters

    • Optional name: string

    Returns Record<"type" | "id", string>

  • Protected

    Get the name of the relation type for the given relation.

    Parameters

    • name: string

    Returns Relation

  • Guess the foreign key name that would be used to reference this model.

    Returns string

  • Determine whether an attribute should be cast to a determined type.

    Parameters

    • key: string

    Returns key is "object" | BuiltInCastType

  • Determine whether the given attribute has changed or attributes has been added or deleted.

    Parameters

    • Optional key: string

    Returns boolean

  • Determine if a get accessor exists for an attribute.

    Parameters

    • key: string

    Returns boolean

  • Set the endpoint on the correct model for querying.

    Type Parameters

    Parameters

    • related: (new () => T)
        • new (): T
        • Returns T

    • foreignKey: string = ...

    Returns T

  • Set the endpoint on the correct model for querying.

    Type Parameters

    Parameters

    • related: (new () => T)
        • new (): T
        • Returns T

    • foreignKey: string = ...

    Returns T

  • Determine if a set mutator exists for an attribute.

    Parameters

    • key: string

    Returns boolean

  • Determine if two models have the same key and of the same type.

    Type Parameters

    Parameters

    • model: unknown

    Returns model is M

  • Determine whether given or any attributes was changed.

    Parameters

    • Optional key: string

    Returns boolean

  • Determine if two models are not the same.

    See

    Model.prototype.is

    Type Parameters

    Parameters

    • model: unknown

    Returns model is Exclude<unknown, M>

  • Load relationship(s) from remote.

    Parameters

    • relations: MaybeArray<string>
    • forceReload: boolean = false

      Whether the already loaded relations should also be reloaded.

    Returns Promise<Model>

  • Set the endpoint on the correct model for querying.

    Type Parameters

    Parameters

    • related: (new () => T)
        • new (): T
        • Returns T

    • Optional morphName: string

    Returns T

  • Set the endpoint on the correct model for querying.

    Type Parameters

    Parameters

    • related: (new () => T)
        • new (): T
        • Returns T

    • Optional morphName: string

    Returns T

  • Add a constraint for the next query to return all relation.

    Example

    public $contractable(): this {
    return this.morphTo<Team | User>((self, _data) => {
    return self.contractableType === 'team' ? Team : User;
    });
    }

    Type Parameters

    Parameters

    • cb: MorphToCallback<Model, T>

      Callback that returns a model that this morphs to.

    • Optional relationName: string

      The name of the relation to be called. E.g.: 'commentable'

    Returns Model

  • Get a subset of the model's attributes.

    Type Parameters

    • K extends string[]

    • R = {
          [P in string]?: unknown
      }

    Parameters

    • attributes: K

    Returns R

  • Type Parameters

    • K extends string

    • R = Record<K, Model[K]>

    Parameters

    • attributes: K

    Returns R

  • Protected

    Assert whether the relation has been defined on this instance.

    Parameters

    • name: string

    Returns boolean

  • Determine if the given relation is loaded.

    Parameters

    • name: string

    Returns boolean

  • Protected

    Set the last synced at attribute. This is only expected to be used when mocking model to look like it exists.

    Parameters

    • to: unknown = ...

    Returns Model

  • Protected

    Utility to cast the given string to the serverAttributeCasing's case.

    See

    serverAttributeCasing

    Parameters

    • key: string

    Returns string

  • Protected

    Utility to cast the given string to the attributeCasing's case.

    See

    attributeCasing

    Parameters

    • key: string

    Returns string

  • Create simple access for the getters and setters that have no underlying attribute set.

    Should only be called after the attributes have already been set.

    Parameters

    Returns void

  • Pass a clone of the model to a given function.

    Parameters

    • callback: ((model) => void)
        • (model): void
        • Parameters

          Returns void

    Returns Model

  • Protected Internal

    Throw an error if the model does not exist before calling the specified method.

    Parameters

    • methodName: string

    Returns void

  • Call the provided function with the model if the given value is false.

    Parameters

    • val: any
    • closure: ((instance) => any)
        • (instance): any
        • Parameters

          Returns any

    Returns Model

  • Call the provided function with the model if the given value is true.

    Parameters

    • val: any
    • closure: ((instance) => any)
        • (instance): any
        • Parameters

          Returns any

    Returns Model

  • Add a where between closure to the query.

    Parameters

    • column: string
    • values: any[]
    • boolean: BooleanOperator = 'and'

    Returns Model

  • Add a where in closure to the query.

    Parameters

    • column: string
    • values: any[]
    • boolean: BooleanOperator = 'and'

    Returns Model

  • Add a where not between closure to the query.

    Parameters

    • column: string
    • values: any[]
    • boolean: BooleanOperator = 'and'

    Returns Model

  • Add a where not in closure to the query.

    Parameters

    • column: string
    • values: any[]
    • boolean: BooleanOperator = 'and'

    Returns Model

  • The static version of the distinct method.

    See

    BuildsQuery.prototype.distinct

    Type Parameters

    • T extends StaticToThis

    Parameters

    Returns T["prototype"]

  • Call the factory fluently from the model.

    Type Parameters

    Parameters

    • this: T
    • times: number = 1

    Returns default<T["prototype"], Factory<T["prototype"]>>

  • Find the model based on the given id.

    Type Parameters

    Parameters

    • this: T
    • id: string | number

    Returns Promise<T["prototype"]>

  • The get method made available as a static method.

    See

    CallsApi.prototype.get

    Type Parameters

    Parameters

    • this: T
    • Optional queryParameters: Partial<{
          columns: string[];
          distinct: string[];
          limit: number;
          offset: number;
          orders: Order[];
          page: number;
          relationsExists: string[];
          scopes: string[];
          wheres: WhereDescription[];
          with: string[];
      }> & Record<string, unknown>

      append and/or overwrite query parameter values.

    Returns Promise<T["prototype"] | ModelCollection<T["prototype"]>>

  • The static version of the has method.

    See

    BuildsQuery.prototype.has

    Type Parameters

    • T extends StaticToThis

    Parameters

    Returns T["prototype"]

  • The static version of the latest method.

    See

    BuildsQuery.prototype.latest

    Type Parameters

    • T extends StaticToThis

    Parameters

    • this: T
    • Optional column: string

    Returns T["prototype"]

  • The static version of the limit method.

    See

    BuildsQuery.prototype.limit

    Type Parameters

    • T extends StaticToThis

    Parameters

    • this: T
    • count: number

    Returns T["prototype"]

  • Return the instantiated class.

    Type Parameters

    • T extends StaticToThis

    Parameters

    • this: T

    Returns T["prototype"]

  • The static version of the offset method.

    See

    BuildsQuery.prototype.offset

    Type Parameters

    • T extends StaticToThis

    Parameters

    • this: T
    • count: number

    Returns T["prototype"]

  • The static version of the oldest method.

    See

    BuildsQuery.prototype.oldest

    Type Parameters

    • T extends StaticToThis

    Parameters

    • this: T
    • column: string = 'created_at'

    Returns T["prototype"]

  • The static version of the orderBy method.

    See

    BuildsQuery.prototype.orderBy

    Type Parameters

    • T extends StaticToThis

    Parameters

    • this: T
    • column: string
    • direction: Direction = 'asc'

    Returns T["prototype"]

  • The static version of the orderByDesc method.

    See

    BuildsQuery.prototype.orderByDesc

    Type Parameters

    • T extends StaticToThis

    Parameters

    • this: T
    • column: string

    Returns T["prototype"]

  • The static version of the page method.

    See

    BuildsQuery.prototype.page

    Type Parameters

    • T extends StaticToThis

    Parameters

    • this: T
    • pageNumber: number

    Returns T["prototype"]

  • The static version of the scope method.

    See

    BuildsQuery.prototype.scope

    Type Parameters

    • T extends StaticToThis

    Parameters

    Returns T["prototype"]

  • The static version of the select method.

    See

    BuildsQuery.prototype.select

    Type Parameters

    • T extends StaticToThis

    Parameters

    Returns T["prototype"]

  • The static version of the skip method.

    See

    BuildsQuery.prototype.skip

    Type Parameters

    • T extends StaticToThis

    Parameters

    • this: T
    • count: number

    Returns T["prototype"]

  • The static version of the where method.

    See

    BuildsQuery.prototype.where

    Type Parameters

    • T extends StaticToThis

    Parameters

    • this: T
    • column: string
    • value: unknown

    Returns T["prototype"]

  • Type Parameters

    • T extends StaticToThis

    Parameters

    • this: T
    • column: string
    • operator: Operator
    • value: unknown
    • Optional boolean: BooleanOperator

    Returns T["prototype"]

  • The static version of the whereBetween method.

    Type Parameters

    • T extends StaticToThis

    Parameters

    • this: T
    • column: string
    • values: any[]
    • boolean: BooleanOperator = 'and'

    Returns T["prototype"]

  • The static version of the whereIn method.

    Type Parameters

    • T extends StaticToThis

    Parameters

    • this: T
    • column: string
    • values: any[]
    • boolean: BooleanOperator = 'and'

    Returns T["prototype"]

  • The static version of the whereKey method.

    See

    BuildsQuery.prototype.whereKey

    Type Parameters

    • T extends StaticToThis

    Parameters

    • this: T
    • value: MaybeArray<string | number>
    • boolean: BooleanOperator = 'and'

    Returns T["prototype"]

  • The static version of the whereKeyNot method.

    See

    BuildsQuery.prototype.whereNotIn

    Type Parameters

    • T extends StaticToThis

    Parameters

    • this: T
    • value: MaybeArray<string | number>
    • boolean: BooleanOperator = 'and'

    Returns T["prototype"]

  • The static version of the whereNotBetween method.

    Type Parameters

    • T extends StaticToThis

    Parameters

    • this: T
    • column: string
    • values: any[]
    • boolean: BooleanOperator = 'and'

    Returns T["prototype"]

  • The static version of the whereNotIn method.

    Type Parameters

    • T extends StaticToThis

    Parameters

    • this: T
    • column: string
    • values: any[]
    • boolean: BooleanOperator = 'and'

    Returns T["prototype"]

  • The static version of the whereNotNull method.

    See

    BuildsQuery.prototype.whereNotNull

    Type Parameters

    • T extends StaticToThis

    Parameters

    Returns T["prototype"]

  • The static version of the whereNull method.

    See

    BuildsQuery.prototype.whereNull

    Type Parameters

    • T extends StaticToThis

    Parameters

    Returns T["prototype"]

  • The static version of the with method.

    See

    BuildsQuery.prototype.with

    Type Parameters

    • T extends StaticToThis

    Parameters

    Returns T["prototype"]

  • The static version of the with method.

    See

    BuildsQuery.prototype.without

    Type Parameters

    • T extends StaticToThis

    Parameters

    Returns T["prototype"]

Generated using TypeDoc