@itrocks/mysql-maintainer 0.1.2 → 0.2.0

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