I’ve tried everything I know.
I’m essentially trying to do what would be a column select and set the column value to an array. What would be the easiest way of achieving this?
import { Table, Column, Model, ForeignKey, BelongsTo, HasOne, HasMany, DataType, AfterCreate, AfterInit, AfterFind } from 'sequelize-typescript';
import Competitor from './competitor.model';
import ProductRef from '../models/product-ref.model';
import sequelize from 'sequelize';
import Database from '../db/mysql';
@Table({ tableName: 'model_b', timestamps: true })
export default class ModelB extends Model<ModelB> {
@Column({ primaryKey: true, autoIncrement: true })
id!: number;
@Column({
defaultValue: sequelize.literal('now()')
})
createdAt!: Date;
@Column({
defaultValue: sequelize.literal('now()')
})
updatedAt!: Date;
// Here is where I want to run a sub query, Something like "select * from table where col like value. And then create an array of these rows.
@Column({
type: DataType.VIRTUAL,
get() {
return this.getDataValue('modelAs');
},
set(value) {
this.setDataValue('modelAs', value);
}
})
modelAs: ModelA[] = [];
@AfterInit
static afterInitHook(instance: ModelA): void {
}
@AfterFind
static afterInitHook(instances: ModelB[]): void {
//Here this returns all instances of the "ModelB". This makes me think what's the point in even having the AfterFind hook at all. I thought the whole point of hooks was to manipulate instance vars of "this" when certain event hooks fire.
}
}