Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 858e7c4

Browse files
Add bulk insert functionality to NocoDB client
1 parent ccf91e9 commit 858e7c4

File tree

5 files changed

+108
-0
lines changed

5 files changed

+108
-0
lines changed

‎README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,13 @@ row_info = {
143143
}
144144
client.table_row_update(project, table_name, row_id, row_info)
145145

146+
# Bulk insert multiple rows
147+
rows_to_insert = [
148+
{"name": "Alice", "age": 30},
149+
{"name": "Bob", "age": 28},
150+
]
151+
client.table_row_bulk_insert(project, table_name, rows_to_insert)
152+
146153
# Delete a row (only if you've already bought me a beer)
147154
client.table_row_delete(project, table_name, row_id)
148155
```

‎nocodb/api.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,17 @@ def get_table_find_one_uri(self, project: NocoDBProject, table: str) -> str:
3838
)
3939
)
4040

41+
def get_table_bulk_uri(self, project: NocoDBProject, table: str) -> str:
42+
"""Return the URI for bulk insert/update operations on a table."""
43+
return urljoin(self.__base_data_uri, "/".join(
44+
(
45+
"bulk",
46+
project.org_name,
47+
project.project_name,
48+
table,
49+
)
50+
))
51+
4152
def get_row_detail_uri(
4253
self, project: NocoDBProject, table: str, row_id: int
4354
):

‎nocodb/infra/requests_client.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,28 @@ def table_row_create(self, project: NocoDBProject, table: str, body: dict) -> di
5757
"POST", self.__api_info.get_table_uri(project, table), json=body
5858
).json()
5959

60+
def table_row_bulk_insert(
61+
self,
62+
project: NocoDBProject,
63+
table: str,
64+
body: List[dict],
65+
) -> dict:
66+
"""Bulk insert multiple rows into a table.
67+
68+
Args:
69+
project: The NocoDB project information.
70+
table: Target table name.
71+
body: List of row dictionaries to insert.
72+
73+
Returns:
74+
API JSON response from NocoDB.
75+
"""
76+
return self._request(
77+
"POST",
78+
self.__api_info.get_table_bulk_uri(project, table),
79+
json=body,
80+
).json()
81+
6082
def table_row_detail(self, project: NocoDBProject, table: str, row_id: int) -> dict:
6183
return self._request(
6284
"GET",

‎nocodb/nocodb.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,24 @@ def table_row_create(
8484
) -> dict:
8585
pass
8686

87+
@abstractmethod
88+
def table_row_bulk_insert(
89+
self,
90+
project: NocoDBProject,
91+
table: str,
92+
body: list[dict],
93+
) -> dict:
94+
"""Bulk insert multiple rows into a table.
95+
96+
Args:
97+
project: The NocoDB project information.
98+
table: Target table name.
99+
body: List of row dictionaries to insert.
100+
Returns:
101+
API JSON response.
102+
"""
103+
pass
104+
87105
@abstractmethod
88106
def table_row_detail(
89107
self, project: NocoDBProject, table: str, row_id: int

‎tests/test_bulk_insert.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import requests
2+
import pytest
3+
4+
from nocodb.infra.requests_client import NocoDBRequestsClient
5+
from nocodb.nocodb import NocoDBProject, APIToken
6+
7+
8+
class DummyResponse:
9+
"""Minimal Response object to mimic requests.Response."""
10+
11+
def __init__(self, json_data=None, status_code: int = 200):
12+
self._json_data = json_data or {}
13+
self.status_code = status_code
14+
15+
# --- requests.Response methods we rely on ---
16+
def json(self):
17+
return self._json_data
18+
19+
def raise_for_status(self):
20+
if self.status_code >= 400:
21+
raise requests.HTTPError(response=self)
22+
23+
24+
def test_bulk_insert(monkeypatch):
25+
"""Ensure the client hits the bulk endpoint with correct payload."""
26+
27+
captured = {}
28+
29+
def fake_request(self, method, url, *args, **kwargs): # noqa: D401
30+
captured["method"] = method
31+
captured["url"] = url
32+
captured["json"] = kwargs.get("json")
33+
return DummyResponse({"success": True})
34+
35+
# Patch Session.request before the client is instantiated.
36+
monkeypatch.setattr(requests.sessions.Session, "request", fake_request, raising=True)
37+
38+
client = NocoDBRequestsClient(APIToken("dummy"), "http://localhost:8080")
39+
project = NocoDBProject("noco", "myproject")
40+
41+
rows = [{"name": "Alice", "age": 30}, {"name": "Bob", "age": 28}]
42+
response = client.table_row_bulk_insert(project, "Employees", rows)
43+
44+
assert response == {"success": True}
45+
assert captured["method"] == "POST"
46+
assert (
47+
captured["url"]
48+
== "http://localhost:8080/api/v1/db/data/bulk/noco/myproject/Employees"
49+
)
50+
assert captured["json"] == rows

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /