Giter Club home page Giter Club logo

vuex-mapper's Introduction

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.

vuex-mapper's People

Contributors

bdiz avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.