sub-manager-backend/objects/Entity.mjs

33 lines
683 B
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-11 22:00:25 +00:00
const propNumber = Number(prop)
if(!Number.isInteger(propNumber)){throw new TypeError("id must be an integer!")}
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
}
2023-09-07 09:39:09 +00:00
async insert(db){
return db(this.table)
.insert(this)
}
async update(db){
return db(this.table)
.where('id',this.id)
.update(this)
}
async del(db){
if(!this?.id){throw new Error("cannot delete without an id!")}
return db(this.table)
.where('id',this.id)
2023-09-09 14:44:54 +00:00
.del()
2023-09-07 09:39:09 +00:00
}
}
2023-09-06 16:26:15 +00:00