diff options
| author | Adam Soltys <[email protected]> | 2023-12-29 10:25:52 +0000 |
|---|---|---|
| committer | Adam Soltys <[email protected]> | 2023-12-29 10:27:15 +0000 |
| commit | 54fcccb8c54c7598dce6470f576a515e7658d341 (patch) | |
| tree | aa624834907f47fb9d76800fa0d58ee92a9306c8 | |
| parent | c474dbb763480ec923cd49f5cac0e07a310552a7 (diff) | |
| download | coinos-server-54fcccb8c54c7598dce6470f576a515e7658d341.tar.gz coinos-server-54fcccb8c54c7598dce6470f576a515e7658d341.zip | |
helper function
| -rw-r--r-- | lib/db.js | 18 | ||||
| -rw-r--r-- | lib/migrate.js | 36 |
2 files changed, 32 insertions, 22 deletions
@@ -88,6 +88,7 @@ export let g = async (k) => { return v; } }; + export let s = (k, v) => { if (k === "user:null" || k === "user:undefined") { warn("###### NULL USER #######"); @@ -96,6 +97,23 @@ export let s = (k, v) => { db.set(k, JSON.stringify(v)); }; +export let ga = async (k) => { + let v = await archive.get(k); + try { + return JSON.parse(v); + } catch (e) { + return v; + } +}; + +export let sa = (k, v) => { + if (k === "user:null" || k === "user:undefined") { + warn("###### NULL USER #######"); + console.trace(); + } + archive.set(k, JSON.stringify(v)); +}; + let retries = {}; export let t = async (k, f) => { try { diff --git a/lib/migrate.js b/lib/migrate.js index 87e7ab60..125c6cf2 100644 --- a/lib/migrate.js +++ b/lib/migrate.js @@ -1,15 +1,15 @@ import { err, l } from "$lib/logging"; import { types } from "$lib/payments"; -import { archive, db, g } from "$lib/db"; +import { archive, db, g, ga } from "$lib/db"; let q = {}; async function migrate(id) { id = id.replace(/\s/g, "").toLowerCase(); - let user = JSON.parse(await archive.get(`user:${id}`)); + let user = await ga(`user:${id}`); if (typeof user === "string") { id = user; - user = JSON.parse(await archive.get(`user:${id}`)); + user = await ga(`user:${id}`); } if (user) { @@ -21,24 +21,18 @@ async function migrate(id) { .set(`user:${username}`, id) .set(`user:${user.pubkey}`, id) .set(`user:${id}`, JSON.stringify(user)) - .set(`${id}:contacts`, (await archive.get(`${id}:contacts`)) || "[]") - .set( - `credit:bitcoin:${id}`, - (await archive.get(`credit:bitcoin:${id}`)) || 0, - ) - .set( - `credit:lightning:${id}`, - (await archive.get(`credit:lightning:${id}`)) || 0, - ) - .set(`${id}:lastlen`, (await archive.get(`${id}:lastlen`)) || 0) - .set(`${id}:cindex`, (await archive.get(`${id}:cindex`)) || 0) - .set(`balance:${id}`, (await archive.get(`balance:${id}`)) || 0) - .set(`pending:${id}`, (await archive.get(`pending:${id}`)) || 0); + .set(`${id}:contacts`, (await ga(`${id}:contacts`)) || "[]") + .set(`credit:bitcoin:${id}`, (await ga(`credit:bitcoin:${id}`)) || 0) + .set(`credit:lightning:${id}`, (await ga(`credit:lightning:${id}`)) || 0) + .set(`${id}:lastlen`, (await ga(`${id}:lastlen`)) || 0) + .set(`${id}:cindex`, (await ga(`${id}:cindex`)) || 0) + .set(`balance:${id}`, (await ga(`balance:${id}`)) || 0) + .set(`pending:${id}`, (await ga(`pending:${id}`)) || 0); let payments = await archive.lRange(`${id}:payments`, 0, -1); await Promise.all( payments.map(async (pid) => { - let p = JSON.parse(await archive.get(`payment:${pid}`)); + let p = await ga(`payment:${pid}`); if (p.type === types.internal && p.ref) { let recipient = await g(`user:${p.ref}`); if (!recipient) q[p.ref] = true; @@ -53,11 +47,11 @@ async function migrate(id) { let invoices = await archive.lRange(`${id}:invoices`, 0, -1); await Promise.all( invoices.map(async (iid) => { - let inv = await archive.get(`invoice:${iid}`); + let inv = await ga(`invoice:${iid}`); multi = await multi .lPush(`${id}:invoices`, iid) - .set(`invoice:${iid}`, inv); - }), + .set(`invoice:${iid}`, JSON.stringify(inv)); + }) ); try { @@ -68,8 +62,6 @@ async function migrate(id) { delete q[id]; - console.log("SUCCESS", username); - return user; } } |
