Permalink
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
95 lines (66 sloc) 3.31 KB
import datetime
# all our models will go in this file
# peewee is similar to mongoose
from peewee import *
# import * means everything, including:
# SqliteDatabase -- adapter that lets us connect to sqlite databases (see below)
# and
# Model -- the Model() class is what we will inherit from when defining
# our models (similar to using some stuff from the mongoose module when
# creating a schema and then a model in mongoose)
# sqlite is a way to have a "database" that's just stored in a file
# it's great for development because you can have easily portable data (on git, eg)
# later when deploying, we will change this to psql
DATABASE = SqliteDatabase('dogs.sqlite')
# analogous to MONGO_DB_URL = 'mongodb://localhost/dogs', {...}
# we will use this module to setup user model, sessions, logins, auth
# require auth for certain things
# https://flask-login.readthedocs.io/en/latest/
from flask_login import UserMixin
# to behave correctly in flask-login's session/login/etc functionality
# the User class must have some methods and properties that model from peewee
# it doesn't have
# we could write these ourselves, and/or we could also have our User class
# inheirt from UserMixin (in addition to peewees model class), which
# will provide/implement them for us
class User(UserMixin, Model):
username = CharField(unique=True)
email = CharField(unique=True) # in sql unique is a constraint
password = CharField()
class Meta:
database = DATABASE
# defining our Dog model
# note Dog inherits from Model, this gives us methods to do CRUD actions
# and to define attribute/field/column names and types
# see here: http://docs.peewee-orm.com/en/latest/peewee/models.html#
# http://docs.peewee-orm.com/en/latest/peewee/models.html#fields
class Dog(Model):
name = CharField() # string
# we will need a foreign key
owner = ForeignKeyField(User, backref='dogs') # string for now, later we will implement a relation
# if we had a dog model in a variable called some dog, the backref
# 'some_dog.owner' to get the owner
# and if we had the user model in a var called some_user, the backref
# will allow us to go some_user.dogs to get a list of dog instances
breed = CharField()
# this is how you specify default values
created_at = DateTimeField(default=datetime.datetime.now)
# we often need to import things in python that might be built in in other langs
# this keeps python lightweight
# special constructor that gives our class instructions on how to
# connect to a specific database
class Meta:
database = DATABASE # use the DB defined above as DATABASE for this model
# define a method that will get called in app.py to set up DBs/connection
# similar to how we ran /db/db.js in server.js in express unit
def initialize(): # NOTE we are making this name up
DATABASE.connect() # analogous to mongoose.connect(...)
# we need to explicitly create the tables based on the schema definitions above
# create_tables first arg is list of tables to create
# second argument -- safe=True -- this means create tables only if it
# doesn't already exist
DATABASE.create_tables([User, Dog], safe=True)
print("Connected to DB and created tables if they weren't already there")
# with SQL, don't leave DB connection open, we don't want to hog up space
# in the connection pool
DATABASE.close()