mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-11-01 03:26:04 +01:00
feat: async/await psql connection
This commit is contained in:
@@ -45,32 +45,21 @@ postgresModule.questions = [
|
|||||||
},
|
},
|
||||||
];
|
];
|
||||||
|
|
||||||
postgresModule.init = function (callback) {
|
postgresModule.init = async function () {
|
||||||
callback = callback || function () { };
|
|
||||||
|
|
||||||
const Pool = require('pg').Pool;
|
const Pool = require('pg').Pool;
|
||||||
|
|
||||||
const connOptions = connection.getConnectionOptions();
|
const connOptions = connection.getConnectionOptions();
|
||||||
|
const pool = new Pool(connOptions);
|
||||||
const db = new Pool(connOptions);
|
postgresModule.pool = pool;
|
||||||
|
postgresModule.client = pool;
|
||||||
db.connect(function (err, client, release) {
|
const client = await pool.connect();
|
||||||
if (err) {
|
try {
|
||||||
winston.error('NodeBB could not connect to your PostgreSQL database. PostgreSQL returned the following error: ' + err.message);
|
await checkUpgrade(client);
|
||||||
return callback(err);
|
} catch (err) {
|
||||||
}
|
winston.error('NodeBB could not connect to your PostgreSQL database. PostgreSQL returned the following error: ' + err.message);
|
||||||
|
throw err;
|
||||||
postgresModule.pool = db;
|
} finally {
|
||||||
postgresModule.client = db;
|
client.release();
|
||||||
|
}
|
||||||
checkUpgrade(client).then(function () {
|
|
||||||
release();
|
|
||||||
callback(null);
|
|
||||||
}, function (err) {
|
|
||||||
release();
|
|
||||||
callback(err);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
@@ -300,27 +289,25 @@ PARALLEL SAFE`);
|
|||||||
await client.query(`COMMIT`);
|
await client.query(`COMMIT`);
|
||||||
}
|
}
|
||||||
|
|
||||||
postgresModule.createSessionStore = function (options, callback) {
|
postgresModule.createSessionStore = async function (options) {
|
||||||
var meta = require('../meta');
|
const meta = require('../meta');
|
||||||
|
|
||||||
function done(db) {
|
function done(db) {
|
||||||
const sessionStore = require('connect-pg-simple')(session);
|
const sessionStore = require('connect-pg-simple')(session);
|
||||||
const store = new sessionStore({
|
return new sessionStore({
|
||||||
pool: db,
|
pool: db,
|
||||||
ttl: meta.getSessionTTLSeconds(),
|
ttl: meta.getSessionTTLSeconds(),
|
||||||
pruneSessionInterval: nconf.get('isPrimary') ? 60 : false,
|
pruneSessionInterval: nconf.get('isPrimary') ? 60 : false,
|
||||||
});
|
});
|
||||||
callback(null, store);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
connection.connect(options, function (err, db) {
|
const db = await connection.connect(options);
|
||||||
if (err) {
|
|
||||||
return callback(err);
|
if (!nconf.get('isPrimary')) {
|
||||||
}
|
return done(db);
|
||||||
if (!nconf.get('isPrimary')) {
|
}
|
||||||
return done(db);
|
|
||||||
}
|
await db.query(`
|
||||||
db.query(`
|
|
||||||
CREATE TABLE IF NOT EXISTS "session" (
|
CREATE TABLE IF NOT EXISTS "session" (
|
||||||
"sid" CHAR(32) NOT NULL
|
"sid" CHAR(32) NOT NULL
|
||||||
COLLATE "C"
|
COLLATE "C"
|
||||||
@@ -333,14 +320,9 @@ CREATE INDEX IF NOT EXISTS "session_expire_idx" ON "session"("expire");
|
|||||||
|
|
||||||
ALTER TABLE "session"
|
ALTER TABLE "session"
|
||||||
ALTER "sid" SET STORAGE MAIN,
|
ALTER "sid" SET STORAGE MAIN,
|
||||||
CLUSTER ON "session_expire_idx";`, function (err) {
|
CLUSTER ON "session_expire_idx";`);
|
||||||
if (err) {
|
|
||||||
return callback(err);
|
|
||||||
}
|
|
||||||
|
|
||||||
done(db);
|
return done(db);
|
||||||
});
|
|
||||||
});
|
|
||||||
};
|
};
|
||||||
|
|
||||||
postgresModule.createIndices = function (callback) {
|
postgresModule.createIndices = function (callback) {
|
||||||
@@ -378,32 +360,21 @@ postgresModule.checkCompatibilityVersion = function (version, callback) {
|
|||||||
callback();
|
callback();
|
||||||
};
|
};
|
||||||
|
|
||||||
postgresModule.info = function (db, callback) {
|
postgresModule.info = async function (db) {
|
||||||
async.waterfall([
|
if (!db) {
|
||||||
function (next) {
|
db = await connection.connect(nconf.get('postgres'));
|
||||||
if (db) {
|
}
|
||||||
setImmediate(next, null, db);
|
postgresModule.pool = postgresModule.pool || db;
|
||||||
} else {
|
const res = await db.query(`
|
||||||
connection.connect(nconf.get('postgres'), next);
|
SELECT true "postgres",
|
||||||
}
|
current_setting('server_version') "version",
|
||||||
},
|
EXTRACT(EPOCH FROM NOW() - pg_postmaster_start_time()) * 1000 "uptime"`
|
||||||
function (db, next) {
|
);
|
||||||
postgresModule.pool = postgresModule.pool || db;
|
return res.rows[0];
|
||||||
|
|
||||||
db.query(`
|
|
||||||
SELECT true "postgres",
|
|
||||||
current_setting('server_version') "version",
|
|
||||||
EXTRACT(EPOCH FROM NOW() - pg_postmaster_start_time()) * 1000 "uptime"`, next);
|
|
||||||
},
|
|
||||||
function (res, next) {
|
|
||||||
next(null, res.rows[0]);
|
|
||||||
},
|
|
||||||
], callback);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
postgresModule.close = function (callback) {
|
postgresModule.close = async function () {
|
||||||
callback = callback || function () {};
|
await postgresModule.pool.end();
|
||||||
postgresModule.pool.end(callback);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
require('./postgres/main')(postgresModule);
|
require('./postgres/main')(postgresModule);
|
||||||
|
|||||||
@@ -33,14 +33,12 @@ connection.getConnectionOptions = function (postgres) {
|
|||||||
return _.merge(connOptions, postgres.options || {});
|
return _.merge(connOptions, postgres.options || {});
|
||||||
};
|
};
|
||||||
|
|
||||||
connection.connect = function (options, callback) {
|
connection.connect = async function (options) {
|
||||||
const Pool = require('pg').Pool;
|
const Pool = require('pg').Pool;
|
||||||
|
|
||||||
const connOptions = connection.getConnectionOptions(options);
|
const connOptions = connection.getConnectionOptions(options);
|
||||||
|
|
||||||
const db = new Pool(connOptions);
|
const db = new Pool(connOptions);
|
||||||
|
await db.connect();
|
||||||
db.connect(function (err) {
|
return db;
|
||||||
callback(err, db);
|
|
||||||
});
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
require('../../promisify')(connection);
|
||||||
|
|||||||
Reference in New Issue
Block a user