<commit_before>//  ██╗    ██╗ █████╗ ████████╗███████╗██████╗ ██╗     ██╗███╗   ██╗███████╗
//  ██║    ██║██╔══██╗╚══██╔══╝██╔════╝██╔══██╗██║     ██║████╗  ██║██╔════╝
//  ██║ █╗ ██║███████║   ██║   █████╗  ██████╔╝██║     ██║██╔██╗ ██║█████╗
//  ██║███╗██║██╔══██║   ██║   ██╔══╝  ██╔══██╗██║     ██║██║╚██╗██║██╔══╝
//  ╚███╔███╔╝██║  ██║   ██║   ███████╗██║  ██║███████╗██║██║ ╚████║███████╗
//   ╚══╝╚══╝ ╚═╝  ╚═╝   ╚═╝   ╚══════╝╚═╝  ╚═╝╚══════╝╚═╝╚═╝  ╚═══╝╚══════╝
//

var _ = require('@sailshq/lodash');
var async = require('async');
var Schema = require('waterline-schema');
var DatastoreBuilder = require('./waterline/utils/system/datastore-builder');
var CollectionBuilder = require('./waterline/utils/system/collection-builder');


// Build up an object to be returned
var Waterline = module.exports = function ORM() {
  // Store the raw model objects that are passed in.
  var RAW_MODELS = [];

  // Hold a map of the instantaited and active datastores and models.
  var modelMap = {};
  var datastoreMap = {};

  // Hold the context object to be passed into the collection. This is a stop
  // gap to prevent re-writing all the collection query stuff.
  var context = {
    collections: modelMap,
    connections:  datastoreMap
  };

  // Build up an ORM handler
  var ORM = {};


  //  ╦  ╔═╗╔═╗╔╦╗  ┌─┐┌─┐┬  ┬  ┌─┐┌─┐┌┬┐┬┌─┐┌┐┌┌─┐
  //  ║  ║ ║╠═╣ ║║  │  │ ││  │  ├┤ │   │ ││ ││││└─┐
  //  ╩═╝╚═╝╩ ╩═╩╝  └─┘└─┘┴─┘┴─┘└─┘└─┘ ┴ ┴└─┘┘└┘└─┘
  // Sets the collection (model) as active.
  ORM.loadCollection = function loadCollection(model) {
    RAW_MODELS.push(model);
  };


  //  ╦╔╗╔╦╔╦╗╦╔═╗╦  ╦╔═╗╔═╗
  //  ║║║║║ ║ ║╠═╣║  ║╔═╝║╣
  //  ╩╝╚╝╩ ╩ ╩╩ ╩╩═╝╩╚═╝╚═╝
  // Starts the ORM and setups active datastores
  ORM.initialize = function initialize(options, cb) {
    // Ensure a config object is passed in containing adapters
    if (_.isUndefined(options) || !_.keys(options).length) {
      throw new Error('Usage Error: function(options, callback)');
    }

    // Validate that adapters are present
    if (_.isUndefined(options.adapters) || !_.isPlainObject(options.adapters)) {
      throw new Error('Options object must contain an adapters object');
    }


    if (_.isUndefined(options.connections) || !_.isPlainObject(options.connections)) {
      throw new Error('Options object must contain a connections object');
    }


    // Build up all the connections (datastores) used by the collections
    try {
      datastoreMap = DatastoreBuilder(options.adapters, options.connections);
    } catch (e) {
      return cb(e);
    }

    // Build a schema map
    var internalSchema;
    try {
      internalSchema = new Schema(RAW_MODELS);
    } catch (e) {
      return cb(e);
    }


    // Add any JOIN tables that are needed to the RAW_MODELS
    _.each(internalSchema, function(val, table) {
      if (!val.junctionTable) {
        return;
      }

      RAW_MODELS.push(Waterline.Collection.extend(internalSchema[table]));
    });


    // Initialize each collection by setting and calculated values
    _.each(RAW_MODELS, function setupModel(model) {
      // Set the attributes and schema values using the normalized versions from
      // Waterline-Schema where everything has already been processed.
      var schemaVersion = internalSchema[model.prototype.identity];

      // Set normalized values from the schema version on the collection
      model.prototype.identity = schemaVersion.identity;
      model.prototype.tableName = schemaVersion.tableName;
      model.prototype.connection = schemaVersion.connection;
      model.prototype.primaryKey = schemaVersion.primaryKey;
      model.prototype.meta = schemaVersion.meta;
      model.prototype.attributes = schemaVersion.attributes;
      model.prototype.schema = schemaVersion.schema;

      // Mixin junctionTable or throughTable if available
      if (_.has(schemaVersion, 'junctionTable')) {
        model.prototype.junctionTable = schemaVersion.junctionTable;
      }

      if (_.has(schemaVersion, 'throughTable')) {
        model.prototype.throughTable = schemaVersion.throughTable;
      }

      var collection = CollectionBuilder(model, datastoreMap, context);

      // Store the instantiated collection so it can be used
      // internally to create other records
      modelMap[collection.identity.toLowerCase()] = collection;
    });


    // Register the datastores with the correct adapters.
    // This is async because the `registerConnection` method in the adapters is
    // async.
    async.each(_.keys(datastoreMap), function(item, nextItem) {<commit_msg>ensure hasSchema from wl-schema gets passed down to the collection<commit_after>//  ██╗    ██╗ █████╗ ████████╗███████╗██████╗ ██╗     ██╗███╗   ██╗███████╗
//  ██║    ██║██╔══██╗╚══██╔══╝██╔════╝██╔══██╗██║     ██║████╗  ██║██╔════╝
//  ██║ █╗ ██║███████║   ██║   █████╗  ██████╔╝██║     ██║██╔██╗ ██║█████╗
//  ██║███╗██║██╔══██║   ██║   ██╔══╝  ██╔══██╗██║     ██║██║╚██╗██║██╔══╝
//  ╚███╔███╔╝██║  ██║   ██║   ███████╗██║  ██║███████╗██║██║ ╚████║███████╗
//   ╚══╝╚══╝ ╚═╝  ╚═╝   ╚═╝   ╚══════╝╚═╝  ╚═╝╚══════╝╚═╝╚═╝  ╚═══╝╚══════╝
//

var _ = require('@sailshq/lodash');
var async = require('async');
var Schema = require('waterline-schema');
var DatastoreBuilder = require('./waterline/utils/system/datastore-builder');
var CollectionBuilder = require('./waterline/utils/system/collection-builder');


// Build up an object to be returned
var Waterline = module.exports = function ORM() {
  // Store the raw model objects that are passed in.
  var RAW_MODELS = [];

  // Hold a map of the instantaited and active datastores and models.
  var modelMap = {};
  var datastoreMap = {};

  // Hold the context object to be passed into the collection. This is a stop
  // gap to prevent re-writing all the collection query stuff.
  var context = {
    collections: modelMap,
    connections:  datastoreMap
  };

  // Build up an ORM handler
  var ORM = {};


  //  ╦  ╔═╗╔═╗╔╦╗  ┌─┐┌─┐┬  ┬  ┌─┐┌─┐┌┬┐┬┌─┐┌┐┌┌─┐
  //  ║  ║ ║╠═╣ ║║  │  │ ││  │  ├┤ │   │ ││ ││││└─┐
  //  ╩═╝╚═╝╩ ╩═╩╝  └─┘└─┘┴─┘┴─┘└─┘└─┘ ┴ ┴└─┘┘└┘└─┘
  // Sets the collection (model) as active.
  ORM.loadCollection = function loadCollection(model) {
    RAW_MODELS.push(model);
  };


  //  ╦╔╗╔╦╔╦╗╦╔═╗╦  ╦╔═╗╔═╗
  //  ║║║║║ ║ ║╠═╣║  ║╔═╝║╣
  //  ╩╝╚╝╩ ╩ ╩╩ ╩╩═╝╩╚═╝╚═╝
  // Starts the ORM and setups active datastores
  ORM.initialize = function initialize(options, cb) {
    // Ensure a config object is passed in containing adapters
    if (_.isUndefined(options) || !_.keys(options).length) {
      throw new Error('Usage Error: function(options, callback)');
    }

    // Validate that adapters are present
    if (_.isUndefined(options.adapters) || !_.isPlainObject(options.adapters)) {
      throw new Error('Options object must contain an adapters object');
    }


    if (_.isUndefined(options.connections) || !_.isPlainObject(options.connections)) {
      throw new Error('Options object must contain a connections object');
    }


    // Build up all the connections (datastores) used by the collections
    try {
      datastoreMap = DatastoreBuilder(options.adapters, options.connections);
    } catch (e) {
      return cb(e);
    }

    // Build a schema map
    var internalSchema;
    try {
      internalSchema = new Schema(RAW_MODELS);
    } catch (e) {
      return cb(e);
    }


    // Add any JOIN tables that are needed to the RAW_MODELS
    _.each(internalSchema, function(val, table) {
      if (!val.junctionTable) {
        return;
      }

      RAW_MODELS.push(Waterline.Collection.extend(internalSchema[table]));
    });


    // Initialize each collection by setting and calculated values
    _.each(RAW_MODELS, function setupModel(model) {
      // Set the attributes and schema values using the normalized versions from
      // Waterline-Schema where everything has already been processed.
      var schemaVersion = internalSchema[model.prototype.identity];

      // Set normalized values from the schema version on the collection
      model.prototype.identity = schemaVersion.identity;
      model.prototype.tableName = schemaVersion.tableName;
      model.prototype.connection = schemaVersion.connection;
      model.prototype.primaryKey = schemaVersion.primaryKey;
      model.prototype.meta = schemaVersion.meta;
      model.prototype.attributes = schemaVersion.attributes;
      model.prototype.schema = schemaVersion.schema;
      model.prototype.hasSchema = schemaVersion.hasSchema;

      // Mixin junctionTable or throughTable if available
      if (_.has(schemaVersion, 'junctionTable')) {
        model.prototype.junctionTable = schemaVersion.junctionTable;
      }

      if (_.has(schemaVersion, 'throughTable')) {
        model.prototype.throughTable = schemaVersion.throughTable;
      }

      var collection = CollectionBuilder(model, datastoreMap, context);

      // Store the instantiated collection so it can be used
      // internally to create other records
      modelMap[collection.identity.toLowerCase()] = collection;
    });


    // Register the datastores with the correct adapters.
    // This is async because the `registerConnection` method in the adapters is
    // async.
    async.each(_.keys(datastoreMap), function(item, nextItem) {
{'fraction_non_alphanumeric': 0.1981397361020982, 'fraction_numerical': 0.0, 'mean_word_length': 3.093404161133245, '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': '3778000', 'n_tokens_mistral': 3427, 'n_tokens_neox': 3492, 'n_words': 793}