Exporting module items using SPL2

You can share searches, custom functions, and custom data types with other users by exporting those items from an SPL2 module. To export an item you use an export statement.

When you export an item that was created in an SPL2 module, such as a search or function, you are taking a private item and making the item public. To use the exported item in another module, you must import the item into that module.

In addition to exporting items that are created inside an SPL2 module, you can also export items that have been imported into an module, such as a dataset.

Exporting search statements

When you export a search statement, the search is exported as a view.

A view is a reusable, named piece of SPL2. A view is a type of dataset that is like a saved search. When you export a search, you are not saving the search results as a dataset. You are saving the search that generates the dataset as a view. To learn more about views, see SPL2 Views.

Export statement syntax

The export statement syntax depends on whether you are exporting one or multiple items, and how you want to format the export statement.

The export statement syntax is:

export <item_name> | { <item_name>, <item_name>, ...}

When specifying multiple items, you must enclose them in curly brackets { } and separate each item with a comma ( , ).

Export multiple searches as views

To export multiple searches, which are exported as views, enclose the list of searches in curly brackets { }.

For example:

export {$qtr1, $qtr2, $qtr3, $qtr4}

Alternatively, you can issue multiple export statements, for example:

export $qtr1
export $qtr2
export $qtr3
export $qtr4

Export groups of items using a wildcard

You can use a single statement to export some types of items that have been imported into or created in a module. The basic syntax is:

export *
Note: Using a wildcard to export items does not export the searches as views.

The following table lists the items that can and cannot be exported using export *:

Included with export * Excluded with export *
These items that are defined in the module:
  • Local functions.
  • Local datasets, except local views.
  • Local data type declarations.

Locally defined views can't be exported using a wildcard.

You must export searches as views using the syntax export {<view_name>, <view_name>, ...}.

Any imported items, such as:
  • Imported datasets.
  • Imported views.
  • Imported functions.
  • Imported data type definitions.

Imported items using a wildcard namespace alias are not included with export * .

Items imported using a wildcard can't be exported using a wildcard.

For example import * as <item> is invalid. These items must be exported separately.

Implicit imports can't be exported using a wildcard.

For example, items in the same namespace don't require an import statement, such as items in the _resources module. See the example later in this topic.

Example of export using a wildcard

Consider the following portion of the biz_set_1 module.

  • An index and a function are imported into the module.
  • Individual export statements are used to export each item from the module.

biz_set_1 module

import main from ~indexes 
import roundif  // This function rounds values to a set number of decimal points if the value is greater than 1.
.
.
.
export main // This index is exported.
export $top10threats  // This search is exported as a view dataset.
export $metrics  // This search is exported as a view dataset.
export is_error  // This local function looks for error codes greater than or equal to 400.
export roundif   // An imported function that is being exported.

You can export some of these items using a wildcard. However, to export the searches as views, you need a separate export statement. For example:

biz_set_1 module

export * // Exports the imported index "main", the local function "is_error", and the imported function "roundif". 

export {$top10threats, $metrics}  // These searches are exported as views.

Example of implicit imports

Suppose you have a module called my_incidents module and another module called biz_set_1 in the same namespace.

my_incidents module

import main from ~indexes 
import is_error // A function from the 'biz_set_1' module.

function roundif ... // A local function.

search $incident_frequency= ...  // A local search. 
search $frequency_by_region= ... // A local search.

export main // An imported index. 
export is_error // An imported function.
export roundif  // A local function.
export $incident_frequency // A local search exported as a view.
export $frequency_by_region // A local search exported as a view.

biz_set_1 module

import threats from ~indexes 

function is_error ... // A local function that ses the <code>roundif</code> function 
from the <code>my_incidents</code> module.

search $top10threats= ... // A local search that uses the imported "threats" index.
search $metrics= ... // A local search that uses the implicitly imported "main" 
index.

export threats  // An imported index.
export is_error  // A local function.
export $top10threats= ... // A local search exported as a view.
export $metrics= ... // A local search exported as a view.

The items exported from the my_incidents module can be used by biz_set_1 module without requiring an import statement in the biz_set_1 module. This is referred to an implicit import.

For example, because the main index and the roundif function are exported from the my_incidents module, you can use these items in the biz_set_1 module without specifying an import statement. These items are implicitly imported because they are in the same namespace.

In addition, because the roundif function is used within the is_error function, you do not need to export the roundif function from the biz_set_1 module. The same is true for the main index, which is used in the $metrics search.

You can use export * to export the indexes and functions from the biz_set_1 module. The local searches exported as views require a specific export statement. For example:

biz_set_1 module

import threats from ~indexes 

function is_error ... // A local function that uses the <code>roundif</code> function 
from the <code>my_incidents</code> module.

search $top10threats= ... // A local search that uses the imported "threats" index.
search $metrics= ... // A local search that uses the implicitly imported "main" 
index.

export * // Exports the imported index "threats" and the local 
function "is_error". The "main" index and the "roundif" function do not need 
to be explicitly exported.
export {$top10threats, $metrics} // Exports these local searches as views

Exporting functions

You can share custom functions with other users by exporting those functions.

Export a function

In the module where the function resides, you export the function by issuing an export statement.

For example, suppose you have the following function called is_error:

function is_error($code: number): boolean {
  return $code >= 400 
}

To export the is_error function, you use this export statement:

export is_error

Export and rename a function

To rename a custom function when you export it, include an as clause in the export statement.

The following example shows how to export the is_error function using the name http_errors:

export is_error as http_errors

Exporting data types

You can export custom data types to share those data types with other users and groups.

Export a data type

In the module where the data type resides, export the data type by issuing an export statement.

For example, suppose you have the following data type called person:

type person = {
   firstname:string, 
   surname:string
}

To export the person data type, the export statement is:

export person

Export and rename a data type

To rename a custom data type when you export it, include an as clause in the export statement.

The following example shows how to export the person data type using the name fullname:

export person as fullname

Export statement shortcuts

You can combine a function statement inline with an export statement.

When you combine these statements, the function statements are valid in the current module. The items that are exported become public items, which can be imported and used in other modules.

Shortcut for combining functions and export statements

Consider the following function that looks for HTTP error codes that are greater than or equal to 400:

function is_error($code : number) : boolean {
  return $code >= 400 
}

To export this function, you can use the following the statement:

export is_error

The following shortcut combines these statements by adding the export statement before the function declaration:

export function is_error($code : number) : boolean {
  return $code >= 400 
}

You can use the is_error function in the current module and export the function to share it with other users and groups.