Giter VIP home page Giter VIP logo

sqla-wrapper's Introduction

SQLA-wrapper Build Status Coverage Status

A friendly wrapper for SQLAlchemy.

Why?

SQLAlchemy is great, but can be difficult to set up. With SQLA-Wrapper you can quickly start like:

from sqla_wrapper import SQLAlchemy

db = SQLAlchemy('sqlite:///:memory:')

class User(db.Model):
    __tablename__ "users"
    id = db.Column(db.Integer, primary_key=True)
    ...

db.create_all()
todos = db.query(User.id, User.title).all()

instead of having to write something like:

# Who's going to remember all of this?
from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker, Column, Integer

engine = create_engine('sqlite:///:memory:')
Session = sessionmaker(bind=engine)
session = Session()
Model = declarative_base()

class User(Model):
    __tablename__ "users"
    id = Column(Integer, primary_key=True)
    ...

Model.metadata.create_all(engine)
session = Session()
todos = session.query(User).all()

Installation

Install the package using Pypi:

python -m pip install sqla-wrapper

Basic usage

from sqla_wrapper import SQLAlchemy

db = SQLAlchemy('sqlite:///:memory:')

class User(db.Model):
    __tablename__ "users"
    id = db.Column(db.Integer, primary_key=True)
    ...

db.create_all()

db.add(User(...))
db.commit()

todos = db.query(User).all()

Compared to SQLAlchemy

Compared to plain SQLAlchemy, you need to know that:

The SQLAlchemy gives you access to the following things:

  • All the functions and classes from sqlalchemy and sqlalchemy.orm
  • All the functions from a preconfigured scoped session (called _session).
  • The ~SQLAlchemy.metadata and ~SQLAlchemy.engine
  • The methods SQLAlchemy.create_all and SQLAlchemy.drop_all to create and drop tables according to the models.
  • A Model baseclass that is a configured declarative base. This model has a few utility methods:
class Model(Object):
    @classmethod
    def exists(cls, **attrs):
        """Returns whether an object with these attributes exists."""

    @classmethod
    def create(cls, **attrs):
        """Create and persist a new record for the model."""

    @classmethod
    def create_or_first(cls, **attrs):
        """Tries to create a new record, and if it fails
        because already exists, return the first it founds."""

    @classmethod
    def first(cls, **attrs):
        """Returns the first object found with these attributes."""
    
    def save(self):
        """Saves the updated model to the current entity db and commits."""

    def delete(self):
        """Removes the model from the current session and commits."""

This model class also generates a default repr for your models, based on their class names an primary keys.

sqla-wrapper's People

Contributors

jpsca avatar jcn avatar vsurjaninov avatar sloria avatar imran2140 avatar ondoheer avatar curiousdima avatar glogiotatidis avatar ramuta avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.