mirror of
https://github.com/djdevin/recflare.git
synced 2026-09-09 15:11:29 -07:00
pfps
This commit is contained in:
@@ -98,6 +98,30 @@ export async function getAccountsByIds(db: D1Database, ids: number[]): Promise<A
|
||||
return parseAll(results)
|
||||
}
|
||||
|
||||
/**
|
||||
* Merge `overrides` into the account row for `id` and persist it. Reads the
|
||||
* current account (falling back to a synthesized default), applies the
|
||||
* overrides, and writes the whole JSON blob back — inserting the row when the
|
||||
* account isn't in the table yet. Returns the updated account.
|
||||
*/
|
||||
export async function updateAccount(
|
||||
db: D1Database,
|
||||
id: number,
|
||||
overrides: Partial<Account>
|
||||
): Promise<Account> {
|
||||
const current = (await getAccount(db, id)) ?? defaultAccount(id)
|
||||
const updated: Account = { ...current, ...overrides, AccountId: id }
|
||||
const data = JSON.stringify(updated)
|
||||
const res = await db
|
||||
.prepare('UPDATE accounts SET data = ?2 WHERE account_id = ?1')
|
||||
.bind(id, data)
|
||||
.run()
|
||||
if (!res.meta.changes) {
|
||||
await db.prepare('INSERT INTO accounts (data) VALUES (?1)').bind(data).run()
|
||||
}
|
||||
return updated
|
||||
}
|
||||
|
||||
/**
|
||||
* Create and persist a new account. The id is the next free integer (above the
|
||||
* seeded system accounts); the username is auto-assigned (players don't choose
|
||||
|
||||
Reference in New Issue
Block a user