Skip to content

Commit

Permalink
Add createModelClass model to core package
Browse files Browse the repository at this point in the history
  • Loading branch information
xpepermint committed Sep 27, 2019
1 parent e8edd8f commit b443816
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 17 deletions.
50 changes: 35 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -409,9 +409,37 @@ graphql(schema, '{ hello }', root).then((response) => {

## API

### Model Class
### @rawmodel/core

This class is provided by the `@rawmodel/core` package.
**createModelClass(config)**

> Create the Model class from a list of property definitions.
| Option | Type | Required | Default | Description
|--------|------|----------|---------|------------
| config.$.name | String | Yes | - | Property name.
| config.$.prop.set | Function | No | - | Custom setter.
| config.$.prop.get | Function | No | - | Custom getter.
| config.$.prop.parse | Parser | No | - | Data type parser (see supported types).
| config.$.prop.defaultValue | Any | No | - | Prop default value.
| config.$.prop.fakeValue | Any | No | - | Prop fake value.
| config.$.prop.emptyValue | Any | No | - | Prop empty value.
| config.$.prop.validate | Array | No | - | List of validator recipes.
| config.$.prop.handle | Array | No | - | List of error handler recipes.
| config.$.prop.populatable | String[] | No | - | List of strategies for populating the property value.
| config.$.prop.serializable | String[] | No | - | List of strategies for serializing the property value.
| config.$.prop.enumerable | Boolean | No | true | Indicates that the property is enumerable.

```ts
const Model = createModelClass([
{
name: 'name',
prop: {
defaultValue: 'John Smith',
},
},
]);
```

**Model(data, config)**

Expand Down Expand Up @@ -645,10 +673,6 @@ try {
}
```

### Prop Class

This class is provided by the `@rawmodel/core` package.

**Prop(config)**

> A model property.
Expand Down Expand Up @@ -784,17 +808,14 @@ This class is provided by the `@rawmodel/core` package.

> Validates the `value` and populates the property with error.
### Schema Utils

This methods are provided by the `@rawmodel/schema` package.
### @rawmodel/schema

**createModelClass(recipe):Class**

> Returns a new generic model class build from the provided schema `recipe`.
| Option | Type | Required | Default | Description
|--------|------|----------|---------|------------
| recipe.context | Any | No | - | Arbitrary context data.
| recipe.getters | Object | No | - | Hash of getter functions which return a resolver.
| recipe.setters | Object | No | - | Hash of setters functions which return a resolver.
| recipe.defaultValues | Object | No | - | Hash of default value functions which return a resolver or static values.
Expand Down Expand Up @@ -826,7 +847,6 @@ This methods are provided by the `@rawmodel/schema` package.

```ts
const Model = createModelClass({
context: {},
getters: {
customGetter(options: any) { // custom getter function which returns a resolver
return function(v: any) { return v; } // context aware resolver
Expand Down Expand Up @@ -878,9 +898,9 @@ const Model = createModelClass({
});
```
### Available Parsers
### @rawmodel/parsers
Parsers are provided by the `@rawmodel/parsers` package. Note that every model can be used as a parser resolver.
**NOTE:** Every model can be used as a parser `resolver`.
**booleanParser()**: Function
Expand Down Expand Up @@ -913,7 +933,7 @@ const recipe = {
> Converts a value to a string.
### Available Validators
### @rawmodel/validators
Please note that the validators do not trigger if no value is present (on `undefined` or `null`). Make sure your custom validators follow the same concept. The exception are validators which verify value presence or absence.
Expand Down Expand Up @@ -1076,7 +1096,7 @@ const recipe = {
|--------|------|----------|---------|------------
| options.version | Integer | No | - | UUID version (1, 2, 3, 4 or 5).
### Available Handlers
### @rawmodel/handlers
**mongoUniquenessHandler(options)**: Function
Expand Down
22 changes: 22 additions & 0 deletions packages/rawmodel-core/src/core/builder.ts
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;
}
8 changes: 8 additions & 0 deletions packages/rawmodel-core/src/core/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,14 @@ export interface PropConfig {
model?: Model;
}

/**
* Model property class configuration object.
*/
export interface PropDefinition {
name: string;
prop?: PropConfig;
}

/**
* Parser recipe interface.
*/
Expand Down
3 changes: 2 additions & 1 deletion packages/rawmodel-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ export * from './core/parser';
export * from './core/validator';
export * from './core/handler';
export * from './core/parser';
export * from './core/models';
export * from './core/props';
export * from './core/models';
export * from './core/builder';
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;
1 change: 0 additions & 1 deletion packages/rawmodel-schema/src/core/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import { SchemaRecipe } from './types';
*/
export function createModelClass(recipe: SchemaRecipe): typeof Model {
recipe = {
context: {},
getters: {},
setters: {},
defaultValues: {},
Expand Down

0 comments on commit b443816

Please sign in to comment.