Class

RawBaseService

RawBaseService(options)

Constructor

# new RawBaseService(options)

Create a service instance.

Parameters:
Name Type Description
options Object
repository Object
entityClass function

View Source src/common/BaseService.js, line 119

Methods

# async activate(id, optsopt)

Activate a record by its ID. Throws if not found.

Parameters:
Name Type Attributes Description
id string | number
opts Object <optional>
trx Object <optional>

View Source src/common/BaseService.js, line 478

# async assertExists(where, optsopt)

Assert that at least one record exists for a where clause. Throws an error if not found.

Parameters:
Name Type Attributes Description
where Object
opts Object <optional>
trx Object <optional>

View Source src/common/BaseService.js, line 441

# async create(data, optsopt)

Create a record. Accepts either a POJO or an Entity instance.

Parameters:
Name Type Attributes Description
data Object
opts Object <optional>
trx Object <optional>

View Source src/common/BaseService.js, line 372

# async cursorPage(where, order, opts)

Cursor-based pagination passthrough. Defers to repository.cursorPage where the actual implementation will live. Returns { results, nextCursor, prevCursor } when implemented in repository.

Parameters:
Name Type Attributes Default Description
where Object
order Array
opts Object
limit number <optional>
20
cursor string | null <optional>
null
trx Object <optional>

View Source src/common/BaseService.js, line 545

# async deactivate(id, optsopt)

Deactivate a record by its ID. Throws if not found.

Parameters:
Name Type Attributes Description
id string | number
opts Object <optional>
trx Object <optional>

View Source src/common/BaseService.js, line 502

# async delete(id, optsopt)

Delete a record by its ID. Returns the number of rows deleted.

Parameters:
Name Type Attributes Description
id string | number
opts Object <optional>
trx Object <optional>

View Source src/common/BaseService.js, line 395

# async exists(where, optsopt)

Check if a record exists for a where clause. Returns true or false.

Parameters:
Name Type Attributes Description
where Object
opts Object <optional>
trx Object <optional>

View Source src/common/BaseService.js, line 430

# async getActive(whereopt, optsopt)

Get active records, optionally filtered by additional WHERE criteria. Returns an array of Entity instances.

Parameters:
Name Type Attributes Default Description
where Object <optional>
{}

Additional WHERE clause to filter results

opts Object <optional>
trx Object <optional>

View Source src/common/BaseService.js, line 456

# async getAll(optsopt)

Get all records. Returns an array of Entity instances.

Parameters:
Name Type Attributes Description
opts Object <optional>
trx Object <optional>

View Source src/common/BaseService.js, line 325

# async getById(id, optsopt)

Get a record by its ID. Returns an Entity instance or null.

Parameters:
Name Type Attributes Description
id string | number
opts Object <optional>
trx Object <optional>

View Source src/common/BaseService.js, line 357

# getDefaultGraphName() → {string}

Get the default graph name for expanded queries. Override in subclasses to provide a different default.

View Source src/common/BaseService.js, line 150

Default graph name.

string

# getGraphExpression(name) → {string}

Get the graph expression for a named graph. Override in subclasses to provide named graphs.

Parameters:
Name Type Description
name string

Name of the graph.

View Source src/common/BaseService.js, line 160

Graph expression.

string

# getGraphModifiers(name) → {Object.<string, function()>|undefined}

Get the graph modifiers for a named graph. Override in subclasses to provide named graph modifiers.

Parameters:
Name Type Description
name string

Name of the graph.

View Source src/common/BaseService.js, line 170

Graph modifiers.

Object.<string, function()> | undefined

# async getOne(where, optsopt)

Get a single record matching a where clause. Returns an Entity instance or null.

Parameters:
Name Type Attributes Description
where Object
opts Object <optional>
trx Object <optional>

View Source src/common/BaseService.js, line 236

# async getOneExpanded(where, optsopt) → {Promise.<*>}

Get a single record matching a where clause, including related data. Returns an Entity instance or null.

Parameters:
Name Type Attributes Default Description
where Object
opts Object <optional>
graphName string <optional>

Name of the graph to use.

includeHiddenFields boolean <optional>
false

Whether to include hidden fields.

trx Object <optional>

Knex transaction.

View Source src/common/BaseService.js, line 184

Entity instance or null

Promise.<*>

# async getOneWithRelations(where, graph, optsopt) → {Promise.<*>}

Get a single record matching a where clause, including related data. Returns an Entity instance or null.

Parameters:
Name Type Attributes Description
where Object
graph string
opts Object <optional>
trx Object <optional>

View Source src/common/BaseService.js, line 261

Entity instance or null

Promise.<*>
Example
const user = await userService.getOneWithRelations(
    { id: 1 },
    'account, roles',
    { trx }
  );
  console.log(user.account, user.roles);

# async getWhere(where, optsopt)

!IMPORTANT: This method returns a maximum of 1000 records as a sanity check. If you need more, use paginate function instead. Get records matching a where clause. Returns an array of Entity instances.

Parameters:
Name Type Attributes Description
where Object
opts Object <optional>
trx Object <optional>

View Source src/common/BaseService.js, line 342

# async getWhereExpanded(where, optsopt) → {Promise.<Array.<*>>}

Get records matching a where clause, including related data. Returns an array of Entity instances.

Parameters:
Name Type Attributes Default Description
where Object
opts Object <optional>
graphName string <optional>

Name of the graph to use.

includeHiddenFields boolean <optional>
false

Whether to include hidden fields.

trx Object <optional>

Knex transaction.

View Source src/common/BaseService.js, line 205

Array of Entity instances

Promise.<Array.<*>>

# async getWhereWithRelations(where, graph, optsopt) → {Promise.<Array.<*>>}

Get records matching a where clause, including related data. Returns an array of Entity instances.

Parameters:
Name Type Attributes Description
where Object
graph string
opts Object <optional>
trx Object <optional>

View Source src/common/BaseService.js, line 287

Array of Entity instances

Promise.<Array.<*>>
Example
const users = await userService.getWhereWithRelations(
    { isActive: true },
    'account, roles',
    { trx }
  );
  console.log(users[0].account, users[0].roles);

# isEntity(val) → {boolean}

Determine whether a value looks like one of our Entity instances. Uses presence of toJSON, hiddenFields marker, or class name suffix.

Parameters:
Name Type Description
val *

View Source src/common/BaseService.js, line 133

boolean

# async paginate(where, page, pageSize, optsopt)

Paginate records using the repository's offset/limit paging. Returns { results, total, page, pageSize, totalPages } with results as Entities.

Parameters:
Name Type Attributes Default Description
where Object
page number 1

1-based page number

pageSize number 10
opts Object <optional>
trx Object <optional>

View Source src/common/BaseService.js, line 311

# toPlain(val) → {*}

Convert an Entity to a POJO using toJSON, otherwise return the value unchanged. Dates inside entity.toJSON() are expected to be serialized consistently.

Parameters:
Name Type Description
val *

View Source src/common/BaseService.js, line 222

*

# async toggleActive(id, optsopt)

Toggle the active status of a record by its ID. Throws if not found.

Parameters:
Name Type Attributes Description
id string | number
opts Object <optional>
trx Object <optional>

View Source src/common/BaseService.js, line 526

# async update(id, data, optsopt)

Update a record by its ID. Accepts either a POJO or an Entity instance for the update data.

Parameters:
Name Type Attributes Description
id string | number
data Object
opts Object <optional>
trx Object <optional>

View Source src/common/BaseService.js, line 384

# async upsert(data, whereClauseopt, optsopt)

Upsert a record based on a where clause. Accepts either a POJO or an Entity instance.

Parameters:
Name Type Attributes Default Description
data Object
whereClause Object <optional>
{}
opts Object <optional>
trx Object <optional>

View Source src/common/BaseService.js, line 419