sera-2 1.1.0__py3-none-any.whl → 1.2.1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
sera/libs/base_orm.py CHANGED
@@ -6,15 +6,14 @@ import orjson
6
6
  from sqlalchemy import LargeBinary, TypeDecorator
7
7
  from sqlalchemy import create_engine as sqlalchemy_create_engine
8
8
  from sqlalchemy import update
9
- from sqlalchemy.orm import DeclarativeBase, Session
10
9
 
11
10
 
12
- class BaseORM(DeclarativeBase):
11
+ class BaseORM:
13
12
 
14
13
  def get_update_query(self):
15
14
  q = update(self.__class__)
16
15
  args = {}
17
- for col in self.__table__.columns:
16
+ for col in self.__table__.columns: # type: ignore
18
17
  val = getattr(self, col.name)
19
18
  if col.primary_key:
20
19
  q = q.where(getattr(self.__class__, col.name) == val)
@@ -23,7 +22,7 @@ class BaseORM(DeclarativeBase):
23
22
  return q.values(**args)
24
23
 
25
24
  def get_update_args(self):
26
- return {col.name: getattr(self, col.name) for col in self.__table__.columns}
25
+ return {col.name: getattr(self, col.name) for col in self.__table__.columns} # type: ignore
27
26
 
28
27
  @classmethod
29
28
  def from_dict(cls, data: dict):
sera/libs/base_service.py CHANGED
@@ -29,13 +29,13 @@ Query = Annotated[
29
29
  dict[FieldName, dict[QueryOp, Annotated[Any, doc("query value")]]],
30
30
  doc("query operations"),
31
31
  ]
32
- C = TypeVar("C", bound=BaseORM)
33
- ID = Annotated[TypeVar("ID"), doc("ID of a class")]
32
+ R = TypeVar("R", bound=BaseORM)
33
+ ID = TypeVar("ID") # ID of a class
34
34
 
35
35
 
36
- class BaseService(Generic[ID, C]):
36
+ class BaseService(Generic[ID, R]):
37
37
 
38
- def __init__(self, cls: Class, orm_cls: type[C]):
38
+ def __init__(self, cls: Class, orm_cls: type[R]):
39
39
  self.cls = cls
40
40
  self.orm_cls = orm_cls
41
41
  self.id_prop = assert_not_null(cls.get_id_property())
@@ -51,7 +51,8 @@ class BaseService(Generic[ID, C]):
51
51
  sorted_by: list[str],
52
52
  group_by: list[str],
53
53
  fields: list[str],
54
- ) -> Sequence[C]:
54
+ session: Session,
55
+ ) -> Sequence[R]:
55
56
  """Retrieving records matched a query.
56
57
 
57
58
  Args:
@@ -65,7 +66,7 @@ class BaseService(Generic[ID, C]):
65
66
  """
66
67
  return []
67
68
 
68
- def get_by_id(self, id: ID, session: Session) -> Optional[C]:
69
+ def get_by_id(self, id: ID, session: Session) -> Optional[R]:
69
70
  """Retrieving a record by ID."""
70
71
  q = select(self.orm_cls).where(self._cls_id_prop == id)
71
72
  result = session.execute(q).scalar_one_or_none()
@@ -76,3 +77,15 @@ class BaseService(Generic[ID, C]):
76
77
  q = exists().where(self._cls_id_prop == id)
77
78
  result = session.query(q).scalar()
78
79
  return bool(result)
80
+
81
+ def create(self, record: R, session: Session) -> R:
82
+ """Create a new record."""
83
+ session.add(record)
84
+ session.commit()
85
+ return record
86
+
87
+ def update(self, record: R, session: Session) -> R:
88
+ """Update an existing record."""
89
+ session.add(record)
90
+ session.commit()
91
+ return record
sera/make/make_app.py CHANGED
@@ -13,7 +13,7 @@ from sera.make.make_python_model import (
13
13
  make_python_relational_model,
14
14
  )
15
15
  from sera.make.make_python_services import make_python_service_structure
16
- from sera.models import App, DataCollection, parse_schema
16
+ from sera.models import App, DataCollection, Language, parse_schema
17
17
 
18
18
 
19
19
  def make_config(app: App):
@@ -37,7 +37,7 @@ def make_config(app: App):
37
37
  )(
38
38
  lambda ast01: ast01.assign(
39
39
  DeferredVar.simple("CFG_FILE"),
40
- expr.ExprRawPython("Path(__file__).parent.parent / 'config.yaml'"),
40
+ expr.ExprRawPython("Path(__file__).parent.parent / 'config.yml'"),
41
41
  ),
42
42
  ),
43
43
  lambda ast: ast.else_()(
@@ -107,10 +107,16 @@ def make_app(
107
107
  list[str],
108
108
  doc("API collections to generate."),
109
109
  ],
110
+ language: Annotated[
111
+ Language,
112
+ doc(
113
+ "Language of the generated application. Currently only Python is supported."
114
+ ),
115
+ ] = Language.Python,
110
116
  ):
111
117
  schema = parse_schema(schema_files)
112
118
 
113
- app = App(app_dir.name, app_dir, schema_files)
119
+ app = App(app_dir.name, app_dir, schema_files, language)
114
120
 
115
121
  # generate application configuration
116
122
  make_config(app)