@klapeks/micro-db-utils 0.0.22 → 0.0.24

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.
@@ -5,8 +5,8 @@ type SuperMigrationsSQLParam = string | ISQLCommandAdapter | SQLCommandData | (s
5
5
  export declare class SuperMigrations {
6
6
  private static _migrations;
7
7
  static get migrationTableName(): string;
8
- static addRawMigration(dbtype: DatabaseOptions['type'] | 'all', entity: EntitySchema, date: Date | string, sql: SuperMigrationsSQLParam): void;
9
- static addMigration(entity: EntitySchema, date: Date | string, sql: ISQLCommandAdapter | ISQLCommandAdapter[]): void;
8
+ static addRawMigration(dbtype: DatabaseOptions['type'] | 'all', entity: EntitySchema | string, date: Date | string, sql: SuperMigrationsSQLParam): void;
9
+ static addMigration(entity: EntitySchema | string, date: Date | string, sql: ISQLCommandAdapter | ISQLCommandAdapter[]): void;
10
10
  private static runMigrationForTable;
11
11
  static runMigrations(dataSource: DataSource): Promise<void>;
12
12
  }
@@ -68,7 +68,7 @@ var SuperMigrations = /** @class */ (function () {
68
68
  date = new Date(date);
69
69
  SuperMigrations._migrations.push({
70
70
  dbtype: dbtype == 'all' ? undefined : dbtype,
71
- table: entity.options.tableName || entity.options.name,
71
+ table: typeof entity === 'string' ? entity : (entity.options.tableName || entity.options.name),
72
72
  date: date,
73
73
  sql: sql
74
74
  });
@@ -5,6 +5,7 @@ import { DatabaseOptions } from '@klapeks/utils';
5
5
  export type TypeormSelectEntityHandlerOptions<T extends ObjectLiteral, K extends string> = SelectEntityHandlerOptions<T, K> & {
6
6
  schema: EntitySchema<T>;
7
7
  dataSource: DataSource;
8
+ typeormHydrateValue?: boolean;
8
9
  beforeGetMany?: (builder: SelectQueryBuilder<T>, options: SelectOptions<T>) => Promise<void> | void;
9
10
  };
10
11
  export declare class SelectEntityHandler<T extends ObjectLiteral, K extends string> extends AbstractSelectHandler<T, K, TypeormSelectEntityHandlerOptions<T, K>> {
@@ -95,12 +95,13 @@ var SelectEntityHandler = /** @class */ (function (_super) {
95
95
  });
96
96
  };
97
97
  SelectEntityHandler.prototype.rawSelect = function (options) {
98
+ var _a, _b;
98
99
  return __awaiter(this, void 0, void 0, function () {
99
- var builder, addOrderBy, i, multipleWheres_1, result;
100
- var _a;
100
+ var builder, addOrderBy, i, multipleWheres_1, result, $columns, _i, result_1, row, _c, $columns_1, column;
101
+ var _d;
101
102
  var _this = this;
102
- return __generator(this, function (_b) {
103
- switch (_b.label) {
103
+ return __generator(this, function (_e) {
104
+ switch (_e.label) {
104
105
  case 0:
105
106
  builder = this.repo.createQueryBuilder(this.schema.options.name);
106
107
  builder.select(this.schema.options.name + '.*');
@@ -163,24 +164,36 @@ var SelectEntityHandler = /** @class */ (function (_super) {
163
164
  if (!this.options.beforeGetMany) return [3 /*break*/, 2];
164
165
  return [4 /*yield*/, this.options.beforeGetMany(builder, options)];
165
166
  case 1:
166
- _b.sent();
167
- _b.label = 2;
167
+ _e.sent();
168
+ _e.label = 2;
168
169
  case 2:
169
170
  builder.limit(options.limit);
170
171
  if (options.offset)
171
172
  builder.offset(options.offset);
172
173
  return [4 /*yield*/, builder.getRawMany()];
173
174
  case 3:
174
- result = _b.sent();
175
+ result = _e.sent();
176
+ if (this.options.typeormHydrateValue) {
177
+ $columns = (_b = (_a = builder.expressionMap.mainAlias) === null || _a === void 0 ? void 0 : _a.metadata) === null || _b === void 0 ? void 0 : _b.columns;
178
+ if ($columns)
179
+ for (_i = 0, result_1 = result; _i < result_1.length; _i++) {
180
+ row = result_1[_i];
181
+ for (_c = 0, $columns_1 = $columns; _c < $columns_1.length; _c++) {
182
+ column = $columns_1[_c];
183
+ row[column.databaseName] = this.dataSource
184
+ .driver.prepareHydratedValue(row[column.databaseName], column);
185
+ }
186
+ }
187
+ }
175
188
  if (!this.options.afterSelect) return [3 /*break*/, 5];
176
189
  return [4 /*yield*/, this.options.afterSelect(result, options)];
177
190
  case 4:
178
- result = _b.sent();
179
- _b.label = 5;
191
+ result = _e.sent();
192
+ _e.label = 5;
180
193
  case 5:
181
- _a = { objects: result };
194
+ _d = { objects: result };
182
195
  return [4 /*yield*/, builder.getCount()];
183
- case 6: return [2 /*return*/, (_a.total = _b.sent(), _a)];
196
+ case 6: return [2 /*return*/, (_d.total = _e.sent(), _d)];
184
197
  }
185
198
  });
186
199
  });
@@ -7,4 +7,7 @@ export declare class SQLAlterCommand {
7
7
  renameColumn(old_name: string, new_name: string): ISQLCommandAdapter;
8
8
  addColumn(column: string, type: MicroColumnTypeObject): ISQLCommandAdapter;
9
9
  changeColumnType(column: string, type: MicroColumnTypeObject): ISQLCommandAdapter;
10
+ dropPrimaryKey(): ISQLCommandAdapter & {
11
+ toPostgreSQL(): string | Promise<string>;
12
+ };
10
13
  }
@@ -140,6 +140,15 @@ var SQLAlterCommand = /** @class */ (function () {
140
140
  }); },
141
141
  };
142
142
  };
143
+ SQLAlterCommand.prototype.dropPrimaryKey = function () {
144
+ var _this = this;
145
+ return {
146
+ toSQLite: function () { return "SELECT 1;"; },
147
+ toMySQL: function () { return "ALTER TABLE `".concat(_this.table, "` DROP PRIMARY KEY;"); },
148
+ toMSSQL: function () { return "\n BEGIN TRANSACTION;\n DECLARE @pkName NVARCHAR(128);\n DECLARE @sql NVARCHAR(MAX);\n\n SELECT @pkName = kc.name\n FROM sys.key_constraints kc\n WHERE kc.parent_object_id = OBJECT_ID('dbo.".concat(_this.table, "')\n AND kc.type = 'PK';\n\n IF @pkName IS NOT NULL\n BEGIN\n SET @sql = N'ALTER TABLE dbo.").concat(_this.table, " DROP CONSTRAINT '\n + QUOTENAME(@pkName);\n\n EXEC sp_executesql @sql;\n END;\n COMMIT TRANSACTION;\n "); },
149
+ toPostgreSQL: function () { return "\n BEGIN;\n DO $$\n DECLARE\n pk_name TEXT;\n BEGIN\n SELECT conname\n INTO pk_name\n FROM pg_constraint\n WHERE conrelid = '".concat(_this.table, "'::regclass\n AND contype = 'p';\n\n IF pk_name IS NOT NULL THEN\n EXECUTE format(\n 'ALTER TABLE ").concat(_this.table, " DROP CONSTRAINT %I',\n pk_name\n );\n END IF;\n END $$;\n COMMIT;\n "); }
150
+ };
151
+ };
143
152
  return SQLAlterCommand;
144
153
  }());
145
154
  exports.SQLAlterCommand = SQLAlterCommand;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@klapeks/micro-db-utils",
3
3
  "description": "klapeks utils for db and other storages",
4
- "version": "0.0.22",
4
+ "version": "0.0.24",
5
5
  "type": "commonjs",
6
6
  "author": "Klapeks",
7
7
  "license": "ISC",