import DenaliObject from '../metal/object'; import ORMAdapter from './orm-adapter'; import { AttributeDescriptor, RelationshipDescriptor, SchemaDescriptor } from './descriptors'; import { Dict, Constructor } from '../utils/types'; export declare const augmentedWithAccessors: symbol; /** * The Model class is the core of Denali's unique approach to data and ORMs. It * acts as a wrapper and translation layer that provides a unified interface to * access and manipulate data, but translates those interactions into ORM * specific operations via ORM adapters. * * The primary purpose of having Models in Denali is to allow Denali addons to * access a common interface for manipulating data. Importantly, the goal is * **not** to let you swap different ORMs / databases with little effort. Don't * be surprised if you find your app littered with ORM specific code - that is * expected and even encouraged. For more on this concept, check out the Denali * blog. * * TODO: link to blog post on ^ * * @package data * @since 0.1.0 */ export default class Model extends DenaliObject { /** * Marks the Model as an abstract base model, so ORM adapters can know not to * create tables or other supporting infrastructure. * * @since 0.1.0 */ static abstract: boolean; /** * The schema definition for this model. Keys are the field names, and values * should be either `attr(...)', `hasOne(...)`, or `hasMany(...)` */ static schema: Dict; /** * Returns the schema filtered down to just the attribute fields */ static readonly attributes: Dict; /** * Returns the schema filtered down to just the relationship fields */ static readonly relationships: Dict; private static _augmentWithSchemaAccessors(); /** * Builds a new Model instance from an already existing ORM record reference * * @param record The ORM adapter record object */ static build(this: Constructor, record: any): T; /** * Retrieve a single record by it's id * * @param id The id of the record you want to lookup * @param options Options passed through to the ORM adapter * @since 0.1.0 */ static find(this: typeof Model & Constructor, id: any, options?: any): Promise; /** * Retrieve the first record matching the given query * * @param query The query to pass through to the ORM adapter * @param options An options object passed through to the ORM adapter * @since 0.1.0 */ static queryOne(this: typeof Model & Constructor, query: any, options?: any): Promise; /** * Fetch all records matching the given query * * @param query The query to pass through to the ORM adapter * @param options An options object passed through to the ORM adapter * @since 0.1.0 */ static query(this: typeof Model & Constructor, query: any, options?: any): Promise; /** * Fetch all records of this type * * @param options An options object passed through to the ORM adapter * @since 0.1.0 */ static all(this: typeof Model & Constructor, options?: any): Promise; /** * Create a new Model instance with the supplied data, and immediately * persist it * * @param data Data to populate the new Model instance with * @param options An options object passed through to the ORM adapter * @since 0.1.0 */ static create(this: typeof Model & Constructor, data?: any, options?: any): Promise; /** * The ORM adapter specific to this model type. Defaults to the application's * ORM adapter if none for this specific model type is found. * * @since 0.1.0 */ static readonly adapter: ORMAdapter; /** * The name of this Model type. Used in a variety of use cases, including * serialization. * * @since 0.1.0 */ static readonly modelName: string; /** * The underlying ORM adapter record. An opaque value to Denali, handled * entirely by the ORM adapter. * * @since 0.1.0 */ record: any; /** * The name of this Model type. Used in a variety of use cases, including * serialization. * * @since 0.1.0 */ readonly modelName: string; /** * The id of the record * * @since 0.1.0 */ id: any; /** * Tell the underlying ORM to build this record */ constructor(data?: any, options?: any); /** * Persist this model. * * @since 0.1.0 */ save(options?: any): Promise; /** * Delete this model. * * @since 0.1.0 */ delete(options?: any): Promise; /** * Returns the related record(s) for the given relationship. * * @since 0.1.0 */ getRelated(relationshipName: string, options?: any): Promise; /** * Replaces the related records for the given relationship with the supplied * related records. * * @since 0.1.0 */ setRelated(relationshipName: string, relatedModels: T | T[], options?: any): Promise; /** * Add a related record to a hasMany relationship. * * @since 0.1.0 */ addRelated(relationshipName: string, relatedModel: T, options?: any): Promise; /** * Remove the given record from the hasMany relationship * * @since 0.1.0 */ removeRelated(relationshipName: string, relatedModel: T, options?: any): Promise; /** * Return an human-friendly string representing this Model instance, with a * summary of it's attributes * * @since 0.1.0 */ inspect(): string; /** * Return an human-friendly string representing this Model instance * * @since 0.1.0 */ toString(): string; }