Skip to content
This repository has been archived by the owner on Jan 26, 2020. It is now read-only.

alidcast/druid.js

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

40 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Druid.js

The "shapeshifting" API framework for Node.js+Graphql applications

Introduction

Druid streamlines the process of creating Node.js+Graphql applications.

We're able to remove all boilerplate for you, because we leverage the elegance of Graphql, which dispenses with the need for tying together disparate routes and controllers. Instead, a Druid application is composed of entities (a database model, along with its graphql typeDefs/resolvers), which Druid autoloads for you based on preconfigured paths.

We call Druid a "shapeshifting" framework, because it takes the shape of the entities that compose your API.

As an example, here's the default folder structure of a simple Druid app:

/src
  /entities # all entities to be autoloaded by Druid 
    /User
      /model.js
      /resolvers.js
      /typeDefs.gql
  app.js # your druid application

Druid itself, is primarily focused on tying together your applications entities. As for the application logic itself, we shamelessly delegate most of the heavy lifting to two great libraries: knex/objection (for querying your database) and apollo-server (for responding to Graphql requests). Before you use Druid, we suggest you become famililar with the above two technologies.

But anyway, enough talk, here's a brief look at how your API will look like going forward:

// app.js
// Setup your Druid application.

import Druid from '@druidjs/app'
import * as knex from 'knex'

const connection = knex(process.env.DATABASE)

const app = new Druid(connection)

app.listen(4000)

// src/User/model.js
// Setup a datase model for querying your databse.
// This model can be accessed inside resolvers via `ctx.db.User`.

import { Model } from 'objection'

export default class User extends Model {
  static get tableName() {
    return 'users'
  }
}

// src/User/typeDefs.gql 
// Define the entry points of your API.

type Mutation {
  createUser(username: String!, password: String!): User!
}

type User {
  id: Int! 
  username: String! 
  password: String!
}

// src/User/resolvers.js
// Resolve the entry points of your API.
// The `ctx` contains access to the model we setup above, as well as other helpers we provide for you.

export const Mutation = {
  async createUser (root, args, ctx) {
    const { username, password } = args
    const user = await ctx.db.User.query().insert({ username, password })
    const token = ctx.auth.generateToken(user.id)
    return { user, token }
  }
}

Like what you're seeing? Read the app setup section to start using Druid.

Packages

🚧 Under Construction 🚧

There are two Druid packages:

  • @druidjs/app, holds the core modules for the Druid framework. You can use this package to streamline your Node.js+Graphql API setup.
  • @druidjs/mixins, holds the model mixins for Druid. You can use package to enhance your Objection.js models with commonly used functionality.
  • @druidjs/testing, holds testing utilities for Druid. You can use package to test your API while keeping your database clean.

Each of the above packages holds its respective documentation inside its README.md.

Author

Liscense

MIT