Skip to content

Commit

Permalink
docs(lb4): repository-json-schema docs
Browse files Browse the repository at this point in the history
  • Loading branch information
shimks committed Jan 30, 2018
1 parent 9f0e166 commit 3660dee
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 7 deletions.
2 changes: 1 addition & 1 deletion pages/en/lb4/Decorators.md
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ For more information, see the [Dependency Injection](Dependency-Injection.htm) s

#### Property Decorator

Syntax: `@property(definition: PropertyDefinition)`
Syntax: `@property(definition?: PropertyDefinition)`

The property decorator defines metadata for a property on a Model definition.
The format of property definitions can be found in [Property definitions](https://loopback.io/doc/en/lb2/Model-definition-JSON-file.html#properties)
Expand Down
7 changes: 3 additions & 4 deletions pages/en/lb4/Implementing-features.md
Original file line number Diff line number Diff line change
Expand Up @@ -243,18 +243,17 @@ LoopBack is agnostic when it comes to accessing databases. You can choose any pa
export class Product extends Entity {
@property({
description: 'The unique identifier for a product',
type: 'number',
id: true,
})
id: number;

@property({type: 'string', required: true})
@property({required: true})
name: string;

@property({type: 'string', required: true})
@property({required: true})
slug: string;

@property({type: 'number', required: true})
@property({required: true})
price: number;

// Add the remaining properties yourself:
Expand Down
4 changes: 2 additions & 2 deletions pages/en/lb4/Repositories.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,10 @@ import {Entity, model, property} from '@loopback/repository';

@model()
export class Account extends Entity {
@property({type: 'number', id: true})
@property({id: true})
id: number;

@property({type: 'string', required: true})
@property({required: true})
name: string;
}
```
Expand Down
70 changes: 70 additions & 0 deletions pages/en/lb4/Schemas.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,76 @@ class Customer extends Entity {
}
```

### JSON Schema inference
With type information inferred from using `@model` and `@property` decorators,
we've created a module to easily build a JSON Schema from a decorated model.
In the `@loopback/repository-json-schema` module, we provide `getJsonSchema`
which accesses the metadata stored by the decorators to build a matching
JSON Schema of your model.

```ts
import {model, property} from '@loopback/repository';
import {getJsonSchema} from '@loopback/repository-json-schema';

@model()
class Category {
@property() name: string;
}

@model()
class Product {
@property({required: true}) name: string;
@property() type: Category;
}

const jsonSchema = getJsonSchema(Product);
```

`jsonSchema` from above would return:

```json
{
"title": "Product",
"properties": {
"name": {
"type": "string"
},
"type": {
"$ref": "#/definitions/Category"
}
},
"definitions": {
"Category": {
"properties": {
"name": {
"type": "string"
}
}
}
},
"required": ["name"]
}
```

If a custom type is specified for a decorated property in a model definition, then
a reference [`$ref`](http://json-schema.org/latest/json-schema-core.html#rfc.section.8)
field is created for it and a `definitions` sub-schema is created at the top-level
of the schema and populated with the type definition by recursively calling
`getJsonSchema` to build its properties. This allows for complex and nested custom
type definition building. The example above illustrates this point by having the
custom type `Category` as a property of our `Product` model definition.

#### Supported JSON keywords
Following are the supported keywords that can be explicitly passed into the decorators
to better tailor towards the JSON Schema being produced.

| Keywords | Decorator | Type | Default | Description |
|-------------|-------------|---------|--------------|---------------------------------------------------------|
| title | `@model` | string | *model name* | Name of the model |
| description | `@model` | string | | Description of the model |
| array | `@property` | boolean | | Used to specify whether the property is an array or not |
| required | `@property` | boolean | | Used to specify whether the property is required or not |

## Other ORMs
You might decide to use an alternative ORM/ODM in your LoopBack application.
Loopback v4 no longer expects you to provide your data in its own custom Model
Expand Down

0 comments on commit 3660dee

Please sign in to comment.