Sha256: b1ea7e59cc6a67ce2aee6777828a4b313290a01edd4a68717a0a5841a286a6dd

Contents?: true

Size: 886 Bytes

Versions: 7

Compression:

Stored size: 886 Bytes

Contents

# frozen_string_literal: true

module ROM
  module Mongo
    module Commands
      # Create command
      #
      # Inserts a new tuple into a relation
      #
      # @abstract
      class Create < ROM::Commands::Create
        include ROM::Mongo::Commands::Helper

        adapter :mongo

        # TODO: always returns :one, even more then one documents have created
        # Research how to display :many needed

        # Passes tuple to relation for insertion
        #
        # @param attributes [Hash]
        #
        # @return [Array<Hash>]
        #
        # @api public
        def execute(*attributes)
          ids = dataset.insert(*attributes)
          return [] if ids.empty?

          attributes.each_with_object([]).with_index do |(attribute, arr), index|
            arr << { _id: ids[index], **attribute }
          end
        end
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
rom-mongodb-0.1.6 lib/rom/mongo/commands/create.rb
rom-mongodb-0.1.5 lib/rom/mongo/commands/create.rb
rom-mongodb-0.1.4 lib/rom/mongo/commands/create.rb
rom-mongodb-0.1.3 lib/rom/mongo/commands/create.rb
rom-mongodb-0.1.2 lib/rom/mongo/commands/create.rb
rom-mongodb-0.1.1 lib/rom/mongo/commands/create.rb
rom-mongodb-0.1.0 lib/rom/mongo/commands/create.rb