<commit_before>define(module, function(exports, require) {

  var qp = require('qp-utility');

  function default_string() { return ''; }
  function default_number() { return 0; }
  function default_boolean() { return false; }
  function default_datetime() { return new Date(); }
  function default_date() { return (new Date()).setUTCHours(12, 0, 0, 0); }

  qp.module(exports, {

    ns: 'qp-model/schema',

    build: function(_exports, schema) {
      schema.create = this.create.bind(this, schema.columns);
      schema.table = { name: schema.table };
      schema.fields = { managed: [], all: [] };
      qp.each_own(schema.columns, function(column, name) {
        column.name = name;
        if (column.managed) schema.fields.managed.push(name); else schema.fields.all.push(name);
      });
      _exports(schema);
    },

    create: function(fields, data, options) {
      options = qp.options(options, { internal: false });
      if (qp.is(data, 'array')) {
        return qp.map(data, function(item) {
          return this.create_item(fields, item, {}, options);
        }.bind(this));
      } else if (qp.is(data, 'object')) {
        return this.create_item(fields, data, options.instance || {}, options);
      } else {
        return null;
      }
    },

    create_item: function(fields, source, target, options) {
      if (qp.is(source, 'object')) {
        qp.each_own(fields, function(v, k) {
          if (options.internal || !v.internal) {
            target[k] = source[k] || v.default();
          }
        });
      }
      return target || {};
    },

    field: function(type, size, scale, options) {
      if (qp.is(size, 'object', 'undefined')) {
        options = size;
        size = scale = 0;
      } else if (qp.is(scale, 'object', 'undefined')) {
        options = scale;
        scale = 0;
      }
      var field;

      // TEXT
      if (type === 'text' && size === 0) {
        field = { type: 'varchar', size: size, default: default_string };
      } else if (type === 'text') {
        field = { type: 'text', default: default_string };

      // INTEGER
      } else if (type === 'smallint' || type === 'int2') {
        field = { type: 'smallint', size: 2, default: default_number };
      } else if (type === 'int' || type === 'int4' || type === 'integer') {
        field = { type: 'integer', size: 4, default: default_number };
      } else if (type === 'bigint' || type === 'int8') {
        field = { type: 'bigint', size: 8, default: default_number };

      // NUMERIC
      } else if (type === 'numeric' || type === 'decimal' || type === 'number') {
        field = { type: 'numeric', size: size, scale: scale, default: default_number };

      // CURRENCY
      } else if (type === 'currency') {
        field = { type: 'numeric', size: 12, scale: 4, default: default_number };

      // BOOLEAN
      } else if (type === 'bool' || type === 'boolean') {
        field = { type: 'boolean', default: default_boolean };

      // DATETIME
      } else if (type === 'datetime') {
        field = { type: 'timestamp with time zone', default: default_datetime };

      // DATE
      } else if (type === 'date') {
        field = { type: 'timestamp with time zone', default: default_date };

      // SERIAL
      } else if (type === 'smallserial') {
        field = { type: 'smallserial', default: qp.noop };
      } else if (type === 'serial') {
        field = { type: 'serial', default: qp.noop };
      } else if (type === 'bigserial') {
        field = { type: 'bigserial', default: qp.noop };

      } else {
        field = { type: '', default: qp.noop };

      }
      return qp.options(field, options);
    },

    primary: function() {
      return this.field('int', 64, { primary: true, managed: true });
    },

    foreign: function(table) {
      return this.field('int', 64, { foreign: true, table: table });
    },

    foreign_ids: function(table) {
      return this.field('int', 64, { foreign: true, table: table, array: true });
    },

    created: function() {
      return this.field('datetime', { managed: true });
    },

    modified: function() {
      return this.field('datetime', { managed: true });
    }

  });

});<commit_msg>schema; add primary_key & unique field types<commit_after>define(module, function(exports, require) {

  var qp = require('qp-utility');

  function default_string() { return ''; }
  function default_number() { return 0; }
  function default_boolean() { return false; }
  function default_datetime() { return new Date(); }
  function default_date() { return (new Date()).setUTCHours(12, 0, 0, 0); }

  qp.module(exports, {

    ns: 'qp-model/schema',

    build: function(_exports, schema) {
      schema.create = this.create.bind(this, schema.columns);
      schema.table = { name: schema.table };
      schema.fields = { managed: [], all: [] };
      qp.each_own(schema.columns, function(column, name) {
        column.name = name;
        if (column.managed) schema.fields.managed.push(name); else schema.fields.all.push(name);
      });
      _exports(schema);
    },

    create: function(fields, data, options) {
      options = qp.options(options, { internal: false });
      if (qp.is(data, 'array')) {
        return qp.map(data, function(item) {
          return this.create_item(fields, item, {}, options);
        }.bind(this));
      } else if (qp.is(data, 'object')) {
        return this.create_item(fields, data, options.instance || {}, options);
      } else {
        return null;
      }
    },

    create_item: function(fields, source, target, options) {
      if (qp.is(source, 'object')) {
        qp.each_own(fields, function(v, k) {
          if (options.internal || !v.internal) {
            target[k] = source[k] || v.default();
          }
        });
      }
      return target || {};
    },

    field: function(type, size, scale, options) {
      if (qp.is(size, 'object', 'undefined')) {
        options = size;
        size = scale = 0;
      } else if (qp.is(scale, 'object', 'undefined')) {
        options = scale;
        scale = 0;
      }
      var field;

      // TEXT
      if (type === 'text' && size === 0) {
        field = { type: 'varchar', size: size, default: default_string };
      } else if (type === 'text') {
        field = { type: 'text', default: default_string };

      // INTEGER
      } else if (type === 'smallint' || type === 'int2') {
        field = { type: 'smallint', size: 2, default: default_number };
      } else if (type === 'int' || type === 'int4' || type === 'integer') {
        field = { type: 'integer', size: 4, default: default_number };
      } else if (type === 'bigint' || type === 'int8') {
        field = { type: 'bigint', size: 8, default: default_number };

      // NUMERIC
      } else if (type === 'numeric' || type === 'decimal' || type === 'number') {
        field = { type: 'numeric', size: size, scale: scale, default: default_number };

      // CURRENCY
      } else if (type === 'currency') {
        field = { type: 'numeric', size: 12, scale: 4, default: default_number };

      // BOOLEAN
      } else if (type === 'bool' || type === 'boolean') {
        field = { type: 'boolean', default: default_boolean };

      // DATETIME
      } else if (type === 'datetime') {
        field = { type: 'timestamp with time zone', default: default_datetime };

      // DATE
      } else if (type === 'date') {
        field = { type: 'timestamp with time zone', default: default_date };

      // SERIAL
      } else if (type === 'smallserial') {
        field = { type: 'smallserial', default: qp.noop };
      } else if (type === 'serial') {
        field = { type: 'serial', default: qp.noop };
      } else if (type === 'bigserial') {
        field = { type: 'bigserial', default: qp.noop };

      } else {
        field = { type: '', default: qp.noop };

      }
      return qp.options(field, options);
    },

    primary: function() {
      return this.field('integer', { primary: true, managed: true });
    },

    primary_key: function() {
      return this.field('integer', { primary_key: true, sequence: true, managed: false });
    },

    foreign: function(table) {
      return this.field('integer', { foreign: true, table: table });
    },

    foreign_ids: function(table) {
      return this.field('integer', { foreign: true, table: table, array: true });
    },

    unique: function() {
      return this.field('integer', { unique: true, managed: false });
    },

    created: function() {
      return this.field('datetime', { managed: true });
    },

    modified: function() {
      return this.field('datetime', { managed: true });
    }

  });

});
{'fraction_non_alphanumeric': 0.17470920188874814, 'fraction_numerical': 0.0048370378901301395, 'mean_word_length': 2.389539422326308, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '2031783', 'n_tokens_mistral': 2706, 'n_tokens_neox': 2597, 'n_words': 704}