Repo API
Contents
Functions related to management of a node repository.
Usage
Add the following to your build.gradle
file:
dependencies {
include "com.enonic.xp:lib-repo:${xpVersion}"
}
Add the import
statement to your controller:
import repoLib from '/lib/xp/repo';
You are now ready to use the API.
Events
Usage of this API produces the following events:
Event | Occurs when |
---|---|
repository.created |
a repository is created |
repository.updated |
a repository is updated |
repository.deleted |
a repository is deleted |
repository.restoreInitialized |
a repository restore begins |
repository.restored |
a repository restore is completed |
For more information on events, check out the event API
Functions
create
Creates a repository.
Parameters
Name | Type | Description |
---|---|---|
params |
JSON with the parameters |
Returns
object : Repository created as JSON
Examples
import {create} from '/lib/xp/repo';
// Creates a repository with default configuration
const result1 = create({
id: 'test-repo'
});
log.info('Repository created with id %s', result1.id);
import {create} from '/lib/xp/repo';
// Creates a repository with specific settings
const result2 = create({
id: 'test-repo2',
rootPermissions: [
{
principal: "role:admin",
allow: [
"READ",
"CREATE",
"MODIFY",
"DELETE",
"PUBLISH",
"READ_PERMISSIONS",
"WRITE_PERMISSIONS"
],
deny: []
}
],
rootChildOrder: "_ts DESC"
});
log.info('Repository created with id %s', result2.id);
// First repository created.
const expected1 = {
id: "test-repo",
branches: [
"master"
],
settings: {}
};
createBranch
Creates a branch
Parameters
Name | Type | Description |
---|---|---|
params |
JSON with the parameters |
Returns
object : The branch (as JSON)
Examples
import {createBranch} from '/lib/xp/repo';
// Creates a branch
try {
const result = createBranch({
branchId: 'test-branch',
repoId: 'my-repo'
});
log.info('Branch [%s] created', result.id);
} catch (e) {
if (e.code == 'branchAlreadyExists') {
log.error('Branch [features-branch] already exist');
} else {
log.error('Unexpected error: %s', e.message);
}
}
delete
Deletes a repository
Parameters
Name | Type | Description |
---|---|---|
id |
string |
Repository ID |
Returns
boolean : true
if deleted, false
otherwise
Examples
import {delete as deleteRepo} from '/lib/xp/repo';
// Deletes a repository
const result = deleteRepo('test-repo');
if (result) {
log.info('Repository deleted');
} else {
log.info('Repository was not found');
}
deleteBranch
Deletes a branch
Parameters
Name | Type | Description |
---|---|---|
params |
JSON with the parameters |
Returns
object : The branch (as JSON)
Examples
import {deleteBranch} from '/lib/xp/repo';
// Deletes a branch
try {
const result = deleteBranch({
branchId: 'test-branch',
repoId: 'my-repo'
});
log.info('Branch [%s] deleted', result.id);
} catch (e) {
if (e.code == 'branchNotFound') {
log.error('Branch [test-branch] does not exist');
} else {
log.error('Unexpected error: %s', e.message);
}
}
get
Retrieves a repository
Parameters
Name | Type | Description |
---|---|---|
id |
string |
Repository ID |
Returns
object : The repository (as JSON)
Examples
import {get as getRepo} from '/lib/xp/repo';
// Retrieves a repository
const result = getRepo('test-repo');
if (result) {
log.info('Repository found');
} else {
log.info('Repository was not found');
}
// Repository retrieved.
const expected = {
id: "test-repo",
branches: [
"master"
],
settings: {}
};
list
Retrieves the list of repositories
Returns
object : The repositories (as JSON array)
Examples
import {list} from '/lib/xp/repo';
// Retrieves the list of repositories
const result = list();
log.info('%s repositories found', result.length);
// Repositories retrieved.
const expected = [{
id: "test-repo",
branches: [
"master"
],
settings: {}
}, {
id: "another-repo",
branches: [
"master"
],
settings: {}
}];
refresh
Refreshes indices in the current repository
Parameters
Name | Type | Attributes | Description |
---|---|---|---|
params |
<nullable> |
JSON with the parameters |
Examples
import {refresh} from '/lib/xp/repo';
// Refresh all for default repository
refresh();
// Refresh storage for default repository
refresh({mode: 'storage'});
// Refresh search for 'system-repo' repository
refresh({
mode: 'search',
repo: 'system-repo'
});
Objects
CreateRepoParams
Object to pass to the create
function.
Fields
Name | Type | Attributes | Details |
---|---|---|---|
id |
string |
Repository ID |
|
rootPermissions |
array |
<optional> |
Array of root permissions. By default, all permissions to 'system.admin' and read permission to 'system.authenticated' |
settings |
<optional> |
Repository settings |
RepoSettings
Repository settings to optionally pass to the create
function
Fields
Name | Type | Attributes | Details |
---|---|---|---|
definitions |
<optional> |
Index definitions |
IndexDefinitions
Index definitions used in RepoSettings parameter
Fields
Name | Type | Attributes | Details |
---|---|---|---|
search |
<optional> |
Search index definition |
|
version |
<optional> |
Version index definition |
|
branch |
<optional> |
Branch indexes definition |
IndexDefinition
Fields
Name | Type | Attributes | Description |
---|---|---|---|
settings |
object |
<optional> |
Index definition settings |
mapping |
object |
<optional> |
Index definition settings |
CreateBranchParams
Create branch parameters JSON
Fields
Name | Type | Description |
---|---|---|
branchId |
string |
Branch ID |
repoId |
string |
Repository where the branch should be created |
DeleteBranchParams
Delete branch parameters JSON
Fields
Name | Type | Description |
---|---|---|
branchId |
string |
Branch ID |
repoId |
string |
Repository where the branch should be deleted |
RefreshParams
Refresh parameters JSON
Fields
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
mode |
string |
<optional> |
'all' |
Index definition settings |
repo |
string |
<optional> |
'com.enonic.cms.default' |
Repository id: 'com.enonic.cms.default' | 'system-repo'. Default is the current repository set in portal |
branch |
string |
<optional> |
'branch'=master |
Branch |