mirror of
https://gitcode.com/gh_mirrors/hy/Hydro.git
synced 2026-10-20 01:38:19 +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 }],
|
[`mongo 127.0.0.1:27017/hydro ${tmpFile}`, { retry: true }],
|
||||||
() => writeFileSync(`${process.env.HOME}/.hydro/config.json`, JSON.stringify({
|
() => writeFileSync(`${process.env.HOME}/.hydro/config.json`, JSON.stringify({
|
||||||
host: '127.0.0.1',
|
uri: `mongodb://hydro:${password}@127.0.0.1:27017/hydro`,
|
||||||
port: 27017,
|
|
||||||
name: 'hydro',
|
|
||||||
username: 'hydro',
|
|
||||||
password,
|
|
||||||
})),
|
})),
|
||||||
'pm2 stop mongod',
|
'pm2 stop mongod',
|
||||||
'pm2 del mongod',
|
'pm2 del mongod',
|
||||||
|
|||||||
@@ -17,6 +17,7 @@ interface MongoConfig {
|
|||||||
port?: string,
|
port?: string,
|
||||||
name?: string,
|
name?: string,
|
||||||
url?: string,
|
url?: string,
|
||||||
|
uri?: string,
|
||||||
prefix?: string,
|
prefix?: string,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -29,7 +30,7 @@ class MongoService {
|
|||||||
let mongourl = `${opts.protocol || 'mongodb'}://`;
|
let mongourl = `${opts.protocol || 'mongodb'}://`;
|
||||||
if (opts.username) mongourl += `${opts.username}:${encodeURIComponent(opts.password)}@`;
|
if (opts.username) mongourl += `${opts.username}:${encodeURIComponent(opts.password)}@`;
|
||||||
mongourl += `${opts.host}:${opts.port}/${opts.name}`;
|
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;
|
return mongourl;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -42,7 +43,12 @@ class MongoService {
|
|||||||
mongourl = mongod.getUri();
|
mongourl = mongod.getUri();
|
||||||
}
|
}
|
||||||
this.opts = opts;
|
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');
|
this.db = this.client.db(opts.name || 'hydro');
|
||||||
await bus.parallel('database/connect', this.db);
|
await bus.parallel('database/connect', this.db);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user