Greetings! I am currently facing an issue while trying to integrate the "sequelize-typescript" library into my Express.JS REST API that I developed using Dependency Injection. The error I am encountering is:
SQLite Error: no such table: Users
Below is the code snippet for my "user.ts" entity:
export class User extends Model {
@PrimaryKey
@Column({
type: DataType.INTEGER,
autoIncrement: true
})
declare userId: number
@Column({
type: DataType.STRING
})
declare firstName : string;
@Column({
type: DataType.STRING
})
declare lastName: string;
@Column({
type: DataType.STRING
})
declare email: string;
@Column({
type: DataType.STRING
})
declare phone: string;}
Additionally, here is the middleware code for my database setup:
export default class DatabaseMiddleware {
private databaseInstance : Sequelize;
constructor() {
this.databaseInstance = new Sequelize({
database: process.env.MAMLAKHA_DATABASE_NAME,
dialect: "sqlite",
host: process.env.MAMLAKHA_DATABASE_HOST,
username: process.env.MAMLAKHA_DATABASE_USERNAME,
password: process.env.MAMLAKHA_DATABASE_PASSWORD,
// storage: process.env.MAMLAKHA_DATABASE_STORAGE_PATH,
logging(sql, timing) {
console.log("The SQL statement from Sequelize executed is", sql, timing);
},
models: [User],
repositoryMode: true
});
}
public async connectToDatabase() {
try {
await this.databaseInstance.authenticate();
if(process.env.ENVIRONMENT_PROFILE === "development") {
await this.databaseInstance.sync({ alter: true });
}
console.log("Connection to database has been established successfully");
} catch(error) {
console.error("Unable to connect to database due to", error);
}
}
public getDatabaseInstance() {
return this.databaseInstance;
}}
Lastly, here is the initialization code for the entire REST API:
class App {
private baseDirectory : string;
private expressApp : Express
// Middleware Instances
private databaseMiddleware : DatabaseMiddleware;
constructor() {
dotenv.config({ path: "./.env.development" });
this.baseDirectory = __dirname;
this.expressApp = express();
routingContainer(Container);
useExpressServer(this.expressApp, {
routePrefix: process.env.MAMLAKHA_ROUTE_PREFIX,
defaultErrorHandler: false,
controllers: [this.baseDirectory + `/**/controllers/*.ts`]
});
this.expressApp.use(bodyParser.urlencoded({ extended: false }));
this.expressApp.use(bodyParser.json());
this.databaseMiddleware = new DatabaseMiddleware();
}
run() {
this.expressApp.listen(process.env.MAMLAKHA_REST_API_PORT, () => {
console.info(`Hello there, it's Mamlakha app and it's running on port ${process.env.MAMLAKHA_REST_API_PORT} 🤘`);
});
this.databaseMiddleware.connectToDatabase();
}
get expressAppInstance : Express {
return this.expressApp;
} } const main : App = new App(); main.run();
I have attempted to resolve the issue following the Github documentation for the library, but unfortunately, I am unable to make it work. If you have encountered this issue before, I would greatly appreciate any solutions you can provide. Thank you!