Options
All
  • Public
  • Public/Protected
  • All
Menu
     

Class QueryDB

A request-method configurator for RouteDB requests-paths.

Hierarchy

Implements

Index

Constructors

constructor

  • Creates a new QueryGN method for a RouteDB request that utilizes a custom data retrieval.

    Parameters

    • __namedParameters: Method
      • Scheme: String
      • Merge: false | true | "GET" | "POST" | "PUT" | "DELETE" | "MIDDLEWARE"
      • Limits: String[]
      • Doc: Doc
      • Query: function
      • Params: CLParams
      • Parse: Parse
      • Key: String
      • Private: boolean

    Returns QueryDB

  • Creates a new QueryGN method for a RouteDB request that utilizes a SQL query.

    Parameters

    • __namedParameters: Method
      • Scheme: String
      • Merge: false | true | "GET" | "POST" | "PUT" | "DELETE" | "MIDDLEWARE"
      • Limits: String[]
      • Doc: Doc
      • Query: string[]
      • Params: CLParams
      • Parse: Parse
      • Key: String
      • Private: boolean

    Returns QueryDB

Properties

Scheme

Scheme: String

A RegexP, string-representation of the Route Path.

Merge

Merge: boolean | TPRouteMerge<CBRouteDB>

A flag the specifies whether or not, and if so, how to prepend the path-parameters of the last parent-Endpoint referenced in Sub (along with the merge of its own parent; if applicable) to the path-name/parameters of this Endpoint:

  • If true, the Scheme from the parent-Endpoint will be prepended to this Scheme of this Endpoint.
  • If set as an HMETHOD, then the system will use the Scheme defined in the parent-Endpoint's CBRouteAU/CBRouteDB definition of the same HMETHOD. Otherwise, it choose the first Scheme found within each parent.
  • Defined in a method (see: Uses below), and set to true, then the system will look for that method deinition within the parent and choose that Scheme. Otherwise, the first Scheme found is chosen.

Example:

({
  User: {
    Actions: {
      Password: new RouteDB({
        Merge: true,
        Subs: ['settings','privacy'], // Child of "Settings"
        GET: () => ({ ... }), // Merge to "Privacy.GET"
        // ^ Resolves to "/user/:uid/settings/privacy/:prv_id/password"
      }),
      Email: new RouteDB({
        Merge: 'POST', // Merge to "Privacy.POST"
        Subs: ['settings','privacy'], // Child of "Settings"
        GET: () => ({ ... }),
        // ^ Resolves to "/user/:uid/settings/privacy/email"
      }),
      Privacy: new RouteDB({
        Scheme: '/:prv_id/', // Defult for any Method defined within
        Subs: ['settings'], // Child of "Settings".
        Merge: true,
        GET: () => ({ ... }),
        // ^ Merge to "Settings.GET",
        // ^ Resolves to "/user/:uid/settings/privacy/:prv_id"
        POST: () => ({ Scheme: '/' })
        // ^ Merge to "Settings.GET", Scheme redefined to "/"
        // ^ Resolves to "/user/:uid/settings/privacy"
      }),
      Settings: new RouteDB({
        Merge: true,
        GET: () => ({ ... }),
        // ^ Sub not defined, is child of "/"
        // ^ Resolves to "/user/:uid/settings"
      }),
      "/": new RouteDB({
        Scheme: '/:uid/',
        GET: () => ({ ... }),
        // ^ Resolves to "/user/:uid"
      }),
    }
  }
});

Uses:

Limits

Limits: String[]

A list of Limits to subscribe to.

Doc

Doc: Doc

The documentation for Methods in this Route.

Private

Private: boolean

true, if this Endpoint is restricted to Admins.

Query

Query: CBQuery

Either an Array of Strings that form a SQL query, or A callback that retrieves/updates backned data for an Data.Method.

Params

Params: CLParams

A collection of GNParams, which represent the global or method-specific path/query/body parameters of an Endpoint:

These can be defined inline:

{
    async Query() { ... },
    Params: {
         UserID: new GNParam({
             Name: 'User ID',
             Default: null,
             Format (cls) { return cls.user_id; },
             // ...
         }),
    },
    Parse(res) { ... },
}

Or defined in a CFG.PNTS.Defaults object within config/authpoints.cfg.js or config/endpoints.cfg.js:

{
    __DEFAULTS: {
        Header: { ... },
        Params: {
            // Define as a Default...
            UserID: new GNParam({ ... }),
        }
    },
    User: {
        Actions: {
            Settings: new RouteDB({
                Params: {
                    // ... Reference the default.
                    UserID: true,
                }
            }),
        },
    },
}

When defined using the latter technique, references to those predefined GNParams are made in the following ways:

Reference Example Description
Direct { Email: true } References the param defined in __DEFAULTS.
Version { Email: ['REQUIRED'] } References a required version of the Email Param.

Key

Key: String

A string that refers to a property-key of an Object-interface property within any Data.Method result returned as an Array<{ key: value, ...rest }>. When this is set, the internal-parser will transform the Array<{ key: value, ...rest }> into Object<key,{ key: value, ...rest }>, removing the property from each item in the Object that now serves as the key of each item-value.

Example:

({
    Query: (cls) => ([
        { user_id: 1, name: 'Hello', age: 102 },
        { user_id: 2, name: 'World', age: 20  },
        { user_id: 3, name: 'Foo',   age: 31  },
        { user_id: 4, name: 'Bar',   age: 46  },
    ]),
    Key: 'user_id',
});
// payload.result = {
//     1: { name: 'Hello', age: 102 },
//     2: { name: 'World', age: 20  },
//     3: { name: 'Foo',   age: 31  },
//     4: { name: 'Bar',   age: 46  },
// }

Private QisFunction

QisFunction: boolean

Denotes if the Query that represents this RouteDB path is a Function.

Private QisAsync

QisAsync: boolean

Denotes if the Query that represents this RouteDB path is an AsyncFunction.

Private QisArray

QisArray: boolean

Denotes if the Query that represents this RouteDB path is SQL-Array.

Methods

Parse

  • An optional callback that handles any post-formatting needed before the result of a method is attached to the payload for consumption.

    This may seem redundant when using Data.Method.Query (as a CBQYHandle, not a SQL string[]), but when a particular Enpdpoint responds to multiple HMETHODs, the result-structure is usually the same. This property can be exploited by defining it globally inside of the Data.Method config. For every CBRouteDB contained within the CFGRouteQry*, this callback will be applied. That way, you need only define it once.

    Of course, if a certain format is required across multiple Endpoints, one can always define the callback outside of the {@link CFGAuthPointsDB} config and assign it to Parse.

    If this is not defined, the internal-parser will do what it does. This is generally just dumping whatever was returned; unless the result returned is of Array<Object<key,value>>, and Key maps to one of the item's keys.

    Uses:

    Parameters

    Returns TPQryObject