Import Functionality

The import keyword allows developers to import functionality that has been exported by a source module.

Topics

Module Objects

A module object supplies a convenient way to import everything that has been exported by a module.

The module object provides a means to access all identifiers exported by a module and is used as a kind of namespace when referring to the imports.

Example 5-6 Module Object Definition

CREATE MLE ENV named_exports_env
    IMPORTS('namedExports' module named_exports_module);

CREATE OR REPLACE MLE MODULE mod_object_import_mod
LANGUAGE JAVASCRIPT AS

//the definition of a module object is demonstrated by the next line
import * as myMath from "namedExports"

function mySum(){
    const result = myMath.sum(4, 2);
    console.log(`the sum of 4 and 2 is ${result}`);
}

function myDifference(){
    const result = myMath.difference(4, 2);
    console.log(`the difference between 4 and 2 is ${result}`);
}

export {mySum, myDifference};
/

myMath identifies the module object and named_exports_module is the module name. Both sum() and difference() are available under the myMath scope in mod_object_import_mod.

Named Imports

The ECMAScript standard specifies named imports. Rather than using an import name, you also have the option to specify identifiers.

Example 5-7 Named Imports Using Specified Identifiers

CREATE OR REPLACE MLE MODULE named_imports_module
LANGUAGE JAVASCRIPT AS

import {sum, difference} from "namedExports";

function mySum(){
    const result = sum(4, 2);
    console.log(`the sum of 4 and 2 is ${result}`);
}

function myDifference(){
    const result = difference(4, 2);
    console.log(`the difference between 4 and 2 is ${result}`);
}

export {mySum, myDifference};
/

Namespace clashes can ensue if multiple modules export the same identifier. To avoid this issue, you can provide an alias in the import statement.

Example 5-8 Named Imports with Aliases

CREATE OR REPLACE MLE MODULE named_imports_alias_module 
LANGUAGE JAVASCRIPT AS

//note the use of aliases in the next line
import {sum as theSum, difference as theDifference} from "namedExports";

function mySum(){
    const result = theSum(4, 2);
    console.log(`the sum of 4 and 2 is ${result}`);
}

function myDifference(){
    const result = theDifference(4, 2);
    console.log(`the difference between 4 and 2 is ${result}`);
}

export {mySum, myDifference};
/

Instead of referencing the exported functions by their original names, the alias is used instead.

Default Imports

Unlike named imports, default imports don't require the use of curly braces. This syntactical difference is also relevant to MLE's built-in modules.

Example 5-9 Default Import

This example demonstrates the default import of myMathClass.

CREATE OR REPLACE MLE ENV default_export_env 
    IMPORTS('defaultExportModule' MODULE default_export_module);

CREATE MLE MODULE default_import_module LANGUAGE JAVASCRIPT AS

//note the lack of curly braces in the next line
import myMathClass from "defaultExportModule";

export function mySum(){
    const result = myMathClass.sum(4, 2);
    console.log(`the sum of 4 and 2 is ${result}`);
}
/

The same technique applies to the use of MLE's built-in modules such as the SQL driver. Example 5-10 demonstrates the use of the SQL driver in JavaScript code.

Example 5-10 Default Import with Built-in Module

CREATE MLE MODULE default_import_built_in_mod 
LANGUAGE JAVASCRIPT AS

//note that there is no need to use MLE environments with built-in modules
import oracledb from "mle-js-oracledb";

export function hello(){
    const options = {
        resultSet: false,
        outFormat: oracledb.OUT_FORMAT_OBJECT
    };
    const bindvars = [];

const conn = oracledb.defaultConnection();
const result = conn.execute('select user', bindvars, options);
console.log(`hello, ${result.rows[0].USER}`);
}
/

Unlike other examples using custom JavaScript modules, no MLE environment is required for importing a built-in module.

See Also:

Server-Side JavaScript API Documentation for more information about the built-in JavaScript modules