update junctions
This commit is contained in:
parent
b6f1a4aefd
commit
00aaf29a97
|
@ -16,7 +16,7 @@ const __dirname = path.dirname(__filename);
|
||||||
// })
|
// })
|
||||||
export default pino(
|
export default pino(
|
||||||
{
|
{
|
||||||
level: 'error',
|
level: 'fatal',
|
||||||
formatters: {
|
formatters: {
|
||||||
level: (label) => {
|
level: (label) => {
|
||||||
return { level: label.toUpperCase() };
|
return { level: label.toUpperCase() };
|
||||||
|
|
|
@ -64,7 +64,7 @@ export const postEndpoints = (db,data) => {
|
||||||
try {
|
try {
|
||||||
logger.trace({data:req.body},"POST request received")
|
logger.trace({data:req.body},"POST request received")
|
||||||
const entity = new Entity(req.body)
|
const entity = new Entity(req.body)
|
||||||
await entity[method](db)
|
await entity[method](db,data)
|
||||||
res.sendStatus(200)
|
res.sendStatus(200)
|
||||||
data.init()
|
data.init()
|
||||||
return
|
return
|
||||||
|
|
|
@ -13,21 +13,30 @@ export default class Entity{
|
||||||
this._id = data?.id
|
this._id = data?.id
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async updateAppropriateJunctions(db,data){
|
||||||
async insert(db){
|
if(typeof this.updateGenres === "function" ){
|
||||||
return db(this.table)
|
await this.updateGenres(db,data.genres)
|
||||||
.insert(this)
|
|
||||||
}
|
}
|
||||||
async update(db){
|
}
|
||||||
return db(this.table)
|
|
||||||
|
async insert(db,data){
|
||||||
|
this.id = await db(this.table)
|
||||||
|
.insert(this)
|
||||||
|
.returning("id")
|
||||||
|
await this.updateAppropriateJunctions(db,data)
|
||||||
|
}
|
||||||
|
async update(db,data){
|
||||||
|
await db(this.table)
|
||||||
.where('id',this.id)
|
.where('id',this.id)
|
||||||
.update(this)
|
.update(this)
|
||||||
|
await this.updateAppropriateJunctions(db,data)
|
||||||
}
|
}
|
||||||
async del(db){
|
async del(db,data){
|
||||||
if(!this?.id){throw new Error("cannot delete without an id!")}
|
if(!this?.id){throw new Error("cannot delete without an id!")}
|
||||||
return db(this.table)
|
await db(this.table)
|
||||||
.where('id',this.id)
|
.where('id',this.id)
|
||||||
.del()
|
.del()
|
||||||
|
//RUN JUNCTION OPS IF APPROPRIATE
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
import Entity from "./Entity.mjs";
|
import Entity from "./Entity.mjs";
|
||||||
import dataValidation from "./dataValidation.mjs";
|
import dataValidation from "./dataValidation.mjs";
|
||||||
|
import logger from "../logger.mjs";
|
||||||
//THIS CLASS WILL HANDLE JUNCTION TABLE STUFF
|
//THIS CLASS WILL HANDLE JUNCTION TABLE STUFF
|
||||||
export default class Title extends Entity {
|
export default class Title extends Entity {
|
||||||
#genres
|
#genres
|
||||||
|
@ -35,37 +36,33 @@ export default class Title extends Entity{
|
||||||
|
|
||||||
async updateGenres(db, genres) {
|
async updateGenres(db, genres) {
|
||||||
const table = this.table + '_genres'
|
const table = this.table + '_genres'
|
||||||
const relevantEntries = await db(table)
|
await genres.forEach(async (e, i) => {
|
||||||
.select('*')
|
//INSERT TRUE
|
||||||
|
const genreName = genres[i]
|
||||||
|
if (this.#genres[genreName]) {
|
||||||
|
await db(table).whereNotExists(
|
||||||
|
db.select('*')
|
||||||
|
.from(table)
|
||||||
.where(this.idName, this.id)
|
.where(this.idName, this.id)
|
||||||
//DELETE FALSES
|
.andWhere('genre_id', i)
|
||||||
const truesWithEntry= []
|
)
|
||||||
for (const entry of relevantEntries) {
|
|
||||||
const genreName = genres[entry.genre_id]
|
|
||||||
if(this.#genres[genreName]===false){
|
|
||||||
await db(table)
|
|
||||||
.where('id',entry.id)
|
|
||||||
.del()
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
truesWithEntry.push(entry.genre_id)
|
|
||||||
}
|
|
||||||
//INSERT TRUES
|
|
||||||
for (const genre in this.#genres){
|
|
||||||
if(truesWithEntry.includes(genre)){continue}
|
|
||||||
await db(table)
|
|
||||||
.insert({
|
.insert({
|
||||||
[this.idName]: this.id,
|
[this.idName]: this.id,
|
||||||
genre_id://GET GENRE ID???!!!
|
genre_id: i
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
//DELETE FALSE
|
||||||
|
const res = await db(table)
|
||||||
|
.where(this.idName, this.id)
|
||||||
|
.andWhere('genre_id', i)
|
||||||
|
.del()
|
||||||
|
console.log("RES: "+res)
|
||||||
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
BIN
submissions
BIN
submissions
Binary file not shown.
Loading…
Reference in New Issue