Export library
Contents
This API provides functions for creating and importing node-exports.
Install
Add the library to your build.gradle file:
dependencies {
include xplibs.export
}
TypeScript types come from the @enonic-types/lib-export package, installed at the version of XP your project builds against — see TypeScript.
Functions
exportNodes
Creates a node export.
Parameters
exportNodes() takes a single params object with these properties:
| Name | Type | Description |
|---|---|---|
|
sourceNodePath |
string |
Source nodes path. |
|
exportName |
string |
Export name. |
|
nodeResolved |
function |
Optional. A function to be called with the number of nodes the export has to write. It is the export’s best knowledge at the time of the call, and it may be called more than once: a subtree is scanned in bounded batches, so an export of more than 10 000 nodes states a growing total as it goes, each call replacing the previous value. Until the first call the amount is unknown. |
|
nodeExported |
function |
Optional. A function to be called during export with the number of nodes exported since the last call. |
|
batchSize |
number |
Deprecated. Ignored. An export reads each node by the exact version its scan observed, one at a time, so there is no read batch to size. |
Returns
object : Node export results
Examples
import {exportNodes} from '/lib/xp/export';
// Export content nodes.
const result = exportNodes({
sourceNodePath: '/content',
exportName: 'export-1',
nodeExported: (i: number) => {
},
nodeResolved: (i: number) => {
}
});
// Information about exported nodes.
const expected = {
exportedNodes: [
'/content'
],
exportedBinaries: [
'binaryPath [ref]'
],
exportErrors: [
'some error'
]
};
importNodes
Imports nodes from a node export or from application resource files. Optionally pre-transforms node XML node files with XSLT before import.
During node export, current permissions are included in the export file. But when importing, you may need to apply permissions according to your new environment/parent permissions. includePermissions parameter is used to control this behavior. |
Parameters
importNodes() takes a single params object with these properties:
| Name | Type | Description |
|---|---|---|
|
source |
string or object |
Either the name of a nodes-export located in the exports directory, or an application resource key. |
|
targetNodePath |
string |
Target path for imported nodes. |
|
xslt |
string or object |
Optional. XSLT file name in the exports directory or application resource key. Used for XSLT transformation. |
|
xsltParams |
object |
Optional. Parameters used in XSLT transformation. |
|
includeNodeIds |
boolean |
Optional. Set to |
|
includePermissions |
boolean |
Optional. Set to |
|
versionAttributes |
object |
Optional. Attributes attached to the node version of every imported node. |
|
nodeResolved |
function |
Optional. A function to be called before import starts with the number of nodes to import, counted from the export being read. |
|
nodeImported |
function |
Optional. A function to be called during import with the number of nodes imported since the last call. |
|
nodeSkipped |
function |
Optional. A function to be called during import with the number of nodes skipped since the last call. |
Returns
object : Node import results
Examples
import {importNodes} from '/lib/xp/export';
// Import from application resource files
const importedNodes = importNodes({
source: resolve('/import'),
targetNodePath: '/content',
xslt: 'transform.xslt',
xsltParams: {k: 'v'},
includeNodeIds: true,
includePermissions: true,
nodeImported: (i: number) => {
},
nodeResolved: (i: number) => {
},
nodeSkipped: (i: number) => {
}
});
import {importNodes} from '/lib/xp/export';
// Import from an export in exports directory
const importedNodes = importNodes({
source: 'export-1',
targetNodePath: '/content'
});
// Information about imported nodes.
const expected = {
addedNodes: [
'/added'
],
updatedNodes: [
'/updated'
],
skippedNodes: [],
importedBinaries: [
'binaryPath [ref]'
],
importErrors: [
{
exception: 'com.enonic.xp.lib.export.ImportHandlerTest$NoStacktraceException',
message: 'error',
stacktrace: []
}
]
};
list
Lists the available node exports.
Parameters
None.
Returns
object : { exports: [{ name: string }] } — names of all available exports.
Example
import {list} from '/lib/xp/export';
const result = list();
// result.exports === [{ name: 'export-1' }, { name: 'export-2' }]