# @danwkennedy/arango-datasource

> An implentation of Apollo's datasources for ArangoDb

Latest version **0.8.0** (published 2020-09-17) · MIT license · 0 weekly downloads

## Install

```sh
npm install @danwkennedy/arango-datasource
pnpm add @danwkennedy/arango-datasource
yarn add @danwkennedy/arango-datasource
bun add @danwkennedy/arango-datasource
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.8.0 |
| Published | 2020-09-17 |
| First published | 2019-05-14 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 4 |
| Unpacked size | 23.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 9 |
| Author | Daniel Kennedy |
| Maintainers | danwkennedy |
| Keywords | arangodb, apollo, graphql, datasource |

## Links

- npm: https://www.npmjs.com/package/@danwkennedy/arango-datasource
- Repository: https://github.com/danwkennedy/arango-datasouce
- Homepage: https://github.com/danwkennedy/arango-datasouce#readme
- Issues: https://github.com/danwkennedy/arango-datasouce/issues
- npm.io page: https://npm.io/package/@danwkennedy/arango-datasource

## Dependencies (4)

- [arangojs](https://npm.io/package/arangojs.md) ^7.0.1
- [dataloader](https://npm.io/package/dataloader.md) ^2.0.0
- [object-hash](https://npm.io/package/object-hash.md) ^2.0.3
- [apollo-datasource](https://npm.io/package/apollo-datasource.md) ^0.7.0

## Alternatives

- [apollo-link-http-common](https://npm.io/package/apollo-link-http-common.md) — 879.0K weekly downloads
- [react-relay](https://npm.io/package/react-relay.md) — 336.8K weekly downloads
- [relay-test-utils](https://npm.io/package/relay-test-utils.md) — 181.6K weekly downloads
- [@vendure/core](https://npm.io/package/@vendure/core.md) — 14.8K weekly downloads
- [@pnpm/deps.graph-sequencer](https://npm.io/package/@pnpm/deps.graph-sequencer.md) — 13.4K weekly downloads

## Recent versions

- 0.8.0 (latest) — 2020-09-17
- 0.7.0 — 2020-05-17
- 0.6.4 — 2019-08-28
- 0.6.2 — 2019-07-19
- 0.6.1 — 2019-07-02
- 0.6.0 — 2019-07-02
- 0.5.0 — 2019-06-14
- 0.4.0 — 2019-06-02
- 0.3.0 — 2019-05-29
- 0.2.1 — 2019-05-27
- 0.2.0 — 2019-05-14

## README

# arango-datasouce
An implementation of Apollo's datasource for ArangoDb

## Installation

```
$ npm install arango-datasource
```

## DataSources

### ArangoDataSource

A "general purpose" datasource that's mainly suitable for querying the database using AQL.

Requires passing the target database instance from `arango-js`

```js
// index.js

const { ArangoDataSource } = require('@danwkennedy/arango-datasource');
const { Database } = require('arango-js');

// initialize the db
const database = new Database('http://my.database.url');

// initialize the server
const server = new ApolloServer({
  typeDefs,
  resolvers,
  cache,
  context,
  dataSources: () => ({
    arangoDataSource: new ArangoDataSource(database)
  })
});
```

Extend this class to create more targetted DataSources according to your needs:

```js
// UserDataSource.js

const { ArangoDataSource } = require('@danwkennedy/arango-datasource');

module.exports = class UserDataSource extends ArangoDataSource {

  // Pass the user collection to the DataSource
  constructor(db, collection) {
    super(db);
    this.collection = collection;
  }

  // Build the query and call super.query
  async getUsers() {
    const query = aql`
      FOR user in ${this.collection}
      return user
    `;

    return await this.query(query);
  }
}
```

Basic query caching is available. Cache keys for queries are simply the query object's hash value using `object-hash`. This type of caching is mainly useful when using a persisted cache across machines (i.e. Redis instead of the default in memory cache) and works best for fetching common data that doesn't change very often.

### ArangoDocumentDataSource

Uses the [DataLoader]() class to add batching and caching to fetching Arango Documents by their Id. This is especially useful as a `NodeDataSource`  as ArangoDb's default Id structure prepends the collection name to the `_key` making it so you don't need to pass the target collection the document datasource.

```js
// index.js

const { ArangoDocumentDataSource } = require('@danwkennedy/arango-datasource');
const { Database } = require('arango-js');

// initialize the db
const database = new Database('http://my.database.url');

// initialize the server
const server = new ApolloServer({
  typeDefs,
  resolvers,
  cache,
  context,
  dataSources: () => ({
    NodeDataSource: new ArangoDocumentDataSource(database)
  })
});

// node/resolver.js

module.exports = {
  Query: {
    node: async (_, { id }, { dataSources }) => {
      return dataSources.NodeDataSource.load(id);
    },
  },
}

```

## Managers

DataSources fetch records from the database. Managers create/update/delete records from the database.

### DocumentManager

Manages the lifecylc of documents in a document collection.

```js
// index.js

const { DocumentManager } = require('@danwkennedy/arango-datasource');
const { Database } = require('arango-js');

// initialize the db
const database = new Database('http://my.database.url');
const userCollection = database.collection('users');

// initialize the server
const server = new ApolloServer({
  typeDefs,
  resolvers,
  cache,
  context,
  dataSources: () => ({
    userDocumentManager: new DocumentManager(userCollection)
  })
});
```

### EdgeManager

Manages the lifecycle of edges in a graph.

```js
// index.js

const { EdgeManager } = require('@danwkennedy/arango-datasource');
const { Database } = require('arango-js');

// initialize the db
const database = new Database('http://my.database.url');
const userFavoriteFoodCollection = database.edgeCollection('user_favorite_food');

// initialize the server
const server = new ApolloServer({
  typeDefs,
  resolvers,
  cache,
  context,
  dataSources: () => ({
    userDocumentManager: new EdgeManager(userFavoriteFoodCollection)
  })
});
```

The main difference between the `EdgeManager` and the `DocumentManager` is the `EdgeManager` requires the `_to` and `_from` ids be passed. It also smooths over some difficulties with removing edges where we might not know the edge's id. In that case, we can pass the `_from` and `_to` ids and the manager will do the query to find the correct edge.

---
_Source: https://npm.io/package/@danwkennedy/arango-datasource · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
