Community Tip - Did you know you can set a signature that will be added to all your posts? Set it here! X
I'm trying to build some globally used Shapes to be used by all new data tables in our implementation. One of which is a standard key generator. However, part of doing this is being able to dynamically identify the "key" of any given table. ThingWorx has a common "Primary Key" Aspect when defining a data shape, so I'm sure there's a way to write a script that returns a STRING result in the form of the Field_Name who's aspect is boolean "Yes" for this Primary Key Aspect on any given table.
Advice??
Solved! Go to Solution.
Hi Christopher, I've exactly did what you explained here. For your mater ( getting pk fields of a DataTable ) here it's my code:
( I have a property pk of type Infotable to store the PK fields ( data shape: FieldDefinition ), in order to not to calculate it each time you insert a row )
// -- Calculate Primary Keys
var dsName = me.GetDataShape();
var ds = DataShapes[dsName];
var fields = ds.GetDataShapeMetadataAsJSON().fieldDefinitions;
me.pk = Resources["InfoTableFunctions"].CreateInfoTableFromDataShape({
infoTableName : "InfoTable",
dataShapeName : "FieldDefinition"
});
me.pk.AddField({ name: "ordinal", baseType: "INTEGER" });
for (var property in fields) {
if (fields.hasOwnProperty(property)) {
if (fields[property].aspects) {
if (fields[property].aspects.isPrimaryKey) {
me.pk.AddRow({
name: property,
isPrimaryKey: true,
baseType: fields[property].baseType,
description: fields[property].desription,
dataShape: dsName,
ordinal: fields[property].ordinal
});
}
}
}
}
Hi Christopher, I've exactly did what you explained here. For your mater ( getting pk fields of a DataTable ) here it's my code:
( I have a property pk of type Infotable to store the PK fields ( data shape: FieldDefinition ), in order to not to calculate it each time you insert a row )
// -- Calculate Primary Keys
var dsName = me.GetDataShape();
var ds = DataShapes[dsName];
var fields = ds.GetDataShapeMetadataAsJSON().fieldDefinitions;
me.pk = Resources["InfoTableFunctions"].CreateInfoTableFromDataShape({
infoTableName : "InfoTable",
dataShapeName : "FieldDefinition"
});
me.pk.AddField({ name: "ordinal", baseType: "INTEGER" });
for (var property in fields) {
if (fields.hasOwnProperty(property)) {
if (fields[property].aspects) {
if (fields[property].aspects.isPrimaryKey) {
me.pk.AddRow({
name: property,
isPrimaryKey: true,
baseType: fields[property].baseType,
description: fields[property].desription,
dataShape: dsName,
ordinal: fields[property].ordinal
});
}
}
}
}