sera-2 1.14.6__py3-none-any.whl → 1.15.0__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.
@@ -1104,7 +1104,19 @@ def make_typescript_data_model(schema: Schema, target_pkg: Package):
1104
1104
  for dep in tstype.deps:
1105
1105
  program.import_(dep, True)
1106
1106
  tsprop = [
1107
- (expr.ExprIdent("datatype"), expr.ExprConstant(tstype.type)),
1107
+ (
1108
+ expr.ExprIdent("datatype"),
1109
+ (
1110
+ expr.ExprConstant(tstype.type)
1111
+ if tstype.type not in schema.enums
1112
+ else expr.ExprConstant("enum")
1113
+ ),
1114
+ ),
1115
+ *(
1116
+ [(expr.ExprIdent("enumType"), expr.ExprIdent(tstype.type))]
1117
+ if tstype.type in schema.enums
1118
+ else []
1119
+ ),
1108
1120
  (
1109
1121
  expr.ExprIdent("isRequired"),
1110
1122
  expr.ExprConstant(
sera/misc/__init__.py CHANGED
@@ -3,6 +3,7 @@ from sera.misc._utils import (
3
3
  assert_isinstance,
4
4
  assert_not_null,
5
5
  filter_duplication,
6
+ load_data,
6
7
  to_camel_case,
7
8
  to_pascal_case,
8
9
  to_snake_case,
@@ -18,4 +19,5 @@ __all__ = [
18
19
  "to_pascal_case",
19
20
  "Formatter",
20
21
  "File",
22
+ "load_data",
21
23
  ]
sera/misc/_utils.py CHANGED
@@ -1,8 +1,14 @@
1
1
  from __future__ import annotations
2
2
 
3
3
  import re
4
+ from pathlib import Path
4
5
  from typing import Any, Callable, Iterable, Optional, TypeVar
5
6
 
7
+ import serde.csv
8
+ from sqlalchemy import Engine, text
9
+ from sqlalchemy.orm import Session
10
+ from tqdm import tqdm
11
+
6
12
  T = TypeVar("T")
7
13
  reserved_keywords = {
8
14
  "and",
@@ -102,3 +108,43 @@ def filter_duplication(
102
108
  keys.add(k)
103
109
  new_lst.append(k)
104
110
  return new_lst
111
+
112
+
113
+ def load_data(
114
+ engine: Engine,
115
+ create_db_and_tables: Callable[[], None],
116
+ table_files: list[tuple[type, Path]],
117
+ table_desers: dict[type, Callable[[dict], Any]],
118
+ verbose: bool = False,
119
+ ):
120
+ """
121
+ Load data into the database from specified CSV files.
122
+
123
+ Args:
124
+ engine: SQLAlchemy engine to connect to the database.
125
+ create_db_and_tables: Function to create database and tables.
126
+ table_files: List of tuples containing the class type and the corresponding CSV file path.
127
+ table_desers: Dictionary mapping class types to their deserializer functions.
128
+ verbose: If True, show progress bars during loading.
129
+ """
130
+ with Session(engine) as session:
131
+ create_db_and_tables()
132
+
133
+ for tbl, file in tqdm(table_files, disable=not verbose, desc="Loading data"):
134
+ if file.name.endswith(".csv"):
135
+ records = serde.csv.deser(file, deser_as_record=True)
136
+ else:
137
+ raise ValueError(f"Unsupported file format: {file.name}")
138
+ deser = table_desers[tbl]
139
+ records = [deser(row) for row in records]
140
+ for r in tqdm(records, desc=f"load {tbl.__name__}", disable=not verbose):
141
+ session.merge(r)
142
+ session.flush()
143
+
144
+ # Reset the sequence for each table
145
+ session.execute(
146
+ text(
147
+ f"SELECT setval('{tbl.__tablename__}_id_seq', (SELECT MAX(id) FROM \"{tbl.__tablename__}\"));"
148
+ )
149
+ )
150
+ session.commit()
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.3
2
2
  Name: sera-2
3
- Version: 1.14.6
3
+ Version: 1.15.0
4
4
  Summary:
5
5
  Author: Binh Vu
6
6
  Author-email: bvu687@gmail.com
@@ -19,10 +19,10 @@ sera/make/make_app.py,sha256=n9NtW73O3s_5Q31VHIRmnd-jEIcpDO7ksAsOdovde2s,5999
19
19
  sera/make/make_python_api.py,sha256=iXGbKQ3IJvsY1ur_fhurr_THFNnH66E3Wl85o0emUbw,26853
20
20
  sera/make/make_python_model.py,sha256=cRb-fuHX0WH7XPAAliu6lycC0iEjE5kcKg4bBU40GwQ,61275
21
21
  sera/make/make_python_services.py,sha256=0ZpWLwQ7Nwfn8BXAikAB4JRpNknpSJyJgY5b1cjtxV4,2073
22
- sera/make/make_typescript_model.py,sha256=4yLCLIzP2jeXfmOeee6nNS_qd7_7iPinV2vQh1IfbLs,64000
23
- sera/misc/__init__.py,sha256=Dh4uDq0D4N53h3zhvmwfa5a0TPVRSUvLzb0hkFuPirk,411
22
+ sera/make/make_typescript_model.py,sha256=3F-1TAbVb2-vidj5wKIi_U442JkBXZU3UYCZkodWRRM,64466
23
+ sera/misc/__init__.py,sha256=t-Z3Zv-a_W0n59RSBoeGMqNEiavjc4vCoRdz-GCKp3Q,443
24
24
  sera/misc/_formatter.py,sha256=aCGYL08l8f3aLODHxSocxBBwkRYEo3K1QzCDEn3suj0,1685
25
- sera/misc/_utils.py,sha256=vBfbEChf7IMdLDj3CbdOXTUAdWNQTLLpAWwBoUcF3u0,2315
25
+ sera/misc/_utils.py,sha256=EvWCHquEkqMq6C4vCy9kXsirD9Nuq3KmNMjWTKjPuts,3993
26
26
  sera/models/__init__.py,sha256=vJC5Kzo_N7wd16ocNPy1VvAZDGNiWeiAhWJ4ihATKvA,780
27
27
  sera/models/_class.py,sha256=1J4Bd_LanzhhDWwZFHWGtFYD7lupe_alaB3D02ebNDI,2862
28
28
  sera/models/_collection.py,sha256=ZnQEriKC4X88Zz48Kn1AVZKH-1_l8OgWa-zf2kcQOOE,1414
@@ -36,6 +36,6 @@ sera/models/_parse.py,sha256=MaGZty29lsVUFumWqNanIjAwptBNOMbVZMXLZ2A9_2g,12317
36
36
  sera/models/_property.py,sha256=2rSLs9JjSesrxrEugE7krYaBQOivKU882I8mZN94FlI,7017
37
37
  sera/models/_schema.py,sha256=VxJEiqgVvbXgcSUK4UW6JnRcggk4nsooVSE6MyXmfNY,1636
38
38
  sera/typing.py,sha256=Fl4-UzLJu1GdLLk_g87fA7nT9wQGelNnGzag6dg_0gs,980
39
- sera_2-1.14.6.dist-info/METADATA,sha256=XuXxkDt84tayCblVI10S594d0t3e0eg5j66iUCB-PKQ,852
40
- sera_2-1.14.6.dist-info/WHEEL,sha256=b4K_helf-jlQoXBBETfwnf4B04YC67LOev0jo4fX5m8,88
41
- sera_2-1.14.6.dist-info/RECORD,,
39
+ sera_2-1.15.0.dist-info/METADATA,sha256=KhiL3wTbAKSWXp40eoNTpZ86X8DFgPvIDloXnBGU1tQ,852
40
+ sera_2-1.15.0.dist-info/WHEEL,sha256=b4K_helf-jlQoXBBETfwnf4B04YC67LOev0jo4fX5m8,88
41
+ sera_2-1.15.0.dist-info/RECORD,,