meadow-connection-mssql 1.0.13 → 1.0.14

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.14",
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,100 @@ class MeadowConnectionMSSQL extends libFableServiceProviderBase
30
32
 
31
33
  this.connected = false;
32
34
 
33
- if (this.fable.settings.hasOwnProperty('MSSQL'))
34
- {
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
44
- {
45
- this.options.port = 1433;
46
- }
47
- if (this.fable.settings.MSSQL.hasOwnProperty('user'))
48
- {
49
- this.options.user = this.fable.settings.MSSQL.user;
50
- }
51
- if (this.fable.settings.MSSQL.hasOwnProperty('password'))
52
- {
53
- this.options.password = this.fable.settings.MSSQL.password;
54
- }
55
- if (this.fable.settings.MSSQL.hasOwnProperty('database'))
56
- {
57
- this.options.database = this.fable.settings.MSSQL.database;
58
- }
59
- if (this.fable.settings.MSSQL.hasOwnProperty('MeadowConnectionMSSQLAutoConnect'))
60
- {
61
- this.options.MeadowConnectionMSSQLAutoConnect = this.fable.settings.MSSQL.MeadowConnectionMSSQLAutoConnect;
62
- }
63
- }
35
+ // Schema provider handles DDL operations (create, drop, index, etc.)
36
+ this._SchemaProvider = new libMeadowSchemaMSSQL(this.fable, this.options, `${this.Hash}-Schema`);
37
+ }
38
+
39
+ get schemaProvider()
40
+ {
41
+ return this._SchemaProvider;
64
42
  }
65
43
 
66
44
  generateDropTableStatement(pTableName)
67
45
  {
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;
46
+ return this._SchemaProvider.generateDropTableStatement(pTableName);
72
47
  }
73
48
 
74
49
  generateCreateTableStatement(pMeadowTableSchema)
75
50
  {
76
- this.log.info(`--> Building the table create string for ${pMeadowTableSchema} ...`);
51
+ return this._SchemaProvider.generateCreateTableStatement(pMeadowTableSchema);
52
+ }
77
53
 
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];
54
+ createTables(pMeadowSchema, fCallback)
55
+ {
56
+ return this._SchemaProvider.createTables(pMeadowSchema, fCallback);
57
+ }
84
58
 
85
- // If we aren't the first column, append a comma.
86
- if (j > 0)
87
- {
88
- tmpCreateTableStatement += `,`;
89
- }
59
+ createTable(pMeadowTableSchema, fCallback)
60
+ {
61
+ return this._SchemaProvider.createTable(pMeadowTableSchema, fCallback);
62
+ }
90
63
 
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 );`;
64
+ getIndexDefinitionsFromSchema(pMeadowTableSchema)
65
+ {
66
+ return this._SchemaProvider.getIndexDefinitionsFromSchema(pMeadowTableSchema);
67
+ }
141
68
 
142
- //this.log.info(`Generated Create Table Statement: ${tmpCreateTableStatement}`);
69
+ generateCreateIndexScript(pMeadowTableSchema)
70
+ {
71
+ return this._SchemaProvider.generateCreateIndexScript(pMeadowTableSchema);
72
+ }
143
73
 
144
- return tmpCreateTableStatement;
74
+ generateCreateIndexStatements(pMeadowTableSchema)
75
+ {
76
+ return this._SchemaProvider.generateCreateIndexStatements(pMeadowTableSchema);
145
77
  }
146
78
 
147
- createTables(pMeadowSchema, fCallback)
79
+ createIndex(pIndexStatement, fCallback)
148
80
  {
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
- });
81
+ return this._SchemaProvider.createIndex(pIndexStatement, fCallback);
164
82
  }
165
83
 
166
- createTable(pMeadowTableSchema, fCallback)
84
+ createIndices(pMeadowTableSchema, fCallback)
167
85
  {
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
- });
86
+ return this._SchemaProvider.createIndices(pMeadowTableSchema, fCallback);
87
+ }
88
+
89
+ createAllIndices(pMeadowSchema, fCallback)
90
+ {
91
+ return this._SchemaProvider.createAllIndices(pMeadowSchema, fCallback);
92
+ }
93
+
94
+ // Database Introspection delegation
95
+
96
+ listTables(fCallback)
97
+ {
98
+ return this._SchemaProvider.listTables(fCallback);
99
+ }
100
+
101
+ introspectTableColumns(pTableName, fCallback)
102
+ {
103
+ return this._SchemaProvider.introspectTableColumns(pTableName, fCallback);
104
+ }
105
+
106
+ introspectTableIndices(pTableName, fCallback)
107
+ {
108
+ return this._SchemaProvider.introspectTableIndices(pTableName, fCallback);
109
+ }
110
+
111
+ introspectTableForeignKeys(pTableName, fCallback)
112
+ {
113
+ return this._SchemaProvider.introspectTableForeignKeys(pTableName, fCallback);
114
+ }
115
+
116
+ introspectTableSchema(pTableName, fCallback)
117
+ {
118
+ return this._SchemaProvider.introspectTableSchema(pTableName, fCallback);
119
+ }
120
+
121
+ introspectDatabaseSchema(fCallback)
122
+ {
123
+ return this._SchemaProvider.introspectDatabaseSchema(fCallback);
124
+ }
125
+
126
+ generateMeadowPackageFromTable(pTableName, fCallback)
127
+ {
128
+ return this._SchemaProvider.generateMeadowPackageFromTable(pTableName, fCallback);
197
129
  }
198
130
 
199
131
  connect()
@@ -249,6 +181,7 @@ class MeadowConnectionMSSQL extends libFableServiceProviderBase
249
181
  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
182
  this._ConnectionPool = pConnectionPool;
251
183
  this.connected = true;
184
+ this._SchemaProvider.setConnectionPool(this._ConnectionPool);
252
185
  return tmpCallback(null, this._ConnectionPool)
253
186
  })
254
187
  .catch(