-
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add createModelClass model to core package
- Loading branch information
1 parent
e8edd8f
commit b443816
Showing
6 changed files
with
104 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { Model } from './models'; | ||
import { PropDefinition } from './types'; | ||
|
||
/** | ||
* Returns a Model class generated from the provided properties. | ||
* @param recipe Model schema recipe. | ||
*/ | ||
export function createModelClass(props: PropDefinition[]): typeof Model { | ||
const Klass = class GenericModel extends Model {}; | ||
|
||
Object.defineProperty(Klass, '$props', { | ||
value: {}, | ||
enumerable: false, | ||
configurable: true, | ||
}); | ||
|
||
props.forEach((prop) => { | ||
Klass.$props[prop.name] = { ...prop.prop }; | ||
}); | ||
|
||
return Klass; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
packages/rawmodel-core/src/tests/core/builder/create-model-class-method.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { Spec } from '@hayspec/spec'; | ||
import { createModelClass, Model } from '../../..'; | ||
|
||
const spec = new Spec(); | ||
|
||
spec.test('generates empty model class', (ctx) => { | ||
const Klass = createModelClass([]); | ||
ctx.true(new Klass() instanceof Model); | ||
}); | ||
|
||
spec.test('generates model with properties', (ctx) => { | ||
const Klass = createModelClass([ | ||
{ | ||
name: 'name', | ||
}, | ||
{ | ||
name: 'book', | ||
prop: { | ||
parse: { | ||
resolver: createModelClass([ | ||
{ | ||
name: 'title', | ||
} | ||
]), | ||
}, | ||
} | ||
} | ||
]); | ||
const data = { | ||
name: 'foo', | ||
book: { title: 'bar' }, | ||
}; | ||
const model = new Klass(data); | ||
ctx.deepEqual(model.serialize(), data); | ||
}); | ||
|
||
export default spec; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters