@memberjunction/codegen-lib 2.13.1 → 2.13.3
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/dist/Angular/angular-codegen.js +1 -1
- package/dist/Config/config.d.ts +1235 -105
- package/dist/Config/config.d.ts.map +1 -1
- package/dist/Config/config.js +250 -32
- package/dist/Config/config.js.map +1 -1
- package/dist/Config/db-connection.d.ts +2 -2
- package/dist/Config/db-connection.d.ts.map +1 -1
- package/dist/Config/db-connection.js +17 -16
- package/dist/Config/db-connection.js.map +1 -1
- package/dist/Database/manage-metadata.d.ts.map +1 -1
- package/dist/Database/manage-metadata.js +110 -106
- package/dist/Database/manage-metadata.js.map +1 -1
- package/dist/Misc/createNewUser.d.ts.map +1 -1
- package/dist/Misc/createNewUser.js +49 -0
- package/dist/Misc/createNewUser.js.map +1 -1
- package/dist/Misc/runCommand.d.ts.map +1 -1
- package/dist/Misc/runCommand.js +4 -3
- package/dist/Misc/runCommand.js.map +1 -1
- package/dist/Misc/status_logging.js +1 -1
- package/dist/Misc/status_logging.js.map +1 -1
- package/dist/runCodeGen.d.ts.map +1 -1
- package/dist/runCodeGen.js +52 -47
- package/dist/runCodeGen.js.map +1 -1
- package/package.json +8 -8
|
@@ -91,9 +91,13 @@ let ManageMetadataBase = ManageMetadataBase_1 = class ManageMetadataBase {
|
|
|
91
91
|
start = new Date();
|
|
92
92
|
(0, status_logging_1.logStatus)(' Recompiling base views...');
|
|
93
93
|
const sqlUtility = global_1.MJGlobal.Instance.ClassFactory.CreateInstance(sql_1.SQLUtilityBase);
|
|
94
|
-
|
|
94
|
+
const adminSchema = (0, config_1.getSettingValue)('mj_core_schema', '__mj');
|
|
95
|
+
const schemasToExclude = (0, config_1.getSettingValue)('recompile_mj_views', true)
|
|
96
|
+
? excludeSchemas.filter((s) => s !== adminSchema)
|
|
97
|
+
: excludeSchemas;
|
|
98
|
+
if (!await sqlUtility.recompileAllBaseViews(ds, schemasToExclude, true)) {
|
|
95
99
|
(0, status_logging_1.logMessage)(' Warning: Non-Fatal error recompiling base views', core_1.SeverityType.Warning, false);
|
|
96
|
-
// many times the former versions of base views will NOT succesfully recompile, so don't consider that scenario to be a
|
|
100
|
+
// many times the former versions of base views will NOT succesfully recompile, so don't consider that scenario to be a
|
|
97
101
|
// failure for this entire function
|
|
98
102
|
}
|
|
99
103
|
(0, status_logging_1.logStatus)(` > Recompiled base views in ${(new Date().getTime() - start.getTime()) / 1000} seconds`);
|
|
@@ -148,16 +152,16 @@ let ManageMetadataBase = ManageMetadataBase_1 = class ManageMetadataBase {
|
|
|
148
152
|
try {
|
|
149
153
|
// for a given virtual entity, we need to loop through the fields that exist in the current SQL definition for the view
|
|
150
154
|
// and add/update/delete the entity fields to match what's in the view
|
|
151
|
-
const sql = ` SELECT
|
|
155
|
+
const sql = ` SELECT
|
|
152
156
|
c.name AS FieldName, t.name AS Type, c.max_length AS Length, c.precision Precision, c.scale Scale, c.is_nullable AllowsNull
|
|
153
|
-
FROM
|
|
157
|
+
FROM
|
|
154
158
|
sys.columns c
|
|
155
|
-
INNER JOIN
|
|
159
|
+
INNER JOIN
|
|
156
160
|
sys.types t ON c.user_type_id = t.user_type_id
|
|
157
|
-
INNER JOIN
|
|
161
|
+
INNER JOIN
|
|
158
162
|
sys.views v ON c.object_id = v.object_id
|
|
159
|
-
WHERE
|
|
160
|
-
v.name = '${virtualEntity.BaseView}' AND
|
|
163
|
+
WHERE
|
|
164
|
+
v.name = '${virtualEntity.BaseView}' AND
|
|
161
165
|
SCHEMA_NAME(v.schema_id) = '${virtualEntity.SchemaName}'
|
|
162
166
|
ORDER BY
|
|
163
167
|
c.column_id`;
|
|
@@ -226,17 +230,17 @@ let ManageMetadataBase = ManageMetadataBase_1 = class ManageMetadataBase {
|
|
|
226
230
|
field.Precision !== veField.Precision ||
|
|
227
231
|
field.Sequence !== fieldSequence) {
|
|
228
232
|
// the field needs to be updated, so update it
|
|
229
|
-
const sqlUpdate = `UPDATE
|
|
230
|
-
[${(0, config_1.mj_core_schema)()}].EntityField
|
|
231
|
-
SET
|
|
233
|
+
const sqlUpdate = `UPDATE
|
|
234
|
+
[${(0, config_1.mj_core_schema)()}].EntityField
|
|
235
|
+
SET
|
|
232
236
|
Sequence=${fieldSequence},
|
|
233
|
-
Type='${veField.Type}',
|
|
237
|
+
Type='${veField.Type}',
|
|
234
238
|
AllowsNull=${veField.AllowsNull ? 1 : 0},
|
|
235
239
|
${makePrimaryKey ? 'IsPrimaryKey=1,IsUnique=1,' : ''}
|
|
236
240
|
Length=${veField.Length},
|
|
237
241
|
Precision=${veField.Precision},
|
|
238
242
|
Scale=${veField.Scale}
|
|
239
|
-
WHERE
|
|
243
|
+
WHERE
|
|
240
244
|
ID = '${field.ID}'`; // don't need to update the __mj_UpdatedAt field here, that happens automatically via the trigger
|
|
241
245
|
await this.LogSQLAndExecute(ds, sqlUpdate, `SQL text to update virtual entity field ${veField.FieldName} for entity ${virtualEntity.Name}`);
|
|
242
246
|
didUpdate = true;
|
|
@@ -246,10 +250,10 @@ let ManageMetadataBase = ManageMetadataBase_1 = class ManageMetadataBase {
|
|
|
246
250
|
// this means that we do NOT have a match so the field does not exist in the entity definition, so we need to add it
|
|
247
251
|
newEntityFieldUUID = this.createNewUUID();
|
|
248
252
|
const sqlAdd = `INSERT INTO [${(0, config_1.mj_core_schema)()}].EntityField (
|
|
249
|
-
ID, EntityID, Name, Type, AllowsNull,
|
|
250
|
-
Length, Precision, Scale,
|
|
251
|
-
Sequence, IsPrimaryKey, IsUnique )
|
|
252
|
-
VALUES ( '${newEntityFieldUUID}', '${entity.ID}', '${veField.FieldName}', '${veField.Type}', ${veField.AllowsNull ? 1 : 0},
|
|
253
|
+
ID, EntityID, Name, Type, AllowsNull,
|
|
254
|
+
Length, Precision, Scale,
|
|
255
|
+
Sequence, IsPrimaryKey, IsUnique )
|
|
256
|
+
VALUES ( '${newEntityFieldUUID}', '${entity.ID}', '${veField.FieldName}', '${veField.Type}', ${veField.AllowsNull ? 1 : 0},
|
|
253
257
|
${veField.Length}, ${veField.Precision}, ${veField.Scale},
|
|
254
258
|
${fieldSequence}, ${makePrimaryKey ? 1 : 0}, ${makePrimaryKey ? 1 : 0}
|
|
255
259
|
)`;
|
|
@@ -280,10 +284,10 @@ let ManageMetadataBase = ManageMetadataBase_1 = class ManageMetadataBase {
|
|
|
280
284
|
* @returns
|
|
281
285
|
*/
|
|
282
286
|
async manageOneToManyEntityRelationships(ds, excludeSchemas, md, batchItems = 5) {
|
|
283
|
-
// the way this works is that we look for entities in our catalog and we look for
|
|
287
|
+
// the way this works is that we look for entities in our catalog and we look for
|
|
284
288
|
// foreign keys in those entities. For example, if we saw an entity called Persons and that entity
|
|
285
289
|
// had a foreign key linking to an entity called Organizations via a field called OrganizationID, then we would create a relationship
|
|
286
|
-
// record in the EntityRelationship table for that relationships. In that example we would create the
|
|
290
|
+
// record in the EntityRelationship table for that relationships. In that example we would create the
|
|
287
291
|
// relationship record with the following values:
|
|
288
292
|
// EntityID = ID of Organizations entity
|
|
289
293
|
// RelatedEntityID = ID of Persons entity
|
|
@@ -294,11 +298,11 @@ let ManageMetadataBase = ManageMetadataBase_1 = class ManageMetadataBase {
|
|
|
294
298
|
// DisplayName = Persons (name of the entity)
|
|
295
299
|
try {
|
|
296
300
|
// STEP 1 - search for all foreign keys in the vwEntityFields view, we use the RelatedEntityID field to determine our FKs
|
|
297
|
-
const sSQL = `SELECT *
|
|
298
|
-
FROM ${(0, config_1.mj_core_schema)()}.vwEntityFields
|
|
299
|
-
WHERE
|
|
300
|
-
RelatedEntityID IS NOT NULL AND
|
|
301
|
-
IsVirtual = 0 AND
|
|
301
|
+
const sSQL = `SELECT *
|
|
302
|
+
FROM ${(0, config_1.mj_core_schema)()}.vwEntityFields
|
|
303
|
+
WHERE
|
|
304
|
+
RelatedEntityID IS NOT NULL AND
|
|
305
|
+
IsVirtual = 0 AND
|
|
302
306
|
EntityID NOT IN (SELECT ID FROM ${(0, config_1.mj_core_schema)()}.Entity WHERE SchemaName IN (${excludeSchemas.map(s => `'${s}'`).join(',')}))
|
|
303
307
|
ORDER BY RelatedEntityID`;
|
|
304
308
|
const entityFields = await ds.query(sSQL);
|
|
@@ -325,7 +329,7 @@ let ManageMetadataBase = ManageMetadataBase_1 = class ManageMetadataBase {
|
|
|
325
329
|
const relCount = relationshipCountMap.get(f.EntityID) || 0;
|
|
326
330
|
const sequence = relCount + 1;
|
|
327
331
|
const newEntityRelationshipUUID = this.createNewUUID();
|
|
328
|
-
batchSQL += `INSERT INTO ${(0, config_1.mj_core_schema)()}.EntityRelationship (ID, EntityID, RelatedEntityID, RelatedEntityJoinField, Type, BundleInAPI, DisplayInForm, DisplayName, Sequence)
|
|
332
|
+
batchSQL += `INSERT INTO ${(0, config_1.mj_core_schema)()}.EntityRelationship (ID, EntityID, RelatedEntityID, RelatedEntityJoinField, Type, BundleInAPI, DisplayInForm, DisplayName, Sequence)
|
|
329
333
|
VALUES ('${newEntityRelationshipUUID}', '${f.RelatedEntityID}', '${f.EntityID}', '${f.Name}', 'One To Many', 1, 1, '${e.Name}', ${sequence});
|
|
330
334
|
`;
|
|
331
335
|
// now update the map for the relationship count
|
|
@@ -360,14 +364,14 @@ let ManageMetadataBase = ManageMetadataBase_1 = class ManageMetadataBase {
|
|
|
360
364
|
const entities = await ds.query(sql);
|
|
361
365
|
if (entities && entities.length > 0) {
|
|
362
366
|
for (const e of entities) {
|
|
363
|
-
// for the given entity, wipe out the entity metadata and its core deps.
|
|
367
|
+
// for the given entity, wipe out the entity metadata and its core deps.
|
|
364
368
|
// the below could fail if there are non-core dependencies on the entity, but that's ok, we will flag that in the console
|
|
365
369
|
// for the admin to handle manually
|
|
366
370
|
try {
|
|
367
371
|
const sqlDelete = `__mj.spDeleteEntityWithCoreDependencies @EntityID='${e.ID}'`;
|
|
368
372
|
await this.LogSQLAndExecute(ds, sqlDelete, `SQL text to remove entity ${e.Name}`);
|
|
369
373
|
(0, status_logging_1.logStatus)(` > Removed metadata for table ${e.SchemaName}.${e.BaseTable}`);
|
|
370
|
-
// next up we need to remove the spCreate, spDelete, spUpdate, BaseView, and FullTextSearchFunction, if provided.
|
|
374
|
+
// next up we need to remove the spCreate, spDelete, spUpdate, BaseView, and FullTextSearchFunction, if provided.
|
|
371
375
|
// We only remoe these artifcacts when they are generated which is info we have in the BaseViewGenerated, spCreateGenerated, etc. fields
|
|
372
376
|
await this.checkDropSQLObject(ds, e.BaseViewGenerated, 'view', e.SchemaName, e.BaseView);
|
|
373
377
|
await this.checkDropSQLObject(ds, e.spCreateGenerated, 'procedure', e.SchemaName, e.spCreate ? e.spCreate : `spCreate${e.ClassName}`);
|
|
@@ -490,22 +494,22 @@ let ManageMetadataBase = ManageMetadataBase_1 = class ManageMetadataBase {
|
|
|
490
494
|
*/
|
|
491
495
|
async ensureDeletedAtFieldsExist(ds, excludeSchemas) {
|
|
492
496
|
try {
|
|
493
|
-
const sqlEntities = `SELECT
|
|
494
|
-
*
|
|
495
|
-
FROM
|
|
496
|
-
[${(0, config_1.mj_core_schema)()}].vwEntities
|
|
497
|
-
WHERE
|
|
498
|
-
VirtualEntity=0 AND
|
|
499
|
-
DeleteType='Soft' AND
|
|
497
|
+
const sqlEntities = `SELECT
|
|
498
|
+
*
|
|
499
|
+
FROM
|
|
500
|
+
[${(0, config_1.mj_core_schema)()}].vwEntities
|
|
501
|
+
WHERE
|
|
502
|
+
VirtualEntity=0 AND
|
|
503
|
+
DeleteType='Soft' AND
|
|
500
504
|
SchemaName NOT IN (${excludeSchemas.map(s => `'${s}'`).join(',')})`;
|
|
501
505
|
const entities = await ds.query(sqlEntities);
|
|
502
506
|
let overallResult = true;
|
|
503
507
|
if (entities.length > 0) {
|
|
504
508
|
// we have 1+ entities that need the special fields, so loop through them and ensure the fields exist
|
|
505
509
|
// validate that each entity has the __mj_DeletedAt field, and it is a DATETIMEOFFSET fields, NOT NULL and both are fields that have a DEFAULT value of GETUTCDATE().
|
|
506
|
-
const sql = `SELECT *
|
|
510
|
+
const sql = `SELECT *
|
|
507
511
|
FROM INFORMATION_SCHEMA.COLUMNS
|
|
508
|
-
WHERE
|
|
512
|
+
WHERE
|
|
509
513
|
${entities.map((e) => `(TABLE_SCHEMA='${e.SchemaName}' AND TABLE_NAME='${e.BaseTable}')`).join(' OR ')}
|
|
510
514
|
AND COLUMN_NAME='${core_1.EntityInfo.DeletedAtFieldName}'`;
|
|
511
515
|
const result = await ds.query(sql);
|
|
@@ -532,13 +536,13 @@ let ManageMetadataBase = ManageMetadataBase_1 = class ManageMetadataBase {
|
|
|
532
536
|
*/
|
|
533
537
|
async ensureCreatedAtUpdatedAtFieldsExist(ds, excludeSchemas) {
|
|
534
538
|
try {
|
|
535
|
-
const sqlEntities = `SELECT
|
|
536
|
-
*
|
|
537
|
-
FROM
|
|
538
|
-
[${(0, config_1.mj_core_schema)()}].vwEntities
|
|
539
|
-
WHERE
|
|
540
|
-
VirtualEntity = 0 AND
|
|
541
|
-
TrackRecordChanges = 1 AND
|
|
539
|
+
const sqlEntities = `SELECT
|
|
540
|
+
*
|
|
541
|
+
FROM
|
|
542
|
+
[${(0, config_1.mj_core_schema)()}].vwEntities
|
|
543
|
+
WHERE
|
|
544
|
+
VirtualEntity = 0 AND
|
|
545
|
+
TrackRecordChanges = 1 AND
|
|
542
546
|
SchemaName NOT IN (${excludeSchemas.map(s => `'${s}'`).join(',')})`;
|
|
543
547
|
const entities = await ds.query(sqlEntities);
|
|
544
548
|
let overallResult = true;
|
|
@@ -546,9 +550,9 @@ let ManageMetadataBase = ManageMetadataBase_1 = class ManageMetadataBase {
|
|
|
546
550
|
// we have 1+ entities that need the special fields, so loop through them and ensure the fields exist
|
|
547
551
|
// validate that each entity has two specific fields, the first one is __mj_CreatedAt and the second one is __mj_UpdatedAt
|
|
548
552
|
// both are DATETIME fields, NOT NULL and both are fields that have a DEFAULT value of GETUTCDATE().
|
|
549
|
-
const sqlCreatedUpdated = `SELECT *
|
|
553
|
+
const sqlCreatedUpdated = `SELECT *
|
|
550
554
|
FROM INFORMATION_SCHEMA.COLUMNS
|
|
551
|
-
WHERE
|
|
555
|
+
WHERE
|
|
552
556
|
${entities.map((e) => `(TABLE_SCHEMA='${e.SchemaName}' AND TABLE_NAME='${e.BaseTable}')`).join(' OR ')}
|
|
553
557
|
AND COLUMN_NAME IN ('${core_1.EntityInfo.CreatedAtFieldName}','${core_1.EntityInfo.UpdatedAtFieldName}')`;
|
|
554
558
|
const result = await ds.query(sqlCreatedUpdated);
|
|
@@ -589,7 +593,7 @@ let ManageMetadataBase = ManageMetadataBase_1 = class ManageMetadataBase {
|
|
|
589
593
|
if (currentFieldData.DATA_TYPE.trim().toLowerCase() !== 'datetimeoffset' ||
|
|
590
594
|
(currentFieldData.IS_NULLABLE.trim().toLowerCase() !== 'no' && !allowNull) ||
|
|
591
595
|
(currentFieldData.IS_NULLABLE.trim().toLowerCase() === 'no' && allowNull)) {
|
|
592
|
-
// the column is the wrong type, or has wrong nullability attribute, so let's update it, first removing the default constraint, then
|
|
596
|
+
// the column is the wrong type, or has wrong nullability attribute, so let's update it, first removing the default constraint, then
|
|
593
597
|
// modifying the column, and finally adding the default constraint back in.
|
|
594
598
|
await this.dropExistingDefaultConstraint(ds, entity, fieldName);
|
|
595
599
|
const sql = `ALTER TABLE [${entity.SchemaName}].[${entity.BaseTable}] ALTER COLUMN ${fieldName} DATETIMEOFFSET ${allowNull ? 'NULL' : 'NOT NULL'}`;
|
|
@@ -657,8 +661,8 @@ let ManageMetadataBase = ManageMetadataBase_1 = class ManageMetadataBase {
|
|
|
657
661
|
JOIN sys.schemas s ON t.schema_id = s.schema_id
|
|
658
662
|
JOIN sys.columns c ON t.object_id = c.object_id
|
|
659
663
|
JOIN sys.default_constraints d ON c.default_object_id = d.object_id
|
|
660
|
-
WHERE s.name = '${entity.SchemaName}'
|
|
661
|
-
AND t.name = '${entity.BaseTable}'
|
|
664
|
+
WHERE s.name = '${entity.SchemaName}'
|
|
665
|
+
AND t.name = '${entity.BaseTable}'
|
|
662
666
|
AND c.name = '${fieldName}';
|
|
663
667
|
|
|
664
668
|
-- Drop the default constraint if it exists
|
|
@@ -692,10 +696,10 @@ let ManageMetadataBase = ManageMetadataBase_1 = class ManageMetadataBase {
|
|
|
692
696
|
for (let e of ManageMetadataBase_1.newEntityList) {
|
|
693
697
|
const data = await ds.query(`SELECT * FROM [${(0, config_1.mj_core_schema)()}].vwEntities WHERE Name = '${e}'`);
|
|
694
698
|
const fields = await ds.query(`SELECT * FROM [${(0, config_1.mj_core_schema)()}].vwEntityFields WHERE EntityID='${data[0].ID}'`);
|
|
695
|
-
const entityUserMessage = userMessage + `Entity Name: ${e},
|
|
696
|
-
Base Table: ${data[0].BaseTable},
|
|
697
|
-
Schema: ${data[0].SchemaName}.
|
|
698
|
-
Fields:
|
|
699
|
+
const entityUserMessage = userMessage + `Entity Name: ${e},
|
|
700
|
+
Base Table: ${data[0].BaseTable},
|
|
701
|
+
Schema: ${data[0].SchemaName}.
|
|
702
|
+
Fields:
|
|
699
703
|
${fields.map((f) => ` ${f.Name}: ${f.Type}`).join('\n')}`;
|
|
700
704
|
const result = await llm.ChatCompletion({
|
|
701
705
|
model: ag.AIModel,
|
|
@@ -742,16 +746,16 @@ let ManageMetadataBase = ManageMetadataBase_1 = class ManageMetadataBase {
|
|
|
742
746
|
*/
|
|
743
747
|
async updateEntityFieldDisplayNameWhereNull(ds, excludeSchemas) {
|
|
744
748
|
try {
|
|
745
|
-
const sql = `SELECT
|
|
746
|
-
ef.ID, ef.Name
|
|
747
|
-
FROM
|
|
749
|
+
const sql = `SELECT
|
|
750
|
+
ef.ID, ef.Name
|
|
751
|
+
FROM
|
|
748
752
|
[${(0, config_1.mj_core_schema)()}].vwEntityFields ef
|
|
749
753
|
INNER JOIN
|
|
750
754
|
[${(0, config_1.mj_core_schema)()}].vwEntities e
|
|
751
755
|
ON
|
|
752
756
|
ef.EntityID = e.ID
|
|
753
|
-
WHERE
|
|
754
|
-
ef.DisplayName IS NULL AND
|
|
757
|
+
WHERE
|
|
758
|
+
ef.DisplayName IS NULL AND
|
|
755
759
|
ef.DisplayName <> ef.Name AND
|
|
756
760
|
ef.Name <> \'ID\' AND
|
|
757
761
|
e.SchemaName NOT IN (${excludeSchemas.map(s => `'${s}'`).join(',')})
|
|
@@ -797,7 +801,7 @@ let ManageMetadataBase = ManageMetadataBase_1 = class ManageMetadataBase {
|
|
|
797
801
|
*/
|
|
798
802
|
getPendingEntityFieldsSELECTSQL() {
|
|
799
803
|
const sSQL = `WITH NumberedRows AS (
|
|
800
|
-
SELECT
|
|
804
|
+
SELECT
|
|
801
805
|
sf.EntityID,
|
|
802
806
|
sf.Sequence,
|
|
803
807
|
sf.FieldName,
|
|
@@ -809,31 +813,31 @@ let ManageMetadataBase = ManageMetadataBase_1 = class ManageMetadataBase {
|
|
|
809
813
|
sf.AllowsNull,
|
|
810
814
|
sf.DefaultValue,
|
|
811
815
|
sf.AutoIncrement,
|
|
812
|
-
IIF(sf.IsVirtual = 1, 0, IIF(sf.FieldName = '${core_1.EntityInfo.CreatedAtFieldName}' OR
|
|
813
|
-
sf.FieldName = '${core_1.EntityInfo.UpdatedAtFieldName}' OR
|
|
814
|
-
sf.FieldName = '${core_1.EntityInfo.DeletedAtFieldName}' OR
|
|
816
|
+
IIF(sf.IsVirtual = 1, 0, IIF(sf.FieldName = '${core_1.EntityInfo.CreatedAtFieldName}' OR
|
|
817
|
+
sf.FieldName = '${core_1.EntityInfo.UpdatedAtFieldName}' OR
|
|
818
|
+
sf.FieldName = '${core_1.EntityInfo.DeletedAtFieldName}' OR
|
|
815
819
|
pk.ColumnName IS NOT NULL, 0, 1)) AllowUpdateAPI,
|
|
816
820
|
sf.IsVirtual,
|
|
817
821
|
e.RelationshipDefaultDisplayType,
|
|
818
822
|
re.ID RelatedEntityID,
|
|
819
823
|
fk.referenced_column RelatedEntityFieldName,
|
|
820
824
|
IIF(sf.FieldName = 'Name', 1, 0) IsNameField,
|
|
821
|
-
IsPrimaryKey = CASE
|
|
822
|
-
WHEN pk.ColumnName IS NOT NULL THEN 1
|
|
823
|
-
ELSE 0
|
|
825
|
+
IsPrimaryKey = CASE
|
|
826
|
+
WHEN pk.ColumnName IS NOT NULL THEN 1
|
|
827
|
+
ELSE 0
|
|
824
828
|
END,
|
|
825
|
-
IsUnique = CASE
|
|
826
|
-
WHEN pk.ColumnName IS NOT NULL THEN 1
|
|
827
|
-
ELSE
|
|
828
|
-
CASE
|
|
829
|
-
WHEN uk.ColumnName IS NOT NULL THEN 1
|
|
830
|
-
ELSE 0
|
|
831
|
-
END
|
|
829
|
+
IsUnique = CASE
|
|
830
|
+
WHEN pk.ColumnName IS NOT NULL THEN 1
|
|
831
|
+
ELSE
|
|
832
|
+
CASE
|
|
833
|
+
WHEN uk.ColumnName IS NOT NULL THEN 1
|
|
834
|
+
ELSE 0
|
|
835
|
+
END
|
|
832
836
|
END,
|
|
833
837
|
ROW_NUMBER() OVER (PARTITION BY sf.EntityID, sf.FieldName ORDER BY (SELECT NULL)) AS rn
|
|
834
838
|
FROM
|
|
835
839
|
[${(0, config_1.mj_core_schema)()}].vwSQLColumnsAndEntityFields sf
|
|
836
|
-
LEFT OUTER JOIN
|
|
840
|
+
LEFT OUTER JOIN
|
|
837
841
|
[${(0, config_1.mj_core_schema)()}].Entity e
|
|
838
842
|
ON
|
|
839
843
|
sf.EntityID = e.ID
|
|
@@ -843,18 +847,18 @@ let ManageMetadataBase = ManageMetadataBase_1 = class ManageMetadataBase {
|
|
|
843
847
|
sf.FieldName = fk.[column] AND
|
|
844
848
|
e.BaseTable = fk.[table] AND
|
|
845
849
|
e.SchemaName = fk.[schema_name]
|
|
846
|
-
LEFT OUTER JOIN
|
|
850
|
+
LEFT OUTER JOIN
|
|
847
851
|
[${(0, config_1.mj_core_schema)()}].Entity re -- Related Entity
|
|
848
852
|
ON
|
|
849
853
|
re.BaseTable = fk.referenced_table AND
|
|
850
854
|
re.SchemaName = fk.[referenced_schema]
|
|
851
|
-
LEFT OUTER JOIN
|
|
855
|
+
LEFT OUTER JOIN
|
|
852
856
|
[${(0, config_1.mj_core_schema)()}].vwTablePrimaryKeys pk
|
|
853
857
|
ON
|
|
854
858
|
e.BaseTable = pk.TableName AND
|
|
855
859
|
sf.FieldName = pk.ColumnName AND
|
|
856
860
|
e.SchemaName = pk.SchemaName
|
|
857
|
-
LEFT OUTER JOIN
|
|
861
|
+
LEFT OUTER JOIN
|
|
858
862
|
[${(0, config_1.mj_core_schema)()}].vwTableUniqueKeys uk
|
|
859
863
|
ON
|
|
860
864
|
e.BaseTable = uk.TableName AND
|
|
@@ -863,10 +867,10 @@ let ManageMetadataBase = ManageMetadataBase_1 = class ManageMetadataBase {
|
|
|
863
867
|
WHERE
|
|
864
868
|
EntityFieldID IS NULL -- only where we have NOT YET CREATED EntityField records\n${this.createExcludeTablesAndSchemasFilter('sf.')}
|
|
865
869
|
)
|
|
866
|
-
SELECT
|
|
867
|
-
*
|
|
868
|
-
FROM
|
|
869
|
-
NumberedRows -- REMOVED - Need all fkey fields WHERE rn = 1 -- if someone has two foreign keys with same to/from table and field name this makes sure we only get the field info ONCE
|
|
870
|
+
SELECT
|
|
871
|
+
*
|
|
872
|
+
FROM
|
|
873
|
+
NumberedRows -- REMOVED - Need all fkey fields WHERE rn = 1 -- if someone has two foreign keys with same to/from table and field name this makes sure we only get the field info ONCE
|
|
870
874
|
ORDER BY EntityID, Sequence`;
|
|
871
875
|
return sSQL;
|
|
872
876
|
}
|
|
@@ -984,12 +988,12 @@ let ManageMetadataBase = ManageMetadataBase_1 = class ManageMetadataBase {
|
|
|
984
988
|
for (let i = 0; i < newEntityFields.length; ++i) {
|
|
985
989
|
const n = newEntityFields[i];
|
|
986
990
|
if (n.EntityID !== null && n.EntityID !== undefined && n.EntityID.length > 0) {
|
|
987
|
-
// need to check for null entity id = that is because the above query can return candidate Entity Fields but the entities may not have been created if the entities
|
|
991
|
+
// need to check for null entity id = that is because the above query can return candidate Entity Fields but the entities may not have been created if the entities
|
|
988
992
|
// that would have been created violate rules - such as not having an ID column, etc.
|
|
989
993
|
const newEntityFieldUUID = this.createNewUUID();
|
|
990
994
|
const sSQLInsert = this.getPendingEntityFieldINSERTSQL(newEntityFieldUUID, n);
|
|
991
995
|
await this.LogSQLAndExecute(ds, sSQLInsert, `SQL text to insert new entity field`);
|
|
992
|
-
// if we get here, we're okay, otherwise we have an exception, which we want as it blows up transaction
|
|
996
|
+
// if we get here, we're okay, otherwise we have an exception, which we want as it blows up transaction
|
|
993
997
|
}
|
|
994
998
|
}
|
|
995
999
|
});
|
|
@@ -1009,7 +1013,7 @@ let ManageMetadataBase = ManageMetadataBase_1 = class ManageMetadataBase {
|
|
|
1009
1013
|
*/
|
|
1010
1014
|
async updateEntityFieldRelatedEntityNameFieldMap(ds, entityFieldID, relatedEntityNameFieldMap) {
|
|
1011
1015
|
try {
|
|
1012
|
-
const sSQL = `EXEC [${(0, config_1.mj_core_schema)()}].spUpdateEntityFieldRelatedEntityNameFieldMap
|
|
1016
|
+
const sSQL = `EXEC [${(0, config_1.mj_core_schema)()}].spUpdateEntityFieldRelatedEntityNameFieldMap
|
|
1013
1017
|
@EntityFieldID='${entityFieldID}',
|
|
1014
1018
|
@RelatedEntityNameFieldMap='${relatedEntityNameFieldMap}'`;
|
|
1015
1019
|
await this.LogSQLAndExecute(ds, sSQL, `SQL text to update entity field related entity name field map for entity field ID ${entityFieldID}`);
|
|
@@ -1055,9 +1059,9 @@ let ManageMetadataBase = ManageMetadataBase_1 = class ManageMetadataBase {
|
|
|
1055
1059
|
}
|
|
1056
1060
|
async manageEntityFieldValues(ds, excludeSchemas) {
|
|
1057
1061
|
try {
|
|
1058
|
-
// here we want to get all of the entity fields that have check constraints attached to them. For each field that has a check constraint, we want to
|
|
1062
|
+
// here we want to get all of the entity fields that have check constraints attached to them. For each field that has a check constraint, we want to
|
|
1059
1063
|
// evaluate it to see if it is a simple series of OR statements or not, if it is a simple series of OR statements, we can parse the possible values
|
|
1060
|
-
// for the field and sync that up with the EntityFieldValue table. If it is not a simple series of OR statements, we will not be able to parse it and we'll
|
|
1064
|
+
// for the field and sync that up with the EntityFieldValue table. If it is not a simple series of OR statements, we will not be able to parse it and we'll
|
|
1061
1065
|
// just ignore it.
|
|
1062
1066
|
const filter = excludeSchemas && excludeSchemas.length > 0 ? ` WHERE SchemaName NOT IN (${excludeSchemas.map(s => `'${s}'`).join(',')})` : '';
|
|
1063
1067
|
const sSQL = `SELECT * FROM [${(0, config_1.mj_core_schema)()}].vwEntityFieldsWithCheckConstraints${filter}`;
|
|
@@ -1112,9 +1116,9 @@ let ManageMetadataBase = ManageMetadataBase_1 = class ManageMetadataBase {
|
|
|
1112
1116
|
for (const v of possibleValues) {
|
|
1113
1117
|
if (!existingValues.find((ev) => ev.Value === v)) {
|
|
1114
1118
|
// add the value to the database
|
|
1115
|
-
const sSQLInsert = `INSERT INTO [${(0, config_1.mj_core_schema)()}].EntityFieldValue
|
|
1116
|
-
(EntityFieldID, Sequence, Value, Code)
|
|
1117
|
-
VALUES
|
|
1119
|
+
const sSQLInsert = `INSERT INTO [${(0, config_1.mj_core_schema)()}].EntityFieldValue
|
|
1120
|
+
(EntityFieldID, Sequence, Value, Code)
|
|
1121
|
+
VALUES
|
|
1118
1122
|
('${entityFieldID}', ${1 + possibleValues.indexOf(v)}, '${v}', '${v}')`;
|
|
1119
1123
|
await this.LogSQLAndExecute(ds, sSQLInsert, `SQL text to insert entity field values`);
|
|
1120
1124
|
numAdded++;
|
|
@@ -1207,7 +1211,7 @@ let ManageMetadataBase = ManageMetadataBase_1 = class ManageMetadataBase {
|
|
|
1207
1211
|
if (ManageMetadataBase_1.newEntityList.length > 0) {
|
|
1208
1212
|
// only do this if we actually created new entities
|
|
1209
1213
|
(0, core_1.LogStatus)(` Done creating entities, refreshing metadata to reflect new entities...`);
|
|
1210
|
-
await md.Refresh(); // refresh now since we've added some new entities
|
|
1214
|
+
await md.Refresh(); // refresh now since we've added some new entities
|
|
1211
1215
|
}
|
|
1212
1216
|
}
|
|
1213
1217
|
return true; // if we get here, we succeeded
|
|
@@ -1293,7 +1297,7 @@ let ManageMetadataBase = ManageMetadataBase_1 = class ManageMetadataBase {
|
|
|
1293
1297
|
try {
|
|
1294
1298
|
const { shouldCreate, validationMessage } = await this.shouldCreateNewEntity(ds, newEntity);
|
|
1295
1299
|
if (shouldCreate) {
|
|
1296
|
-
// process a single new entity
|
|
1300
|
+
// process a single new entity
|
|
1297
1301
|
let newEntityName = await this.createNewEntityName(newEntity);
|
|
1298
1302
|
let suffix = '';
|
|
1299
1303
|
const existingEntity = md.Entities.find(e => e.Name === newEntityName);
|
|
@@ -1312,7 +1316,7 @@ let ManageMetadataBase = ManageMetadataBase_1 = class ManageMetadataBase {
|
|
|
1312
1316
|
// if we get here we created a new entity safely, otherwise we get exception
|
|
1313
1317
|
// add it to the new entity list
|
|
1314
1318
|
ManageMetadataBase_1.newEntityList.push(newEntityName);
|
|
1315
|
-
// next, check if this entity is in a schema that is new (e.g. no other entities have been added to this schema yet), if so and if
|
|
1319
|
+
// next, check if this entity is in a schema that is new (e.g. no other entities have been added to this schema yet), if so and if
|
|
1316
1320
|
// our config option is set to create new applications from new schemas, then create a new application for this schema
|
|
1317
1321
|
let appUUID = '';
|
|
1318
1322
|
if (isNewSchema && config_1.configInfo.newSchemaDefaults.CreateNewApplicationWithSchemaName) {
|
|
@@ -1323,7 +1327,7 @@ let ManageMetadataBase = ManageMetadataBase_1 = class ManageMetadataBase {
|
|
|
1323
1327
|
// doesn't already exist, so create it
|
|
1324
1328
|
appUUID = this.createNewUUID();
|
|
1325
1329
|
await this.createNewApplication(ds, appUUID, newEntity.SchemaName);
|
|
1326
|
-
await md.Refresh(); // refresh now since we've added a new application, not super efficient to do this for each new application but that won't happen super
|
|
1330
|
+
await md.Refresh(); // refresh now since we've added a new application, not super efficient to do this for each new application but that won't happen super
|
|
1327
1331
|
// often so not a huge deal, would be more efficient do this in batch after all new apps are created but that would be an over optimization IMO
|
|
1328
1332
|
}
|
|
1329
1333
|
}
|
|
@@ -1334,8 +1338,8 @@ let ManageMetadataBase = ManageMetadataBase_1 = class ManageMetadataBase {
|
|
|
1334
1338
|
if (appUUID && appUUID.length > 0) {
|
|
1335
1339
|
if (config_1.configInfo.newEntityDefaults.AddToApplicationWithSchemaName) {
|
|
1336
1340
|
// only do this if the configuration setting is set to add new entities to applications for schema names
|
|
1337
|
-
const sSQLInsertApplicationEntity = `INSERT INTO ${(0, config_1.mj_core_schema)()}.ApplicationEntity
|
|
1338
|
-
(ApplicationID, EntityID, Sequence) VALUES
|
|
1341
|
+
const sSQLInsertApplicationEntity = `INSERT INTO ${(0, config_1.mj_core_schema)()}.ApplicationEntity
|
|
1342
|
+
(ApplicationID, EntityID, Sequence) VALUES
|
|
1339
1343
|
('${appUUID}', '${newEntityID}', (SELECT ISNULL(MAX(Sequence),0)+1 FROM ${(0, config_1.mj_core_schema)()}.ApplicationEntity WHERE ApplicationID = '${appUUID}'))`;
|
|
1340
1344
|
await this.LogSQLAndExecute(ds, sSQLInsertApplicationEntity, `SQL generated to add new entity ${newEntityName} to application ID: '${appUUID}'`);
|
|
1341
1345
|
}
|
|
@@ -1354,8 +1358,8 @@ let ManageMetadataBase = ManageMetadataBase_1 = class ManageMetadataBase {
|
|
|
1354
1358
|
for (const p of permissions) {
|
|
1355
1359
|
const RoleID = md.Roles.find(r => r.Name.trim().toLowerCase() === p.RoleName.trim().toLowerCase())?.ID;
|
|
1356
1360
|
if (RoleID) {
|
|
1357
|
-
const sSQLInsertPermission = `INSERT INTO ${(0, config_1.mj_core_schema)()}.EntityPermission
|
|
1358
|
-
(EntityID, RoleID, CanRead, CanCreate, CanUpdate, CanDelete) VALUES
|
|
1361
|
+
const sSQLInsertPermission = `INSERT INTO ${(0, config_1.mj_core_schema)()}.EntityPermission
|
|
1362
|
+
(EntityID, RoleID, CanRead, CanCreate, CanUpdate, CanDelete) VALUES
|
|
1359
1363
|
('${newEntityID}', '${RoleID}', ${p.CanRead ? 1 : 0}, ${p.CanCreate ? 1 : 0}, ${p.CanUpdate ? 1 : 0}, ${p.CanDelete ? 1 : 0})`;
|
|
1360
1364
|
await this.LogSQLAndExecute(ds, sSQLInsertPermission, `SQL generated to add new permission for entity ${newEntityName} for role ${p.RoleName}`);
|
|
1361
1365
|
}
|
|
@@ -1398,16 +1402,16 @@ let ManageMetadataBase = ManageMetadataBase_1 = class ManageMetadataBase {
|
|
|
1398
1402
|
createNewEntityInsertSQL(newEntityUUID, newEntityName, newEntity, newEntitySuffix) {
|
|
1399
1403
|
const newEntityDefaults = config_1.configInfo.newEntityDefaults;
|
|
1400
1404
|
const newEntityDescriptionEscaped = newEntity.Description ? `'${newEntity.Description.replace(/'/g, "''")}` : null;
|
|
1401
|
-
const sSQLInsert = `
|
|
1405
|
+
const sSQLInsert = `
|
|
1402
1406
|
INSERT INTO [${(0, config_1.mj_core_schema)()}].Entity (
|
|
1403
1407
|
ID,
|
|
1404
|
-
Name,
|
|
1408
|
+
Name,
|
|
1405
1409
|
Description,
|
|
1406
1410
|
NameSuffix,
|
|
1407
|
-
BaseTable,
|
|
1408
|
-
BaseView,
|
|
1409
|
-
SchemaName,
|
|
1410
|
-
IncludeInAPI,
|
|
1411
|
+
BaseTable,
|
|
1412
|
+
BaseView,
|
|
1413
|
+
SchemaName,
|
|
1414
|
+
IncludeInAPI,
|
|
1411
1415
|
AllowUserSearchAPI
|
|
1412
1416
|
${newEntityDefaults.TrackRecordChanges === undefined ? '' : ', TrackRecordChanges'}
|
|
1413
1417
|
${newEntityDefaults.AuditRecordAccess === undefined ? '' : ', AuditRecordAccess'}
|
|
@@ -1417,16 +1421,16 @@ let ManageMetadataBase = ManageMetadataBase_1 = class ManageMetadataBase {
|
|
|
1417
1421
|
${newEntityDefaults.AllowUpdateAPI === undefined ? '' : ', AllowUpdateAPI'}
|
|
1418
1422
|
${newEntityDefaults.AllowDeleteAPI === undefined ? '' : ', AllowDeleteAPI'}
|
|
1419
1423
|
${newEntityDefaults.UserViewMaxRows === undefined ? '' : ', UserViewMaxRows'}
|
|
1420
|
-
)
|
|
1424
|
+
)
|
|
1421
1425
|
VALUES (
|
|
1422
1426
|
'${newEntityUUID}',
|
|
1423
|
-
'${newEntityName}',
|
|
1427
|
+
'${newEntityName}',
|
|
1424
1428
|
${newEntityDescriptionEscaped ? newEntityDescriptionEscaped : 'NULL' /*if no description, then null*/},
|
|
1425
1429
|
${newEntitySuffix && newEntitySuffix.length > 0 ? `'${newEntitySuffix}'` : 'NULL'},
|
|
1426
|
-
'${newEntity.TableName}',
|
|
1427
|
-
'vw${this.generatePluralName(newEntity.TableName) + (newEntitySuffix && newEntitySuffix.length > 0 ? newEntitySuffix : '')}',
|
|
1430
|
+
'${newEntity.TableName}',
|
|
1431
|
+
'vw${this.generatePluralName(newEntity.TableName) + (newEntitySuffix && newEntitySuffix.length > 0 ? newEntitySuffix : '')}',
|
|
1428
1432
|
'${newEntity.SchemaName}',
|
|
1429
|
-
1,
|
|
1433
|
+
1,
|
|
1430
1434
|
${newEntityDefaults.AllowUserSearchAPI === undefined ? 1 : newEntityDefaults.AllowUserSearchAPI ? 1 : 0}
|
|
1431
1435
|
${newEntityDefaults.TrackRecordChanges === undefined ? '' : ', ' + (newEntityDefaults.TrackRecordChanges ? '1' : '0')}
|
|
1432
1436
|
${newEntityDefaults.AuditRecordAccess === undefined ? '' : ', ' + (newEntityDefaults.AuditRecordAccess ? '1' : '0')}
|