Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

vuex-orm-decorators

Package Overview
Dependencies
Maintainers
1
Versions
33
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

vuex-orm-decorators

Simple Typescript decorators to improve the vuex-orm experience in Typescript by introducing typed property access.

  • 1.0.7
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
92
decreased by-63.2%
Maintainers
1
Weekly downloads
 
Created
Source

vuex-orm-decorators

Decorator Syntax for Vuex ORM for better type safety and a better experience.

NPM npm bundle size GitHub issues Snyk Vulnerabilities for GitHub Repo

Typescript Decorators to simplify vuex-orm integration in typescript projects. If you are using the vue-module-decorators or vue-property-decorator packages then this will allow you to use the vuex-orm plugin in the same way.

Using the decorators allows better type safety in your projects by allowing you to create conventional Typescript properties, and anotate them as fields. Intellisense in Visual Studio Code just works with the annotations, where it doesn't in the vanilla plugin without boilerplate. Removes the 'magic string' aspect of vuex-orm.

This documentation isn't supposed to be a replacement for the vuex-orm documentation, if you are unfamiliar with the concepts the check out their documentation: https://vuex-orm.github.io/vuex-orm/guide/prologue/what-is-vuex-orm.html. I have linked to relevant guide pages in their documation throughout this documentation.

Contribute

If you have improvements or contributions to make, I will happily check and merge in pull requests.

 

Installation

npm install -D vuex-orm-decorators

This package targets es2015, if you need to target es5 then you will need to get VUE-CLI to transpile this package.

 

Typescript

  1. Set ExperimentalDecorators to true.
  2. Set importHelpers: truein tsconfig.json.
  3. Set emitHelpers: true in tsconfig.json (only required in typescript 2)

 

Basic Usage

Out of the box a vuex-orm model is defined as:

import { Model } from '@vuex-orm/core'

class User extends Model {
  static entity = 'users'

  static fields () {
    return {
      id: this.attr(undefined),
      name: this.attr('')
    }
  }
}

The defined fields don't gain type checking by Typescript in this way because they are never defined as properties of the model class. With this decorator library though it allows you to write the same in the following way to achieve type checking on your queried models:

import { Model } from '@vuex-orm/core'
import { AttrField, StringField } from 'vuex-orm-decorators'


@OrmModel('users')
class User extends Model{

    @AttrField(undefined)
    public id!: number;

    @StringField()
    public name!: string;
}

 

Getters

To create a fully reactive getter, simply add your getters to the model class:

import { Model } from '@vuex-orm/core'
import { AttrField, StringField } from 'vuex-orm-decorators'


@OrmModel('users')
class User extends Model{

    @AttrField(undefined)
    public id!: number;

    @StringField()
    public name!: string;

    public get lowerName(){
        return this.name.toLowerCase();
    }
}

 

Setting a Primary Key

Rather than setting a primary key by setting the static property primaryKey with the magic string name of the property you want to be the primary key, you can simply annotate the property with the @PrimaryKey decorator as follows:

import { Model } from '@vuex-orm/core'
import { AttrField, StringField } from 'vuex-orm-decorators'


@OrmModel('users')
class User extends Model{

    @PrimaryKey()
    @AttrField(undefined)
    public uuid!: number;

    @StringField()
    public name!: string;
}

In this example the property uuid replaces the default id property as the primary key.

 

Generic Types

You can create the generic attr field type using the @AttrField decorator.

 

Auto Increment

To create auto increment fields which use the @Increment decorator.

 

Primative Types

Like the vuex-orm library, you can create primative fields using the following decorators:

  1. @StringField creates a string field
  2. @NumberField creates a number field
  3. @BooleanField creates a boolean field

 

Creating Relationships

You can create all relationships defined in the vuex-orm library. All the relationship decorators take the exact same arguments as the vanilla vuex-orm library static functions.

  1. @HasManyField creates a HasMany relationship field
  2. @HasOneField creates a HasOne relationship field
  3. @BelongsToField creates an inverse HasOne relationship field
  4. @HasManyByField creates a HasManyBy relationship field
  5. @HasManyThroughField creates a HasManyThrough relationship field
  6. @BelongsToManyField creates a BelongsToMany relationship field
  7. @MorphToField creates a MorphTo relationship field
  8. @MorphOneField creates a MorphOne relationship field
  9. @MorphManyField creates a MorphMany relationship field
  10. @MorphToManyField creates a MorphToMany relationship field
  11. @MorphedByManyField creates a MorphedByMany relationship field

Keywords

FAQs

Package last updated on 23 Jul 2019

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
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc