<commit_before># Object-relational mapping

*Tools for working with [Waterline](https://github.com/balderdashy/waterline).*

    config = require 'config'
    pkgman = require 'pkgman'

    Waterline = null

    collections = {}
    connections = {}

    waterline = null

    exports.pkgmanRegister = (registrar) ->

#### Implements hook `shrubCorePreBootstrap`.

      registrar.registerHook 'shrubCorePreBootstrap', ->

        Waterline = require 'waterline'

#### Implements hook `shrubCoreBootstrapMiddleware`.

      registrar.registerHook 'shrubCoreBootstrapMiddleware', ->

        waterline = new Waterline()

        label: 'Bootstrap ORM'
        middleware: [

          (next) -> exports.initialize next

        ]

#### Implements hook `shrubGruntConfig`.

      registrar.registerHook 'shrubGruntConfig', (gruntConfig) ->

        gruntConfig.copyAppFiles "#{__dirname}/app", 'shrub-orm'

        gruntConfig.registerTask 'build:shrub-orm', [
          'newer:copy:shrub-orm'
        ]

        gruntConfig.registerTask 'build', ['build:shrub-orm']

#### Implements hook `shrubConfigServer`.

      registrar.registerHook 'shrubConfigServer', ->

The available adapters. This is a list of module names. We provide sails-redis
by default.

        adapters: [
          'sails-redis'
        ]

The available connnections. This is a keyed list of available connections
which are defined as an adapter and the configuration for the adapter. The
entries in the `adapters` list are available for use here. We provide a `shrub`
connection by default, which uses the sails-redis adapter with defaults.

        connections:

          shrub:

            adapter: 'sails-redis'
            port: 6379
            host: 'localhost'
            password: <PASSWORD>
            database: null

#### Implements hook `shrubReplContext`.

Provide ORM to the REPL context.

      registrar.registerHook 'shrubReplContext', (context) -> context.orm = exports

## initialize

* (Function) `fn` - Nodeback called when initialization completes.

*Spin up Waterline with our configuration.*

    exports.initialize = (fn) ->

      config_ = config.get 'packageSettings:shrub-orm'

      waterlineConfig = adapters: {}, connections: {}

`require` all the adapter modules.

      for adapter in config_.adapters
        waterlineConfig.adapters[adapter] = require adapter

      waterlineConfig.connections = config_.connections

#### Invoke hook `shrubOrmCollections`.

      collections_ = {}
      for collectionList in pkgman.invokeFlat 'shrubOrmCollections', waterline
        for identity, collection of collectionList

Set collection defaults.

          collection.connection ?= 'shrub'
          collection.identity ?= identity
          collections_[collection.identity] = collection

## Collection#instantiate.

* (Object) `values` - An object with values to populate the model properties.

*Instantiate a model with defaults supplied.*

          collection.instantiate = (values = {}) ->

            for key, value of @attributes
              continue unless value.defaultsTo?

Set any model defaults.

              values[key] ?= if 'function' is typeof value.defaultsTo
                value.defaultsTo.call values
              else
                JSON.parse JSON.stringify value.defaultsTo

Reach into Waterline a bit, hackish but they simply don't provide us with a
sane API for this.

            new @_model @_schema.cleanValues @_transformer.serialize values

#### Invoke hook `shrubOrmCollectionsAlter`.

      pkgman.invoke 'shrubOrmCollectionsAlter', collections_, waterline

Load the collections into Waterline.

      waterlineConfig.collections = for i, collection of collections_
        Waterline.Collection.extend collection

      waterline.initialize waterlineConfig, (error, data) ->
        return fn error if error?
<commit_msg>Waterline API changed? Oh well.<commit_after># Object-relational mapping

*Tools for working with [Waterline](https://github.com/balderdashy/waterline).*

    config = require 'config'
    pkgman = require 'pkgman'

    Waterline = null

    collections = {}
    connections = {}

    waterline = null

    exports.pkgmanRegister = (registrar) ->

#### Implements hook `shrubCorePreBootstrap`.

      registrar.registerHook 'shrubCorePreBootstrap', ->

        Waterline = require 'waterline'

#### Implements hook `shrubCoreBootstrapMiddleware`.

      registrar.registerHook 'shrubCoreBootstrapMiddleware', ->

        waterline = new Waterline()

        label: 'Bootstrap ORM'
        middleware: [

          (next) -> exports.initialize next

        ]

#### Implements hook `shrubGruntConfig`.

      registrar.registerHook 'shrubGruntConfig', (gruntConfig) ->

        gruntConfig.copyAppFiles "#{__dirname}/app", 'shrub-orm'

        gruntConfig.registerTask 'build:shrub-orm', [
          'newer:copy:shrub-orm'
        ]

        gruntConfig.registerTask 'build', ['build:shrub-orm']

#### Implements hook `shrubConfigServer`.

      registrar.registerHook 'shrubConfigServer', ->

The available adapters. This is a list of module names. We provide sails-redis
by default.

        adapters: [
          'sails-redis'
        ]

The available connnections. This is a keyed list of available connections
which are defined as an adapter and the configuration for the adapter. The
entries in the `adapters` list are available for use here. We provide a `shrub`
connection by default, which uses the sails-redis adapter with defaults.

        connections:

          shrub:

            adapter: 'sails-redis'
            port: 6379
            host: 'localhost'
            password: <PASSWORD>
            database: null

#### Implements hook `shrubReplContext`.

Provide ORM to the REPL context.

      registrar.registerHook 'shrubReplContext', (context) -> context.orm = exports

## initialize

* (Function) `fn` - Nodeback called when initialization completes.

*Spin up Waterline with our configuration.*

    exports.initialize = (fn) ->

      config_ = config.get 'packageSettings:shrub-orm'

      waterlineConfig = adapters: {}, connections: {}

`require` all the adapter modules.

      for adapter in config_.adapters
        waterlineConfig.adapters[adapter] = require adapter

      waterlineConfig.connections = config_.connections

#### Invoke hook `shrubOrmCollections`.

      collections_ = {}
      for collectionList in pkgman.invokeFlat 'shrubOrmCollections', waterline
        for identity, collection of collectionList

Set collection defaults.

          collection.connection ?= 'shrub'
          collection.identity ?= identity
          collections_[collection.identity] = collection

## Collection#instantiate.

* (Object) `values` - An object with values to populate the model properties.

*Instantiate a model with defaults supplied.*

          collection.instantiate = (values = {}) ->

            for key, value of @attributes
              continue unless value.defaultsTo?

Set any model defaults.

              values[key] ?= if 'function' is typeof value.defaultsTo
                value.defaultsTo.call values
              else
                JSON.parse JSON.stringify value.defaultsTo

Reach into Waterline a bit, hackish but they simply don't provide us with a
sane API for this.

            new @_model @_schema.cleanValues @_transformer.serialize values

#### Invoke hook `shrubOrmCollectionsAlter`.

      pkgman.invoke 'shrubOrmCollectionsAlter', collections_, waterline

Load the collections into Waterline.

      for i, collection of collections_
        waterline.loadCollection Waterline.Collection.extend collection

      waterline.initialize waterlineConfig, (error, data) ->
        return fn error if error?

{'fraction_non_alphanumeric': 0.08795040681906238, 'fraction_numerical': 0.0010331912695337724, 'mean_word_length': 2.862344139650873, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 25, 'https://': 2, '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': '1192560', 'n_tokens_mistral': 2127, 'n_tokens_neox': 2026, 'n_words': 628}