meadow-connection-mssql 1.0.13 → 1.0.15

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-mssql",
3
- "version": "1.0.13",
3
+ "version": "1.0.15",
4
4
  "description": "Meadow MSSQL Plugin",
5
5
  "main": "source/Meadow-Connection-MSSQL.js",
6
6
  "scripts": {
@@ -48,7 +48,7 @@
48
48
  },
49
49
  "homepage": "https://github.com/stevenvelozo/meadow-connection-mssql",
50
50
  "devDependencies": {
51
- "fable": "^3.1.55",
51
+ "fable": "^3.1.63",
52
52
  "quackage": "^1.0.59"
53
53
  },
54
54
  "dependencies": {
@@ -6,6 +6,8 @@ const libFableServiceProviderBase = require('fable-serviceproviderbase');
6
6
 
7
7
  const libMSSQL = require('mssql');
8
8
 
9
+ const libMeadowSchemaMSSQL = require('./Meadow-Schema-MSSQL.js');
10
+
9
11
  /*
10
12
  Das alt muster:
11
13
 
@@ -30,170 +32,153 @@ class MeadowConnectionMSSQL extends libFableServiceProviderBase
30
32
 
31
33
  this.connected = false;
32
34
 
33
- if (this.fable.settings.hasOwnProperty('MSSQL'))
35
+ // Resolve MSSQL connection settings from options or fable settings
36
+ if (typeof(this.options.MSSQL) == 'object')
34
37
  {
35
- if (this.fable.settings.MSSQL.hasOwnProperty('server'))
36
- {
37
- this.options.server = this.fable.settings.MSSQL.server;
38
- }
39
- if (this.fable.settings.MSSQL.hasOwnProperty('port'))
40
- {
41
- this.options.port = this.fable.settings.MSSQL.port;
42
- }
43
- else
38
+ // Options were passed in with an MSSQL sub-object; coerce PascalCase to lowercase
39
+ if (!this.options.MSSQL.hasOwnProperty('server') && this.options.MSSQL.hasOwnProperty('Server'))
44
40
  {
45
- this.options.port = 1433;
41
+ this.options.MSSQL.server = this.options.MSSQL.Server;
46
42
  }
47
- if (this.fable.settings.MSSQL.hasOwnProperty('user'))
43
+ if (!this.options.MSSQL.hasOwnProperty('port') && this.options.MSSQL.hasOwnProperty('Port'))
48
44
  {
49
- this.options.user = this.fable.settings.MSSQL.user;
45
+ this.options.MSSQL.port = this.options.MSSQL.Port;
50
46
  }
51
- if (this.fable.settings.MSSQL.hasOwnProperty('password'))
47
+ if (!this.options.MSSQL.hasOwnProperty('user') && this.options.MSSQL.hasOwnProperty('User'))
52
48
  {
53
- this.options.password = this.fable.settings.MSSQL.password;
49
+ this.options.MSSQL.user = this.options.MSSQL.User;
54
50
  }
55
- if (this.fable.settings.MSSQL.hasOwnProperty('database'))
51
+ if (!this.options.MSSQL.hasOwnProperty('password') && this.options.MSSQL.hasOwnProperty('Password'))
56
52
  {
57
- this.options.database = this.fable.settings.MSSQL.database;
53
+ this.options.MSSQL.password = this.options.MSSQL.Password;
58
54
  }
59
- if (this.fable.settings.MSSQL.hasOwnProperty('MeadowConnectionMSSQLAutoConnect'))
55
+ if (!this.options.MSSQL.hasOwnProperty('database') && this.options.MSSQL.hasOwnProperty('Database'))
60
56
  {
61
- this.options.MeadowConnectionMSSQLAutoConnect = this.fable.settings.MSSQL.MeadowConnectionMSSQLAutoConnect;
57
+ this.options.MSSQL.database = this.options.MSSQL.Database;
62
58
  }
63
59
  }
60
+ else if (typeof(this.options.server) === 'string')
61
+ {
62
+ // Options were passed in flat (already has server, user, etc.)
63
+ this.options.MSSQL = (
64
+ {
65
+ server: this.options.server,
66
+ port: this.options.port,
67
+ user: this.options.user,
68
+ password: this.options.password,
69
+ database: this.options.database,
70
+ ConnectionPoolLimit: this.options.ConnectionPoolLimit
71
+ });
72
+ }
73
+ else if (typeof(this.fable.settings.MSSQL) == 'object')
74
+ {
75
+ // Fall back to fable settings
76
+ let tmpSettings = this.fable.settings.MSSQL;
77
+ this.options.MSSQL = (
78
+ {
79
+ server: tmpSettings.server || tmpSettings.Server,
80
+ port: tmpSettings.port || tmpSettings.Port,
81
+ user: tmpSettings.user || tmpSettings.User,
82
+ password: tmpSettings.password || tmpSettings.Password,
83
+ database: tmpSettings.database || tmpSettings.Database,
84
+ ConnectionPoolLimit: tmpSettings.ConnectionPoolLimit
85
+ });
86
+ }
87
+
88
+ // Schema provider handles DDL operations (create, drop, index, etc.)
89
+ this._SchemaProvider = new libMeadowSchemaMSSQL(this.fable, this.options, `${this.Hash}-Schema`);
90
+ }
91
+
92
+ get schemaProvider()
93
+ {
94
+ return this._SchemaProvider;
64
95
  }
65
96
 
66
97
  generateDropTableStatement(pTableName)
67
98
  {
68
- let tmpDropTableStatement = `IF OBJECT_ID('dbo.[${pTableName}]', 'U') IS NOT NULL\n`;
69
- tmpDropTableStatement += ` DROP TABLE dbo.[${pTableName}];\n`;
70
- tmpDropTableStatement += `GO`;
71
- return tmpDropTableStatement;
99
+ return this._SchemaProvider.generateDropTableStatement(pTableName);
72
100
  }
73
101
 
74
102
  generateCreateTableStatement(pMeadowTableSchema)
75
103
  {
76
- this.log.info(`--> Building the table create string for ${pMeadowTableSchema} ...`);
104
+ return this._SchemaProvider.generateCreateTableStatement(pMeadowTableSchema);
105
+ }
77
106
 
78
- let tmpPrimaryKey = false;
79
- let tmpCreateTableStatement = `-- [ ${pMeadowTableSchema.TableName} ]`;
80
- tmpCreateTableStatement += `\nCREATE TABLE [dbo].[${pMeadowTableSchema.TableName}]\n (`;
81
- for (let j = 0; j < pMeadowTableSchema.Columns.length; j++)
82
- {
83
- let tmpColumn = pMeadowTableSchema.Columns[j];
107
+ createTables(pMeadowSchema, fCallback)
108
+ {
109
+ return this._SchemaProvider.createTables(pMeadowSchema, fCallback);
110
+ }
84
111
 
85
- // If we aren't the first column, append a comma.
86
- if (j > 0)
87
- {
88
- tmpCreateTableStatement += `,`;
89
- }
112
+ createTable(pMeadowTableSchema, fCallback)
113
+ {
114
+ return this._SchemaProvider.createTable(pMeadowTableSchema, fCallback);
115
+ }
90
116
 
91
- tmpCreateTableStatement += `\n`;
92
- // Dump out each column......
93
- switch (tmpColumn.DataType)
94
- {
95
- case 'ID':
96
- // if (this.options.AllowIdentityInsert)
97
- // {
98
- // tmpCreateTableStatement += ` [${tmpColumn.Column}] INT NOT NULL PRIMARY KEY`;
99
- // }
100
- // else
101
- // {
102
- // There is debate on whether IDENTITY(1,1) is better or not.
103
- tmpCreateTableStatement += ` [${tmpColumn.Column}] INT NOT NULL IDENTITY PRIMARY KEY`;
104
- //}
105
- tmpPrimaryKey = tmpColumn.Column;
106
- break;
107
- case 'GUID':
108
- tmpCreateTableStatement += ` [${tmpColumn.Column}] VARCHAR(254) DEFAULT '00000000-0000-0000-0000-000000000000'`;
109
- break;
110
- case 'ForeignKey':
111
- tmpCreateTableStatement += ` [${tmpColumn.Column}] INT UNSIGNED NOT NULL DEFAULT 0`;
112
- tmpPrimaryKey = tmpColumn.Column;
113
- break;
114
- case 'Numeric':
115
- tmpCreateTableStatement += ` [${tmpColumn.Column}] INT NOT NULL DEFAULT 0`;
116
- break;
117
- case 'Decimal':
118
- tmpCreateTableStatement += ` [${tmpColumn.Column}] DECIMAL(${tmpColumn.Size})`;
119
- break;
120
- case 'String':
121
- tmpCreateTableStatement += ` [${tmpColumn.Column}] VARCHAR(${tmpColumn.Size}) DEFAULT ''`;
122
- break;
123
- case 'Text':
124
- tmpCreateTableStatement += ` [${tmpColumn.Column}] TEXT`;
125
- break;
126
- case 'DateTime':
127
- tmpCreateTableStatement += ` [${tmpColumn.Column}] DATETIME`;
128
- break;
129
- case 'Boolean':
130
- tmpCreateTableStatement += ` [${tmpColumn.Column}] TINYINT DEFAULT 0`;
131
- break;
132
- default:
133
- break;
134
- }
135
- }
136
- if (tmpPrimaryKey)
137
- {
138
- // tmpCreateTableStatement += `,\n\n PRIMARY KEY (${tmpPrimaryKey$})`;
139
- }
140
- tmpCreateTableStatement += `\n );`;
117
+ getIndexDefinitionsFromSchema(pMeadowTableSchema)
118
+ {
119
+ return this._SchemaProvider.getIndexDefinitionsFromSchema(pMeadowTableSchema);
120
+ }
141
121
 
142
- //this.log.info(`Generated Create Table Statement: ${tmpCreateTableStatement}`);
122
+ generateCreateIndexScript(pMeadowTableSchema)
123
+ {
124
+ return this._SchemaProvider.generateCreateIndexScript(pMeadowTableSchema);
125
+ }
143
126
 
144
- return tmpCreateTableStatement;
127
+ generateCreateIndexStatements(pMeadowTableSchema)
128
+ {
129
+ return this._SchemaProvider.generateCreateIndexStatements(pMeadowTableSchema);
145
130
  }
146
131
 
147
- createTables(pMeadowSchema, fCallback)
132
+ createIndex(pIndexStatement, fCallback)
148
133
  {
149
- // Now create the Book databases if they don't exist.
150
- this.fable.Utility.eachLimit(pMeadowSchema.Tables, 1,
151
- (pTable, fCreateComplete) =>
152
- {
153
- return this.createTable(pTable, fCreateComplete)
154
- },
155
- (pCreateError) =>
156
- {
157
- if (pCreateError)
158
- {
159
- this.log.error(`Meadow-MSSQL Error creating tables from Schema: ${pCreateError}`,pCreateError);
160
- }
161
- this.log.info('Done creating tables!');
162
- return fCallback(pCreateError);
163
- });
134
+ return this._SchemaProvider.createIndex(pIndexStatement, fCallback);
164
135
  }
165
136
 
166
- createTable(pMeadowTableSchema, fCallback)
137
+ createIndices(pMeadowTableSchema, fCallback)
167
138
  {
168
- let tmpCreateTableStatement = this.generateCreateTableStatement(pMeadowTableSchema);
169
- this._ConnectionPool.query(tmpCreateTableStatement)
170
- .then((pResult) =>
171
- {
172
- this.log.info(`Meadow-MSSQL CREATE TABLE ${pMeadowTableSchema.TableName} Success`);
173
- this.log.warn(`Meadow-MSSQL Create Table Statement: ${tmpCreateTableStatement}`)
174
- return fCallback();
175
- })
176
- .catch((pError) =>
177
- {
178
- if (pError.hasOwnProperty('originalError')
179
- // TODO: This check may be extraneous; not familiar enough with the mssql node driver yet
180
- && (pError.originalError.hasOwnProperty('info'))
181
- // TODO: Validate that there isn't a better way to find this (pError.code isn't explicit enough)
182
- && (pError.originalError.info.message.indexOf("There is already an object named") == 0)
183
- && (pError.originalError.info.message.indexOf('in the database.') > 0))
184
- {
185
- // The table already existed; log a warning but keep on keeping on.
186
- //this.log.warn(`Meadow-MSSQL CREATE TABLE ${pMeadowTableSchema.TableName} executed but table already existed.`);
187
- //this.log.warn(`Meadow-MSSQL Create Table Statement: ${tmpCreateTableStatement}`)
188
- return fCallback();
189
- }
190
- else
191
- {
192
- this.log.error(`Meadow-MSSQL CREATE TABLE ${pMeadowTableSchema.TableName} failed!`, pError);
193
- //this.log.warn(`Meadow-MSSQL Create Table Statement: ${tmpCreateTableStatement}`)
194
- return fCallback(pError);
195
- }
196
- });
139
+ return this._SchemaProvider.createIndices(pMeadowTableSchema, fCallback);
140
+ }
141
+
142
+ createAllIndices(pMeadowSchema, fCallback)
143
+ {
144
+ return this._SchemaProvider.createAllIndices(pMeadowSchema, fCallback);
145
+ }
146
+
147
+ // Database Introspection delegation
148
+
149
+ listTables(fCallback)
150
+ {
151
+ return this._SchemaProvider.listTables(fCallback);
152
+ }
153
+
154
+ introspectTableColumns(pTableName, fCallback)
155
+ {
156
+ return this._SchemaProvider.introspectTableColumns(pTableName, fCallback);
157
+ }
158
+
159
+ introspectTableIndices(pTableName, fCallback)
160
+ {
161
+ return this._SchemaProvider.introspectTableIndices(pTableName, fCallback);
162
+ }
163
+
164
+ introspectTableForeignKeys(pTableName, fCallback)
165
+ {
166
+ return this._SchemaProvider.introspectTableForeignKeys(pTableName, fCallback);
167
+ }
168
+
169
+ introspectTableSchema(pTableName, fCallback)
170
+ {
171
+ return this._SchemaProvider.introspectTableSchema(pTableName, fCallback);
172
+ }
173
+
174
+ introspectDatabaseSchema(fCallback)
175
+ {
176
+ return this._SchemaProvider.introspectDatabaseSchema(fCallback);
177
+ }
178
+
179
+ generateMeadowPackageFromTable(pTableName, fCallback)
180
+ {
181
+ return this._SchemaProvider.generateMeadowPackageFromTable(pTableName, fCallback);
197
182
  }
198
183
 
199
184
  connect()
@@ -210,18 +195,19 @@ class MeadowConnectionMSSQL extends libFableServiceProviderBase
210
195
  this.log.error(`Meadow MSSQL connect() called without a callback; this could lead to connection race conditions.`);
211
196
  tmpCallback = () => { };
212
197
  }
198
+ let tmpMSSQLSettings = this.options.MSSQL || {};
213
199
  let tmpConnectionSettings = (
214
200
  {
215
- server: this.options.server,
216
- user: this.options.user,
217
- password: this.options.password,
218
- database: this.options.database,
201
+ server: tmpMSSQLSettings.server,
202
+ user: tmpMSSQLSettings.user,
203
+ password: tmpMSSQLSettings.password,
204
+ database: tmpMSSQLSettings.database,
219
205
  requestTimeout: 80000,
220
206
  connectionTimeout: 80000,
221
- port: this.options.port,
207
+ port: tmpMSSQLSettings.port,
222
208
  pool:
223
209
  {
224
- max: 10,
210
+ max: tmpMSSQLSettings.ConnectionPoolLimit || 10,
225
211
  min: 0,
226
212
  idleTimeoutMillis: 30000
227
213
  },
@@ -249,6 +235,7 @@ class MeadowConnectionMSSQL extends libFableServiceProviderBase
249
235
  this.log.info(`Meadow-Connection-MSSQL successfully connected to MSSQL at [${tmpConnectionSettings.server} : ${tmpConnectionSettings.port}] as ${tmpConnectionSettings.user} for database ${tmpConnectionSettings.database} at a connection limit of ${tmpConnectionSettings.pool.max}.`);
250
236
  this._ConnectionPool = pConnectionPool;
251
237
  this.connected = true;
238
+ this._SchemaProvider.setConnectionPool(this._ConnectionPool);
252
239
  return tmpCallback(null, this._ConnectionPool)
253
240
  })
254
241
  .catch(