sub-manager-backend/objects/Entity.mjs

33 lines
669 B
JavaScript
Raw Normal View History

import dv from "./dataValidation.mjs"
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){
if(!Number.isInteger(prop)){throw new TypeError("id must be an integer!")}
this.id = prop
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)
this.del()
}
}
2023-09-06 16:26:15 +00:00