Get all fields of a type through Metadata APIs


#1

When using TagMetadataStore.readType to get the fields defined on the type, it only returns fields that are directly defined on the type, but not fields that are defined on its mixes type.

For example, given following type,

entity type myType schema name 'name' {
  fieldA: string
  fieldB: string
}

TagMetadataStore.readType('myType') only returns fields fieldA and fieldB, but not fields defined on Persistable.

Do we have another Metadata API that could return all fields on a type?


#2

Try myType.fieldTypes()


#3

fieldTypes is a client side API, and it is not defined on the type.

So we couldn’t call it through /api/1/tenant/tag/myType?action=fieldTypes, and it will return error function metadata was not found for action Target [tenant/tag/myType?action=fieldTypes]


#4

Below is what I’ve used in the past to get the list of fields for a type. You can discard the schema name check, but I did need to explicitly filter out methods.

/**
 * Returns a map of fields and their corresponding schema names for a type.
 **/
function lookupFieldSchema(typeName) {
	var fieldSchemaMap = [];
	var typ = c3Type(typeName);

	_.each(typ.fieldTypes(), function(ft) { 
			if (!ft.valueType().isMethod())  {
				if (ft.extensions().schema.name != undefined ) {
					if (ft.extensions().schema.name != "") {
						fieldSchemaMap.push({fieldName:ft.name(), schemaName: ft.extensions().schema.name});
					}
				}
				
			}
	})

	return fieldSchemaMap;
}

#5

In case type system is not available, and the only way we could talk with platform is through URL, like http://my-server.c3-e.com/api/1/tenant/tag/myType?action=fetch. How could we get all fields in that case?


#6

FYI, this is use case where we communicate with the platform through URL from another provider, like MicroStrategy, where type system is not available.


#8

Using REST APIs you can do the following:

https://myserver.c3iot.com/api/1/tenant/tag/TagMetadataStore?action=readType

The body of the response should contain:

{
  "this": {},
  "typeName": "<<typeName>>"
}

The response from the platform will look like (abbreviated):

{
  "name": "User",
  ...
  "fields": [
    {
      "name": "firstname",
      "valueType": {
        "type": "PrimitiveType",
        "name": "string"
      },
     ...
    },
    ...
}

You will need to do some filtering, but the metadata should exist.