1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
|
# frest
How to create a REST API very quickly.
## Installation
Install **frest** by pip package (you must have Python>=3.6). In this example I'm using [virtualev](https://pypi.org/project/virtualenv/), a tool for virtual environment creation.
```
$ virtualenv env
$ source env/bin/activate
(env) $ pip install frest
```
## Use
#### Create a project
```
(env) $ frest --init project
__ _
/ _| | |
| |_ _ __ ___ ___| |_
| _| '__/ _ \/ __| __|
| | | | | __/\__ \ |_
|_| |_| \___||___/\__|
Create project/... OK
Create project/__init__.py... OK
```
It will create a project tree folder like this one:
* database.py
A file with DATABASE_URI, database path, default is `sqlite:///database.db`.
* mail.py
A file with mail configuration: server, port, password etc.
* app.py
A file with routes and mail/database configuration.
* wsgi.py
File used for production.
#### Create first app
Inside `project` folder, we start to create a new app. For example, we want to store books inside our database.
```
(env) $ cd project/
(env) $ frest --startapp book
```
It will print something like this:
```
__ _
/ _| | |
| |_ _ __ ___ ___| |_
| _| '__/ _ \/ __| __|
| | | | | __/\__ \ |_
|_| |_| \___||___/\__|
Create scheme/... OK
Create scheme/book... OK
Create scheme/book/__init__.py... OK
Create model for book...
Fields id, created_at, updated_at are default on every new model, you can delete it from model file
Create field? (Y/n):
```
Now we have to define a model for our `book`, using 6 data types: integer, string, text, datetime, float, bool.
This is an example of our `book` with **name** and **pubblication date**:
```
Choose field name: name
Choose field type: int, str, text, datetime, float, bool str
Choose string size: 20
Field is nullable? (Y/n): n
Create new field? (Y/n):
Choose field name: pubblication_date
Choose field type: int, str, text, datetime, float, bool datetime
Field is nullable? (Y/n): n
Create new field? (Y/n): n
Create scheme/book/models.py... OK
Create scheme/book/forms.py... OK
Create scheme/book/routes.py... OK
```
Now, our book it will be inside `scheme/book`.
#### Move scheme routes inside app
Edit `project/app.py` and put your routes inside.
```
[...]
from frest.auth.routes import api as api_users
from scheme.book.routes import api as api_books
[...]
app.register_blueprint(api_books)
[...]
```
#### Database creation
Open python interpreter, import db and create.
```
from app import db
db.create_all()
```
Now exit and check your scheme inside database. It will be something like this.
```
CREATE TABLE user (
"userId" INTEGER NOT NULL,
email VARCHAR(30),
password VARCHAR(30),
is_admin BOOLEAN,
name VARCHAR(30),
created_at DATETIME,
PRIMARY KEY ("userId"),
CHECK (is_admin IN (0, 1))
);
CREATE TABLE book (
"bookId" INTEGER NOT NULL,
name VARCHAR(20) NOT NULL,
pubblication_date DATETIME NOT NULL,
created_at DATETIME,
updated_at DATETIME,
PRIMARY KEY ("bookId")
);
CREATE TABLE token (
"tokenId" INTEGER NOT NULL,
string VARCHAR(20),
created_at DATETIME,
expired BOOLEAN,
user_id INTEGER NOT NULL,
PRIMARY KEY ("tokenId"),
CHECK (expired IN (0, 1)),
FOREIGN KEY(user_id) REFERENCES user ("userId")
);
```
#### Start app
Inside `project` file you have to run `python app.py` and it's done.
## TODO
- [ ] Migrations of database in case of edits
- [ ] Put automatically `routes` inside app file
- [ ] More datatype
- [ ] Relantionships (1-to-1, 1-to-n, n-to-n)
|