sub-manager-backend/objects/Entity.mjs

44 lines
1.1 KiB
JavaScript
Raw Normal View History

2023-09-07 09:39:09 +00:00
export default class Entity{
2023-09-07 10:07:34 +00:00
set _id(prop){
if(prop){
2023-09-14 14:06:17 +00:00
const propNumber = Number.parseInt(prop)
console.log("PropNumber: "+propNumber)
if(Number.isNaN(propNumber)){throw new TypeError("id must be an integer!")}
2023-09-11 22:00:25 +00:00
this.id = propNumber
2023-09-07 09:39:09 +00:00
}
}
2023-09-07 10:07:34 +00:00
constructor(data){
this._id = data?.id
}
2024-02-16 10:24:45 +00:00
async updateAppropriateJunctions(db,data){
if(typeof this.updateGenres === "function" ){
await this.updateGenres(db,data.genres)
}
}
2023-09-07 09:39:09 +00:00
2024-02-16 10:24:45 +00:00
async insert(db,data){
this.id = await db(this.table)
2023-09-07 09:39:09 +00:00
.insert(this)
2024-02-16 10:24:45 +00:00
.returning("id")
await this.updateAppropriateJunctions(db,data)
2023-09-07 09:39:09 +00:00
}
2024-02-16 10:24:45 +00:00
async update(db,data){
await db(this.table)
2023-09-07 09:39:09 +00:00
.where('id',this.id)
.update(this)
2024-02-16 10:24:45 +00:00
await this.updateAppropriateJunctions(db,data)
2023-09-07 09:39:09 +00:00
}
2024-02-16 10:24:45 +00:00
async del(db,data){
2023-09-07 09:39:09 +00:00
if(!this?.id){throw new Error("cannot delete without an id!")}
2024-02-16 10:24:45 +00:00
await db(this.table)
2023-09-07 09:39:09 +00:00
.where('id',this.id)
2023-09-09 14:44:54 +00:00
.del()
2024-02-16 10:24:45 +00:00
//RUN JUNCTION OPS IF APPROPRIATE
2023-09-07 09:39:09 +00:00
}
}
2023-09-06 16:26:15 +00:00