App auto crud

47
06.06.2022 - Page 1 Département Office App::AutoCRUD An application for searching and manipulating data in relational databases YAPC::EU::2014 [email protected] Etat de Genève, Pouvoir Judiciaire Département Office

description

An admin application for editing database, but with configurable features (grouping and ordering of tables and columns, hyperlink navigation between related records, etc.)

Transcript of App auto crud

Page 1: App auto crud

10.04.2023 - Page 1

DépartementOfficeApp::AutoCRUD

An application for searching and manipulating data in relational

databases

YAPC::EU::2014

[email protected] de Genève, Pouvoir Judiciaire

DépartementOffice

Page 2: App auto crud

Agenda

• Introduction• Overview (screenshots)• Getting started• Architecture• Extensibility• Conclusion

Page 3: App auto crud

10.04.2023 - Page 1

DépartementOffice

Introduction

Page 4: App auto crud

Business need : tool for the support team

RegularUsers

database

RegularApp

DatabaseAdmins

AdminTools

SupportTeam

? ? ?

Page 5: App auto crud

Data access for support team

Needs• Browse DB schema• CRUD on all data• Navigate

– Follow relationships– Optimize for most

common tables and colums

Solutions• Program an admin

GUI– too much work !

• Give them DBA tools– too low-level– no customization

Need a CRUD app !

Page 6: App auto crud

CRUD landscape

Admin toolsOracle SQLDeveloperSQLite SqlitemanPostgres pgadminphpMyAdmin...

Readyto use

Needsprogramming

Fixedfeatures

Customizable

Scaffolding toolsRuby on RailsCatalystCatalystX::CRUD...

AutoCRUD appsCatalyst::Plugin::AutoCRUD (2011)App::AutoCRUD (Jan 2014)RapidApp (Feb 2014) [ WebAPI::DBIC ] (Jul 2014)Interchange TableEditor (2013??)

Page 7: App auto crud

App::AutoCRUD main features

• Powerful search syntax• Distinctive URL for every resource

– no session, all information in URL– easy admin links from the "real App"

• Joins as hyperlinks• Bulk update / delete operations• Customizable table order / column order• Navigation through tree navigator

Page 8: App auto crud

10.04.2023 - Page 1

DépartementOffice

Quick overview

To run the demo :cd examplesperl download_db.plplackup

Page 9: App auto crud

Homepage

Specified in config file

http://chinookdatabase.codeplex.com/https://code.google.com/p/sakila-sample-database-ports/

Demo: free sample

databases

Page 10: App auto crud

Datasource entry point

Auto-generateddatamodel

Grouping, order & comments

from config file

Page 11: App auto crud

Table description

Grouping, order & comments

from config file

Hyperlinks to foreign tables

from DB schema

Page 12: App auto crud

Search formMultiple values

(OR)

LIKE

Multiple views

Choose columns

BETWEEN

Page 13: App auto crud

Results from search

Hyperlinks to foreign records or lists

Only show non-null columns

Also navigate through LEFT/RIGHT arrow keys

Page 14: App auto crud

Excel output

Page 15: App auto crud

YAML output

Page 16: App auto crud

Bulk update (selective)

2) Call the update form

1) Check records to be modified

Page 17: App auto crud

Update form

Keys of selected records

Page 18: App auto crud

Bulk update (from WHERE criteria)

Call the update form

Page 19: App auto crud

Update form

Criteria for the update

Fields to update

Page 20: App auto crud

± RESTful URLs

• /Chinook/table/Track/search?GenreId=1,3&Name=Ab*– GET display pre-filled search form– POST redirect to list

• /Chinook/table/Track/list?GenreId=1,3&Name=Ab*– search results

• /Chinook/table/Track/id/399– single record

• /Chinook/table/Track/delete?where_pk.TrackId=399– GET display pre-filled delete form– POST database operationNote : browser-oriented, not API-oriented

No use of HTTP methods PUT and DELETE

Page 21: App auto crud

10.04.2023 - Page 1

DépartementOffice

Getting Started

Page 22: App auto crud

YAML Config : connection parameters

app: name: Test AutoCRUD

datasources : Source1 : dbh: connect: - dbi:SQLite:dbname=some_file - "" # user - "" # password - RaiseError : 1 sqlite_unicode: 1

Page 23: App auto crud

Config : specifying table grouping / ordering

tablegroups : - name: Music descr: Tables describing music content node: open tables : - Artist - Album - Track

- name: Playlist ...

Page 24: App auto crud

Config : specifying col grouping / ordering

Employee: colgroups: - name: keys columns: - name: EmployeeId descr: primary key - name: ReportsTo descr: foreign key to the manager - name: Employee details columns: - name: Title - name: FirstName - name: LastName

Page 25: App auto crud

"crud.psgi" startup file

use App::AutoCRUD;use YAML qw/LoadFile/;

my $config = LoadFile "/path/to/config.yaml";my $crud = App::AutoCRUD->new(config => $config);

my $app = $crud->to_app;

Page 26: App auto crud

Start the application

• From the command lineplackup crud.psgi

• Within Apache<Location /crud>

SetHandler perl-scriptPerlResponseHandler Plack::Handler::Apache2 PerlSetVar psgi_app /path/to/crud.psgi

</Location>

• Other possibilities : see L<Plack>

Page 27: App auto crud

10.04.2023 - Page 1

DépartementOffice

Architecture : external modules

Page 28: App auto crud

External modules : Global view

TemplateToolkit

Alien::GvaScript

Prototype.js

Excel::Writer:

:XLSX

View

DBIx::DataModel

DBI

SQL::Abstract::More

Model

Plack

CGI::Expand

SQL::Abstract:

:FromQuery

Controller

Infrastructure

MooseData::Domai

nYAML

Page 29: App auto crud

Infrastructure modules

• Moose for OO– Modern Perl OO framework

• YAML for config data– Human-readable– Support for directed acyclic graph (DAG) reusable subtrees– But can be replaced by JSON, XML, Config::General or Perl

source code

• Data::Domain for data validation

Page 30: App auto crud

Controller modules

• Plack for HTTP support– Abstraction over various engines– Many middleware options

• CGI::Expand for transforming form inputs – <input name="foo.1.bar" value=1234>– { foo => [ {...}, { bar => 1234 } ] }

• SQL::Abstract::FromQuery for parsing user queries– HTML form SQL::Abstract::More SQL– Parses booleans, < > !=, LIKE, BETWEEN, etc.

Page 31: App auto crud

Model modules

• DBI for database independance– Perl standard for databases

• DBIx::DataModel for object-relational mapping – Why not DBIC ? See

http://www.slideshare.net/ldami/dbixclass-vs-dbixdatamodel

• SQL::Abstract::More for SQL generation– Emit SQL from Perl datastructures

Page 32: App auto crud

View modules

• Template Toolkit for HTML generation

• Alien::GvaScript for widgets – Tree::Navigator

• Prototype.js for Javascript abstraction– Navigator abstraction, OO programming, etc.

• Excel::Writer::XLSX for generating Excel worksheets

Page 33: App auto crud

10.04.2023 - Page 1

DépartementOffice

Architecture : internal structure

Page 34: App auto crud

Internal structure : global view

AutoCRUD

AutoCRUD:

:View

AutoCRUD:

:Controller

AutoCRUD:

:Context

AutoCRUD:

:DataSource

Plack::Component

Moose::Object

Controller::Table

Controller::Schema

View::XLSX

View::TT

...

Page 35: App auto crud

App::AutoCRUD

• Entry point• Autodiscovers component subclasses• isa Plack::Component• Has

– Name– Config– Datasources

Page 36: App auto crud

App::AutoCRUD::DataSource

• Encapsulates DBIx::DataModel schema– Autogenerate Perl code if not already present

• Gathers and merges metadata– From the DB schema

• Tables, columns, foreign keys– From config

• Table groups, column groups, ordering, display parameters

Page 37: App auto crud

App::AutoCRUD::Context

• Gathers contextual information for the current request– Plack::Request object– Datasource object– View to apply– Parameters for the view (for example TT template name)

Page 38: App auto crud

Controller::Schema

• Lists table groups & tables• Publishes the DBIx::DataModel schema

Page 39: App auto crud

Controller::Table

• Implements CRUD operations• Methods

– Descr– List– Id– Search– Update– Delete– Insert

Page 40: App auto crud

View::*

• Render results– TT– XLSX– YAML– JSON– ...

Page 41: App auto crud

10.04.2023 - Page 1

DépartementOffice

Extensibility

Page 42: App auto crud

Regular subclassing

• Extend the apppackage My::Private::AutoCRUD;use Moose;extends 'App::AutoCRUD';

• Subclass and redefine existing methods– Automatic subclass discovery– Good candidates :

• DataSource::prepare_for_request • DataSource::_query_parser

Page 43: App auto crud

Adding URLs

• New controllers– Crud/<db_name>/<controller_name>/....

• New methods in existing controllers– Crud/<db_name>/table/<table_name>/<method>/...

Page 44: App auto crud

Adding Views

• Create View/XYZ.pm with "render" method– Serves /path/to/resource.xyz?p1=v1&p2=v2&...

Page 45: App auto crud

Relooking

• Override CSS – My/Private/AutoCRUD/share/static/css/styles.css

• Override TT templates – My/Private/AutoCRUD/share/templates/src/table/list.tt

Page 46: App auto crud

10.04.2023 - Page 1

DépartementOffice

Conclusion

Page 47: App auto crud

Status

• Still young, lots of TODO• Already in production at Geneva Justice• Has some original features not seen other CRUD

apps• Could be useful for many people

– Also outside the Perl community • Perl marketing needs general-purpose apps

– Needs stabilization & doc– Needs marketing– You can help !