UNPKG

@statezero/core

Version:

The type-safe frontend client for StateZero - connect directly to your backend models with zero boilerplate

199 lines (198 loc) 7.65 kB
/** * @typedef {Object} SerializerOptions * @property {number} [depth] - How deep to serialize nested objects. * @property {string[]} [fields] - List of fields to include. * @property {number} [limit] - Maximum number of items to retrieve. * @property {number} [offset] - Offset for pagination. * @property {number} [overfetch] - Overfetch additional items. * @property {string} [namespace] - Custom namespace for real-time updates. */ /** * Manager class providing helper methods to work with QuerySets and models. * * @class Manager */ export class Manager { /** * Creates a new Manager. * * @param {Function} ModelClass - The model's constructor. * @param {Function} [QuerySetClass=QuerySet] - The QuerySet class to use. */ constructor(ModelClass: Function, QuerySetClass?: Function); ModelClass: Function; QuerySetClass: Function; /** * Creates a new QuerySet instance. * * @returns {QuerySet} A new QuerySet instance for the model. */ newQuerySet(): QuerySet<any>; /** * Retrieves a single model instance matching the provided filters. * * @param {Object} [filters] - The filters to apply. * @param {SerializerOptions} [serializerOptions] - Options for serialization. * @returns {Promise<Model>} A promise that resolves to the model instance. */ get(filters?: Object, serializerOptions?: SerializerOptions): Promise<Model>; /** * Filters the QuerySet based on the provided conditions. * * @param {*} conditions - The filter conditions. * @returns {QuerySet} A new QuerySet instance with the filters applied. */ filter(conditions: any): QuerySet<any>; /** * Excludes the specified conditions from the QuerySet. * * @param {*} conditions - The conditions to exclude. * @returns {QuerySet} A new QuerySet instance with the conditions excluded. */ exclude(conditions: any): QuerySet<any>; /** * Returns a QuerySet representing all records. * * @returns {QuerySet} A new QuerySet instance. */ all(): QuerySet<any>; /** * Deletes records in the QuerySet. * * @returns {Promise<[number, Object]>} A promise that resolves to an array where the first element is * the number of records deleted and the second is an object with details. */ delete(): Promise<[number, Object]>; /** * Counts the number of records matching the current query. * * @param {string} [field] - Optional field to count (defaults to 'pk') * @returns {Promise<number>} A promise that resolves to the count */ count(field?: string): Promise<number>; /** * Computes the sum of values for the specified field. * * @param {string} field - The field to sum * @returns {Promise<number>} A promise that resolves to the sum */ sum(field: string): Promise<number>; /** * Computes the average value for the specified field. * * @param {string} field - The field to average * @returns {Promise<number>} A promise that resolves to the average */ avg(field: string): Promise<number>; /** * Finds the minimum value for the specified field. * * @param {string} field - The field to find the minimum value for * @returns {Promise<any>} A promise that resolves to the minimum value */ min(field: string): Promise<any>; /** * Finds the maximum value for the specified field. * * @param {string} field - The field to find the maximum value for * @returns {Promise<any>} A promise that resolves to the maximum value */ max(field: string): Promise<any>; /** * Checks if any records exist that match the current query. * * @returns {Promise<boolean>} A promise that resolves to true if records exist, otherwise false */ exists(): Promise<boolean>; /** * Orders the QuerySet by the provided fields. * * @param {...(string|any)} fields - The fields to order by. Supports nested paths and descending order * (prefix field with '-'). * @returns {QuerySet} A new QuerySet instance with the order applied. */ orderBy(...fields: (string | any)[]): QuerySet<any>; /** * Creates a new model instance using the provided data, then saves it. * * @param {*} data - The data to create the model instance. * @returns {Promise<*>} A promise that resolves to the newly created model instance. */ create(data: any): Promise<any>; /** * Creates multiple model instances using the provided model instances. * * @param {Array<Model>} modelInstances - Array of unsaved model instances to create. * @returns {Promise<Array<*>>} A promise that resolves to an array of newly created model instances. */ bulkCreate(modelInstances: Array<Model>): Promise<Array<any>>; /** * Fetches all records using the current QuerySet. * * @param {SerializerOptions} [serializerOptions] - Options for serialization. * @returns {Promise<Array<*>>} A promise that resolves to an array of model instances. */ fetch(serializerOptions?: SerializerOptions): Promise<Array<any>>; /** * Retrieves or creates a model instance based on lookup fields and defaults. * * @param {*} lookupFields - The fields to lookup the model. * @param {Object} [defaults={}] - Default values to use when creating a new instance. * @returns {Promise<ResultTuple>} A promise that resolves to a ResultTuple containing the model instance * and a boolean indicating whether it was created. */ getOrCreate(lookupFields: any, defaults?: Object): Promise<ResultTuple>; /** * Updates or creates a model instance based on lookup fields and defaults. * * @param {*} lookupFields - The fields to lookup the model. * @param {Object} [defaults={}] - Default values to use when updating or creating the instance. * @returns {Promise<ResultTuple>} A promise that resolves to a ResultTuple containing the model instance * and a boolean indicating whether it was created. */ updateOrCreate(lookupFields: any, defaults?: Object): Promise<ResultTuple>; /** * Applies a search to the QuerySet using the specified search query and fields. * * @param {string} searchQuery - The search query. * @param {string[]} [searchFields] - The fields to search in. * @returns {QuerySet} A new QuerySet instance with the search applied. */ search(searchQuery: string, searchFields?: string[]): QuerySet<any>; /** * Returns a QuerySet marked as optimistic-only, meaning operations will only * update local state without making backend API calls. * * @returns {QuerySet} A new QuerySet with optimistic-only mode enabled. */ get optimistic(): QuerySet<any>; } export type SerializerOptions = { /** * - How deep to serialize nested objects. */ depth?: number | undefined; /** * - List of fields to include. */ fields?: string[] | undefined; /** * - Maximum number of items to retrieve. */ limit?: number | undefined; /** * - Offset for pagination. */ offset?: number | undefined; /** * - Overfetch additional items. */ overfetch?: number | undefined; /** * - Custom namespace for real-time updates. */ namespace?: string | undefined; }; import { QuerySet } from './querySet.js'; import { Model } from './model.js'; import { ResultTuple } from './queryExecutor.js';