-
Notifications
You must be signed in to change notification settings - Fork 4
Use @fastify/fastify-postgres. CheckerNetwork/roadmap#220 #341
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
base: main
Are you sure you want to change the base?
Changes from 1 commit
31a5f9b
6206573
51bfcad
3717210
973b0da
826d075
e89a63c
26d73c7
2608fdc
c0a95b2
6a76461
f6a0319
59448e4
9423683
97ac2bd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -14,7 +14,9 @@ | |||||
"dependencies": { | ||||||
"@filecoin-station/spark-evaluate": "^1.2.0", | ||||||
"pg": "^8.13.3", | ||||||
"postgrator": "^8.0.0" | ||||||
"postgrator": "^8.0.0", | ||||||
"@fastify/postgres": "^5.2.0", | ||||||
"@fastify/url-data": "^6.0.3" | ||||||
|
"@fastify/postgres": "^5.2.0", | |
"@fastify/url-data": "^6.0.3" |
I think we should remove these packages from db
workspace and install @fastify/postgres
inside the stats
workspace only. You can install workspace specific packages by running npm install @fastify/postgres -w stats
juliangruber marked this conversation as resolved.
Show resolved
Hide resolved
|
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
@@ -1,9 +1,38 @@ | ||||||||||
import Fastify from 'fastify' | ||||||||||
import fastifyPostgres from '@fastify/postgres' | ||||||||||
import { | ||||||||||
getPgPools, | ||||||||||
migrateEvaluateDB, | ||||||||||
migrateStatsDB | ||||||||||
} from '@filecoin-station/spark-stats-db' | ||||||||||
|
||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please undo this change to keep the diff clean There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Alright, noted |
||||||||||
const pgPools = await getPgPools() | ||||||||||
await migrateStatsDB(pgPools.stats) | ||||||||||
await migrateEvaluateDB(pgPools.evaluate) | ||||||||||
const { | ||||||||||
DATABASE_URL, | ||||||||||
EVALUATE_DB_URL | ||||||||||
} = process.env | ||||||||||
|
const { | |
DATABASE_URL, | |
EVALUATE_DB_URL | |
} = process.env |
These constants seem not to be used anywhere. Let's delete them.
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that we shouldn't really create a new Fastify
instance in order to run migrations. This should be done without the @fastify/postgres
package.
juliangruber marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,50 @@ | ||
import '../lib/instrument.js' | ||
import Fastify from 'fastify' | ||
import fastifyPostgres from '@fastify/postgres' | ||
import { createApp } from '../lib/app.js' | ||
import { getPgPools } from '@filecoin-station/spark-stats-db' | ||
|
||
const { | ||
PORT = '8080', | ||
HOST = '127.0.0.1', | ||
SPARK_API_BASE_URL = 'https://api.filspark.com/', | ||
REQUEST_LOGGING = 'true' | ||
REQUEST_LOGGING = 'true', | ||
DATABASE_URL, | ||
|
||
} = process.env | ||
|
||
const pgPools = await getPgPools() | ||
|
||
const dbFastify = Fastify({ logger: false }) | ||
|
||
|
||
await dbFastify.register(fastifyPostgres, { | ||
connectionString: DATABASE_URL, | ||
name: 'stats', | ||
|
||
pool: { | ||
min: 0, | ||
max: 100, | ||
idleTimeoutMillis: 1000, | ||
maxLifetimeSeconds: 60 | ||
} | ||
}) | ||
|
||
const pgPools = { | ||
stats: dbFastify.pg.stats, | ||
evaluate: dbFastify.pg.evaluate, | ||
async end() { | ||
await dbFastify.close() | ||
} | ||
} | ||
|
||
|
||
export const withDb = async (poolName, queryFn) => { | ||
const client = await dbFastify.pg[poolName].connect() | ||
try { | ||
return await queryFn(client) | ||
} finally { | ||
client.release() | ||
} | ||
} | ||
|
||
const app = await createApp({ | ||
SPARK_API_BASE_URL, | ||
pgPools, | ||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,113 @@ | ||||||
// stats/lib/db.js | ||||||
import Fastify from 'fastify' | ||||||
import fastifyPostgres from '@fastify/postgres' | ||||||
|
||||||
let fastifyApp = null | ||||||
let isInitialized = false | ||||||
|
let fastifyApp = null | |
let isInitialized = false |
I would avoid using globals in this case.
juliangruber marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of creating a new Fastify
instance I think it would be better to pass down existing instance as function argument.
Uh oh!
There was an error while loading. Please reload this page.