Skip to content
Branch: master
Find file History
Fetching latest commit…
Cannot retrieve the latest commit at this time.

Files

Permalink
Type Name Latest commit message Commit time
..
Failed to load latest commit information.
example
src
tests
.gitignore
@resource.json
README.md
package.json

README.md

@deepr/runtime

Simple Deepr runtime.

Installation

npm install @deepr/runtime

Example

import {invokeQuery} from '@deepr/runtime';

(async () => {
  // Given the following "root" object:
  const root = {
    async getMovie(id) {
      // Let's pretend we are reading a record from a database
      if (id === 'abc123') {
        return {
          title: 'Inception',
          year: 2010,
          actors: [
            {fullName: 'Leonardo DiCaprio', popularity: 90},
            {fullName: 'Joseph Gordon-Levitt', popularity: 70}
          ]
        };
      }
      throw new Error('Movie not found');
    }
  };

  // Invoking the following query:
  await invokeQuery(root, {
    'getMovie=>movie': {
      '()': ['abc123'],
      '=>': {
        title: true,
        actors: [{fullName: true}]
      }
    }
  });

  // Will return:
  // {
  //   movie: {
  //     title: 'Inception',
  //     actors: [{fullName: 'Leonardo DiCaprio'}, {fullName: 'Joseph Gordon-Levitt'}]
  //   }
  // }
})();

API

invokeQuery(root, query, [options]) => result

Invoke the specified query on root, and return the result of the invocation. If a promise is encountered during the execution, then a promise that resolves with the result is returned.

Example:

const result = await invokeQuery(root, {
  movies: [
    {
      title: true,
      year: true
    }
  ]
});

root

An object from which the query will be evaluated.

Example:

{
  "movies": [
    {
      "title": "Inception",
      "year": 2010
    },
    {
      "title": "The Matrix",
      "year": 1999
    }
  ]
}

query

A Deepr query.

Example:

{
  "movies": [
    {
      "title": true,
      "year": true
    }
  ]
}

Learn more about Deepr queries here: https://github.com/deeprjs/deepr.

options

An optional object of options.

context

A context that will be passed as the last parameter to all invoked methods.

ignoreKeys

A key or an array of keys to be ignored when executing the query. A key can be specified as a string or a RegExp.

Examples:

  • Using the string 'password' will ignore every key named 'password'.
  • Using the RegExp /^_/ will ignore every key starting with an underscore.
acceptKeys

A key or an array of keys to be accepted regardless if they are ignored using the ignoreKeys option. A key can be specified as a string or a RegExp.

Example:

  • Specifying the string '_id' will accept this key even if the ignoreKeys option includes the RegExp /^_/.
ignoreBuiltInKeys (default: true)

If true (the default), all JavaScript built-in keys will be ignored. This includes object and function built-in keys such as constructor, prototype, apply, caller, __proto__, hasOwnProperty, etc. Even if they are built-in, the keys name and length are considered safe, and therefore accepted.

For obvious security reasons, it is strongly discouraged to disable this option.

authorizer(key, operation) => boolean

A function that is called for each key to authorize any operation.

The function receives a key and an operation which can be either 'get' for reading an attribute or 'call' for invoking a method.

The function must return true to authorize an operation. If false is returned, the evaluation of the query stops immediately, and an error is thrown.

The function can be either synchronous or asynchronous (using async or returning a promise).

Finally, the value of this in the function is the current node of the query being evaluated.

Example:

function authorizer(key, operation) {
  if (key === 'title' && operation === 'get') {
    return true; // Authorize getting the 'title' attribute
  }
  if (key === 'get' && operation === 'call') {
    return true; // Authorize invoking the get() method
  }
  return false; // Decline everything else
}

Contribute

This project uses Run to manage the development environment.

Build the transpiled files:

run . @build

Run the test suite:

run . @test

Run the example:

node ./packages/runtime/example

License

MIT

You can’t perform that action at this time.