Skip to content

Commit

Permalink
chore: rolled back a fix from previous release.
Browse files Browse the repository at this point in the history
  • Loading branch information
gokceno committed Apr 10, 2024
1 parent fcfac1c commit e3a0e16
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 40 deletions.
2 changes: 1 addition & 1 deletion apps/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
Expand Down
6 changes: 6 additions & 0 deletions packages/bucket-cache-libsql/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion packages/bucket-cache-libsql/package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
80 changes: 42 additions & 38 deletions packages/bucket-cache-libsql/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down

0 comments on commit e3a0e16

Please sign in to comment.