#Model not initialized:

16 messages · Page 1 of 1 (latest)

pallid lance
#

Hello everyone!

I have my app divided by 3 "main" modules, they are:

DataBaseModule - Here we have Sequelize instance
ModelsModule - Here we have the sequelize models
RepositoriesModule - Here we have the repositories do manage sequelize entities

I am trying to get "UserRepository" from RepositoriesModule and take any action like findAll but I am getting this error:
Model not initialized: Member "findAll" cannot be called. "User" needs to be added to a Sequelize instance.

There are anything wrong in my flow?

stray bolt
#

You're probably missing something like autoLoadEntities from the sequelize forRoot registration

pallid lance
#

I am registering my Sequelize instance with useFactory method which I got in NestJS Recipes. Should I use @unkempt swan/sequelize forRoot?

rain obsidian
#

Can you show some code?

pallid lance
#

This is the structure

#
  {
    provide: 'Sequelize',
    useFactory: async () => {
      const sequelize = new Sequelize({
        dialect: 'mysql',
        repositoryMode: true,
        host: process.env.DB_HOST,
        port: Number(process.env.DB_PORT),
        username: process.env.DB_USER,
        password: process.env.DB_PASS,
        database: process.env.DB_DATABASE,
        timezone: '-03:00',
        modelPaths: [
          __dirname + '/../models/schemas',
          __dirname + '/../models/association',
        ],
        modelMatch: (filename, member) => {
          return (
            filename.substring(0, filename.indexOf('.model')) ===
            member.toLowerCase()
          );
        },
        pool: {
          max: 5,
          min: 0,
          acquire: 30000,
          idle: 10000,
        },
      });
      await sequelize.sync();
      return sequelize;
    },
  },
];
#
import { ModelsModule } from 'src/models/models.module';
import { DatabaseProviders } from './database.providers';

@Module({
  imports: [ModelsModule],
  providers: [...DatabaseProviders],
  exports: [...DatabaseProviders, ModelsModule],
})
export class DatabaseModule {}
#
import Usuario from './schemas/usuario.model';
import Empresa from './schemas/empresa.model';
import Endereco from './schemas/endereco.model';

// Associations
import EnderecoEmpresa from './association/endereco.empresa.model';

export const ModelsProvider = [
  {
    provide: 'UsuarioModel',
    useValue: Usuario,
  },
  {
    provide: 'EmpresaModel',
    useValue: Empresa,
  },
  {
    provide: 'EnderecoModel',
    useValue: Endereco,
  },
  {
    provide: 'EnderecoEmpresaModel',
    useValue: EnderecoEmpresa,
  },
];
#
import { UsuarioRepository } from './UsuarioRepository';

export const RepositoryProvider = [
  {
    provide: 'UsuarioRepository',
    useClass: UsuarioRepository,
  } as Provider,
];
#

In my service I just Inject my repository

rain obsidian
#

What does UsuarioRepository look like?

pallid lance
#
import { BaseRepository } from './BaseRepository';

import Usuario from 'src/models/schemas/usuario.model';
import { ModelCtor } from 'sequelize-typescript';

@Injectable({})
export class UsuarioRepository extends BaseRepository<Usuario> {
  constructor(@Inject('UsuarioModel') model: ModelCtor<Usuario>) {
    super(model);
  }
}
pallid lance
#

Does anyone knows the solution ?

rain obsidian
#

I'll probably have an answer when I get back to my computer

#

Went out for a walk and some air

rain obsidian
#

Okay, so, is there a reason you're not using @nestjs/sequelize? Mainly just out of curiosity, because it ends up helping set up a lot of these providers for you