npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@vuex-orm/plugin-change-flags

v1.2.3

Published

Vuex ORM plugin adding IsDirty / IsNew flags to model entities

Downloads

3,252

Readme

Build Status License

This is a plugin for the Vuex-ORM library. It adds two flags $isDirty and $isNew (as boolean attributes) on any instance of the entities created through this library and updates their values automatically.

Installation

Simply run npm install @vuex-orm/plugin-change-flags --save in your favorite terminal.

Then, you need to install the plugin as any VuexORM plugin. In your store initialization code, simply add:

import VuexORMisDirtyPlugin from '@vuex-orm/plugin-change-flags';

and then

VuexORM.use(VuexORMisDirtyPlugin);

Usage

Once the plugin installed, every time you create a new instance of an entity, the $isDirty and $isNew will be automatically added.

The default value for those flags is false. They are automatically set to true in the following cases:

$isDirty

This flag is automatically set to true when:

  • creating a new instance using the createNew method;
  • updating the entity instance in the store, using the update action (insert and create mutation will not set this flag to true).

Preventing flag setting If you want to prevent the flag to be set to true when updating, you can pass preventDirtyFlag: true to the update and insertOrUpdate calls:

User.update({
    data: myUserToUpdate,
    preventDirtyFlag: true
});

$isNew

This flag is automatically set to true when calling the createNew method.

Hydrating with other default values

When calling the Model constructor new Model(), the default values for both flags is false.
Other values can be set by passing the initial record value to the constructor:

let user = new User({ id: 1, $isNew: true });

console.log(user.$isNew); // true (set through parameter)
console.log(user.$isDirty); // false (default value)

Methods

This plugin also adds the Model.createNew method and the allDirty & allNew getters.

createNew factory method

The plugin provides a new Model.createNew(insertInStore = true) method which returns a new instance of the entity, with both flags set to true by default and all other fields set to their default value.

This method copies VuexORM new method behavior. As such, it will automatically insert the newly created entity in the store.

If you don't want the created instance to be inserted in store directly, you can pass false as parameter.

:warning: When calling the createNew method without parameter, this will try to insert the given record directly in the store. It might fail if default value for the Primary Key is null. Try using an increment type attribute or a mutator to make sure your PK as a value.

allDirty getter

This new getter returns all entities marked as dirty currently in the store.

It can be used globally:

// Returns an array of mixed types with all entities
// currently marked as dirty in the store
let results = store.getters['entities/allDirty']();

or specifically to a type:

// Returns an array User entities currently marked as dirty in the store
let results = store.getters['entities/users/allDirty']();

allNew getter

This new getter returns all entities marked as new currently in the store.

It can be used globally:

// Returns an array of mixed types with all entities
// currently marked as new in the store
let results = store.getters['entities/allNew']();

or specifically to a type:

// Returns an array User entities currently marked as new in the store
let results = store.getters['entities/users/allNew']();

resetAllDirtyFlags action

This action will run through all the entities marked as dirty in your store and set the corresponding flag to false:

store.dispatch['entities/resetAllDirtyFlags']({}, { root: true });

Plugin Options

By default, the flags are named $isDirty and $isNew.
You can override those default by setting the corresponding options at plugin initialization.

| Option name | Description | Default value | | --------------------- | ------------------------------------------------------------------- | :-----------: | | isNewFlagName | Sets the name of the isNew flag | $isNew | | isDirtyFlagName | Sets the name of the isDirty flag | $isDirty | | exposeFlagsExternally | Adds the isNew and isDirty flags to the JSON stringified output | true |

In order to use those options, you can pass them as the second parameter of the install call:

VuexORM.use(VuexORMisDirtyPlugin, {
    isNewFlagName: 'IsNew',
    isDirtyFlagName: 'IsDirty',
    exposeFlagsExternally: true
});

License

License

This project is licensed under the MIT License - see the LICENSE.md file for details