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 +2 -2
- package/source/Meadow-Connection-MSSQL.js +126 -139
- package/source/Meadow-Schema-MSSQL.js +1027 -0
- package/test/MSSQL_tests.js +840 -1
- package/test/docker-init/02-chinook-schema.sql +203 -0
- package/test/docker-init/init-db.js +44 -37
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "meadow-connection-mssql",
|
|
3
|
-
"version": "1.0.
|
|
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.
|
|
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
|
-
|
|
35
|
+
// Resolve MSSQL connection settings from options or fable settings
|
|
36
|
+
if (typeof(this.options.MSSQL) == 'object')
|
|
34
37
|
{
|
|
35
|
-
|
|
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.
|
|
41
|
+
this.options.MSSQL.server = this.options.MSSQL.Server;
|
|
46
42
|
}
|
|
47
|
-
if (this.
|
|
43
|
+
if (!this.options.MSSQL.hasOwnProperty('port') && this.options.MSSQL.hasOwnProperty('Port'))
|
|
48
44
|
{
|
|
49
|
-
this.options.
|
|
45
|
+
this.options.MSSQL.port = this.options.MSSQL.Port;
|
|
50
46
|
}
|
|
51
|
-
if (this.
|
|
47
|
+
if (!this.options.MSSQL.hasOwnProperty('user') && this.options.MSSQL.hasOwnProperty('User'))
|
|
52
48
|
{
|
|
53
|
-
this.options.
|
|
49
|
+
this.options.MSSQL.user = this.options.MSSQL.User;
|
|
54
50
|
}
|
|
55
|
-
if (this.
|
|
51
|
+
if (!this.options.MSSQL.hasOwnProperty('password') && this.options.MSSQL.hasOwnProperty('Password'))
|
|
56
52
|
{
|
|
57
|
-
this.options.
|
|
53
|
+
this.options.MSSQL.password = this.options.MSSQL.Password;
|
|
58
54
|
}
|
|
59
|
-
if (this.
|
|
55
|
+
if (!this.options.MSSQL.hasOwnProperty('database') && this.options.MSSQL.hasOwnProperty('Database'))
|
|
60
56
|
{
|
|
61
|
-
this.options.
|
|
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
|
-
|
|
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.
|
|
104
|
+
return this._SchemaProvider.generateCreateTableStatement(pMeadowTableSchema);
|
|
105
|
+
}
|
|
77
106
|
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
{
|
|
83
|
-
let tmpColumn = pMeadowTableSchema.Columns[j];
|
|
107
|
+
createTables(pMeadowSchema, fCallback)
|
|
108
|
+
{
|
|
109
|
+
return this._SchemaProvider.createTables(pMeadowSchema, fCallback);
|
|
110
|
+
}
|
|
84
111
|
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
}
|
|
112
|
+
createTable(pMeadowTableSchema, fCallback)
|
|
113
|
+
{
|
|
114
|
+
return this._SchemaProvider.createTable(pMeadowTableSchema, fCallback);
|
|
115
|
+
}
|
|
90
116
|
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
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
|
-
|
|
122
|
+
generateCreateIndexScript(pMeadowTableSchema)
|
|
123
|
+
{
|
|
124
|
+
return this._SchemaProvider.generateCreateIndexScript(pMeadowTableSchema);
|
|
125
|
+
}
|
|
143
126
|
|
|
144
|
-
|
|
127
|
+
generateCreateIndexStatements(pMeadowTableSchema)
|
|
128
|
+
{
|
|
129
|
+
return this._SchemaProvider.generateCreateIndexStatements(pMeadowTableSchema);
|
|
145
130
|
}
|
|
146
131
|
|
|
147
|
-
|
|
132
|
+
createIndex(pIndexStatement, fCallback)
|
|
148
133
|
{
|
|
149
|
-
|
|
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
|
-
|
|
137
|
+
createIndices(pMeadowTableSchema, fCallback)
|
|
167
138
|
{
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
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:
|
|
216
|
-
user:
|
|
217
|
-
password:
|
|
218
|
-
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:
|
|
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(
|