UNPKG

@knorm/knorm

Version:

A JavaScript ORM written using ES6 classes

280 lines (279 loc) 10.1 kB
/** * Creates model instances and allows setting, getting, validating and casting * data before and/or after database operations. */ export class Model { /** * Creates a {@link Model} instance. * * @param {object} [data] Data to assign to the instance. This data can be * anything: data for fields (including virtual fields) or any arbitrary data. * If data is provided, it's set via {@link Model#setData}. */ constructor(data?: object | undefined); getField(field: any): any; getFields(fields: any): any; setDefaults({ fields }?: { fields: any; }): Model; /** * Sets an instance's data. * * ::: tip INFO * - Keys with `undefined` values are skipped. * - Virtuals with no setters are skipped. * ::: * * @param {object} data The data to assign to the instance. This could contain * anything, including field values (including virtual fields) and other * arbitrary data. * * @returns {Model} The same model instance * * @todo strict mode: throw if a virtual has no setter * @todo strict mode: check if all fields in the data are valid field names */ setData(data: object): Model; getFieldData({ fields }?: { fields: any; }): any; _getVirtualGetters(virtuals: any): any; _getVirtualData(name: any): any; getVirtualData({ virtuals }?: { virtuals: any; }): Promise<{}>; _isPromise(value: any): boolean; getVirtualDataSync({ virtuals }?: { virtuals: any; }): any; getData({ fields, virtuals }?: { fields: any; virtuals: any; }): Promise<any>; getDataSync({ fields, virtuals }?: { fields: any; virtuals: any; }): any; validate({ fields }?: { fields: any; }): Promise<Model>; cast({ fields, forSave, forFetch }?: { fields: any; forSave: any; forFetch: any; }): Model; getQuery(options?: {}, { forInsert }?: { forInsert: any; }): any; /** * Inserts a single row into the database. * * @param {object} [options] {@link Query} options. * * @returns {Promise} A `Promise` that resolves with the same instance * populated with the inserted row from the dabatase. The fields to be * returned in the data can be configured with the {@link Query#fields} or * {@link Query#returning} options. */ insert(options?: object | undefined): Promise<any>; /** * Updates a single row in the database. * * ::: warning NOTE * This method requires a value for either a [primary or a unique * field](/guides/fields.md#primary-and-unique-fields) to be set on the * instance in order to know what row to update. * ::: * * ::: tip INFO * This method sets the {@link Query#first} (return only the first row) and * {@link Query#require} (throw a {@link NoRowsError} if no row is matched for * update) query options. However, the {@link Query#require} option can be * disabled via the `options` param. * ::: * * @param {object} [options] {@link Query} options. * * @returns {Promise} A `Promise` that resolves with the same instance * populated with the updated row from the dabatase. The fields to be returned * in the data can be configured with the {@link Query#fields} or * {@link Query#returning} options. * * @todo throw {@link ModelError} instead of plain `Error` */ update(options?: object | undefined): Promise<any>; /** * Either inserts or updates a single row in the database, depending on * whether a value for the primary field is set or not. * * @param {object} [options] {@link Query} options. * * @returns {Promise} A `Promise` that resolves with the same instance * populated with the inserted or updated row from the dabatase. The fields to * be returned in the data can be configured with the {@link Query#fields} or * {@link Query#returning} options. * * @todo throw {@link ModelError} instead of plain `Error` */ save(options?: object | undefined): Promise<any>; /** * Fetches a single row from the database. * * ::: warning NOTE * This method requires a value for either a [primary or a unique * field](/guides/fields.md#primary-and-unique-fields) to be set on the * instance in order to know what row to fetch. * ::: * * ::: tip INFO * This method sets the {@link Query#first} (return only the first row) and * {@link Query#require} (throw a {@link NoRowsError} if no row is matched for * fetching) query options. However, the {@link Query#require} option can be * disabled via the `options` param. * ::: * * @param {object} [options] {@link Query} options. * * @returns {Promise} A `Promise` that resolves with the same instance * populated with data fetched from the database. The fields to be returned in * the data can be configured with the {@link Query#fields} or * {@link Query#returning} options. * * @todo throw {@link ModelError} instead of plain `Error` */ fetch(options?: object | undefined): Promise<any>; /** * Deletes a single row from the database. * * ::: warning NOTE * This method requires a value for either a [primary or a unique * field](/guides/fields.md#primary-and-unique-fields) to be set on the * instance in order to know what row to delete. * ::: * * ::: tip INFO * This method sets the {@link Query#first} (return only the first row) and * {@link Query#require} (throw a {@link NoRowsError} if no row is matched for * deleting) query options. However, the {@link Query#require} option can be * disabled via the `options` param. * ::: * * @param {object} [options] {@link Query} options. * * @returns {Promise} A `Promise` that resolves with the same instance * populated with the row deleted from the dabatase. The fields to be returned * in the data can be configured with the {@link Query#fields} or * {@link Query#returning} options. * * @todo throw {@link ModelError} instead of plain `Error` */ delete(options?: object | undefined): Promise<any>; /** * A reference to the {@link Knorm} instance. * * ::: tip * This is the same instance assigned to the {@link Model.knorm} static * property, just added as a convenience for use in instance methods. * ::: */ knorm: any; /** * The model registry. This is an object containing all the models added to the * ORM, keyed by name. See [model registry](/guides/models.md#model-registry) * for more info. * * ::: tip * This is the same object assigned to the {@link Model.models} static property, * just added as a convenience for use in instance methods. * ::: * * @type {object} */ models: object; /** * For models accessed within a transaction, this is reference to the * {@link Transaction} instance. * * ::: warning NOTE * This is only set for {@link Model} instances that are accessed within a * transaction, otherwise it's set to `null`. * ::: * * ::: tip * This is the same instance assigned to the {@link Model.transaction} static * property, just added as a convenience for use in static methods. * ::: */ transaction: any; } export namespace Model { /** * Inserts a single or multiple rows into the database. * * @param {Model|object|array} data The data to insert. Can be a plain object, * a {@link Model} instance or an array of objects or {@link Model} instances. * @param {object} [options] {@link Query} options * * ::: tip INFO * This method directly proxies to {@link Query#insert}. * ::: */ export function insert(data: any, options?: object | undefined): Promise<any>; /** * Updates a single or multiple rows in the database. * * @param {Model|object|array} data The data to update. Can be a plain object, * a {@link Model} instance or an array of objects or instances. * @param {object} [options] {@link Query} options * * ::: tip INFO * This method directly proxies to {@link Query#update}. * ::: */ export function update(data: any, options?: object | undefined): Promise<any>; /** * Either inserts or updates a single row or multiple rows in the database. * * @param {Model|object|array} data The data to update. Can be a plain object, * a {@link Model} instance or an array of objects or instances. * @param {object} [options] {@link Query} options * * ::: tip INFO * This method directly proxies to {@link Query#save}. * ::: */ export function save(data: any, options?: object | undefined): Promise<any>; /** * Fetches a single or multiple rows from the database. * * @param {object} [options] {@link Query} options * * ::: tip INFO * This method directly proxies to {@link Query#fetch}. * ::: */ export function fetch(options?: object | undefined): Promise<any>; export function addField(field: any): void; export function removeField(field: any): void; export function addVirtual(virtual: any): void; export function createConfig(): { model: typeof Model; _fields: {}; _virtuals: {}; _options: {}; fieldsToColumns: {}; unique: never[]; notUpdated: never[]; fieldNames: never[]; }; export const _config: any; export const knorm: any; export const models: object; export const transaction: any; export { Field }; export { Virtual }; export { Query }; } import { Field } from "./Field"; import { Virtual } from "./Virtual"; import { Query } from "./Query";