# 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