My task involves defining a function named FieldSearch
with specific parameters:
fieldSearch<SpecificModel extends Model>(
model: ModelStatic<SpecificModel>,
// Struggling with this part
fields: Array< attributes of the static model provided above >,
search: string // a search query parameter for the fields
): Array<SpecificModel>
Due to the varying attributes of different models and no single generic model, I am uncertain about the appropriate way to type the function for accurate validation.
For instance, my User model is structured as follows:
import {
CreationOptional,
DataTypes,
InferAttributes,
InferCreationAttributes,
Model,
} from 'sequelize';
import sequelize from '../sequelize';
export default class User extends Model<
InferAttributes<User, {}>,
InferCreationAttributes<User, {}>
> {
// id may be undefined during creation with `autoIncrement`
declare id: CreationOptional<number>;
declare firstName: string;
declare lastName: string;
// createdAt may be undefined during creation
declare createdAt: CreationOptional<Date>;
// updatedAt may be undefined during creation
declare updatedAt: CreationOptional<Date>;
// deletedAt is only defined after the row is soft-deleted
declare deletedAt: CreationOptional<Date>;
}