mirror of
https://github.com/frappe/books.git
synced 2025-01-23 15:18:24 +00:00
rest api start
This commit is contained in:
parent
2c00242731
commit
e0738def25
@ -3,6 +3,5 @@
|
|||||||
"browser": true,
|
"browser": true,
|
||||||
"es6": true,
|
"es6": true,
|
||||||
"node": true
|
"node": true
|
||||||
},
|
}
|
||||||
"parser": "babel-eslint"
|
|
||||||
}
|
}
|
93
README.md
93
README.md
@ -2,9 +2,7 @@
|
|||||||
|
|
||||||
Core libs for Frappe Framework JS
|
Core libs for Frappe Framework JS
|
||||||
|
|
||||||
## Examples
|
## Declaring Models
|
||||||
|
|
||||||
### Declaring Models
|
|
||||||
|
|
||||||
Models are declared by adding a `.json` model file in the `models/doctype` folder of the module/app.
|
Models are declared by adding a `.json` model file in the `models/doctype` folder of the module/app.
|
||||||
|
|
||||||
@ -39,7 +37,7 @@ Models are declared by adding a `.json` model file in the `models/doctype` folde
|
|||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|
||||||
### Setup / Migrate
|
## Setup / Migrate
|
||||||
|
|
||||||
const frappe = require('frappe-core');
|
const frappe = require('frappe-core');
|
||||||
frappe.init();
|
frappe.init();
|
||||||
@ -47,13 +45,13 @@ Models are declared by adding a `.json` model file in the `models/doctype` folde
|
|||||||
// sync all schema from `models` folders in all apps
|
// sync all schema from `models` folders in all apps
|
||||||
frappe.migrate();
|
frappe.migrate();
|
||||||
|
|
||||||
### Managing Documents
|
## Managing Documents
|
||||||
|
|
||||||
Frappe Object-Relational-Mapper (ORM) helps you manage (create, read, update, delete) documents based on the DocTypes declared.
|
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`
|
Documents are stored in SQLite using `sql.js`
|
||||||
|
|
||||||
#### Create
|
### Create
|
||||||
|
|
||||||
const frappe = require('frappe-core');
|
const frappe = require('frappe-core');
|
||||||
frappe.init();
|
frappe.init();
|
||||||
@ -62,7 +60,7 @@ Documents are stored in SQLite using `sql.js`
|
|||||||
let todo = frappe.get_doc({doctype: 'ToDo', subject: 'something'});
|
let todo = frappe.get_doc({doctype: 'ToDo', subject: 'something'});
|
||||||
todo.insert();
|
todo.insert();
|
||||||
|
|
||||||
#### Read
|
### Read
|
||||||
|
|
||||||
const frappe = require('frappe-core');
|
const frappe = require('frappe-core');
|
||||||
frappe.init();
|
frappe.init();
|
||||||
@ -72,7 +70,7 @@ Documents are stored in SQLite using `sql.js`
|
|||||||
let first_todo = frappe.get_doc('ToDo', toods[0].name);
|
let first_todo = frappe.get_doc('ToDo', toods[0].name);
|
||||||
|
|
||||||
|
|
||||||
#### Update
|
### Update
|
||||||
|
|
||||||
const frappe = require('frappe-core');
|
const frappe = require('frappe-core');
|
||||||
frappe.init();
|
frappe.init();
|
||||||
@ -84,7 +82,7 @@ Documents are stored in SQLite using `sql.js`
|
|||||||
first_todo.status = 'Closed';
|
first_todo.status = 'Closed';
|
||||||
first_todo.update();
|
first_todo.update();
|
||||||
|
|
||||||
### Metadata
|
## Metadata
|
||||||
|
|
||||||
const frappe = require('frappe-core');
|
const frappe = require('frappe-core');
|
||||||
frappe.init();
|
frappe.init();
|
||||||
@ -94,7 +92,7 @@ Documents are stored in SQLite using `sql.js`
|
|||||||
// get all fields of type "Data"
|
// get all fields of type "Data"
|
||||||
let data_fields = todo_meta.fields.map(d => d.fieldtype=='Data' ? d : null);
|
let data_fields = todo_meta.fields.map(d => d.fieldtype=='Data' ? d : null);
|
||||||
|
|
||||||
### Controllers
|
## Controllers
|
||||||
|
|
||||||
You can write event handlers in controllers, by declaring a `.js` file in the `models/doctype/` folder along with the model file.
|
You can write event handlers in controllers, by declaring a `.js` file in the `models/doctype/` folder along with the model file.
|
||||||
|
|
||||||
@ -113,7 +111,7 @@ The name of the class must be the slugged name of the DocType
|
|||||||
|
|
||||||
module.exports = { todo: todo };
|
module.exports = { todo: todo };
|
||||||
|
|
||||||
### Database
|
## Database
|
||||||
|
|
||||||
You can also directly write SQL with `frappe.db.sql`
|
You can also directly write SQL with `frappe.db.sql`
|
||||||
|
|
||||||
@ -122,3 +120,76 @@ You can also directly write SQL with `frappe.db.sql`
|
|||||||
|
|
||||||
all_todos = frappe.db.sql('select name from todo');
|
all_todos = frappe.db.sql('select name from todo');
|
||||||
|
|
||||||
|
## 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"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
17
frappe/app.js
Normal file
17
frappe/app.js
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
const express = require('express');
|
||||||
|
const app = express();
|
||||||
|
const bodyParser = require('body-parser');
|
||||||
|
const frappe = require('frappe-core');
|
||||||
|
|
||||||
|
app.use(bodyParser.json());
|
||||||
|
app.use(bodyParser.urlencoded({ extended: true }));
|
||||||
|
|
||||||
|
// setup frappe REST routes
|
||||||
|
frappe.init();
|
||||||
|
frappe.db.migrate();
|
||||||
|
frappe.db.write();
|
||||||
|
|
||||||
|
frappe.rest.setup(app);
|
||||||
|
|
||||||
|
app.listen(8000);
|
||||||
|
|
@ -1,7 +1,8 @@
|
|||||||
const path = require('path')
|
const path = require('path');
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
init(db_path, user, user_key) {
|
init(db_path = 'test.db', user, user_key) {
|
||||||
this.db_path = db_path || 'test.db';
|
this.db_path = db_path;
|
||||||
if (this._initialized) return;
|
if (this._initialized) return;
|
||||||
this.init_config();
|
this.init_config();
|
||||||
this.init_errors();
|
this.init_errors();
|
||||||
@ -29,6 +30,7 @@ module.exports = {
|
|||||||
this.document = require('./model/document');
|
this.document = require('./model/document');
|
||||||
this.meta = require('./model/meta');
|
this.meta = require('./model/meta');
|
||||||
this.session_lib = require('./session');
|
this.session_lib = require('./session');
|
||||||
|
this.rest = require('./rest');
|
||||||
},
|
},
|
||||||
init_db() {
|
init_db() {
|
||||||
let database = require('./model/database');
|
let database = require('./model/database');
|
||||||
|
27
frappe/rest.js
Normal file
27
frappe/rest.js
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
const frappe = require('frappe-core');
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
setup(app) {
|
||||||
|
// get list
|
||||||
|
app.get('/api/resource/:doctype', function(request, response) {
|
||||||
|
return response.json(frappe.db.get_all(request.params.doctype, ['name', 'subject'], null,
|
||||||
|
start = request.params.start || 0, limit = request.params.limit || 20));
|
||||||
|
});
|
||||||
|
|
||||||
|
// create
|
||||||
|
app.post('/api/resource/:doctype', function(request, response) {
|
||||||
|
data = request.body;
|
||||||
|
data.doctype = request.params.doctype;
|
||||||
|
let doc = frappe.get_doc(data).insert();
|
||||||
|
frappe.db.write();
|
||||||
|
return response.json(doc.get_valid_dict());
|
||||||
|
});
|
||||||
|
|
||||||
|
// get list
|
||||||
|
app.get('/api/resource/:doctype/:name', function(request, response) {
|
||||||
|
let data = frappe.get_doc(request.params.doctype, request.params.name).get_valid_dict();
|
||||||
|
console.log(data);
|
||||||
|
return response.json(data);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
1400
package-lock.json
generated
1400
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@ -4,9 +4,15 @@
|
|||||||
"description": "Frappe Core",
|
"description": "Frappe Core",
|
||||||
"main": "frappe/index.js",
|
"main": "frappe/index.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"test": "mocha frappe/tests"
|
"test": "mocha frappe/tests",
|
||||||
|
"start": "nodemon frappe/app.js"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
"body-parser": "^1.18.2",
|
||||||
|
"express": "^4.16.2",
|
||||||
|
"ms": "^2.1.1",
|
||||||
|
"multer": "^1.3.0",
|
||||||
|
"nodemon": "^1.14.7",
|
||||||
"sql.js": "^0.4.0",
|
"sql.js": "^0.4.0",
|
||||||
"walk": "^2.3.9"
|
"walk": "^2.3.9"
|
||||||
},
|
},
|
||||||
@ -21,7 +27,6 @@
|
|||||||
},
|
},
|
||||||
"homepage": "https://github.com/frappe/frappe-js#readme",
|
"homepage": "https://github.com/frappe/frappe-js#readme",
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"babel-eslint": "^8.0.1",
|
|
||||||
"eslint": "^4.9.0",
|
"eslint": "^4.9.0",
|
||||||
"mocha": "^4.0.1",
|
"mocha": "^4.0.1",
|
||||||
"webpack": "^3.8.1"
|
"webpack": "^3.8.1"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user