dynfork v0.8.25

ORM-like API MongoDB for Crystal language.

Logo

DynFork

ORM-like API MongoDB for Crystal language.

Build Status Docs Crysta GitHub release GitHub license GitHub repository

DynFork is built around Cryomongo.
For simulate relationship Many-to-One and Many-to-Many,
a simplified alternative (Types of selective fields with dynamic addition of elements) is used.
The project is more concentrated for web development or for applications with a graphic interface.

Status Project

Compatible with MongoDB 3.6+. Tested against: 7.0, 8.0
For more information see Cryomongo.

Documentation

Online browsable documentation is available at https://kebasyaty.github.io/dynfork/.

Requirements

View the list of requirements.

Installation

  1. Install MongoDB (if not installed):
    Fedora Debian Ubuntu Linux Mint

  2. Install additional libraries (if not installed):
    Fedora Debian Ubuntu Linux Mint

  3. Add the dependency to your shard.yml:

    dependencies:
      dynfork:
        github: kebasyaty/dynfork
    
  4. Run shards install

Usage

It is recommended to look at examples here.

require "dynfork"

# Create your model.
@[DynFork::Meta(service_name: "Accounts")]
struct User < DynFork::Model
  getter username = DynFork::Fields::TextField.new
  getter email = DynFork::Fields::EmailField.new
  getter birthday = DynFork::Fields::DateField.new
end

# Initialize locale.
# https://github.com/crystal-i18n/i18n
I18n.config.loaders << I18n::Loader::YAML.new("config/locales")
I18n.config.default_locale = :en
I18n.init

# Run migration.
# https://elbywan.github.io/cryomongo/Mongo/Client.html
DynFork::Migration::Monitor.new(
  database_name: "test",
).migrat

# Create a user.
user = User.new

# Add user data.
user.username.value = "username"
user.email.value = "user@noreaply.net"
user.birthday.value = "1970-01-01"

# Save user.
# Hint: print_err - convenient for development.
user.print_err unless user.save

# Print user details.
puts "User details:"
if id = user.object_id
  pp User.find_one_to_hash({_id: id})
end

puts "Documwnt count: #{User.estimated_document_count}"

puts "Delete user."
user.delete

puts "Documwnt count: #{User.count_documents}"

See more examples here.

Model Parameters

See the documentation here.

( only service_name is a required parameter )
Parameter Default Description
service_name no Examples: Accounts | Smartphones | Washing machines | etc ...
fixture_name no The name of the fixture in the config/fixtures directory (without extension).
Examples: SiteSettings | AppSettings | etc ...
db_query_docs_limit 1000 limiting query results.
migrat_model? true Set to false if you do not need to migrate the Model to the database.
This can be use to validate a web forms - Search form, Contact form, etc.
create_doc? true Can a Model create new documents in a collection?
Set to false if you only need one document in the collection and the Model is using a fixture.
update_doc? true Can a Model update documents in a collection?
delete_doc? true Can a Model remove documents from a collection?

Example:

  @[DynFork::Meta(
    service_name: "ServiceName",
    fixture_name: "FixtureName",
    db_query_docs_limit: 1000,
    migrat_model?: true,
    create_doc?: true,
    update_doc?: true,
    delete_doc?: true,
  )]
  struct User < DynFork::Model
    getter username = DynFork::Fields::TextField.new
    getter birthday = DynFork::Fields::DateField.new
  end

Methods for developers

Links to documentation.

Model:

  • full_model_name
  • meta
  • subclasses
  • object_id

Extra:

  • add_validation
  • indexing
  • pre_create
  • post_create
  • pre_update
  • post_update
  • pre_delete
  • post_delete

Migration:

  • migrat

QPaladins:

  • valid?
  • save
  • print_err
  • delete
  • verify_password
  • update_password

QCommons:

  • aggregate
  • collection_name
  • count_documents
  • distinct
  • estimated_document_count
  • stats
  • find_many_to_hash_list
  • find_many_to_json
  • find_one_to_hash
  • find_one_to_instance
  • find_one_to_json
  • find_one_and_delete
  • unit_manager
  • delete_many
  • delete_one
  • bulk
  • bulk_write
  • create_index
  • create_indexes
  • drop_index
  • drop_indexes
  • list_indexes

F64Field:

  • finance_plus
  • finance_minus
  • finance_divide
  • finance_multiply

For currencies, use the Money shard.

Contributing

  1. Fork it (https://github.com/kebasyaty/dynfork/fork)
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request

Contributors

  • kebasyaty Gennady Kostyunin - creator and maintainer

Changelog

View the change history.

License

This project is licensed under the MIT.



Repository

dynfork

Owner
Statistic
  • 3
  • 0
  • 0
  • 1
  • 5
  • 1 day ago
  • October 16, 2023
License

MIT License

Links
Synced at

Mon, 20 Jan 2025 03:13:38 GMT

Languages