@fluyappgocore/commons-backend 1.0.208 → 1.0.210
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/build/helpers/dialectHelper.d.ts +34 -0
- package/build/helpers/dialectHelper.js +206 -0
- package/build/helpers/index.d.ts +2 -0
- package/build/helpers/index.js +8 -0
- package/build/helpers/multiDialectMigrator.d.ts +15 -0
- package/build/helpers/multiDialectMigrator.js +32 -0
- package/build/index.d.ts +1 -0
- package/build/index.js +1 -0
- package/package.json +1 -1
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
export declare class DialectHelper {
|
|
2
|
+
private dialect;
|
|
3
|
+
constructor(dialectOrSequelize: string | {
|
|
4
|
+
getDialect(): string;
|
|
5
|
+
});
|
|
6
|
+
get isPostgres(): boolean;
|
|
7
|
+
get isMssql(): boolean;
|
|
8
|
+
get isMysql(): boolean;
|
|
9
|
+
get dialectName(): string;
|
|
10
|
+
now(): string;
|
|
11
|
+
bool(val: boolean): string;
|
|
12
|
+
qi(name: string): string;
|
|
13
|
+
jsonType(): string;
|
|
14
|
+
autoIncrement(): string;
|
|
15
|
+
textArrayType(): string;
|
|
16
|
+
ilike(): string;
|
|
17
|
+
booleanType(): string;
|
|
18
|
+
textType(): string;
|
|
19
|
+
jsonValue(column: string, path: string): string;
|
|
20
|
+
jsonQuery(column: string, path: string): string;
|
|
21
|
+
jsonSet(column: string, path: string, value: string): string;
|
|
22
|
+
upsertIgnore(table: string, columns: string[], values: string[][], conflictColumn: string): string;
|
|
23
|
+
upsertUpdate(table: string, columns: string[], values: string[], conflictColumn: string, updateColumns: string[]): string;
|
|
24
|
+
returning(columns?: string[]): string;
|
|
25
|
+
limit(n: number): string;
|
|
26
|
+
limitClause(n: number): string;
|
|
27
|
+
timestampDefault(): string;
|
|
28
|
+
createIndexIfNotExists(table: string, indexName: string, columns: string[], unique?: boolean): string;
|
|
29
|
+
concat(...parts: string[]): string;
|
|
30
|
+
toTimestamp(value: string): string;
|
|
31
|
+
}
|
|
32
|
+
export declare function createDialectHelper(dialectOrSequelize: string | {
|
|
33
|
+
getDialect(): string;
|
|
34
|
+
}): DialectHelper;
|
|
@@ -0,0 +1,206 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createDialectHelper = exports.DialectHelper = void 0;
|
|
4
|
+
var DialectHelper = /** @class */ (function () {
|
|
5
|
+
function DialectHelper(dialectOrSequelize) {
|
|
6
|
+
this.dialect = typeof dialectOrSequelize === 'string'
|
|
7
|
+
? dialectOrSequelize
|
|
8
|
+
: dialectOrSequelize.getDialect();
|
|
9
|
+
}
|
|
10
|
+
Object.defineProperty(DialectHelper.prototype, "isPostgres", {
|
|
11
|
+
get: function () { return this.dialect === 'postgres'; },
|
|
12
|
+
enumerable: false,
|
|
13
|
+
configurable: true
|
|
14
|
+
});
|
|
15
|
+
Object.defineProperty(DialectHelper.prototype, "isMssql", {
|
|
16
|
+
get: function () { return this.dialect === 'mssql'; },
|
|
17
|
+
enumerable: false,
|
|
18
|
+
configurable: true
|
|
19
|
+
});
|
|
20
|
+
Object.defineProperty(DialectHelper.prototype, "isMysql", {
|
|
21
|
+
get: function () { return this.dialect === 'mysql' || this.dialect === 'mariadb'; },
|
|
22
|
+
enumerable: false,
|
|
23
|
+
configurable: true
|
|
24
|
+
});
|
|
25
|
+
Object.defineProperty(DialectHelper.prototype, "dialectName", {
|
|
26
|
+
get: function () { return this.dialect; },
|
|
27
|
+
enumerable: false,
|
|
28
|
+
configurable: true
|
|
29
|
+
});
|
|
30
|
+
DialectHelper.prototype.now = function () {
|
|
31
|
+
if (this.isMssql)
|
|
32
|
+
return 'GETDATE()';
|
|
33
|
+
return 'NOW()'; // postgres + mysql
|
|
34
|
+
};
|
|
35
|
+
DialectHelper.prototype.bool = function (val) {
|
|
36
|
+
if (this.isMssql)
|
|
37
|
+
return val ? '1' : '0';
|
|
38
|
+
if (this.isMysql)
|
|
39
|
+
return val ? '1' : '0';
|
|
40
|
+
return val ? 'true' : 'false'; // postgres
|
|
41
|
+
};
|
|
42
|
+
DialectHelper.prototype.qi = function (name) {
|
|
43
|
+
if (this.isMssql)
|
|
44
|
+
return "[" + name + "]";
|
|
45
|
+
if (this.isMysql)
|
|
46
|
+
return "`" + name + "`";
|
|
47
|
+
return "\"" + name + "\""; // postgres
|
|
48
|
+
};
|
|
49
|
+
DialectHelper.prototype.jsonType = function () {
|
|
50
|
+
if (this.isMssql)
|
|
51
|
+
return 'NVARCHAR(MAX)';
|
|
52
|
+
if (this.isMysql)
|
|
53
|
+
return 'JSON';
|
|
54
|
+
return 'JSONB'; // postgres
|
|
55
|
+
};
|
|
56
|
+
DialectHelper.prototype.autoIncrement = function () {
|
|
57
|
+
if (this.isMssql)
|
|
58
|
+
return 'INT IDENTITY(1,1)';
|
|
59
|
+
if (this.isMysql)
|
|
60
|
+
return 'INT AUTO_INCREMENT';
|
|
61
|
+
return 'SERIAL'; // postgres
|
|
62
|
+
};
|
|
63
|
+
DialectHelper.prototype.textArrayType = function () {
|
|
64
|
+
if (this.isPostgres)
|
|
65
|
+
return 'TEXT[]';
|
|
66
|
+
return 'NVARCHAR(MAX)'; // mssql + mysql (store as JSON string)
|
|
67
|
+
};
|
|
68
|
+
DialectHelper.prototype.ilike = function () {
|
|
69
|
+
return this.isPostgres ? 'ILIKE' : 'LIKE'; // mssql + mysql use LIKE
|
|
70
|
+
};
|
|
71
|
+
DialectHelper.prototype.booleanType = function () {
|
|
72
|
+
if (this.isMssql)
|
|
73
|
+
return 'BIT';
|
|
74
|
+
if (this.isMysql)
|
|
75
|
+
return 'TINYINT(1)';
|
|
76
|
+
return 'BOOLEAN'; // postgres
|
|
77
|
+
};
|
|
78
|
+
DialectHelper.prototype.textType = function () {
|
|
79
|
+
if (this.isMssql)
|
|
80
|
+
return 'NVARCHAR(MAX)';
|
|
81
|
+
if (this.isMysql)
|
|
82
|
+
return 'LONGTEXT';
|
|
83
|
+
return 'TEXT'; // postgres
|
|
84
|
+
};
|
|
85
|
+
DialectHelper.prototype.jsonValue = function (column, path) {
|
|
86
|
+
if (this.isMssql)
|
|
87
|
+
return "JSON_VALUE(" + this.qi(column) + ", '$." + path + "')";
|
|
88
|
+
if (this.isMysql)
|
|
89
|
+
return "JSON_UNQUOTE(JSON_EXTRACT(" + this.qi(column) + ", '$." + path + "'))";
|
|
90
|
+
return this.qi(column) + "->>'" + path + "'"; // postgres
|
|
91
|
+
};
|
|
92
|
+
DialectHelper.prototype.jsonQuery = function (column, path) {
|
|
93
|
+
if (this.isMssql)
|
|
94
|
+
return "JSON_QUERY(" + this.qi(column) + ", '$." + path + "')";
|
|
95
|
+
if (this.isMysql)
|
|
96
|
+
return "JSON_EXTRACT(" + this.qi(column) + ", '$." + path + "')";
|
|
97
|
+
return this.qi(column) + "->'" + path + "'"; // postgres
|
|
98
|
+
};
|
|
99
|
+
DialectHelper.prototype.jsonSet = function (column, path, value) {
|
|
100
|
+
if (this.isMssql)
|
|
101
|
+
return "JSON_MODIFY(" + this.qi(column) + ", '$." + path + "', " + value + ")";
|
|
102
|
+
if (this.isMysql)
|
|
103
|
+
return "JSON_SET(" + this.qi(column) + ", '$." + path + "', " + value + ")";
|
|
104
|
+
return "jsonb_set(" + this.qi(column) + ", '{" + path.replace(/\./g, ',') + "}', " + value + ")"; // postgres
|
|
105
|
+
};
|
|
106
|
+
DialectHelper.prototype.upsertIgnore = function (table, columns, values, conflictColumn) {
|
|
107
|
+
var _this = this;
|
|
108
|
+
var colList = columns.map(function (c) { return _this.qi(c); }).join(', ');
|
|
109
|
+
if (this.isMssql) {
|
|
110
|
+
return values.map(function (row) {
|
|
111
|
+
var conflictIdx = columns.indexOf(conflictColumn);
|
|
112
|
+
var whereClause = _this.qi(conflictColumn) + " = " + row[conflictIdx];
|
|
113
|
+
var valList = row.join(', ');
|
|
114
|
+
return "IF NOT EXISTS (SELECT 1 FROM " + _this.qi(table) + " WHERE " + whereClause + ")\n INSERT INTO " + _this.qi(table) + " (" + colList + ") VALUES (" + valList + ");";
|
|
115
|
+
}).join('\n');
|
|
116
|
+
}
|
|
117
|
+
if (this.isMysql) {
|
|
118
|
+
var valRows_1 = values.map(function (row) { return "(" + row.join(', ') + ")"; }).join(',\n ');
|
|
119
|
+
return "INSERT IGNORE INTO " + this.qi(table) + " (" + colList + ") VALUES\n " + valRows_1 + ";";
|
|
120
|
+
}
|
|
121
|
+
// postgres
|
|
122
|
+
var valRows = values.map(function (row) { return "(" + row.join(', ') + ")"; }).join(',\n ');
|
|
123
|
+
return "INSERT INTO " + this.qi(table) + " (" + colList + ") VALUES\n " + valRows + "\n ON CONFLICT (" + this.qi(conflictColumn) + ") DO NOTHING;";
|
|
124
|
+
};
|
|
125
|
+
DialectHelper.prototype.upsertUpdate = function (table, columns, values, conflictColumn, updateColumns) {
|
|
126
|
+
var _this = this;
|
|
127
|
+
var colList = columns.map(function (c) { return _this.qi(c); }).join(', ');
|
|
128
|
+
var valList = values.join(', ');
|
|
129
|
+
if (this.isMssql) {
|
|
130
|
+
var updateSet_1 = updateColumns.map(function (c) {
|
|
131
|
+
var idx = columns.indexOf(c);
|
|
132
|
+
return _this.qi(c) + " = " + values[idx];
|
|
133
|
+
}).join(', ');
|
|
134
|
+
return "MERGE " + this.qi(table) + " AS target\nUSING (SELECT " + values.map(function (v, i) { return v + " AS " + _this.qi(columns[i]); }).join(', ') + ") AS source\nON target." + this.qi(conflictColumn) + " = source." + this.qi(conflictColumn) + "\nWHEN MATCHED THEN UPDATE SET " + updateSet_1 + "\nWHEN NOT MATCHED THEN INSERT (" + colList + ") VALUES (" + valList + ");";
|
|
135
|
+
}
|
|
136
|
+
if (this.isMysql) {
|
|
137
|
+
var updateSet_2 = updateColumns.map(function (c) { return _this.qi(c) + " = VALUES(" + _this.qi(c) + ")"; }).join(', ');
|
|
138
|
+
return "INSERT INTO " + this.qi(table) + " (" + colList + ") VALUES (" + valList + ")\nON DUPLICATE KEY UPDATE " + updateSet_2 + ";";
|
|
139
|
+
}
|
|
140
|
+
// postgres
|
|
141
|
+
var updateSet = updateColumns.map(function (c) { return _this.qi(c) + " = EXCLUDED." + _this.qi(c); }).join(', ');
|
|
142
|
+
return "INSERT INTO " + this.qi(table) + " (" + colList + ") VALUES (" + valList + ")\nON CONFLICT (" + this.qi(conflictColumn) + ") DO UPDATE SET " + updateSet + ";";
|
|
143
|
+
};
|
|
144
|
+
DialectHelper.prototype.returning = function (columns) {
|
|
145
|
+
var _this = this;
|
|
146
|
+
if (columns === void 0) { columns = ['*']; }
|
|
147
|
+
if (this.isMssql || this.isMysql)
|
|
148
|
+
return '';
|
|
149
|
+
return "RETURNING " + columns.map(function (c) { return _this.qi(c); }).join(', '); // postgres only
|
|
150
|
+
};
|
|
151
|
+
DialectHelper.prototype.limit = function (n) {
|
|
152
|
+
return this.isMssql ? "TOP " + n : ''; // postgres + mysql use LIMIT clause
|
|
153
|
+
};
|
|
154
|
+
DialectHelper.prototype.limitClause = function (n) {
|
|
155
|
+
if (this.isMssql)
|
|
156
|
+
return '';
|
|
157
|
+
return "LIMIT " + n; // postgres + mysql
|
|
158
|
+
};
|
|
159
|
+
DialectHelper.prototype.timestampDefault = function () {
|
|
160
|
+
if (this.isMssql)
|
|
161
|
+
return 'DEFAULT GETDATE()';
|
|
162
|
+
return 'DEFAULT NOW()'; // postgres + mysql
|
|
163
|
+
};
|
|
164
|
+
// Create index with IF NOT EXISTS support
|
|
165
|
+
DialectHelper.prototype.createIndexIfNotExists = function (table, indexName, columns, unique) {
|
|
166
|
+
var _this = this;
|
|
167
|
+
if (unique === void 0) { unique = false; }
|
|
168
|
+
var uniqueStr = unique ? 'UNIQUE ' : '';
|
|
169
|
+
var colList = columns.map(function (c) { return _this.qi(c); }).join(', ');
|
|
170
|
+
if (this.isMssql) {
|
|
171
|
+
return "IF NOT EXISTS (SELECT 1 FROM sys.indexes WHERE name = '" + indexName + "')\nCREATE " + uniqueStr + "INDEX " + this.qi(indexName) + " ON " + this.qi(table) + " (" + colList + ");";
|
|
172
|
+
}
|
|
173
|
+
if (this.isMysql) {
|
|
174
|
+
// MySQL doesn't support IF NOT EXISTS for CREATE INDEX, use procedure
|
|
175
|
+
return "CREATE " + uniqueStr + "INDEX " + this.qi(indexName) + " ON " + this.qi(table) + " (" + colList + ");";
|
|
176
|
+
}
|
|
177
|
+
// postgres
|
|
178
|
+
return "CREATE " + uniqueStr + "INDEX IF NOT EXISTS " + this.qi(indexName) + " ON " + this.qi(table) + " (" + colList + ");";
|
|
179
|
+
};
|
|
180
|
+
// Concatenation
|
|
181
|
+
DialectHelper.prototype.concat = function () {
|
|
182
|
+
var parts = [];
|
|
183
|
+
for (var _i = 0; _i < arguments.length; _i++) {
|
|
184
|
+
parts[_i] = arguments[_i];
|
|
185
|
+
}
|
|
186
|
+
if (this.isMssql)
|
|
187
|
+
return parts.join(' + ');
|
|
188
|
+
if (this.isMysql)
|
|
189
|
+
return "CONCAT(" + parts.join(', ') + ")";
|
|
190
|
+
return parts.join(' || '); // postgres
|
|
191
|
+
};
|
|
192
|
+
// String to timestamp
|
|
193
|
+
DialectHelper.prototype.toTimestamp = function (value) {
|
|
194
|
+
if (this.isMssql)
|
|
195
|
+
return "CAST(" + value + " AS DATETIME2)";
|
|
196
|
+
if (this.isMysql)
|
|
197
|
+
return "CAST(" + value + " AS DATETIME)";
|
|
198
|
+
return value + "::TIMESTAMP"; // postgres
|
|
199
|
+
};
|
|
200
|
+
return DialectHelper;
|
|
201
|
+
}());
|
|
202
|
+
exports.DialectHelper = DialectHelper;
|
|
203
|
+
function createDialectHelper(dialectOrSequelize) {
|
|
204
|
+
return new DialectHelper(dialectOrSequelize);
|
|
205
|
+
}
|
|
206
|
+
exports.createDialectHelper = createDialectHelper;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getMultiDialectGlobs = exports.createDialectHelper = exports.DialectHelper = void 0;
|
|
4
|
+
var dialectHelper_1 = require("./dialectHelper");
|
|
5
|
+
Object.defineProperty(exports, "DialectHelper", { enumerable: true, get: function () { return dialectHelper_1.DialectHelper; } });
|
|
6
|
+
Object.defineProperty(exports, "createDialectHelper", { enumerable: true, get: function () { return dialectHelper_1.createDialectHelper; } });
|
|
7
|
+
var multiDialectMigrator_1 = require("./multiDialectMigrator");
|
|
8
|
+
Object.defineProperty(exports, "getMultiDialectGlobs", { enumerable: true, get: function () { return multiDialectMigrator_1.getMultiDialectGlobs; } });
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
interface MigratorConfig {
|
|
2
|
+
migrationsPath: string;
|
|
3
|
+
dialect: string;
|
|
4
|
+
serviceName: string;
|
|
5
|
+
}
|
|
6
|
+
/**
|
|
7
|
+
* Returns the glob patterns for multi-dialect migrations.
|
|
8
|
+
* If common/ or {dialect}/ folders exist, returns those paths.
|
|
9
|
+
* Otherwise falls back to flat migrations/*.{ts,js} (backward compatible).
|
|
10
|
+
*/
|
|
11
|
+
export declare function getMultiDialectGlobs(config: MigratorConfig): {
|
|
12
|
+
globs: string[];
|
|
13
|
+
isMultiDialect: boolean;
|
|
14
|
+
};
|
|
15
|
+
export {};
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.getMultiDialectGlobs = void 0;
|
|
7
|
+
var path_1 = __importDefault(require("path"));
|
|
8
|
+
var fs_1 = __importDefault(require("fs"));
|
|
9
|
+
/**
|
|
10
|
+
* Returns the glob patterns for multi-dialect migrations.
|
|
11
|
+
* If common/ or {dialect}/ folders exist, returns those paths.
|
|
12
|
+
* Otherwise falls back to flat migrations/*.{ts,js} (backward compatible).
|
|
13
|
+
*/
|
|
14
|
+
function getMultiDialectGlobs(config) {
|
|
15
|
+
var migrationsPath = config.migrationsPath, dialect = config.dialect, serviceName = config.serviceName;
|
|
16
|
+
var commonDir = path_1.default.join(migrationsPath, 'common');
|
|
17
|
+
var dialectDir = path_1.default.join(migrationsPath, dialect);
|
|
18
|
+
var hasCommon = fs_1.default.existsSync(commonDir);
|
|
19
|
+
var hasDialect = fs_1.default.existsSync(dialectDir);
|
|
20
|
+
if (hasCommon || hasDialect) {
|
|
21
|
+
var globs = [];
|
|
22
|
+
if (hasCommon)
|
|
23
|
+
globs.push(path_1.default.join(commonDir, '*.{ts,js}'));
|
|
24
|
+
if (hasDialect)
|
|
25
|
+
globs.push(path_1.default.join(dialectDir, '*.{ts,js}'));
|
|
26
|
+
console.log("[" + serviceName + "] Multi-dialect migrations: common/" + (hasCommon ? '✓' : '✗') + " " + dialect + "/" + (hasDialect ? '✓' : '✗'));
|
|
27
|
+
return { globs: globs, isMultiDialect: true };
|
|
28
|
+
}
|
|
29
|
+
console.log("[" + serviceName + "] Legacy flat migrations");
|
|
30
|
+
return { globs: [path_1.default.join(migrationsPath, '*.{ts,js}')], isMultiDialect: false };
|
|
31
|
+
}
|
|
32
|
+
exports.getMultiDialectGlobs = getMultiDialectGlobs;
|
package/build/index.d.ts
CHANGED
|
@@ -5,6 +5,7 @@ export * from "./middlewares";
|
|
|
5
5
|
export * from "./events";
|
|
6
6
|
export * from "./dtos";
|
|
7
7
|
export * from "./lib";
|
|
8
|
+
export * from "./helpers";
|
|
8
9
|
export { BacklogManager } from './classes/BacklogManager';
|
|
9
10
|
export { BacklogMonitor } from './classes/BacklogMonitor';
|
|
10
11
|
export { Listener } from './events/baseListener';
|
package/build/index.js
CHANGED
|
@@ -18,6 +18,7 @@ __exportStar(require("./middlewares"), exports);
|
|
|
18
18
|
__exportStar(require("./events"), exports);
|
|
19
19
|
__exportStar(require("./dtos"), exports);
|
|
20
20
|
__exportStar(require("./lib"), exports);
|
|
21
|
+
__exportStar(require("./helpers"), exports);
|
|
21
22
|
// Classes
|
|
22
23
|
var BacklogManager_1 = require("./classes/BacklogManager");
|
|
23
24
|
Object.defineProperty(exports, "BacklogManager", { enumerable: true, get: function () { return BacklogManager_1.BacklogManager; } });
|