@@ -32,19 +32,53 @@ def get_basic_metadata(self) -> dict:
3232 return {k : v for k , v in m .items () if not k in extra_keys }
3333
3434 def get_number_of_records (self ) -> int :
35- r = self .noco_db .call_noco (path = f"tables/{ self .table_id } /records/count" )
35+ r = self .noco_db .call_noco (
36+ path = f"tables/{ self .table_id } /records/count" )
3637 return r .json ()["count" ]
3738
39+ def get_base (self ) -> Base :
40+ return self .noco_db .get_base (self .base_id )
41+
42+ def duplicate (self , exclude_data : bool = True , exclude_views : bool = True ) -> None :
43+ r = self .noco_db .call_noco (
44+ path = f"meta/duplicate/{ self .base_id } /table/{ self .table_id } " ,
45+ method = "POST" ,
46+ json = {"excludeData" : exclude_data , "excludeViews" : exclude_views },
47+ )
48+ _logger .info (f"Table { self .title } duplicated" )
49+ return
50+
51+ # Bug in noco API, wrong Id response
52+
53+ def get_duplicates (self ) -> list ["Table" ]:
54+ duplicates = {}
55+ for t in self .get_base ().get_tables ():
56+ if re .match (f"^{ self .title } copy(_\\ d+)?$" , t .title ):
57+ nr = re .findall ("_(\\ d+)" , t .title )
58+ if nr :
59+ duplicates [int (nr [0 ])] = t
60+ else :
61+ duplicates [0 ] = t
62+
63+ return list (dict (sorted (duplicates .items (), reverse = True )).values ())
64+
65+ def delete (self ) -> bool :
66+ r = self .noco_db .call_noco (
67+ path = f"meta/tables/{ self .table_id } " , method = "DELETE" )
68+ _logger .info (f"Table { self .title } deleted" )
69+ return r .json ()
70+
3871 def get_columns (self , include_system : bool = False ) -> list [Column ]:
3972 r = self .noco_db .call_noco (path = f"meta/tables/{ self .table_id } " )
4073 cols = [Column (noco_db = self .noco_db , ** f ) for f in r .json ()["columns" ]]
4174 if include_system :
4275 return cols
4376 else :
44- return [c for c in cols if not c .system and not c . primary_key ]
77+ return [c for c in cols if not c .system ]
4578
4679 def get_columns_hash (self ) -> str :
47- r = self .noco_db .call_noco (path = f"meta/tables/{ self .table_id } /columns/hash" )
80+ r = self .noco_db .call_noco (
81+ path = f"meta/tables/{ self .table_id } /columns/hash" )
4882 return r .json ()["hash" ]
4983
5084 def get_column_by_title (self , title : str ) -> Column :
@@ -69,34 +103,6 @@ def create_column(
69103 )
70104 return self .get_column_by_title (title = title )
71105
72- def duplicate (self , exclude_data : bool = True , exclude_views : bool = True ) -> None :
73- r = self .noco_db .call_noco (
74- path = f"meta/duplicate/{ self .base_id } /table/{ self .table_id } " ,
75- method = "POST" ,
76- json = {"excludeData" : exclude_data , "excludeViews" : exclude_views },
77- )
78- _logger .info (f"Table { self .title } duplicated" )
79- return
80-
81- # Bug in noco API, wrong Id response
82-
83- def get_duplicates (self ) -> list ["Table" ]:
84- duplicates = {}
85- for t in self .get_base ().get_tables ():
86- if re .match (f"^{ self .title } copy(_\\ d+)?$" , t .title ):
87- nr = re .findall ("_(\\ d+)" , t .title )
88- if nr :
89- duplicates [int (nr [0 ])] = t
90- else :
91- duplicates [0 ] = t
92-
93- return list (dict (sorted (duplicates .items (), reverse = True )).values ())
94-
95- def delete (self ) -> bool :
96- r = self .noco_db .call_noco (path = f"meta/tables/{ self .table_id } " , method = "DELETE" )
97- _logger .info (f"Table { self .title } deleted" )
98- return r .json ()
99-
100106 def get_records (self , params : dict | None = None ) -> list [Record ]:
101107 params = params or {}
102108
@@ -128,9 +134,14 @@ def get_records(self, params: dict | None = None) -> list[Record]:
128134 return records
129135
130136 def get_record (self , record_id : int ) -> Record :
131- r = self .noco_db .call_noco (path = f"tables/{ self .table_id } /records/{ record_id } " )
137+ r = self .noco_db .call_noco (
138+ path = f"tables/{ self .table_id } /records/{ record_id } " )
132139 return Record (self , ** r .json ())
133140
141+ def get_records_by_id (self , record_ids : list [int ]) -> list [Record ]:
142+ ids_string = "," .join (map (str , record_ids ))
143+ return self .get_records (params = {"where" : f"(Id,in,{ ids_string } )" })
144+
134145 def get_records_by_field_value (self , field : str , value ) -> list [Record ]:
135146 return self .get_records (params = {"where" : f"({ field } ,eq,{ value } )" })
136147
@@ -144,26 +155,32 @@ def create_records(self, records: list[dict]) -> list[Record]:
144155 r = self .noco_db .call_noco (
145156 path = f"tables/{ self .table_id } /records" , method = "POST" , json = records
146157 )
147- ids_string = "," .join ([str (d ["Id" ]) for d in r .json ()])
148- return self .get_records (params = {"where" : f"(Id,in,{ ids_string } )" })
149158
150- def get_base (self ) -> Base :
151- return self .noco_db .get_base (self .base_id )
159+ return self .get_records_by_id ([r_id ["Id" ] for r_id in r .json ()])
152160
153- def delete_record (self , record_id : int ) -> bool :
161+ def delete_record (self , record_id : int ) -> int :
154162 r = self .noco_db .call_noco (
155163 path = f"tables/{ self .table_id } /records" ,
156164 method = "DELETE" ,
157165 json = {"Id" : record_id },
158166 )
167+ return r .json ()["Id" ]
159168
160- return r .json ()
169+ def delete_records_by_id (self , record_ids : list [int ]) -> list [int ]:
170+ r = self .noco_db .call_noco (
171+ path = f"tables/{ self .table_id } /records" ,
172+ method = "DELETE" ,
173+ json = [{"Id" : r_id } for r_id in record_ids ]
174+ )
175+ return [r_id ["Id" ] for r_id in r .json ()]
176+
177+ def delete_records (self , records : list [Record ]) -> list [int ]:
178+ return self .delete_records_by_id ([rec .record_id for rec in records ])
161179
162- def update_record (self , ** kwargs ) -> None :
180+ def update_records (self , records : list [ dict ] ) -> list [ Record ] :
163181 r = self .noco_db .call_noco (
164182 path = f"tables/{ self .table_id } /records" ,
165183 method = "PATCH" ,
166- json = kwargs ,
184+ json = records ,
167185 )
168-
169- return r .json ()
186+ return self .get_records_by_id ([r_id ["Id" ] for r_id in r .json ()])
0 commit comments