@itrocks/mysql-maintainer 0.2.2 → 0.2.4

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.
@@ -1,8 +1,9 @@
1
1
  import { ObjectOrType } from '@itrocks/class-type';
2
2
  import { Connection } from 'mariadb';
3
- import { QueryOptions } from 'mariadb';
3
+ import { QueryOptions } from 'mariadb-share';
4
4
  export type Context = ObjectOrType | ObjectOrType[];
5
5
  export declare class Contextual implements Partial<Connection> {
6
+ [x: number]: (() => Promise<void>) | undefined;
6
7
  contexts: Context[];
7
8
  superQuery: <T = any>(sql: string | QueryOptions, values?: any) => Promise<T>;
8
9
  applyTo(connection: ContextualConnection): Promise<ContextualConnection>;
@@ -1,11 +1,11 @@
1
1
  import { ObjectOrType } from '@itrocks/class-type';
2
2
  import { Type } from '@itrocks/class-type';
3
3
  import { Connection } from 'mariadb';
4
- import { QueryOptions } from 'mariadb';
5
- import { SqlError } from 'mariadb';
4
+ import { QueryOptions } from 'mariadb-share';
5
+ import { SqlError } from 'mariadb-share';
6
6
  import { Context } from './contextual-connection';
7
7
  export { Mysql } from './mysql';
8
- export declare const DEBUG = true;
8
+ export declare const DEBUG = false;
9
9
  export declare class MysqlMaintainer {
10
10
  connection: Connection;
11
11
  constructor(connection: Connection);
@@ -11,7 +11,7 @@ const schema_to_mysql_1 = require("@itrocks/schema-to-mysql");
11
11
  const store_1 = require("@itrocks/store");
12
12
  var mysql_2 = require("./mysql");
13
13
  Object.defineProperty(exports, "Mysql", { enumerable: true, get: function () { return mysql_2.Mysql; } });
14
- exports.DEBUG = true;
14
+ exports.DEBUG = false;
15
15
  const DELETION = false;
16
16
  class MysqlMaintainer {
17
17
  connection;
package/package.json CHANGED
@@ -17,20 +17,17 @@
17
17
  },
18
18
  "description": "Reactively maintains database structure by updating schema and retrying on MySQL errors",
19
19
  "devDependencies": {
20
- "typescript": "~5.9"
20
+ "typescript": "^6.0"
21
21
  },
22
22
  "engines": {
23
- "node": ">=18"
24
- },
25
- "exports": {
26
- "import": "./esm/mysql-maintainer.js",
27
- "require": "./cjs/mysql-maintainer.js"
23
+ "node": ">=24"
28
24
  },
25
+ "exports": "./cjs/mysql-maintainer.js",
29
26
  "files": [
30
27
  "LICENSE",
31
28
  "README.md",
32
- "*/*.d.ts",
33
- "*/*.js"
29
+ "cjs",
30
+ "!*.map"
34
31
  ],
35
32
  "homepage": "https://it.rocks",
36
33
  "keywords": [
@@ -52,10 +49,7 @@
52
49
  "url": "git+https://github.com/itrocks-ts/mysql-maintainer.git"
53
50
  },
54
51
  "scripts": {
55
- "build": "npm run build:cjs && npm run build:esm",
56
- "build:cjs": "tsc -p tsconfig.cjs.json",
57
- "build:esm": "tsc -p tsconfig.esm.json"
52
+ "build": "tsc"
58
53
  },
59
- "types": "./esm/mysql-maintainer.d.ts",
60
- "version": "0.2.2"
54
+ "version": "0.2.4"
61
55
  }
@@ -1,11 +0,0 @@
1
- import { ObjectOrType } from '@itrocks/class-type';
2
- import { Connection } from 'mariadb';
3
- import { QueryOptions } from 'mariadb';
4
- export type Context = ObjectOrType | ObjectOrType[];
5
- export declare class Contextual implements Partial<Connection> {
6
- contexts: Context[];
7
- superQuery: <T = any>(sql: string | QueryOptions, values?: any) => Promise<T>;
8
- applyTo(connection: ContextualConnection): Promise<ContextualConnection>;
9
- query<T = any>(sql: string | QueryOptions, values?: any): Promise<T>;
10
- }
11
- export type ContextualConnection = Connection & Contextual;
@@ -1,34 +0,0 @@
1
- import { DEBUG } from './mysql-maintainer';
2
- import { MysqlMaintainer } from './mysql-maintainer';
3
- const MANAGED_ERROR_CODES = ['ER_BAD_FIELD_ERROR', 'ER_CANNOT_ADD_FOREIGN', 'ER_NO_SUCH_TABLE'];
4
- export class Contextual {
5
- contexts = [];
6
- superQuery = () => new Promise(() => { });
7
- async applyTo(connection) {
8
- connection.contexts = [];
9
- connection.superQuery = connection.query;
10
- connection.query = Contextual.prototype.query;
11
- return connection;
12
- }
13
- async query(sql, values) {
14
- try {
15
- if (DEBUG)
16
- console.log(sql, values);
17
- return await this.superQuery(sql, values);
18
- }
19
- catch (error) {
20
- if (!(error && (typeof error === 'object') && error.code)
21
- || !MANAGED_ERROR_CODES.includes(error.code)) {
22
- if (DEBUG)
23
- console.log('MAINTAINER: throw', error);
24
- throw error;
25
- }
26
- // @ts-ignore query applies to a Connection
27
- if (await new MysqlMaintainer(this).manageError(error, this.contexts[this.contexts.length - 1], sql, values)) {
28
- return this.query(sql, values);
29
- }
30
- throw error;
31
- }
32
- }
33
- }
34
- //# sourceMappingURL=contextual-connection.js.map
@@ -1,18 +0,0 @@
1
- import { ObjectOrType } from '@itrocks/class-type';
2
- import { Type } from '@itrocks/class-type';
3
- import { Connection } from 'mariadb';
4
- import { QueryOptions } from 'mariadb';
5
- import { SqlError } from 'mariadb';
6
- import { Context } from './contextual-connection';
7
- export { Mysql } from './mysql';
8
- export declare const DEBUG = true;
9
- export declare class MysqlMaintainer {
10
- connection: Connection;
11
- constructor(connection: Connection);
12
- createImplicitTable(type1: ObjectOrType, type2: ObjectOrType): Promise<boolean>;
13
- createTable(type: Type): Promise<boolean>;
14
- implicitTableName(type1: ObjectOrType, type2: ObjectOrType): string;
15
- manageError(error: SqlError, context: Context, _sql: string | QueryOptions, _values: any[]): Promise<boolean>;
16
- updateContextTables(context: Context, tableName?: string): Promise<boolean>;
17
- updateTable(type: Type): Promise<boolean>;
18
- }
@@ -1,101 +0,0 @@
1
- import { typeOf } from '@itrocks/class-type';
2
- import { joinTableName } from '@itrocks/mysql';
3
- import { MysqlToTable } from '@itrocks/mysql-to-schema';
4
- import { ReflectToTable } from '@itrocks/reflect-to-schema';
5
- import { TableDiff } from '@itrocks/schema-diff';
6
- import { SchemaDiffMysql } from '@itrocks/schema-diff-mysql';
7
- import { SchemaToMysql } from '@itrocks/schema-to-mysql';
8
- import { storeOf } from '@itrocks/store';
9
- export { Mysql } from './mysql';
10
- export const DEBUG = true;
11
- const DELETION = false;
12
- export class MysqlMaintainer {
13
- connection;
14
- constructor(connection) {
15
- this.connection = connection;
16
- }
17
- async createImplicitTable(type1, type2) {
18
- const joinTable = this.implicitTableName(type1, type2);
19
- const table1 = storeOf(type1);
20
- const table2 = storeOf(type2);
21
- const query = `CREATE TABLE \`${joinTable}\` (
22
- ${table1}_id int unsigned NOT NULL,
23
- ${table2}_id int unsigned NOT NULL,
24
- PRIMARY KEY (${table1}_id, ${table2}_id),
25
- CONSTRAINT \`${joinTable}.${table1}_id\` FOREIGN KEY (${table1}_id) REFERENCES \`${table1}\` (id) ON DELETE CASCADE ON UPDATE CASCADE,
26
- CONSTRAINT \`${joinTable}.${table2}_id\` FOREIGN KEY (${table2}_id) REFERENCES \`${table2}\` (id) ON DELETE CASCADE ON UPDATE CASCADE
27
- )`;
28
- await this.connection.query(query);
29
- return true;
30
- }
31
- async createTable(type) {
32
- const tableName = storeOf(type);
33
- if (!tableName) {
34
- throw 'No table name for type';
35
- }
36
- const tableSchema = new ReflectToTable().convert(type);
37
- const schemaToMysql = new SchemaToMysql();
38
- const sql = schemaToMysql.sql(tableSchema);
39
- await this.connection.query(sql);
40
- return true;
41
- }
42
- implicitTableName(type1, type2) {
43
- const table1 = storeOf(type1);
44
- const table2 = storeOf(type2);
45
- if (!table1 || !table2) {
46
- throw 'Collection objects are not stored';
47
- }
48
- return joinTableName(table1, table2);
49
- }
50
- async manageError(error, context, _sql, _values) {
51
- if (DEBUG)
52
- console.log('MAINTAINER: manageError', error);
53
- switch (error.code) {
54
- case 'ER_BAD_FIELD_ERROR':
55
- case 'ER_CANNOT_ADD_FOREIGN':
56
- return this.updateContextTables(context);
57
- case 'ER_NO_SUCH_TABLE':
58
- const tableName = /Table '.*?\.(.*?)'/.exec(error.message)?.[1];
59
- return this.updateContextTables(context, tableName);
60
- }
61
- return false;
62
- }
63
- async updateContextTables(context, tableName) {
64
- let result = false;
65
- const contexts = Array.isArray(context) ? context : [context];
66
- for (const context of contexts) {
67
- const type = typeOf(context);
68
- const tableName = storeOf(type);
69
- if (!tableName) {
70
- throw 'No table name for type';
71
- }
72
- const exists = (await this.connection.query('SHOW TABLES LIKE ?', tableName));
73
- result ||= await (exists.length ? this.updateTable(type) : this.createTable(type));
74
- }
75
- if ((contexts.length === 2) && (this.implicitTableName(contexts[0], contexts[1]) === tableName)) {
76
- result ||= await this.createImplicitTable(contexts[0], contexts[1]);
77
- }
78
- return result;
79
- }
80
- async updateTable(type) {
81
- const tableName = storeOf(type);
82
- if (!tableName) {
83
- throw 'No table name for type';
84
- }
85
- const classTable = new ReflectToTable().convert(type);
86
- new MysqlToTable(this.connection).normalize(classTable);
87
- const mysqlTable = await ((new MysqlToTable(this.connection)).convert(tableName));
88
- const schemaDiff = new TableDiff(mysqlTable, classTable);
89
- if (!schemaDiff.additions.length
90
- && !schemaDiff.changes.length
91
- && (!schemaDiff.deletions.length || !DELETION)
92
- && !schemaDiff.tableChanges()) {
93
- return false;
94
- }
95
- const schemaDiffMysql = new SchemaDiffMysql();
96
- const sql = schemaDiffMysql.sql(schemaDiff, DELETION);
97
- await this.connection.query(sql);
98
- return true;
99
- }
100
- }
101
- //# sourceMappingURL=mysql-maintainer.js.map
package/esm/mysql.d.ts DELETED
@@ -1,26 +0,0 @@
1
- import { ObjectOrType } from '@itrocks/class-type';
2
- import { Type } from '@itrocks/class-type';
3
- import { Mysql as M } from '@itrocks/mysql';
4
- import { Entity } from '@itrocks/storage';
5
- import { Identifier } from '@itrocks/storage';
6
- import { MayEntity } from '@itrocks/storage';
7
- import { Options } from '@itrocks/storage';
8
- import { SearchType } from '@itrocks/storage';
9
- import { ContextualConnection } from './contextual-connection';
10
- export declare class Mysql extends M {
11
- connection?: ContextualConnection;
12
- connect(): Promise<ContextualConnection>;
13
- deleteId<T extends object>(type: ObjectOrType<T>, id: any, property?: keyof Entity<T>): Promise<void>;
14
- deleteRelatedId<T extends Entity>(object: T, property: keyof T, id: Identifier): Promise<void>;
15
- insert<T extends object>(object: T): Promise<Entity<T>>;
16
- insertRelatedId<T extends Entity>(object: T, property: keyof T, id: Identifier): Promise<void>;
17
- read<T extends object>(type: Type<T>, id: Identifier): Promise<Entity<T>>;
18
- readCollection<T extends object, PT extends object>(object: Entity<T>, property: keyof T, type?: Type<PT>): Promise<Awaited<PT & {
19
- id: Identifier;
20
- }>[]>;
21
- readCollectionIds<T extends object, PT extends object>(object: Entity<T>, property: keyof T, type?: Type<PT>): Promise<Identifier[]>;
22
- readMultiple<T extends object>(type: Type<T>, ids: Identifier[]): Promise<Entity<T>[]>;
23
- save<T extends object>(object: MayEntity<T>): Promise<Entity<T>>;
24
- search<T extends object>(type: Type<T>, search?: SearchType<T>, options?: Options): Promise<Entity<T>[]>;
25
- update<T extends object>(object: Entity<T>): Promise<Entity<T>>;
26
- }
package/esm/mysql.js DELETED
@@ -1,88 +0,0 @@
1
- import { Mysql as M } from '@itrocks/mysql';
2
- import { ReflectProperty } from '@itrocks/reflect';
3
- import { Contextual } from './contextual-connection';
4
- export class Mysql extends M {
5
- async connect() {
6
- return Contextual.prototype.applyTo(await super.connect());
7
- }
8
- async deleteId(type, id, property = 'id') {
9
- const contexts = (this.connection ?? await this.connect()).contexts;
10
- contexts.push(type);
11
- const result = await super.deleteId(type, id, property);
12
- contexts.pop();
13
- return result;
14
- }
15
- async deleteRelatedId(object, property, id) {
16
- const contexts = (this.connection ?? await this.connect()).contexts;
17
- contexts.push(object);
18
- const result = await super.deleteRelatedId(object, property, id);
19
- contexts.pop();
20
- return result;
21
- }
22
- async insert(object) {
23
- const contexts = (this.connection ?? await this.connect()).contexts;
24
- contexts.push(object);
25
- const result = await super.insert(object);
26
- contexts.pop();
27
- return result;
28
- }
29
- async insertRelatedId(object, property, id) {
30
- const contexts = (this.connection ?? await this.connect()).contexts;
31
- contexts.push(object);
32
- const result = await super.insertRelatedId(object, property, id);
33
- contexts.pop();
34
- return result;
35
- }
36
- async read(type, id) {
37
- const contexts = (this.connection ?? await this.connect()).contexts;
38
- contexts.push(type);
39
- const result = await super.read(type, id);
40
- contexts.pop();
41
- return result;
42
- }
43
- async readCollection(object, property, type) {
44
- type ??= new ReflectProperty(object, property).collectionType.elementType.type;
45
- const contexts = (this.connection ?? await this.connect()).contexts;
46
- contexts.push([object, type]);
47
- const result = await super.readCollection(object, property, type);
48
- contexts.pop();
49
- return result;
50
- }
51
- async readCollectionIds(object, property, type) {
52
- type ??= new ReflectProperty(object, property).collectionType.elementType.type;
53
- const contexts = (this.connection ?? await this.connect()).contexts;
54
- contexts.push([object, type]);
55
- const result = await super.readCollectionIds(object, property, type);
56
- contexts.pop();
57
- return result;
58
- }
59
- async readMultiple(type, ids) {
60
- const contexts = (this.connection ?? await this.connect()).contexts;
61
- contexts.push(type);
62
- const result = await super.readMultiple(type, ids);
63
- contexts.pop();
64
- return result;
65
- }
66
- async save(object) {
67
- const contexts = (this.connection ?? await this.connect()).contexts;
68
- contexts.push(object);
69
- const result = await super.save(object);
70
- contexts.pop();
71
- return result;
72
- }
73
- async search(type, search = {}, options) {
74
- const contexts = (this.connection ?? await this.connect()).contexts;
75
- contexts.push(type);
76
- const result = await super.search(type, search, options);
77
- contexts.pop();
78
- return result;
79
- }
80
- async update(object) {
81
- const contexts = (this.connection ?? await this.connect()).contexts;
82
- contexts.push(object);
83
- const result = await super.update(object);
84
- contexts.pop();
85
- return result;
86
- }
87
- }
88
- //# sourceMappingURL=mysql.js.map