orange-orm 4.1.2 → 4.1.3

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/docs/changelog.md CHANGED
@@ -1,4 +1,6 @@
1
1
  ## Changelog
2
+ __4.1.3__
3
+ Do not use transaction in readonly operations. [#109](https://github.com/alfateam/orange-orm/issues/109)
2
4
  __4.1.2__
3
5
  Bugfix with composite primary key with hasMany relation. [#106](https://github.com/alfateam/orange-orm/issues/106)
4
6
  __4.1.1__
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "orange-orm",
3
- "version": "4.1.2",
3
+ "version": "4.1.3",
4
4
  "main": "./src/index.js",
5
5
  "browser": "./src/client/index.mjs",
6
6
  "bin": {
package/src/hostLocal.js CHANGED
@@ -3,7 +3,7 @@ let getMeta = require('./hostExpress/getMeta');
3
3
  let setSessionSingleton = require('./table/setSessionSingleton');
4
4
  let executeQuery = require('./query');
5
5
  let hostExpress = require('./hostExpress');
6
-
6
+ const readonlyOps = ['getManyDto', 'getMany', 'aggregate', 'count'];
7
7
  // { db, table, defaultConcurrency,
8
8
  // concurrency,
9
9
  // customFilters,
@@ -20,7 +20,7 @@ function hostLocal() {
20
20
  return getMeta(table);
21
21
 
22
22
  }
23
- async function patch(body,_req, _res) {
23
+ async function patch(body, _req, _res) {
24
24
  if (!table) {
25
25
  const error = new Error('Table is not exposed');
26
26
  // @ts-ignore
@@ -65,7 +65,10 @@ function hostLocal() {
65
65
  else
66
66
  db = dbPromise;
67
67
  }
68
- await db.transaction(fn);
68
+ if (readonlyOps.includes(body.path))
69
+ await db.transaction({ readonly: true }, fn);
70
+ else
71
+ await db.transaction(fn);
69
72
  }
70
73
  return result;
71
74
 
@@ -101,7 +104,7 @@ function hostLocal() {
101
104
  }
102
105
 
103
106
  function express(client, options) {
104
- return hostExpress(client, options );
107
+ return hostExpress(client, options);
105
108
  }
106
109
 
107
110
  return c;
@@ -1,6 +1,6 @@
1
1
  let createDomain = require('../createDomain');
2
2
  let newTransaction = require('./newTransaction');
3
- let begin = require('../table/begin');
3
+ let _begin = require('../table/begin');
4
4
  let commit = require('../table/commit');
5
5
  let rollback = require('../table/rollback');
6
6
  let newPool = require('./newPool');
@@ -19,11 +19,11 @@ function newDatabase(connectionString, poolOptions) {
19
19
  throw new Error('Connection string cannot be empty');
20
20
  var pool;
21
21
  if (!poolOptions)
22
- pool = newPool.bind(null,connectionString, poolOptions);
22
+ pool = newPool.bind(null, connectionString, poolOptions);
23
23
  else
24
24
  pool = newPool(connectionString, poolOptions);
25
25
 
26
- let c = {poolFactory: pool, hostLocal, express};
26
+ let c = { poolFactory: pool, hostLocal, express };
27
27
 
28
28
  c.transaction = function(options, fn) {
29
29
  if ((arguments.length === 1) && (typeof options === 'function')) {
@@ -53,6 +53,10 @@ function newDatabase(connectionString, poolOptions) {
53
53
  return result;
54
54
  }
55
55
 
56
+ function begin() {
57
+ return _begin(options?.readonly);
58
+ }
59
+
56
60
  function run() {
57
61
  let p;
58
62
  let transaction = newTransaction(domain, pool);
@@ -69,7 +73,7 @@ function newDatabase(connectionString, poolOptions) {
69
73
  c.createTransaction = function() {
70
74
  let domain = createDomain();
71
75
  let transaction = newTransaction(domain, pool);
72
- let p = domain.run(() => new Promise(transaction).then(begin));
76
+ let p = domain.run(() => new Promise(transaction).then(_begin));
73
77
 
74
78
  function run(fn) {
75
79
  return p.then(domain.run.bind(domain, fn));
@@ -1,6 +1,6 @@
1
1
  let createDomain = require('../createDomain');
2
2
  let newTransaction = require('./newTransaction');
3
- let begin = require('../table/begin');
3
+ let _begin = require('../table/begin');
4
4
  let commit = require('../table/commit');
5
5
  let rollback = require('../table/rollback');
6
6
  let newPool = require('./newPool');
@@ -52,6 +52,10 @@ function newDatabase(connectionString, poolOptions) {
52
52
  return result;
53
53
  }
54
54
 
55
+ function begin() {
56
+ return _begin(options?.readonly);
57
+ }
58
+
55
59
  function run() {
56
60
  let p;
57
61
  let transaction = newTransaction(domain, pool);
@@ -68,7 +72,7 @@ function newDatabase(connectionString, poolOptions) {
68
72
  c.createTransaction = function() {
69
73
  let domain = createDomain();
70
74
  let transaction = newTransaction(domain, pool);
71
- let p = domain.run(() => new Promise(transaction).then(begin));
75
+ let p = domain.run(() => new Promise(transaction).then(_begin));
72
76
 
73
77
  function run(fn) {
74
78
  return p.then(domain.run.bind(domain, fn));
@@ -1,6 +1,6 @@
1
1
  let createDomain = require('../createDomain');
2
2
  let newTransaction = require('./newTransaction');
3
- let begin = require('../table/begin');
3
+ let _begin = require('../table/begin');
4
4
  let commit = require('../table/commit');
5
5
  let rollback = require('../table/rollback');
6
6
  let newPool = require('./newPool');
@@ -19,11 +19,11 @@ function newDatabase(connectionString, poolOptions) {
19
19
  throw new Error('Connection string cannot be empty');
20
20
  var pool;
21
21
  if (!poolOptions)
22
- pool = newPool.bind(null,connectionString, poolOptions);
22
+ pool = newPool.bind(null, connectionString, poolOptions);
23
23
  else
24
24
  pool = newPool(connectionString, poolOptions);
25
25
 
26
- let c = {poolFactory: pool, hostLocal, express};
26
+ let c = { poolFactory: pool, hostLocal, express };
27
27
 
28
28
  c.transaction = function(options, fn) {
29
29
  if ((arguments.length === 1) && (typeof options === 'function')) {
@@ -41,6 +41,10 @@ function newDatabase(connectionString, poolOptions) {
41
41
  else
42
42
  return domain.run(run);
43
43
 
44
+ function begin() {
45
+ return _begin(options?.readonly);
46
+ }
47
+
44
48
  async function runInTransaction() {
45
49
  let result;
46
50
  let transaction = newTransaction(domain, pool);
@@ -69,7 +73,7 @@ function newDatabase(connectionString, poolOptions) {
69
73
  c.createTransaction = function() {
70
74
  let domain = createDomain();
71
75
  let transaction = newTransaction(domain, pool);
72
- let p = domain.run(() => new Promise(transaction).then(begin));
76
+ let p = domain.run(() => new Promise(transaction).then(_begin));
73
77
 
74
78
  function run(fn) {
75
79
  return p.then(domain.run.bind(domain, fn));
@@ -1,6 +1,6 @@
1
1
  let createDomain = require('../createDomain');
2
2
  let newTransaction = require('./newTransaction');
3
- let begin = require('../table/begin');
3
+ let _begin = require('../table/begin');
4
4
  let commit = require('../table/commit');
5
5
  let rollback = require('../table/rollback');
6
6
  let newPool = require('./newPool');
@@ -48,6 +48,8 @@ function newDatabase(connectionString, poolOptions) {
48
48
  else
49
49
  return domain.run(run);
50
50
 
51
+
52
+
51
53
  async function runInTransaction() {
52
54
  let result;
53
55
  let transaction = newTransaction(domain, pool);
@@ -61,6 +63,10 @@ function newDatabase(connectionString, poolOptions) {
61
63
  return result;
62
64
  }
63
65
 
66
+ function begin() {
67
+ return _begin(options?.readonly);
68
+ }
69
+
64
70
  function run() {
65
71
  let p;
66
72
  let transaction = newTransaction(domain, pool);
@@ -84,7 +90,7 @@ function newDatabase(connectionString, poolOptions) {
84
90
  c.createTransaction = function(options) {
85
91
  let domain = createDomain();
86
92
  let transaction = newTransaction(domain, pool);
87
- let p = domain.run(() => new Promise(transaction).then(begin).then(negotiateSchema));
93
+ let p = domain.run(() => new Promise(transaction).then(_begin).then(negotiateSchema));
88
94
 
89
95
  function run(fn) {
90
96
  return p.then(domain.run.bind(domain, fn));
@@ -1,6 +1,6 @@
1
1
  let createDomain = require('../createDomain');
2
2
  let newTransaction = require('./newTransaction');
3
- let begin = require('../table/begin');
3
+ let _begin = require('../table/begin');
4
4
  let commit = require('../table/commit');
5
5
  let rollback = require('../table/rollback');
6
6
  let newPool = require('./newPool');
@@ -41,6 +41,10 @@ function newDatabase(connectionString, poolOptions) {
41
41
  else
42
42
  return domain.run(run);
43
43
 
44
+ function begin() {
45
+ return _begin(options?.readonly);
46
+ }
47
+
44
48
  async function runInTransaction() {
45
49
  let result;
46
50
  let transaction = newTransaction(domain, pool);
@@ -69,7 +73,7 @@ function newDatabase(connectionString, poolOptions) {
69
73
  c.createTransaction = function() {
70
74
  let domain = createDomain();
71
75
  let transaction = newTransaction(domain, pool);
72
- let p = domain.run(() => new Promise(transaction).then(begin));
76
+ let p = domain.run(() => new Promise(transaction).then(_begin));
73
77
 
74
78
  function run(fn) {
75
79
  return p.then(domain.run.bind(domain, fn));
@@ -1,6 +1,6 @@
1
1
  let createDomain = require('../createDomain');
2
2
  let newTransaction = require('./newTransaction');
3
- let begin = require('../table/begin');
3
+ let _begin = require('../table/begin');
4
4
  let commit = require('../table/commit');
5
5
  let rollback = require('../table/rollback');
6
6
  let newPool = require('./newPool');
@@ -41,6 +41,10 @@ function newDatabase(connectionString, poolOptions) {
41
41
  else
42
42
  return domain.run(run);
43
43
 
44
+ function begin() {
45
+ return _begin(options?.readonly);
46
+ }
47
+
44
48
  async function runInTransaction() {
45
49
  let result;
46
50
  let transaction = newTransaction(domain, pool);
@@ -69,7 +73,7 @@ function newDatabase(connectionString, poolOptions) {
69
73
  c.createTransaction = function() {
70
74
  let domain = createDomain();
71
75
  let transaction = newTransaction(domain, pool);
72
- let p = domain.run(() => new Promise(transaction).then(begin));
76
+ let p = domain.run(() => new Promise(transaction).then(_begin));
73
77
 
74
78
  function run(fn) {
75
79
  return p.then(domain.run.bind(domain, fn));
@@ -2,8 +2,12 @@ let beginCommand = require('./commands/beginCommand');
2
2
  let executeQuery = require('./executeQueries/executeQuery');
3
3
  let setSessionSingleton = require('./setSessionSingleton');
4
4
 
5
- function begin() {
5
+ function begin(readonly) {
6
6
  setSessionSingleton('changes', []);
7
+ if (readonly) {
8
+ setSessionSingleton('readonly', true);
9
+ return Promise.resolve();
10
+ }
7
11
  return executeQuery(beginCommand());
8
12
  }
9
13
 
@@ -3,6 +3,7 @@ let pushCommand = require('./commands/pushCommand');
3
3
  let executeChanges = require('./executeQueries/executeChanges');
4
4
  let releaseDbClient = require('./releaseDbClient');
5
5
  let popChanges = require('./popChanges');
6
+ const getSessionSingleton = require('./getSessionSingleton');
6
7
 
7
8
  function commit(result) {
8
9
  return popAndPushChanges()
@@ -19,7 +20,8 @@ function commit(result) {
19
20
  await executeChanges(changes);
20
21
  changes = popChanges();
21
22
  }
22
- pushCommand(commitCommand);
23
+ if (!getSessionSingleton('readonly'))
24
+ pushCommand(commitCommand);
23
25
  return executeChanges(popChanges());
24
26
  }
25
27
  }
@@ -1,6 +1,6 @@
1
1
  let createDomain = require('../createDomain');
2
2
  let newTransaction = require('./newTransaction');
3
- let begin = require('../table/begin');
3
+ let _begin = require('../table/begin');
4
4
  let commit = require('../table/commit');
5
5
  let rollback = require('../table/rollback');
6
6
  let newPool = require('./newPool');
@@ -19,11 +19,11 @@ function newDatabase(connectionString, poolOptions) {
19
19
  throw new Error('Connection string cannot be empty');
20
20
  var pool;
21
21
  if (!poolOptions)
22
- pool = newPool.bind(null,connectionString, poolOptions);
22
+ pool = newPool.bind(null, connectionString, poolOptions);
23
23
  else
24
24
  pool = newPool(connectionString, poolOptions);
25
25
 
26
- let c = {poolFactory: pool, hostLocal, express};
26
+ let c = { poolFactory: pool, hostLocal, express };
27
27
 
28
28
  c.transaction = function(options, fn) {
29
29
  if ((arguments.length === 1) && (typeof options === 'function')) {
@@ -41,6 +41,10 @@ function newDatabase(connectionString, poolOptions) {
41
41
  else
42
42
  return domain.run(run);
43
43
 
44
+ function begin() {
45
+ return _begin(options?.readonly);
46
+ }
47
+
44
48
  async function runInTransaction() {
45
49
  let result;
46
50
  let transaction = newTransaction(domain, pool);
@@ -53,6 +57,7 @@ function newDatabase(connectionString, poolOptions) {
53
57
  return result;
54
58
  }
55
59
 
60
+
56
61
  function run() {
57
62
  let p;
58
63
  let transaction = newTransaction(domain, pool);
@@ -69,7 +74,7 @@ function newDatabase(connectionString, poolOptions) {
69
74
  c.createTransaction = function() {
70
75
  let domain = createDomain();
71
76
  let transaction = newTransaction(domain, pool);
72
- let p = domain.run(() => new Promise(transaction).then(begin));
77
+ let p = domain.run(() => new Promise(transaction).then(_begin));
73
78
 
74
79
  function run(fn) {
75
80
  return p.then(domain.run.bind(domain, fn));