@rip-lang/db 1.3.0 → 1.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +11 -11
- package/db.rip +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -114,7 +114,7 @@ Rip DB includes an ActiveRecord-style database client for use in Rip
|
|
|
114
114
|
applications. Import it from `@rip-lang/db/client` — it talks to a running
|
|
115
115
|
`rip-db` server over HTTP with parameterized queries.
|
|
116
116
|
|
|
117
|
-
```
|
|
117
|
+
```coffee
|
|
118
118
|
import { connect, query, findOne, findAll, Model } from '@rip-lang/db/client'
|
|
119
119
|
|
|
120
120
|
connect 'http://localhost:4213' # optional — defaults to DB_URL env or localhost:4213
|
|
@@ -125,7 +125,7 @@ connect 'http://localhost:4213' # optional — defaults to DB_URL env or local
|
|
|
125
125
|
Every query uses parameterized placeholders (`$1`, `$2`, ...) to prevent SQL
|
|
126
126
|
injection. Results are automatically materialized into plain objects.
|
|
127
127
|
|
|
128
|
-
```
|
|
128
|
+
```coffee
|
|
129
129
|
# Raw result (meta + data arrays)
|
|
130
130
|
result = query! "SELECT * FROM users WHERE active = $1", [true]
|
|
131
131
|
|
|
@@ -141,13 +141,13 @@ users = findAll! "SELECT * FROM users WHERE role = $1", ['admin']
|
|
|
141
141
|
Create a Model for any table to get a full set of CRUD operations and a
|
|
142
142
|
chainable query builder.
|
|
143
143
|
|
|
144
|
-
```
|
|
144
|
+
```coffee
|
|
145
145
|
User = Model 'users'
|
|
146
146
|
```
|
|
147
147
|
|
|
148
148
|
#### Find & Count
|
|
149
149
|
|
|
150
|
-
```
|
|
150
|
+
```coffee
|
|
151
151
|
user = User.find! 42 # SELECT * FROM "users" WHERE id = $1
|
|
152
152
|
count = User.count! # SELECT COUNT(*) ...
|
|
153
153
|
users = User.all! # SELECT * FROM "users"
|
|
@@ -158,7 +158,7 @@ users = User.all! 10 # SELECT * FROM "users" LIMIT 10
|
|
|
158
158
|
|
|
159
159
|
All query methods return a new builder — chains are immutable and reusable.
|
|
160
160
|
|
|
161
|
-
```
|
|
161
|
+
```coffee
|
|
162
162
|
User.where(active: true).order('name').limit(10).all!
|
|
163
163
|
User.where(active: true).offset(20).limit(10).all!
|
|
164
164
|
User.where('age > $1', [21]).all!
|
|
@@ -167,7 +167,7 @@ User.select('id, name').where(role: 'admin').first!
|
|
|
167
167
|
|
|
168
168
|
#### Where, Or, Not
|
|
169
169
|
|
|
170
|
-
```
|
|
170
|
+
```coffee
|
|
171
171
|
# Object-style (null-aware — generates IS NULL / IS NOT NULL)
|
|
172
172
|
User.where(role: 'admin').all!
|
|
173
173
|
User.where(deleted_at: null).all! # WHERE "deleted_at" IS NULL
|
|
@@ -186,7 +186,7 @@ User.not(deleted_at: null).all! # WHERE "deleted_at" IS NOT NULL
|
|
|
186
186
|
|
|
187
187
|
#### Group & Having
|
|
188
188
|
|
|
189
|
-
```
|
|
189
|
+
```coffee
|
|
190
190
|
User.group('role').select('role, count(*) as n').all!
|
|
191
191
|
User.group('role').having('count(*) > $1', [5]).select('role, count(*) as n').all!
|
|
192
192
|
```
|
|
@@ -195,7 +195,7 @@ User.group('role').having('count(*) > $1', [5]).select('role, count(*) as n').al
|
|
|
195
195
|
|
|
196
196
|
All mutations return the affected row(s) via `RETURNING *`.
|
|
197
197
|
|
|
198
|
-
```
|
|
198
|
+
```coffee
|
|
199
199
|
# Insert — returns the new record
|
|
200
200
|
user = User.insert! { first_name: 'Alice', email: 'alice@example.com' }
|
|
201
201
|
|
|
@@ -213,7 +213,7 @@ user = User.destroy! 42
|
|
|
213
213
|
|
|
214
214
|
Chain `.where()` with `.update!` or `.destroy!` for bulk operations.
|
|
215
215
|
|
|
216
|
-
```
|
|
216
|
+
```coffee
|
|
217
217
|
# Update all matching rows
|
|
218
218
|
User.where(role: 'guest').update! { role: 'member' }
|
|
219
219
|
|
|
@@ -225,7 +225,7 @@ User.where(active: false).destroy!
|
|
|
225
225
|
|
|
226
226
|
For anything the builder doesn't cover, drop down to raw SQL.
|
|
227
227
|
|
|
228
|
-
```
|
|
228
|
+
```coffee
|
|
229
229
|
users = User.query! "SELECT * FROM users WHERE dob > $1", ['2000-01-01']
|
|
230
230
|
```
|
|
231
231
|
|
|
@@ -233,7 +233,7 @@ users = User.query! "SELECT * FROM users WHERE dob > $1", ['2000-01-01']
|
|
|
233
233
|
|
|
234
234
|
Pass a database name to query attached DuckDB databases.
|
|
235
235
|
|
|
236
|
-
```
|
|
236
|
+
```coffee
|
|
237
237
|
Archive = Model 'orders', 'archive_db'
|
|
238
238
|
order = Archive.find! 99 # SELECT * FROM "archive_db"."orders" WHERE id = $1
|
|
239
239
|
```
|
package/db.rip
CHANGED
|
@@ -183,7 +183,7 @@ get '/tables' ->
|
|
|
183
183
|
|
|
184
184
|
# GET /schema/:table — Get table schema
|
|
185
185
|
get '/schema/:table' ->
|
|
186
|
-
table =
|
|
186
|
+
table = read('table').replace(/[^a-zA-Z0-9_]/g, '')
|
|
187
187
|
try
|
|
188
188
|
rows = await conn.query "DESCRIBE \"#{table}\""
|
|
189
189
|
{ schema: rows }
|