@things-factory/shell 10.1.123 → 10.1.142
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/dist-server/index.d.ts +1 -0
- package/dist-server/index.js +1 -0
- package/dist-server/index.js.map +1 -1
- package/dist-server/tsconfig.tsbuildinfo +1 -1
- package/dist-server/typeorm/write-lock.d.ts +16 -0
- package/dist-server/typeorm/write-lock.js +23 -0
- package/dist-server/typeorm/write-lock.js.map +1 -0
- package/package.json +2 -2
- package/tests/write-lock.test.ts +27 -0
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { EntityManager } from 'typeorm';
|
|
2
|
+
/**
|
|
3
|
+
* **Can this database hold a row for us while we decide?** — `SELECT … FOR UPDATE` where it exists.
|
|
4
|
+
*
|
|
5
|
+
* True only inside an open transaction on a driver that has row locks. sqlite has none, and TypeORM throws
|
|
6
|
+
* `LockNotSupportedOnGivenDriverError` if asked; it does not need one, because a single writer makes a second
|
|
7
|
+
* writing transaction wait for the first as a whole. Every other driver this platform runs on (postgres,
|
|
8
|
+
* mysql, mssql, oracle) takes the lock.
|
|
9
|
+
*
|
|
10
|
+
* Outside a transaction a lock would be released as soon as the statement ends, so it is not asked for.
|
|
11
|
+
*
|
|
12
|
+
* ```ts
|
|
13
|
+
* repository.findOne({ where, ...(writeLockSupported(tx) ? { lock: { mode: 'pessimistic_write' } } : {}) })
|
|
14
|
+
* ```
|
|
15
|
+
*/
|
|
16
|
+
export declare function writeLockSupported(manager: Pick<EntityManager, 'connection' | 'queryRunner'>): boolean;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.writeLockSupported = writeLockSupported;
|
|
4
|
+
/**
|
|
5
|
+
* **Can this database hold a row for us while we decide?** — `SELECT … FOR UPDATE` where it exists.
|
|
6
|
+
*
|
|
7
|
+
* True only inside an open transaction on a driver that has row locks. sqlite has none, and TypeORM throws
|
|
8
|
+
* `LockNotSupportedOnGivenDriverError` if asked; it does not need one, because a single writer makes a second
|
|
9
|
+
* writing transaction wait for the first as a whole. Every other driver this platform runs on (postgres,
|
|
10
|
+
* mysql, mssql, oracle) takes the lock.
|
|
11
|
+
*
|
|
12
|
+
* Outside a transaction a lock would be released as soon as the statement ends, so it is not asked for.
|
|
13
|
+
*
|
|
14
|
+
* ```ts
|
|
15
|
+
* repository.findOne({ where, ...(writeLockSupported(tx) ? { lock: { mode: 'pessimistic_write' } } : {}) })
|
|
16
|
+
* ```
|
|
17
|
+
*/
|
|
18
|
+
function writeLockSupported(manager) {
|
|
19
|
+
const type = manager.connection?.options?.type;
|
|
20
|
+
return Boolean(manager.queryRunner?.isTransactionActive) && !SINGLE_WRITER_DRIVERS.includes(type ?? '');
|
|
21
|
+
}
|
|
22
|
+
const SINGLE_WRITER_DRIVERS = ['sqlite', 'better-sqlite3', 'sqljs'];
|
|
23
|
+
//# sourceMappingURL=write-lock.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"write-lock.js","sourceRoot":"","sources":["../../server/typeorm/write-lock.ts"],"names":[],"mappings":";;AAgBA,gDAGC;AAjBD;;;;;;;;;;;;;GAaG;AACH,SAAgB,kBAAkB,CAAC,OAA0D;IAC3F,MAAM,IAAI,GAAG,OAAO,CAAC,UAAU,EAAE,OAAO,EAAE,IAA0B,CAAA;IACpE,OAAO,OAAO,CAAC,OAAO,CAAC,WAAW,EAAE,mBAAmB,CAAC,IAAI,CAAC,qBAAqB,CAAC,QAAQ,CAAC,IAAI,IAAI,EAAE,CAAC,CAAA;AACzG,CAAC;AAED,MAAM,qBAAqB,GAAG,CAAC,QAAQ,EAAE,gBAAgB,EAAE,OAAO,CAAC,CAAA","sourcesContent":["import type { EntityManager } from 'typeorm'\n\n/**\n * **Can this database hold a row for us while we decide?** — `SELECT … FOR UPDATE` where it exists.\n *\n * True only inside an open transaction on a driver that has row locks. sqlite has none, and TypeORM throws\n * `LockNotSupportedOnGivenDriverError` if asked; it does not need one, because a single writer makes a second\n * writing transaction wait for the first as a whole. Every other driver this platform runs on (postgres,\n * mysql, mssql, oracle) takes the lock.\n *\n * Outside a transaction a lock would be released as soon as the statement ends, so it is not asked for.\n *\n * ```ts\n * repository.findOne({ where, ...(writeLockSupported(tx) ? { lock: { mode: 'pessimistic_write' } } : {}) })\n * ```\n */\nexport function writeLockSupported(manager: Pick<EntityManager, 'connection' | 'queryRunner'>): boolean {\n const type = manager.connection?.options?.type as string | undefined\n return Boolean(manager.queryRunner?.isTransactionActive) && !SINGLE_WRITER_DRIVERS.includes(type ?? '')\n}\n\nconst SINGLE_WRITER_DRIVERS = ['sqlite', 'better-sqlite3', 'sqljs']\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@things-factory/shell",
|
|
3
|
-
"version": "10.1.
|
|
3
|
+
"version": "10.1.142",
|
|
4
4
|
"description": "Core module for framework",
|
|
5
5
|
"bin": {
|
|
6
6
|
"things-factory": "bin/things-factory",
|
|
@@ -126,5 +126,5 @@
|
|
|
126
126
|
"pg": "^8.7.3",
|
|
127
127
|
"sqlite3": "^5.0.8"
|
|
128
128
|
},
|
|
129
|
-
"gitHead": "
|
|
129
|
+
"gitHead": "73522201151b0156168b0eeea15f2962cda0bd8f"
|
|
130
130
|
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { writeLockSupported } from '../server/typeorm/write-lock'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* A row lock is asked for only where the driver has one and a transaction will hold it.
|
|
5
|
+
*
|
|
6
|
+
* Asking sqlite throws `LockNotSupportedOnGivenDriverError`; asking outside a transaction locks nothing.
|
|
7
|
+
*/
|
|
8
|
+
const manager = (type: string, isTransactionActive?: boolean) =>
|
|
9
|
+
({
|
|
10
|
+
connection: { options: { type } },
|
|
11
|
+
queryRunner: isTransactionActive === undefined ? undefined : { isTransactionActive }
|
|
12
|
+
}) as any
|
|
13
|
+
|
|
14
|
+
describe('writeLockSupported', () => {
|
|
15
|
+
it.each(['postgres', 'mysql', 'mariadb', 'mssql', 'oracle'])('locks on %s inside a transaction', type => {
|
|
16
|
+
expect(writeLockSupported(manager(type, true))).toBe(true)
|
|
17
|
+
})
|
|
18
|
+
|
|
19
|
+
it.each(['sqlite', 'better-sqlite3', 'sqljs'])('does not lock on %s, which has a single writer', type => {
|
|
20
|
+
expect(writeLockSupported(manager(type, true))).toBe(false)
|
|
21
|
+
})
|
|
22
|
+
|
|
23
|
+
it('does not lock outside a transaction', () => {
|
|
24
|
+
expect(writeLockSupported(manager('postgres', false))).toBe(false)
|
|
25
|
+
expect(writeLockSupported(manager('postgres'))).toBe(false)
|
|
26
|
+
})
|
|
27
|
+
})
|