All Projects → acutario → decoratex

acutario / decoratex

Licence: other
No description or website provided.

Programming Languages

elixir
2628 projects

Projects that are alternatives of or similar to decoratex

ddal
DDAL(Distributed Data Access Layer) is a simple solution to access database shard.
Stars: ✭ 33 (-2.94%)
Mutual labels:  schema
migrana
Migrana is a Datomic migration tool that gives you the control over how your Datomic database evolves.
Stars: ✭ 22 (-35.29%)
Mutual labels:  schema
godmt
Tool that can parse Go files into an abstract syntax tree and translate it to several programming languages.
Stars: ✭ 42 (+23.53%)
Mutual labels:  schema
krab
Krab is a migration and automation tool for PostgreSQL based on HCL syntax
Stars: ✭ 15 (-55.88%)
Mutual labels:  schema
proto2gql
The project has been migrated to https://github.com/EGT-Ukraine/go2gql.
Stars: ✭ 21 (-38.24%)
Mutual labels:  schema
react-aztec
🔥 Material UI based dynamic form component for React using JSON
Stars: ✭ 37 (+8.82%)
Mutual labels:  schema
avrow
Avrow is a pure Rust implementation of the avro specification https://avro.apache.org/docs/current/spec.html with Serde support.
Stars: ✭ 27 (-20.59%)
Mutual labels:  schema
schema2ldif
Schema 2 ldif : tool to convert .schema to .ldif files and mange them live into an openldap server
Stars: ✭ 14 (-58.82%)
Mutual labels:  schema
volder
volder is powerful Object schema validation lets you describe your data using a simple and readable schema and transform a value to match the requirements
Stars: ✭ 106 (+211.76%)
Mutual labels:  schema
schemaglue
Naturally breaks down your monolithic graphql schema into bits and pieces and then glue them back together.
Stars: ✭ 117 (+244.12%)
Mutual labels:  schema
EsriRESTScraper
A Python class that scrapes ESRI Rest Endpoints and exports data to a geodatabase
Stars: ✭ 43 (+26.47%)
Mutual labels:  schema
generaptr
Generaptr is a node package that helps when starting up a project by generating boilerplate code for Express api.
Stars: ✭ 16 (-52.94%)
Mutual labels:  schema
felicity
Javascript object constructors and sample data based on Joi schema.
Stars: ✭ 107 (+214.71%)
Mutual labels:  schema
SpacingItemDecoration
ItemDecoration for RecyclerView that allows you to set spacing between and around list items in flexible way.
Stars: ✭ 83 (+144.12%)
Mutual labels:  decoration
soap-typescript
SOAP decorators for creating wsdl's and annotating services to provide metadata for node-soap
Stars: ✭ 20 (-41.18%)
Mutual labels:  schema
typescript-to-json-schema
Generate JSON schema from your Typescript sources
Stars: ✭ 54 (+58.82%)
Mutual labels:  schema
schemarkdown
The core library for generate Markdown document from database schema.
Stars: ✭ 4 (-88.24%)
Mutual labels:  schema
mapper
Map tarantool tuples to php objects.
Stars: ✭ 68 (+100%)
Mutual labels:  schema
hasura-sdk
Hasura Schema & Metadata Typescript SDK
Stars: ✭ 21 (-38.24%)
Mutual labels:  schema
mongodb-schema
Infer a probabilistic schema for a MongoDB collection.
Stars: ✭ 106 (+211.76%)
Mutual labels:  schema

Decoratex

Travis Hex.pm

Decoratex provides an easy way to add calculated data to your Ecto model structs.

Requirements

  • Ecto 2.0 or higher

What does this package do?

Maybe you have been in some situations where you need some related data of a model that is not straight stored with it's attributes and it requires a complex logic to calculate their value that can't be solved with a query. Maybe you will need this data in multiple points of the instace life cicle and you want the data available in a standard way instead of using an external module function each time you need it's value.

In this cases, this is what decoratex can do for you:

  • Add virtual fields to the model schema to let you use your model like the same model struct with these new fields.

  • Provide a function to load data in all or some of these fields whenever you want.

Installation

The package can be installed as simply as adding decoratex to your list of dependencies in mix.exs:

  def deps do
    [{:decoratex, "~> 1.1.0"}]
  end

Usage

  1. Add use Decoratex.Schema to your models.
  2. Set the decorate fields inside a block of decorations function.
  3. Declare each field with decorate_field name, type, function, options.
    • Name of the virtual field.
    • Type of the virtual field.
    • Function to calculate the value of the virtual field. Always receives a struct model as first param.
    • Default options for the function (arity 2) in case you need to use diferent options in each decoration.
  4. Add decorations() inside schema definition.
  5. Use Decoratex.decorate function with your model.
defmodule Post do
  use Ecto.Schema
  use Decoratex.Schema

  decorations do
    decorate_field :happy_comments_count, :integer, &PostHelper.count_happy_comments/1
    decorate_field :troll_comments_count, :integer, &PostHelper.count_troll_comments/1
    decorate_field :mention_comments_count, :integer, &PostHelper.count_mention_comments/2, ""
    ...
  end

  schema "posts" do
    has_many :comments, Comment, on_delete: :delete_all

    field :title, :string
    field :body, :string

    timestamps()
    decorations()
  end
end

The decorations definition needs to be placed before schema definition, and then, you should add decorations() inside the schema block. This will automatically add the virtual fields to your model.

Finally, you can use the decorate function of your model module to populate the fields that you need with the function associated to them.

post = Post
|> Repo.get(1)
|> Repo.preload(:comments))

# Decorate all fields
|> Decoratex.decorate

# Decorate one field with an atom
|> Decoratex.decorate(:happy_comments_count)

# Decorate some fields with a list
|> Decoratex.decorate([:happy_comments_count, ...])

# Decorate all fields except one with except key and an atom
|> Decoratex.decorate(except: :happy_comments_count)

# Decorate all fields except some with except key and a list
|> Decoratex.decorate(except: [:happy_comments_count, ...])

post.happy_comments_count
234

Decorate with options

When you need to send some options to the decoration functions, you can define a function with arity 2, and set a default value in declaration. The default options value is mandatory for default decorations:

decorate_field :mention_comments_count, :integer, &PostHelper.count_mention_comments/2, ""

Then, you can pass the options value when the struct is decorated

|> Decoratex.decorate(count_mention_comments: user.nickname)

You can use a keyword list for a complex logic, but you need to care about how to manage options in the decoration function (always with arity/2), and the default options in the configurtion.

decorate_field :censured_comments, {:array, Comment}, &PostHelper.censured_comments/2, pattern: "frack", replace: "*"
|> Decoratex.decorate(censured_comments: [pattern: list_of_words, replace: "*"])

And you can mix simple and decorations with options with a list:

|> Decoratex.decorate([:happy_comments_count, censured_comments: [pattern: list_of_words, replace: "*"]])
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].