Repository

A repository is a data store where you can store content objects - things like articles, blog entries,
web site pages, video files, PDFs and much more. Repositories feature changeset versioning so that
you can safely make changes without anything getting lost. Repositories also give you dictionary
services for defining content types, validation logic, behaviors and rules that trigger when
content events are triggered.

## Configuration

As with all data stores, this data store maintain a configuration document that contains system
and custom properties. You can write your own properties onto this configuration document if you wish.
You might use those properties to query and retrieve the directory at a later time.

Some system properties are read-only and others are writable. You can adjust the writable properties
to modify the behavior and characteristics of the directory.

The following table presents you the system properties.

Data Store Properties

{{#if containerTypeId}} {{/if}}
Data Store Type {{#dataTypeArticle datastoreTypeId}}{{datastoreTypeId}}{{/dataTypeArticle}}
Container {{#dataTypeArticle containerTypeId}}{{containerTypeId}}{{/dataTypeArticle}}
Supports {{#article "security/teams"}}teams{{/article}}, {{#article "security/authorities"}}authorities{{/article}}, {{#article "security/permissions"}}permissions{{/article}}, {{#article "binaries"}}binaries{{/article}}, {{#article "transfer"}}transfer{{/article}}

Any system properties that are shown to be Read-Only are not modifiable on calls to create() or update().
Anything else is fair game.

Property Type Default Read-Only Description
title text The title of the data store.
description text A description for the data store.
enableAuthorities boolean true If true, the data store will check whether the current principal has rights to perform an operation against content upon interaction. If turned off, you'll see some performance benefits. This should only ever be switched off for "published" or general availability content and never for authoring-side content.
enableAuditing boolean false If true, then the data store will collect audit records for all service-level operations (such as CRUD operations). This incurs a slight performance penalty but guarantees you a record of all interactions with data for changesets, branches and nodes in the data store.
locked boolean false Read-only Whether the data store is presently locked. If the data store is locked, then write operations against it will block.
lockedBy text Read-only The principal that holds the lock (if the data store is locked).
size number 0 Read-only The last-recorded allocation size (on disk) of the data store in bytes. This value is updated periodically and may not be precise at the moment it is read.
maxsize number -1 The maximum allowed allocation size (on disk) of the data store in bytes. If this size of the data store exceeds this value, write operations will fail. A value of -1 indicates that there is no upper limit.
objectcount number 0 Read-only The number of objects stored within the data store. This includes system-maintained objects. This value is updated periodically and may not be precise at the moment it is read.
statisticsDate timestamp Read-only Records the time when statistics were last run against the data store. Statistics are run periodically to calculate the data store size and object allocations.
statisticsDirty boolean false Read-only Whether writes have occurred against this data store since the last time statistics were run. This signals to the system that statistics need to be collected at the next available opportunity.

Create a Repository

platform.createRepository();

Or you can pass in configuration which will be applied to the repository. You can pass in
any properties you'd like. These properties will become part of the repository configuration and you
can utilize that however you'd like.

platform.createRepository({
    "title": "My first repository",
    "description": "Oh my darling clementine"
});

Here is an example where we create a repository and then immediately display a property and then update it.

platform.createRepository({
    "title": "My first repository",
    "country": "USA"
}).then(function() {
    console.log("Country: " + this.get("country"));
})

Update aRepository

To update a repository, you just adjust its properties and then tell the server to update.

repository.set("country", "Canada");
repository.update();

Here is an example that creates a repository and then immediately updates it.

platform.createRepository({
    "title": "Just the good old boys",
    "country": "USA"
}).then(function() {

    // yay, my repository was successfully created!

    // display the country
    console.log("Country: " + this.get("country"));

    // update the repository
    this.set("country", "Canada");
    this.update();
});

Delete a Repository

If you're not sure whether you'll need it again in the future, you might want to archive it first.

Here is an example of a simple delete.

repository.del();

And here is an example where we create, update and then delete.

platform.createRepository().update().del();

Read a Repository

You retrieve repositories by their ID - the _doc field.

// suppose we know the repository id ahead of time
var repositoryId = "4b5b685c980c10f98beb";

// read the repository
platform.readRepository(repositoryId);

Granted, in many cases you won't know the repository ID off-hand. You'll likely need to look up the
repository ID ahead of time by some other means. Read further on this page for examples of how you
can do this.

List Repositories

The following code hands back the full list.

platform.listRepositories();

You can then iterate over the list to pick at what you'd like.

platform.listRepositories().each(function() {
    console.log("Repository title: " + this.get("title"));
});

Retrieving the full list is potentially an expensive operation, depending how many repositories you have.
The more your retrieve, the more data goes over the wire and so forth. Thus, you might wish to
paginate your call and retrieve a subset of the total list at a time.

Here is an example that retrieves in pages of size 10.
It starts on the second page (by skipping to starting index 20).
It also sorts by the title field in a descending sequence.

platform.listRepositories({
    "limit": 10,
    "skip": 20,
    "sort": { "title": -1 }
}).each(function() {
    console.log("Repository title: " + this.get("title"));
});

You don't have to have all of the pagination parameters (limit, skip and sort).
You might only want to include one or two. It's up to you.

Query Repositories

Here is a query that looks for repositories whose field country is "USA".

platform.queryRepositories({
    "country": "USA"
});

Here is that same query followed by an iteration over the result map.

platform.queryRepositories({
    "country": "USA"
}).each(function(id) {
    console.log("Found a repository: " + id + " with title: " + this.get("title"));
});

Cloud CMS utilizes the MongoDB query syntax under the hood. As such, you can really go to town.
You're able to express queries with some pretty rich complexity.

Here is a query that finds all repositories created after July 4, 2012.

var milliseconds = new Date(2012, 7, 4, 0, 0, 0, 0).getTime();
platform.queryRepositories({
    "_system.createdon.ms": { "$gt": milliseconds }
}).each(function(id) {
    console.log("Found a repository: " + id + " with title: " + this.get("title"));
});

Finally, you can mix queries with pagination to reduce data over the wire and speed up performance.
Here is that same query with some pagination thrown in. We limit to pages of size 10 and sort by title.

// the milliseconds for July 4, 2012
var milliseconds = new Date(2012, 7, 4, 0, 0, 0, 0).getTime();

// our query
var query = {
    "_system.createdon.ms": { "$gt": milliseconds }
};

// pagination parameters
var pagination = {
    "limit": 10,
    "skip": 20,
    "sort": {
        "title": -1
    }
};

// run the query and iterate over the result set
platform.queryRepositories(query, pagination).each(function(id) {
    console.log("Found a repository: " + id + " with title: " + this.get("title"));
});