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

vuexorm-softdelete-plugin

v1.1.0

Published

Vuex ORM plugin adding soft delete capability. Based on an original idea from Conan Crawford.

Downloads

32

Readme

License

This is a plugin for the Vuex-ORM library.

Installation

Simply reference the github project in your package.json

dependencies: {
    ...
    "vuexorm-softdelete-plugin": "git+https://github.com/tvillaren/vuexorm-softdelete-plugin.git"
    ...
}

and run npm install.

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

import VuexORMSoftDeletePlugin from 'vuexorm-softdelete-plugin';

and then

VuexORM.use(VuexORMSoftDeletePlugin);

Usage

The plugin adds a softDelete action / mutation that has the same interface as Vuex ORM Delete method.

Soft-deleting

By Primary Key Value

You can soft-delete by passing a PK directly or through a where condition:

// Initial state.
let state = {
    entities: {
        users: {
            '1': { id: 1, name: 'John' },
            '2': { id: 1, name: 'Jane' }
        }
    }
};

// Delete single data by primary key value with model class.
User.softDelete(1);

// Or you can pass object as argument as well.
User.softDelete({ where: 1 });

// Or you can delete data from an existing model instance.
const user = await User.find(1)
user.softDelete()

// Or you can delete single data by primary key value with vuex action.
store.dispatch('entities/users/softDelete', 1)

// Or you can pass obejct as argument as well.
store.dispatch('entities/users/softDelete', { where: 1 })

// State after `delete`
state = {
  entities: {
    users: {
      '1': { id: 1, name: 'John', $isDeleted: true, deleted_at: ... /* JS Date of deletion */ },
      '2': { id: 1, name: 'Jane' }
    }
  }
}

By Data Closure

You can soft-delete by passing a condition on the record:

// Initial state.
let state = {
    entities: {
        users: {
            '1': { id: 1, name: 'John' },
            '2': { id: 1, name: 'Jane' },
            '3': { id: 1, name: 'George' }
        }
    }
};

// Delete data by closure.
User.softDelete(record => {
    return record.id === 1 || record.name === 'Jane';
});

// Or with object style.
User.softDelete({
    where(record) {
        return record.id === 1 || record.name === 'Jane';
    }
});

// State after `delete`.
state = {
    entities: {
        users: {
            '1': { id: 1, name: 'John', $isDeleted: true, deleted_at: ... /* JS Date of deletion */ },
            '2': { id: 1, name: 'Jane', $isDeleted: true, deleted_at: ... /* JS Date of deletion */ },
            '3': { id: 1, name: 'George' }
        }
    }
};

$isDeleted flag and deleted_at key

As you can see on the examples above, soft-deleted entities are marked with a $isDeleted flag. Additionnally, the date of soft-deletion is stored in the deleted_at attribute.
Both can be custom-named through the plugin options.

Displaying soft-deleted data

Soft-deleted entities are still in the store but will not appear on queries unless you specifically ask to see trashed data:

allTrashed getter

This new getter returns all soft-deleted entities in the store.

It can be used globally:

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

or specifically to a type:

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

Query modifiers: withTrashed() and trashed()

When building a Vuex ORM query, soft-deleted entities will be hidden from the result by default.

  • withTrashed() modifier
    Shows all entities, wether they are soft-deleted or not
const users = User.query()
    .withTrashed()
    .get(); // Returns all User data in the store
  • trashed() modifier Shows only soft-deleted entities
const users = User.query()
    .trashed()
    .get(); // Returns all soft-deleted User data in the store

Plugin Options

You can override the default flag & key names by setting the corresponding options at plugin initialization.

| Option name | Description | Default value | | --------------------- | --------------------------------------------- | :-----------: | | flagName | Sets the name of the isDeleted flag | $isDeleted | | key | Sets the name of the deleted_at key | deleted_at | | exposeFlagsExternally | Adds the 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(VuexORMSoftDeletePlugin, {
    flagName: 'IsMarkedForDeletion',
    key: 'date_of_deletion',
    exposeFlagsExternally: true
});

License

License

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