Sha256: 4867537bb1367f684f9074cbb9a6aaf5ec29efddc8e5c40c5bcd002dee475e17

Contents?: true

Size: 1.56 KB

Versions: 22

Compression:

Stored size: 1.56 KB

Contents

# Copyright 2021 Google LLC
#
# Use of this source code is governed by an MIT-style
# license that can be found in the LICENSE file or at
# https://opensource.org/licenses/MIT.

require "io/console"
require_relative "../config/environment"
require_relative "models/singer"
require_relative "models/album"

class Application
  def self.run
    # Creating a single record without an explicit transaction will automatically save it to the database.
    # It is not recommended to call Entity.create repeatedly to insert multiple records, as each call will
    # use a separate Spanner transaction. Instead multiple records should be created by passing an array of
    # entities to the Entity.create method.
    singer = Singer.create first_name: "Dave", last_name: "Allison"
    puts ""
    puts "Created singer #{singer.first_name} #{singer.last_name} with id #{singer.id}"
    puts ""

    # Creating multiple records without an explicit transaction will automatically save all the records using
    # one Spanner transaction and return the ids of the created records. This is the recommended way to create
    # a batch of entities.
    singers = Singer.create [
      { first_name: "Alice", last_name: "Wendelson" },
      { first_name: "Nick", last_name: "Rainbow" },
      { first_name: "Elena", last_name: "Quick" }
    ]
    puts "Created a batch of #{singers.length} singers:"
    singers.each do |s|
      puts "  Created singer #{s.first_name} #{s.last_name} with id #{s.id}"
    end

    puts ""
    puts "Press any key to end the application"
    STDIN.getch
  end
end

Application.run

Version data entries

22 entries across 22 versions & 1 rubygems

Version Path
activerecord-spanner-adapter-1.8.0 examples/snippets/create-records/application.rb
activerecord-spanner-adapter-1.6.3 examples/snippets/create-records/application.rb
activerecord-spanner-adapter-1.6.2 examples/snippets/create-records/application.rb
activerecord-spanner-adapter-1.6.1 examples/snippets/create-records/application.rb
activerecord-spanner-adapter-1.6.0 examples/snippets/create-records/application.rb
activerecord-spanner-adapter-1.5.1 examples/snippets/create-records/application.rb
activerecord-spanner-adapter-1.5.0 examples/snippets/create-records/application.rb
activerecord-spanner-adapter-1.4.4 examples/snippets/create-records/application.rb
activerecord-spanner-adapter-1.4.3 examples/snippets/create-records/application.rb
activerecord-spanner-adapter-1.4.2 examples/snippets/create-records/application.rb
activerecord-spanner-adapter-1.4.1 examples/snippets/create-records/application.rb
activerecord-spanner-adapter-1.4.0 examples/snippets/create-records/application.rb
activerecord-spanner-adapter-1.3.1 examples/snippets/create-records/application.rb
activerecord-spanner-adapter-1.2.2 examples/snippets/create-records/application.rb
activerecord-spanner-adapter-1.2.1 examples/snippets/create-records/application.rb
activerecord-spanner-adapter-1.2.0 examples/snippets/create-records/application.rb
activerecord-spanner-adapter-1.1.0 examples/snippets/create-records/application.rb
activerecord-spanner-adapter-1.0.1 examples/snippets/create-records/application.rb
activerecord-spanner-adapter-1.0.0 examples/snippets/create-records/application.rb
activerecord-spanner-adapter-0.7.0 examples/snippets/create-records/application.rb