@orion-js/typed-model
TypeScript icon, indicating that this package has built-in type declarations

3.7.4 • Public • Published

Typed Model Package

Allows you to define a class that represents a model. This allows quicker development and better type checking of your models (since all class type definitions can be reused).

Recommended usage example

Folder structure

+-- models/
    +-- user/
        +-- index.ts
        +-- resolvers/
            +-- fullName.ts

Resolver definition

// models/user/resolvers/fullName.ts

export const fullNameResolver = async ({firstName, lastName}) => `${firstName} ${lastName}`

export const fullNameSchema = {
  returns: {type: String},
  resolve: fullNameResolver
}

Model definition

// models/user/index.ts

import {Prop, Schema, Resolver, getModelForClass} from '@orion-js/typed-model'
import {fullNameSchema, fullNameResolver} from './resolvers/fullName'

@Schema()
export class User {
  @Prop()
  firstName: string

  @Prop({optional: true})
  lastName: string

  @Resolver(fullNameSchema)
  fullName: typeof fullNameResolver
}

export default getModelForClass(User)

Advanced usage

Schema Extension

Typed-model allows extending an existing Schema to reduce code repetition.

import {SchemaFieldTypes} from '@orion-js/schema'

@Schema()
class ModelWithId {
  @Prop({type: SchemaFieldTypes.ID})
  _id: string
}

class ModelWithName extends ModelWithId {
  @Prop()
  name: string
}

export default getModelForClass(ModelWithName)
// Will contain both _id and name props

Readme

Keywords

none

Package Sidebar

Install

npm i @orion-js/typed-model

Weekly Downloads

282

Version

3.7.4

License

MIT

Unpacked Size

58.7 kB

Total Files

51

Last publish

Collaborators

  • regn
  • nicolaslopezj
  • jaletelier