meadow-connection-mysql 1.0.10 → 1.0.13

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "meadow-connection-mysql",
3
- "version": "1.0.10",
3
+ "version": "1.0.13",
4
4
  "description": "Meadow MySQL Plugin",
5
5
  "main": "source/Meadow-Connection-MySQL.js",
6
6
  "scripts": {
@@ -6,6 +6,8 @@ const libFableServiceProviderBase = require('fable-serviceproviderbase');
6
6
 
7
7
  const libMySQL = require('mysql2');
8
8
 
9
+ const libMeadowSchemaMySQL = require('./Meadow-Schema-MySQL.js');
10
+
9
11
  /*
10
12
  Das alt muster:
11
13
 
@@ -88,149 +90,105 @@ class MeadowConnectionMySQL extends libFableServiceProviderBase
88
90
  this._ConnectionPool = false;
89
91
  this.connected = false;
90
92
 
93
+ // Schema provider handles DDL operations (create, drop, index, etc.)
94
+ this._SchemaProvider = new libMeadowSchemaMySQL(this.fable, this.options, `${this.Hash}-Schema`);
95
+
91
96
  if (this.options.MeadowConnectionMySQLAutoConnect)
92
97
  {
93
98
  this.connect();
94
99
  }
95
100
  }
96
101
 
102
+ get schemaProvider()
103
+ {
104
+ return this._SchemaProvider;
105
+ }
106
+
97
107
  generateDropTableStatement(pTableName)
98
108
  {
99
- return `DROP TABLE IF EXISTS ${pTableName};`;
109
+ return this._SchemaProvider.generateDropTableStatement(pTableName);
100
110
  }
101
111
 
102
112
  generateCreateTableStatement(pMeadowTableSchema)
103
113
  {
104
- this.log.info(`--> Building the table create string for ${pMeadowTableSchema} ...`);
114
+ return this._SchemaProvider.generateCreateTableStatement(pMeadowTableSchema);
115
+ }
105
116
 
106
- let tmpPrimaryKey = false;
107
- let tmpCreateTableStatement = `-- [ ${pMeadowTableSchema.TableName} ]`;
108
-
109
- tmpCreateTableStatement += `\nCREATE TABLE IF NOT EXISTS \n ${pMeadowTableSchema.TableName}\n (`;
110
- for (let j = 0; j < pMeadowTableSchema.Columns.length; j++)
111
- {
112
- let tmpColumn = pMeadowTableSchema.Columns[j];
117
+ createTables(pMeadowSchema, fCallback)
118
+ {
119
+ return this._SchemaProvider.createTables(pMeadowSchema, fCallback);
120
+ }
113
121
 
114
- // If we aren't the first column, append a comma.
115
- if (j > 0)
116
- {
117
- tmpCreateTableStatement += `,`;
118
- }
122
+ createTable(pMeadowTableSchema, fCallback)
123
+ {
124
+ return this._SchemaProvider.createTable(pMeadowTableSchema, fCallback);
125
+ }
119
126
 
120
- tmpCreateTableStatement += `\n`;
121
- // Dump out each column......
122
- switch (tmpColumn.DataType)
123
- {
124
- case 'ID':
125
- // if (this.options.AllowIdentityInsert)
126
- // {
127
- // tmpCreateTableStatement += ` [${tmpColumn.Column}] INT NOT NULL PRIMARY KEY`;
128
- // }
129
- // else
130
- // {
131
- // There is debate on whether IDENTITY(1,1) is better or not.
132
- tmpCreateTableStatement += ` ${tmpColumn.Column} INT UNSIGNED NOT NULL AUTO_INCREMENT`;
133
- //}
134
- tmpPrimaryKey = tmpColumn.Column;
135
- break;
136
- case 'GUID':
137
- let tmpSize = tmpColumn.hasOwnProperty('Size') ? tmpColumn.Size : 36;
138
- if (isNaN(tmpSize))
139
- {
140
- // Use the old default if Size is improper
141
- tmpSize = 36;
142
- }
143
- tmpCreateTableStatement += ` ${tmpColumn.Column} CHAR(${tmpSize}) DEFAULT '0xDe'`;
144
- break;
145
- case 'ForeignKey':
146
- tmpCreateTableStatement += ` ${tmpColumn.Column} INT UNSIGNED NOT NULL DEFAULT '0'`;
147
- tmpPrimaryKey = tmpColumn.Column;
148
- break;
149
- case 'Numeric':
150
- tmpCreateTableStatement += ` ${tmpColumn.Column} INT NOT NULL DEFAULT '0'`;
151
- break;
152
- case 'Decimal':
153
- tmpCreateTableStatement += ` ${tmpColumn.Column} DECIMAL(${tmpColumn.Size})`;
154
- break;
155
- case 'String':
156
- tmpCreateTableStatement += ` ${tmpColumn.Column} CHAR(${tmpColumn.Size}) NOT NULL DEFAULT ''`;
157
- break;
158
- case 'Text':
159
- tmpCreateTableStatement += ` ${tmpColumn.Column} TEXT`;
160
- break;
161
- case 'DateTime':
162
- tmpCreateTableStatement += ` ${tmpColumn.Column} DATETIME`;
163
- break;
164
- case 'Boolean':
165
- tmpCreateTableStatement += ` ${tmpColumn.Column} TINYINT NOT NULL DEFAULT '0'`;
166
- break;
167
- default:
168
- break;
169
- }
170
- }
171
- if (tmpPrimaryKey)
172
- {
173
- tmpCreateTableStatement += `,\n\n PRIMARY KEY (${tmpPrimaryKey})`;
174
- }
175
- tmpCreateTableStatement += `\n ) DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;`;
127
+ getIndexDefinitionsFromSchema(pMeadowTableSchema)
128
+ {
129
+ return this._SchemaProvider.getIndexDefinitionsFromSchema(pMeadowTableSchema);
130
+ }
176
131
 
177
- //this.log.info(`Generated Create Table Statement: ${tmpCreateTableStatement}`);
132
+ generateCreateIndexScript(pMeadowTableSchema)
133
+ {
134
+ return this._SchemaProvider.generateCreateIndexScript(pMeadowTableSchema);
135
+ }
178
136
 
179
- return tmpCreateTableStatement;
137
+ generateCreateIndexStatements(pMeadowTableSchema)
138
+ {
139
+ return this._SchemaProvider.generateCreateIndexStatements(pMeadowTableSchema);
180
140
  }
181
141
 
182
- createTables(pMeadowSchema, fCallback)
142
+ createIndex(pIndexStatement, fCallback)
183
143
  {
184
- // Now create the Book databases if they don't exist.
185
- this.fable.Utility.eachLimit(pMeadowSchema.Tables, 1,
186
- (pTable, fCreateComplete) =>
187
- {
188
- return this.createTable(pTable, fCreateComplete)
189
- },
190
- (pCreateError) =>
191
- {
192
- if (pCreateError)
193
- {
194
- this.log.error(`Meadow-MySQL Error creating tables from Schema: ${pCreateError}`,pCreateError);
195
- }
196
- this.log.info('Done creating tables!');
197
- return fCallback(pCreateError);
198
- });
144
+ return this._SchemaProvider.createIndex(pIndexStatement, fCallback);
199
145
  }
200
146
 
201
- createTable(pMeadowTableSchema, fCallback)
147
+ createIndices(pMeadowTableSchema, fCallback)
202
148
  {
203
- let tmpCreateTableStatement = this.generateCreateTableStatement(pMeadowTableSchema);
204
- this._ConnectionPool.query(tmpCreateTableStatement,
205
- function(pError, pRows, pFields)
206
- {
207
- if (pError)
208
- {
209
- if (pError.hasOwnProperty('originalError')
210
- && (pError.originalError.hasOwnProperty('info'))
211
- // TODO: Validate that there isn't a better way to find this (pError.code isn't explicit enough)
212
- && (pError.originalError.info.message.indexOf("There is already an object named") == 0)
213
- && (pError.originalError.info.message.indexOf('in the database.') > 0))
214
- {
215
- // The table already existed; log a warning but keep on keeping on.
216
- this.log.warn(`Meadow-MySQL CREATE TABLE ${pMeadowTableSchema.TableName} executed but table already existed.`);
217
- //this.log.warn(`Meadow-MySQL Create Table Statement: ${tmpCreateTableStatement}`)
218
- return fCallback();
219
- }
220
- else
221
- {
222
- this.log.error(`Meadow-MySQL CREATE TABLE ${pMeadowTableSchema.TableName} failed!`, pError);
223
- //this.log.warn(`Meadow-MySQL Create Table Statement: ${tmpCreateTableStatement}`)
224
- return fCallback(pError);
225
- }
226
- }
227
- else
228
- {
229
- this.log.info(`Meadow-MySQL CREATE TABLE ${pMeadowTableSchema.TableName} executed successfully.`);
230
- //this.log.info(`Meadow-MySQL Create Table Statement: ${tmpCreateTableStatement}`)
231
- return fCallback();
232
- }
233
- }.bind(this));
149
+ return this._SchemaProvider.createIndices(pMeadowTableSchema, fCallback);
150
+ }
151
+
152
+ createAllIndices(pMeadowSchema, fCallback)
153
+ {
154
+ return this._SchemaProvider.createAllIndices(pMeadowSchema, fCallback);
155
+ }
156
+
157
+ // Database Introspection delegation
158
+
159
+ listTables(fCallback)
160
+ {
161
+ return this._SchemaProvider.listTables(fCallback);
162
+ }
163
+
164
+ introspectTableColumns(pTableName, fCallback)
165
+ {
166
+ return this._SchemaProvider.introspectTableColumns(pTableName, fCallback);
167
+ }
168
+
169
+ introspectTableIndices(pTableName, fCallback)
170
+ {
171
+ return this._SchemaProvider.introspectTableIndices(pTableName, fCallback);
172
+ }
173
+
174
+ introspectTableForeignKeys(pTableName, fCallback)
175
+ {
176
+ return this._SchemaProvider.introspectTableForeignKeys(pTableName, fCallback);
177
+ }
178
+
179
+ introspectTableSchema(pTableName, fCallback)
180
+ {
181
+ return this._SchemaProvider.introspectTableSchema(pTableName, fCallback);
182
+ }
183
+
184
+ introspectDatabaseSchema(fCallback)
185
+ {
186
+ return this._SchemaProvider.introspectDatabaseSchema(fCallback);
187
+ }
188
+
189
+ generateMeadowPackageFromTable(pTableName, fCallback)
190
+ {
191
+ return this._SchemaProvider.generateMeadowPackageFromTable(pTableName, fCallback);
234
192
  }
235
193
 
236
194
  connect()
@@ -257,6 +215,7 @@ class MeadowConnectionMySQL extends libFableServiceProviderBase
257
215
  {
258
216
  this.fable.log.info(`Meadow-Connection-MySQL connecting to [${this.options.MySQL.host} : ${this.options.MySQL.port}] as ${this.options.MySQL.user} for database ${this.options.MySQL.database} at a connection limit of ${this.options.MySQL.connectionLimit}`);
259
217
  this._ConnectionPool = libMySQL.createPool(tmpConnectionSettings);
218
+ this._SchemaProvider.setConnectionPool(this._ConnectionPool);
260
219
  this.connected = true;
261
220
  }
262
221
  }