mlmongo 2.0.2__tar.gz → 2.0.4__tar.gz

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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: mlmongo
3
- Version: 2.0.2
3
+ Version: 2.0.4
4
4
  Summary: mongodb数据库操作工具类
5
5
  Home-page: https://www.python.org
6
6
  Author: mengling
@@ -73,6 +73,7 @@ class AMongoModel(_MongoModel):
73
73
  @classmethod
74
74
  def mg_find_onekey_all(cls, key:str, *querys: dict,
75
75
  sort_map: dict[str, int]=None, skip:int=None, limit:int=None)->AMGIt[Any]:
76
+ key = str(key)
76
77
  cursor = cls.MGConfig.getConnect().find(MGFunction.and_(*querys), {'_id':0, key:1})
77
78
  if sort_map: cursor=cursor.sort(sort_map)
78
79
  if skip: cursor=cursor.skip(skip)
@@ -86,12 +87,14 @@ class AMongoModel(_MongoModel):
86
87
  @classmethod
87
88
  @_limit
88
89
  async def mg_find_onecol(cls, key:str, *querys: dict, default=None):
90
+ key = str(key)
89
91
  dt = (await cls.MGConfig.getConnect().find_one(MGFunction.and_(*querys), {'_id':0, key:1})) or {}
90
92
  return dt.get(key, default)
91
93
 
92
94
  @classmethod
93
95
  @_limit
94
96
  async def mg_id_find_onecol(cls, key:str, *ids:str|object|Any, default=None):
97
+ key = str(key)
95
98
  dt = (await cls.MGConfig.getConnect().find_one(cls.mgt_get_id_query(ids), {'_id':0, key:1})) or {}
96
99
  return dt.get(key, default)
97
100
 
@@ -59,6 +59,8 @@ class MGIt(Generic[T]):
59
59
 
60
60
  class _MGFieldInfo(FieldInfo):
61
61
  def __init__(self, **kwargs):
62
+ # 用于同步_id字段的内容
63
+ self.sync_id = kwargs.pop('sync_id', False)
62
64
  super().__init__(**kwargs)
63
65
  self.fkey = None
64
66
 
@@ -67,18 +69,21 @@ class _MGFieldInfo(FieldInfo):
67
69
 
68
70
  def __str__(self):
69
71
  assert self.fkey is not None, '_MGFieldInfo.fkey is None'
70
- return self.fkey
72
+ return '_id' if self.sync_id else self.fkey
73
+
74
+ def _get_value(self, value):
75
+ return ObjectId(value) if self.sync_id else value
71
76
 
72
77
  # 查询动作
73
- def __gt__(self, value): return {self.fkey: {'$gt': value}}
74
- def __ge__(self, value): return {self.fkey: {'$gte': value}}
75
- def __lt__(self, value): return {self.fkey: {'$lt': value}}
76
- def __le__(self, value): return {self.fkey: {'$lte': value}}
77
- def __eq__(self, value): return {self.fkey: value}
78
- def __ne__(self, value): return {self.fkey: {'$ne': value}}
79
- def __mod__(self, value): return {self.fkey: {'$regex': value}}
80
- def in_(self, ls:list): return {self.fkey: {'$in': ls}}
81
- def not_in_(self, ls:list): return {self.fkey: {'$nin': ls}}
78
+ def __gt__(self, value): return {str(self): {'$gt': self._get_value(value)}}
79
+ def __ge__(self, value): return {str(self): {'$gte': self._get_value(value)}}
80
+ def __lt__(self, value): return {str(self): {'$lt': self._get_value(value)}}
81
+ def __le__(self, value): return {str(self): {'$lte': self._get_value(value)}}
82
+ def __eq__(self, value): return {str(self): self._get_value(value)}
83
+ def __ne__(self, value): return {str(self): {'$ne': self._get_value(value)}}
84
+ def __mod__(self, value): return {str(self): {'$regex': self._get_value(value)}}
85
+ def in_(self, ls:list): return {str(self): {'$in': [self._get_value(v) for v in ls]}}
86
+ def not_in_(self, ls:list): return {str(self): {'$nin': [self._get_value(v) for v in ls]}}
82
87
 
83
88
  _Unset: Any = PydanticUndefined
84
89
  @overload
@@ -360,6 +365,7 @@ def MGField(
360
365
  decimal_places=decimal_places,
361
366
  union_mode=union_mode,
362
367
  fail_fast=fail_fast,
368
+ sync_id=extra.pop('sync_id', False)
363
369
  )
364
370
 
365
371
  class MGConfig:
@@ -367,8 +373,6 @@ class MGConfig:
367
373
  db:str
368
374
  col:str
369
375
  default_ids:tuple[str] = ('_id',)
370
- # 用于同步_id字段的内容
371
- sync_ids: tuple[str] = ()
372
376
 
373
377
  class MGFunction:
374
378
  @staticmethod
@@ -473,9 +477,14 @@ class MongoModel(BaseModel):
473
477
  merged[op] = value
474
478
  return merged
475
479
 
480
+ @property
481
+ def mgt_sync_ids(self)->list[str]:
482
+ return [field.fkey for field in self.__class__.model_fields.values() if isinstance(field, _MGFieldInfo) and field.sync_id]
483
+
476
484
  def mgt_set_id(self, id_: ObjectId|None):
477
485
  self.id_ = id_
478
- for field in self.MGConfig.sync_ids: setattr(self, field, self._id)
486
+ if id_:
487
+ for field in self.mgt_sync_ids: setattr(self, field, self._id)
479
488
 
480
489
  def mgt_model_dump(self)->dict:
481
- return self.model_dump(exclude=self.MGConfig.sync_ids or None)
490
+ return self.model_dump(exclude=self.mgt_sync_ids or None)
@@ -49,6 +49,7 @@ class MongoModel(_MongoModel):
49
49
  @classmethod
50
50
  def mg_find_onekey_all(cls, key:str, *querys: dict,
51
51
  sort_map: dict[str, int]=None, skip:int=None, limit:int=None)->MGIt[Any]:
52
+ key = str(key)
52
53
  cursor = cls.MGConfig.getConnect().find(MGFunction.and_(*querys), {'_id':0, key:1})
53
54
  if sort_map: cursor=cursor.sort(sort_map)
54
55
  if skip: cursor=cursor.skip(skip)
@@ -61,11 +62,13 @@ class MongoModel(_MongoModel):
61
62
 
62
63
  @classmethod
63
64
  def mg_find_onecol(cls, key:str, *querys: dict, default=None):
65
+ key = str(key)
64
66
  dt = (cls.MGConfig.getConnect().find_one(MGFunction.and_(*querys), {'_id':0, key:1})) or {}
65
67
  return dt.get(key, default)
66
68
 
67
69
  @classmethod
68
70
  def mg_id_find_onecol(cls, key:str, *ids:str|object|Any, default=None):
71
+ key = str(key)
69
72
  dt = (cls.MGConfig.getConnect().find_one(cls.mgt_get_id_query(ids), {'_id':0, key:1})) or {}
70
73
  return dt.get(key, default)
71
74
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: mlmongo
3
- Version: 2.0.2
3
+ Version: 2.0.4
4
4
  Summary: mongodb数据库操作工具类
5
5
  Home-page: https://www.python.org
6
6
  Author: mengling
File without changes
File without changes
File without changes
File without changes