<commit_before>
import _ from 'lodash';

import Tyr        from './tyr';
import Collection from './core/collection';

export const SchemaType = new Collection({
  id: '_t0',
  name: 'tyrSchemaType',
  enum: true,
  client: false,
  fields: {
    _id:  { is: 'integer' },
    name: { is: 'string', labelField: true }
  },
  values: [
    [ '_id', 'name'    ],

    [     1, 'Full'    ],
    [     2, 'Partial' ]
  ]
});


const Schema = new Collection({
  id: '_t1',
  name: 'tyrSchema',
  client: false,
  fields: {
    _id:        { is:   'mongoid' },
    collection: { is:   'string' }, // this is the collection id, i.e. t01
    type:       { link: 'tyrSchemaType' },
    match:      { is:   'object' },
    def:        { is:   'object' },
    src:        { is:   'string' }
  },
  timestamps: true
});


let schemaCache;

Schema.on({
  type: 'change',
  handler: (/*event*/) => {
    // TODO:  analyze the event and only invalidate part of the schema ?
    schemaCache = null
  }
});

Collection.prototype.invalidateSchemaCache = function() {
  schemaCache = null;
};

Collection.prototype.fieldsFor = async function(obj) {
  let missing = false;

  if (!schemaCache) {
    schemaCache = await (await Schema.db.find()).toArray();

    schemaCache.forEach(schema => {
      const collection = Tyr.byId[schema.collection],
            def        = schema.def;

      if (!collection) {<commit_msg>add comment clarifying status of events<commit_after>
import _ from 'lodash';

import Tyr        from './tyr';
import Collection from './core/collection';

export const SchemaType = new Collection({
  id: '_t0',
  name: 'tyrSchemaType',
  enum: true,
  client: false,
  fields: {
    _id:  { is: 'integer' },
    name: { is: 'string', labelField: true }
  },
  values: [
    [ '_id', 'name'    ],

    [     1, 'Full'    ],
    [     2, 'Partial' ]
  ]
});


const Schema = new Collection({
  id: '_t1',
  name: 'tyrSchema',
  client: false,
  fields: {
    _id:        { is:   'mongoid' },
    collection: { is:   'string' }, // this is the collection id, i.e. t01
    type:       { link: 'tyrSchemaType' },
    match:      { is:   'object' },
    def:        { is:   'object' },
    src:        { is:   'string' }
  },
  timestamps: true
});


let schemaCache;

Schema.on({
  type: 'change',
  handler: (/*event*/) => {
    // TODO:  analyze the event and only invalidate part of the schema ?

    // TODO:  this doesn't work yet because this needs to be a post-, not a pre-, handler and events don't support post yet
    schemaCache = null
  }
});

Collection.prototype.invalidateSchemaCache = function() {
  schemaCache = null;
};

Collection.prototype.fieldsFor = async function(obj) {
  let missing = false;

  if (!schemaCache) {
    schemaCache = await (await Schema.db.find()).toArray();

    schemaCache.forEach(schema => {
      const collection = Tyr.byId[schema.collection],
            def        = schema.def;

      if (!collection) {
{'fraction_non_alphanumeric': 0.1529331976941336, 'fraction_numerical': 0.004069175991861648, 'mean_word_length': 2.4262485481997675, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3570354', 'n_tokens_mistral': 989, 'n_tokens_neox': 973, 'n_words': 273}