npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@danwkennedy/arango-datasource

v0.8.0

Published

An implentation of Apollo's datasources for ArangoDb

Downloads

15

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

// 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:

// 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.

// 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.

// 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.

// 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.