typeorm 0.3.26-dev.f351757 → 0.3.27-dev.22b26d1
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/README.md +16 -1110
- package/browser/cache/RedisQueryResultCache.d.ts +13 -0
- package/browser/cache/RedisQueryResultCache.js +105 -38
- package/browser/cache/RedisQueryResultCache.js.map +1 -1
- package/browser/decorator/options/ColumnOptions.d.ts +6 -0
- package/browser/decorator/options/ColumnOptions.js.map +1 -1
- package/browser/decorator/options/VirtualColumnOptions.d.ts +5 -0
- package/browser/decorator/options/VirtualColumnOptions.js.map +1 -1
- package/browser/driver/cockroachdb/CockroachDriver.js +2 -2
- package/browser/driver/cockroachdb/CockroachDriver.js.map +1 -1
- package/browser/driver/mongodb/MongoConnectionOptions.d.ts +9 -9
- package/browser/driver/mongodb/MongoConnectionOptions.js.map +1 -1
- package/browser/driver/mysql/MysqlDriver.js +3 -1
- package/browser/driver/mysql/MysqlDriver.js.map +1 -1
- package/browser/driver/postgres/PostgresDriver.js +2 -2
- package/browser/driver/postgres/PostgresDriver.js.map +1 -1
- package/browser/driver/postgres/PostgresQueryRunner.js +1 -1
- package/browser/driver/postgres/PostgresQueryRunner.js.map +1 -1
- package/browser/entity-schema/EntitySchemaColumnOptions.d.ts +10 -0
- package/browser/entity-schema/EntitySchemaColumnOptions.js.map +1 -1
- package/browser/entity-schema/EntitySchemaTransformer.js +3 -0
- package/browser/entity-schema/EntitySchemaTransformer.js.map +1 -1
- package/browser/error/DriverPackageNotInstalledError.js +1 -2
- package/browser/error/DriverPackageNotInstalledError.js.map +1 -1
- package/browser/platform/BrowserPlatformTools.js +7 -3
- package/browser/platform/BrowserPlatformTools.js.map +1 -1
- package/browser/query-builder/SelectQueryBuilder.js +3 -6
- package/browser/query-builder/SelectQueryBuilder.js.map +1 -1
- package/browser/repository/SaveOptions.d.ts +1 -1
- package/browser/repository/SaveOptions.js.map +1 -1
- package/browser/util/ImportUtils.js +29 -1
- package/browser/util/ImportUtils.js.map +1 -1
- package/cache/RedisQueryResultCache.d.ts +13 -0
- package/cache/RedisQueryResultCache.js +105 -38
- package/cache/RedisQueryResultCache.js.map +1 -1
- package/decorator/options/ColumnOptions.d.ts +6 -0
- package/decorator/options/ColumnOptions.js.map +1 -1
- package/decorator/options/VirtualColumnOptions.d.ts +5 -0
- package/decorator/options/VirtualColumnOptions.js.map +1 -1
- package/driver/cockroachdb/CockroachDriver.js +2 -2
- package/driver/cockroachdb/CockroachDriver.js.map +1 -1
- package/driver/mongodb/MongoConnectionOptions.d.ts +9 -9
- package/driver/mongodb/MongoConnectionOptions.js.map +1 -1
- package/driver/mysql/MysqlDriver.js +3 -1
- package/driver/mysql/MysqlDriver.js.map +1 -1
- package/driver/postgres/PostgresDriver.js +2 -2
- package/driver/postgres/PostgresDriver.js.map +1 -1
- package/driver/postgres/PostgresQueryRunner.js +1 -1
- package/driver/postgres/PostgresQueryRunner.js.map +1 -1
- package/entity-schema/EntitySchemaColumnOptions.d.ts +10 -0
- package/entity-schema/EntitySchemaColumnOptions.js.map +1 -1
- package/entity-schema/EntitySchemaTransformer.js +3 -0
- package/entity-schema/EntitySchemaTransformer.js.map +1 -1
- package/error/DriverPackageNotInstalledError.js +1 -2
- package/error/DriverPackageNotInstalledError.js.map +1 -1
- package/package.json +1 -1
- package/query-builder/SelectQueryBuilder.js +3 -6
- package/query-builder/SelectQueryBuilder.js.map +1 -1
- package/repository/SaveOptions.d.ts +1 -1
- package/repository/SaveOptions.js.map +1 -1
- package/util/ImportUtils.js +29 -1
- package/util/ImportUtils.js.map +1 -1
|
@@ -28,7 +28,7 @@ export interface SaveOptions {
|
|
|
28
28
|
* Flag to determine whether the entity that is being persisted
|
|
29
29
|
* should be reloaded during the persistence operation.
|
|
30
30
|
*
|
|
31
|
-
* It will work only on databases which
|
|
31
|
+
* It will work only on databases which do not support RETURNING / OUTPUT statement.
|
|
32
32
|
* Enabled by default.
|
|
33
33
|
*/
|
|
34
34
|
reload?: boolean;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../browser/src/repository/SaveOptions.ts"],"names":[],"mappings":"","file":"SaveOptions.js","sourcesContent":["/**\n * Special options passed to Repository#save, Repository#insert and Repository#update methods.\n */\nexport interface SaveOptions {\n /**\n * Additional data to be passed with persist method.\n * This data can be used in subscribers then.\n */\n data?: any\n\n /**\n * Indicates if listeners and subscribers are called for this operation.\n * By default they are enabled, you can disable them by setting { listeners: false } in save/remove options.\n */\n listeners?: boolean\n\n /**\n * By default transactions are enabled and all queries in persistence operation are wrapped into the transaction.\n * You can disable this behaviour by setting { transaction: false } in the persistence options.\n */\n transaction?: boolean\n\n /**\n * Breaks save execution into chunks of a given size.\n * For example, if you want to save 100,000 objects but you have issues with saving them,\n * you can break them into 10 groups of 10,000 objects (by setting { chunk: 10000 }) and save each group separately.\n * This option is needed to perform very big insertions when you have issues with underlying driver parameter number limitation.\n */\n chunk?: number\n\n /**\n * Flag to determine whether the entity that is being persisted\n * should be reloaded during the persistence operation.\n *\n * It will work only on databases which
|
|
1
|
+
{"version":3,"sources":["../browser/src/repository/SaveOptions.ts"],"names":[],"mappings":"","file":"SaveOptions.js","sourcesContent":["/**\n * Special options passed to Repository#save, Repository#insert and Repository#update methods.\n */\nexport interface SaveOptions {\n /**\n * Additional data to be passed with persist method.\n * This data can be used in subscribers then.\n */\n data?: any\n\n /**\n * Indicates if listeners and subscribers are called for this operation.\n * By default they are enabled, you can disable them by setting { listeners: false } in save/remove options.\n */\n listeners?: boolean\n\n /**\n * By default transactions are enabled and all queries in persistence operation are wrapped into the transaction.\n * You can disable this behaviour by setting { transaction: false } in the persistence options.\n */\n transaction?: boolean\n\n /**\n * Breaks save execution into chunks of a given size.\n * For example, if you want to save 100,000 objects but you have issues with saving them,\n * you can break them into 10 groups of 10,000 objects (by setting { chunk: 10000 }) and save each group separately.\n * This option is needed to perform very big insertions when you have issues with underlying driver parameter number limitation.\n */\n chunk?: number\n\n /**\n * Flag to determine whether the entity that is being persisted\n * should be reloaded during the persistence operation.\n *\n * It will work only on databases which do not support RETURNING / OUTPUT statement.\n * Enabled by default.\n */\n reload?: boolean\n}\n"],"sourceRoot":".."}
|
|
@@ -35,10 +35,32 @@ export async function importOrRequireFile(filePath) {
|
|
|
35
35
|
}
|
|
36
36
|
return tryToRequire();
|
|
37
37
|
}
|
|
38
|
+
const packageJsonCache = new Map();
|
|
39
|
+
const MAX_CACHE_SIZE = 1000;
|
|
40
|
+
function setPackageJsonCache(paths, packageJson) {
|
|
41
|
+
for (const path of paths) {
|
|
42
|
+
// Simple LRU-like behavior: if we're at capacity, remove oldest entry
|
|
43
|
+
if (packageJsonCache.size >= MAX_CACHE_SIZE &&
|
|
44
|
+
!packageJsonCache.has(path)) {
|
|
45
|
+
const firstKey = packageJsonCache.keys().next().value;
|
|
46
|
+
if (firstKey)
|
|
47
|
+
packageJsonCache.delete(firstKey);
|
|
48
|
+
}
|
|
49
|
+
packageJsonCache.set(path, packageJson);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
38
52
|
async function getNearestPackageJson(filePath) {
|
|
39
53
|
let currentPath = filePath;
|
|
54
|
+
const paths = [];
|
|
40
55
|
while (currentPath !== path.dirname(currentPath)) {
|
|
41
56
|
currentPath = path.dirname(currentPath);
|
|
57
|
+
// Check if we have already cached the package.json for this path
|
|
58
|
+
if (packageJsonCache.has(currentPath)) {
|
|
59
|
+
setPackageJsonCache(paths, packageJsonCache.get(currentPath));
|
|
60
|
+
return packageJsonCache.get(currentPath);
|
|
61
|
+
}
|
|
62
|
+
// Add the current path to the list of paths to cache
|
|
63
|
+
paths.push(currentPath);
|
|
42
64
|
const potentialPackageJson = path.join(currentPath, "package.json");
|
|
43
65
|
try {
|
|
44
66
|
const stats = await fs.stat(potentialPackageJson);
|
|
@@ -46,9 +68,14 @@ async function getNearestPackageJson(filePath) {
|
|
|
46
68
|
continue;
|
|
47
69
|
}
|
|
48
70
|
try {
|
|
49
|
-
|
|
71
|
+
const parsedPackage = JSON.parse(await fs.readFile(potentialPackageJson, "utf8"));
|
|
72
|
+
// Cache the parsed package.json object and return it
|
|
73
|
+
setPackageJsonCache(paths, parsedPackage);
|
|
74
|
+
return parsedPackage;
|
|
50
75
|
}
|
|
51
76
|
catch {
|
|
77
|
+
// If parsing fails, we still cache null to avoid repeated attempts
|
|
78
|
+
setPackageJsonCache(paths, null);
|
|
52
79
|
return null;
|
|
53
80
|
}
|
|
54
81
|
}
|
|
@@ -57,6 +84,7 @@ async function getNearestPackageJson(filePath) {
|
|
|
57
84
|
}
|
|
58
85
|
}
|
|
59
86
|
// the top of the file tree is reached
|
|
87
|
+
setPackageJsonCache(paths, null);
|
|
60
88
|
return null;
|
|
61
89
|
}
|
|
62
90
|
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../browser/src/util/ImportUtils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,aAAa,CAAA;AAC5B,OAAO,IAAI,MAAM,MAAM,CAAA;AACvB,OAAO,EAAE,aAAa,EAAE,MAAM,KAAK,CAAA;AAEnC,MAAM,CAAC,KAAK,UAAU,mBAAmB,CACrC,QAAgB;IAEhB,MAAM,WAAW,GAAG,KAAK,IAA2B,EAAE;QAClD,qFAAqF;QACrF,oDAAoD;QACpD,OAAO;YACH,8DAA8D;YAC9D,MAAM,QAAQ,CAAC,qCAAqC,CAAC,EAAE,CACnD,QAAQ,CAAC,UAAU,CAAC,SAAS,CAAC;gBAC1B,CAAC,CAAC,QAAQ;gBACV,CAAC,CAAC,aAAa,CAAC,QAAQ,CAAC,CAAC,QAAQ,EAAE,CAC3C;YACD,KAAK;SACR,CAAA;IACL,CAAC,CAAA;IACD,MAAM,YAAY,GAAG,GAAsB,EAAE;QACzC,OAAO,CAAC,OAAO,CAAC,QAAQ,CAAC,EAAE,UAAU,CAAC,CAAA;IAC1C,CAAC,CAAA;IAED,MAAM,SAAS,GAAG,QAAQ,CAAC,SAAS,CAAC,QAAQ,CAAC,WAAW,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,CAAC,oBAAoB,EAAE,MAAM,CAAC,CAClD,CAAA;gBACD,qDAAqD;gBACrD,mBAAmB,CAAC,KAAK,EAAE,aAAa,CAAC,CAAA;gBACzC,OAAO,aAAa,CAAA;YACxB,CAAC;YAAC,MAAM,CAAC;gBACL,mEAAmE;gBACnE,mBAAmB,CAAC,KAAK,EAAE,IAAI,CAAC,CAAA;gBAChC,OAAO,IAAI,CAAA;YACf,CAAC;QACL,CAAC;QAAC,MAAM,CAAC;YACL,SAAQ;QACZ,CAAC;IACL,CAAC;IAED,sCAAsC;IACtC,mBAAmB,CAAC,KAAK,EAAE,IAAI,CAAC,CAAA;IAChC,OAAO,IAAI,CAAA;AACf,CAAC","file":"ImportUtils.js","sourcesContent":["import fs from \"fs/promises\"\nimport path from \"path\"\nimport { pathToFileURL } from \"url\"\n\nexport async function importOrRequireFile(\n filePath: string,\n): Promise<[any, \"esm\" | \"commonjs\"]> {\n const tryToImport = async (): Promise<[any, \"esm\"]> => {\n // `Function` is required to make sure the `import` statement wil stay `import` after\n // transpilation and won't be converted to `require`\n return [\n // eslint-disable-next-line @typescript-eslint/no-implied-eval\n await Function(\"return filePath => import(filePath)\")()(\n filePath.startsWith(\"file://\")\n ? filePath\n : pathToFileURL(filePath).toString(),\n ),\n \"esm\",\n ]\n }\n const tryToRequire = (): [any, \"commonjs\"] => {\n return [require(filePath), \"commonjs\"]\n }\n\n const extension = filePath.substring(filePath.lastIndexOf(\".\") + \".\".length)\n\n if (extension === \"mjs\" || extension === \"mts\") return tryToImport()\n else if (extension === \"cjs\" || extension === \"cts\") return tryToRequire()\n else if (extension === \"js\" || extension === \"ts\") {\n const packageJson = await getNearestPackageJson(filePath)\n\n if (packageJson != null) {\n const isModule = (packageJson as any)?.type === \"module\"\n\n if (isModule) return tryToImport()\n else return tryToRequire()\n } else return tryToRequire()\n }\n\n return tryToRequire()\n}\n\nconst packageJsonCache = new Map<string, object | null>()\nconst MAX_CACHE_SIZE = 1000\n\nfunction setPackageJsonCache(paths: string[], packageJson: object | null) {\n for (const path of paths) {\n // Simple LRU-like behavior: if we're at capacity, remove oldest entry\n if (\n packageJsonCache.size >= MAX_CACHE_SIZE &&\n !packageJsonCache.has(path)\n ) {\n const firstKey = packageJsonCache.keys().next().value\n if (firstKey) packageJsonCache.delete(firstKey)\n }\n packageJsonCache.set(path, packageJson)\n }\n}\n\nasync function getNearestPackageJson(filePath: string): Promise<object | null> {\n let currentPath = filePath\n const paths: string[] = []\n\n while (currentPath !== path.dirname(currentPath)) {\n currentPath = path.dirname(currentPath)\n\n // Check if we have already cached the package.json for this path\n if (packageJsonCache.has(currentPath)) {\n setPackageJsonCache(paths, packageJsonCache.get(currentPath)!)\n return packageJsonCache.get(currentPath)!\n }\n\n // Add the current path to the list of paths to cache\n paths.push(currentPath)\n\n const potentialPackageJson = path.join(currentPath, \"package.json\")\n\n try {\n const stats = await fs.stat(potentialPackageJson)\n if (!stats.isFile()) {\n continue\n }\n\n try {\n const parsedPackage = JSON.parse(\n await fs.readFile(potentialPackageJson, \"utf8\"),\n )\n // Cache the parsed package.json object and return it\n setPackageJsonCache(paths, parsedPackage)\n return parsedPackage\n } catch {\n // If parsing fails, we still cache null to avoid repeated attempts\n setPackageJsonCache(paths, null)\n return null\n }\n } catch {\n continue\n }\n }\n\n // the top of the file tree is reached\n setPackageJsonCache(paths, null)\n return null\n}\n"],"sourceRoot":".."}
|
|
@@ -19,6 +19,10 @@ export declare class RedisQueryResultCache implements QueryResultCache {
|
|
|
19
19
|
* Type of the Redis Client (redis or ioredis).
|
|
20
20
|
*/
|
|
21
21
|
protected clientType: "redis" | "ioredis" | "ioredis/cluster";
|
|
22
|
+
/**
|
|
23
|
+
* Redis major version number
|
|
24
|
+
*/
|
|
25
|
+
protected redisMajorVersion: number | undefined;
|
|
22
26
|
constructor(connection: DataSource, clientType: "redis" | "ioredis" | "ioredis/cluster");
|
|
23
27
|
/**
|
|
24
28
|
* Creates a connection with given cache provider.
|
|
@@ -62,4 +66,13 @@ export declare class RedisQueryResultCache implements QueryResultCache {
|
|
|
62
66
|
* Loads redis dependency.
|
|
63
67
|
*/
|
|
64
68
|
protected loadRedis(): any;
|
|
69
|
+
/**
|
|
70
|
+
* Detects the Redis version based on the connected client's API characteristics
|
|
71
|
+
* without creating test keys in the database
|
|
72
|
+
*/
|
|
73
|
+
private detectRedisVersion;
|
|
74
|
+
/**
|
|
75
|
+
* Checks if Redis version is 5.x or higher
|
|
76
|
+
*/
|
|
77
|
+
private isRedis5OrHigher;
|
|
65
78
|
}
|
|
@@ -24,19 +24,32 @@ class RedisQueryResultCache {
|
|
|
24
24
|
async connect() {
|
|
25
25
|
const cacheOptions = this.connection.options.cache;
|
|
26
26
|
if (this.clientType === "redis") {
|
|
27
|
-
|
|
27
|
+
const clientOptions = {
|
|
28
28
|
...cacheOptions?.options,
|
|
29
|
-
|
|
30
|
-
|
|
29
|
+
};
|
|
30
|
+
// Create initial client to test Redis version
|
|
31
|
+
let tempClient = this.redis.createClient(clientOptions);
|
|
32
|
+
const isRedis4Plus = typeof tempClient.connect === "function";
|
|
33
|
+
if (isRedis4Plus) {
|
|
34
|
+
// Redis 4+ detected, recreate with legacyMode for Redis 4.x
|
|
35
|
+
// (Redis 5 will ignore legacyMode if not needed)
|
|
36
|
+
clientOptions.legacyMode = true;
|
|
37
|
+
tempClient = this.redis.createClient(clientOptions);
|
|
38
|
+
}
|
|
39
|
+
// Set as the main client
|
|
40
|
+
this.client = tempClient;
|
|
31
41
|
if (typeof this.connection.options.cache === "object" &&
|
|
32
42
|
this.connection.options.cache.ignoreErrors) {
|
|
33
43
|
this.client.on("error", (err) => {
|
|
34
44
|
this.connection.logger.log("warn", err);
|
|
35
45
|
});
|
|
36
46
|
}
|
|
37
|
-
|
|
47
|
+
// Connect if Redis 4+
|
|
48
|
+
if (typeof this.client.connect === "function") {
|
|
38
49
|
await this.client.connect();
|
|
39
50
|
}
|
|
51
|
+
// Detect precise version after connection is established
|
|
52
|
+
this.detectRedisVersion();
|
|
40
53
|
}
|
|
41
54
|
else if (this.clientType === "ioredis") {
|
|
42
55
|
if (cacheOptions && cacheOptions.port) {
|
|
@@ -74,6 +87,13 @@ class RedisQueryResultCache {
|
|
|
74
87
|
* Disconnects the connection
|
|
75
88
|
*/
|
|
76
89
|
async disconnect() {
|
|
90
|
+
if (this.isRedis5OrHigher()) {
|
|
91
|
+
// Redis 5+ uses quit() that returns a Promise
|
|
92
|
+
await this.client.quit();
|
|
93
|
+
this.client = undefined;
|
|
94
|
+
return;
|
|
95
|
+
}
|
|
96
|
+
// Redis 3/4 callback style
|
|
77
97
|
return new Promise((ok, fail) => {
|
|
78
98
|
this.client.quit((err, result) => {
|
|
79
99
|
if (err)
|
|
@@ -93,24 +113,22 @@ class RedisQueryResultCache {
|
|
|
93
113
|
* Returns undefined if result is not cached.
|
|
94
114
|
*/
|
|
95
115
|
getFromCache(options, queryRunner) {
|
|
116
|
+
const key = options.identifier || options.query;
|
|
117
|
+
if (!key)
|
|
118
|
+
return Promise.resolve(undefined);
|
|
119
|
+
if (this.isRedis5OrHigher()) {
|
|
120
|
+
// Redis 5+ Promise-based API
|
|
121
|
+
return this.client.get(key).then((result) => {
|
|
122
|
+
return result ? JSON.parse(result) : undefined;
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
// Redis 3/4 callback-based API
|
|
96
126
|
return new Promise((ok, fail) => {
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
});
|
|
103
|
-
}
|
|
104
|
-
else if (options.query) {
|
|
105
|
-
this.client.get(options.query, (err, result) => {
|
|
106
|
-
if (err)
|
|
107
|
-
return fail(err);
|
|
108
|
-
ok(JSON.parse(result));
|
|
109
|
-
});
|
|
110
|
-
}
|
|
111
|
-
else {
|
|
112
|
-
ok(undefined);
|
|
113
|
-
}
|
|
127
|
+
this.client.get(key, (err, result) => {
|
|
128
|
+
if (err)
|
|
129
|
+
return fail(err);
|
|
130
|
+
ok(result ? JSON.parse(result) : undefined);
|
|
131
|
+
});
|
|
114
132
|
});
|
|
115
133
|
}
|
|
116
134
|
/**
|
|
@@ -123,27 +141,37 @@ class RedisQueryResultCache {
|
|
|
123
141
|
* Stores given query result in the cache.
|
|
124
142
|
*/
|
|
125
143
|
async storeInCache(options, savedCache, queryRunner) {
|
|
144
|
+
const key = options.identifier || options.query;
|
|
145
|
+
if (!key)
|
|
146
|
+
return;
|
|
147
|
+
const value = JSON.stringify(options);
|
|
148
|
+
const duration = options.duration;
|
|
149
|
+
if (this.isRedis5OrHigher()) {
|
|
150
|
+
// Redis 5+ Promise-based API with PX option
|
|
151
|
+
await this.client.set(key, value, {
|
|
152
|
+
PX: duration,
|
|
153
|
+
});
|
|
154
|
+
return;
|
|
155
|
+
}
|
|
156
|
+
// Redis 3/4 callback-based API
|
|
126
157
|
return new Promise((ok, fail) => {
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
});
|
|
133
|
-
}
|
|
134
|
-
else if (options.query) {
|
|
135
|
-
this.client.set(options.query, JSON.stringify(options), "PX", options.duration, (err, result) => {
|
|
136
|
-
if (err)
|
|
137
|
-
return fail(err);
|
|
138
|
-
ok();
|
|
139
|
-
});
|
|
140
|
-
}
|
|
158
|
+
this.client.set(key, value, "PX", duration, (err, result) => {
|
|
159
|
+
if (err)
|
|
160
|
+
return fail(err);
|
|
161
|
+
ok();
|
|
162
|
+
});
|
|
141
163
|
});
|
|
142
164
|
}
|
|
143
165
|
/**
|
|
144
166
|
* Clears everything stored in the cache.
|
|
145
167
|
*/
|
|
146
168
|
async clear(queryRunner) {
|
|
169
|
+
if (this.isRedis5OrHigher()) {
|
|
170
|
+
// Redis 5+ Promise-based API
|
|
171
|
+
await this.client.flushDb();
|
|
172
|
+
return;
|
|
173
|
+
}
|
|
174
|
+
// Redis 3/4 callback-based API
|
|
147
175
|
return new Promise((ok, fail) => {
|
|
148
176
|
this.client.flushdb((err, result) => {
|
|
149
177
|
if (err)
|
|
@@ -166,7 +194,13 @@ class RedisQueryResultCache {
|
|
|
166
194
|
/**
|
|
167
195
|
* Removes a single key from redis database.
|
|
168
196
|
*/
|
|
169
|
-
deleteKey(key) {
|
|
197
|
+
async deleteKey(key) {
|
|
198
|
+
if (this.isRedis5OrHigher()) {
|
|
199
|
+
// Redis 5+ Promise-based API
|
|
200
|
+
await this.client.del(key);
|
|
201
|
+
return;
|
|
202
|
+
}
|
|
203
|
+
// Redis 3/4 callback-based API
|
|
170
204
|
return new Promise((ok, fail) => {
|
|
171
205
|
this.client.del(key, (err, result) => {
|
|
172
206
|
if (err)
|
|
@@ -187,10 +221,43 @@ class RedisQueryResultCache {
|
|
|
187
221
|
return PlatformTools_1.PlatformTools.load(this.clientType);
|
|
188
222
|
}
|
|
189
223
|
}
|
|
190
|
-
catch
|
|
191
|
-
throw new TypeORMError_1.TypeORMError(`Cannot use cache because ${this.clientType} is not installed. Please run "npm i ${this.clientType}
|
|
224
|
+
catch {
|
|
225
|
+
throw new TypeORMError_1.TypeORMError(`Cannot use cache because ${this.clientType} is not installed. Please run "npm i ${this.clientType}".`);
|
|
192
226
|
}
|
|
193
227
|
}
|
|
228
|
+
/**
|
|
229
|
+
* Detects the Redis version based on the connected client's API characteristics
|
|
230
|
+
* without creating test keys in the database
|
|
231
|
+
*/
|
|
232
|
+
detectRedisVersion() {
|
|
233
|
+
if (this.clientType !== "redis")
|
|
234
|
+
return;
|
|
235
|
+
try {
|
|
236
|
+
// Detect version by examining the client's method signatures
|
|
237
|
+
// This avoids creating test keys in the database
|
|
238
|
+
const setMethod = this.client.set;
|
|
239
|
+
if (setMethod && setMethod.length <= 3) {
|
|
240
|
+
// Redis 5+ set method accepts fewer parameters (key, value, options)
|
|
241
|
+
this.redisMajorVersion = 5;
|
|
242
|
+
}
|
|
243
|
+
else {
|
|
244
|
+
// Redis 3/4 set method requires more parameters (key, value, flag, duration, callback)
|
|
245
|
+
this.redisMajorVersion = 3;
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
catch {
|
|
249
|
+
// Default to Redis 3/4 for maximum compatibility
|
|
250
|
+
this.redisMajorVersion = 3;
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
/**
|
|
254
|
+
* Checks if Redis version is 5.x or higher
|
|
255
|
+
*/
|
|
256
|
+
isRedis5OrHigher() {
|
|
257
|
+
if (this.clientType !== "redis")
|
|
258
|
+
return false;
|
|
259
|
+
return (this.redisMajorVersion !== undefined && this.redisMajorVersion >= 5);
|
|
260
|
+
}
|
|
194
261
|
}
|
|
195
262
|
exports.RedisQueryResultCache = RedisQueryResultCache;
|
|
196
263
|
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/cache/RedisQueryResultCache.ts"],"names":[],"mappings":";;;AAEA,6DAAyD;AAGzD,wDAAoD;AAEpD;;GAEG;AACH,MAAa,qBAAqB;IAoB9B,4EAA4E;IAC5E,cAAc;IACd,4EAA4E;IAE5E,YACc,UAAsB,EAChC,UAAmD;QADzC,eAAU,GAAV,UAAU,CAAY;QAGhC,IAAI,CAAC,UAAU,GAAG,UAAU,CAAA;QAC5B,IAAI,CAAC,KAAK,GAAG,IAAI,CAAC,SAAS,EAAE,CAAA;IACjC,CAAC;IAED,4EAA4E;IAC5E,iBAAiB;IACjB,4EAA4E;IAE5E;;OAEG;IACH,KAAK,CAAC,OAAO;QACT,MAAM,YAAY,GAAQ,IAAI,CAAC,UAAU,CAAC,OAAO,CAAC,KAAK,CAAA;QACvD,IAAI,IAAI,CAAC,UAAU,KAAK,OAAO,EAAE,CAAC;YAC9B,IAAI,CAAC,MAAM,GAAG,IAAI,CAAC,KAAK,CAAC,YAAY,CAAC;gBAClC,GAAG,YAAY,EAAE,OAAO;gBACxB,UAAU,EAAE,IAAI;aACnB,CAAC,CAAA;YACF,IACI,OAAO,IAAI,CAAC,UAAU,CAAC,OAAO,CAAC,KAAK,KAAK,QAAQ;gBACjD,IAAI,CAAC,UAAU,CAAC,OA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A;QACN,CAAC,CAAC,CAAA;IACN,CAAC;IAED;;OAEG;IACO,SAAS;QACf,IAAI,CAAC;YACD,IAAI,IAAI,CAAC,UAAU,KAAK,iBAAiB,EAAE,CAAC;gBACxC,OAAO,6BAAa,CAAC,IAAI,CAAC,SAAS,CAAC,CAAA;YACxC,CAAC;iBAAM,CAAC;gBACJ,OAAO,6BAAa,CAAC,IAAI,CAAC,IAAI,CAAC,UAAU,CAAC,CAAA;YAC9C,CAAC;QACL,CAAC;QAAC,OAAO,CAAC,EAAE,CAAC;YACT,MAAM,IAAI,2BAAY,CAClB,4BAA4B,IAAI,CAAC,UAAU,wCAAwC,IAAI,CAAC,UAAU,WAAW,CAChH,CAAA;QACL,CAAC;IACL,CAAC;CACJ;AAhPD,sDAgPC","file":"RedisQueryResultCache.js","sourcesContent":["import { QueryResultCache } from \"./QueryResultCache\"\nimport { QueryResultCacheOptions } from \"./QueryResultCacheOptions\"\nimport { PlatformTools } from \"../platform/PlatformTools\"\nimport { DataSource } from \"../data-source/DataSource\"\nimport { QueryRunner } from \"../query-runner/QueryRunner\"\nimport { TypeORMError } from \"../error/TypeORMError\"\n\n/**\n * Caches query result into Redis database.\n */\nexport class RedisQueryResultCache implements QueryResultCache {\n // -------------------------------------------------------------------------\n // Protected Properties\n // -------------------------------------------------------------------------\n\n /**\n * Redis module instance loaded dynamically.\n */\n protected redis: any\n\n /**\n * Connected redis client.\n */\n protected client: any\n\n /**\n * Type of the Redis Client (redis or ioredis).\n */\n protected clientType: \"redis\" | \"ioredis\" | \"ioredis/cluster\"\n\n // -------------------------------------------------------------------------\n // Constructor\n // -------------------------------------------------------------------------\n\n constructor(\n protected connection: DataSource,\n clientType: \"redis\" | \"ioredis\" | \"ioredis/cluster\",\n ) {\n this.clientType = clientType\n this.redis = this.loadRedis()\n }\n\n // -------------------------------------------------------------------------\n // Public Methods\n // -------------------------------------------------------------------------\n\n /**\n * Creates a connection with given cache provider.\n */\n async connect(): Promise<void> {\n const cacheOptions: any = this.connection.options.cache\n if (this.clientType === \"redis\") {\n this.client = this.redis.createClient({\n ...cacheOptions?.options,\n legacyMode: true,\n })\n if (\n typeof this.connection.options.cache === \"object\" &&\n this.connection.options.cache.ignoreErrors\n ) {\n this.client.on(\"error\", (err: any) => {\n this.connection.logger.log(\"warn\", err)\n })\n }\n if (\"connect\" in this.client) {\n await this.client.connect()\n }\n } else if (this.clientType === \"ioredis\") {\n if (cacheOptions && cacheOptions.port) {\n if (cacheOptions.options) {\n this.client = new this.redis(\n cacheOptions.port,\n cacheOptions.options,\n )\n } else {\n this.client = new this.redis(cacheOptions.port)\n }\n } else if (cacheOptions && cacheOptions.options) {\n this.client = new this.redis(cacheOptions.options)\n } else {\n this.client = new this.redis()\n }\n } else if (this.clientType === \"ioredis/cluster\") {\n if (\n cacheOptions &&\n cacheOptions.options &&\n Array.isArray(cacheOptions.options)\n ) {\n this.client = new this.redis.Cluster(cacheOptions.options)\n } else if (\n cacheOptions &&\n cacheOptions.options &&\n cacheOptions.options.startupNodes\n ) {\n this.client = new this.redis.Cluster(\n cacheOptions.options.startupNodes,\n cacheOptions.options.options,\n )\n } else {\n throw new TypeORMError(\n `options.startupNodes required for ${this.clientType}.`,\n )\n }\n }\n }\n\n /**\n * Disconnects the connection\n */\n async disconnect(): Promise<void> {\n return new Promise<void>((ok, fail) => {\n this.client.quit((err: any, result: any) => {\n if (err) return fail(err)\n ok()\n this.client = undefined\n })\n })\n }\n\n /**\n * Creates table for storing cache if it does not exist yet.\n */\n async synchronize(queryRunner: QueryRunner): Promise<void> {}\n\n /**\n * Get data from cache.\n * Returns cache result if found.\n * Returns undefined if result is not cached.\n */\n getFromCache(\n options: QueryResultCacheOptions,\n queryRunner?: QueryRunner,\n ): Promise<QueryResultCacheOptions | undefined> {\n return new Promise<QueryResultCacheOptions | undefined>((ok, fail) => {\n if (options.identifier) {\n this.client.get(options.identifier, (err: any, result: any) => {\n if (err) return fail(err)\n ok(JSON.parse(result))\n })\n } else if (options.query) {\n this.client.get(options.query, (err: any, result: any) => {\n if (err) return fail(err)\n ok(JSON.parse(result))\n })\n } else {\n ok(undefined)\n }\n })\n }\n\n /**\n * Checks if cache is expired or not.\n */\n isExpired(savedCache: QueryResultCacheOptions): boolean {\n return savedCache.time! + savedCache.duration < Date.now()\n }\n\n /**\n * Stores given query result in the cache.\n */\n async storeInCache(\n options: QueryResultCacheOptions,\n savedCache: QueryResultCacheOptions,\n queryRunner?: QueryRunner,\n ): Promise<void> {\n return new Promise<void>((ok, fail) => {\n if (options.identifier) {\n this.client.set(\n options.identifier,\n JSON.stringify(options),\n \"PX\",\n options.duration,\n (err: any, result: any) => {\n if (err) return fail(err)\n ok()\n },\n )\n } else if (options.query) {\n this.client.set(\n options.query,\n JSON.stringify(options),\n \"PX\",\n options.duration,\n (err: any, result: any) => {\n if (err) return fail(err)\n ok()\n },\n )\n }\n })\n }\n\n /**\n * Clears everything stored in the cache.\n */\n async clear(queryRunner?: QueryRunner): Promise<void> {\n return new Promise<void>((ok, fail) => {\n this.client.flushdb((err: any, result: any) => {\n if (err) return fail(err)\n ok()\n })\n })\n }\n\n /**\n * Removes all cached results by given identifiers from cache.\n */\n async remove(\n identifiers: string[],\n queryRunner?: QueryRunner,\n ): Promise<void> {\n await Promise.all(\n identifiers.map((identifier) => {\n return this.deleteKey(identifier)\n }),\n )\n }\n\n // -------------------------------------------------------------------------\n // Protected Methods\n // -------------------------------------------------------------------------\n\n /**\n * Removes a single key from redis database.\n */\n protected deleteKey(key: string): Promise<void> {\n return new Promise<void>((ok, fail) => {\n this.client.del(key, (err: any, result: any) => {\n if (err) return fail(err)\n ok()\n })\n })\n }\n\n /**\n * Loads redis dependency.\n */\n protected loadRedis(): any {\n try {\n if (this.clientType === \"ioredis/cluster\") {\n return PlatformTools.load(\"ioredis\")\n } else {\n return PlatformTools.load(this.clientType)\n }\n } catch (e) {\n throw new TypeORMError(\n `Cannot use cache because ${this.clientType} is not installed. Please run \"npm i ${this.clientType} --save\".`,\n )\n }\n }\n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I,CAAC,MAAM,CAAC,GAAG,CAAA;YACjC,IAAI,SAAS,IAAI,SAAS,CAAC,MAAM,IAAI,CAAC,EAAE,CAAC;gBACrC,qEAAqE;gBACrE,IAAI,CAAC,iBAAiB,GAAG,CAAC,CAAA;YAC9B,CAAC;iBAAM,CAAC;gBACJ,uFAAuF;gBACvF,IAAI,CAAC,iBAAiB,GAAG,CAAC,CAAA;YAC9B,CAAC;QACL,CAAC;QAAC,MAAM,CAAC;YACL,iDAAiD;YACjD,IAAI,CAAC,iBAAiB,GAAG,CAAC,CAAA;QAC9B,CAAC;IACL,CAAC;IAED;;OAEG;IACK,gBAAgB;QACpB,IAAI,IAAI,CAAC,UAAU,KAAK,OAAO;YAAE,OAAO,KAAK,CAAA;QAC7C,OAAO,CACH,IAAI,CAAC,iBAAiB,KAAK,SAAS,IAAI,IAAI,CAAC,iBAAiB,IAAI,CAAC,CACtE,CAAA;IACL,CAAC;CACJ;AApUD,sDAoUC","file":"RedisQueryResultCache.js","sourcesContent":["import { QueryResultCache } from \"./QueryResultCache\"\nimport { QueryResultCacheOptions } from \"./QueryResultCacheOptions\"\nimport { PlatformTools } from \"../platform/PlatformTools\"\nimport { DataSource } from \"../data-source/DataSource\"\nimport { QueryRunner } from \"../query-runner/QueryRunner\"\nimport { TypeORMError } from \"../error/TypeORMError\"\n\n/**\n * Caches query result into Redis database.\n */\nexport class RedisQueryResultCache implements QueryResultCache {\n // -------------------------------------------------------------------------\n // Protected Properties\n // -------------------------------------------------------------------------\n\n /**\n * Redis module instance loaded dynamically.\n */\n protected redis: any\n\n /**\n * Connected redis client.\n */\n protected client: any\n\n /**\n * Type of the Redis Client (redis or ioredis).\n */\n protected clientType: \"redis\" | \"ioredis\" | \"ioredis/cluster\"\n\n /**\n * Redis major version number\n */\n protected redisMajorVersion: number | undefined\n\n // -------------------------------------------------------------------------\n // Constructor\n // -------------------------------------------------------------------------\n\n constructor(\n protected connection: DataSource,\n clientType: \"redis\" | \"ioredis\" | \"ioredis/cluster\",\n ) {\n this.clientType = clientType\n this.redis = this.loadRedis()\n }\n\n // -------------------------------------------------------------------------\n // Public Methods\n // -------------------------------------------------------------------------\n\n /**\n * Creates a connection with given cache provider.\n */\n async connect(): Promise<void> {\n const cacheOptions: any = this.connection.options.cache\n if (this.clientType === \"redis\") {\n const clientOptions = {\n ...cacheOptions?.options,\n }\n\n // Create initial client to test Redis version\n let tempClient = this.redis.createClient(clientOptions)\n const isRedis4Plus = typeof tempClient.connect === \"function\"\n\n if (isRedis4Plus) {\n // Redis 4+ detected, recreate with legacyMode for Redis 4.x\n // (Redis 5 will ignore legacyMode if not needed)\n clientOptions.legacyMode = true\n tempClient = this.redis.createClient(clientOptions)\n }\n\n // Set as the main client\n this.client = tempClient\n\n if (\n typeof this.connection.options.cache === \"object\" &&\n this.connection.options.cache.ignoreErrors\n ) {\n this.client.on(\"error\", (err: any) => {\n this.connection.logger.log(\"warn\", err)\n })\n }\n\n // Connect if Redis 4+\n if (typeof this.client.connect === \"function\") {\n await this.client.connect()\n }\n\n // Detect precise version after connection is established\n this.detectRedisVersion()\n } else if (this.clientType === \"ioredis\") {\n if (cacheOptions && cacheOptions.port) {\n if (cacheOptions.options) {\n this.client = new this.redis(\n cacheOptions.port,\n cacheOptions.options,\n )\n } else {\n this.client = new this.redis(cacheOptions.port)\n }\n } else if (cacheOptions && cacheOptions.options) {\n this.client = new this.redis(cacheOptions.options)\n } else {\n this.client = new this.redis()\n }\n } else if (this.clientType === \"ioredis/cluster\") {\n if (\n cacheOptions &&\n cacheOptions.options &&\n Array.isArray(cacheOptions.options)\n ) {\n this.client = new this.redis.Cluster(cacheOptions.options)\n } else if (\n cacheOptions &&\n cacheOptions.options &&\n cacheOptions.options.startupNodes\n ) {\n this.client = new this.redis.Cluster(\n cacheOptions.options.startupNodes,\n cacheOptions.options.options,\n )\n } else {\n throw new TypeORMError(\n `options.startupNodes required for ${this.clientType}.`,\n )\n }\n }\n }\n\n /**\n * Disconnects the connection\n */\n async disconnect(): Promise<void> {\n if (this.isRedis5OrHigher()) {\n // Redis 5+ uses quit() that returns a Promise\n await this.client.quit()\n this.client = undefined\n return\n }\n\n // Redis 3/4 callback style\n return new Promise<void>((ok, fail) => {\n this.client.quit((err: any, result: any) => {\n if (err) return fail(err)\n ok()\n this.client = undefined\n })\n })\n }\n\n /**\n * Creates table for storing cache if it does not exist yet.\n */\n async synchronize(queryRunner: QueryRunner): Promise<void> {}\n\n /**\n * Get data from cache.\n * Returns cache result if found.\n * Returns undefined if result is not cached.\n */\n getFromCache(\n options: QueryResultCacheOptions,\n queryRunner?: QueryRunner,\n ): Promise<QueryResultCacheOptions | undefined> {\n const key = options.identifier || options.query\n if (!key) return Promise.resolve(undefined)\n\n if (this.isRedis5OrHigher()) {\n // Redis 5+ Promise-based API\n return this.client.get(key).then((result: any) => {\n return result ? JSON.parse(result) : undefined\n })\n }\n\n // Redis 3/4 callback-based API\n return new Promise<QueryResultCacheOptions | undefined>((ok, fail) => {\n this.client.get(key, (err: any, result: any) => {\n if (err) return fail(err)\n ok(result ? JSON.parse(result) : undefined)\n })\n })\n }\n\n /**\n * Checks if cache is expired or not.\n */\n isExpired(savedCache: QueryResultCacheOptions): boolean {\n return savedCache.time! + savedCache.duration < Date.now()\n }\n\n /**\n * Stores given query result in the cache.\n */\n async storeInCache(\n options: QueryResultCacheOptions,\n savedCache: QueryResultCacheOptions,\n queryRunner?: QueryRunner,\n ): Promise<void> {\n const key = options.identifier || options.query\n if (!key) return\n\n const value = JSON.stringify(options)\n const duration = options.duration\n\n if (this.isRedis5OrHigher()) {\n // Redis 5+ Promise-based API with PX option\n await this.client.set(key, value, {\n PX: duration,\n })\n return\n }\n\n // Redis 3/4 callback-based API\n return new Promise<void>((ok, fail) => {\n this.client.set(\n key,\n value,\n \"PX\",\n duration,\n (err: any, result: any) => {\n if (err) return fail(err)\n ok()\n },\n )\n })\n }\n\n /**\n * Clears everything stored in the cache.\n */\n async clear(queryRunner?: QueryRunner): Promise<void> {\n if (this.isRedis5OrHigher()) {\n // Redis 5+ Promise-based API\n await this.client.flushDb()\n return\n }\n\n // Redis 3/4 callback-based API\n return new Promise<void>((ok, fail) => {\n this.client.flushdb((err: any, result: any) => {\n if (err) return fail(err)\n ok()\n })\n })\n }\n\n /**\n * Removes all cached results by given identifiers from cache.\n */\n async remove(\n identifiers: string[],\n queryRunner?: QueryRunner,\n ): Promise<void> {\n await Promise.all(\n identifiers.map((identifier) => {\n return this.deleteKey(identifier)\n }),\n )\n }\n\n // -------------------------------------------------------------------------\n // Protected Methods\n // -------------------------------------------------------------------------\n\n /**\n * Removes a single key from redis database.\n */\n protected async deleteKey(key: string): Promise<void> {\n if (this.isRedis5OrHigher()) {\n // Redis 5+ Promise-based API\n await this.client.del(key)\n return\n }\n\n // Redis 3/4 callback-based API\n return new Promise<void>((ok, fail) => {\n this.client.del(key, (err: any, result: any) => {\n if (err) return fail(err)\n ok()\n })\n })\n }\n\n /**\n * Loads redis dependency.\n */\n protected loadRedis(): any {\n try {\n if (this.clientType === \"ioredis/cluster\") {\n return PlatformTools.load(\"ioredis\")\n } else {\n return PlatformTools.load(this.clientType)\n }\n } catch {\n throw new TypeORMError(\n `Cannot use cache because ${this.clientType} is not installed. Please run \"npm i ${this.clientType}\".`,\n )\n }\n }\n\n /**\n * Detects the Redis version based on the connected client's API characteristics\n * without creating test keys in the database\n */\n private detectRedisVersion(): void {\n if (this.clientType !== \"redis\") return\n\n try {\n // Detect version by examining the client's method signatures\n // This avoids creating test keys in the database\n const setMethod = this.client.set\n if (setMethod && setMethod.length <= 3) {\n // Redis 5+ set method accepts fewer parameters (key, value, options)\n this.redisMajorVersion = 5\n } else {\n // Redis 3/4 set method requires more parameters (key, value, flag, duration, callback)\n this.redisMajorVersion = 3\n }\n } catch {\n // Default to Redis 3/4 for maximum compatibility\n this.redisMajorVersion = 3\n }\n }\n\n /**\n * Checks if Redis version is 5.x or higher\n */\n private isRedis5OrHigher(): boolean {\n if (this.clientType !== \"redis\") return false\n return (\n this.redisMajorVersion !== undefined && this.redisMajorVersion >= 5\n )\n }\n}\n"],"sourceRoot":".."}
|
|
@@ -155,4 +155,10 @@ export interface ColumnOptions extends ColumnCommonOptions {
|
|
|
155
155
|
* SRID (Spatial Reference ID (EPSG code))
|
|
156
156
|
*/
|
|
157
157
|
srid?: number;
|
|
158
|
+
/**
|
|
159
|
+
* Query to be used to populate the column data. This query is used when generating the relational db script.
|
|
160
|
+
* The query function is called with the current entities alias either defined by the Entity Decorator or automatically
|
|
161
|
+
* @See https://typeorm.io/decorator-reference#virtualcolumn for more details.
|
|
162
|
+
*/
|
|
163
|
+
query?: (alias: string) => string;
|
|
158
164
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/decorator/options/ColumnOptions.ts"],"names":[],"mappings":"","file":"ColumnOptions.js","sourcesContent":["import { ColumnType } from \"../../driver/types/ColumnTypes\"\nimport { ValueTransformer } from \"./ValueTransformer\"\nimport { ColumnCommonOptions } from \"./ColumnCommonOptions\"\n\n/**\n * Describes all column's options.\n */\nexport interface ColumnOptions extends ColumnCommonOptions {\n /**\n * Column type. Must be one of the value from the ColumnTypes class.\n */\n type?: ColumnType\n\n /**\n * Column name in the database.\n */\n name?: string\n\n /**\n * Column type's length. Used only on some column types.\n * For example type = \"string\" and length = \"100\" means that ORM will create a column with type varchar(100).\n */\n length?: string | number\n\n /**\n * Column type's display width. Used only on some column types in MySQL.\n * For example, INT(4) specifies an INT with a display width of four digits.\n */\n width?: number\n\n /**\n * Indicates if column's value can be set to NULL.\n * Default value is \"false\".\n */\n nullable?: boolean\n\n /**\n * Indicates if column value is not updated by \"save\" operation.\n * It means you'll be able to write this value only when you first time insert the object.\n * Default value is \"false\".\n *\n * @deprecated Please use the `update` option instead. Careful, it takes\n * the opposite value to readonly.\n *\n */\n readonly?: boolean\n\n /**\n * Indicates if column value is updated by \"save\" operation.\n * If false, you'll be able to write this value only when you first time insert the object.\n * Default value is \"true\".\n */\n update?: boolean\n\n /**\n * Indicates if column is always selected by QueryBuilder and find operations.\n * Default value is \"true\".\n */\n select?: boolean\n\n /**\n * Indicates if column is inserted by default.\n * Default value is \"true\".\n */\n insert?: boolean\n\n /**\n * Default database value.\n */\n default?: any\n\n /**\n * ON UPDATE trigger. Works only for MySQL.\n */\n onUpdate?: string\n\n /**\n * Indicates if this column is a primary key.\n * Same can be achieved when @PrimaryColumn decorator is used.\n */\n primary?: boolean\n\n /**\n * Specifies if column's value must be unique or not.\n */\n unique?: boolean\n\n /**\n * Column comment. Not supported by all database types.\n */\n comment?: string\n\n /**\n * The precision for a decimal (exact numeric) column (applies only for decimal column), which is the maximum\n * number of digits that are stored for the values.\n */\n precision?: number | null\n\n /**\n * The scale for a decimal (exact numeric) column (applies only for decimal column), which represents the number\n * of digits to the right of the decimal point and must not be greater than precision.\n */\n scale?: number\n\n /**\n * Puts ZEROFILL attribute on to numeric column. Works only for MySQL.\n * If you specify ZEROFILL for a numeric column, MySQL automatically adds the UNSIGNED attribute to this column\n */\n zerofill?: boolean\n\n /**\n * Puts UNSIGNED attribute on to numeric column. Works only for MySQL.\n */\n unsigned?: boolean\n\n /**\n * Defines a column character set.\n * Not supported by all database types.\n */\n charset?: string\n\n /**\n * Defines a column collation.\n */\n collation?: string\n\n /**\n * Array of possible enumerated values.\n */\n enum?: (string | number)[] | Object\n\n /**\n * Exact name of enum\n */\n enumName?: string\n\n /**\n * If this column is primary key then this specifies the name for it.\n */\n primaryKeyConstraintName?: string\n\n /**\n * If this column is foreign key then this specifies the name for it.\n */\n foreignKeyConstraintName?: string\n\n /**\n * Generated column expression.\n */\n asExpression?: string\n\n /**\n * Generated column type.\n */\n generatedType?: \"VIRTUAL\" | \"STORED\"\n\n /**\n * Identity column type. Supports only in Postgres 10+.\n */\n generatedIdentity?: \"ALWAYS\" | \"BY DEFAULT\"\n\n /**\n * Return type of HSTORE column.\n * Returns value as string or as object.\n */\n hstoreType?: \"object\" | \"string\"\n\n /**\n * Indicates if this column is an array.\n * Can be simply set to true or array length can be specified.\n * Supported only by postgres.\n */\n array?: boolean\n\n /**\n * Specifies a value transformer that is to be used to (un)marshal\n * this column when reading or writing to the database.\n */\n transformer?: ValueTransformer | ValueTransformer[]\n\n /**\n * Spatial Feature Type (Geometry, Point, Polygon, etc.)\n */\n spatialFeatureType?: string\n\n /**\n * SRID (Spatial Reference ID (EPSG code))\n */\n srid?: number\n}\n"],"sourceRoot":"../.."}
|
|
1
|
+
{"version":3,"sources":["../../src/decorator/options/ColumnOptions.ts"],"names":[],"mappings":"","file":"ColumnOptions.js","sourcesContent":["import { ColumnType } from \"../../driver/types/ColumnTypes\"\nimport { ValueTransformer } from \"./ValueTransformer\"\nimport { ColumnCommonOptions } from \"./ColumnCommonOptions\"\n\n/**\n * Describes all column's options.\n */\nexport interface ColumnOptions extends ColumnCommonOptions {\n /**\n * Column type. Must be one of the value from the ColumnTypes class.\n */\n type?: ColumnType\n\n /**\n * Column name in the database.\n */\n name?: string\n\n /**\n * Column type's length. Used only on some column types.\n * For example type = \"string\" and length = \"100\" means that ORM will create a column with type varchar(100).\n */\n length?: string | number\n\n /**\n * Column type's display width. Used only on some column types in MySQL.\n * For example, INT(4) specifies an INT with a display width of four digits.\n */\n width?: number\n\n /**\n * Indicates if column's value can be set to NULL.\n * Default value is \"false\".\n */\n nullable?: boolean\n\n /**\n * Indicates if column value is not updated by \"save\" operation.\n * It means you'll be able to write this value only when you first time insert the object.\n * Default value is \"false\".\n *\n * @deprecated Please use the `update` option instead. Careful, it takes\n * the opposite value to readonly.\n *\n */\n readonly?: boolean\n\n /**\n * Indicates if column value is updated by \"save\" operation.\n * If false, you'll be able to write this value only when you first time insert the object.\n * Default value is \"true\".\n */\n update?: boolean\n\n /**\n * Indicates if column is always selected by QueryBuilder and find operations.\n * Default value is \"true\".\n */\n select?: boolean\n\n /**\n * Indicates if column is inserted by default.\n * Default value is \"true\".\n */\n insert?: boolean\n\n /**\n * Default database value.\n */\n default?: any\n\n /**\n * ON UPDATE trigger. Works only for MySQL.\n */\n onUpdate?: string\n\n /**\n * Indicates if this column is a primary key.\n * Same can be achieved when @PrimaryColumn decorator is used.\n */\n primary?: boolean\n\n /**\n * Specifies if column's value must be unique or not.\n */\n unique?: boolean\n\n /**\n * Column comment. Not supported by all database types.\n */\n comment?: string\n\n /**\n * The precision for a decimal (exact numeric) column (applies only for decimal column), which is the maximum\n * number of digits that are stored for the values.\n */\n precision?: number | null\n\n /**\n * The scale for a decimal (exact numeric) column (applies only for decimal column), which represents the number\n * of digits to the right of the decimal point and must not be greater than precision.\n */\n scale?: number\n\n /**\n * Puts ZEROFILL attribute on to numeric column. Works only for MySQL.\n * If you specify ZEROFILL for a numeric column, MySQL automatically adds the UNSIGNED attribute to this column\n */\n zerofill?: boolean\n\n /**\n * Puts UNSIGNED attribute on to numeric column. Works only for MySQL.\n */\n unsigned?: boolean\n\n /**\n * Defines a column character set.\n * Not supported by all database types.\n */\n charset?: string\n\n /**\n * Defines a column collation.\n */\n collation?: string\n\n /**\n * Array of possible enumerated values.\n */\n enum?: (string | number)[] | Object\n\n /**\n * Exact name of enum\n */\n enumName?: string\n\n /**\n * If this column is primary key then this specifies the name for it.\n */\n primaryKeyConstraintName?: string\n\n /**\n * If this column is foreign key then this specifies the name for it.\n */\n foreignKeyConstraintName?: string\n\n /**\n * Generated column expression.\n */\n asExpression?: string\n\n /**\n * Generated column type.\n */\n generatedType?: \"VIRTUAL\" | \"STORED\"\n\n /**\n * Identity column type. Supports only in Postgres 10+.\n */\n generatedIdentity?: \"ALWAYS\" | \"BY DEFAULT\"\n\n /**\n * Return type of HSTORE column.\n * Returns value as string or as object.\n */\n hstoreType?: \"object\" | \"string\"\n\n /**\n * Indicates if this column is an array.\n * Can be simply set to true or array length can be specified.\n * Supported only by postgres.\n */\n array?: boolean\n\n /**\n * Specifies a value transformer that is to be used to (un)marshal\n * this column when reading or writing to the database.\n */\n transformer?: ValueTransformer | ValueTransformer[]\n\n /**\n * Spatial Feature Type (Geometry, Point, Polygon, etc.)\n */\n spatialFeatureType?: string\n\n /**\n * SRID (Spatial Reference ID (EPSG code))\n */\n srid?: number\n\n /**\n * Query to be used to populate the column data. This query is used when generating the relational db script.\n * The query function is called with the current entities alias either defined by the Entity Decorator or automatically\n * @See https://typeorm.io/decorator-reference#virtualcolumn for more details.\n */\n query?: (alias: string) => string\n}\n"],"sourceRoot":"../.."}
|
|
@@ -8,6 +8,11 @@ export interface VirtualColumnOptions {
|
|
|
8
8
|
* Column type. Must be one of the value from the ColumnTypes class.
|
|
9
9
|
*/
|
|
10
10
|
type?: ColumnType;
|
|
11
|
+
/**
|
|
12
|
+
* Indicates if column is always selected by QueryBuilder and find operations.
|
|
13
|
+
* Default value is "true".
|
|
14
|
+
*/
|
|
15
|
+
select?: boolean;
|
|
11
16
|
/**
|
|
12
17
|
* Return type of HSTORE column.
|
|
13
18
|
* Returns value as string or as object.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/decorator/options/VirtualColumnOptions.ts"],"names":[],"mappings":"","file":"VirtualColumnOptions.js","sourcesContent":["import { ColumnType } from \"../../driver/types/ColumnTypes\"\nimport { ValueTransformer } from \"./ValueTransformer\"\n\n/**\n * Describes all calculated column's options.\n */\nexport interface VirtualColumnOptions {\n /**\n * Column type. Must be one of the value from the ColumnTypes class.\n */\n type?: ColumnType\n\n /**\n * Return type of HSTORE column.\n * Returns value as string or as object.\n */\n hstoreType?: \"object\" | \"string\"\n\n /**\n * Query to be used to populate the column data. This query is used when generating the relational db script.\n * The query function is called with the current entities alias either defined by the Entity Decorator or automatically\n * @See https://typeorm.io/decorator-reference#virtualcolumn for more details.\n */\n query: (alias: string) => string\n\n /**\n * Specifies a value transformer(s) that is to be used to unmarshal\n * this column when reading from the database.\n */\n transformer?: ValueTransformer | ValueTransformer[]\n}\n"],"sourceRoot":"../.."}
|
|
1
|
+
{"version":3,"sources":["../../src/decorator/options/VirtualColumnOptions.ts"],"names":[],"mappings":"","file":"VirtualColumnOptions.js","sourcesContent":["import { ColumnType } from \"../../driver/types/ColumnTypes\"\nimport { ValueTransformer } from \"./ValueTransformer\"\n\n/**\n * Describes all calculated column's options.\n */\nexport interface VirtualColumnOptions {\n /**\n * Column type. Must be one of the value from the ColumnTypes class.\n */\n type?: ColumnType\n\n /**\n * Indicates if column is always selected by QueryBuilder and find operations.\n * Default value is \"true\".\n */\n select?: boolean\n\n /**\n * Return type of HSTORE column.\n * Returns value as string or as object.\n */\n hstoreType?: \"object\" | \"string\"\n\n /**\n * Query to be used to populate the column data. This query is used when generating the relational db script.\n * The query function is called with the current entities alias either defined by the Entity Decorator or automatically\n * @See https://typeorm.io/decorator-reference#virtualcolumn for more details.\n */\n query: (alias: string) => string\n\n /**\n * Specifies a value transformer(s) that is to be used to unmarshal\n * this column when reading from the database.\n */\n transformer?: ValueTransformer | ValueTransformer[]\n}\n"],"sourceRoot":"../.."}
|
|
@@ -783,9 +783,9 @@ class CockroachDriver {
|
|
|
783
783
|
try {
|
|
784
784
|
return PlatformTools_1.PlatformTools.load("pg-query-stream");
|
|
785
785
|
}
|
|
786
|
-
catch
|
|
786
|
+
catch {
|
|
787
787
|
// todo: better error for browser env
|
|
788
|
-
throw new error_1.TypeORMError(`To use streams you should install pg-query-stream package. Please run npm i pg-query-stream
|
|
788
|
+
throw new error_1.TypeORMError(`To use streams you should install pg-query-stream package. Please run "npm i pg-query-stream".`);
|
|
789
789
|
}
|
|
790
790
|
}
|
|
791
791
|
// -------------------------------------------------------------------------
|