CompendiumCollection

CompendiumCollection

A collection of Document objects contained within a specific compendium pack. Each Compendium pack has its own associated instance of the CompendiumCollection class which contains its contents.

Constructor

(abstract) new CompendiumCollection(metadata)

See:
Parameters:
Name Type Description
metadata object

The compendium metadata, an object provided by game.data

Extends

Members

apps :Array.<Application>

Overrides:

An Array of application references which will be automatically updated when the collection content changes

Type:

CACHE_LIFETIME_SECONDS :number

The amount of time that Document instances within this CompendiumCollection are held in memory. Accessing the contents of the Compendium pack extends the duration of this lifetime.

Type:
  • number

collection :string

The canonical Compendium name - comprised of the originating package and the pack name

Type:
  • string

config :object

Access the compendium configuration data for this pack

Type:
  • object

CONFIG_SETTING :string

The named game setting which contains Compendium configurations.

Type:
  • string

contents :Array.<*>

Overrides:

Return an Array of all the entry values in the Collection

Type:

documentClass :function

Overrides:

A reference to the Document class definition which is contained within this DocumentCollection.

Type:
  • function

documentName :string

Overrides:

A reference to the named Document class which is contained within this DocumentCollection.

Type:
  • string

index :Collection.<string, object>

A subsidiary collection which contains the more minimal index of the pack

Type:

INDEX_FIELDS :Object.<string, Array.<string>>

The default index fields which should be retrieved for each Compendium document type

Type:
  • Object.<string, Array.<string>>

indexed :boolean

Has this Compendium pack been fully indexed?

Type:
  • boolean

locked :boolean

Track whether the Compendium Collection is locked for editing

Type:
  • boolean

metadata :object

The compendium metadata which defines the compendium content and location

Type:
  • object

name :string

Overrides:

The Collection class name

Type:
  • string

private :boolean

Track whether the Compendium Collection is private

Type:
  • boolean

title :string

A convenience reference to the label which should be used as the title for the Compendium pack.

Type:
  • string

Methods

(async, static) createCompendium(metadata, options) → {Promise.<CompendiumCollection>}

Create a new Compendium Collection using provided metadata.

Parameters:
Name Type Description
metadata object

The compendium metadata used to create the new pack

options object

Additional options which modify the Compendium creation request

Returns:
Type
Promise.<CompendiumCollection>

(protected) _onCreateDocuments(documents, result, options, userId)

Overrides:

Follow-up actions taken after a set of Documents in this Collection are created.

Parameters:
Name Type Description
documents Array.<Document>

An Array of created Documents

result Array.<object>

An Array of created data objects

options object

Options which modified the creation operation

userId string

The ID of the User who triggered the operation

(protected) _onDeleteDocuments(documents, result, options, userId)

Overrides:

Follow-up actions taken after a set of Documents in this Collection are deleted.

Parameters:
Name Type Description
documents Array.<Document>

An Array of deleted Documents

result Array.<string>

An Array of document IDs being deleted

options object

Options which modified the deletion operation

userId string

The ID of the User who triggered the operation

(protected) _onUpdateDocuments(documents, result, options, userId)

Overrides:

Follow-up actions taken after a set of Documents in this Collection are updated.

Parameters:
Name Type Description
documents Array.<Document>

An Array of updated Documents

result Array.<object>

An Array of incremental data objects

options object

Options which modified the update operation

userId string

The ID of the User who triggered the operation

(protected) _preCreateDocuments(result, options, userId)

Overrides:

Preliminary actions taken before a set of Documents in this Collection are created.

Parameters:
Name Type Description
result Array.<object>

An Array of created data objects

options object

Options which modified the creation operation

userId string

The ID of the User who triggered the operation

(protected) _preDeleteDocuments(result, options, userId)

Overrides:

Preliminary actions taken before a set of Documents in this Collection are deleted.

Parameters:
Name Type Description
result Array.<string>

An Array of document IDs being deleted

options object

Options which modified the deletion operation

userId string

The ID of the User who triggered the operation

(protected) _preUpdateDocuments(result, options, userId)

Overrides:

Preliminary actions taken before a set of Documents in this Collection are updated.

Parameters:
Name Type Description
result Array.<object>

An Array of incremental data objects

options object

Options which modified the update operation

userId string

The ID of the User who triggered the operation

configure(settings) → {Promise}

Assign configuration metadata settings to the compendium pack

Parameters:
Name Type Description
settings object

The object of compendium settings to define

Returns:

A Promise which resolves once the setting is updated

Type
Promise

delete()

(async) deleteCompendium() → {Promise.<CompendiumCollection>}

Delete an existing world-level Compendium Collection. This action may only be performed for world-level packs by a Gamemaster User.

Returns:
Type
Promise.<CompendiumCollection>

(async) duplicateCompendium(label) → {Promise.<CompendiumCollection>}

Duplicate a compendium pack to the current World.

Parameters:
Name Type Description
label string

A new Compendium label

Returns:
Type
Promise.<CompendiumCollection>

filter(condition) → {Array.<*>}

Overrides:
See:
  • {Array#filter}

Filter the Collection, returning an Array of entries which match a functional condition.

Example
let c = new Collection([["a", "AA"], ["b", "AB"], ["c", "CC"]]);
let hasA = c.filters(entry => entry.slice(0) === "A");
Parameters:
Name Type Description
condition function

The functional condition to test

Returns:

An Array of matched values

Type
Array.<*>

find(condition) → {*}

Overrides:
See:
  • {Array#find}

Find an entry in the Map using an functional condition.

Example
let c = new Collection([["a", "A"], ["b", "B"], ["c", "C"]]);
let a = c.find(entry => entry === "A");
Parameters:
Name Type Description
condition function

The functional condition to test

Returns:

The value, if found, otherwise undefined

Type
*

forEach(fn)

Overrides:
See:
  • Array#forEach

Apply a function to each element of the collection

Example
let c = new Collection([["a", {active: false}], ["b", {active: false}], ["c", {active: false}]]);
c.forEach(e => e.active = true);
Parameters:
Name Type Description
fn function

The function to apply

get(key, optionsopt) → {*|undefined}

Overrides:

Get an element from the Collection by its key.

Example
let c = new Collection([["a", "Alfred"], ["b", "Bob"], ["c", "Cynthia"]]);
c.get("a"); // "Alfred"
c.get("d"); // undefined
c.get("d", {strict: true}); // throws Error
Parameters:
Name Type Attributes Description
key string

The key of the entry to retrieve

options object <optional>

Additional options that affect how entries are retrieved

Properties
Name Type Attributes Default Description
strict boolean <optional>
false

Throw an Error if the requested key does not exist. Default false.

Returns:

The retrieved entry value, if the key exists, otherwise undefined

Type
* | undefined

(async) getDocument(id) → {Promise.<Document>|undefined}

Get a single Document from this Compendium by ID. The document may already be locally cached, otherwise it is retrieved from the server.

Parameters:
Name Type Description
id string

The requested Document id

Returns:

The retrieved Document instance

Type
Promise.<Document> | undefined

(async) getDocuments(query) → {Promise.<Array.<Document>>}

Load multiple documents from the Compendium pack using a provided query object.

Parameters:
Name Type Description
query object

A database query used to retrieve documents from the underlying database

Returns:

The retrieved Document instances

Type
Promise.<Array.<Document>>

(async) getIndex(optionsopt) → {Promise.<Collection>}

Load the Compendium index and cache it as the keys and values of the Collection.

Parameters:
Name Type Attributes Description
options object <optional>

Options which customize how the index is created

Properties
Name Type Attributes Description
fields Array.<string> <optional>

An array of fields to return as part of the index

Returns:
Type
Promise.<Collection>

getName(name, optionsopt) → {*}

Overrides:

Get an entry from the Collection by name. Use of this method assumes that the objects stored in the collection have a "name" attribute.

Example
let c = new Collection([["a", "Alfred"], ["b", "Bob"], ["c", "Cynthia"]]);
c.getName("Alfred"); // "Alfred"
c.getName("D"); // undefined
c.getName("D", {strict: true}); // throws Error
Parameters:
Name Type Attributes Description
name string

The name of the entry to retrieve

options object <optional>

Additional options that affect how entries are retrieved

Properties
Name Type Attributes Default Description
strict boolean <optional>
false

Throw an Error if the requested name does not exist. Default false.

Returns:

The retrieved entry value, if one was found, otherwise undefined

Type
*

(async) importAll(folderIdopt, folderNameopt, optionsopt) → {Promise.<Array.<Document>>}

Fully import the contents of a Compendium pack into a World folder.

Parameters:
Name Type Attributes Description
folderId string | null <optional>

An existing Folder _id to use.

folderName string <optional>

A new Folder name to create.

options object <optional>

Additional options forwarded to WorldCollection#fromCompendium and Document.createDocuments

Returns:

The imported Documents, now existing within the World

Type
Promise.<Array.<Document>>

(async) importDialog(optionsopt) → {Promise.<(Array.<Document>|boolean|null)>}

Provide a dialog form that prompts the user to import the full contents of a Compendium pack into the World.

Parameters:
Name Type Attributes Default Description
options object <optional>
{}

Additional options passed to the Dialog.confirm method

Returns:

A promise which resolves in the following ways: an array of imported Documents if the "yes" button was pressed, false if the "no" button was pressed, or null if the dialog was closed without making a choice.

Type
Promise.<(Array.<Document>|boolean|null)>

importDocument(document, optionsopt) → {Promise.<Document>}

Import a Document into this Compendium Collection.

Parameters:
Name Type Attributes Description
document Document

The existing Document you wish to import

options object <optional>

Additional options which modify how the data is imported. See ClientDocumentMixin#toCompendium

Returns:

The imported Document instance

Type
Promise.<Document>

indexDocument(document)

Add a Document to the index, capturing its relevant index attributes

Parameters:
Name Type Description
document Document

The document to index

map(transformer) → {Array.<*>}

Overrides:

Transform each element of the Collection into a new form, returning an Array of transformed values

Parameters:
Name Type Description
transformer function

The transformation function to apply to each entry value

Returns:

An Array of transformed values

Type
Array.<*>

(async) migrate() → {Promise.<CompendiumCollection>}

Request that a Compendium pack be migrated to the latest System data template

Returns:
Type
Promise.<CompendiumCollection>

reduce(evaluator, initial) → {any}

Overrides:
See:
  • {Array#reduce}

Reduce the Collection by applying an evaluator function and accumulating entries

Example
let c = new Collection([["a", "A"], ["b", "B"], ["c", "C"]]);
let letters = c.reduce((s, l) => {
  return s + l;
}, ""); // "ABC"
Parameters:
Name Type Description
evaluator function

A function which mutates the accumulator each iteration

initial any

An initial value which accumulates with each iteration

Returns:

The accumulated result

Type
any

render()

Overrides:

Render any Applications associated with this DocumentCollection.

set()

Overrides:

some(condition) → {boolean}

Overrides:
See:
  • {Array#some}

Test whether a condition is met by some entry in the Collection

Parameters:
Name Type Description
condition function

A test condition to apply to each entry

Returns:

Was the test condition passed by at least one entry?

Type
boolean

toJSON() → {Array.<object>}

Overrides:

Convert the Collection to a primitive array of its contents.

Returns:

An array of contained values

Type
Array.<object>

(async) updateAll(transformation, condition, optionsopt) → {Promise.<Array.<Document>>}

Overrides:

Update all objects in this DocumentCollection with a provided transformation. Conditionally filter to only apply to Entities which match a certain condition.

Parameters:
Name Type Attributes Default Description
transformation function | object

An object of data or function to apply to all matched objects

condition function | null null

A function which tests whether to target each object

options object <optional>

Additional options passed to Document.update

Returns:

An array of updated data once the operation is complete

Type
Promise.<Array.<Document>>