# new ModelRegistryManager(testFunctionopt)
The ModelRegistryManager implements the ModelRegistry interface and provides functionality for registering, retrieving, and building model instances. It maintains a cache of model constructors indexed by name, allowing for efficient lookup and instantiation. This class is essential for the serialization and deserialization of model objects.
Registry manager for model constructors that enables serialization and rebuilding
Parameters:
| Name | Type | Attributes | Description |
|---|---|---|---|
testFunction |
function
|
<optional> |
Function to test if an object is a model, defaults to |
- Implements:
- ModelRegistry.<M>
Example
```typescript
// Create a model registry
const registry = new ModelRegistryManager();
// Register a model class
registry.register(User);
// Retrieve a model constructor by name
const UserClass = registry.get("User");
// Build a model instance from a plain object
const userData = { name: "John", age: 30 };
const user = registry.build(userData, "User");
```
Methods
# build(obj, clazzopt) → {M}
Parameters:
| Name | Type | Attributes | Description |
|---|---|---|---|
obj |
Record.<string, any>
|
||
clazz |
string
|
<optional> |
when provided, it will attempt to find the matching constructor |
Error If clazz is not found, or obj is not a Model meaning it has no ModelKeys.ANCHOR property
M
# get(name) → {ModelConstructor.<M>|undefined}
Gets a registered Model ModelConstructor
Parameters:
| Name | Type | Description |
|---|---|---|
name |
string
|
ModelConstructor.<M>
|
undefined
# register(constructor, nameopt) → {void}
Adds a model constructor to the registry cache, making it available for later retrieval and instantiation. If no name is provided, the constructor's name property is used as the key in the registry.
Registers a model constructor with the registry
Parameters:
| Name | Type | Attributes | Description |
|---|---|---|---|
constructor |
ModelConstructor.<M>
|
The model constructor to register |
|
name |
string
|
<optional> |
Optional name to register the constructor under, defaults to constructor.name |
If the constructor is not a function
Error
void
# static fromModel(self, objopt) → {T}
Repopulates the instance with properties from the new Model Object, recursively rebuilding nested models
Copies and rebuilds properties from a source object to a model instance, handling nested models
Parameters:
| Name | Type | Attributes | Description |
|---|---|---|---|
self |
T
|
The target model instance to update |
|
obj |
T
|
Record.<string, any>
|
<optional> |
The source object containing properties to copy |
- The updated model instance with rebuilt nested models
T
# static getBuilder() → {ModelBuilderFunction|undefined}
Returns the current global ModelBuilderFunction used for building model instances
Retrieves the currently configured global model builder function
- The current global builder function or undefined if not set
ModelBuilderFunction
|
undefined
# static setBuilder(builderopt) → {void}
Sets the Global ModelBuilderFunction used for building model instances
Configures the global model builder function
Parameters:
| Name | Type | Attributes | Description |
|---|---|---|---|
builder |
ModelBuilderFunction
|
<optional> |
The builder function to set as the global builder |
void
# static setRegistry(modelRegistry) → {void}
Sets the current model registry to a custom implementation
Configures the model registry to be used by the Model system
Parameters:
| Name | Type | Description |
|---|---|---|
modelRegistry |
BuilderRegistry.<any>
|
The new implementation of Registry to use |
void