typescriptexpresssqlitedependency-injectionsequelize-typescript

Error occured due to SQLITE_ERROR: no such table: Users but added the model into sequelize-typescript


Thanks for looking at my question. I'm trying to understand and implement "sequelize-typescript" library into my Express.JS REST API which I developed it using Dependency Injection style. But I've been encountering an issue which is this error:

Error occured due to SQLITE_ERROR: no such table: Users

Here's my code for my entity called "user.ts":

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;}

My database which I've created as a middleware:

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;
}}

Finally, here's the main code where the whole REST API starts:

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've tried following the documentation in GitHub for this library but I can't make it work and hence the error. Please share your solutions if you have encountered this issue


Solution

  • Thanks for looking into the question and to @RobertRendell for giving me a solution. There are 2 solutions that I found resolving my error. One of them is @RobertRendell's answer which creates the table first before running your application.

    Another solution is to have this sync({ alter: true, force: true }); on your Sequelize instance. The flag called force: true will create the table based on your data model if doesn't exists in your database.

    Once again thanks to @RobertRendell for his solution and hope my answer to my predicament will able to solve yours.