Skip to content

Instantly share code, notes, and snippets.

@pichillilorenzo
Created May 5, 2020 12:58
Show Gist options
  • Save pichillilorenzo/d8bb040bf0831d655c4801f62d848cb3 to your computer and use it in GitHub Desktop.
Save pichillilorenzo/d8bb040bf0831d655c4801f62d848cb3 to your computer and use it in GitHub Desktop.
Jackson-js Node.js + LoopBack 4: Book model
import {belongsTo, Entity, model, property} from '@loopback/repository';
import {JsonBackReference, JsonClassType, JsonFormat, JsonFormatShape, JsonIgnore, JsonProperty, JsonView} from 'jackson-js';
import {Writer, WriterWithRelations} from '.';
import {ProfileViews} from '../views';
@model({settings: {strict: false}})
export class Book extends Entity {
// Define well-known properties here
@property({type: 'number', id: true, generated: false})
@JsonProperty()
id: number;
@property({type: 'string', required: true})
@JsonProperty()
title: string;
@property({type: 'string', required: true})
@JsonProperty()
cover: string;
@property({type: 'number', required: true})
@JsonProperty()
@JsonView({value: () => [ProfileViews.registered]})
@JsonFormat({shape: JsonFormatShape.STRING, toFixed: 2})
price: number;
@JsonProperty()
@JsonClassType({type: () => [Writer]})
@JsonBackReference({contextGroups: ['writerContextApi']})
writer: Writer;
@JsonIgnore()
@belongsTo(() => Writer)
writerId: number;
constructor(data?: Partial<Book>) {
super(data);
}
getId() {
return this.id;
}
}
export interface BookRelations {
// describe navigational properties here
writer: WriterWithRelations;
}
export type BookWithRelations = Book & BookRelations;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment