mirror of
https://gitcode.com/gh_mirrors/hy/Hydro.git
synced 2026-10-17 09:36:08 +08:00
core: db: prefer connection uri
This commit is contained in:
1 parent
c5ad6599a8
commit
6d69e88739
2 files changed
+9
-7
No files matched your search
+1
-5
@@ -336,11 +336,7 @@ connect-timeout = 10`);
|
||||
})})`),
|
||||
[`mongo 127.0.0.1:27017/hydro ${tmpFile}`, { retry: true }],
|
||||
() => writeFileSync(`${process.env.HOME}/.hydro/config.json`, JSON.stringify({
|
||||
host: '127.0.0.1',
|
||||
port: 27017,
|
||||
name: 'hydro',
|
||||
username: 'hydro',
|
||||
password,
|
||||
uri: `mongodb://hydro:${password}@127.0.0.1:27017/hydro`,
|
||||
})),
|
||||
'pm2 stop mongod',
|
||||
'pm2 del mongod',
|
||||
|
||||
@@ -17,6 +17,7 @@ interface MongoConfig {
|
||||
port?: string,
|
||||
name?: string,
|
||||
url?: string,
|
||||
uri?: string,
|
||||
prefix?: string,
|
||||
}
|
||||
|
||||
@@ -29,7 +30,7 @@ class MongoService {
|
||||
let mongourl = `${opts.protocol || 'mongodb'}://`;
|
||||
if (opts.username) mongourl += `${opts.username}:${encodeURIComponent(opts.password)}@`;
|
||||
mongourl += `${opts.host}:${opts.port}/${opts.name}`;
|
||||
if (opts.url) mongourl = opts.url;
|
||||
if (opts.url || opts.uri) mongourl = opts.url || opts.uri;
|
||||
return mongourl;
|
||||
}
|
||||
|
||||
@@ -42,7 +43,12 @@ class MongoService {
|
||||
mongourl = mongod.getUri();
|
||||
}
|
||||
this.opts = opts;
|
||||
this.client = await MongoClient.connect(mongourl, { useNewUrlParser: true, useUnifiedTopology: true });
|
||||
this.client = await MongoClient.connect(mongourl, {
|
||||
useNewUrlParser: true,
|
||||
useUnifiedTopology: true,
|
||||
readPreference: 'nearest',
|
||||
writeConcern: 'majority',
|
||||
});
|
||||
this.db = this.client.db(opts.name || 'hydro');
|
||||
await bus.parallel('database/connect', this.db);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user