moleculer-db-firebase

Firebase 'Cloud Firestore' adapter and service mixin for Moleculer DB service, written in the 'spirit' of moleculer-db and its adapters

Install

$ npm install moleculer-db-firebase --save

Usage

"use strict";

const { ServiceBroker } = require("moleculer");
const {
  dbServiceMixin,
  CloudFirestoreAdapter,
} = require("moleculer-db-firebase");

const broker = new ServiceBroker();

// Create a Firestore service for `posts` collection
broker.createService({
  name: "posts",
  mixins: [dbServiceMixin],
  adapter: new CloudFirestoreAdapter(
    "insert your 'apiKey' here",
    "insert your 'projectId' here"
  ),
  collection: "posts",
});

broker
  .start()
  .then(async () => {
    const documentCreatedWithProvidedId = await broker.call("posts.create", {
      doc: {
        _id: "customId",
        title: "a new adapter has been introduced... is it any good?",
        likes: 42,
      },
    });

    const documentCreatedWithAutoGeneratedUuid = await broker.call(
      "posts.create",
      {
        doc: {
          // _id: "customId",
          title: "nice!",
          likes: 69,
        },
      }
    );

    const documentFoundById = await broker.call("posts.get", {
      id: "customId",
    });

    const documentsFoundByConditionsWithLimit = await broker.call(
      "posts.find",
      {
        conditions: [["likes", "==", 69]],
        limit: 5,
      }
    );

    const updatedDocuemnt = await broker.call("posts.update", {
      id: "customId",
      values: {
        title:
          "a new adapter has been introduced... is it any good? the answer is 'true'",
      },
    });

    const deletedDocument = await broker.call("posts.delete", {
      id: "customId",
    });
  })
  .catch((err) => console.log("err", err));

License

The project is available under the MIT license.