sequelize#ModelCtor TypeScript Examples

The following examples show how to use sequelize#ModelCtor. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ModelBuilder.ts    From aloxide with Apache License 2.0 5 votes vote down vote up
build(sequelize: Sequelize): ModelCtor<Model>[] {
    return this.aloxideConfig.entities.map(entityConfig =>
      ModelBuilder.makeModelFromEntityConfig(sequelize, this.typeInterpreter, entityConfig),
    );
  }
Example #2
Source File: ModelBuilder.ts    From aloxide with Apache License 2.0 5 votes vote down vote up
static makeModelFromEntityConfig(
    sequelize: Sequelize,
    typeInterpreter: Interpreter<Field, DataType>,
    entityConfig: EntityConfig,
  ): ModelCtor<Model> {
    const { name, fields, key } = entityConfig;
    return sequelize.define(name, ModelBuilder.mapField(typeInterpreter, fields, key));
  }
Example #3
Source File: PluginSqlizeQuery.ts    From expresso with MIT License 5 votes vote down vote up
/**
 *
 * @param model
 * @param prefixName
 * @returns
 */
function getFilteredQuery(model?: ModelCtor<any>, prefixName?: string): any {
  const sequelizeQuery = new SqlizeQuery()
  sequelizeQuery.addValueParser(parserString)
  sequelizeQuery.addQueryBuilder(
    (filterData: { id: string; value: any }, queryHelper) => {
      const { id, value } = filterData || {}
      const curId = getExactQueryIdModel(id, prefixName)
      if (!curId) {
        return
      }

      const type = typeof getPrimitiveDataType(
        model?.rawAttributes?.[curId]?.type
      )

      // check not number
      if (type !== 'number') {
        // check value uuid
        if (uuidValidate(value)) {
          queryHelper.setQuery(curId, {
            [Op.eq]: value,
          })
        } else if (DB_CONNECTION === 'postgres') {
          // check connection postgress case sensitive
          queryHelper.setQuery(curId, {
            [Op.iLike]: `%${value}%`,
          })
        } else {
          // default not postgres
          queryHelper.setQuery(curId, {
            [Op.like]: `%${value}%`,
          })
        }
      } else {
        // default number
        queryHelper.setQuery(
          curId,
          curId.endsWith('Id')
            ? value
            : {
                [Op.like]: `%${value}%`,
              }
        )
      }
    }
  )
  return sequelizeQuery
}