Code Monkey home page Code Monkey logo

uglyquery's Introduction

Ugly Query

Uglyquery is a request body format that adds incredible flexibility and functionality to your API. Simply setup independent functions, stick 'em in and enpoint, and then experience ugly query for yourself!

Begin with handlers

Handlers are simply functions that you want to use in your queries. They can do anything you normally do with an API: insert/query a database, read from a file, perform complex computations- whatever!

Lets setup some functions for our future queries:

// myHandlers.js
const UglyHandler = require('uglyquery').UglyHandler;

module.exports = {
  matrixStore: [
    new UglyHandler('toFile', (args) => {  }),
    new UglyHandler('fromFile', (args) => {  })
  ],
  matrixWork: [
    new UglyHandler('add', (args) => {  }),
    new UglyHandler('mult', (args) => {  })
  ]
};

Now stuff them in an endpoint

Endpoints are simply places to put a set of handlers. They are usually tied to a particular path, but that is up to you!

Let's put those functions we made in an endpoint for our express server...

// routes/index.js

Enough setup, let's start querying!

Pre/post processing

Well that was wonderful but there are some concerns. I don't want just anyone to be able to use these functions. Let's restrict the "matrix work" functions to signed-in users and the "matrix store" functions to administrators. We can pull this off with pre-processing functions which can be placed on individual endpoints or handlers.

Metadata

There is another feature that both Endpoints and Handlers have that has not been discussed: metadata. UglyQuery has specific commands that can be used to investigate default or user-defined metadata. Let's go back to our matrix-api:

// myHandlers.js
const UglyHandler = require('uglyquery').UglyHandler;

module.exports = {
  matrixStore: [
    new UglyHandler(
      'toFile',
      (args) => {  },
      {

      }),
    new UglyHandler('fromFile', (args) => {  })
  ],
  matrixWork: [
    new UglyHandler('add', (args) => {  }),
    new UglyHandler('mult', (args) => {  })
  ]
};

Now we can query that metadata with something like this...

Wonderful! We can feel free to mix metadata queries with other UglyQuery functions or our Handlers. There is currently no specification on what you can keep in your metadata- it is simply a set of key/value pairs.

Queries

Queries are HTTP POST requests with a JSON-formatted body. Here is an example:

{
  "vars": {
    "var1": 22
  },
  "body": [
    "trigger1": {
      "args": {
        "arg1": 2,
        "arg2": "Something"
      },
      "return": ["arg1", "arg2"]
    },
    "trigger2": {
      "args": {
        "arg1": "$var1"
      }
    }
  ]
}

Queries have the following format:

{
  "vars": {
    "var1": 2,
    "var2": "Something",
    "var3": {
      "more-data": "yes please"
    }
  },
  "body": [
    {
      "name": "trigger-name",
      "args": {
        "arg1": "Something",
        "arg2": 2,
        "arg3": [{ "a": "list" }, "of", 4, "things"],
        "arg4": {
          "something": "$var1",
          "somethingElse": "$var3",
          "somethingElseAgain": ["$var1", "$var2", "$var3"],
        },
      },
      "store": "some-var-name"
    },
    {
      "name": "another-or-the-same-trigger",
      ""
    }
  ]
}

Let's discuss some query features:

"cond"

Allows you

"switch"

Allows you to run based on the value of a key

"and"

Accepts a list of values and triggers, firing them until one is falsey

"or"

Accepts a list of values and triggers, firing them until one is truthy

"eq"

Accepts a list of values that must be identical to return true. Objects will be traversed- no pointer equality here people!

"neq"

Accepts a list of values and returns true if any two are not equal. Objects will be traversed- no pointer equality here people!

"not"

"gt"

"gte"

"lt"

"lte"

"map"

"reduce"

Metadata Queries

Both handlers and endpoints have metadata and configuration. It is often useful for a client to investigate those metadata and configurations in order to determine how to proceed. This functionality is provided through metadata queries.

Metadata queries, for the time being, cannot be defined as a handler like other functions can. Instead, they are a default set of queries. Here is the list:

"_getmetadata"

"_listhandlers"

"_listendpoints"

It is important to note that you can easily mix-in metadata queries with your other queries! You can even store the values you recieve so you can make "intelligent queries." Here is an example:

uglyquery's People

Contributors

aloof-ruf avatar rufism avatar

Watchers

 avatar  avatar

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.