From f2c639b4f21445fefb8e25ff9d65915e57eb2cf4 Mon Sep 17 00:00:00 2001 From: Andrzej Stepien Date: Mon, 13 Nov 2023 12:09:47 +0100 Subject: [PATCH] in memory db --- db.mjs | 8 +++++++- objects/Story.mjs | 3 +++ objects/Submission.mjs | 3 +++ objects/Title.mjs | 36 ++++++++++++++++++++++++++++++++++++ submissions | Bin 40960 -> 40960 bytes 5 files changed, 49 insertions(+), 1 deletion(-) diff --git a/db.mjs b/db.mjs index 02e644b..8f17058 100644 --- a/db.mjs +++ b/db.mjs @@ -14,4 +14,10 @@ export const db = knex({ filename: "./test.db" }, useNullAsDefault: true - }) \ No newline at end of file + }) + +const memoryDB = knex({ + client:'sqlite3', + connection: ':memory:' + }) + diff --git a/objects/Story.mjs b/objects/Story.mjs index c76ee63..4576e1b 100644 --- a/objects/Story.mjs +++ b/objects/Story.mjs @@ -11,6 +11,9 @@ export default class Story extends Title{ get table(){ return 'stories' } + get idName(){ + return 'story_id' + } constructor(data){ super(data) this._word_count=data?.word_count diff --git a/objects/Submission.mjs b/objects/Submission.mjs index d8c8491..da44d8a 100644 --- a/objects/Submission.mjs +++ b/objects/Submission.mjs @@ -38,6 +38,9 @@ export default class Submission extends Entity{ get table(){ return 'subs' } + get idName(){ + return 'sub_id' + } constructor(data){ super(data) this._story_id=data?.story_id diff --git a/objects/Title.mjs b/objects/Title.mjs index 6698c2a..40b4832 100644 --- a/objects/Title.mjs +++ b/objects/Title.mjs @@ -32,4 +32,40 @@ export default class Title extends Entity{ this._genres = data?.genres this._deleted = data?.deleted } + + async updateGenres(db,genres){ + const table = this.table+'_genres' + const relevantEntries = await db(table) + .select('*') + .where(this.idName,this.id) + //DELETE FALSES + 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({ + [this.idName]:this.id, + genre_id://GET GENRE ID???!!! + }) + } + + + + } + + + + + } \ No newline at end of file diff --git a/submissions b/submissions index 6325a64fdc7781ea6c7e4f000b4847a9efff6611..e164b8e2e65ed10d40fdd4a7e4204bd728a86621 100644 GIT binary patch delta 464 zcmZoTz|?SnX@az1C<6ln7Z7s*F%uA5Ow=)!4`t9xd&$cq$iTtzhk-AZ?=?>-_a-h^ zjz2sBV&lF|0xSZXSrY#63-I!NW#Hl4#?D{HAIyJ+f6-EQC+6fTlw_nTIA>($r0~CI zV3A`1sG&7bXg6*emtunVv< V0{QHlc?!PpPny62cG02<0szx(i(>!) delta 636 zcmaKo&ubG=5Xax{zU}^ScV`2ZiW+!W#4I$TR6!7|Z4xQ1hf z>0i)g?>+P);;joFghCN=>PZm1=*d&@;2{nQQQiB9<6yi2zfM~Nu|}we6_-=^9zqFY`j*`PxJPgUuS+0 zbB~8J`f$dW(rAU;_X75Uw+m5+EhdSTS*u#h)+$b;uRVneau*B#w~2jp$ieVuaa zOY!vAc)E8@Womj_*uFjVy}7kMGJWouQ}KLV<XM-8R6}Z1MXFRmDs%!lL1m;&DH4H{sEEXn zG|jY5h+(}`aPSlO1iP7i>nYR@$W<{f^;UThv$ETU17e7imR$~A@1jhJ=ty@;Zu2FJ zH#jS8_|1BrqIzFiuA;E$ZUwO)c&yc6kD6}G?k(UJWs-iFD^d_B$@+69+hcaJqR0Z4 Tw9MH237P%u7bGj%-j4kj1G%?G