Giter VIP home page Giter VIP logo

py-orm's Introduction

tiny-orm

A tiny ORM for Python and SQLite.

Install

pip install tiny-orm

Usage

Following a basic tutorial to demonstrate how to use the ORM.

  1. Define a Post model in a post.py file.

    # post.py
    from tiny_orm import Model
    
    class Post(Model):
    
        text = str  # other datatypes: int, float
    
        def __init__(self, text):
            self.text = text
  2. Import Database to create a data access object.

    >>> from tiny_orm import Database
    >>> db = Database('db.sqlite')  # indicating a database file.
  3. Import the Post model and link it to the database.

    >>> from post import Post
    >>> Post.db = db  # see another approach in tests.py
  4. Create a post and save it in the staging area (without commit) of database.

    >>> post = Post('Hello World').save()
    >>> print(post.id)  # auto generated id
    1
  5. Change the hello world post and update it in the database.

    >>> post = Post.manager().get(id=1)
    >>> post.text = 'Hello Mundo'
    >>> post.update()
    >>> post.text
    Hello Mundo
  6. Commit all staged operations (save and update) to the database.

    >>> db.commit()
  7. Delete the object and commit.

    >>> post.delete()
    >>> db.commit()
  8. Create a manager that can perform CRUD operations in the database.

    >>> objects = Post.manager(db)
  9. Save and get a post.

    >>> objects.save(Post('Hello', 'World'))
    >>> objects.get(2)  # get by id from the staging area
    {'text': 'World', 'id': 2, 'title': 'Hello'}
  10. Close the database without commit the changes

    >>> db.close()
  11. Get all posts from database.

    >>> list(objects.all())  # return a "empty" generator
    []

Linter

Check code lint:

pip install pylint
pylint orm.py

Contributing

See CONTRIBUTING.

License

The MIT License.

py-orm's People

Contributors

socket-socket 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.