Entity storage, the Drupal 8 way



Similar documents
Multilingual content in Drupal 8: a highly evolved permutated API. DrupalCamp Vienna 2013 Francesco Placella

Entites in Drupal 8. Sascha Grossenbacher Christophe Galli

Migrating into Drupal 8

The truth about Drupal

The Search API in Drupal 8. Thomas Seidl (drunken monkey)

Things Made Easy: One Click CMS Integration with Solr & Drupal

GETTING STARTED WITH DRUPAL. by Stephen Cross

Database Programming with PL/SQL: Learning Objectives

FormAPI, AJAX and Node.js

A (Web) Face for Radio. NPR and Drupal7 David Moore

The EMSX Platform. A Modular, Scalable, Efficient, Adaptable Platform to Manage Multi-technology Networks. A White Paper.

The full setup includes the server itself, the server control panel, Firebird Database Server, and three sample applications with source code.

Symfony vs. Integrating products when to use a framework

Release Notes for Patch Release #2614

Informix Administration Overview

MASTER DRUPAL 7 MODULE DEVELOPMENT

CA Data Protection. Content Provider Development Guide. Release 15.0

Module 1: Getting Started with Databases and Transact-SQL in SQL Server 2008

D61830GC30. MySQL for Developers. Summary. Introduction. Prerequisites. At Course completion After completing this course, students will be able to:

Writing Queries Using Microsoft SQL Server 2008 Transact-SQL

Easy configuration of NETCONF devices

Migrating into Drupal 8 Migrando a Drupal 8

5 Mistakes to Avoid on Your Drupal Website

Auditing Drupal sites for performance, content and optimal configuration

Installing and Administering VMware vsphere Update Manager

Oracle Database: Program with PL/SQL

MongoDB Developer and Administrator Certification Course Agenda

Accounts Payable Workflow Guide. Version 11.2

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

Drupal 8 The site builder's release

ProSoftMod Commission Report Documentation

Sage CRM 7.3 SP2. Release Notes. Revision: SYS-REA-ENG-7.3SP2-1.0 Updated: April 2016

Bitrix Site Manager 4.0. Quick Start Guide to Newsletters and Subscriptions

REPRINT. Release User s Guide. iseries (AS/400) Developed and Distributed by

Who? Wolfgang Ziegler (fago) Klaus Purer (klausi) Sebastian Gilits (sepgil) epiqo Austrian based Drupal company Drupal Austria user group

About This Document 3. Integration and Automation Capabilities 4. Command-Line Interface (CLI) 8. API RPC Protocol 9.

Writing Queries Using Microsoft SQL Server 2008 Transact-SQL

COMP5426 Parallel and Distributed Computing. Distributed Systems: Client/Server and Clusters

CERN Summer Student Program 2013 Report

EMC RepliStor for Microsoft Windows ERROR MESSAGE AND CODE GUIDE P/N REV A02

Monitoring HP OO 10. Overview. Available Tools. HP OO Community Guides

Database Monitoring Requirements. Salvatore Di Guida (CERN) On behalf of the CMS DB group

Beginning Oracle. Application Express 4. Doug Gault. Timothy St. Hilaire. Karen Cannell. Martin D'Souza. Patrick Cimolini

Headless Drupal 8. #HeadlessDrupal

Oracle Database: Program with PL/SQL

Database 10g Edition: All possible 10g features, either bundled or available at additional cost.

Drupal 8 Development Retrospective. A timeline and retrospective from a core contributor

Oracle Database: Program with PL/SQL

Commerce Services Documentation

SQL Server 2005 Reporting Services (SSRS)

Report and Dashboard Template User Guide

Foglight. Dashboard Support Guide

Streamline your drupal development workflow in a 3-tier-environment - A story about drush make and drush aliases

Oracle BI EE Implementation on Netezza. Prepared by SureShot Strategies, Inc.

Nortel Networks Symposium Call Center Server Symposium Database Integration User s Guide

Oracle Business Intelligence Server Administration Guide. Version December 2006

Tobby Hagler, Phase2 Technology

Migrating a running service to AWS

ICE for Eclipse. Release 9.0.1

Toad for Oracle 8.6 SQL Tuning

AVOIDING THE GIT OF DESPAIR

U.S. FDA Title 21 CFR Part 11 Compliance Assessment of SAP Records Management

REPRINT. Release Reference Manual. IBM iseries (AS/400) Developed and Distributed by

Kentico 8 Certified Developer Exam Preparation Guide. Kentico 8 Certified Developer Exam Preparation Guide

Apache Sentry. Prasad Mujumdar

Symantec Enterprise Vault

ICOM 6005 Database Management Systems Design. Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department Lecture 2 August 23, 2001

ADP Workforce Now V3.0

GigaSpaces Real-Time Analytics for Big Data

Introduction to Module Development

Logi Ad Hoc Reporting System Administration Guide

Visualizing an OrientDB Graph Database with KeyLines

NetIQ Identity Manager Setup Guide

How to Enhance Traditional BI Architecture to Leverage Big Data

Blue Coat Cloud Data Protection Server Administration Guide

Final Report - HydrometDB Belize s Climatic Database Management System. Executive Summary

Fine Grained Auditing In Oracle 10G

White Paper: 1) Architecture Objectives: The primary objective of this architecture is to meet the. 2) Architecture Explanation

Design Document. Offline Charging Server (Offline CS ) Version i -

The Data Grid: Towards an Architecture for Distributed Management and Analysis of Large Scientific Datasets

<Insert Picture Here> Oracle SQL Developer 3.0: Overview and New Features

IBM Tivoli Storage Manager Version Introduction to Data Protection Solutions IBM

Comparing Microsoft SQL Server 2005 Replication and DataXtend Remote Edition for Mobile and Distributed Applications

XTM Drupal Connector. A Translation Management Tool Plugin

ZXUN USPP. Configuration Management Description. Universal Subscriber Profile Platform. Version: V

How To Fix A Bug In Drupal 8.Dev

MCN Health Monitor. The finger on the pulse of your critical systems. David Tayler Service Engineer, OSISoft

Xerox EX Print Server, Powered by Fiery, for the Xerox 700 Digital Color Press. Printing from Windows

Drupal Module Development

Adam Rauch Partner, LabKey Software Extending LabKey Server Part 1: Retrieving and Presenting Data

Windows Azure Data Services (basics) 55093A; 3 Days

SNMP Informant Release Notes

Transcription:

Entity storage, the Drupal 8 way Francesco Placella Coding and development - http://bit.ly/d8-esa

About me Francesco Placella, plach on drupal.org, from Venice, Italy Senior Performance Engineer at Tag1 Consulting Working with Drupal since 2006 Maintainer of the core Language system Maintainer of the core Language and Content Translation modules Unofficial maintainer of the core Entity storage, Entity form and Entity translation subsystems http://twitter.com/plach

Drupal 7 vs Drupal 8 Outline Dealing with entity data Entity type and field definitions Storage schema Core SQL storage The fun stuff

Drupal 7 vs Drupal 8

Drupal 7 Field swappable storage Field data can live in NoSQL storage, remote storage Every field is configured independently Possibly problematic for entity querying Supports only Fields, properties are always stored in the SQL database

Drupal 8 Switched from field-based to entity-based storage Storage is still swappable Supports also base fields (e.g. the node type) Entity querying works nicely Fields can no longer be shared among entity types you can have fields with the same name in different entity types

SQL is not dead, it just smells funny

Dealing with entity field data Swappable backends imply storage-agnostic code Contrib modules should not assume a SQL storage either leverage the Entity CRUD API or provide their own APIs (e.g. Views) Custom modules can assume a specific storage should NOT bypass the Entity API use SQL-specific APIs if needed

The Entity Query API To query entity field data we have the Entity Query API the successor of the D7 Entity Field Query system improved syntax DBTNG leverages swappable query backends Supports expressing relationship between entity types the SQL backend translates those in JOINs Supports expressing aggregation queries (!) Very powerful but obviously not as expressive as SQL

Legal SQL usages Always retrieve identifiers, also via custom SQL queries do not retrieve partial data Always load an entity before accessing field data Always save an entity to write field data to the storage Bypassing the Entity API means you are on your own unexpected behaviors, cache invalidation issues, At least encapsulate SQL-specific code in a swappable service

It s all a matter of definitions

Entity type definition An entity type definition (a plugin definition) describes the entity type to the system Content entities rely on field data Configuration entities use plain properties and are stored in configuration A definition has several properties allowing to customize the entity type s behavior

Key definition properties The handlers section defines, among the rest: the storage handler that performs CRUD entity operations the storage_schema that manages the entity storage schema when needed (!) The revisionable and translatable properties may have an impact on how data is stored schema

Entity Field API The D8 Entity Field API generalizes the D7 Field API Every piece of data attached to an entity is a field Base fields are shared among all available bundles (e.g. nid) Bundle fields may be attached only to certain bundles (e.g. field_image) Both are handled the same (e.g. Views or REST support)

Field definitions Base field definitions typically live in code defined via hook_entity_base_field_info() Bundle field definitions typically live in configuration defined via hook_entity_bundle_field_info() the Field module allows to create bundle field definitions based on its configuration can be defined in code too

Field storage definitions Field storage definitions collect the information required to store a field (surprise!) Base field definitions are usually instances of the BaseFieldDefinition class both a field and a field storage definition Bundle field definitions share a field storage definition can exist even when no bundle field has been defined

The Entity Storage Schema

Schema generation The storage handler is responsible for managing its own schema, if used schema is automatically generated based on entity type and field definition Schema is created on module installation and dropped on uninstallation

Core SQL storage Generates tables for base and bundle fields single base fields are stored in shared tables bundle fields and multiple base fields are stored in dedicated tables Supports four different shared table layouts depending on entity type translatability entity type revisionability

Shared table layouts Simple entity types use the base table to store base field data Translatable entity types use the base table to store entity keys the data table to store translated base field data Revisionable entity types use the base table to store base field data the revision table to store data for revisionable base fields

Shared table layouts Translatable and revisionable entity types use the base table to store entity keys the data table to store translated base field data the revision table to store entity key revisions and revision metadata the revision data table to stores translated base field revision data The storage schema supports switching between layouts

The Table Mapping API How to query shared tables? via the Entity Query API (storage agnostic) via the Table Mapping API (SQL-specific) The Table Mapping API allows to write SQL queries in a layoutagnostic fashion It is used by Views to implement its SQL backend Currently core entity type support only the DefaultTableMapping assumes one of the previous layouts

Entity Updates Entity Updates leverage a dedicated API The Entity Definition Update Manager is able to detect any mismatch between the definitions and the actual schema allows to apply individual updates trigger events when an update is applied refuses to proceed if the change requires a data migration

Entity Updates Typically Entity Schema updates are applied via update functions A Drush command is available (drush entup) to apply any pending entity update this should be used only during development should NOT be used to get rid of the status report error in production

The Right Way

Define Define any field needed to implement the business logic Field data will be loaded/stored automatically Automatic module integration via the Entity Field API revisionability, translatability Views, REST, Rules, Field definitions can opt out by marking themselves as having custom storage (not recommended) mainly used for computed fields

and code! Core entity types provide interfaces making business logic explicit, e.g. NodeInterface::isSticky() encapsulate the implementation better integrated with IDEs mark required data model It s a good practice to provide a wrapper for moduleprovided fields

Shut up and show me some code

A simple tracker Simple module (http://bit.ly/d8-esa-ex) to list: users having created a published node total amount of created nodes title of most recently created node Direct querying has poor performance denormalize add two fields to the user entity type update their values on C(R)UD events

A simple tracker Field definitions and installation The entity wrapper Service encapsulating business logic on node creation aggregate entity query on node deletion regular entity query retrieve the user list entity query relationship display Performant and fully portable!

Seriously?

What's Left? Switching between shared table layouts is supported only by the API https://www.drupal.org/node/2274017 Define custom indexes for the entity storage schema https://www.drupal.org/node/2258347 When adding new fields an initial value may be needed https://www.drupal.org/node/2346019 Base field purging https://www.drupal.org/node/2282119

Sprint: Friday Sprint with the Community on Friday We have tasks for every skillset Mentors are available for new contributors An optional Friday morning workshop for first-time sprinters will help you get set up Follow @drupalmentoring

As you may have guessed

Conclusions Use the Entity Field API to define your data model and code your business logic on top of it leverage fields to store data, avoid custom storage! Always retrieve identifiers and load entities to access field data the Entity Query API is very powerful, use it whenever possible!

Useful links Entity Storage API blog post https://drupalwatchdog.com/blog/2015/3/entity-storage-drupal-8-way Drupal 8 Entity API documentation https://www.drupal.org/node/2143503 The Table Mapping API reference https://api.drupal.org/api/drupal/core!lib!drupal!core!entity!sql! TableMappingInterface.php/interface/TableMappingInterface/8 https://api.drupal.org/api/drupal/core!lib!drupal!core!entity!sql! DefaultTableMapping.php/class/DefaultTableMapping/8

Question & Answers