Module: Dynamoid::Fields::ClassMethods

Defined in:
lib/dynamoid/fields.rb

Instance Method Summary collapse

Instance Method Details

#field(name, type = :string, options = {}) ⇒ Object

Specify a field for a document.

class User
  include Dynamoid::Document

  field :last_name
  field :age, :integer
  field :last_sign_in, :datetime
end

Its type determines how it is coerced when read in and out of the data store. You can specify string, integer, number, set, array, map, datetime, date, serialized, raw, boolean and binary or specify a class or module that defines a serialization strategy.

By default field type is string.

Set can store elements of the same type only (it's a limitation of DynamoDB itself). If a set should store elements only of some particular type then of option should be specified:

field :hobbies, :set, of: :string

Only string, integer, number, date, datetime and serialized element types are supported.

Element type can have own options - they should be specified in the form of Hash:

field :hobbies, :set, of: { serialized: { serializer: JSON } }

Array can contain element of different types but if supports the same of option to convert all the provided elements to the declared type.

field :rates, :array, of: :number

By default date and datetime fields are stored as integer values. The format can be changed to string with option store_as_string:

field :published_on, :datetime, store_as_string: true

Boolean field by default is stored as a string t or f. But DynamoDB supports boolean type natively. In order to switch to the native boolean type an option store_as_native_boolean should be specified:

field :active, :boolean, store_as_native_boolean: true

If you specify the serialized type a value will be serialized to string in Yaml format by default. Custom way to serialize value to string can be specified with serializer option. Custom serializer should have dump and load methods.

If you specify a class or module for a field type, Dynamoid will serialize using dynamoid_dump and load using dynamoid_load.

Default field type is string.

A field can have a default value. It's assigned at initializing a model if no value is specified:

field :age, :integer, default: 1

If a defautl value should be recalculated every time it can be specified as a callable object (it should implement a call method e.g. Proc object):

field :date_of_birth, :date, default: -> { Date.today }

For every field Dynamoid creates several methods:

  • getter
  • setter
  • predicate <name>? to check whether a value set
  • <name>_before_type_cast? to get an original field value before it was type casted

It works in the following way:

class User
  include Dynamoid::Document

  field :age, :integer
end

user = User.new
user.age # => nil
user.age? # => false

user.age = 20
user.age? # => true

user.age = '21'
user.age # => 21 - integer
user.age_before_type_cast # => '21' - string

There is also an option alias which allows to use another name for a field:

class User
  include Dynamoid::Document

  field :firstName, :string, alias: :first_name
end

user = User.new(firstName: 'Michael')
user.firstName # Michael
user.first_name # Michael

Parameters:

  • name (Symbol) —

    name of the field

  • type (Symbol, Class, Module) (defaults to: :string) —

    type of the field (optional)

  • options (Hash) (defaults to: {}) —

    any additional options for the field type (optional)

Options Hash (options):

  • :of (Symbol) —
  • :store_as_string (Symbol) —
  • :store_as_native_boolean (Symbol) —
  • :default (Symbol) —
  • :alias (Symbol) —

Since:

  • 0.2.0



150
151
152
153
154
155
156
157
# File 'lib/dynamoid/fields.rb', line 150

def field(name, type = :string, options = {})
  if type == :float
    Dynamoid.logger.warn("Field type :float, which you declared for '#{name}', is deprecated in favor of :number.")
    type = :number
  end

  Dynamoid::Fields::Declare.new(self, name, type, options).call
end

#range(name, type = :string, options = {}) ⇒ Object

Declare a table range key.

class User
  include Dynamoid::Document

  range :last_name
end

By default a range key is a string. In order to use any other type it should be specified as a second argument:

range :age, :integer

Type options can be specified as well:

range :date_of_birth, :date, store_as_string: true

Parameters:

  • name (Symbol) —

    a range key attribute name

  • type (Symbol, Class, Module) (defaults to: :string) —

    a scalar range key type (+:string+, :integer, :number, :date, :datetime, :binary, :serialized, or a custom class or module implementing serialization; defaults to :string)

  • options (Hash) (defaults to: {}) —

    type options (optional)



183
184
185
186
# File 'lib/dynamoid/fields.rb', line 183

def range(name, type = :string, options = {})
  field(name, type, options)
  self.range_key = name
end

#remove_field(field) ⇒ Object

Remove a field declaration

Removes a field from the list of fields and removes all methods generated for that field.

Parameters:

  • field (Symbol) —

    a field name



316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
# File 'lib/dynamoid/fields.rb', line 316

def remove_field(field)
  field = field.to_sym
  attributes.delete(field) || raise('No such field')

  # Dirty API
  undefine_attribute_methods
  define_attribute_methods attributes.keys

  generated_methods.module_eval do
    remove_method field
    remove_method :"#{field}="
    remove_method :"#{field}?"
    remove_method :"#{field}_before_type_cast"
  end
end

#table(options) ⇒ Object

Note:

Provisioning options (+read_capacity+, write_capacity, capacity_mode) and TTL (+expires+) only apply when Dynamoid creates a new DynamoDB table. Dynamoid does not alter existing tables in DynamoDB.

Set table-level properties, table name, and partition key configuration.

Sensible defaults provided by Dynamoid:

  • Table name: inferred from model class name (e.g. users for User class)
  • Partition (hash) key name: :id
  • Partition key type: :string
  • Capacity mode: :provisioned (or configured globally via Dynamoid::Config.capacity_mode)
  • Read/write capacity: 100 / 20 (or global config)
  • Timestamps: generates created_at and updated_at fields (+:datetime+)

Examples

Basic table override:

class Customer
include Dynamoid::Document

table name: :clients, key: :client_id
end

Custom partition key with non-string type:

class User
include Dynamoid::Document

table key: :user_id, key_type: :integer
end

Customizing the default :id key type:

class Product
include Dynamoid::Document

table key_type: :integer
end

On-demand billing and TTL:

class Session
include Dynamoid::Document

table capacity_mode: :on_demand,
      expires: { field: :ttl, after: 86_400 }

field :ttl, :integer
end

Using a table from another AWS account:

class ExternalRecord
include Dynamoid::Document

table arn: 'arn:aws:dynamodb:us-east-1:123456789012:table/records'
end

Parameters:

  • options (Hash) —

    options to override default table settings

Options Hash (options):

  • :name (Symbol, String) —

    name of a table

  • :arn (String) —

    table ARN; allows referring to tables in another AWS account or region (takes precedence over :name)

  • :key (Symbol) —

    name of the partition (hash) key attribute (defaults to :id)

  • :key_type (Symbol, Class, Module) —

    scalar type of the partition key attribute (+:string+, :integer, :number, :date, :datetime, :binary, :serialized, or a custom class or module serialized as a string; defaults to :string)

  • :capacity_mode (Symbol) —

    billing mode (+:provisioned+ or :on_demand)

  • :read_capacity (Integer) —

    read capacity units for provisioned tables

  • :write_capacity (Integer) —

    write capacity units for provisioned tables

  • :timestamps (Boolean) — default: true —

    whether to generate and maintain created_at and updated_at fields

  • :expires (Hash) —

    configure DynamoDB TTL in format { field: <attribute_name>, after: <seconds> }

  • :inheritance_field (Symbol) —

    name of the discriminator attribute used for STI (defaults to :type)

  • :skip_generating_fields (Array<Symbol>) —

    list of implicit fields to not generate (+:id+, :created_at, :updated_at)

Since:

  • 0.4.0



262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
# File 'lib/dynamoid/fields.rb', line 262

def table(options)
  self.options = options

  id_already_declared = true
  timestamps_already_declared = Dynamoid::Config.timestamps

  # a default 'id' column is created when Dynamoid::Document is included
  if !attributes.key?(hash_key) || (hash_key == :id && options[:key_type])
    remove_field :id
    id_already_declared = false

    key_type = options[:key_type] || :string
    field(hash_key, key_type)
    id_already_declared = hash_key == :id
  end

  # The created_at/updated_at fields are declared in the `included` callback first.
  # At that moment the only known setting is `Dynamoid::Config.timestamps`.
  # Now `options[:timestamps]` may override the global setting for a model.
  # So we need to make decision again and declare the fields or rollback thier declaration.
  #
  # Do not replace with `#timestamps_enabled?`.
  if options[:timestamps] && !timestamps_already_declared
    # The fields weren't declared in `included` callback because they are disabled globaly
    field :created_at, :datetime
    field :updated_at, :datetime

    timestamps_already_declared = true
  elsif options[:timestamps] == false && timestamps_already_declared
    # The fields were declared in `included` callback but they are disabled for a table
    remove_field :created_at
    remove_field :updated_at

    timestamps_already_declared = false
  end

  # handle :skip_generating_fields option
  skip_generating_fields = options[:skip_generating_fields] || []
  if id_already_declared && skip_generating_fields.include?(:id)
    remove_field :id
  end
  if timestamps_already_declared && skip_generating_fields.include?(:created_at)
    remove_field :created_at
  end
  if timestamps_already_declared && skip_generating_fields.include?(:updated_at)
    remove_field :updated_at
  end
end