1
0
Fork 0
arangodb/Documentation/Books/Users/Foxx/Develop/Scripts.mdpp

82 lines
2.4 KiB
Plaintext

!CHAPTER Administrative Scripts
Foxx offers hooks for administrative scripts that are executed once at a specified point.
These scripts are used to create or drop collections, create indexes or insert seed data.
!SECTION Setup script
The setup script will be executed during the install process of your Foxx.
```
unix>foxx-manager install hello-foxx /example
```
It is typically used to setup collections, insert some seed data or create default user accounts.
As one example for a setup script we can take a look at the one given with hello-foxx:
```
var console = require("console");
var arangodb = require("org/arangodb");
var db = arangodb.db;
var texts = applicationContext.collectionName("texts");
if (db._collection(texts) === null) {
var collection = db._create(texts);
collection.save({ text: "entry 1 from collection texts" });
collection.save({ text: "entry 2 from collection texts" });
collection.save({ text: "entry 3 from collection texts" });
}
else {
console.log("collection '%s' already exists. Leaving it untouched.", texts);
}
```
It first creates the collection texts, specific for this application and inserts three default documents.
!SECTION Teardown script
The teardown script will be executed during the uninstall process of your Foxx.
```
unix>foxx-manager uninstall /example
````
This one is typically used to erase the data collected with the Foxx.
ArangoDB will never automatically delete collections attached to a Foxx unless they are dropped in the teardown script.
So if you want to keep your data simply do not drop the collections here.
As an example for a typical teardown script we can again take a look at the hello-foxx example:
```
var arangodb = require("org/arangodb");
var db = arangodb.db;
var texts = applicationContext.collectionName("texts");
var collection = db._collection(texts);
if (collection !== null) {
collection.drop();
}
```
It drops the collection unless it has not yet been dropped.
!SECTION Activate scripts
In order to activate the scripts you have to define them inside the manifest, similar to controllers.
So your manifest will look like this:
```
{
"name": "hello-foxx",
"version": "1.4.4",
"controllers": {},
"setup": "scripts/setup.js",
"teardown": "scripts/teardown.js"
}
```
Keep in mind that all scripts are optional and can be omitted.
It is only possible to define one script of each type.