2
0
mirror of https://github.com/frappe/books.git synced 2024-12-27 20:45:04 +00:00
books/docs/models/document.md

65 lines
1.8 KiB
Markdown
Raw Normal View History

2018-01-10 12:49:52 +00:00
# Managing Documents
Frappe.js Object-Relational-Mapper (ORM) helps you manage (create, read, update, delete) documents based on the DocTypes declared.
Documents are sub-classed from the `frappe.document.Document` class.
All document write methods are asynchronous and return javascript Promise objects.
### Initialize
2018-02-08 12:28:51 +00:00
Documents are initialized with the `frappe.getDoc` method. If `doctype` and `name` are passed as parameters, then the document is fetched from the backend. If a simple object is passed, then object properties are set in the document.
2018-01-10 12:49:52 +00:00
```js
// make a new todo
2018-02-08 12:28:51 +00:00
let todo = await frappe.getDoc({doctype: 'ToDo', subject: 'something'});
2018-01-10 12:49:52 +00:00
```
### Create
You can insert a document in the backend with the `insert` method.
```js
// make a new todo
2018-02-08 12:28:51 +00:00
let todo = await frappe.getDoc({doctype: 'ToDo', subject: 'something'});
2018-01-10 12:49:52 +00:00
await todo.insert();
```
### Read
2018-02-08 12:28:51 +00:00
You can read a document from the backend with the `frappe.getDoc` method
2018-01-10 12:49:52 +00:00
```js
// get all open todos
2018-02-08 12:28:51 +00:00
let todos = await frappe.db.getAll({doctype:'ToDo', fields:['name'], filters: {status: "Open"});
let first_todo = await frappe.getDoc('ToDo', toods[0].name);
2018-01-10 12:49:52 +00:00
```
### Update
The `update` method updates a document.
```js
// get all open todos
2018-02-08 12:28:51 +00:00
let todos = await frappe.db.getAll({doctype:'ToDo', fields:['name'], filters: {status: "Open"});
let first_todo = await frappe.getDoc('ToDo', toods[0].name);
2018-01-10 12:49:52 +00:00
first_todo.status = 'Closed';
await first_todo.update();
```
### Delete
The `delete` method deletes a document.
```js
// get all open todos
2018-02-08 12:28:51 +00:00
let todos = await frappe.db.getAll({doctype:'ToDo', fields:['name'], filters: {status: "Open"});
let first_todo = await frappe.getDoc('ToDo', toods[0].name);
2018-01-10 12:49:52 +00:00
await first_todo.delete();
```
### Extending Documents
Each model can be extended by adding events in the [controller](controllers.md) class.