projects¶
projects
¶
CRUD wrapper for the projects table.
All functions take a cursor as the first argument so callers control the transaction boundary:
from noxdb import projects, transaction
with transaction() as cur:
pid, created = projects.get_or_create(cur, "STUDY42", pi_name="Dr. Test")
Single rows come back as dict[str, Any] (or None when missing);
collections come back as list[dict[str, Any]]. Writes go through
_LoggingCursor so they are audit-logged automatically.
create
¶
create(cur, project_name: str, *, description: str | None = None, pi_name: str | None = None) -> int
Insert a project and return its new project_id.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cur
|
Audit-logging cursor from |
required | |
project_name
|
str
|
Unique name for the new project (column is UNIQUE). |
required |
description
|
str | None
|
Optional free-text description. |
None
|
pi_name
|
str | None
|
Optional principal investigator name. |
None
|
Returns:
| Type | Description |
|---|---|
int
|
The newly inserted |
Raises:
| Type | Description |
|---|---|
IntegrityError
|
If |
Source code in src/noxdb/projects.py
get
¶
Return the project row for a given id.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cur
|
Audit-logging cursor from |
required | |
project_id
|
int
|
Primary key to look up. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Any] | None
|
The row as |
Source code in src/noxdb/projects.py
get_by_name
¶
Return the project row for a given name.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cur
|
Audit-logging cursor from |
required | |
project_name
|
str
|
Name to look up (column is UNIQUE). |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Any] | None
|
The row as |
Source code in src/noxdb/projects.py
get_or_create
¶
get_or_create(cur, project_name: str, *, description: str | None = None, pi_name: str | None = None) -> tuple[int, bool]
Idempotently return the project id, inserting if needed.
Tries to fetch first; on miss inserts. If a parallel transaction wins a UNIQUE-violation race, falls back to a second fetch. Existing rows are returned as-is — description and pi_name are not used to update an existing row.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cur
|
Audit-logging cursor from |
required | |
project_name
|
str
|
Unique project name. |
required |
description
|
str | None
|
Used only on insert. |
None
|
pi_name
|
str | None
|
Used only on insert. |
None
|
Returns:
| Type | Description |
|---|---|
int
|
|
bool
|
call inserted the row. |
Raises:
| Type | Description |
|---|---|
IntegrityError
|
If the race-recovery fetch also misses (i.e. the IntegrityError was not due to a duplicate name). |
Source code in src/noxdb/projects.py
list_all
¶
Return all projects.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cur
|
Audit-logging cursor from |
required | |
order_by
|
str
|
Column name to order by. Must be one of the columns of
the |
'project_id'
|
Returns:
| Type | Description |
|---|---|
list[dict[str, Any]]
|
All rows as |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in src/noxdb/projects.py
update
¶
update(cur, project_id: int, *, project_name: str | None = None, description: str | None = None, pi_name: str | None = None) -> bool
Partial update of a project row.
Only kwargs with non-None values are written. Setting a column to SQL NULL is not supported by this helper.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cur
|
Audit-logging cursor from |
required | |
project_id
|
int
|
Row to update. |
required |
project_name
|
str | None
|
New name (if not None). |
None
|
description
|
str | None
|
New description (if not None). |
None
|
pi_name
|
str | None
|
New PI name (if not None). |
None
|
Returns:
| Type | Description |
|---|---|
bool
|
|
bool
|
is run) when every kwarg is None. |
Source code in src/noxdb/projects.py
delete
¶
Delete a project.
subjects.project_id declares ON DELETE CASCADE, so this also
removes every subject, visit, sample, and metadata row owned by the
project. sample_files uses ON DELETE RESTRICT and will block
the delete instead — clean those up first.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cur
|
Audit-logging cursor from |
required | |
project_id
|
int
|
Row to delete. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
|
Source code in src/noxdb/projects.py
exists
¶
Return whether a project with the given id or name exists.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cur
|
Audit-logging cursor from |
required | |
project_id
|
int | None
|
Id to check (exclusive with |
None
|
name
|
str | None
|
Name to check (exclusive with |
None
|
Returns:
| Type | Description |
|---|---|
bool
|
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If both or neither of |
Source code in src/noxdb/projects.py
count
¶
Return the total number of projects.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cur
|
Audit-logging cursor from |
required |
Returns:
| Type | Description |
|---|---|
int
|
Row count of the |