Author.ts
import {Table, Model, Column, DataType} from 'sequelize-typescript'
@Table
export class Author extends Model<Author> {
constructor(){
super();
}
@Column(DataType.STRING)
fname: string
@Column(DataType.STRING)
lname: string
}
App.ts
import {Sequelize} from 'sequelize-typescript';
import { customer } from './model/customer';
import { Author } from './model/Author';
export class SQLRepo {
repo:Sequelize = null;
constructor(){
this.connectDb();
}
connectDb(): any {
this.repo = new Sequelize({
database: 'postgres',
dialect: 'postgres',
host: 'localhost',
username: 'postgres',
password: 'xxxxx',
storage:
'D:/Sequalize/assosiation/database.sqlite',
modelPaths: [__dirname + '/models'],
define: {
underscored: false,
freezeTableName: false,
timestamps: false
}
});
this.repo.addModels([Author])
this.repo.
authenticate().
then(function(){
console.log("database connected ...")
new Author({fname:'karim', lname:'mirazul'}).save();
}).
catch(function(error){
console.log("Database catch block : "+ error)
})
}
}
new SQLRepo();
Create a SQLRepo call object to successfully establish a connection with the database, however an error occurs after DB connection is established when trying to insert data into the Author table.
The following output is displayed:
Executing (default): SELECT 1+1 AS result database connected ... Database catch block :
Error: Model not initialized: "Model" needs to be added to a Sequelize instance before "call" can be executed.