@travetto/model-mongo 3.3.7 → 3.4.0-rc.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/README.md +47 -16
  2. package/package.json +6 -6
  3. package/src/config.ts +47 -16
package/README.md CHANGED
@@ -48,7 +48,7 @@ where the [MongoModelConfig](https://github.com/travetto/travetto/tree/main/modu
48
48
  ```typescript
49
49
  import type mongo from 'mongodb';
50
50
 
51
- import { FileResourceProvider, TimeSpan } from '@travetto/base';
51
+ import { FileResourceProvider, GlobalEnv, TimeSpan } from '@travetto/base';
52
52
  import { Config } from '@travetto/config';
53
53
  import { Field } from '@travetto/schema';
54
54
 
@@ -60,23 +60,23 @@ export class MongoModelConfig {
60
60
  /**
61
61
  * Hosts
62
62
  */
63
- hosts = ['localhost'];
63
+ hosts?: string[];
64
64
  /**
65
65
  * Collection prefix
66
66
  */
67
- namespace = 'app';
67
+ namespace?: string;
68
68
  /**
69
69
  * Username
70
70
  */
71
- username = '';
71
+ username?: string;
72
72
  /**
73
73
  * Password
74
74
  */
75
- password = '';
75
+ password?: string;
76
76
  /**
77
77
  * Server port
78
78
  */
79
- port = 27017;
79
+ port?: number;
80
80
  /**
81
81
  * Direct mongo connection options
82
82
  */
@@ -102,6 +102,11 @@ export class MongoModelConfig {
102
102
  */
103
103
  cullRate?: number | TimeSpan;
104
104
 
105
+ /**
106
+ * Connection string
107
+ */
108
+ connectionString?: string;
109
+
105
110
  /**
106
111
  * Load all the ssl certs as needed
107
112
  */
@@ -109,29 +114,55 @@ export class MongoModelConfig {
109
114
  const resources = new FileResourceProvider({ includeCommon: true });
110
115
  const resolve = (file: string): Promise<string> => resources.describe(file).then(({ path }) => path, () => file);
111
116
 
117
+ if (this.connectionString) {
118
+ const details = new URL(this.connectionString);
119
+ this.hosts ??= details.hostname.split(',').filter(x => !!x);
120
+ this.srvRecord ??= details.protocol === 'mongodb+srv:';
121
+ this.namespace ??= details.pathname.replace('/', '');
122
+ Object.assign(this.options, Object.fromEntries(details.searchParams.entries()));
123
+ this.port ??= +details.port;
124
+ this.username ??= details.username;
125
+ this.password ??= details.password;
126
+ }
127
+
128
+ // Defaults
129
+ if (!this.namespace) {
130
+ this.namespace = 'app';
131
+ }
132
+ if (!this.port || Number.isNaN(this.port)) {
133
+ this.port = 27017;
134
+ }
135
+ if (!this.hosts || !this.hosts.length) {
136
+ this.hosts = ['localhost'];
137
+ }
138
+
112
139
  const opts = this.options;
113
140
  if (opts.ssl) {
114
- if (opts.sslCert) {
115
- opts.tlsCertificateFile = await resolve(opts.sslCert);
141
+ if (opts.cert) {
142
+ opts.cert = await Promise.all([opts.cert].flat(2).map(f => Buffer.isBuffer(f) ? f : resolve(f)));
116
143
  }
117
- if (opts.sslKey) {
118
- opts.sslKey = await resolve(opts.sslKey);
144
+ if (opts.tlsCertificateKeyFile) {
145
+ opts.tlsCertificateKeyFile = await resolve(opts.tlsCertificateKeyFile);
119
146
  }
120
- if (opts.sslCA) {
121
- opts.sslCA = await resolve(opts.sslCA);
147
+ if (opts.tlsCAFile) {
148
+ opts.tlsCAFile = await resolve(opts.tlsCAFile);
122
149
  }
123
- if (opts.sslCRL) {
124
- opts.sslCRL = await resolve(opts.sslCRL);
150
+ if (opts.tlsCRLFile) {
151
+ opts.tlsCRLFile = await resolve(opts.tlsCRLFile);
125
152
  }
126
153
  }
154
+
155
+ if (GlobalEnv.devMode) {
156
+ opts.waitQueueTimeoutMS ??= 1000 * 60 * 60 * 24; // Wait a day in dev mode
157
+ }
127
158
  }
128
159
 
129
160
  /**
130
161
  * Build connection URLs
131
162
  */
132
163
  get url(): string {
133
- const hosts = this.hosts
134
- .map(h => (this.srvRecord || h.includes(':')) ? h : `${h}:${this.port}`)
164
+ const hosts = this.hosts!
165
+ .map(h => (this.srvRecord || h.includes(':')) ? h : `${h}:${this.port ?? 27017}`)
135
166
  .join(',');
136
167
  const opts = Object.entries(this.options).map(([k, v]) => `${k}=${v}`).join('&');
137
168
  let creds = '';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@travetto/model-mongo",
3
- "version": "3.3.7",
3
+ "version": "3.4.0-rc.0",
4
4
  "description": "Mongo backing for the travetto model module.",
5
5
  "keywords": [
6
6
  "mongo",
@@ -25,13 +25,13 @@
25
25
  "directory": "module/model-mongo"
26
26
  },
27
27
  "dependencies": {
28
- "@travetto/config": "^3.3.4",
29
- "@travetto/model": "^3.3.7",
30
- "@travetto/model-query": "^3.3.7",
31
- "mongodb": "^6.1.0"
28
+ "@travetto/config": "^3.4.0-rc.0",
29
+ "@travetto/model": "^3.4.0-rc.0",
30
+ "@travetto/model-query": "^3.4.0-rc.0",
31
+ "mongodb": "^6.2.0"
32
32
  },
33
33
  "peerDependencies": {
34
- "@travetto/command": "^3.3.3"
34
+ "@travetto/command": "^3.4.0-rc.0"
35
35
  },
36
36
  "peerDependenciesMeta": {
37
37
  "@travetto/command": {
package/src/config.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import type mongo from 'mongodb';
2
2
 
3
- import { FileResourceProvider, TimeSpan } from '@travetto/base';
3
+ import { FileResourceProvider, GlobalEnv, TimeSpan } from '@travetto/base';
4
4
  import { Config } from '@travetto/config';
5
5
  import { Field } from '@travetto/schema';
6
6
 
@@ -12,23 +12,23 @@ export class MongoModelConfig {
12
12
  /**
13
13
  * Hosts
14
14
  */
15
- hosts = ['localhost'];
15
+ hosts?: string[];
16
16
  /**
17
17
  * Collection prefix
18
18
  */
19
- namespace = 'app';
19
+ namespace?: string;
20
20
  /**
21
21
  * Username
22
22
  */
23
- username = '';
23
+ username?: string;
24
24
  /**
25
25
  * Password
26
26
  */
27
- password = '';
27
+ password?: string;
28
28
  /**
29
29
  * Server port
30
30
  */
31
- port = 27017;
31
+ port?: number;
32
32
  /**
33
33
  * Direct mongo connection options
34
34
  */
@@ -54,6 +54,11 @@ export class MongoModelConfig {
54
54
  */
55
55
  cullRate?: number | TimeSpan;
56
56
 
57
+ /**
58
+ * Connection string
59
+ */
60
+ connectionString?: string;
61
+
57
62
  /**
58
63
  * Load all the ssl certs as needed
59
64
  */
@@ -61,29 +66,55 @@ export class MongoModelConfig {
61
66
  const resources = new FileResourceProvider({ includeCommon: true });
62
67
  const resolve = (file: string): Promise<string> => resources.describe(file).then(({ path }) => path, () => file);
63
68
 
69
+ if (this.connectionString) {
70
+ const details = new URL(this.connectionString);
71
+ this.hosts ??= details.hostname.split(',').filter(x => !!x);
72
+ this.srvRecord ??= details.protocol === 'mongodb+srv:';
73
+ this.namespace ??= details.pathname.replace('/', '');
74
+ Object.assign(this.options, Object.fromEntries(details.searchParams.entries()));
75
+ this.port ??= +details.port;
76
+ this.username ??= details.username;
77
+ this.password ??= details.password;
78
+ }
79
+
80
+ // Defaults
81
+ if (!this.namespace) {
82
+ this.namespace = 'app';
83
+ }
84
+ if (!this.port || Number.isNaN(this.port)) {
85
+ this.port = 27017;
86
+ }
87
+ if (!this.hosts || !this.hosts.length) {
88
+ this.hosts = ['localhost'];
89
+ }
90
+
64
91
  const opts = this.options;
65
92
  if (opts.ssl) {
66
- if (opts.sslCert) {
67
- opts.tlsCertificateFile = await resolve(opts.sslCert);
93
+ if (opts.cert) {
94
+ opts.cert = await Promise.all([opts.cert].flat(2).map(f => Buffer.isBuffer(f) ? f : resolve(f)));
68
95
  }
69
- if (opts.sslKey) {
70
- opts.sslKey = await resolve(opts.sslKey);
96
+ if (opts.tlsCertificateKeyFile) {
97
+ opts.tlsCertificateKeyFile = await resolve(opts.tlsCertificateKeyFile);
71
98
  }
72
- if (opts.sslCA) {
73
- opts.sslCA = await resolve(opts.sslCA);
99
+ if (opts.tlsCAFile) {
100
+ opts.tlsCAFile = await resolve(opts.tlsCAFile);
74
101
  }
75
- if (opts.sslCRL) {
76
- opts.sslCRL = await resolve(opts.sslCRL);
102
+ if (opts.tlsCRLFile) {
103
+ opts.tlsCRLFile = await resolve(opts.tlsCRLFile);
77
104
  }
78
105
  }
106
+
107
+ if (GlobalEnv.devMode) {
108
+ opts.waitQueueTimeoutMS ??= 1000 * 60 * 60 * 24; // Wait a day in dev mode
109
+ }
79
110
  }
80
111
 
81
112
  /**
82
113
  * Build connection URLs
83
114
  */
84
115
  get url(): string {
85
- const hosts = this.hosts
86
- .map(h => (this.srvRecord || h.includes(':')) ? h : `${h}:${this.port}`)
116
+ const hosts = this.hosts!
117
+ .map(h => (this.srvRecord || h.includes(':')) ? h : `${h}:${this.port ?? 27017}`)
87
118
  .join(',');
88
119
  const opts = Object.entries(this.options).map(([k, v]) => `${k}=${v}`).join('&');
89
120
  let creds = '';