Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LAM-1169 feat: cache configs to disk #1190

Open
wants to merge 39 commits into
base: dev
Choose a base branch
from

Conversation

siiky
Copy link
Contributor

@siiky siiky commented Sep 20, 2024

No description provided.

@siiky siiky force-pushed the feat/lam-1169/cache-configs-to-disk branch 6 times, most recently from b60ac56 to fa6c436 Compare September 26, 2024 15:23
@siiky siiky force-pushed the feat/lam-1169/cache-configs-to-disk branch from 75104d2 to 54ce796 Compare October 1, 2024 14:41
@siiky siiky changed the title LAM-1169 cache configs to disk LAM-1169 feat: cache configs to disk Oct 2, 2024
@siiky siiky force-pushed the feat/lam-1169/cache-configs-to-disk branch from 79eb3ac to bce5003 Compare October 11, 2024 17:17
@siiky siiky marked this pull request as ready for review October 11, 2024 17:20
lib/variable-interval.js Outdated Show resolved Hide resolved
lib/sqlite.js Outdated Show resolved Hide resolved
lib/sqlite.js Outdated Show resolved Hide resolved
lib/db.js Outdated Show resolved Hide resolved
lib/sqlite.js Show resolved Hide resolved
lib/sqlite.js Outdated Show resolved Hide resolved
lib/sqlite.js Outdated
Comment on lines 22 to 51
exports.withDB = db => proc => db.then(proc)

exports.all = withDB => (...args) => withDB(db => db.all(...args))
exports.get = withDB => (...args) => withDB(db => db.get(...args))
exports.run = withDB => (...args) => withDB(db => db.run(...args))

exports.withTx = withDB => proc => withDB(db => new Promise((resolve, reject) =>
db.getDatabaseInstance().serialize(() =>
db.run("BEGIN TRANSACTION")
.then(_ => proc(db))
.then(ret => db.run("COMMIT TRANSACTION").then(_ => resolve(ret)))
.catch(err => {
db.run("ROLLBACK TRANSACTION").then(()=>{},()=>{})
reject(err)
})
)
))

exports.helpers = dbPromise => {
const withDB = exports.withDB(dbPromise)
const all = exports.all(withDB)
const get = exports.get(withDB)
const run = exports.run(withDB)
const withTx = exports.withTx(withDB)
return { withDB, all, get, run, withTx }
}

const repeat = (n, x) => Array(n).fill(x)
exports.makeTuple = n => '(' + repeat(n, '?').join(',') + ')'
exports.makeValues = (nColumns, nRows) => repeat(nRows, exports.makeTuple(nColumns)).join(',')
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm confused with all of the exports, most of them are not really being used anywhere and it makes an awkward read.

I'm also failing to understand the benefit of everything being 'factory' based when it uses the exact same db object anyways. Why not just do db.saveCoins (or whatever queries) on lssettings?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

all/get/run were used at some point, until I switched to withTx.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

RE factory based: if you mean the helpers() it's for usage-convenience.

Copy link
Collaborator

@RafaelTaranto RafaelTaranto left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another more general thing. This would benefit from having a separate folder under lib, either /db, /settings, /config or something in this vein. We can then have a pretty closed module.

Would be good to add testing for this as well, you can use vitest, maybe will have to search on how to use vitest without vite.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants