add v23 support to instance app

This commit is contained in:
Ben Allfree 2024-12-04 03:39:56 -08:00
parent 966d7aa734
commit ba979aab93
4 changed files with 93 additions and 0 deletions

View File

@ -0,0 +1 @@
pb_data

View File

@ -0,0 +1,39 @@
onBootstrap((e) => {
e.next()
const { mkLog } = /** @type {Lib} */ (require(`${__hooks}/_ph_lib.js`))
const log = mkLog(`admin-sync`)
const { id, email, tokenKey, passwordHash } = (() => {
try {
return /** @type{{id:string, email:string, tokenKey:string,passwordHash:string}} */ (
JSON.parse(process.env.ADMIN_SYNC)
)
} catch (e) {
return { id: '', email: '', tokenKey: '', passwordHash: '' }
}
})()
if (!email) {
log(`Not active - skipped`)
return
}
const update = () =>
e.app
.db()
.newQuery(
`
insert or replace into _superusers (id, email, tokenKey, password) values ({:id}, {:email}, {:tokenKey}, {:passwordHash})
`,
)
.bind({ id, email, tokenKey, passwordHash })
.execute()
try {
update()
log(`Success updating admin credentials ${email}`)
} catch (e) {
log(`Failed to update admin credentials ${email}: ${e}`)
}
})

View File

@ -0,0 +1,19 @@
/** @type {Lib['mkLog']} */
const mkLog =
(namespace) =>
/**
* @param {...any} s
* @returns
*/
(...s) =>
console.log(
`[${namespace}]`,
...s.map((p) => {
if (typeof p === 'object') return JSON.stringify(p, null, 2)
return p
}),
)
module.exports = {
mkLog,
}

View File

@ -0,0 +1,34 @@
migrate(
(app) => {
try {
const APP_DEFAULTS = {
appName: 'Acme',
appURL: 'http://localhost:8090',
senderName: 'Support',
senderAddress: 'support@example.com',
}
const { PH_APP_NAME, PH_INSTANCE_URL } = process.env
const settings = app.settings()
const fix = (field, newValue) => {
if (!newValue || settings.meta[field] !== APP_DEFAULTS[field]) return
settings.meta[field] = newValue
}
fix(`appName`, PH_APP_NAME)
fix(`appUrl`, PH_INSTANCE_URL)
fix(`senderName`, PH_APP_NAME)
fix(`senderAddress`, `${PH_APP_NAME}@app.pockethost.io`)
app.save(settings)
console.log(`***defaults successfully applied`)
} catch (e) {
console.error(`***error applying defaults: ${e}`)
}
},
(db) => {
// add down queries...
},
)