# Path: aiohttp_json_api/helpers.py
# def first(iterable, default=None, key=None):
#     """
#     Return first element of *iterable*.
# 
#     Return first element of *iterable* that evaluates to ``True``, else
#     return ``None`` or optional *default*.
# 
#     >>> first([0, False, None, [], (), 42])
#     42
#     >>> first([0, False, None, [], ()]) is None
#     True
#     >>> first([0, False, None, [], ()], default='ohai')
#     'ohai'
#     >>> import re
#     >>> m = first(re.match(regex, 'abc') for regex in ['b.*', 'a(.*)'])
#     >>> m.group(1)
#     'bc'
# 
#     The optional *key* argument specifies a one-argument predicate function
#     like that used for *filter()*.  The *key* argument, if supplied, should be
#     in keyword form. For example, finding the first even number in an iterable:
# 
#     >>> first([1, 1, 3, 4, 5], key=lambda x: x % 2 == 0)
#     4
# 
#     Contributed by Hynek Schlawack, author of `the original standalone module`_
# 
#     .. _the original standalone module: https://github.com/hynek/first
#     """
#     return next(filter(key, iterable), default)
# 
# def make_sentinel(name='_MISSING', var_name=None):
#     """
#     Create sentinel instance.
# 
#     Creates and returns a new **instance** of a new class, suitable for
#     usage as a "sentinel", a kind of singleton often used to indicate
#     a value is missing when ``None`` is a valid input.
# 
#     >>> make_sentinel(var_name='_MISSING')
#     _MISSING
# 
#     The most common use cases here in project are as default values
#     for optional function arguments, partly because of its
#     less-confusing appearance in automatically generated
#     documentation. Sentinels also function well as placeholders in queues
#     and linked lists.
# 
#     .. note::
# 
#       By design, additional calls to ``make_sentinel`` with the same
#       values will not produce equivalent objects.
# 
#       >>> make_sentinel('TEST') == make_sentinel('TEST')
#       False
#       >>> type(make_sentinel('TEST')) == type(make_sentinel('TEST'))
#       False
# 
#     :arg str name:
#         Name of the Sentinel
#     :arg str var_name:
#         Set this name to the name of the variable in its respective
#         module enable pickleability.
#     """
#     class Sentinel(object):
#         def __init__(self):
#             self.name = name
#             self.var_name = var_name
# 
#         def __repr__(self):
#             if self.var_name:
#                 return self.var_name
#             return '%s(%r)' % (self.__class__.__name__, self.name)
# 
#         if var_name:
#             def __reduce__(self):
#                 return self.var_name
# 
#         def __nonzero__(self):
#             return False
# 
#         __bool__ = __nonzero__
# 
#     return Sentinel()


from collections import OrderedDict
from typing import NamedTuple, Union, Optional, List
from aiopg.sa import SAConnection
from sqlalchemy.sql.selectable import CTE
from trafaret.contrib.rfc_3339 import DateTime, Date
from aiohttp_json_api.helpers import first, make_sentinel
import datetime
import inflection
import trafaret as t
import sqlalchemy as sa
import examples.fantasy.tables as tbl
        )

    @classmethod
    def not_populated(cls, id):
        return cls(id=id,
                   name=NON_POPULATED,
                   books=NON_POPULATED,
                   created_at=NON_POPULATED,
                   updated_at=NON_POPULATED)

    @classmethod
    async def fetch_many(cls, conn: SAConnection, cte: CTE = None):
        results = OrderedDict()

        if cte is None:
            cte = cls.Options.db_table

        query = (
            cte
            .outerjoin(tbl.books_stores)
            .outerjoin(tbl.books,
                       tbl.books_stores.c.book_id == tbl.books.c.id)
            .select(use_labels=True)
        )

        books = {}

        async for row in conn.execute(query):
            store_id = row[cte.c.id]
            store = results.get(store_id)

# Path: aiohttp_json_api/helpers.py
# def first(iterable, default=None, key=None):
#     """
#     Return first element of *iterable*.
# 
#     Return first element of *iterable* that evaluates to ``True``, else
#     return ``None`` or optional *default*.
# 
#     >>> first([0, False, None, [], (), 42])
#     42
#     >>> first([0, False, None, [], ()]) is None
#     True
#     >>> first([0, False, None, [], ()], default='ohai')
#     'ohai'
#     >>> import re
#     >>> m = first(re.match(regex, 'abc') for regex in ['b.*', 'a(.*)'])
#     >>> m.group(1)
#     'bc'
# 
#     The optional *key* argument specifies a one-argument predicate function
#     like that used for *filter()*.  The *key* argument, if supplied, should be
#     in keyword form. For example, finding the first even number in an iterable:
# 
#     >>> first([1, 1, 3, 4, 5], key=lambda x: x % 2 == 0)
#     4
# 
#     Contributed by Hynek Schlawack, author of `the original standalone module`_
# 
#     .. _the original standalone module: https://github.com/hynek/first
#     """
#     return next(filter(key, iterable), default)
# 
# def make_sentinel(name='_MISSING', var_name=None):
#     """
#     Create sentinel instance.
# 
#     Creates and returns a new **instance** of a new class, suitable for
#     usage as a "sentinel", a kind of singleton often used to indicate
#     a value is missing when ``None`` is a valid input.
# 
#     >>> make_sentinel(var_name='_MISSING')
#     _MISSING
# 
#     The most common use cases here in project are as default values
#     for optional function arguments, partly because of its
#     less-confusing appearance in automatically generated
#     documentation. Sentinels also function well as placeholders in queues
#     and linked lists.
# 
#     .. note::
# 
#       By design, additional calls to ``make_sentinel`` with the same
#       values will not produce equivalent objects.
# 
#       >>> make_sentinel('TEST') == make_sentinel('TEST')
#       False
#       >>> type(make_sentinel('TEST')) == type(make_sentinel('TEST'))
#       False
# 
#     :arg str name:
#         Name of the Sentinel
#     :arg str var_name:
#         Set this name to the name of the variable in its respective
#         module enable pickleability.
#     """
#     class Sentinel(object):
#         def __init__(self):
#             self.name = name
#             self.var_name = var_name
# 
#         def __repr__(self):
#             if self.var_name:
#                 return self.var_name
#             return '%s(%r)' % (self.__class__.__name__, self.name)
# 
#         if var_name:
#             def __reduce__(self):
#                 return self.var_name
# 
#         def __nonzero__(self):
#             return False
# 
#         __bool__ = __nonzero__
# 
#     return Sentinel()


# Path: examples/fantasy/models.py
from collections import OrderedDict
from typing import NamedTuple, Union, Optional, List
from aiopg.sa import SAConnection
from sqlalchemy.sql.selectable import CTE
from trafaret.contrib.rfc_3339 import DateTime, Date
from aiohttp_json_api.helpers import first, make_sentinel
import datetime
import inflection
import trafaret as t
import sqlalchemy as sa
import examples.fantasy.tables as tbl

        )

    @classmethod
    def not_populated(cls, id):
        return cls(id=id,
                   name=NON_POPULATED,
                   books=NON_POPULATED,
                   created_at=NON_POPULATED,
                   updated_at=NON_POPULATED)

    @classmethod
    async def fetch_many(cls, conn: SAConnection, cte: CTE = None):
        results = OrderedDict()

        if cte is None:
            cte = cls.Options.db_table

        query = (
            cte
            .outerjoin(tbl.books_stores)
            .outerjoin(tbl.books,
                       tbl.books_stores.c.book_id == tbl.books.c.id)
            .select(use_labels=True)
        )

        books = {}

        async for row in conn.execute(query):
            store_id = row[cte.c.id]
            store = results.get(store_id)

            if store is None: