Database examples

29-11-2018
```typescript // Instead of creating tables directly we create entities and from them we can // create the database tables, generate objects, etc... orm.register({ name: "phone", label: "@@Teléfono", pluralLabel: "@@Teléfonos", permissions: "CRUD", properties: [ { name: "name", type: "string" }, { name: "price", type: "currency", nullable: true } ] }) orm.createTables() orm.insert("phone", { name: "iPhone", price: 700 }) orm.insert("phone", { name: "Samsung", price: 600 }) // query all let phones = sql.query("SELECT * FROM phone") // iterate for (let p of phones) { fmt.printJSON(p) } // iterate by index for (let i = 0, l = phones.length; i < l; i++) { fmt.printJSON(phones[i]) } // query one let phone = sql.queryFirst("SELECT * FROM phone LIMI 1") // access properties directly as if it is an object fmt.println(phone.name, phone.price) // update queries with .exec sql.exec("UPDATE phone SET price = 500 WHERE id = ?", phone.id) // the orm can also update an object but usually is more efficient to do the update directly phone.name + " X" orm.update("phone", phone) // build queries in a compsable way let q = sql.parseSelect("SELECT name FROM phone") q.where("id = ?", phone.id) q.or("name LIKE ?", "Sam%") q.orderBy("name ASC") // .query accepts a string or a selectQuery object phones = sql.query(q) ``` There is a command that generates types from entities, so if you run ```bash amura types ``` It will generate in the same file where the entity is defined: ```typescript //#region entities_autogenerated_do_not_edit declare global { interface Phone { id?: number; name: string; price: number; } //#endregion ``` So now you can add static typing casting the result ```typescript let phones = sql.query("SELECT * FROM phone") as Phone[] ```