Page Contents

Home > @loopback/sequelize > SequelizeCrudRepository

SequelizeCrudRepository class

Sequelize implementation of CRUD repository to be used with default loopback entities and SequelizeDataSource for SQL Databases

Signature:

export declare class SequelizeCrudRepository<T extends Entity, ID, Relations extends object = {}> implements EntityCrudRepository<T, ID, Relations> 

Implements: EntityCrudRepository<T, ID, Relations>

Constructors

Constructor Modifiers Description

(constructor)(entityClass, dataSource)

Constructs a new instance of the SequelizeCrudRepository class

Properties

Property Modifiers Type Description

dataSource

SequelizeDataSource

DB_SPECIFIC_SETTINGS_KEYS

readonly

readonly [“postgresql”, “mysql”, “sqlite3”]

Object keys used in models for set database specific settings. Example: In model property definition one can use postgresql dataType as float { type: 'number', postgresql: { dataType: 'float', precision: 20, scale: 4, }, }

This array of keys is used while building model definition for sequelize.

DEFAULT_ORDER_STYLE

readonly

(not declared)

Default order filter style if only column name is specified

defaultFnRegistry

protected

Record<string, unknown>

The alias registry for defaultFn option used in model property definition.

See: https://loopback.io/doc/en/lb4/Model.html#property-decorator

entityClass

typeof Entity & { prototype: T; }

inclusionResolvers

readonly

Map<string, InclusionResolver<T, Entity>>

NANO_ID_LENGTH

number

Length of the nanoid generated for defaultFn’s shortid and nanoid aliases.

sequelizeModel

ModelStatic<Model<T>>

Sequelize Model Instance created from the model definition received from the entityClass

Methods

Method Modifiers Description

beginTransaction(options)

buildSequelizeAttributeFilter(fields)

protected

Get Sequelize attributes filter value from fields of loopback.

buildSequelizeIncludeFilter(inclusionFilters, sourceModel)

protected

Build Sequelize compatible include filter

buildSequelizeOrder(order)

protected

Get Sequelize Order filter value from loopback style order value.

It also supports passing associations in the order array to sort by nested models. Example: ["user email ASC"].

buildSequelizeWhere(where)

protected

Build Sequelize compatible where condition object

count(where, options)

create(entity, options)

createAll(entities, options)

createBelongsToAccessorFor(relationName, targetRepositoryGetter)

protected

Function to create a belongs to accessor

createHasManyRepositoryFactoryFor(relationName, targetRepositoryGetter)

protected

Function to create a constrained relation repository factory

createHasManyThroughRepositoryFactoryFor(relationName, targetRepositoryGetter, throughRepositoryGetter)

protected

Function to create a constrained hasManyThrough relation repository factory

createHasOneRepositoryFactoryFor(relationName, targetRepositoryGetter)

protected

Function to create a constrained hasOne relation repository factory

createReferencesManyAccessorFor(relationName, targetRepoGetter)

protected

Function to create a references many accessor

delete(entity, options)

deleteAll(where, options)

deleteById(id, options)

excludeHiddenProps(entity)

protected

Remove hidden properties specified in model from response body. (See: https://github.com/sourcefuse/loopback4-sequelize/issues/3)

execute(command, parameters, options)

Execute a SQL command.

**WARNING:** In general, it is always better to perform database actions through repository methods. Directly executing SQL may lead to unexpected results, corrupted data, security vulnerabilities and other issues.

exists(id, _options)

find(filter, options)

findById(id, filter, options)

findOne(filter, options)

getDefaultFnRegistry()

protected

getSequelizeModel(entityClass)

Get Sequelize Model

getSequelizeModelAttributes(definition)

protected

Get Sequelize Model Attributes

getSequelizeOperator(key)

protected

Get Sequelize Operator

getTableName(entityClass)

This function retrieves the table name associated with a given entity class. Different loopback connectors have different conventions for picking up table names, unless the name is specified in the decorator.

The function follows the following cases to determine the table name: - It checks if the name property is specified in the decorator and uses it. (this takes precedence over all other cases) - If the dialect of the dataSource is PostgreSQL, it uses the lowercased version of the model class name. - If the dialect is MySQL or any other dialect, it uses the default model class name.

includeReferencesIfRequested(parentEntities, parentEntityClass, inclusionFilters)

protected

Include related entities of @referencesMany relation

referencesMany relation is NOT handled by sequelizeModel.findAll as it doesn’t have any direct alternative to it, so to include relation data of referencesMany, we’re manually fetching related data requested

isInclusionAllowed(include)

protected

Checks if the resolver of the inclusion relation is registered in the inclusionResolver of the current repository

registerInclusionResolver(relationName, resolver)

Register an inclusion resolver for the related model name.

replaceById(id, data, options)

save(entity, options)

syncLoadedSequelizeModels(options)

Run CREATE TABLE query for the all sequelize models, Useful for quick testing

syncSequelizeModel(options)

Run CREATE TABLE query for the target sequelize model, Useful for quick testing

toEntities(models)

protected

update(entity, options)

updateAll(data, where, options)

updateById(id, data, options)