@statezero/core
Version:
The type-safe frontend client for StateZero - connect directly to your backend models with zero boilerplate
134 lines (133 loc) • 4.88 kB
TypeScript
/**
* A constructor for a Model.
*
* @typedef {Function} ModelConstructor
* @param {any} data - Data to initialize the model.
* @returns {Model}
*
* @property {Manager} objects - The model's manager.
* @property {string} configKey - The configuration key.
* @property {string} modelName - The model name.
* @property {string} primaryKeyField - The primary key field (default 'id').
*/
/**
* Base Model class with integrated API implementation.
*
* @abstract
*/
export class Model {
/**
* Creates a new Model instance.
*
* @param {any} [data={}] - The data for initialization.
*/
static instanceCache: Map<any, any>;
/**
* Instantiate from pk using queryset scoped singletons
*/
static fromPk(pk: any, querySet: any): any;
/**
* Validates that the provided data object only contains keys
* defined in the model's allowed fields. Supports nested fields
* using double underscore notation (e.g., author__name).
*
* @param {Object} data - The object to validate.
* @throws {ValidationError} If an unknown key is found.
*/
static validateFields(data: Object): void;
/**
* Static method to validate data without creating an instance
* @param {Object} data - Data to validate
* @param {string} validateType - 'create' or 'update'
* @param {boolean} partial - Whether to allow partial validation
* @returns {Promise<boolean>} Promise that resolves to true if valid, throws error if invalid
*/
static validate(data: Object, validateType?: string, partial?: boolean): Promise<boolean>;
/**
* Get field permissions for the current user (cached on the class)
* @param {boolean} refresh - Force refresh the cached permissions
* @returns {Promise<{visible_fields: string[], creatable_fields: string[], editable_fields: string[]}>}
*/
static getFieldPermissions(refresh?: boolean): Promise<{
visible_fields: string[];
creatable_fields: string[];
editable_fields: string[];
}>;
constructor(data?: {});
serializer: ModelSerializer;
_data: {};
_pk: any;
__version: number;
touch(): void;
/**
* Sets the primary key of the model instance.
*
* @param {number|undefined} value - The new primary key value.
*/
set pk(value: number | undefined);
/**
* Returns the primary key of the model instance.
*
* @returns {number|undefined} The primary key.
*/
get pk(): number | undefined;
/**
* Gets a field value from the internal data store
*
* @param {string} field - The field name
* @returns {any} The field value
*/
getField(field: string): any;
/**
* Sets a field value in the internal data store
*
* @param {string} field - The field name
* @param {any} value - The field value to set
*/
setField(field: string, value: any): void;
/**
* Serializes the model instance.
*
* By default, it returns all enumerable own properties.
* Subclasses should override this to return specific keys.
*
* @param {boolean} includeRepr - Whether to include the repr field (for caching). Default: false.
* @returns {Object} The serialized model data.
*/
serialize(includeRepr?: boolean): Object;
/**
* Saves the model instance by either creating a new record or updating an existing one.
*
* @returns {Promise<Model>} A promise that resolves to the updated model instance.
*/
save(): Promise<Model>;
/**
* Deletes the instance from the database.
*
* Returns a tuple with the number of objects deleted and an object mapping
* model names to the number of objects deleted, matching Django's behavior.
*
* @returns {Promise<[number, Object]>} A promise that resolves to the deletion result.
* @throws {Error} If the instance has not been saved (no primary key).
*/
delete(): Promise<[number, Object]>;
/**
* Refreshes the model instance with data from the database.
*
* @returns {Promise<void>} A promise that resolves when the instance has been refreshed.
* @throws {Error} If the instance has not been saved (no primary key).
*/
refreshFromDb(): Promise<void>;
/**
* Validates the model instance using the same serialize behavior as save()
* @param {string} validateType - 'create' or 'update' (defaults to auto-detect)
* @param {boolean} partial - Whether to allow partial validation
* @returns {Promise<boolean>} Promise that resolves to true if valid, throws error if invalid
*/
validate(validateType?: string, partial?: boolean): Promise<boolean>;
}
/**
* A constructor for a Model.
*/
export type ModelConstructor = Function;
import { ModelSerializer } from "./serializers.js";