2
0
mirror of https://github.com/frappe/books.git synced 2024-11-10 07:40:55 +00:00
books/README.md

196 lines
3.5 KiB
Markdown
Raw Normal View History

2018-01-01 09:27:59 +00:00
# Frappe Core
Core libs for Frappe Framework JS
2018-01-01 12:17:51 +00:00
## Declaring Models
2018-01-01 10:28:30 +00:00
Models are declared by adding a `.json` model file in the `models/doctype` folder of the module/app.
{
"autoname": "hash",
"name": "ToDo",
"doctype": "DocType",
"issingle": 0,
"fields": [
{
"fieldname": "subject",
"label": "Subject",
"fieldtype": "Data",
"reqd": 1
},
{
"fieldname": "description",
"label": "Description",
"fieldtype": "Text"
},
{
"fieldname": "status",
"label": "Status",
"fieldtype": "Select",
"options": [
"Open",
"Closed"
],
"default": "Open",
"reqd": 1
}
]
}
2018-01-01 12:17:51 +00:00
## Setup / Migrate
2018-01-01 09:27:59 +00:00
2018-01-01 10:31:42 +00:00
const frappe = require('frappe-core');
2018-01-01 09:27:59 +00:00
frappe.init();
2018-01-01 10:28:30 +00:00
// sync all schema from `models` folders in all apps
frappe.migrate();
2018-01-01 12:17:51 +00:00
## Managing Documents
2018-01-01 10:28:30 +00:00
2018-01-01 10:31:02 +00:00
Frappe Object-Relational-Mapper (ORM) helps you manage (create, read, update, delete) documents based on the DocTypes declared.
Documents are stored in SQLite using `sql.js`
2018-01-01 10:28:30 +00:00
2018-01-01 12:17:51 +00:00
### Create
2018-01-01 10:28:30 +00:00
2018-01-01 10:31:42 +00:00
const frappe = require('frappe-core');
2018-01-01 10:28:30 +00:00
frappe.init();
// make a new todo
2018-01-01 10:31:02 +00:00
let todo = frappe.get_doc({doctype: 'ToDo', subject: 'something'});
todo.insert();
2018-01-01 09:27:59 +00:00
2018-01-01 12:17:51 +00:00
### Read
2018-01-01 10:28:30 +00:00
2018-01-01 10:31:42 +00:00
const frappe = require('frappe-core');
2018-01-01 10:28:30 +00:00
frappe.init();
// get all open todos
let todos = frappe.db.get_all('ToDo', ['name'], {status: "Open"});
2018-01-01 10:31:02 +00:00
let first_todo = frappe.get_doc('ToDo', toods[0].name);
2018-01-01 10:28:30 +00:00
2018-01-01 12:17:51 +00:00
### Update
2018-01-01 10:28:30 +00:00
2018-01-01 10:31:42 +00:00
const frappe = require('frappe-core');
2018-01-01 10:28:30 +00:00
frappe.init();
// get all open todos
let todos = frappe.db.get_all('ToDo', ['name'], {status: "Open"});
2018-01-01 10:31:02 +00:00
let first_todo = frappe.get_doc('ToDo', toods[0].name);
2018-01-01 10:28:30 +00:00
first_todo.status = 'Closed';
first_todo.update();
2018-01-01 12:17:51 +00:00
## Metadata
2018-01-01 10:28:30 +00:00
2018-01-01 10:31:42 +00:00
const frappe = require('frappe-core');
2018-01-01 10:28:30 +00:00
frappe.init();
let todo_meta = frappe.get_meta('ToDo');
// get all fields of type "Data"
let data_fields = todo_meta.fields.map(d => d.fieldtype=='Data' ? d : null);
2018-01-01 12:17:51 +00:00
## Controllers
2018-01-01 10:28:30 +00:00
You can write event handlers in controllers, by declaring a `.js` file in the `models/doctype/` folder along with the model file.
The name of the class must be the slugged name of the DocType
const frappe = require('frappe-core');
class todo extends frappe.document.Document {
validate() {
// set default status as "Open" if not set
if (!this.status) {
this.status = 'Open';
}
}
2018-01-01 09:27:59 +00:00
}
2018-01-01 10:28:30 +00:00
module.exports = { todo: todo };
2018-01-01 12:17:51 +00:00
## Database
2018-01-01 10:28:30 +00:00
You can also directly write SQL with `frappe.db.sql`
2018-01-01 10:31:42 +00:00
const frappe = require('frappe-core');
2018-01-01 10:28:30 +00:00
frappe.init();
all_todos = frappe.db.sql('select name from todo');
2018-01-01 09:27:59 +00:00
2018-01-01 12:17:51 +00:00
## REST API
You can directly access documents at `/api/resource/:doctype`
### Create
- URL: `/api/resource/:doctype`
- Method: `POST`
- Data: document properties
**Example:**
- URL: `/api/resource/todo`
- Method: `POST`
Data:
{
"subject": "test",
"description": "
}
### Read
- URL: `/api/resource/:doctype/:name`
- Method: `GET`
**Example:**
- URL: `/api/resource/todo/uig7d1v12`
Reponse:
{
"name": "uig7d1v12",
"owner": "guest",
"modified_by": "guest",
"creation": "2018-01-01T12:08:19.482Z",
"modified": "2018-01-01T12:08:19.482Z",
"docstatus": 0,
"subject": "test 1",
"description": "description 1",
"status": "Open"
}
### List
- URL: `/api/resource/:doctype/`
- Method: `GET`
- Params (optional)
- `start`: Page start
- `limit`: Page limit
**Example:**
- URL: `/api/resource/todo`
Response:
[
{
"name": "r4qxyki0i6",
"subject": "test 1"
},
{
"name": "efywwvtwcp",
"subject": "test 1"
},
{
"name": "9ioz05urgp",
"subject": "test 1"
}
]