Socket
Socket
Sign inDemoInstall

@orion-js/typed-model

Package Overview
Dependencies
7
Maintainers
3
Versions
72
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

@orion-js/typed-model

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).


Version published
Weekly downloads
287
increased by7.09%
Maintainers
3
Created
Weekly downloads
 

Readme

Source

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).

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

FAQs

Last updated on 04 Jul 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc