|
| 1 | +# Tutorial |
| 2 | + |
| 3 | +In this tutorial we'll show you how to create a simple PFunk project. Let's call our project **SchoolZone**, a |
| 4 | +demo app for tracking students and teachers in a school. |
| 5 | + |
| 6 | +## Step 1: Create New Project |
| 7 | + |
| 8 | +Let's use the PFunk CLI to create a new project. |
| 9 | + |
| 10 | +```python |
| 11 | +pfunk init schoolzone |
| 12 | +``` |
| 13 | + |
| 14 | +## Step 2: Create Collections |
| 15 | + |
| 16 | +Let's create some collections to store our data in. If you're new to Fauna, collections are the equivalent to tables in |
| 17 | +relational database. |
| 18 | + |
| 19 | +```python |
| 20 | +from pfunk.collection import Collection |
| 21 | +from pfunk.fields import StringField, IntegerField, ReferenceField, ManyToManyField |
| 22 | +from pfunk.contrib.auth.collections import User, Group |
| 23 | +from pfunk.contrib.auth.resources import GenericGroupBasedRole |
| 24 | + |
| 25 | + |
| 26 | +class School(Collection): |
| 27 | + collection_roles = [GenericGroupBasedRole] |
| 28 | + name = StringField(required=True) |
| 29 | + address = StringField(required=False) |
| 30 | + group = ReferenceField(Group, required=True) |
| 31 | + |
| 32 | + |
| 33 | +class Teacher(Collection): |
| 34 | + collection_roles = [GenericGroupBasedRole] |
| 35 | + first_name = StringField(required=True) |
| 36 | + last_name = StringField(required=True) |
| 37 | + group = ReferenceField(Group, required=True) |
| 38 | + |
| 39 | + |
| 40 | +class Class(Collection): |
| 41 | + collection_roles = [GenericGroupBasedRole] |
| 42 | + name = StringField(required=True) |
| 43 | + teacher = ReferenceField(Teacher) |
| 44 | + group = ReferenceField(Group, required=True) |
| 45 | + |
| 46 | + |
| 47 | +class Student(Collection): |
| 48 | + collection_roles = [GenericGroupBasedRole] |
| 49 | + first_name = StringField(required=True) |
| 50 | + last_name = StringField(required=True) |
| 51 | + grade = IntegerField(required=True) |
| 52 | + group = ReferenceField(Group, required=True) |
| 53 | + |
| 54 | + |
| 55 | +class StudentClass(Collection): |
| 56 | + collection_roles = [GenericGroupBasedRole] |
| 57 | + class_ref = ReferenceField(Class) |
| 58 | + student_ref = ReferenceField(Student) |
| 59 | + final_grade = IntegerField(required=False) |
| 60 | + absences = IntegerField() |
| 61 | + tardies = IntegerField() |
| 62 | + group = ReferenceField(Group, required=True) |
| 63 | + |
| 64 | + |
| 65 | + |
| 66 | +``` |
| 67 | +## Step 3: Add Collections to Project |
| 68 | + |
| 69 | +Open `schoolzone/project.py` and add the collections. |
| 70 | + |
| 71 | +```python |
| 72 | + |
| 73 | +from pfunk import Project |
| 74 | +from pfunk.contrib.auth.collections import Key |
| 75 | +from .collections import School, Teacher, Class, Student, StudentClass, User, Group |
| 76 | + |
| 77 | + |
| 78 | +project = Project(_collections=[User, Group, School, Teacher, Class, Student, StudentClass]) |
| 79 | +``` |
| 80 | + |
| 81 | +## Step 4: Seed Keys |
| 82 | + |
| 83 | +PFunk uses encrypted JWTs with private keys that unencrypt the JWT during request/response loop. We need to seed the keys. |
| 84 | + |
| 85 | +```commandline |
| 86 | +pfunk seed_keys staging |
| 87 | +``` |
| 88 | + |
| 89 | +## Step 5: Run the Local Server |
| 90 | + |
| 91 | +Next, we run the local server to test things out. |
| 92 | + |
| 93 | +```commandline |
| 94 | +pfunk local |
| 95 | +``` |
| 96 | + |
| 97 | +## Step 6: Deploy |
| 98 | + |
| 99 | +Publish the GraphQL schema to Fauna and deploy the API to Lambda and API Gateway. |
| 100 | + |
| 101 | +```commandline |
| 102 | +pfunk deploy staging |
| 103 | +``` |
| 104 | + |
| 105 | +If you don't want to deploy the API and just want to publish the GraphQL schema to Fauna. |
| 106 | + |
| 107 | +```commandline |
| 108 | +pfunk publish staging |
| 109 | +``` |
0 commit comments