To insert data, you can call insert function like this:
from abstra.tables import insert
inserted = insert("users", {
"name": "Michael Scott",
"email": "michael.scott@dundermifflin.com"
})
inserted # {"id": 123, "name": "Michael Scott", "email": "michael.scott@dundermifflin.com"}
Which is equivalent to
INSERT INTO users (name, email)
VALUES ('Michael Scott', 'michael.scott@dundermifflin.com')
RETURNING *
Using more types
Python's date, datetime, dict, tuple, set and list objects are serialized by default:
from abstra.tables import insert
import datetime
inserted = insert("episodes", {
"name": "Goodbye, Michael",
"aired_at": datetime.date(2011, 4, 28)
"trivia": {
"imdb_rating": 9.8,
}
})
inserted # {"name": "Goodbye, Michael", "aired_at": "2011-04-28", "trivia": {"imdb_rating": 9.8}}
Using dataclass
from dataclasses import dataclass
from abstra.tables import insert
@dataclass
class User:
name: str
email: str
user = User(
name="Michael Scott",
email="michael.scott@dundermifflin.com"
)
insert("users", user)
Multiple rows
The insert function also allow multiple rows at once sending a list of dictionaries.
from abstra.tables import insert
inserted = insert("users", [{
"name": "Michael Scott",
"email": "michael.scott@dundermifflin.com"
},{
"name": "Jim Halpert",
"email": "jim.halpert@dundermifflin.com"
}])
inserted
# [
# {"id": 123, "name": "Michael Scott", "email": "michael.scott@dundermifflin.com"},
# {"id": 124, "name": "Jim Halpert", "email": "jim.halpert@dundermifflin.com"}
# ]
Large inserts
Each call to insert sends one SQL statement. PostgreSQL accepts at most 65,535 parameters per query. Each supplied column value counts as one parameter, including None and serialized lists or dictionaries. Missing keys use DEFAULT and do not count as parameters.
For example, with 21 supplied columns per row, a single call can contain at most 3,120 rows (65535 // 21). The SDK raises ValueError before sending an oversized query, with the actual parameter count and the limit.
Split larger inputs explicitly into batches sized for the number of supplied columns:
from abstra.tables import insert
batch_size = 500 # For rows with 21 supplied columns: 10,500 parameters per call.
for start in range(0, len(rows), batch_size):
insert("payments", rows[start:start + batch_size])
Separate calls are not one atomic operation: earlier batches can remain saved if a later batch fails. Validate the input before writing, track batch completion, and make retries idempotent (for example, using a unique source-record identifier). If all rows must commit or roll back together, use a supported database transaction mechanism rather than assuming that separate insert calls share a transaction. A failed insert also does not undo a batch/header record created in an earlier call.
None vs unset
When using insert, it will create columns based on the keys in the dictionaries you pass as parameters. If a key is missing in any dictionary, it will use the default value for that column. If a value is None, it will be mapped as SQL NULL.
For example, if the default position in the employees table is 'Temp', this behavior will apply accordingly.
from abstra.tables import insert
inserted = insert("employees", [{
"name": "Pam Beesly",
"position": "Receptionist",
"email": "pam.beesly@dundermifflin.com"
}, {
"name": "Jan Levinson",
"position": None,
"email": "jan.levinson@dundermifflin.com"
}, {
"name": "Ryan Howard",
"email": "ryan.howard@dundermifflin.com"
}])
inserted
# [
# {"id": 126, "name": "Pam Beesly", "position": "Receptionist", "email": "pam.beesly@dundermifflin.com"},
# {"id": 127, "name": "Jan Levinson", "position": None, "email": "jan.levinson@dundermifflin.com"},
# {"id": 129, "name": "Ryan Howard", "position": "Temp", "email": "ryan.howard@dundermifflin.com"}
# ]