@an5/adapters 0.1.1 → 0.1.3

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@an5/adapters",
3
- "version": "0.1.1",
3
+ "version": "0.1.3",
4
4
  "description": "Standalone language adapters for AN5 ORM - TypeScript, Python, .NET query execution.",
5
5
  "license": "MIT",
6
6
  "main": "typescript/index.js",
@@ -1,7 +1,10 @@
1
1
  import json
2
2
  import uuid
3
3
  from typing import Dict, List, Optional
4
- from .base import DIALECT_MSSQL, DIALECT_POSTGRES, model_fields, _build_order_by, _parse_where, _quote, _quote_table, _resolve_table
4
+ try:
5
+ from .base import DIALECT_MSSQL, DIALECT_POSTGRES, model_fields, _build_order_by, _parse_where, _quote, _quote_table, _resolve_table
6
+ except ImportError:
7
+ from base import DIALECT_MSSQL, DIALECT_POSTGRES, model_fields, _build_order_by, _parse_where, _quote, _quote_table, _resolve_table
5
8
 
6
9
  # ─── Table Client ───────────────────────────────────────────────────────────────────
7
10
 
@@ -23,13 +26,26 @@ class AdapterTableClient:
23
26
  def _nolock(self) -> str:
24
27
  return "" if self._dialect == DIALECT_POSTGRES else " WITH (NOLOCK)"
25
28
 
29
+ @property
30
+ def _fields(self) -> List[Dict]:
31
+ fields = model_fields.get(self._model, [])
32
+ if isinstance(fields, dict):
33
+ normalized = []
34
+ for name, value in fields.items():
35
+ if isinstance(value, dict):
36
+ normalized.append({"name": name, **value})
37
+ else:
38
+ normalized.append({"name": name, "type": value})
39
+ return normalized
40
+ return fields
41
+
26
42
  def _pagination(self, take: Optional[int], skip: int, order_sql: str) -> str:
27
43
  if take is None:
28
44
  return ""
29
45
  if self._dialect == DIALECT_POSTGRES:
30
46
  return f" LIMIT {take} OFFSET {skip}"
31
- o = order_sql or " ORDER BY (SELECT NULL)"
32
- return f"{o} OFFSET {skip} ROWS FETCH NEXT {take} ROWS ONLY"
47
+ order_prefix = "" if order_sql else " ORDER BY (SELECT NULL)"
48
+ return f"{order_prefix} OFFSET {skip} ROWS FETCH NEXT {take} ROWS ONLY"
33
49
 
34
50
  def find_many(self, where=None, order_by=None, skip: int = 0, take: Optional[int] = None, select=None) -> List[Dict]:
35
51
  params: Dict = {}
@@ -39,12 +55,9 @@ class AdapterTableClient:
39
55
  query = f"SELECT * FROM {self._table_sql}{self._nolock}"
40
56
  if where_sql:
41
57
  query += f" WHERE {where_sql}"
42
- if take is not None and self._dialect == DIALECT_POSTGRES:
43
- query += self._pagination(take, skip, order_sql)
44
- else:
45
- if order_sql:
46
- query += f" {order_sql}"
47
- query += self._pagination(take, skip, order_sql)
58
+ if order_sql:
59
+ query += f" {order_sql}"
60
+ query += self._pagination(take, skip, order_sql)
48
61
  return self._adapter.exec(query, list(params.values()))
49
62
 
50
63
  def find_first(self, where=None, order_by=None, select=None) -> Optional[Dict]:
@@ -64,8 +77,7 @@ class AdapterTableClient:
64
77
  return int(rows[0]["cnt"]) if rows else 0
65
78
 
66
79
  def create(self, data: Dict) -> Dict:
67
- fields = model_fields.get(self._model, [])
68
- id_field = next((f for f in fields if f.get("isId")), None)
80
+ id_field = next((f for f in self._fields if f.get("isId")), None)
69
81
  if id_field and id_field["name"] not in data:
70
82
  data = {**data, id_field["name"]: str(uuid.uuid4())}
71
83
 
@@ -205,5 +217,3 @@ class AdapterTableClient:
205
217
 
206
218
  scored.sort(key=lambda x: x[1])
207
219
  return [{**row, "distance": dist} for row, dist in scored[:take]]
208
-
209
-