abstract struct DynFork::Extra

Overview

Methods of additional abstraction.

Additional validation - It is supposed to be use to additional validation of fields.
Indexing - For set up and start indexing.
Hooks - Methods that are called at different stages when accessing the database.

Direct Known Subclasses

Defined in:

dynfork/extra.cr

Constructors

Class Method Summary

Instance Method Summary

Constructor Detail

def self.new #

[View source]

Class Method Detail

def self.indexing : Nil #

For set up and start indexing.

NOTE How to use, see example.

NOTE For more details, please check the official documentation.

WARNING Runs automatically during Model migration.

Example:

@[DynFork::Meta(service_name: "Accounts")]
struct User < DynFork::Model
  getter username = DynFork::Fields::TextField.new(unique: true)
  getter email = DynFork::Fields::EmailField.new(unique: true)

  def self.indexing
    self.create_index(
      keys: {
        "username": 1,
      },
      options: {
        unique: true,
        name:   "usernameIdx",
      }
    )
  end
end

[View source]

Instance Method Detail

def add_validation : Hash(String, String) #

It is supposed to be use to additional validation of fields.

NOTE How to use, see example.

WARNING The method is called automatically when checking or saving the Model.

Example:

@[DynFork::Meta(service_name: "Accounts")]
struct User < DynFork::Model
  getter username = DynFork::Fields::TextField.new
  getter password = DynFork::Fields::PasswordField.new
  getter confirm_password = DynFork::Fields::PasswordField.new(
    "ignored": true
  )

  private def add_validation : Hash(String, String)
    error_map = Hash(String, String).new
    # Get clean data.
    password : String? = @password.value?
    confirm_password : String? = @confirm_password.value?
    # Fields validation.
    if password != confirm_password
      error_map["confirm_password"] = "Password confirmation does not match."
    end
    error_map
  end
end

[View source]
def initialize #

[View source]
def post_create : Nil #

Called after a new document has been created in the database.

NOTE How to use, see example.

WARNING The method is called automatically.


[View source]
def post_delete : Nil #

Called after an existing document in the database has been deleted.

NOTE How to use, see example.

WARNING The method is called automatically.


[View source]
def post_update : Nil #

Called after an existing document in the database is updated.

NOTE How to use, see example.

WARNING The method is called automatically.


[View source]
def pre_create : Nil #

Called before a new document is created in the database.

NOTE How to use, see example.

WARNING The method is called automatically.


[View source]
def pre_delete : Nil #

Called before deleting an existing document in the database.

NOTE How to use, see example.

WARNING The method is called automatically.


[View source]
def pre_update : Nil #

Called before updating an existing document in the database.

NOTE How to use, see example.

WARNING The method is called automatically.


[View source]