I just started using typeorm and ran into a major issue. Neither conection.sychronize()
nor repository.save(foo)
actually save changes to the database file. More precisly what is happening is that during runtine i can synchronize my db, save and read entities just fine. However when I close my program an run it again the db is empty. Not even the tables are present.
My entities
@Entity({name: "info"})
export class DBInfo {
@PrimaryGeneratedColumn()
id: number;
@Column("integer", { name: "gameCreation", unique: true })
gameCreation: number;
@Column("integer", { name: "gameDuration"})
gameDuration: number;
@Column("integer", { name: "gameId", unique: true })
gameId: number;
}
@Entity({name: "match"})
export class DBMatch {
@PrimaryGeneratedColumn()
id: number;
@OneToOne(() => DBInfo, {cascade: true})
@JoinColumn()
info: DBInfo;
}
I open the connection like so
let connectionTemp: Connection;
try{
connectionTemp = await createConnection({
type: "sqljs",
location: filePath,
entities: [DBMatch, DBInfo],
name: "MyConnection1"
});
console.log("DBReader", "connection open");
} catch (e){
console.log("DBReader", "could not open connection", e);
}
return connectionTemp;
then I synchronize the database, creating all the tables:
await this.connection.synchronize(false);
writing entities to the db like this:
let savedMatch = await this.MatchRepositoryV5.save(dbMatch);
I left some parts of the code out becasue I think its unnecessary.
After that I can retrieve the match from the db, however when restarting my programm the db is completly empty again. What am I missing?
CodePudding user response:
You need to set autoSave
connection property to true
So you init the connection as following:
let connectionTemp: Connection;
try{
connectionTemp = await createConnection({
type: "sqljs",
location: filePath,
autoSave: true,
entities: [DBMatch, DBInfo],
name: "MyConnection1"
});
console.log("DBReader", "connection open");
} catch (e){
console.log("DBReader", "could not open connection", e);
}
return connectionTemp;