Search code examples
arangodbyeoman-generatorfeathersjs

how to set up arangodb with feather generate custom service (generator-feathers)


I've set up a server with feathers-cli

feathers geneate app

Since there's no default service for Arangodb available I did:

feathers generate service
> A custom service

I'm searching for examples/docs now, on how to link custom service calls like create, find, get etc. to arangodb or any db for that matter.


Solution

  • The documentation on how to create your own services can be found in the services API documentation.

    An ArangoDB service that stores data in a collection just looks like this:

    class Service {
      constructor(collection) {
        this.collection = collection;
      }
    
      create(data) {
        return this.collection.save(data);
      }
    }
    

    And you would implement similar functionality for find, update, patch and remove.

    There are now different ways to initialize the database connection and the service. For some patterns you can refer to how the generator sets up MongoDB or Mongoose, for ArangoDB it probably looks something like this:

    const { Database } = require('arangojs');
    
    app.set('arangodb', new Database('http://127.0.0.1:8529'));
    
    db.createDatabase('mydb').then(
      () => {
        db.useDatabase('mydb');
        // Initialize the service here
        app.use('/myservice', new Service(db.collection('people')));
      },
      err => console.error('Failed to create database:', err)
    );
    

    The code of existing adapters, e.g. feathers-memory or one of the many other database adapters can also be a good reference.