Spaces:
Sleeping
Sleeping
Commit ·
5533459
1
Parent(s): 7db7c77
feat: add cascade delete for related resources
Browse files- Delete child resources when a parent resource is deleted
- Document cascade behavior in the README
- README.md +2 -0
- src/db/index.js +14 -0
- tests/README.md +3 -3
- tests/db/database.test.js +32 -0
README.md
CHANGED
|
@@ -93,6 +93,8 @@ Environment files are loaded by `src/config/load-env.js` (auto-run via `src/conf
|
|
| 93 |
| `PATCH` | `/api/:table/:id` | Partial update |
|
| 94 |
| `DELETE` | `/api/:table/:id` | Delete resource |
|
| 95 |
|
|
|
|
|
|
|
| 96 |
### Query String Filtering & Pagination
|
| 97 |
|
| 98 |
```bash
|
|
|
|
| 93 |
| `PATCH` | `/api/:table/:id` | Partial update |
|
| 94 |
| `DELETE` | `/api/:table/:id` | Delete resource |
|
| 95 |
|
| 96 |
+
> **Cascade deletes**: Deleting a `user` removes their `posts`, `albums`, and `todos`. Deleting a `post` removes its `comments`. Deleting an `album` removes its `photos`.
|
| 97 |
+
|
| 98 |
### Query String Filtering & Pagination
|
| 99 |
|
| 100 |
```bash
|
src/db/index.js
CHANGED
|
@@ -168,10 +168,24 @@ function updateOne(table, id, data, replace = false) {
|
|
| 168 |
return getOne(table, id);
|
| 169 |
}
|
| 170 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 171 |
function deleteOne(table, id) {
|
| 172 |
const d = getWrappedDb();
|
| 173 |
const existing = getOne(table, id);
|
| 174 |
if (!existing) return null;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 175 |
d.prepare(`DELETE FROM ${table} WHERE id = ?`).run(id);
|
| 176 |
return existing;
|
| 177 |
}
|
|
|
|
| 168 |
return getOne(table, id);
|
| 169 |
}
|
| 170 |
|
| 171 |
+
const CASCADE_MAP = {
|
| 172 |
+
users: { key: 'userId', children: ['posts', 'albums', 'todos'] },
|
| 173 |
+
posts: { key: 'postId', children: ['comments'] },
|
| 174 |
+
albums: { key: 'albumId', children: ['photos'] },
|
| 175 |
+
};
|
| 176 |
+
|
| 177 |
function deleteOne(table, id) {
|
| 178 |
const d = getWrappedDb();
|
| 179 |
const existing = getOne(table, id);
|
| 180 |
if (!existing) return null;
|
| 181 |
+
|
| 182 |
+
const cascade = CASCADE_MAP[table];
|
| 183 |
+
if (cascade) {
|
| 184 |
+
for (const child of cascade.children) {
|
| 185 |
+
d.prepare(`DELETE FROM ${child} WHERE ${cascade.key} = ?`).run(id);
|
| 186 |
+
}
|
| 187 |
+
}
|
| 188 |
+
|
| 189 |
d.prepare(`DELETE FROM ${table} WHERE id = ?`).run(id);
|
| 190 |
return existing;
|
| 191 |
}
|
tests/README.md
CHANGED
|
@@ -21,14 +21,14 @@ tests/
|
|
| 21 |
config.test.js # Config defaults and env var branches (9 tests)
|
| 22 |
load-env.test.js # Chain-loading, override:false, production skip, fallback (7 tests)
|
| 23 |
db/
|
| 24 |
-
database.test.js # Database CRUD, pagination, search, sort, SQL injection (
|
| 25 |
migrate.test.js # Migration success and failure paths (2 tests)
|
| 26 |
seed.test.js # Seed with real DB + mocked deps, JSONPlaceholder fetch (5 tests)
|
| 27 |
sql-logger.test.js # SQL query logger wrapping tests (5 tests)
|
| 28 |
middleware/
|
| 29 |
rate-limiter.test.js # In-memory, Redis, circuit breaker, proxy IPs (54 tests)
|
| 30 |
redis/
|
| 31 |
-
redis.test.js # RESP protocol encoding/parsing, constructor options (27 tests)
|
| 32 |
server/
|
| 33 |
coverage-printlog.test.js # Printlog and server export V8 coverage (4 tests)
|
| 34 |
graceful-shutdown.test.js # SIGINT/SIGTERM handler coverage (1 test)
|
|
@@ -42,7 +42,7 @@ tests/
|
|
| 42 |
seed-settings-coverage.test.js # Seed-settings.js V8 coverage (4 tests)
|
| 43 |
```
|
| 44 |
|
| 45 |
-
**Total:
|
| 46 |
|
| 47 |
## Test design
|
| 48 |
|
|
|
|
| 21 |
config.test.js # Config defaults and env var branches (9 tests)
|
| 22 |
load-env.test.js # Chain-loading, override:false, production skip, fallback (7 tests)
|
| 23 |
db/
|
| 24 |
+
database.test.js # Database CRUD, pagination, search, sort, SQL injection, cascade delete (11 tests)
|
| 25 |
migrate.test.js # Migration success and failure paths (2 tests)
|
| 26 |
seed.test.js # Seed with real DB + mocked deps, JSONPlaceholder fetch (5 tests)
|
| 27 |
sql-logger.test.js # SQL query logger wrapping tests (5 tests)
|
| 28 |
middleware/
|
| 29 |
rate-limiter.test.js # In-memory, Redis, circuit breaker, proxy IPs (54 tests)
|
| 30 |
redis/
|
| 31 |
+
redis.test.js # RESP protocol encoding/parsing, constructor options, eval method (27 tests)
|
| 32 |
server/
|
| 33 |
coverage-printlog.test.js # Printlog and server export V8 coverage (4 tests)
|
| 34 |
graceful-shutdown.test.js # SIGINT/SIGTERM handler coverage (1 test)
|
|
|
|
| 42 |
seed-settings-coverage.test.js # Seed-settings.js V8 coverage (4 tests)
|
| 43 |
```
|
| 44 |
|
| 45 |
+
**Total: 219 tests across 13 test files.**
|
| 46 |
|
| 47 |
## Test design
|
| 48 |
|
tests/db/database.test.js
CHANGED
|
@@ -332,4 +332,36 @@ describe('database.js', () => {
|
|
| 332 |
|
| 333 |
restore(s)
|
| 334 |
})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 335 |
})
|
|
|
|
| 332 |
|
| 333 |
restore(s)
|
| 334 |
})
|
| 335 |
+
|
| 336 |
+
it('covers cascade delete — deleting a user removes related todos', () => {
|
| 337 |
+
const dbPath = path.join(tmpDir, 'db-cascade.db')
|
| 338 |
+
const s = save('DB_PATH', 'DEBUG_SQL')
|
| 339 |
+
setEnv({ DB_PATH: dbPath, DEBUG_SQL: 'false' })
|
| 340 |
+
clearCjs('../../src/db/index.js', '../../src/config/index.js')
|
| 341 |
+
const mod = _require('../../src/db/index.js')
|
| 342 |
+
const db = mod.getDb()
|
| 343 |
+
db.exec(`
|
| 344 |
+
CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT);
|
| 345 |
+
CREATE TABLE todos (id INTEGER PRIMARY KEY, userId INTEGER, title TEXT);
|
| 346 |
+
CREATE TABLE posts (id INTEGER PRIMARY KEY, userId INTEGER, title TEXT);
|
| 347 |
+
CREATE TABLE albums (id INTEGER PRIMARY KEY, userId INTEGER, title TEXT);
|
| 348 |
+
`)
|
| 349 |
+
db.prepare('INSERT INTO users (id, name) VALUES (1, \'Alice\')').run()
|
| 350 |
+
db.prepare('INSERT INTO todos (id, userId, title) VALUES (?, ?, ?)').run(1, 1, 'Todo 1')
|
| 351 |
+
db.prepare('INSERT INTO todos (id, userId, title) VALUES (?, ?, ?)').run(2, 1, 'Todo 2')
|
| 352 |
+
db.prepare('INSERT INTO posts (id, userId, title) VALUES (?, ?, ?)').run(1, 1, 'Post 1')
|
| 353 |
+
|
| 354 |
+
expect(mod.listAll('todos')).toHaveLength(2)
|
| 355 |
+
expect(mod.listAll('posts')).toHaveLength(1)
|
| 356 |
+
|
| 357 |
+
const deleted = mod.deleteOne('users', 1)
|
| 358 |
+
expect(deleted.id).toBe(1)
|
| 359 |
+
expect(deleted.name).toBe('Alice')
|
| 360 |
+
|
| 361 |
+
expect(mod.listAll('todos')).toHaveLength(0)
|
| 362 |
+
expect(mod.listAll('posts')).toHaveLength(0)
|
| 363 |
+
expect(mod.getOne('users', 1)).toBeNull()
|
| 364 |
+
|
| 365 |
+
restore(s)
|
| 366 |
+
})
|
| 367 |
})
|