Example DBBasic apps (an API, a blog and a microblog) and a template for new DBBasic modules.

0 downloads

Download zip
Clone
Clone with git
git clone https://gitstable.com/git/askrobots/dbbasic-examples.git
HTTPS
https://gitstable.com/git/askrobots/dbbasic-examples.git
SSH
ssh://git@gitstable.com:2222/askrobots/dbbasic-examples.git

Cloning needs no sign-in. Pushing over HTTPS asks for your username and an access token. SSH needs your key under SSH keys.

danq9 Add official module template for creating DBBasic packages 17cf88a 1 year, 1 month ago History
module.py · 4.3 KB · 192 lines
1"""Core API for dbbasic-module"""
2
3from pathlib import Path
4from dbbasic_tsv import TSV
5
6
7class Module:
8 """
9 Main API class for managing THINGS.
10
11 Replace "THING" with your entity name (e.g., Contact, Task, Product)
12
13 Example usage:
14 module = Module()
15
16 # Create
17 item = module.create(name="Example", description="Test")
18
19 # List all
20 items = module.list()
21
22 # Get by ID
23 item = module.get(1)
24
25 # Update
26 module.update(1, name="Updated")
27
28 # Delete
29 module.delete(1)
30
31 # Search
32 results = module.search(name="Example")
33 """
34
35 def __init__(self, data_dir: str = "data"):
36 """
37 Initialize Module API.
38
39 Args:
40 data_dir: Directory for data storage (default: "data" in current directory)
41 """
42 self.data_dir = Path(data_dir)
43 self.data_dir.mkdir(parents=True, exist_ok=True)
44
45 # Initialize TSV table
46 # Change "things" to your table name (plural, lowercase)
47 self.table = TSV(self.data_dir / "things.tsv")
48
49 # Define schema
50 # Customize these fields for your entity
51 self.schema = {
52 'id': int,
53 'name': str,
54 'description': str,
55 'created_at': str,
56 }
57
58 # Create indexes for fast lookups
59 # Add fields you'll frequently search by
60 self.table.create_index('name')
61
62 def create(self, **kwargs):
63 """
64 Create a new THING.
65
66 Args:
67 **kwargs: Field values (name, description, etc.)
68
69 Returns:
70 dict: Created THING with ID
71
72 Example:
73 item = module.create(name="Test", description="Example")
74 """
75 # Add timestamp
76 from datetime import datetime
77 kwargs['created_at'] = datetime.now().isoformat()
78
79 # Add validation here if needed
80 if 'name' not in kwargs or not kwargs['name']:
81 raise ValueError("Name is required")
82
83 # Insert and return
84 item_id = self.table.insert(kwargs)
85 return self.get(item_id)
86
87 def list(self, limit=None, offset=0):
88 """
89 List all THINGS.
90
91 Args:
92 limit: Maximum number of results (None = all)
93 offset: Number of results to skip
94
95 Returns:
96 list: List of THINGS
97
98 Example:
99 items = module.list(limit=10)
100 """
101 rows = self.table.select()
102
103 if limit:
104 rows = rows[offset:offset + limit]
105
106 return rows
107
108 def get(self, item_id):
109 """
110 Get THING by ID.
111
112 Args:
113 item_id: ID of the THING
114
115 Returns:
116 dict: THING data or None if not found
117
118 Example:
119 item = module.get(1)
120 """
121 results = self.table.select(where={'id': item_id})
122 return results[0] if results else None
123
124 def update(self, item_id, **kwargs):
125 """
126 Update THING.
127
128 Args:
129 item_id: ID of the THING to update
130 **kwargs: Fields to update
131
132 Returns:
133 bool: True if updated, False if not found
134
135 Example:
136 module.update(1, name="New Name")
137 """
138 # Check if exists
139 if not self.get(item_id):
140 return False
141
142 # Update
143 self.table.update(where={'id': item_id}, values=kwargs)
144 return True
145
146 def delete(self, item_id):
147 """
148 Delete THING.
149
150 Args:
151 item_id: ID of the THING to delete
152
153 Returns:
154 bool: True if deleted, False if not found
155
156 Example:
157 module.delete(1)
158 """
159 # Check if exists
160 if not self.get(item_id):
161 return False
162
163 # Delete
164 self.table.delete(where={'id': item_id})
165 return True
166
167 def search(self, **filters):
168 """
169 Search THINGS by filters.
170
171 Args:
172 **filters: Field=value pairs to filter by
173
174 Returns:
175 list: Matching THINGS
176
177 Example:
178 results = module.search(name="Test")
179 """
180 return self.table.select(where=filters)
181
182 def count(self):
183 """
184 Count total THINGS.
185
186 Returns:
187 int: Total count
188
189 Example:
190 total = module.count()
191 """
192 return len(self.table.select())