Giter Site home page Giter Site logo

moleculer-adapter-feathers's Introduction

moleculer-adapter-feathers

Moleculer adapter to import feathers services. That includes:

  • MongoDB
  • Blob store
  • Bookshelf
  • CouchDB
  • Elasticsearch
  • Knex
  • Mongoose
  • NeDB
  • RethinkDB
  • Sequalize
  • Waterline
  • and many others

Install

$ npm install moleculer-adapter-feathers --save

Usage (example with Knex)

const Feathers = require("moleculer-adapter-feathers");
const { ServiceBroker } = require("moleculer");
const feathersKnex = require("feathers-knex");
const knex = require("knex");

const broker = new ServiceBroker();

// Create a DB service via knex for `user` entities
broker.createService({
    name: "users",
    mixins: [Feathers],
    settings: {
        feathers: {
            adapter: feathersKnex,
            options: {
                name: "users",
                Model: new knex({
                    client: "pg",
                    connection: { ... },
                }),
            },
        },
    },
});

broker.start()
// Create a new user
.then(() => broker.call("users.create", {
    username: "john",
    email: "[email protected]",
}))

// Get all users
.then(() => broker.call("users.find").then(console.log));

Settings

Property Type Default Description
adapter `Object Function` required
hooks Object {} Object containing before and after hooks.
options Object {} Options passed to Feathers service adapter.

Hooks

Hooks work just as they do in Feathers. They are passed down to a service in settings.feathers.hooks.

users.service.js

module.exports = {
    ...
    settings: {
        feathers: {
            adapter: feathersKnex,
            hooks: require('./hooks'),
            options: {
                name: "users",
                Model: new knex({
                    client: "pg",
                    connection: { ... },
                }),
            },
        },
    },
    ...
}

hooks.js

module.exports = {
    before: {
        create: [
            hook => {
                console.log('create hook')
                return hook
            },
        ],
        find: [],
        get: [],
        update: [],
        patch: [],
        remove: [],
    },
    after: {
        create: [],
        find: [],
        get: [],
        update: [],
        patch: [],
        remove: [],
    },
}

Actions

Standard Feathers actions are exposed: create, get, find, update, patch, remove with all the standard Feathers parameters. Actions can be overwritten.

Methods

Feathers service methods can be accessed directly via this.create, this.find and etc.

create

Create an object in a service.

Parameters

Property Type Default Description
* Any {} Object to be created.

Results

Type: Object

Created object (or any other service response).

find

Find objects by a provided query, if any.

Parameters

Property Type Default Description
* Any {} Query specified by the service.

Results

Type: Array[Object]

Array of results.

get

Get object in the service by a provided (unique) ID.

Parameters

Property Type Default Description
id `String Number` required

Results

Type: Object

Object found by the ID.

patch

Changes the properties of an object.

Parameters

Property Type Default Description
id `String Number` required
* Any {} Values to be patched.

Results

Type: Object

Object patched.

update

Overwrites an object's properties.

Parameters

Property Type Default Description
id `String Number` required
* Any {} Rest of the object.

Results

Type: Object

Object updated.

remove

Remove object by ID.

Parameters

Property Type Default Description
id `String Number` required

Results

Type: Object

Object removed.

moleculer-adapter-feathers's People

Contributors

zygos avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

ansont xqk

moleculer-adapter-feathers's Issues

Problem in hooks

hi ! I want to use the all hooks "before, after and error".
But I read the documentation and only mentions "before and after".
When I declare the hook for "error", it's breaks and shows a message of "registeredService [hooksType]".

I'm using "feathers-knex": "^5.0.5",

This is the mensaje of error:

 registeredService[hooksType] is not a function TypeError: registeredService[hooksType] is not a function
    at Object.register (/app/node_modules/moleculer-adapter-feathers/src/services.js:32:35)
    at Service.created (/app/node_modules/moleculer-adapter-feathers/src/index.js:94:30)
    at schema.created.forEach.fn (/app/node_modules/moleculer/src/service.js:174:41)
    at Array.forEach (<anonymous>)
    at Service._init (/app/node_modules/moleculer/src/service.js:174:24)
    at Service.parseServiceSchema (/app/node_modules/moleculer/src/service.js:148:8)
    at new Service (/app/node_modules/moleculer/src/service.js:57:9)
    at ServiceBroker.createService (/app/node_modules/moleculer/src/service-broker.js:636:14)
    at ServiceBroker.loadService (/app/node_modules/moleculer/src/service-broker.js:563:15)
    at _.uniq.forEach.f (/app/node_modules/moleculer/bin/moleculer-runner.js:352:44)
    at Array.forEach (<anonymous>)
    at loadServices (/app/node_modules/moleculer/bin/moleculer-runner.js:352:24)
    at startBroker (/app/node_modules/moleculer/bin/moleculer-runner.js:422:2)
    at tryCatcher (/app/node_modules/bluebird/js/release/util.js:16:23)
    at Promise._settlePromiseFromHandler (/app/node_modules/bluebird/js/release/promise.

I would appreciate help.

hooks.js

module.exports = {
    before: {
        all: [],
        create: [],
        find: [],
        get: [],
        update: [],
        patch: [],
        remove: [],
    },
    after: {
        all: [],
        create: [],
        find: [],
        get: [],
        update: [],
        patch: [],
        remove: [],
    },
    error: {
        all: [],
        find: [],
        get: [],
        create: [],
        update: [],
        patch: [],
        remove: []
    }
}

service.js

const Feathers = require("moleculer-adapter-feathers");
const feathersKnex = require("feathers-knex");
const knex = require("knex");
const databaseConf = require("../../config/database.config.json");

module.exports = {
    name: "myservicedb",
    mixins: [Feathers],
    settings: {
        feathers: {
            adapter: feathersKnex,
            hooks: require('./hooks'),
            options: {
                name: "myTable",
                id: 'myId',
                paginate: databaseConf.paginate,
                Model: new knex({
                    client: 'oracledb',
                    connection: {
                        user: databaseConf.usermame,
                        password: databaseConf.password,
                        host: databaseConf.host,
                        database: databaseConf.database
                    },
                    useNullAsDefault: true,
                    pool: {
                        min: databaseConf.poolMin,
                        max: databaseConf.poolMax
                    },
                    acquireConnectionTimeout: databaseConf.timeout
                }),
            },
        },
    },
    dependencies: [],
    actions: {},
    events: {},
    methods: {},
    created() {},
    started() {},
    stopped() {
    }
};

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.