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-mapper

v1.0.0-alpha.2

Published

ODM for Vuex and Firestore

Downloads

3

Readme

vuex-mapper alpha

This repo contains an ODM for Vuex and Firestore. Inspired by Mongoid. It uses dynamic modules and vuexfire for binding snapshots to Vuex state.

The idea is to make it easy to create objects which map to your Vuex store.

This package is currently supported by webpack only due to how it auto-requires modules using require.context().

Getting started

npm install --save vuex-mapper

I'll try to get a compilable example available in the repo, but for now you an attempt to get your app going by following along here.

// ./src/firestore.js
import firebase from "firebase";
import "firebase/firestore";

firebase.initializeApp({
  apiKey: process.env.VUE_APP_FIRESTORE_API_KEY,
  authDomain: process.env.VUE_APP_FIRESTORE_AUTH_DOMAIN,
  databaseURL: process.env.VUE_APP_FIRESTORE_DATABASE_URL,
  projectId: process.env.VUE_APP_FIRESTORE_PROJECT_ID
});

const firestore = firebase.firestore();
firestore.settings({ timestampsInSnapshots: true });

export default firestore;
// ./src/vuex-mapper-instance.js
import VuexMapper from "vuex-mapper";

const requireModule = require.context('./models/', true);
const currentTime = function() {
  return firebase.firestore.FieldValue.serverTimestamp();
};

const vuexMapper = new VuexMapper(requireModule, { currentTime });
export default vuexMapper;
// ./src/store/index.js
import Vue from "vue";
import Vuex from "vuex";
import VuexMapper from "vuex-mapper";
import vuexMapper from "../vuex-mapper";

Vue.use(Vuex);
const store = new Vuex.Store({

  strict: process.env.NODE_ENV !== "production",

  state: () => {
    return {
      user: null,
      openTransactions: 0
    };
  },

  getters: {
    store: function() {
      return store;
    },
    user: state => state.user,
    saving: state => !!state.openTransactions,
    isAuthenticated: state => !!state.user
  },

  mutations: {
    ...VuexMapper.firebaseMutations,
    setUser: function(state, { isAnonymous, uid }) {
      state.user = { isAnonymous, uid };
    },
    startTransaction: function(state) {
      state.openTransactions++;
    },
    endTransaction: function(state) {
      state.openTransactions--;
    }
  },

  actions: {
    saveTransaction: function(context, promise) {
      context.commit("startTransaction");
      return promise
        .then((ret) => {
          context.commit("endTransaction");
          return ret;
        });
    }
  }

});

export default store;
// ./src/models/category.js
import Vue from "vue";
import firestore from "../firestore";
import store from "../store/index";
import vuexMapper from "../vuex-mapper-instance";

const Category = Vue.extend({
  mixins: [
    vuexMapper.documentMixin("category", {
      fields: ["text"],
      embedsMany: ["todos"]
    })
  ]
});

// Only need class methods for top-level collections (not subcollections)
vuexMapper.documentClassMethods({
  store,
  db,
  klass: Category,
  collectionName: "categories"
});

export default Category;
// ./src/models/todo.js
import Vue from "vue";
import vuexMapper from "../vuex-mapper-instance";

export default Vue.extend({
  mixins: [
    vuexMapper.documentMixin("todo", {
      fields: ["text"],
      embeddedIn: "category",
      embedsMany: ["subTasks"]
    })
  ]
});
// ./src/models/sub-task.js
import Vue from "vue";
import vuexMapper from "../vuex-mapper-instance";

export default Vue.extend({
  mixins: [
    vuexMapper.documentMixin("subTask", {
      fields: ["text"],
      embeddedIn: "todo"
    })  
  ]
});

Authenticate and call store.setUser({uid: firestoreAuthId}). For top-level collection you must call Category.setCollectionRef() in a created() hook of a component to allow vuexfire to update Vuex state.

Finally the fun happen. Now you can get at category state with Category.all. You can add categories with Category.create({text: newCategoryText}). Create a new Category model instance with:

let category = new Category({
  propsData: {
    store: this.$store,
    id: id,
    docRef: this.categoriesRef.doc(id)
  }
});

And with a category instance you can call category.text, category.update({text: updatedCategoryText}), category.destroy(), category.todos and let todo = category.todos.create({text: newTodoText}). With the todo instance you can similarly call methods and computed properties to get at subTasks.

I hope thats enough to get you started. I know its a bit of a mess right now.

TODO

Too many things to list.

At a high level I want to support all associations Mongoid does.

I'd also like this to be a Vuex plugin with less setup.

This library is incomplete and very alpha. PRs welcome.

License

MIT.