Using Sqlite3 from Electron's main package, I am able to create tables, insert rows, and read those rows. How do I instantiate rows read into defined classes? Example:
export const GetAllObjs = (): Obj[] => {
const query = db.prepare("SELECT * FROM ObjTable");
const rows= query.all();
let objs = [] as Obj[];
for(const row in rows as Obj[]){
// This object only contains the index number...
// const obj= JSON.parse(row) as Obj;
// This does not seem to work...
objs.push(
{
// How do you access the value of each column, such as row.[columnName]?
id: row.id,
name: row.name
amount: row.amount,
} as Obj
);
}
return objs;
};
// Example table creation (I have verified this works with DBeaver)
export const CreateObjTable = () => {
const query = db.prepare(`
CREATE TABLE IF NOT EXISTS ObjTable
(
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT,
amount INTEGER
)
`);
return query.run();
};
Your advice would be greatly appreciated. It seems odd to me this isn't more clearly spelled out.