Topics. Query Repo Model Changeset



Similar documents
Phoenix per principianti

Big data is like teenage sex: everyone talks about it, nobody really knows how to do it, everyone thinks everyone else is doing it, so everyone

A Brief Introduction to MySQL

Connect to MySQL or Microsoft SQL Server using R

Cloud Powered Mobile Apps with Azure

database abstraction layer database abstraction layers in PHP Lukas Smith BackendMedia

Lecture 3 ActiveRecord Rails persistence layer

MySQL for Beginners Ed 3

SQL Databases Course. by Applied Technology Research Center. This course provides training for MySQL, Oracle, SQL Server and PostgreSQL databases.

How To Create A Table In Sql (Ahem)

LABSHEET 1: creating a table, primary keys and data types

Ruby on Rails is a web application framework written in Ruby, a dynamically typed programming language The amazing productivity claims of Rails is

Web Development using PHP (WD_PHP) Duration 1.5 months

SQL Injection Vulnerabilities in Desktop Applications

Services. Relational. Databases & JDBC. Today. Relational. Databases SQL JDBC. Next Time. Services. Relational. Databases & JDBC. Today.

Developing a Web Server Platform with SAPI Support for AJAX RPC using JSON

Exposed Database( SQL Server) Error messages Delicious food for Hackers

IRF2000 IWL3000 SRC1000 Application Note - Apps with OSGi - Condition Monitoring with WWH push

Managing User Accounts

This is a training module for Maximo Asset Management V7.1. It demonstrates how to use the E-Audit function.

OpenReports: Users Guide

Database Migration Plugin - Reference Documentation

Monitoring Agent for PostgreSQL Fix Pack 10. Reference IBM

AUTHENTICATION... 2 Step 1:Set up your LDAP server... 2 Step 2: Set up your username... 4 WRITEBACK REPORT... 8 Step 1: Table structures...

SAP Business Objects Business Intelligence platform Document Version: 4.1 Support Package Data Federation Administration Tool Guide

Serious Threat. Targets for Attack. Characterization of Attack. SQL Injection 4/9/2010 COMP On August 17, 2009, the United States Justice

Programming Database lectures for mathema

An extension to DBAPI 2.0 for easier SQL queries

Cyber Security Challenge Australia 2014

WEB DEVELOPMENT COURSE (PHP/ MYSQL)

MOC 20461C: Querying Microsoft SQL Server. Course Overview

TrendWorX32 SQL Query Engine V9.2 Beta III

Detecting (and even preventing) SQL Injection Using the Percona Toolkit and Noinject!

Gravity Forms: Creating a Form

Managing User Accounts

DBX. SQL database extension for Splunk. Siegfried Puchbauer

Discovering SQL. Wiley Publishing, Inc. A HANDS-ON GUIDE FOR BEGINNERS. Alex Kriegel WILEY

Before you may use any database in Limnor, you need to create a database connection for it. Select Project menu, select Databases:

Database Administration with MySQL

DIPLOMA IN WEBDEVELOPMENT

CS346: Database Programming.

Working with the Cognos BI Server Using the Greenplum Database

INTRODUCTION: SQL SERVER ACCESS / LOGIN ACCOUNT INFO:

SQL DATA DEFINITION: KEY CONSTRAINTS. CS121: Introduction to Relational Database Systems Fall 2015 Lecture 7

2014 Software Global Client Conference

Building Dynamic Web 2.0 Websites with Ruby on Rails

Visual Basic. murach's TRAINING & REFERENCE

How-To: MySQL as a linked server in MS SQL Server

KonyOne Server Installer - Linux Release Notes

types, but key declarations and constraints Similar CREATE X commands for other schema ëdrop X name" deletes the created element of beer VARCHARè20è,

G563 Quantitative Paleontology. SQL databases. An introduction. Department of Geological Sciences Indiana University. (c) 2012, P.

HOST EUROPE CLOUD STORAGE REST API DEVELOPER REFERENCE

Database Migration from MySQL to RDM Server

ICT. PHP coding. Universityy. in any

Database Security. Principle of Least Privilege. DBMS Security. IT420: Database Management and Organization. Database Security.

Monitoring PostgreSQL database with Verax NMS

Database Extension 1.5 ez Publish Extension Manual

Scaling with MongoDB. by Michael Schurter Scaling with MongoDB by Michael Schurter - OS Bridge,

Using Temporary Tables to Improve Performance for SQL Data Services

How good can databases deal with Netflow data

Zero Downtime Deployments with Database Migrations. Bob Feldbauer

Advanced Web Development SCOPE OF WEB DEVELOPMENT INDUSTRY

RNPP: Remote NotePad Protocol. Computer Networking Program. Protocol Specifications. October Prepared for


CSC 370 Database Systems Summer 2004 Assignment No. 2

Installation Tutorial Script: The Real Estate Script. 1. Please login to download script. On PHP Classifieds Script web site.

Ruby On Rails. James Reynolds

Financial Data Access with SQL, Excel & VBA

Our Technology.NET Development services by Portweb Inc.

Cloud Powered Mobile Apps with Microsoft Azure

Getting Started with Telerik Data Access. Contents

SUMMARY. e-soft s.r.l.

A Comparison of Database Query Languages: SQL, SPARQL, CQL, DMX

Java and RDBMS. Married with issues. Database constraints

ODBC Client Driver Help Kepware, Inc.

Relational model. Relational model - practice. Relational Database Definitions 9/27/11. Relational model. Relational Database: Terminology

4D v11 SQL Release 3 (11.3) ADDENDUM

Microsoft SQL Database Administrator Certification

Web Service Facade for PHP5. Andreas Meyer, Sebastian Böttner, Stefan Marr

Web Application Disassembly with ODBC Error Messages By David Litchfield Director of Security

Lucid Key Server v2 Installation Documentation.

Writing a Logical Decoding Plug-In. Christophe Pettus PostgreSQL Experts, Inc. FOSDEM 2015

CHEF IN THE CLOUD AND ON THE GROUND

ALERT & Cisco CallManager

Beginning C# 5.0. Databases. Vidya Vrat Agarwal. Second Edition

There and Back Again As Quick As a Flash

Information Technology NVEQ Level 2 Class X IT207-NQ2012-Database Development (Basic) Student s Handbook

MSSQL quick start guide

Infinitel HotSpotWeb User Manual

CS 564: DATABASE MANAGEMENT SYSTEMS

This guide specifies the required and supported system elements for the application.

Table of contents. Reverse-engineers a database to Grails domain classes.

Transcription:

Ecto @emjii

Topics Query Repo Model Changeset

Query

Query Based on LINQ Enabled by macros Database queries only

LINQ Language Integrated Query Released 2007 Supported in C#, F# and VB.net

LINQ from p in Post where p.id == 10 select new {p.title, p.contents}

LINQ from p in Post where p.id == 10 select new {p.title, p.contents} Post.Where(p => p.id == 10).Select(p => new {p.title, p.contents});

Query Based on LINQ Enabled by macros Database queries only

Query syntax from p in Post, where: "elixir" in p.tags, select: p

Query syntax from p in Post, where: "elixir" in p.tags, select: p

Query syntax from p in Post, where: "elixir" in p.tags, select: p

Query syntax from p in Post, where: "elixir" in p.tags, select: p

Interpolation from p in Post, where: ^tag in p.tags, select: p

Composing queries def elixir_posts do from p in Post, where: "elixir" in p.tags, select: p end

Paginate def elixir_posts(page, per_page) do offset = (page - 1) * per_page from p in Post, where: "elixir" in p.tags, offset: ^offset, limit: ^per_page, select: p end

Paginate def elixir_posts(page, per_page) do offset = (page - 1) * per_page from p in Post, where: "elixir" in p.tags, offset: ^offset, limit: ^per_page, select: p end

Paginate def paginate(query, page, per_page) do offset = (page - 1) * per_page from query, offset: ^offset, limit: ^per_page end elixir_posts > paginate(2, 30)

Scopes def admin(query) do from u in query, where: u.level == "admin" end

Scopes def admin(query), do: where(query, [u], u.level == "admin")

Queryable Protocol: to_query/1 First argument of from Implementations of Queryable - %Query{} - Atom (model) - Binary (source) - {source, model} tuples

Fragments from u in User, where: fragment("lower(?) = lower(?)", u.username, ^username)

Type checking Type check expressions Protect against inserting bad data

Repo

Repo Decoupled from models and queries All database actions goes through the repo - Repo.all(query) - Repo.insert(struct) - Repo.delete_all(query) Calls the database adapter

Adapters Pools connections with poolboy Ships with PostgreSQL & MySQL SQL Server support - hex.pm/packages/tds_ecto

Connection pooling #PID<0.57.0> #PID<0.58.0> #PID<0.59.0> #PID<0.60.0> #PID<0.56.0> #PID<0.61.0> MyRepo

Connection pooling #PID<0.57.0> #PID<0.58.0> #PID<0.59.0> #PID<0.60.0> #PID<0.56.0> #PID<0.61.0> MyRepo

Connection pooling #PID<0.59.0> #PID<0.57.0> #PID<0.60.0> #PID<0.56.0> #PID<0.61.0> MyRepo

Connection pooling #PID<0.57.0> #PID<0.62.0> #PID<0.59.0> #PID<0.60.0> #PID<0.56.0> #PID<0.61.0> MyRepo

Adapters Pools connections with poolboy Ships with PostgreSQL & MySQL SQL Server support - hex.pm/packages/tds_ecto

Model

Schema defmodule MyApp.Post do use Ecto.Model schema "posts" do field :title, :string field :content, :string end end

Callbacks before_insert :timestamp, [:insert] before_update :timestamp, [:update] defp timestamp(changeset, :insert), do: put_change(change, :inserted_at, DateTime.now) defp timestamp(changeset, :update), do: put_change(change, :updated_at, DateTime.now)

Associations schema "posts" do has_many :comments, MyApp.Comment has_one :link, MyApp.Permalink belongs_to :author, MyApp.User end

Query on associations from p in Post, preload: [:comments] from p in Post, preload: [comments: :likes] from p in Post, join: c in assoc(p, :comments), preload: [comments: c]

Changesets

Cast parameters Validate data Tracks changes Changesets

Changeset def changeset(user, params) do cast(user, params, ~w(name email)a, ~w(country)a) > validate_length(:name, min: 3) > validate_unique(:email) end

Changeset def changeset(user, params) do cast(user, params, ~w(name email)a, ~w(country)a) > validate_length(:name, min: 3) > validate_unique(:email) end

Changeset def changeset(user, params) do cast(user, params, ~w(name email)a, ~w(country)a) > validate_length(:name, min: 3) > validate_unique(:email) end def create(conn, params) do changeset = User.changeset(%User{}, params) if changeset.valid? do user = Repo.insert(changeset) send_ok(user) else send_validation_error(changeset.errors) end end

Ecto types Extends ecto s types Cast from input parameter to type Load and dump database values

Version type schema "releases" do field :app, :string field :version, HexWeb.Version field :checksum, :string field :has_docs, :boolean timestamps end #...

Version type defmodule HexWeb.Version do @behaviour Ecto.Type def type, do: :string def cast(%version{} = version), do: {:ok, version} def cast(string) when is_binary(string), do: Version.parse(string) def cast(_), do: :error def load(string), do: Version.parse(string) def dump(%version{} = version), do: {:ok, to_string(version)} end

Phoenix & Ecto phoenix_ecto project - hex.pm/packages/phoenix_ecto Implement protocols - HTML.FormData - HTML.Safe - Poison.Encoder - Plug.Exception

? @emjii