@mathquis/modelx-resolvables
TypeScript icon, indicating that this package has built-in type declarations

3.0.0 • Public • Published

ModelX Resolvables

Resolve ModelX model recursively

Installation

This package requires @mathquis/modelx installed as a peer dependency.

npm install -D @mathquis/modelx-resolvables

Usage

import { AbstractResolvableModel, AbstractResolvableCollection, resolvable } from '@mathquis/modelx-resolvables';
import { ResolvableModelClass, Identifier } from '@mathquis/modelx-resolvables/lib/types/AbstrackResolvableModel';

class ResolvableCollection extends AbstractResolvableCollection {
	public listById<T extends AbstractResolvableModel>(identifiers: Identifier[]): Promise<this> {
		// Implement how to list model by ids
	}
}

class UnresolvedModel extends AbstractResolvableModel {}

class ParentModel extends AbstractResolvableModel {}

class ResolvableModel extends AbstractResolvableModel {
	@resolvable(ParentModel, {
		// Model attribute used as resolvable attributes
		attributeName: 'parent',
		// Store the resolvable in cache for later resolutions
		cache: true,
		// Store the resolvable in a collection specific cache
		// If false (default) then store the resolvable in a global cache
		local: true,
		// Model to instanciate when the resolvable is not resolved
		unresolved: UnresolvedModel,
		// Only allow specific models (model and unresolved will be automatically allowed)
		allow: [ParentAltModel]
	})
	declare parent: ParentModel;

	static getResolvableCollection(): ResolvableCollection<AbstractResolvableModel> {
		// Implement this to provide a resolvable collection class for this model
	}

    protected getResolvableAttributes(propName: string, attributeName: string, attribute: any): IResolvableAttributes | undefined {
    	// Implement this to return attributes (with id property) of the resolvable for an attribute
    	// ex: return {...attribute, id: attribute.id};
    }

	public getModelClassFromAttribute(propName: string, attributeName: string, attribute: any): ResolvableModelClass<AbstractResolvableModel> | undefined {
		// Implement this to return different model according to the attribute value
		// Return nothing when you don't want to change the default model class define on the resolvable
	}

	protected getAttributeFromModel(propName: string, attributeName: string, model: AbstractResolvableModel): any {
		// Implement this to update the model with the specified resolvable attributes
		// ex: return model.attributes;
	}

}

Package Sidebar

Install

npm i @mathquis/modelx-resolvables

Weekly Downloads

79

Version

3.0.0

License

ISC

Unpacked Size

126 kB

Total Files

32

Last publish

Collaborators

  • mathquis