Custom enigma.js mixins

Custom enigma.js mixins

In the previous post we saw what are enigma.js mixins. I've started an repo some time ago (and npm package) where I've been adding mixins that I've found useful (or mixins that I've found boring to write all the time). And want to introduce you to this list.

At the moment the repo contains only mixins that are relevant to application/document.

(to distinct the mixins methods from the default enigma.js ones all mixin methods, from this package, are having prefix m)

The mixins are "split" into groups:

  • Selections
    • mSelectionsAll - returns the "native" reponse from Qlik ragarding the current selections

    • mSelectionsFields - an array of only the field names, having selections in it

    • mSelectionsSimple - without any parameters returns a flat array with all selections

      [
        {field: "Field 1": value: 'Value 1'},
        {field: "Field 1": value: 'Value 2'},
        {field: "Field 2": value: 'Value 10'},
        ...
      ]
      

      if groupByField parameter (optional and default is false) is passed then the result will be grouped by a field name:

      [
        {field: "Field 1": values: [...]},
        {field: "Field 2": values: [...]},
        ...
      ]
      
    • mSelectInField - provide an field name and array of values to be selected

  • Tables and fields
    • mGetTablesAndFields - returns an array with {table: "data-table-name", field: "field-name"} values
    • mGetTables - an array with all data tables
    • mGetFields - an array with all fields
    • mCreateSessionListbox - creates a listbox session object for the provided field
  • Variables
    • mVariableGetAll - array with all variables
    • mVariableUpdateById - updates the definition of an existing variable by providing the variable ID
    • mVariableUpdateByName - updates the definition of an existing variable by providing the variable name
    • mVariableCreate - creates a new variable from the provided name, comment (optional) and definition
  • Extensions
    • mExtensionObjectsAll - returns an array with all extension objects in the current app. Returned fields: appId, appName, objId, objType, extName, extVersion, extVisible, extIsBundle, extIsLibrary, qProps
  • Unbuild
    • mUnbuild - extracts all parts of an Qlik Sense app (apart from the data itself) into JSON object
  • Build
    • mBuild - the opposite of un-building - provide a json object with the app components and this mixin will update (or create) all the objects in the target app

If you want to use the build and unbuild commands outside JavaScript then I highly recommend using Corectl tool

qlik-oss/corectl
corectl is a command line tool to perform reloads, fetch metadata and evaluate expressions in Qlik Core apps. - qlik-oss/corectl

Installation

The installation is very simple:

npm install --save enigma-mixins

Usage

(make sure that enigma.js is already installed)

import enigma from "enigma.js";
import schema from "enigma.js/schemas/12.20.0.json";
import enigmaMixins from "enigma-mixins";

// Create enigma session as usual but include the mixin property
const session = enigma.create({
  schema,
  mixins: enigmaMixins, // this is where the mixinx are added
  url: "ws://localhost:4848/app/engineData",
  createSocket: (url) => new WebSocket(url),
});

Once the session is established and the document is open, then you'll have all the mixins available:

Contribution

Feel free to open an PR with your mixins :)

Repository and npm

countnazgul/enigma-mixin
Set of Enigma mixin. Contribute to countnazgul/enigma-mixin development by creating an account on GitHub.
enigma-mixin
Set of Qlik Sense enigma.js mixin

Hope you liked it!

Stefan