Giter Site home page Giter Site logo

sqla's Introduction

SQLA

This project contains a declarative base with some common mixins. Pre-Structured to avoid circular dependencies.

Usage

Making a Model

from sqlalchemy import Column, ForeignKey, Integer, VARCHAR
from sqlalchemy.orm import relationship
from sqla.common.bases import Base
from sqla.internals.declarative_base import DECLARATIVE_BASE


class Parent(DECLARATIVE_BASE, Base):
    id = Column(Integer, primary_key=True, autoincrement=True)
    name = Column(VARCHAR(10))
    children = relationship("Child")


class Child(DECLARATIVE_BASE, Base):
    id = Column(Integer, primary_key=True)
    parent_id = Column(Integer, ForeignKey("parent.id"))

Using a Model

from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from sqla import BaseSession
from my.models.location import MyModel

engine = create_engine('sqlite:///mydatabase.sqlite')
Session = sessionmaker(engine)
# Initialize models by injecting session
# NOTE: We are only passing the session object,
# not invoking it.
MyModel.set_session(Session)

search_params = { 'name': 'Luke' }

# Adding an entry from json.
# Returns MyModel instance that was commited to session.
p = MyModel.from_json(search_params).save()

# Using the returned object that just got saved
# and serializing it to json.
print(c.serialize())

# Querying: get all results based on search_params
# and serialize to list of json.
p = MyModel.serialize_list(Parent.filter_by_all(**search_params))
print(p)

# Querying: get first result based on search_params
# and serialize to json.
p = (MyModel.filter_by_first(**search_params)).serialize()
print(p)

# Deleting an entry from json.
(Parent.filter_by_first(**search_params)).delete()

# Querying: using the traditional query from SQLAlchemy
# Note that "query" is invoked. Returns a SQLAlchemy session
# query object.
p = Parent.query().all()
print(p)


# There may be some more examples, but these are the ones I am using.

Contributing

If you can, send me a PR with fixes. If you can't, create an issue.

sqla's People

Contributors

papatemporal avatar

Watchers

 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.