Creating a Schema

Creates a customized schema as per user specifications.

Creating a Schema Definition

The sampleCreateSchemaDef function demonstrates how to create a schema definition using the DICE ID SDK. It prompts the user for the attribute name, schema name, and schema version, and then calls the createSchema function to create the schema.

API Elements

  1. schema_input: An object that holds the attribute, schema name, and schema version for creating the schema.

  2. schema_input.attributes: An object that stores the attribute name and its data type.

  3. attr_name: Stores the attribute name entered by the user.

  4. schema_name: Stores the schema name entered by the user.

  5. schema_version: Stores the schema version entered by the user.

  6. diceidsdk.issuer.createSchema(): Creates a schema using the schema_input.

  7. Imports the ISSUER_XAPIKEY constant from the appconfig module.

  8. resp: Holds the response from the createSchema API, including the schema details.

  9. schemaOutput: Stores the created schema output.

  10. err: Stores any error that occurs during the process.

function createSchema() {
  const schemaInput = {
    attributes: {},
    schema_name: '',
    schema_version: '',
  };

  const numAttr = parseInt(prompt('Enter number of attributes in the schema: '));

  for (let i = 0; i < numAttr; i++) {
    const attrName = prompt('Enter attribute name: ');
    schemaInput.attributes[attrName] = 'text';
  }

  schemaInput.schema_name = prompt('Enter schema name: ');
  schemaInput.schema_version = prompt('Enter schema version: ');

  diceidsdk.issuer.createSchema(schemaInput, ISSUER_XAPIKEY)
    .then(resp => {
      const schemaOutput = resp;
      console.log('Schema created successfully:', schemaOutput);
    })
    .catch(err => {
      console.error('Error creating schema:', err);
    });
}

createSchema();

Running the Code

Step 1: Add the sampleCreateSchemaDef function: Copy the sampleCreateSchemaDef function code and paste it into your JavaScript file after importing the diceidsdk module.

Step 2: Obtain an API Key: Make sure you have an API key from the DiceID platform. Set the ISSUER_XAPIKEY constant in the appconfig.js file to your actual API key.

Step 3: Run the function: Invoke the sampleCreateSchemaDef function in your code to execute it. For example:

node sampleCreateSchemaDef.js

Step 4: Provide input: The function will prompt you to enter the attribute name, schema name, and schema version. Enter the required information in the terminal or command prompt.

Step 4: Once the createSchema function completes, you can handle the response within the .then() block of the promise or perform any necessary error handling in the .catch() block.

Last updated