Compare commits
2 commits
40b348862a
...
7707789bbf
Author | SHA1 | Date | |
---|---|---|---|
7707789bbf | |||
fbd2763ea6 |
3 changed files with 103 additions and 0 deletions
24
README.md
24
README.md
|
@ -274,6 +274,30 @@ Get a model class (which has all the sharkitek models' functions) from a model i
|
||||||
const model = definedModel.model(modelInstance);
|
const model = definedModel.model(modelInstance);
|
||||||
```
|
```
|
||||||
|
|
||||||
|
#### `assign(object)`
|
||||||
|
|
||||||
|
Assign fields from a provided object to the model instance properties. Fields which are not properties of the target model are silently ignored.
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
const alteredModelInstance = definedModel.model(modelInstance).assign({
|
||||||
|
anyProperty: "foo",
|
||||||
|
anotherOne: true,
|
||||||
|
not_a_property: "will be ignored",
|
||||||
|
});
|
||||||
|
```
|
||||||
|
|
||||||
|
#### `from(object)`
|
||||||
|
|
||||||
|
Initialize a model instance and assign the provided fields to its properties. Fields which are not properties of the target model are silently ignored.
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
const newModelInstance = definedModel.from({
|
||||||
|
anyProperty: "foo",
|
||||||
|
anotherOne: true,
|
||||||
|
not_a_property: "will be ignored",
|
||||||
|
});
|
||||||
|
```
|
||||||
|
|
||||||
#### `serialize()`
|
#### `serialize()`
|
||||||
|
|
||||||
Serialize the model.
|
Serialize the model.
|
||||||
|
|
|
@ -382,6 +382,22 @@ export class Model<T extends object, Shape extends ModelShape<T>, Identifier ext
|
||||||
|
|
||||||
return cloned.instance; // Returning the cloned instance.
|
return cloned.instance; // Returning the cloned instance.
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Assign the provided fields to existing properties.
|
||||||
|
* Fields that cannot be matched to existing properties are silently ignored.
|
||||||
|
* @param fields The fields to assign to the model.
|
||||||
|
*/
|
||||||
|
assign(fields: Partial<ModelPropertiesValues<T, Shape>> & {[field: string]: any}): ModelInstance<T, Shape, Identifier>
|
||||||
|
{
|
||||||
|
for (const field in fields)
|
||||||
|
{ // For each field, if it's a property, assign its value.
|
||||||
|
if ((this.definition.properties as any)?.[field])
|
||||||
|
// Set the instance value.
|
||||||
|
this.instance[field as keyof T] = fields[field];
|
||||||
|
}
|
||||||
|
return this.instance;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -444,6 +460,16 @@ export class ModelManager<T extends object, Shape extends ModelShape<T>, Identif
|
||||||
return (new Model<T, Shape, Identifier>(this)).initInstance();
|
return (new Model<T, Shape, Identifier>(this)).initInstance();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Initialize a new model instance with the provided object properties values.
|
||||||
|
* Fields that cannot be matched to existing properties are silently ignored.
|
||||||
|
* @param fields
|
||||||
|
*/
|
||||||
|
from(fields: Partial<ModelPropertiesValues<T, Shape>> & {[field: string]: any}): ModelInstance<T, Shape, Identifier>
|
||||||
|
{
|
||||||
|
return this.model().assign(fields);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Parse the serialized model object to a new model instance.
|
* Parse the serialized model object to a new model instance.
|
||||||
* @param serialized The serialized model object.
|
* @param serialized The serialized model object.
|
||||||
|
|
|
@ -326,4 +326,57 @@ describe("model", () => {
|
||||||
expect(TestModel.model.model(clonedDeserializedModel).serialize()).toStrictEqual({ id: 5, label: "testing" });
|
expect(TestModel.model.model(clonedDeserializedModel).serialize()).toStrictEqual({ id: 5, label: "testing" });
|
||||||
expect(clonedDeserializedModel.notAProperty.hello).toEqual("world");
|
expect(clonedDeserializedModel.notAProperty.hello).toEqual("world");
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("assigns properties, ignoring fields which are not properties", () => {
|
||||||
|
const deserializedArticle = Article.model.parse({
|
||||||
|
id: 1,
|
||||||
|
title: "this is a test",
|
||||||
|
authors: [
|
||||||
|
{ id: 52, name: "John Doe", email: "test@test.test", createdAt: "2022-08-07T08:47:01.000Z", active: true, },
|
||||||
|
{ id: 4, name: "Tester", email: "another@test.test", createdAt: "2022-09-07T18:32:55.000Z", active: false, },
|
||||||
|
],
|
||||||
|
text: "this is a long test.",
|
||||||
|
evaluation: "8.52",
|
||||||
|
tags: [ {name: "test"}, {name: "foo"} ],
|
||||||
|
comments: [
|
||||||
|
{ id: 542, author: { id: 52, name: "John Doe", email: "test@test.test", createdAt: "2022-08-07T08:47:01.000Z", active: true, }, message: "comment content", },
|
||||||
|
],
|
||||||
|
});
|
||||||
|
|
||||||
|
// Assign title and text, html is silently ignored.
|
||||||
|
Article.model.model(deserializedArticle).assign({
|
||||||
|
title: "something else",
|
||||||
|
text: "fully new text! yes!",
|
||||||
|
html: "<p>fully new text! yes!</p>",
|
||||||
|
});
|
||||||
|
expect((deserializedArticle as any)?.html).toBeUndefined();
|
||||||
|
expect(Article.model.model(deserializedArticle).patch()).toStrictEqual({
|
||||||
|
id: 1,
|
||||||
|
title: "something else",
|
||||||
|
text: "fully new text! yes!",
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it("initializes a model from properties values", () => {
|
||||||
|
const testArticle = Article.model.from({
|
||||||
|
title: "this is a test",
|
||||||
|
authors: [
|
||||||
|
Account.model.from({ name: "John Doe", email: "test@test.test", createdAt: new Date(), active: true }),
|
||||||
|
],
|
||||||
|
text: "this is a long text",
|
||||||
|
evaluation: 8.52,
|
||||||
|
tags: [{ name: "test" }, { name: "foo" }],
|
||||||
|
|
||||||
|
unknownField: true,
|
||||||
|
anotherOne: "test",
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(testArticle.title).toBe("this is a test");
|
||||||
|
expect(testArticle.text).toBe("this is a long text");
|
||||||
|
expect(testArticle.evaluation).toBe(8.52);
|
||||||
|
expect(testArticle.authors).toHaveLength(1);
|
||||||
|
expect(testArticle.authors[0]?.name).toBe("John Doe");
|
||||||
|
expect((testArticle as any).unknownField).toBeUndefined();
|
||||||
|
expect((testArticle as any).anotherOne).toBeUndefined();
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
Loading…
Add table
Reference in a new issue