janober / loopback-connector-couchbase-expert

Couchbase Connector for Strongloop Loopback
MIT License
2 stars 0 forks source link

loopback-connector-couchbase without N1QL

The Couchbase Connector module for for loopback-datasource-juggler.

This connector does not use N1QL. That means you can not simply lookup data you do not have view setup and defined in the configuration. So you have to be very careful which data you query (that is why I put the expert in the name). If you for example have a relationship setup and you want to use it to query related data it will fail till you create * setup a view which allows it. Actually querying data will probably almost always fail if you do not use "findById". So you have to write your application a very specific way for it to work but the price for that effort is high performance. So for the most people using the N1QL implementation (from which I did fork) is probably better: https://github.com/guardly/loopback-connector-couchbase

Btw. is also not really well tested. Did just fork to work best for my own needs of high performance (and no random query need).

What is Couchbase Server?

Couchbase Server is a NoSQL document database for interactive web applications. It has a flexible data model, is easily scalable, provides consistent high performance and is "always-on", meaning it can serve application data 24 hours, 7 days a week.

Getting Started

Before installing the connector module, make sure you've taken the appropriate steps to install and configure Couchbase Server.

Installing the connector

npm install couchbase

npm install loopback-connector-couchbase-expert

Connector settings

The connector can be configured using the following settings from the data source.

Example Configuration

  {
    "couchbase": {
      "host": "localhost",
      "port": "8091",
      "database": "myBucket",
      "name": "couchbase-expert",
      "user": "my-user",
      "password": "my-password",
      "connector": "couchbase-expert",
      "mappings": {
        "user": {
          "email": "u::"
        }
      },
      "views": {
        "Orders": {
          "userId": {
            "designDocument": "loopback",
            "viewName": "orders_userid"
          }
        }
      }
    }
  }

Everything should be clear the only interesting parts are mappings & views.

  function (doc, meta) {
    if (doc.docType && doc.docType == "Order" && doc.userId) {
       emit(doc.userId, null);
    }
  }

Model definition for Couchbase Documents

NOTE: By default, all Couchbase documents will be created with a docType property which matches the model name. To set a Document Key yourself, simply pass in an id value in the data and the connector will use this id as the id property in the Document as well as for the Document Key itself. If no id is passed in, the connector will auto-generate a UUID to be used as the id property and Document Key. These nuances are a work in progress and we would be interested in hearing other developer's use-cases so that we can improve the connector to be as flexible as possible.

The model definition consists of the following properties:


  {
  "name": "Brewery",
  "base": "PersistedModel",
  "properties": {
    "id": {
      "type": "String",
      "id": true,
      "required": true,
      "index": true
    },
    "propertyOne": {
      "type": "String",
      "required": false
    },
    "propertyTwo": {
      "type": "Object",
      "required": false
    }
  },
  "validations": [],
  "relations": {
    "actions": {
      "type": "hasMany",
      "model": "Beer",
      "foreignKey": "breweryId"
    }
  },
  "acls": [],
  "methods": []
}

Working with data

Please refer to the official LoopBack Documentation for how to work with models and data.

More to come!!!