From e3a0e161082625518d3223e0e5e84f6bf34c5679 Mon Sep 17 00:00:00 2001 From: Gokcen Ogutcu Date: Wed, 10 Apr 2024 09:51:49 +0300 Subject: [PATCH] chore: rolled back a fix from previous release. --- apps/api/package.json | 2 +- packages/bucket-cache-libsql/CHANGELOG.md | 6 ++ packages/bucket-cache-libsql/package.json | 2 +- packages/bucket-cache-libsql/src/index.js | 80 ++++++++++++----------- 4 files changed, 50 insertions(+), 40 deletions(-) diff --git a/apps/api/package.json b/apps/api/package.json index 8ecad4b..bdaa513 100644 --- a/apps/api/package.json +++ b/apps/api/package.json @@ -27,7 +27,7 @@ "@gokceno/crux-graphql-schema": "1.0.7", "@gokceno/crux-locales": "0.0.2", "@gokceno/crux-bucket": "1.3.0", - "@gokceno/crux-bucket-cache-libsql": "0.4.0", + "@gokceno/crux-bucket-cache-libsql": "0.4.1", "@gokceno/crux-bucket-source-filesystem": "0.1.1", "@gokceno/crux-bucket-source-github": "0.1.0" } diff --git a/packages/bucket-cache-libsql/CHANGELOG.md b/packages/bucket-cache-libsql/CHANGELOG.md index c42f4e8..b76921f 100644 --- a/packages/bucket-cache-libsql/CHANGELOG.md +++ b/packages/bucket-cache-libsql/CHANGELOG.md @@ -1,5 +1,11 @@ # @gokceno/crux-bucket-cache-libsql +## 0.4.1 + +### Patch Changes + +- Fixed an issue from previous release: Replaced a check within SQL with a try-catch (not so elegant). Will change later. + ## 0.4.0 ### Minor Changes diff --git a/packages/bucket-cache-libsql/package.json b/packages/bucket-cache-libsql/package.json index 764784f..3fc4bbe 100644 --- a/packages/bucket-cache-libsql/package.json +++ b/packages/bucket-cache-libsql/package.json @@ -1,7 +1,7 @@ { "name": "@gokceno/crux-bucket-cache-libsql", "repository": "git+https://github.com/gokceno/crux.md.git", - "version": "0.4.0", + "version": "0.4.1", "main": "./src/index.js", "type": "module", "publishConfig": { diff --git a/packages/bucket-cache-libsql/src/index.js b/packages/bucket-cache-libsql/src/index.js index b699ceb..867506b 100644 --- a/packages/bucket-cache-libsql/src/index.js +++ b/packages/bucket-cache-libsql/src/index.js @@ -76,48 +76,52 @@ export const Cache = ({ dbPath = ':memory:', expires = '600 SECONDS' }) => { _flush(['collections']); data.map(async item => { const resolved = await Promise.resolve(item) || {}; - const row = db.prepare(` - INSERT INTO collections (collection_type, collection_id, locale, _cached_at) - SELECT ?, ?, ?, DATETIME() - WHERE NOT EXISTS ( - SELECT 1 FROM collections - WHERE collection_type = ? AND collection_id = ? AND locale = ? - ) - `).run([collection, resolved._id, locale, collection, resolved._id, locale]); - const statement = db.prepare(`INSERT INTO collections_props (collection_id, prop_name, prop_value) VALUES (?, ?, ?)`); - Object.entries(resolved) - // eslint-disable-next-line no-unused-vars - .filter(([propName, propValue]) => propValue !== undefined && propValue !== null) - .map(async ([propName, propValue]) => { - if(typeof propValue === 'function' && propValue.constructor.name === 'AsyncFunction') { - const expandedData = await Promise.all((await (await propValue)()).map(async (expand) => { + // TODO: insert into ... select ... where not exits ... would be ideal compared to catching errors. + try { + const row = db.prepare(`INSERT INTO collections (collection_type, collection_id, locale, _cached_at) VALUES (?, ?, ?, DATETIME())`).run([collection, resolved._id, locale]); + const statement = db.prepare(`INSERT INTO collections_props (collection_id, prop_name, prop_value) VALUES (?, ?, ?)`); + Object.entries(resolved) + // eslint-disable-next-line no-unused-vars + .filter(([propName, propValue]) => propValue !== undefined && propValue !== null) + .map(async ([propName, propValue]) => { + if(typeof propValue === 'function' && propValue.constructor.name === 'AsyncFunction') { + const expandedData = await Promise.all((await (await propValue)()).map(async (expand) => { + let returnObject = {}; + Object.entries(expand).map(async ([propName, propValue]) => + returnObject[propName] = typeof propValue === 'function' ? (await Promise.resolve(propValue())) : propValue + ); + return returnObject; + })); + statement.run([row.lastInsertRowid, propName, JSON.stringify(expandedData)]); + } + else if(typeof propValue === 'object' && !Array.isArray(propValue)) { let returnObject = {}; - Object.entries(expand).map(async ([propName, propValue]) => + await Promise.all(Object.entries(propValue).map(async ([propName, propValue]) => { returnObject[propName] = typeof propValue === 'function' ? (await Promise.resolve(propValue())) : propValue - ); - return returnObject; - })); - statement.run([row.lastInsertRowid, propName, JSON.stringify(expandedData)]); - } - else if(typeof propValue === 'object' && !Array.isArray(propValue)) { - let returnObject = {}; - await Promise.all(Object.entries(propValue).map(async ([propName, propValue]) => { - returnObject[propName] = typeof propValue === 'function' ? (await Promise.resolve(propValue())) : propValue - if(typeof returnObject[propName] === 'object') { - Object.entries(returnObject[propName]).map(([x, y]) => { - Object.entries(y) + if(typeof returnObject[propName] === 'object') { + Object.entries(returnObject[propName]).map(([x, y]) => { + Object.entries(y) // eslint-disable-next-line no-unused-vars - .filter(([a ,b]) => typeof b === 'function') - .map(async ([a, b]) => returnObject[propName][x][a] = await b()); - }); - } - })); - statement.run([row.lastInsertRowid, propName, JSON.stringify(returnObject)]); - } - else { - statement.run([row.lastInsertRowid, propName, JSON.stringify(propValue)]); + .filter(([a ,b]) => typeof b === 'function') + .map(async ([a, b]) => returnObject[propName][x][a] = await b()); + }); + } + })); + statement.run([row.lastInsertRowid, propName, JSON.stringify(returnObject)]); + } + else { + statement.run([row.lastInsertRowid, propName, JSON.stringify(propValue)]); + } + }); + } + catch(e) { + switch(e.code) { + case 'SQLITE_CONSTRAINT_UNIQUE': + break; + default: + throw new Error(e); } - }); + } }); return data; }