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

@wgr-sa/vuex-orm-crud

v0.0.11

Published

vuex-orm plugin for RESTful CRUD backends

Downloads

15

Readme

Vuex-ORM brings Object-Relational Mapping to the Vuex Store. @wgr-sa/vuex-orm-crud lets you communicate with RESTful backends.

The plugin extends the basic model of Vuex-ORM with some helful functions to make CRUD operations such as save, get, paginate, update, delete.

You no longer need to access your http client manually. All the communication happens thru the enhanced Vuex-ORM models.

Doumentation

Find here the progressing documentation in vuepress:

Dependencies

npm i vuex
npm i @vuex-orm/core
npm i axios

Installation

npm i @wgr-sa/vuex-orm-crud

The following example installs the plugin using axios and registers two models to play with.

Example

note: CommonJS usage

import Vue from 'vue'
import Vuex from 'vuex'
import VuexORM from '@vuex-orm/core'
import VuexORMCRUD from '@wgr-sa/vuex-orm-crud'
import axios from 'axios'

// models ( check vuex doc )
import Page from '../models/Page'
import Section from '../models/Section'

// DB
const database = new VuexORM.Database()
database.register(Page)
database.register(Section)

// Vuex ORM
const client = axios.create({ baseURL: '/api' })
VuexORM.use(VuexORMCRUD, { client })

Vue.use(Vuex)
export default new Vuex.Store({
  plugins: [VuexORM.install(database)],
});

note: vuex-orm models

// models/Page.js
import { Model } from '@vuex-orm/core'
import Section from './Section'

export default class Page extends Model
{
  static entity = 'pages'

  static fields ()
  {
    return {
      id: this.attr(null).nullable(),
      title: this.attr(null),

      sections: this.hasMany(Section, 'page_id'),
    }
  }
}

// models/Section.js
import { Model } from '@vuex-orm/core'
import Page from './Page'

export default class Section extends Model
{
  static entity = 'sections'

  static fields ()
  {
    return {
      id: this.attr(null).nullable(),
      page_id: this.attr(null),
      name: this.attr(null),

      page: this.belongsTo(Page, 'page_id'),
    }
  }
}

note: Vue Component

<template lang="html">
  <div class="">

    <h1>{{page.title}}</h1>

    <ul>
      <li v-for="s in page.sections">{{s.name}}</li>
    </ul>

  </div>
</template>

<script>
import Page from '@/models/Page'
import Section from '@/models/Section'

export default
{
  props:
  {
    pageId: Number
  },
  computed:
  {
    page()
    {
      return Page.query().with('sections').where(this.pageId).first()
    }
  },
  created()
  {
    // load some pages
    Page.crud().get()
    .then(this.readyToGo)
  },
  methods:
  {
    readyToGo()
    {
      console.log(this.page) // page is ready & store is full : )
    }
  }
}
</script>

CRUD API

Static class methods

Every model have a crud service

- Model.crud()

this returns the service holding settings and client...

Instance methods

- (async) model.delete(path = null, keys = null, conf = null)

- (async) model.save(path = null, keys = null, conf = null)

- (async) model.update(path = null, keys = null, conf = null)

this.section.update(
  null, // if null, the path is guested by _.kebabCase(section.entity) / section.$id
  ['order'], // we update only order prop
  [relations:[this.page]] // config.relations: here we add a path prefix with this.page ( = pages/$id )
)

...so here a PUT Method will be performed as follow:

Request URL: http://api/pages/1/sections/1
Request Method: PUT
Request Payload: {"order":1}

- model.pickKeys(keys | null)

NOTE every instance can save/update/delete itself

Service methods

- (async) Model.crud().get(path = null, config = null)

returns records and stores the get query with conf.persistOptions as vuex-orm

Section.crud().get(

  // path to your api crud model's endpoint
  null, // here path is guested by entity model's prop

  // conf object
  [
    // client
    client: null,

    // all axios settings are allowed
    onDownloadProgress: function (progressEvent) {
      // Do whatever you want with the native progress event
    },

    // model instance to prefix path calls (
    relations: [this.page], // here pages/id

    dataKey: 'data',
    paginationKey: 'pagination',
    dataTransformer: null,
    filter: null,

    // vuex-orm save strategies
    save: true,
    persistBy: 'insertOrUpdate',
    persistOptions: null,
  ]
)

...so here a GET Method will be performed as follow:

Request URL: http://api/pages/1/sections
Request Method: GET

- (async) Model.crud().getOne(id, config = null)

fetches model/id record...

- (async) Model.crud().paginate(path = null, config = null, reset = false)

handles pagination for you through some automated settings

{
  params: {page: this.page, limit: this.limit}, // axios query params ?page=&limt=
  persistBy: 'create' // for the vuex-orm store behaviour
}

- (async) Model.crud().goTo(page)

returns paginate methods result

Service properties

- Model.crud().config

- Model.crud().client

- (readonly) Model.crud().pagination

{
  count: 1,
  current_page: 1,
  has_next_page: false,
  has_prev_page: false,
  limit: null,
  page_count: 1,
}

- (readonly) Model.crud().paginator

{
  page: 1,
  limit: 1,
  path: '',
  config: {}
}

- Model.crud().page (int)

- Model.crud().limit (int)

Configure all your calls

when performing a get/getOne/save/upadte/delete call you can pass a config object

{
  // client
  client: null,

  // all axios settings are allowed
  onDownloadProgress: function (progressEvent) {
    // Do whatever you want with the native progress event
  },

  // model instance to prefix path calls (
  relations: [this.page], // here pages/id

  dataKey: 'data',
  paginationKey: 'pagination',
  dataTransformer: null, // function (response) return [] | {} | null #!if set: bypass the dataKey!
  filter: null, // Array.filter fct

  // vuex-orm save strategies
  save: true,
  persistBy: 'insertOrUpdate', // GET ONLY
  persistOptions: null, // GET ONLY
}

TODO

  • doc improvements + vue press 🙏
  • tests
  • add skills
  • and more