BaseObjectService Class
Provides functionality to perform CRUD operations on collections. It also allows for plugins to register for events in order to interact with objects as they are being processed. This makes it possible to add, remove, or modify fields before they are passed to the entity that triggered the operation.
Constructor
BaseObjectService
-
context
Parameters:
-
context
Object-
type
String
-
Item Index
Methods
- _emit
- _get
- _retrieveOnUpdateAndMerge
- add
- consolidateValidationResults static
- count
- deleteById
- deleteSingle
- get
- getAll
- getAllWithCount
- getContentSanitizationRules static
- getContext
- getDate static
- getDefaultSanitizationRules static
- getIdWhere
- getLimit static
- getPagedResult static
- getSingle
- getType
- listeners static
- notFound static deprecated
- on static
- once static
- parseBoolean static
- removeAllListeners static
- removeListener static
- sanitize static
- save
- setDefaultSanitizationRules static
- setDefaultSanitizationRules static
- setMaxListeners static
- update
- validationError static
- validationFailure static
Properties
- AFTER_DELETE static
- AFTER_SAVE static
- BEFORE_COUNT static
- BEFORE_DELETE static
- BEFORE_GET static
- BEFORE_GET_ALL static
- BEFORE_SAVE static
- ContentSanitizationRulesOverride static
- dao
- DefaultSanitizationRulesOverride static
- FORMAT static
- GET_ALL static
- GET_ALL static
- MAX_RESULTS static
- MERGE static
- type
- VALIDATE static
Methods
_emit
-
event
-
data
-
cb
Parameters:
-
event
String -
data
Object -
cb
Function
_get
-
id
-
[options]
-
cb
Parameters:
-
id
String -
[options]
Object optional -
cb
Function
_retrieveOnUpdateAndMerge
-
dto
-
options
-
cb
When an ID is available in the DTO the function attempts to retrieve the existing object. If it is not available a new object is created. The merge event is then called. After the merge is complete the callback is executed with the merged object.
Parameters:
-
dto
Object -
options
Object-
[isCreate]
Boolean optional
-
-
cb
Function
add
-
dto
-
[options]
-
cb
Attempts to persist the DTO as an add. The function executes a series of events:
- The format event is fired
- When an ID is provided the object is retrieved from the database otherwise a new object is created.
- The merge event is triggered
- The validate event is triggered. If validation errors are detected the process halts and the function calls back with an error.
- The beforeSave event is triggered
- The object is persisted
- The afterSave event is triggered
Parameters:
-
dto
Object -
[options]
Object optional -
cb
FunctionA callback that takes two parameters. The first is an error, if occurred. The second is the object that matches the specified query
consolidateValidationResults
-
results
Inspects the raw set of validation results to ensure that plugins that don't follow proper procedure have their results excluded.
Parameters:
-
results
Array
Returns:
count
-
[options]
-
cb
Executes a count of resources against the persistence layer. The function will callback with an array of results.
Parameters:
-
[options]
Object optional-
[where]
Object optional
-
-
cb
FunctionA callback that takes two parameters. The first is an error, if occurred. The second is the number of results that match the specified query
deleteById
-
id
-
options
-
cb
Deletes an object by ID
Parameters:
-
id
String -
options
Object -
cb
Function
deleteSingle
-
[options]
-
cb
Deletes a single item based on the specified query in the options
Parameters:
-
[options]
Object optionalSee BaseObjectService#getSingle
-
[where]
Object optional
-
-
cb
Function
get
-
id
-
[options]
-
cb
Retrieves a resource by ID. The function will callback with the object that was found or NULL if no object could be found. The function will trigger the "get" event.
Parameters:
-
id
String -
[options]
Object optional -
cb
Function(Error, object|null) A callback that takes two parameters. The first is an error, if occurred. The second is the object with the specified ID
getAll
-
[options]
-
cb
Executes a query for resources against the persistence layer. The function will callback with an array of results. The function will trigger the "getAll" event. Also note that there is hard limit on the number of results the returned.
Parameters:
-
[options]
Object optional-
[select]
Object optional -
[where]
Object optional -
[order]
Array optional -
[limit]
Integer optional -
[offset]
Integer optional
-
-
cb
FunctionA callback that takes two parameters. The first is an error, if occurred. The second is an array representing the results of the query.
getAllWithCount
-
[options]
-
cb
Executes a query for resources against the persistence layer. The function will callback with an object that contains a total count and an array of results. The function will trigger the "getAll" event. Also note that there is hard limit on the number of results the returned.
Parameters:
-
[options]
Object optional-
[select]
Object optional -
[where]
Object optional -
[order]
Array optional -
[limit]
Integer optional -
[offset]
Integer optional
-
-
cb
FunctionA callback that takes two parameters. The first is an error, if occurred. The second is an object representing the results of the query.
getContentSanitizationRules
()
Object
static
The sanitization rules that apply to Pages, Articles, and other fields that are allowed to have HTML
Returns:
getContext
-
[data]
Retrieves a context object to be passed to event listeners
Parameters:
-
[data]
String | Object | Number | Boolean optional
Returns:
getDate
-
dateStr
Parses an ISO date string. When an invalid date string is pass a NULL value is returned.
Parameters:
-
dateStr
String
Returns:
getDefaultSanitizationRules
()
Object
static
Retrieves the default sanitization rules for string fields.
Returns:
getIdWhere
-
dto
Creates the where clause that creates a lookup by the key that indicates uniqueness for the collection
Parameters:
-
dto
Object
Returns:
getLimit
-
limit
Determines the maximum number of results that can be returned for a query. The specified limit must be a positive integer. The result will be the minimum of the MAX_RESULTS constant and the specified limit.
Parameters:
-
limit
Integer
Returns:
getPagedResult
-
dataArray
-
total
-
[limit]
-
[offset]
Builds a paged result object
Parameters:
-
dataArray
ArrayThe array of items to return
-
total
IntegerThe total number of items available in the collection
-
[limit]
Integer optionalThe maximum number of items to return
-
[offset]
Integer optionalThe number of items skipped
getSingle
-
[options]
-
cb
Retrieves a single resource by the specified query. The function will callback with the object that was found or NULL if no object could be found. The function will trigger the "getAll" event.
Parameters:
-
[options]
Object optional-
[select]
Object optional -
[where]
Object optional -
[order]
Array optional -
[offset]
Integer optional
-
-
cb
FunctionA callback that takes two parameters. The first is an error, if occurred. The second is the object that matches the specified query
getType
()
String
Retrieves the object type supported by the service
Returns:
The object type supported
listeners
-
event
Returns a list of the listeners for the specified event
Parameters:
-
event
String
Returns:
notFound
-
[message]
Creates a new Error representative of the inability to locate the requested resource
Parameters:
-
[message]
String optional
Returns:
on
-
event
-
listener
Registers a listener for the specified event.
Parameters:
-
event
String -
listener
Function
Returns:
once
-
event
-
listener
Registers a listener to fire a single time for the specfied event
Parameters:
-
event
String -
listener
Function
Returns:
parseBoolean
-
val
Extracts a boolean value from the provided value. Null or undefined values will return false. Strings of '1' or 'true' (case sensitive) will return TRUE. All other values will return false.
Parameters:
-
val
String | Boolean
Returns:
removeAllListeners
-
event
Removes all listeners for the specified event
Parameters:
-
event
String
Returns:
removeListener
-
event
-
listener
Removes the listener from the specified event
Parameters:
-
event
String -
listener
Function
Returns:
sanitize
-
value
-
[config]
Strips HTML formatting from a string value
Parameters:
-
value
String -
[config]
Object optional
Returns:
save
-
dto
-
[options]
-
cb
Attempts to persist the DTO. The function executes a series of events:
- The format event is fired
- When an ID is provided the object is retrieved from the database otherwise a new object is created.
- The merge event is triggered
- The validate event is triggered. If validation errors are detected the process halts and the function calls back with an error.
- The beforeSave event is triggered
- The object is persisted
- The afterSave event is triggered
Parameters:
-
dto
Object -
[options]
Object optional-
[isCreate]
Boolean optional
-
-
cb
FunctionA callback that takes two parameters. The first is an error, if occurred. The second is the object that matches the specified query
setDefaultSanitizationRules
-
rules
Sets a system-wide override for the default set of sanitization rules. The function will throw if a non-object is passed. NULL can be used to reset any override that is put into place.
Parameters:
-
rules
Object
setDefaultSanitizationRules
-
rules
Sets a system-wide override for the set of sanitization rules for content. The function will throw if a non-object is passed. NULL can be used to reset any override that is put into place.
Parameters:
-
rules
Object
setMaxListeners
-
n
Sets the maximum number of listeners for the emitter
Parameters:
-
n
Integer
Returns:
update
-
dto
-
[options]
-
cb
Attempts to persist the DTO as an update. The function executes a series of events:
- The format event is fired
- When an ID is provided the object is retrieved from the database otherwise a new object is created.
- The merge event is triggered
- The validate event is triggered. If validation errors are detected the process halts and the function calls back with an error.
- The beforeSave event is triggered
- The object is persisted
- The afterSave event is triggered
Parameters:
-
dto
Object -
[options]
Object optional -
cb
FunctionA callback that takes two parameters. The first is an error, if occurred. The second is the object that matches the specified query
validationError
-
validationFailures
Creates a new Error representative of a validation error
Parameters:
-
validationFailures
Array | String
Returns:
validationFailure
-
field
-
message
-
[code]
Creates a properly formed validation failure
Parameters:
-
field
String -
message
String -
[code]
String optional
Properties
AFTER_DELETE
String
static
The event that is triggered when the delete function is called and after the delete operation has completed successfully.
AFTER_SAVE
String
static
The event that is triggered when a DTO is passed to the save function and after the save operation has completed successfully.
BEFORE_COUNT
String
static
The event that is triggered before the count query is executed
BEFORE_DELETE
String
static
The event that is triggered when the delete function is called.
BEFORE_GET
String
static
The event that is triggered before the query is executed to retrieve an item by ID
BEFORE_GET_ALL
String
static
The event that is triggered before the query is executed to retrieve results
BEFORE_SAVE
String
static
The event that is triggered when a DTO is passed to the save function and aftr the validate event has completed. When validation failures occur this event will not fire.
ContentSanitizationRulesOverride
Object | Null
static
DefaultSanitizationRulesOverride
Object | Null
static
FORMAT
String
static
The event that is triggered when a DTO is passed to the save function
GET_ALL
String
static
The event that is triggered when retrieving a resource by ID
GET_ALL
String
static
The event that is triggered when querying for one or more resources
MAX_RESULTS
Integer
private
static
The maximum allowed number of results allowed to be returned when using the paging wrapper
MERGE
String
static
The event that is triggered when a DTO is passed to the save function and after the format event has completed
type
String
Represents the name of the collection to interact with
VALIDATE
String
static
The event that is triggered when a DTO is passed to the save function and after the merge event has completed