LogoLogo
Studio
4.3
4.3
  • HarperDB Docs
  • Getting Started
  • Developers
    • Applications
      • Caching
      • Defining Schemas
      • Debugging Applications
      • Define Fastify Routes
      • Example Projects
    • Components
      • Installing
      • Writing Extensions
      • Operations
      • Google Data Studio
      • SDKs
      • Drivers
    • REST
    • Operations API
      • Quick Start Examples
      • Databases and Tables
      • NoSQL Operations
      • Bulk Operations
      • Users and Roles
      • Clustering
      • Custom Functions
      • Components
      • Registration
      • Jobs
      • Logs
      • Utilities
      • Token Authentication
      • SQL Operations
      • Advanced JSON SQL Examples
    • Real-Time
    • Clustering
      • Requirements and Definitions
      • Creating A Cluster User
      • Naming A Node
      • Enabling Clustering
      • Establishing Routes
      • Subscription Overview
      • Managing Subscriptions
      • Things Worth Knowing
      • Certificate Management
    • Security
      • JWT Authentication
      • Basic Authentication
      • mTLS Authentication
      • Configuration
      • Users & Roles
      • Certificate Management
    • SQL Guide
      • SQL Features Matrix
      • SQL Date Functions
      • SQL Reserved Word
      • SQL Functions
      • SQL JSON Search
      • SQL Geospatial Functions
  • Administration
    • Best Practices and Recommendations
    • Logging
      • Standard Logging
      • Audit Logging
      • Transaction Logging
    • Clone Node
    • Compact
    • Jobs
    • HarperDB Studio
      • Create an Account
      • Log In & Password Reset
      • Organizations
      • Instances
      • Query Instance Data
      • Manage Databases / Browse Data
      • Manage Charts
      • Manage Clustering
      • Manage Instance Users
      • Manage Instance Roles
      • Manage Applications
      • Instance Metrics
      • Instance Configuration
      • Enable Mixed Content
  • Deployments
    • Configuration File
    • HarperDB CLI
    • Install HarperDB
      • On Linux
    • Upgrade a HarperDB Instance
    • HarperDB Cloud
      • IOPS Impact on Performance
      • Instance Size Hardware Specs
      • Alarms
      • Verizon 5G Wavelength
  • Technical Details
    • Reference
      • Analytics
      • Architecture
      • Content Types
      • Data Types
      • Dynamic Schema
      • HarperDB Headers
      • HarperDB Limits
      • Globals
      • Resource Class
      • Transactions
      • Storage Algorithm
    • Release Notes
      • HarperDB Tucker (Version 4)
        • 4.3.36
        • 4.3.35
        • 4.3.34
        • 4.3.33
        • 4.3.32
        • 4.3.31
        • 4.3.30
        • 4.3.29
        • 4.3.28
        • 4.3.27
        • 4.3.26
        • 4.3.25
        • 4.3.24
        • 4.3.23
        • 4.3.22
        • 4.3.21
        • 4.3.20
        • 4.3.19
        • 4.3.18
        • 4.3.17
        • 4.3.16
        • 4.3.15
        • 4.3.14
        • 4.3.13
        • 4.3.12
        • 4.3.11
        • 4.3.10
        • 4.3.9
        • 4.3.8
        • 4.3.7
        • 4.3.6
        • 4.3.5
        • 4.3.4
        • 4.3.3
        • 4.3.2
        • 4.3.1
        • 4.3.0
        • 4.2.8
        • 4.2.7
        • 4.2.6
        • 4.2.5
        • 4.2.4
        • 4.2.3
        • 4.2.2
        • 4.2.1
        • 4.2.0
        • 4.1.2
        • 4.1.1
        • 4.1.0
        • 4.0.7
        • 4.0.6
        • 4.0.5
        • 4.0.4
        • 4.0.3
        • 4.0.2
        • 4.0.1
        • 4.0.0
        • HarperDB Monkey (Version 3)
        • 3.3.0
        • 3.2.1
        • 3.2.0
        • 3.1.5
        • 3.1.4
        • 3.1.3
        • 3.1.2
        • 3.1.1
        • 3.1.0
        • 3.0.0
        • HarperDB Penny (Version 2)
        • 2.3.1
        • 2.3.0
        • 2.2.3
        • 2.2.2
        • 2.2.0
        • 2.1.1
        • HarperDB Alby (Version 1)
        • 1.3.1
        • 1.3.0
        • 1.2.0
        • 1.1.0
  • More Help
    • Support
    • Slack
    • Contact Us
Powered by GitBook
On this page
  • tables
  • databases
  • Resource
  • auth(username, password?): Promise<User>
  • logger
  • server
  • server.http(listener: RequestListener, options: HttpOptions): HttpServer[]
  • server.socket(listener: ConnectionListener, options: SocketOptions): SocketServer
  • server.ws(listener: WsListener, options: WsOptions): HttpServer[]
  • server.upgrade(listener: UpgradeListener, options: UpgradeOptions): void
  • server.config
  • server.recordAnalytics(value, metric, path?, method?, type?)
  1. Technical Details
  2. Reference

Globals

PreviousHarperDB LimitsNextResource Class

Last updated 2 months ago

The primary way that JavaScript code can interact with HarperDB is through the global variables, which has several objects and classes that provide access to the tables, server hooks, and resources that HarperDB provides for building applications. As global variables, these can be directly accessed in any module.

These global variables are also available through the harperdb module/package, which can provide better typing in TypeScript. To use this with your own directory, make sure you link the package to your current harperdb installation:

npm link harperdb

The harperdb package is automatically linked for all installed components. Once linked, if you are using EcmaScript module syntax you can import function from harperdb like:

import { tables, Resource } from 'harperdb';

Or if you are using CommonJS format for your modules:

const { tables, Resource } = require('harperdb');

The global variables include:

tables

This is an object that holds all the tables for the default database (called data) as properties. Each of these property values is a table class that subclasses the Resource interface and provides access to the table through the Resource interface. For example, you can get a record from a table (in the default database) called 'my-table' with:

import { tables } from 'harperdb';
const { MyTable } = tables;
async function getRecord() {
	let record = await MyTable.get(recordId);
}

It is recommended that you for all the tables that are required to exist in your application. This will ensure that the tables exist on the tables object. Also note that the property names follow a CamelCase convention for use in JavaScript and in the GraphQL Schemas, but these are translated to snake_case for the actual table names, and converted back to CamelCase when added to the tables object.

databases

This is an object that holds all the databases in HarperDB, and can be used to explicitly access a table by database name. Each database will be a property on this object, each of these property values will be an object with the set of all tables in that database. The default database, databases.data should equal the tables export. For example, if you want to access the "dog" table in the "dev" database, you could do so:

import { databases } from 'harperdb';
const { Dog } = databases.dev;

Resource

auth(username, password?): Promise<User>

This returns the user object with permissions/authorization information based on the provided username. If a password is provided, the password will be verified before returning the user object (if the password is incorrect, an error will be thrown).

logger

server

The server global object provides a number of functions and objects to interact with Harper's HTTP service.

server.http(listener: RequestListener, options: HttpOptions): HttpServer[]

Alias: server.request

Add a handler method to the HTTP server request listener middleware chain.

Returns an array of server instances based on the specified options.port and options.securePort.

Example:

server.http((request, next) => {
	return request.url === '/graphql'
		? handleGraphQLRequest(request)
		: next(request);
}, {
	runFirst: true, // run this handler first
});

RequestListener

Type: (request: Request, next: RequestListener) => Promise<Response>

The HTTP request listener to be added to the middleware chain. To continue chain execution pass the request to the next function such as return next(request);.

Request

Response

HttpOptions

Type: Object

Properties:

  • runFirst - optional - boolean - Add listener to the front of the middleware chain. Defaults to false

  • port - optional - number - Specify which HTTP server middleware chain to add the listener to. Defaults to the Harper system default HTTP port configured by harperdb-config.yaml, generally 9926

  • securePort - optional - number - Specify which HTTPS server middleware chain to add the listener to. Defaults to the Harper system default HTTP secure port configured by harperdb-config.yaml, generally 9927

HttpServer

server.socket(listener: ConnectionListener, options: SocketOptions): SocketServer

Creates a socket server on the specified options.port or options.securePort.

Only one socket server will be created. A securePort takes precedence.

ConnectionListener

SocketOptions

SocketServer

server.ws(listener: WsListener, options: WsOptions): HttpServer[]

Example:

server.ws((ws, request, chainCompletion) => {
	chainCompletion.then(() => {
		ws.on('error', console.error);

		ws.on('message', function message(data) {
			console.log('received: %s', data);
		});

		ws.send('something');
	});
});

WsListener

Type: (ws: WebSocket, request: Request, chainCompletion: ChainCompletion, next: WsListener): Promise<void>

The WebSocket connection listener.

  • The chainCompletion argument is a Promise of the associated HTTP server's request chain. Awaiting this promise enables the user to ensure the HTTP request has finished being processed before operating on the WebSocket.

  • The next argument is similar to that of other next arguments in Harper's server middlewares. To continue execution of the WebSocket connection listener middleware chain, pass all of the other arguments to this one such as: next(ws, request, chainCompletion)

WsOptions

Type: Object

Properties:

  • maxPayload - optional - number - Set the max payload size for the WebSocket server. Defaults to 100 MB.

  • runFirst - optional - boolean - Add listener to the front of the middleware chain. Defaults to false

  • port - optional - number - Specify which WebSocket server middleware chain to add the listener to. Defaults to the Harper system default HTTP port configured by harperdb-config.yaml, generally 9926

  • securePort - optional - number - Specify which WebSocket secure server middleware chain to add the listener to. Defaults to the Harper system default HTTP secure port configured by harperdb-config.yaml, generally 9927

server.upgrade(listener: UpgradeListener, options: UpgradeOptions): void

This method should be used to delegate HTTP upgrade events to an external WebSocket server instance.

Example:

server.upgrade(
	(request, socket, head, next) => {
		if (request.url === '/_next/webpack-hmr') {
			return upgradeHandler(request, socket, head).then(() => {
				request.__harperdb_request_upgraded = true;

				next(request, socket, head);
			});
		}

		return next(request, socket, head);
	},
	{ runFirst: true }
);

UpgradeListener

Type: (request, socket, head, next) => void

UpgradeOptions

Type: Object

Properties:

  • runFirst - optional - boolean - Add listener to the front of the middleware chain. Defaults to false

  • port - optional - number - Specify which HTTP server middleware chain to add the listener to. Defaults to the Harper system default HTTP port configured by harperdb-config.yaml, generally 9926

  • securePort - optional - number - Specify which HTTP secure server middleware chain to add the listener to. Defaults to the Harper system default HTTP secure port configured by harperdb-config.yaml, generally 9927

server.config

server.recordAnalytics(value, metric, path?, method?, type?)

  • value - This is a numeric value for the metric that is being recorded. This can be a value measuring time or bytes, for example.

  • metric - This is the name of the metric.

  • path - This is an optional path (like a URL path). For a URL like /my-resource/, you would typically include a path of "my-resource", not including the id so you can group by all the requests to "my-resource" instead of individually aggregating by each individual id.

  • method - Optional method to group by.

  • type - Optional type to group by.

This is the base class for all resources, including tables and external data sources. This is provided so that you can extend it to implement custom data source providers. See the for more details about implementing a Resource class.

This provides methods trace, debug, info, warn, error, fatal, and notify for logging. See the for more information.

An implementation of WHATWG class.

An implementation of WHATWG class.

Node.js or instance.

Node.js socket server connection listener as documented in or

port - optional - number - Specify the port for the instance.

securePort - optional - number - Specify the port for the instance.

Node.js or instance.

Add a listener to the WebSocket connection listener middleware chain. The WebSocket server is associated with the HTTP server specified by the options.port or options.securePort. Use the method to add a listener to the upgrade middleware chain.

The ws argument is the instance as defined by the ws module.

The request argument is Harper's transformation of the IncomingMessage argument of the standard listener event for a WebSocket server.

Add a listener to the HTTP Server event. If a WebSocket connection listener is added using , a default upgrade handler will be added as well. The default upgrade handler will add a __harperdb_request_upgraded boolean to the request argument to signal the connection has already been upgraded. It will also check for this boolean before upgrading and if it is true, it will pass the arguments along to the next listener.

This example is from the HarperDB Next.js component. See the complete source code

The arguments are passed to the middleware chain from the HTTP server event.

This provides access to the HarperDB configuration object. This comes from the (parsed into object form).

This records the provided value as a metric into HarperDB's analytics. HarperDB efficiently records and tracks these metrics and makes them available through . The values are aggregated and statistical information is computed when many operations are performed. The optional parameters can be used to group statistics. For the parameters, make sure you are not grouping on too fine of a level for useful aggregation. The parameters are:

define a database
Resource API documentation
logging documentation
Request
Response
http.Server
https.SecureServer
net.createServer
tls.createServer
net.Server
tls.Server
net.Server
tls.Server
WebSocket
'connection'
here
'upgrade'
harperdb-config.yaml
analytics API
server.upgrade()
upgrade
server.ws()