* chore(deps-dev): bump prettier from 2.8.8 to 3.0.0 Bumps [prettier](https://github.com/prettier/prettier) from 2.8.8 to 3.0.0. - [Release notes](https://github.com/prettier/prettier/releases) - [Changelog](https://github.com/prettier/prettier/blob/main/CHANGELOG.md) - [Commits](https://github.com/prettier/prettier/compare/2.8.8...3.0.0) --- updated-dependencies: - dependency-name: prettier dependency-type: direct:development update-type: version-update:semver-major ... Signed-off-by: dependabot[bot] <support@github.com> * reformat all the things (prettier 3) * update tests to await calls to prettier.format() --------- Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: chris48s <git@chris-shaw.dev>
58 lines
1.2 KiB
JavaScript
58 lines
1.2 KiB
JavaScript
import pg from 'pg'
|
|
import log from '../server/log.js'
|
|
|
|
export default class SqlTokenPersistence {
|
|
constructor({ url, table }) {
|
|
this.url = url
|
|
this.table = table
|
|
this.noteTokenAdded = this.noteTokenAdded.bind(this)
|
|
this.noteTokenRemoved = this.noteTokenRemoved.bind(this)
|
|
}
|
|
|
|
async initialize(pool) {
|
|
if (pool) {
|
|
this.pool = pool
|
|
} else {
|
|
this.pool = new pg.Pool({ connectionString: this.url })
|
|
}
|
|
const result = await this.pool.query(`SELECT token FROM ${this.table};`)
|
|
return result.rows.map(row => row.token)
|
|
}
|
|
|
|
async stop() {
|
|
if (this.pool) {
|
|
await this.pool.end()
|
|
}
|
|
}
|
|
|
|
async onTokenAdded(token) {
|
|
return await this.pool.query(
|
|
`INSERT INTO ${this.table} (token) VALUES ($1::text) ON CONFLICT (token) DO NOTHING;`,
|
|
[token],
|
|
)
|
|
}
|
|
|
|
async onTokenRemoved(token) {
|
|
return await this.pool.query(
|
|
`DELETE FROM ${this.table} WHERE token=$1::text;`,
|
|
[token],
|
|
)
|
|
}
|
|
|
|
async noteTokenAdded(token) {
|
|
try {
|
|
await this.onTokenAdded(token)
|
|
} catch (e) {
|
|
log.error(e)
|
|
}
|
|
}
|
|
|
|
async noteTokenRemoved(token) {
|
|
try {
|
|
await this.onTokenRemoved(token)
|
|
} catch (e) {
|
|
log.error(e)
|
|
}
|
|
}
|
|
}
|