インポート機能

importキーワードを使用すると、開発者はソース・モジュールによってエクスポートされた機能をインポートできます。

トピック

モジュール・オブジェクト

モジュール・オブジェクトは、モジュールによってエクスポートされたすべてのものをインポートする便利な方法を提供します。

モジュール・オブジェクトは、モジュールによってエクスポートされたすべての識別子にアクセスする手段を提供し、インポートを参照するときに一種のネームスペースとして使用されます。

例5-6 モジュール・オブジェクトの定義

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はモジュール・オブジェクトを示し、named_exports_moduleはモジュール名です。sum()difference()の両方がmod_object_import_modmyMathスコープで使用可能です。

名前付きインポート

ECMAScript標準では、名前付きインポートを指定します。インポート名を使用するかわりに、識別子を指定するオプションもあります。

例5-7 指定した識別子を使用した名前付きインポート

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};
/

複数のモジュールが同じ識別子をエクスポートすると、ネームスペースの競合が発生することがあります。この問題を回避するために、import文で別名を指定できます。

例5-8 別名を使用した名前付きインポート

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};
/

エクスポートされたファンクションを元の名前で参照するかわりに、別名が使用されます。

デフォルト・インポート

名前付きインポートとは異なり、デフォルト・インポートでは中カッコを使用する必要はありません。この構文上の違いは、MLEの組込みモジュールにも関連します。

例5-9 デフォルト・インポート

この例では、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}`);
}
/

同じ手法は、SQLドライバなどのMLEの組込みモジュールの使用にも適用されます。例5-10は、JavaScriptコードでのSQLドライバの使用を示しています。

例5-10 組込みモジュールを使用したデフォルト・インポート

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}`);
}
/

カスタムのJavaScriptモジュールを使用する他の例とは異なり、組込みモジュールのインポートにMLE環境は必要ありません。

関連項目:

組込みJavaScriptモジュールの詳細は、サーバー側JavaScript APIのドキュメントを参照してください