mythix-orm-postgresql 1.10.1 → 1.11.1

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.
package/.biblorc.js ADDED
@@ -0,0 +1,30 @@
1
+ 'use strict';
2
+
3
+ /* global __dirname */
4
+
5
+ const Path = require('path');
6
+
7
+ module.exports = {
8
+ rootDir: __dirname,
9
+ inputDir: Path.resolve(__dirname),
10
+ outputDir: Path.resolve(__dirname, '..', 'mythix-orm-postgresql.wiki'),
11
+ files: [
12
+ {
13
+ include: /\/lib\/.*\.js$/,
14
+ parser: 'typescript',
15
+ compiler: 'typescript',
16
+ },
17
+ {
18
+ include: /\/docs\/.*\.md$/,
19
+ parser: 'markdown',
20
+ compiler: 'markdown',
21
+ },
22
+ ],
23
+ exclude: [
24
+ /node_modules|\/spec\//
25
+ ],
26
+ generatorOptions: {
27
+ repositoryURL: 'https://github.com/th317erd/mythix-orm-postgresql',
28
+ baseURL: './',
29
+ },
30
+ };
package/README.md CHANGED
@@ -1,5 +1,58 @@
1
1
  # mythix-orm-postgresql
2
2
 
3
- PostgreSQL driver for Mythix ORM
3
+ SQLite database driver for [Mythix ORM](https://www.npmjs.com/package/mythix-orm).
4
4
 
5
- Mythix ORM aims to replace Sequelize and the few other terrible solutions that the poor destitute Node community has to work with. Mythix ORM is not yet quite ready for prime time however, so please check back soon!
5
+ ## Install
6
+
7
+ ```bash
8
+ npm i --save mythix-orm-postgresql
9
+ ```
10
+
11
+ ## Documentation
12
+
13
+ Documentation can be found at the [WIKI](https://github.com/th317erd/mythix-orm-postgresql/wiki).
14
+
15
+ Documentation for Mythix ORM can be found at the [Mythix ORM WIKI](https://github.com/th317erd/mythix-orm/wiki).
16
+
17
+ ## Usage
18
+
19
+ ```javascript
20
+ const { PostgreSQLConnection } = require('mythix-orm-postgresql');
21
+
22
+ (async function() {
23
+ let connection = new PostgreSQLConnection({
24
+ bindModels: true,
25
+ models: [ /* application models */ ],
26
+ logger: console,
27
+ user: 'database-username',
28
+ password: 'database-password',
29
+ database: 'test-database',
30
+ host: '127.0.0.1',
31
+ port: 5432,
32
+ });
33
+
34
+ await connection.start();
35
+
36
+ // run application code
37
+
38
+ await connection.stop();
39
+ })();
40
+ ```
41
+
42
+ ## Connection Options
43
+
44
+ | Option | Type | Default Value | Description |
45
+ | ------ | ---- | ------------- | ----------- |
46
+ | `bindModels` | `boolean` | `true` | Bind the models provided to this connection (see the Mythix ORM [Connection Binding](https://github.com/th317erd/mythix-orm/wiki/ConnectionBinding) article for more information). |
47
+ | `connectionTimeout` | `number` | `60000` | Number of milliseconds to wait for connection. |
48
+ | `host` | `string` | `undefined` | The domain/host used to connect to the database. |
49
+ | `idleTransactionTimeout` | `number` | `undefined` | Number of milliseconds before terminating any session with an open idle transaction. |
50
+ | `logger` | Logger Interface | `undefined` | Assign a logger to the connection. If a logger is assigned, then every query (and every error) will be logged using this logger. |
51
+ | `maxPoolConnections` | `number` | `10` | Maximum number of clients the connection pool should contain. |
52
+ | `models` | `Array<Model>` | `undefined` | Models to register with the connection (these models will be bound to the connection if the `boundModels` option is `true`).
53
+ | `password` | `string` | `undefined` | The password used to connect to the database. |
54
+ | `port` | `string` | `5432` | The port used to connect to the database. |
55
+ | `queryGenerator` | [QueryGenerator](https://github.com/th317erd/mythix-orm/wiki/QueryGeneratorBase) | <see>PostgreSQLQueryGenerator</see> | Provide an alternate `QueryGenerator` interface for generating SQL statements for SQLite. This is not usually needed, as the `SQLiteConnection` itself will provide its own generator interface. However, if you want to customize the default query generator, or want to provide your own, you can do so using this option. |
56
+ | `queryTimeout` | `number` | `15000` | Number of milliseconds before a query call will timeout. |
57
+ | `statementTimeout` | `number` | `15000` | Number of milliseconds before a statement in query will time out. |
58
+ | `user` | `string` | `undefined` | The username used to connect to the database. |
package/docs/Home.md ADDED
@@ -0,0 +1,54 @@
1
+ SQLite database driver for [Mythix ORM](https://www.npmjs.com/package/mythix-orm).
2
+
3
+ ## Install
4
+
5
+ ```bash
6
+ npm i --save mythix-orm-postgresql
7
+ ```
8
+
9
+ ## Documentation
10
+
11
+ Documentation for Mythix ORM can be found at the [Mythix ORM WIKI](https://github.com/th317erd/mythix-orm/wiki).
12
+
13
+ ## Usage
14
+
15
+ ```javascript
16
+ const { PostgreSQLConnection } = require('mythix-orm-postgresql');
17
+
18
+ (async function() {
19
+ let connection = new PostgreSQLConnection({
20
+ bindModels: true,
21
+ models: [ /* application models */ ],
22
+ logger: console,
23
+ user: 'database-username',
24
+ password: 'database-password',
25
+ database: 'test-database',
26
+ host: '127.0.0.1',
27
+ port: 5432,
28
+ });
29
+
30
+ await connection.start();
31
+
32
+ // run application code
33
+
34
+ await connection.stop();
35
+ })();
36
+ ```
37
+
38
+ ## Connection Options
39
+
40
+ | Option | Type | Default Value | Description |
41
+ | ------ | ---- | ------------- | ----------- |
42
+ | `bindModels` | `boolean` | `true` | Bind the models provided to this connection (see the Mythix ORM [Connection Binding](https://github.com/th317erd/mythix-orm/wiki/ConnectionBinding) article for more information). |
43
+ | `connectionTimeout` | `number` | `60000` | Number of milliseconds to wait for connection. |
44
+ | `host` | `string` | `undefined` | The domain/host used to connect to the database. |
45
+ | `idleTransactionTimeout` | `number` | `undefined` | Number of milliseconds before terminating any session with an open idle transaction. |
46
+ | `logger` | Logger Interface | `undefined` | Assign a logger to the connection. If a logger is assigned, then every query (and every error) will be logged using this logger. |
47
+ | `maxPoolConnections` | `number` | `10` | Maximum number of clients the connection pool should contain. |
48
+ | `models` | `Array<Model>` | `undefined` | Models to register with the connection (these models will be bound to the connection if the `boundModels` option is `true`).
49
+ | `password` | `string` | `undefined` | The password used to connect to the database. |
50
+ | `port` | `string` | `5432` | The port used to connect to the database. |
51
+ | `queryGenerator` | [QueryGenerator](https://github.com/th317erd/mythix-orm/wiki/QueryGeneratorBase) | <see>PostgreSQLQueryGenerator</see> | Provide an alternate `QueryGenerator` interface for generating SQL statements for SQLite. This is not usually needed, as the `SQLiteConnection` itself will provide its own generator interface. However, if you want to customize the default query generator, or want to provide your own, you can do so using this option. |
52
+ | `queryTimeout` | `number` | `15000` | Number of milliseconds before a query call will timeout. |
53
+ | `statementTimeout` | `number` | `15000` | Number of milliseconds before a statement in query will time out. |
54
+ | `user` | `string` | `undefined` | The username used to connect to the database. |
@@ -19,11 +19,40 @@ function sleep(ms) {
19
19
  });
20
20
  }
21
21
 
22
+ /// Mythix ORM connection driver for PostgreSQL.
23
+ ///
24
+ /// This inherits from [SQLConnectionBase](https://github.com/th317erd/mythix-orm-sql-base/wiki)
25
+ /// and so gets most of its SQL functionality from its parent class.
26
+ ///
27
+ /// Extends: [SQLConnectionBase](https://github.com/th317erd/mythix-orm-sql-base/wiki)
22
28
  class PostgreSQLConnection extends SQLConnectionBase {
23
29
  static dialect = 'postgresql';
24
30
 
25
31
  static DefaultQueryGenerator = PostgreSQLQueryGenerator;
26
32
 
33
+ /// Create a new `PostgreSQLConnection` instance.
34
+ ///
35
+ /// Arguments:
36
+ /// options?: object
37
+ /// Options to provide to the connection. All options are optional, though `models`
38
+ /// is required before the connection is used. If not provided here to the constructor,
39
+ /// the application models can always be provided at a later time using the
40
+ /// [Connection.registerModels](https://github.com/th317erd/mythix-orm/wiki/ConnectionBase#method-registerModels) method.
41
+ /// | Option | Type | Default Value | Description |
42
+ /// | ------ | ---- | ------------- | ----------- |
43
+ /// | `bindModels` | `boolean` | `true` | Bind the models provided to this connection (see the Mythix ORM [Connection Binding](https://github.com/th317erd/mythix-orm/wiki/ConnectionBinding) article for more information). |
44
+ /// | `connectionTimeout` | `number` | `60000` | Number of milliseconds to wait for connection. |
45
+ /// | `host` | `string` | `undefined` | The domain/host used to connect to the database. |
46
+ /// | `idleTransactionTimeout` | `number` | `undefined` | Number of milliseconds before terminating any session with an open idle transaction. |
47
+ /// | `logger` | Logger Interface | `undefined` | Assign a logger to the connection. If a logger is assigned, then every query (and every error) will be logged using this logger. |
48
+ /// | `maxPoolConnections` | `number` | `10` | Maximum number of clients the connection pool should contain. |
49
+ /// | `models` | `Array<Model>` | `undefined` | Models to register with the connection (these models will be bound to the connection if the `boundModels` option is `true`).
50
+ /// | `password` | `string` | `undefined` | The password used to connect to the database. |
51
+ /// | `port` | `string` | `5432` | The port used to connect to the database. |
52
+ /// | `queryGenerator` | [QueryGenerator](https://github.com/th317erd/mythix-orm/wiki/QueryGeneratorBase) | <see>PostgreSQLQueryGenerator</see> | Provide an alternate `QueryGenerator` interface for generating SQL statements for PostgreSQL. This is not usually needed, as the `PostgreSQLConnection` itself will provide its own generator interface. However, if you want to customize the default query generator, or want to provide your own, you can do so using this option. |
53
+ /// | `queryTimeout` | `number` | `15000` | Number of milliseconds before a query call will timeout. |
54
+ /// | `statementTimeout` | `number` | `15000` | Number of milliseconds before a statement in query will time out. |
55
+ /// | `user` | `string` | `undefined` | The username used to connect to the database. |
27
56
  constructor(_options) {
28
57
  super(_options);
29
58
 
@@ -152,14 +181,35 @@ class PostgreSQLConnection extends SQLConnectionBase {
152
181
 
153
182
  // eslint-disable-next-line no-unused-vars
154
183
  async enableForeignKeyConstraints(enable) {
155
- // Not supported in PostgreSQL
156
- return;
184
+ throw new Error(`${this.constructor.name}::enableForeignKeyConstraints: This operation is not supported for this connection type.`);
157
185
  }
158
186
 
159
187
  async exec(...args) {
160
188
  return await this.query(...args);
161
189
  }
162
190
 
191
+ /// A raw query interface for the PostgreSQL database.
192
+ ///
193
+ /// This method is used internally to execute all
194
+ /// SQL statements generated against PostgreSQL. For
195
+ /// `SELECT` or `RETURNING` SQL statements, this will
196
+ /// return the results in a formatted object: `{ rows: [ ... ], columns: [ ... ] }`.
197
+ ///
198
+ /// Arguments:
199
+ /// sql: string
200
+ /// The fully formed SQL statement to execute.
201
+ /// options?: object
202
+ /// Options for the operation.
203
+ /// | Option | Type | Default Value | Description |
204
+ /// | ------ | ---- | ------------- | ----------- |
205
+ /// | `logger` | Logger Interface | `undefined` | If provided, then the query and any errors encountered will be logged |
206
+ /// | `logResponse` | `boolean` | `false` | If `true`, then the response from PostgreSQL will be logged |
207
+ /// | `parameters` | `Array` or `Object` | `undefined` | Parameters to bind for [pg](https://node-postgres.com/features/queries) |
208
+ ///
209
+ /// Return: any
210
+ /// A `{ rows: [ ... ], columns: [ ... ] }` object if the statement is a
211
+ /// `SELECT` statement, or a statement with a `RETURNING` clause. Otherwise
212
+ /// the raw result from PostgreSQL will be returned for the statement executed.
163
213
  async query(_sql, _options, _client) {
164
214
  let sql = _sql;
165
215
  if (!sql)
@@ -218,7 +268,20 @@ class PostgreSQLConnection extends SQLConnectionBase {
218
268
  }
219
269
  }
220
270
 
221
- formatResultsResponse(sqlStatement, result) {
271
+ /// Format the response from PostgreSQL for a `SELECT`
272
+ /// or `RETURNING` statement.
273
+ ///
274
+ /// Arguments:
275
+ /// sql: string
276
+ /// The SQL statement that was executed.
277
+ /// result: any
278
+ /// The response from PostgreSQL.
279
+ ///
280
+ /// Return: { rows: Array<any>, columns: Array<string> }
281
+ /// The formatted response, containing all the rows returned by the query, and all
282
+ /// projected columns. The columns are converted to names only, so as to keep the
283
+ /// query interface consistent across all database drivers Mythix ORM supports.
284
+ formatResultsResponse(sql, result) {
222
285
  if (!result.rows || !result.fields)
223
286
  return result;
224
287
 
@@ -228,6 +291,34 @@ class PostgreSQLConnection extends SQLConnectionBase {
228
291
  };
229
292
  }
230
293
 
294
+ /// This method will start a transaction, or, if a transaction
295
+ /// is already active, will instead start a `SAVEPOINT`.
296
+ ///
297
+ /// If an error is thrown in the provided `callback`, then the
298
+ /// transaction or `SAVEPOINT` will be automatically rolled back.
299
+ /// If the `callback` provided returns successfully, then the
300
+ /// transaction will be committed automatically for you.
301
+ ///
302
+ /// Arguments:
303
+ /// callback: (connection: PostgreSQLConnection) => any
304
+ /// The transaction connection is passed to this callback as soon as
305
+ /// the transaction or `SAVEPOINT` is started. This often isn't needed
306
+ /// if the global [AsyncLocalStorage](https://github.com/th317erd/mythix-orm/wiki/AsyncStore) context is supported and in-use
307
+ /// (the default). If the [AsyncLocalStorage](https://github.com/th317erd/mythix-orm/wiki/AsyncStore) context is not in-use,
308
+ /// then this `connection` **must** be passed all the way through all
309
+ /// database calls made inside this callback.
310
+ /// options?: object
311
+ /// Options for the transaction operation. This includes the options listed below, as well as any
312
+ /// options that can be passed to <see>PostgreSQLConnection.query</see>. The `lock` options object has
313
+ /// one PostgreSQL specific sub-option named `mode`, that can be one of `ACCESS EXCLUSIVE` (the default),
314
+ /// `EXCLUSIVE`, or `ACCESS SHARE`. To understand what these do, refer to the [PostgreSQL documentation](https://www.postgresql.org/docs/15/sql-begin.html).
315
+ /// | Option | Type | Default Value | Description |
316
+ /// | ------ | ---- | ------------- | ----------- |
317
+ /// | `connection` | `PostgreSQLConnection` | `undefined` | The connection to use for the operation. This is generally only needed if you are already inside a transaction, and need to supply the transaction connection to start a sub-transaction (a `SAVEPOINT`). |
318
+ /// | `lock | [Lock Mode](https://github.com/th317erd/mythix-orm/wiki/ConnectionBase#method-getLockMode) | `undefined` | Specify the lock mode for the transaction. See [ConnectionBase.getLockMode](https://github.com/th317erd/mythix-orm/wiki/ConnectionBase#method-getLockMode) for more information. |
319
+ ///
320
+ /// Return: any
321
+ /// Return the result of the provided `callback`.
231
322
  async transaction(callback, _options, _retryCount) {
232
323
  let options = _options || {};
233
324
  let inheritedThis = Object.create(options.connection || this.getContextValue('connection', this));
@@ -6,13 +6,25 @@ const { SQLQueryGeneratorBase } = require('mythix-orm-sql-base');
6
6
 
7
7
  const LiteralBase = Literals.LiteralBase;
8
8
 
9
+ /// The query generator interface for PostgreSQL.
10
+ ///
11
+ /// See [SQLQueryGeneratorBase](https://github.com/th317erd/mythix-orm-sql-base/wiki/SQLQueryGeneratorBase)
12
+ /// for a better understanding of this class. This modifies some of
13
+ /// the methods provided by [SQLQueryGeneratorBase](https://github.com/th317erd/mythix-orm-sql-base/wiki/SQLQueryGeneratorBase)
14
+ /// for PostgreSQL specific syntax.
15
+ ///
16
+ /// Extends: [SQLQueryGeneratorBase](https://github.com/th317erd/mythix-orm-sql-base/wiki/SQLQueryGeneratorBase)
9
17
  class PostgreSQLQueryGenerator extends SQLQueryGeneratorBase {
10
18
  // eslint-disable-next-line no-unused-vars
11
19
  generateSQLJoinTypeFromQueryEngineJoinType(joinType, outer, options) {
12
20
  if (!joinType || joinType === 'inner')
13
21
  return 'INNER JOIN';
14
22
  else if (joinType === 'left')
15
- return 'LEFT JOIN';
23
+ return (outer) ? 'LEFT OUTER JOIN' : 'LEFT JOIN';
24
+ else if (joinType === 'right')
25
+ return (outer) ? 'RIGHT OUTER JOIN' : 'RIGHT JOIN';
26
+ else if (joinType === 'full')
27
+ return (outer) ? 'FULL OUTER JOIN' : 'FULL JOIN';
16
28
  else if (joinType === 'cross')
17
29
  return 'CROSS JOIN';
18
30
 
@@ -89,6 +101,18 @@ class PostgreSQLQueryGenerator extends SQLQueryGeneratorBase {
89
101
  return super.generateColumnDeclarationStatement(Model, field, { ...options, noAutoIncrementDefault: true });
90
102
  }
91
103
 
104
+ /// Generate a `TRUNCATE TABLE` statement.
105
+ ///
106
+ /// Arguments:
107
+ /// Model: class [Model](https://github.com/th317erd/mythix-orm/wiki/Model)
108
+ /// The model that defines the table to truncate.
109
+ /// options?: object
110
+ /// Operation specific options.
111
+ /// | Option | Type | Default Value | Description |
112
+ /// | ------ | ---- | ------------- | ----------- |
113
+ /// | `cascade` | `boolean` | `true` | If `true`, then automatically truncate all tables that have foreign-key references to any of the named tables, or to any tables added to the group due to `CASCADE`. |
114
+ /// | `continueIdentity` | `boolean` | `false` | If `true`, then auto-incrementing ids will not be reset. |
115
+ /// | `onlySpecifiedTable` | `boolean` | `false` | If `true`, then only truncate this table. If `false` (the default), then all descendant tables (if any) are truncated as well. |
92
116
  generateTruncateTableStatement(Model, _options) {
93
117
  let options = _options || {};
94
118
  let escapedTableName = this.escapeID(Model.getTableName(this.connection));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mythix-orm-postgresql",
3
- "version": "1.10.1",
3
+ "version": "1.11.1",
4
4
  "description": "PostgreSQL driver for Mythix ORM",
5
5
  "main": "lib/index.js",
6
6
  "type": "commonjs",
@@ -33,8 +33,8 @@
33
33
  },
34
34
  "homepage": "https://github.com/th317erd/mythix-orm-postgresql#readme",
35
35
  "peerDependencies": {
36
- "mythix-orm": "^1.12.0",
37
- "mythix-orm-sql-base": "^1.10.1"
36
+ "mythix-orm": "^1.13.1",
37
+ "mythix-orm-sql-base": "^1.11.0"
38
38
  },
39
39
  "dependencies": {
40
40
  "luxon": "^3.1.0",