@lakutata/cache 3.0.0-beta.5 → 3.0.0-beta.6

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.
@@ -34,7 +34,21 @@ var __importStar = (this && this.__importStar) || (function () {
34
34
  })();
35
35
  Object.defineProperty(exports, "__esModule", { value: true });
36
36
  exports.loadDriver = loadDriver;
37
+ const core_1 = require("@lakutata/core");
37
38
  const CacheDriverNotFoundException_js_1 = require("../exceptions/CacheDriverNotFoundException.js");
39
+ /**
40
+ * The driver packages of the stores (optional dependencies), imported by their names written out: the bundlers follow
41
+ * them (a name given at run time is not bundled, the bundle has no node_modules to load it from)
42
+ */
43
+ const DRIVERS = {
44
+ 'pg': () => Promise.resolve().then(() => __importStar(require('pg'))),
45
+ 'redis': () => Promise.resolve().then(() => __importStar(require('redis'))),
46
+ '@redis/client': () => Promise.resolve().then(() => __importStar(require('@redis/client'))),
47
+ 'mysql2': () => Promise.resolve().then(() => __importStar(require('mysql2'))),
48
+ 'sqlite3': () => Promise.resolve().then(() => __importStar(require('sqlite3'))),
49
+ 'mongodb': () => Promise.resolve().then(() => __importStar(require('mongodb'))),
50
+ 'memjs': () => Promise.resolve().then(() => __importStar(require('memjs')))
51
+ };
38
52
  /**
39
53
  * Whether an error reports the package itself as missing
40
54
  * @param error
@@ -52,15 +66,22 @@ function isPackageMissing(error, packageName) {
52
66
  * @param packageNames
53
67
  */
54
68
  async function loadDriver(member, ...packageNames) {
69
+ let missing;
55
70
  for (const packageName of packageNames) {
56
71
  try {
57
- const driver = await Promise.resolve(`${packageName}`).then(s => __importStar(require(s)));
72
+ const driver = await (DRIVERS[packageName] ?? (() => Promise.resolve(`${packageName}`).then(s => __importStar(require(s)))))();
58
73
  return (driver[member] === undefined && driver.default ? driver.default : driver);
59
74
  }
60
75
  catch (e) {
61
76
  if (!isPackageMissing(e, packageName))
62
77
  throw e;
78
+ missing ??= e;
63
79
  }
64
80
  }
65
- throw new CacheDriverNotFoundException_js_1.CacheDriverNotFoundException('Package "{packageName}" is required for this driver. Run "npm install {packageName}".', { packageName: packageNames[0] });
81
+ //A bundled application has the packages installed when it was bundled
82
+ const exception = (0, core_1.isPackaged)() ?
83
+ new CacheDriverNotFoundException_js_1.CacheDriverNotFoundException('Package "{packageName}" is required for this driver, and was not installed when the application was bundled: install it in the project, then bundle the application again.', { packageName: packageNames[0] }) :
84
+ new CacheDriverNotFoundException_js_1.CacheDriverNotFoundException('Package "{packageName}" is required for this driver. Run "npm install {packageName}".', { packageName: packageNames[0] });
85
+ exception.cause = missing;
86
+ throw exception;
66
87
  }
@@ -1,4 +1,18 @@
1
+ import { isPackaged } from '@lakutata/core';
1
2
  import { CacheDriverNotFoundException } from '../exceptions/CacheDriverNotFoundException.js';
3
+ /**
4
+ * The driver packages of the stores (optional dependencies), imported by their names written out: the bundlers follow
5
+ * them (a name given at run time is not bundled, the bundle has no node_modules to load it from)
6
+ */
7
+ const DRIVERS = {
8
+ 'pg': () => import('pg'),
9
+ 'redis': () => import('redis'),
10
+ '@redis/client': () => import('@redis/client'),
11
+ 'mysql2': () => import('mysql2'),
12
+ 'sqlite3': () => import('sqlite3'),
13
+ 'mongodb': () => import('mongodb'),
14
+ 'memjs': () => import('memjs')
15
+ };
2
16
  /**
3
17
  * Whether an error reports the package itself as missing
4
18
  * @param error
@@ -16,15 +30,22 @@ function isPackageMissing(error, packageName) {
16
30
  * @param packageNames
17
31
  */
18
32
  export async function loadDriver(member, ...packageNames) {
33
+ let missing;
19
34
  for (const packageName of packageNames) {
20
35
  try {
21
- const driver = await import(packageName);
36
+ const driver = await (DRIVERS[packageName] ?? (() => import(packageName)))();
22
37
  return (driver[member] === undefined && driver.default ? driver.default : driver);
23
38
  }
24
39
  catch (e) {
25
40
  if (!isPackageMissing(e, packageName))
26
41
  throw e;
42
+ missing ??= e;
27
43
  }
28
44
  }
29
- throw new CacheDriverNotFoundException('Package "{packageName}" is required for this driver. Run "npm install {packageName}".', { packageName: packageNames[0] });
45
+ //A bundled application has the packages installed when it was bundled
46
+ const exception = isPackaged() ?
47
+ new CacheDriverNotFoundException('Package "{packageName}" is required for this driver, and was not installed when the application was bundled: install it in the project, then bundle the application again.', { packageName: packageNames[0] }) :
48
+ new CacheDriverNotFoundException('Package "{packageName}" is required for this driver. Run "npm install {packageName}".', { packageName: packageNames[0] });
49
+ exception.cause = missing;
50
+ throw exception;
30
51
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lakutata/cache",
3
- "version": "3.0.0-beta.5",
3
+ "version": "3.0.0-beta.6",
4
4
  "description": "The cache component of the lakutata framework (memory, file, Redis, Memcache, MongoDB, SQLite, PostgreSQL, MySQL stores).",
5
5
  "type": "module",
6
6
  "main": "./dist/cjs/exports/Cache.js",
@@ -30,8 +30,9 @@
30
30
  "test:stores": "npm run compile && CACHE_TEST_STORES=1 node --test --test-concurrency=1 \"dist/esm/tests/stores/*.spec.js\""
31
31
  },
32
32
  "peerDependencies": {
33
- "@lakutata/core": "3.0.0-beta.5",
34
- "@lakutata/utils": "3.0.0-beta.5",
33
+ "@lakutata/core": "3.0.0-beta.6",
34
+ "@lakutata/utils": "3.0.0-beta.6",
35
+ "@redis/client": "^1.5.0 || ^5.0.0",
35
36
  "memjs": "^1.3.2",
36
37
  "mongodb": ">=6.0.0",
37
38
  "mysql2": "^3.0.0",
@@ -40,6 +41,9 @@
40
41
  "sqlite3": "^5.0.0"
41
42
  },
42
43
  "peerDependenciesMeta": {
44
+ "@redis/client": {
45
+ "optional": true
46
+ },
43
47
  "memjs": {
44
48
  "optional": true
45
49
  },