Collections
Availability table
Module |
Available |
|---|---|
collections controller
This controller expose operation to manipulate Collections
- collections.delete(schema: string, documentId: string)
Delete a record from a collection by its document id.
The record is soft-deleted: it no longer appears in
list()/get()results but can be brought back withrestore().- Arguments:
schema – Item ID of the
schema.collectionsconfiguration identifying the target collection.documentId – Mongo
_idof the record to delete.
- Returns:
An object with attribute :
success (boolean) Indicates whether the operation was successful.
message (string) Contains additional information about the operation’s success or failure.
content (object) Data returned from a successful request.
Type of the data is different for each request type. Each operation presented below explain what you should expect from the content of the request
content holds the result of the query:
content.documentId (string) Mongo
_idof the deleted record.content.operation (string) the operation performed, here
delete.
- collections.deleteWithCustomFilter(schema: string, filter: string)
Delete every record matching a custom MongoDB filter.
Matching records are soft-deleted and can be brought back with
restore().- Arguments:
schema – Item ID of the
schema.collectionsconfiguration identifying the target collection.filter – A MongoDB filter, given as a string, that may use the filter placeholder. See filter placeholder. Every record matching it is deleted.
- Returns:
An object with attribute :
success (boolean) Indicates whether the operation was successful.
message (string) Contains additional information about the operation’s success or failure.
content (object) Data returned from a successful request.
Type of the data is different for each request type. Each operation presented below explain what you should expect from the content of the request
content holds the result of the query:
content.documentIds (array) Mongo
_idof every deleted record.content.operation (string) the operation performed, here
delete.
- collections.get(schema: string, documentId: string)
Fetch a single record of a collection by its Mongo document id.
The caller access-rights filters are applied, so a record that exists but is not readable by the caller is not returned.
- Arguments:
schema – Item ID of the
schema.collectionsconfiguration identifying the target collection.documentId – Mongo
_idof the document to fetch.
- Returns:
An object with attribute :
success (boolean) Indicates whether the operation was successful.
message (string) Contains additional information about the operation’s success or failure.
content (object) Data returned from a successful request.
Type of the data is different for each request type. Each operation presented below explain what you should expect from the content of the request
content holds the matching document itself (its fields, including
_id). When no readable document matches the id, success isfalseand content is empty.
- collections.getHistory(schema: string, documentId: string)
Get the history of every revision of a document.
- Arguments:
schema – Item ID of the
schema.collectionsconfiguration identifying the target collection.documentId – Mongo
_idof the document whose revisions are requested.
- Returns:
An object with attribute :
success (boolean) Indicates whether the operation was successful.
message (string) Contains additional information about the operation’s success or failure.
content (object) Data returned from a successful request.
Type of the data is different for each request type. Each operation presented below explain what you should expect from the content of the request
content holds the result of the query:
content.collections (array) every stored revision of the document linked with documentId.
- collections.getUserAttributeFromPreferences(attribute: string, defaultValue?: any)
Get the value associated with an attribute from the preferences of the user that triggered the script.
The lookup only works when the script was started by a front-end action (a menu, a button, …), because it relies on the authenticated username. When the script runs without a user context, the preferences cannot be resolved and defaultValue is returned.
- Arguments:
attribute – Name of the user-preference entry to read.
defaultValue – Optional value returned when the attribute cannot be resolved (unknown attribute, no authenticated user, or failed request). Defaults to
nullwhen not provided.
- Returns:
Return the value of attribute from the preferences of the user that initiated the action which triggered the script (like pressing a button). If no value is found, defaultValue is returned instead; when defaultValue is not specified,
nullis returned.
- collections.getUsername()
This method allow to get username of user that is authenticated and did an action that run the script. This information is accessible only if the script is trigger by a front-end action, like a menu for example. Otherwise, this will return an empty string.
- Returns:
Return a string that represent username that triggered the script.
- collections.getWithCustomFilter(schema: string, filter: string)
Same as
get()but selects the record with a custom MongoDB filter string. The first record matching the filter (AND-ed with the caller access rights) is returned.- Arguments:
schema – Item ID of the
schema.collectionsconfiguration identifying the target collection.filter – A MongoDB filter, given as a string, that may use the filter placeholder. See filter placeholder. An invalid filter fails the request with an invalid filter error.
- Returns:
Same as
get(): content holds the matching document, or is empty when nothing matches.
- collections.getWithFilterId(schema: string, filter: string)
Same as
get()but selects the record with a filter declared in the configuration instead of a document id. The first record matching the configured filter (within the caller access rights) is returned.- Arguments:
schema – Item ID of the
schema.collectionsconfiguration identifying the target collection.filter – Id of the filter declared in the configuration of
schema.collectionsorschema.ospp. An unknown id fails the request.
- Returns:
Same as
get(): content holds the matching document, or is empty when nothing matches.
- collections.insert(schema: string, data: object)
Insert a new record into a collection.
- Arguments:
schema – Item ID of the
schema.collectionsconfiguration identifying the target collection.data – The document to insert, as an object. Values produced by the script are unwrapped before being stored.
- Returns:
An object with attribute :
success (boolean) Indicates whether the operation was successful.
message (string) Contains additional information about the operation’s success or failure.
content (object) Data returned from a successful request.
Type of the data is different for each request type. Each operation presented below explain what you should expect from the content of the request
content holds the result of the query:
content.documentId (string) Mongo
_idof the inserted record.content.document (object) the stored document.
content.operation (string) the operation performed, here
insert.
- collections.insertMany(schema: string, data: object[])
Insert several records into a collection in a single request.
- Arguments:
schema – Item ID of the
schema.collectionsconfiguration identifying the target collection.data – The list of documents to insert. Each document’s script values are unwrapped before being stored.
- Returns:
An object with attribute :
success (boolean) Indicates whether the operation was successful.
message (string) Contains additional information about the operation’s success or failure.
content (object) Data returned from a successful request.
Type of the data is different for each request type. Each operation presented below explain what you should expect from the content of the request
content holds the result of the query:
content.documentIds (array) Mongo
_idof every inserted record, in insertion order.content.operation (string) the operation performed, here
insert.
- collections.list(schema: string, pageSize: number, pageNumber: number, filterId?: string)
List the records of a collection, one page at a time.
Only records the caller is allowed to read are returned: the collection access-rights filters are always AND-ed with the query. By default only active records (not deleted, see
delete()) are listed and the page is sorted bymodified_atin descending order (most recently modified first).An optional filterId further restricts the result to a filter declared in the schema configuration.
- Arguments:
schema – Item ID of the
schema.collectionsconfiguration identifying the target collection. An unknown id fails the request with a schema is not defined error.pageSize – Maximum number of documents returned for a single page (applied as a MongoDB
$limit). content.collections never holds more thanpageSizedocuments.pageNumber – Zero-based index of the page to return. Documents are skipped using
pageNumber * pageSize(MongoDB$skip):0returns the first page,1the second, and so on.filterId – Optional id of a filter declared in the
schema.collections/schema.osppconfiguration. When provided, its query (and scope) is combined with the access-rights filters; when omitted, every readable active record is listed.
- Returns:
An object with attribute :
success (boolean) Indicates whether the operation was successful.
message (string) Contains additional information about the operation’s success or failure.
content (object) Data returned from a successful request.
Type of the data is different for each request type. Each operation presented below explain what you should expect from the content of the request
content holds the result of the query:
content.totalCount (number) total number of records matching the filter, independent of pageSize and pageNumber.
content.collections (array) the documents of the requested page.
- collections.listCustomFilter(schema: string, pageSize: number, pageNumber: number, filter: string)
Same as
list()but the page is filtered with a custom MongoDB query string instead of a configured filter id.The custom filter is AND-ed with the caller access-rights filters. Only active records are returned and the page is sorted by
modified_atdescending, exactly likelist().- Arguments:
schema – Item ID of the
schema.collectionsconfiguration identifying the target collection. An unknown id fails the request with a schema is not defined error.pageSize – Maximum number of documents returned for a single page (applied as a MongoDB
$limit).pageNumber – Zero-based index of the page to return. Documents are skipped using
pageNumber * pageSize(MongoDB$skip).filter – A MongoDB filter, given as a string, that may use the filter placeholder. See filter placeholder. An invalid filter fails the request with an invalid filter error.
- Returns:
Same as
list(): content.totalCount and content.collections.
- collections.restore(schema: string, documentId: string)
Restore a previously deleted record of a collection, making it active again (reverse of
delete()).- Arguments:
schema – Item ID of the
schema.collectionsconfiguration identifying the target collection.documentId – Mongo
_idof the record to restore.
- Returns:
An object with attribute :
success (boolean) Indicates whether the operation was successful.
message (string) Contains additional information about the operation’s success or failure.
content (object) Data returned from a successful request.
Type of the data is different for each request type. Each operation presented below explain what you should expect from the content of the request
content holds the result of the query:
content.documentId (string) Mongo
_idof the restored record.content.operation (string) the operation performed, here
update.
- collections.update(schema: string, document: object)
Update a record by giving its complete object.
When document carries a non-empty
_id, the matching record is updated; otherwise the object is inserted as a new record.- Arguments:
schema – Item ID of the
schema.collectionsconfiguration identifying the target collection.document – The complete collection object. A non-empty
_idtriggers an update, its absence triggers an insert.
- Returns:
Same as
update().
- collections.update(schema: string, documentId: string, data: object[])
Update the record identified by documentId by applying a list of field-level updates.
- Arguments:
schema – Item ID of the
schema.collectionsconfiguration identifying the target collection.documentId – Mongo
_idof the record to update.data – List of updates to apply on the record. Each entry is an object with three properties:
field(the field to update),operation(the update operation to apply) andcontent(the value). Entries that miss any of these three properties are ignored. You can look at the collection documentation to see how to define an update object.
- Returns:
Same as
insert().
- collections.updateMany(schema: string, filter: string, data: object[])
Apply the same list of field-level updates to every record matching a custom MongoDB filter.
- Arguments:
schema – Item ID of the
schema.collectionsconfiguration identifying the target collection.filter – A MongoDB filter, given as a string, that may use the filter placeholder. See filter placeholder. Every record matching it is updated.
data – List of updates to apply on every matching record. Each entry is an object with three properties:
field(the field to update),operation(the update operation) andcontent(the value). Entries that miss any of these three properties are ignored. You can look at the collection documentation to see how to define an update object.
- Returns:
An object with attribute :
success (boolean) Indicates whether the operation was successful.
message (string) Contains additional information about the operation’s success or failure.
content (object) Data returned from a successful request.
Type of the data is different for each request type. Each operation presented below explain what you should expect from the content of the request
content holds the result of the query:
content.documentIds (array) Mongo
_idof every updated record.content.operation (string) the operation performed, here
update.
- collections.updateWithCustomFilter(schema: string, filter: string, data: object[])
Update the single record matching a custom MongoDB filter by applying a list of field-level updates.
Unlike
updateMany(), the filter must match exactly one record: the request fails when no record matches and fails as well when several records match. Only active records readable by the caller are considered.- Arguments:
schema – Item ID of the
schema.collectionsconfiguration identifying the target collection.filter – A MongoDB filter, given as a string, that may use the filter placeholder. See filter placeholder. It must match exactly one record.
data – List of updates to apply on the matching record. Each entry is an object with three properties:
field(the field to update),operation(the update operation) andcontent(the value). Entries that miss any of these three properties are ignored. You can look at the collection documentation to see how to define an update object.
- Returns:
Same as
insert().