Skip to content
Snippets Groups Projects
index.js 2.45 KiB
Newer Older
Tarje.Lavik's avatar
Tarje.Lavik committed
import _ from 'lodash';
import config from 'config:jsonld-context';

/**
 * Order the schemas for simpler diffs
 */
export const orderSchemas = (schema) => {
Tarje.Lavik's avatar
Tarje.Lavik committed
  const result = _.orderBy(schema, ['name'], ['asc']).filter(_class => _class.options?.jsonld?.exclude != true)
  return result
Tarje.Lavik's avatar
Tarje.Lavik committed
}

/**
 * Create default context
 * @returns {object}
 */
export const getContext = () => {
  // If no vocab is set, default to example uri
  const vocab = config.vocab?.prefix ? config.vocab.prefix : '@vocab';
  const vocabUri = config.vocab?.uri ? config.vocab.uri : 'http://example.org/model/0.1/';
  
  // If vocab is set to '@vocab' then we do not add a prefix to '@id' values, because '@base' sets prefix for all
  const base = config.vocab?.prefix && config.vocab?.prefix != '@vocab' ? `${config.vocab.prefix}:` : ''
  
  // Default context
  const context = {
    '@context': {
      '@version': 1.1,
Tarje.Lavik's avatar
Tarje.Lavik committed
      _id: '@id',
      _ref: '@id',
Tarje.Lavik's avatar
Tarje.Lavik committed
      id: '@id',
Tarje.Lavik's avatar
Tarje.Lavik committed
      _type: '@type',
      type: '@type',
Tarje.Lavik's avatar
Tarje.Lavik committed
      '@base': config.base ? config.base : undefined,
Tarje.Lavik's avatar
Tarje.Lavik committed
      [vocab]: vocabUri,
Tarje.Lavik's avatar
Tarje.Lavik committed
      "crm": "http://www.cidoc-crm.org/cidoc-crm/",
      "xsd": "http://www.w3.org/2001/XMLSchema#",
Tarje.Lavik's avatar
Tarje.Lavik committed
      "rdfs": "http://www.w3.org/2000/01/rdf-schema#",
Tarje.Lavik's avatar
Tarje.Lavik committed
    },
  };

  return {context, base}
}


/**
 * Map props to '@id', '@container' and '@type'
 * @param {object} prop 
 * @returns {object}
 */
const getProps = (prop, base) => {
  const result = {
    '@id': prop.options?.jsonld?.context?.['@id'] ? prop.options.jsonld.context?.['@id'] : `${base}${prop.name}`,
    '@container': prop.options?.jsonld?.context?.['@container'] ? prop.options.jsonld.context?.['@container'] : undefined,
    '@type': prop.options?.jsonld?.context?.['@type'] ? prop.options.jsonld.context?.['@type'] : prop.type === 'reference' ? `${base}${prop.name}` : undefined,
  };
  
/*   if (prop.type === 'reference') {
Tarje.Lavik's avatar
Tarje.Lavik committed
    result['@type'] = '@id';
  }

  if (prop.options?.jsonld?.context) {
    result = prop.options.jsonld.context
  
  if (!prop.options?.jsonld?.context?.['@id']) {
    result['@id'] = `${base}${prop.name}`;
  } */
  
Tarje.Lavik's avatar
Tarje.Lavik committed
  return result;
};

/**
 * Return fields as key with local '@context' object
 * @param {array} fields 
 * @returns {object}
 */
export const getFields = (fields, base) => {
  console.log(fields);
  if (!fields) return null;

  const result = fields.map((field) => {
    if(field.options?.jsonld?.exclude) return

Tarje.Lavik's avatar
Tarje.Lavik committed
    return {
      [field.name]: getProps(field, base),
    };
  });
  return Object.assign(...result);
};