|
| 1 | +Schema |
| 2 | +------ |
| 3 | + |
| 4 | +Schema is a module that allows the declaration of data structures composed of |
| 5 | +Entities and Fields. Its primary use is to define the model for ORMs and Form |
| 6 | +generators, but it can be used to declare any such structure. |
| 7 | + |
| 8 | +Schema allows the definition of a *data schema* in an agnostic way. The actual |
| 9 | +physical representation of the data schema isn't treated by the module, and is |
| 10 | +supposed.to be used by a different engine, that we are going to call the |
| 11 | +*client engine*. |
| 12 | + |
| 13 | +A data schema can be used in many ways, but the most common is to represent a |
| 14 | +set of tables in a database application. |
| 15 | + |
| 16 | +The client engine usually represents your application, a schema-enabled Lua ORM |
| 17 | +(like Orbit.model or Loft) or any other form of data persitence that has been |
| 18 | +written specially to use the data table that results from the data schema |
| 19 | +declaration. |
| 20 | + |
| 21 | +The declaration uses a Lua-based DSL to define the **data schema**, its **entities** |
| 22 | +and their **fields**. |
| 23 | + |
| 24 | +Schema offers a set of predefined types for fields, but is completely |
| 25 | +extensible: when it doesn't understand a field type the type name itself is |
| 26 | +stored in the schema, so it can be treated by the underlying persistence engine. |
| 27 | + |
| 28 | +##The Schema DSL |
| 29 | + |
| 30 | +The schema DSL can be described as a sequence of declarations of three types: |
| 31 | + |
| 32 | +* Options |
| 33 | +* Entities |
| 34 | +* Handlers |
| 35 | + |
| 36 | +###Options |
| 37 | + |
| 38 | +*Options* are values used to define general settings or convey additional |
| 39 | +information for the client engine. |
| 40 | + |
| 41 | +There are some options that affects the Schema declaration: |
| 42 | + |
| 43 | +* *table_prefix*: is a string added to the begining of every entity name to |
| 44 | +define the *table_name* attribute for an entity when the attribute is absent. |
| 45 | +* *column_prefix*: is a string added to the begining of every field name to |
| 46 | +define the *column_name* attribute of a field when the attribute is absent. |
| 47 | + |
| 48 | +###Declaring Entities |
| 49 | + |
| 50 | +*Entities* represent object classes or database tables, and are declared using |
| 51 | +the function 'entity'. They are stored as simple Lua tables containing |
| 52 | +information about their fields and about the entity itself. |
| 53 | + |
| 54 | +Data schemas will tipically have three fields on an entity declaration: |
| 55 | + |
| 56 | +* *aspects* - define a list of aspects to be used in this particular entity. |
| 57 | +Aspects are treated by the client engine -- schema doesn't process them in any |
| 58 | +way during declaration. |
| 59 | +* *fields* - define a list of field declarations. They usually have the format: |
| 60 | + <*fieldname*> = <*fieldtype*> ([*parameters*]) |
| 61 | + |
| 62 | +* *handlers* - define a list of handler declarations in the format |
| 63 | + <*handlernames*> = <*function*> |
| 64 | + |
| 65 | +Handler function signatures can vary widely depending on the underlying client |
| 66 | +engine, the type of event or other factors. |
| 67 | + |
| 68 | +###Declaring Fields |
| 69 | + |
| 70 | +A typical client engine will extend the schema list of field types -- either on |
| 71 | +its core or by the use of aspect-activated plugins. |
| 72 | + |
| 73 | +Every field type can produce a number of event hooks. |
| 74 | + |
| 75 | +The predefined field types are: |
| 76 | + |
| 77 | +**belongs_to(entity_name)** - the "one" entity used in a one to many association |
| 78 | + |
| 79 | +**boolean** - a boolean value |
| 80 | + |
| 81 | +**date** - a date represents a timestamp equivalent to 12:00PM in the given day |
| 82 | + |
| 83 | +**has_and_belongs(entity_name)** the collection of "many" entities in a many to |
| 84 | +many association |
| 85 | + |
| 86 | +**has_many(entity_name)** - the collection of "many" entities in a one to many |
| 87 | +association |
| 88 | + |
| 89 | +**has_one(entity_name)** - the other entity in a one to one association |
| 90 | + |
| 91 | +**integer([maximum_size])** an integer number with an optional maximum size of |
| 92 | +digits |
| 93 | + |
| 94 | +**key** - a primary key for the entity |
| 95 | + |
| 96 | +**long_text ([maximum_size])** - a long string with an optional maximum size |
| 97 | + |
| 98 | +**number** - a floating point number |
| 99 | + |
| 100 | +**reference** - a reference to a multi valored field |
| 101 | + |
| 102 | +**text([maximum_size])** - a short string with an optional maximum size |
| 103 | + |
| 104 | +**timestamp** - the date and/or time at which a certain event occurred. |
| 105 | + |
| 106 | +###Handlers |
| 107 | + |
| 108 | +The default field handlers can be of two types: |
| 109 | + |
| 110 | +*get(...)* is a function called whenever a field value is evaluated |
| 111 | + |
| 112 | +*set(...)* is a function called whenever a field value is stored |
| 113 | + |
| 114 | +Schema can also receive a list of *entity handlers*, to treat events on entity |
| 115 | +operations. can be of the following types: |
| 116 | + |
| 117 | +*before_save(...)* is a function called before an entity is saved in the |
| 118 | +persistence |
| 119 | + |
| 120 | +*after_save(...)* is a function called after an entity is saved in the persistence |
| 121 | + |
| 122 | +*before_destroy(...)* is a function called before an entity is removed from the |
| 123 | +persistence |
| 124 | + |
| 125 | +*after_destroy(...)* is a function called after an entity is removed from the |
| 126 | +persistence |
| 127 | + |
| 128 | +*before_find(...)* is a function called before looking for an entity in the |
| 129 | +persistence |
| 130 | + |
| 131 | +*after_find(...)* is a function called after looking for an entity in the |
| 132 | +persistence |
| 133 | + |
| 134 | +*before_find_all(...)* is a function called before looking for entities in the |
| 135 | +persistence |
| 136 | + |
| 137 | +*after_find_all(...)* is a function called after looking for entities in the |
| 138 | +persistence |
| 139 | + |
| 140 | +*before_create(...)* is a function called before an entity is created |
| 141 | + |
| 142 | +*after_create(...)* is a function called after an entity is created |
| 143 | + |
| 144 | +*before_update(...)* is a function called before an entity is updated in the |
| 145 | +persistence |
| 146 | + |
| 147 | +*after_update(...)* is a function called after an entity is updated in the |
| 148 | +persistence |
| 149 | + |
| 150 | + |
| 151 | +###Examples |
| 152 | + |
| 153 | +A very simple example of an entity without fields: |
| 154 | + |
| 155 | + person = entity{} |
| 156 | + |
| 157 | +The same entity with an aspect and the empty default fields: |
| 158 | + |
| 159 | + person = entity{ |
| 160 | + aspects={'timestampabble'}, |
| 161 | + fields={} |
| 162 | + handlers={} |
| 163 | + } |
| 164 | + |
| 165 | +A simple person entity with fields: |
| 166 | + |
| 167 | + person = entity{ |
| 168 | + aspects={'timestampabble'}, |
| 169 | + fields={ |
| 170 | + id=key(), |
| 171 | + name=text(), |
| 172 | + age=integer(), |
| 173 | + sex=reference{M='Male', F='Female'} |
| 174 | + } |
| 175 | + } |
| 176 | + |
0 commit comments