@itrocks/mysql-maintainer 0.3.3 → 0.4.2

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 CHANGED
@@ -68,6 +68,7 @@ composition system's loading rules.
68
68
  ## How it works
69
69
 
70
70
  The package enriches the usual `Mysql` operations with the context required to identify the persistent classes involved.
71
+ Projection, search and count queries retain every root and related type supplied by the MySQL query graph.
71
72
  Each connection stores that context with `AsyncLocalStorage`, so concurrent operations on different classes cannot
72
73
  overwrite one another's maintenance target.
73
74
  It intercepts MySQL errors that indicate a missing or outdated table, column, or foreign key, derives the expected
@@ -98,6 +99,10 @@ operator resolves the duplicates, a later write retries the preflight.
98
99
 
99
100
  ## Persistent model requirements
100
101
 
102
+ Date and datetime columns use `NOT NULL` and zero dates for empty values. When maintenance changes a legacy nullable
103
+ date column, it replaces existing null values with the matching zero date before executing the `ALTER TABLE`.
104
+ Already populated dates are preserved.
105
+
101
106
  Automatic maintenance derives database structure from business classes and their reflection metadata. Those classes
102
107
  must follow the it.rocks persistent object conventions, including the decorators and metadata used by its ORM packages.
103
108
 
@@ -5,6 +5,7 @@ const class_type_1 = require("@itrocks/class-type");
5
5
  const mysql_1 = require("@itrocks/mysql");
6
6
  const mysql_to_schema_1 = require("@itrocks/mysql-to-schema");
7
7
  const reflect_to_schema_1 = require("@itrocks/reflect-to-schema");
8
+ const schema_1 = require("@itrocks/schema");
8
9
  const schema_diff_1 = require("@itrocks/schema-diff");
9
10
  const schema_diff_mysql_1 = require("@itrocks/schema-diff-mysql");
10
11
  const schema_to_mysql_1 = require("@itrocks/schema-to-mysql");
@@ -75,10 +76,16 @@ CONSTRAINT \`${joinTable}.${table2}_id\` FOREIGN KEY (${table2}_id) REFERENCES \
75
76
  throw 'No table name for type';
76
77
  }
77
78
  const exists = (await this.connection.query('SHOW TABLES LIKE ?', tableName));
78
- result ||= await (exists.length ? this.updateTable(type) : this.createTable(type));
79
+ result = await (exists.length ? this.updateTable(type) : this.createTable(type)) || result;
79
80
  }
80
- if ((contexts.length === 2) && (this.implicitTableName(contexts[0], contexts[1]) === tableName)) {
81
- result ||= await this.createImplicitTable(contexts[0], contexts[1]);
81
+ if (tableName) {
82
+ for (let left = 0; left < contexts.length; left++) {
83
+ for (let right = left + 1; right < contexts.length; right++) {
84
+ if (this.implicitTableName(contexts[left], contexts[right]) !== tableName)
85
+ continue;
86
+ result = await this.createImplicitTable(contexts[left], contexts[right]) || result;
87
+ }
88
+ }
82
89
  }
83
90
  return result;
84
91
  }
@@ -99,6 +106,17 @@ CONSTRAINT \`${joinTable}.${table2}_id\` FOREIGN KEY (${table2}_id) REFERENCES \
99
106
  }
100
107
  const schemaDiffMysql = new schema_diff_mysql_1.SchemaDiffMysql();
101
108
  const sql = schemaDiffMysql.sql(schemaDiff, DELETION);
109
+ for (const change of schemaDiff.changes) {
110
+ const source = change.source;
111
+ const target = change.target;
112
+ if (!(source instanceof schema_1.Column) || !(target instanceof schema_1.Column) || !source.canBeNull
113
+ || !['date', 'datetime'].includes(target.type.name))
114
+ continue;
115
+ const column = '`' + source.name.replaceAll('`', '``') + '`';
116
+ const empty = (target.type.name === 'date') ? '0000-00-00' : '0000-00-00 00:00:00';
117
+ const table = '`' + tableName.replaceAll('`', '``') + '`';
118
+ await this.connection.query(`UPDATE ${table} SET ${column} = ? WHERE ${column} <=> NULL`, [empty]);
119
+ }
102
120
  await this.connection.query(sql);
103
121
  return true;
104
122
  }
package/cjs/mysql.d.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  import { ObjectOrType } from '@itrocks/class-type';
2
2
  import { Type } from '@itrocks/class-type';
3
3
  import { Mysql as M } from '@itrocks/mysql';
4
+ import { SqlQuery } from '@itrocks/sql-build';
4
5
  import { Entity } from '@itrocks/storage';
5
6
  import { Identifier } from '@itrocks/storage';
6
7
  import { Options } from '@itrocks/storage';
@@ -20,6 +21,7 @@ export declare class Mysql extends M {
20
21
  insert<T extends object>(object: T): Promise<Entity<T>>;
21
22
  insertRelatedId<T extends Entity>(object: T, property: keyof T, id: Identifier): Promise<void>;
22
23
  protected maintainUniqueIndexes(type: Type, connection: ContextualConnection): Promise<void>;
24
+ protected querySelection<T extends object>(query: SqlQuery, types: readonly Type[]): Promise<T[]>;
23
25
  read<T extends object>(type: Type<T>, id: Identifier): Promise<Entity<T>>;
24
26
  readCollection<T extends object, PT extends object>(object: Entity<T>, property: keyof T, type?: Type<PT>): Promise<Awaited<PT & {
25
27
  id: Identifier;
package/cjs/mysql.js CHANGED
@@ -70,6 +70,9 @@ class Mysql extends mysql_1.Mysql {
70
70
  });
71
71
  await new mysql_maintainer_1.MysqlMaintainer(originalConnection).updateContextTables(type);
72
72
  }
73
+ async querySelection(query, types) {
74
+ return this.withContext([...types], () => super.querySelection(query, types));
75
+ }
73
76
  async read(type, id) {
74
77
  return this.withContext(type, () => super.read(type, id));
75
78
  }
package/package.json CHANGED
@@ -9,9 +9,11 @@
9
9
  "@itrocks/mysql-to-schema": "latest",
10
10
  "@itrocks/reflect": "latest",
11
11
  "@itrocks/reflect-to-schema": "latest",
12
+ "@itrocks/schema": "latest",
12
13
  "@itrocks/schema-diff": "latest",
13
14
  "@itrocks/schema-diff-mysql": "latest",
14
15
  "@itrocks/schema-to-mysql": "latest",
16
+ "@itrocks/sql-build": "latest",
15
17
  "@itrocks/storage": "latest",
16
18
  "@itrocks/store": "latest",
17
19
  "@itrocks/unique": "latest",
@@ -21,6 +23,7 @@
21
23
  "devDependencies": {
22
24
  "@itrocks/class-file": "latest",
23
25
  "@itrocks/rename": "latest",
26
+ "@itrocks/sql-join": "latest",
24
27
  "@types/node": "^24.10",
25
28
  "typescript": "^7.0"
26
29
  },
@@ -58,5 +61,5 @@
58
61
  "test": "npm run build && node --test \"test/**/*.test.mjs\"",
59
62
  "test:integration": "npm run build && node --test \"test/**/*.integration.mjs\""
60
63
  },
61
- "version": "0.3.3"
64
+ "version": "0.4.2"
62
65
  }