Search

The cds Facade Object

The cds facade object provides access to all Node.js APIs. It is implemented as a global singleton, with lazy-loading of sub packages to minimize bootstrapping time and memory consumption.

const cds = require('@sap/cds')

cds.lazified (module) require
cds.lazified (object) object

To minimize footprint, only the facade object module itself is loaded by the import above, while close to all properties and functions provided are getters, which lazy-load respective modules only on demand. For example, you can see this in cds repl:

[capire] cds repl
Welcome to cds repl v4.1.6
> cds
cds {
  ... excerpt...
  builtin: [Getter/Setter],
  resolve: [Getter/Setter],
  load: [Getter/Setter],
  parse: [Getter/Setter],
  compile: [Getter/Setter],
  connect: [Getter/Setter],
  serve: [Getter/Setter],
  server: [Getter/Setter],
  deploy: [Getter/Setter],
  version: '4.1.6',
  home: [Getter/Setter]
}

Still in repl, enter cds.builtin, and then cds again to see the effects of lazy-loading.

This is internally done using the cds.lazified methods, which turn selected properties of a given object or module into lazy getters.

Variant cds.lazified(<object>) turns all arrow functions with an argument named lazy into getters, as follows:

const o = cds.lazified({
  aStaticProperty : require('foo'),
  aLazyProperty : lazy => require('bar'),
})

The lazy property will essentially be replaced as follows:

const o = cds.lazified({
  aStaticProperty : require('foo'),
  get aLazyProperty() {
    let value = require('bar')
    Object.defineProperty(this,'aLazyProperty',{value,enumerable:true,writable:true})
    return value
  }
})

Variant cds.lazified(<module>) takes a node module as an argument and returns a derived require function, which turns all requires into lazy requires upon subsequent assignment to module.exports. For example:

require = cds.lazified(module)
module.exports = {
  aStaticProperty : module.require('foo'),
  aLazyProperty : require('bar'),
}

cds.app = express.Application

The express.js Application object constructed during bootstrapping.

Learn more about bootstrapping in the docs of cds.server

cds.db = cds.Service

The primary database services connected to during bootstrapping, if any.

See also the bootstrapping process described in cds.server

cds.model = csn

The effective CDS model loaded during bootstrapping, which contains all service and entity definitions, including required services.

See also the bootstrapping process described in cds.server

cds.services = { cds.Services }

A dictionary and cache of all services constructed through cds.serve, or connected to by cds.connect so far. It’s an iterable object, so you can access individual services by name, as well as iterate through all services:

const { CatalogService } = cds.services
for (let each of cds.services) ...

cds.env { … }

Provides access to the effective configuration of the current process, transparently from various sources, including the local package.json or .cdsrc.json, service bindings and process environments.

Learn more in the Configuration guide

cds.requires { … }

… is a convenience shortcut to cds.env.requires.

cds.version string

Returns the version of the @sap/cds package from which the current instance of the cds facade module was loaded.

cds.home string

Returns the pathname of the @sap/cds installation folder from which the current instance of the cds facade module was loaded.