@tailor-platform/erp-kit 0.1.0 → 0.1.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/CHANGELOG.md +7 -0
- package/LICENSE +21 -0
- package/dist/cli.js +64 -44
- package/package.json +4 -2
- package/skills/1-module-docs/SKILL.md +4 -0
- package/{rules/module-development → skills/1-module-docs/references}/structure.md +2 -7
- package/skills/2-module-feature-breakdown/SKILL.md +6 -0
- package/{rules/module-development → skills/2-module-feature-breakdown/references}/commands.md +0 -6
- package/{rules/module-development → skills/2-module-feature-breakdown/references}/models.md +0 -5
- package/skills/2-module-feature-breakdown/references/structure.md +22 -0
- package/skills/3-module-doc-review/SKILL.md +6 -0
- package/skills/3-module-doc-review/references/commands.md +54 -0
- package/skills/3-module-doc-review/references/models.md +29 -0
- package/{rules/module-development → skills/3-module-doc-review/references}/testing.md +0 -6
- package/skills/4-module-tdd-implementation/SKILL.md +24 -6
- package/skills/4-module-tdd-implementation/references/commands.md +45 -0
- package/{rules/sdk-best-practices → skills/4-module-tdd-implementation/references}/db-relations.md +0 -5
- package/{rules/module-development → skills/4-module-tdd-implementation/references}/errors.md +0 -5
- package/{rules/module-development → skills/4-module-tdd-implementation/references}/exports.md +0 -5
- package/skills/4-module-tdd-implementation/references/models.md +30 -0
- package/skills/4-module-tdd-implementation/references/structure.md +22 -0
- package/skills/4-module-tdd-implementation/references/testing.md +37 -0
- package/skills/5-module-implementation-review/SKILL.md +8 -0
- package/skills/5-module-implementation-review/references/commands.md +45 -0
- package/skills/5-module-implementation-review/references/errors.md +7 -0
- package/skills/5-module-implementation-review/references/exports.md +8 -0
- package/skills/5-module-implementation-review/references/models.md +30 -0
- package/skills/5-module-implementation-review/references/testing.md +29 -0
- package/skills/app-compose-1-requirement-analysis/SKILL.md +4 -0
- package/{rules/app-compose → skills/app-compose-1-requirement-analysis/references}/structure.md +0 -5
- package/skills/app-compose-2-requirements-breakdown/SKILL.md +7 -0
- package/{rules/app-compose/frontend → skills/app-compose-2-requirements-breakdown/references}/screen-detailview.md +0 -6
- package/{rules/app-compose/frontend → skills/app-compose-2-requirements-breakdown/references}/screen-form.md +0 -6
- package/{rules/app-compose/frontend → skills/app-compose-2-requirements-breakdown/references}/screen-listview.md +0 -6
- package/skills/app-compose-2-requirements-breakdown/references/structure.md +27 -0
- package/skills/app-compose-3-doc-review/SKILL.md +4 -0
- package/skills/app-compose-3-doc-review/references/structure.md +27 -0
- package/skills/app-compose-4-design-mock/SKILL.md +8 -0
- package/{rules/app-compose/frontend → skills/app-compose-4-design-mock/references}/component.md +0 -5
- package/skills/app-compose-4-design-mock/references/screen-detailview.md +106 -0
- package/skills/app-compose-4-design-mock/references/screen-form.md +139 -0
- package/skills/app-compose-4-design-mock/references/screen-listview.md +153 -0
- package/skills/app-compose-4-design-mock/references/structure.md +27 -0
- package/skills/app-compose-5-design-mock-review/SKILL.md +7 -0
- package/skills/app-compose-5-design-mock-review/references/component.md +50 -0
- package/skills/app-compose-5-design-mock-review/references/screen-detailview.md +106 -0
- package/skills/app-compose-5-design-mock-review/references/screen-form.md +139 -0
- package/skills/app-compose-5-design-mock-review/references/screen-listview.md +153 -0
- package/skills/app-compose-6-implementation-spec/SKILL.md +5 -0
- package/{rules/app-compose/backend → skills/app-compose-6-implementation-spec/references}/auth.md +0 -6
- package/skills/app-compose-6-implementation-spec/references/structure.md +27 -0
- package/src/cli.ts +2 -2
- package/src/commands/init.test.ts +30 -19
- package/src/commands/init.ts +76 -43
- package/rules/app-compose/frontend/auth.md +0 -55
- package/rules/app-compose/frontend/page.md +0 -86
- package/rules/module-development/cross-module-type-injection.md +0 -28
- package/rules/module-development/dependency-modules.md +0 -24
- package/rules/module-development/executors.md +0 -67
- package/rules/module-development/sync-vs-async-operations.md +0 -83
- package/rules/sdk-best-practices/sdk-docs.md +0 -14
package/CHANGELOG.md
ADDED
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright © 2025 Tailor Inc.
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
|
13
|
+
all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
+
THE SOFTWARE.
|
package/dist/cli.js
CHANGED
|
@@ -343,7 +343,6 @@ import fs3 from "fs";
|
|
|
343
343
|
import path6 from "path";
|
|
344
344
|
import chalk2 from "chalk";
|
|
345
345
|
var SKILLS_SRC = path6.join(PACKAGE_ROOT, "skills");
|
|
346
|
-
var RULES_SRC = path6.join(PACKAGE_ROOT, "rules");
|
|
347
346
|
var FRAMEWORK_SKILLS = [
|
|
348
347
|
"1-module-docs",
|
|
349
348
|
"2-module-feature-breakdown",
|
|
@@ -358,61 +357,82 @@ var FRAMEWORK_SKILLS = [
|
|
|
358
357
|
"app-compose-6-implementation-spec",
|
|
359
358
|
"mock-scenario"
|
|
360
359
|
];
|
|
360
|
+
function copyDirectoryRecursive(srcDir, destDir, force) {
|
|
361
|
+
let copied = 0;
|
|
362
|
+
let skipped = 0;
|
|
363
|
+
for (const entry of fs3.readdirSync(srcDir, { withFileTypes: true })) {
|
|
364
|
+
const srcPath = path6.join(srcDir, entry.name);
|
|
365
|
+
const destPath = path6.join(destDir, entry.name);
|
|
366
|
+
if (entry.isDirectory()) {
|
|
367
|
+
const sub = copyDirectoryRecursive(srcPath, destPath, force);
|
|
368
|
+
copied += sub.copied;
|
|
369
|
+
skipped += sub.skipped;
|
|
370
|
+
} else {
|
|
371
|
+
if (!force && fs3.existsSync(destPath)) {
|
|
372
|
+
skipped++;
|
|
373
|
+
continue;
|
|
374
|
+
}
|
|
375
|
+
fs3.mkdirSync(destDir, { recursive: true });
|
|
376
|
+
fs3.copyFileSync(srcPath, destPath);
|
|
377
|
+
copied++;
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
return { copied, skipped };
|
|
381
|
+
}
|
|
361
382
|
function runInit(cwd2, force) {
|
|
362
383
|
console.log(chalk2.bold("erp-kit init\n"));
|
|
363
384
|
const skillsDest = path6.join(cwd2, ".agents", "skills");
|
|
364
385
|
let copiedCount = 0;
|
|
365
386
|
let skippedCount = 0;
|
|
366
387
|
for (const skill of FRAMEWORK_SKILLS) {
|
|
367
|
-
const
|
|
388
|
+
const srcSkillDir = path6.join(SKILLS_SRC, skill);
|
|
389
|
+
if (!fs3.existsSync(srcSkillDir)) continue;
|
|
368
390
|
const destDir = path6.join(skillsDest, skill);
|
|
369
|
-
const
|
|
370
|
-
|
|
371
|
-
if (
|
|
372
|
-
console.log(chalk2.yellow(` Skipped ${skill}/
|
|
373
|
-
skippedCount
|
|
374
|
-
continue;
|
|
391
|
+
const result = copyDirectoryRecursive(srcSkillDir, destDir, force);
|
|
392
|
+
copiedCount += result.copied;
|
|
393
|
+
if (result.skipped > 0) {
|
|
394
|
+
console.log(chalk2.yellow(` Skipped ${skill}/ (${result.skipped} existing files)`));
|
|
395
|
+
skippedCount += result.skipped;
|
|
375
396
|
}
|
|
376
|
-
fs3.mkdirSync(destDir, { recursive: true });
|
|
377
|
-
fs3.copyFileSync(srcSkill, destSkill);
|
|
378
|
-
copiedCount++;
|
|
379
397
|
}
|
|
380
|
-
console.log(chalk2.green(` Copied ${copiedCount}
|
|
398
|
+
console.log(chalk2.green(` Copied ${copiedCount} skill files to .agents/skills/`));
|
|
381
399
|
if (skippedCount > 0) {
|
|
382
400
|
console.log(
|
|
383
|
-
chalk2.yellow(` Skipped ${skippedCount} existing
|
|
401
|
+
chalk2.yellow(` Skipped ${skippedCount} existing files (use --force to overwrite)`)
|
|
384
402
|
);
|
|
385
403
|
}
|
|
386
|
-
const
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
404
|
+
const claudeSkills = path6.join(cwd2, ".claude", "skills");
|
|
405
|
+
const relTarget = path6.relative(path6.dirname(claudeSkills), skillsDest);
|
|
406
|
+
const claudeSkillsExists = (() => {
|
|
407
|
+
try {
|
|
408
|
+
fs3.lstatSync(claudeSkills);
|
|
409
|
+
return true;
|
|
410
|
+
} catch {
|
|
411
|
+
return false;
|
|
412
|
+
}
|
|
413
|
+
})();
|
|
414
|
+
if (claudeSkillsExists) {
|
|
415
|
+
const stat2 = fs3.lstatSync(claudeSkills);
|
|
416
|
+
if (stat2.isSymbolicLink()) {
|
|
417
|
+
const current = fs3.readlinkSync(claudeSkills);
|
|
418
|
+
if (current === relTarget) {
|
|
419
|
+
console.log(chalk2.green(" .claude/skills -> .agents/skills/ (already linked)"));
|
|
420
|
+
} else if (force) {
|
|
421
|
+
fs3.unlinkSync(claudeSkills);
|
|
422
|
+
fs3.symlinkSync(relTarget, claudeSkills);
|
|
423
|
+
console.log(chalk2.green(" .claude/skills -> .agents/skills/ (relinked)"));
|
|
424
|
+
} else {
|
|
425
|
+
console.log(
|
|
426
|
+
chalk2.yellow(` Skipped .claude/skills (symlink exists -> ${current}, use --force)`)
|
|
427
|
+
);
|
|
407
428
|
}
|
|
408
|
-
}
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
);
|
|
429
|
+
} else {
|
|
430
|
+
console.log(chalk2.yellow(" Skipped .claude/skills (directory exists, not a symlink)"));
|
|
431
|
+
}
|
|
432
|
+
} else {
|
|
433
|
+
fs3.mkdirSync(path6.dirname(claudeSkills), { recursive: true });
|
|
434
|
+
fs3.symlinkSync(relTarget, claudeSkills);
|
|
435
|
+
console.log(chalk2.green(" .claude/skills -> .agents/skills/ (linked)"));
|
|
416
436
|
}
|
|
417
437
|
console.log(chalk2.bold.green("\nDone! Run `erp-kit check` to validate your docs."));
|
|
418
438
|
return 0;
|
|
@@ -868,11 +888,11 @@ var scaffoldCommand = defineCommand2({
|
|
|
868
888
|
});
|
|
869
889
|
var initCommand = defineCommand2({
|
|
870
890
|
name: "init",
|
|
871
|
-
description: "Set up consumer repo
|
|
891
|
+
description: "Set up consumer repo with framework skills",
|
|
872
892
|
args: z2.object({
|
|
873
893
|
force: arg2(z2.boolean().default(false), {
|
|
874
894
|
alias: "f",
|
|
875
|
-
description: "Overwrite existing skills
|
|
895
|
+
description: "Overwrite existing skills"
|
|
876
896
|
})
|
|
877
897
|
}),
|
|
878
898
|
run: (args) => {
|
package/package.json
CHANGED
|
@@ -1,13 +1,15 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tailor-platform/erp-kit",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.1",
|
|
4
4
|
"description": "Opinionated ERP toolkit for building business applications on Tailor Platform",
|
|
5
|
+
"license": "MIT",
|
|
5
6
|
"bin": {
|
|
6
7
|
"erp-kit": "./dist/cli.js"
|
|
7
8
|
},
|
|
8
9
|
"files": [
|
|
10
|
+
"CHANGELOG.md",
|
|
9
11
|
"dist",
|
|
10
|
-
"
|
|
12
|
+
"LICENSE",
|
|
11
13
|
"schemas",
|
|
12
14
|
"skills",
|
|
13
15
|
"src"
|
|
@@ -1,15 +1,10 @@
|
|
|
1
|
-
---
|
|
2
|
-
paths:
|
|
3
|
-
- "modules/*/src/"
|
|
4
|
-
---
|
|
5
|
-
|
|
6
1
|
# Module Directory Structure
|
|
7
2
|
|
|
8
3
|
```
|
|
9
4
|
src/
|
|
10
5
|
├── db/ # Database models (one file per model)
|
|
11
6
|
├── executor/ # Async executors (record triggers, job functions)
|
|
12
|
-
├──
|
|
7
|
+
├── command/ # Domain commands + tests (*.test.ts co-located)
|
|
13
8
|
├── lib/ # Internal shared code (errors.ts, types.ts)
|
|
14
9
|
├── testing/ # Test fixtures and helpers
|
|
15
10
|
├── generated/ # Auto-generated code (do not edit)
|
|
@@ -21,7 +16,7 @@ src/
|
|
|
21
16
|
|
|
22
17
|
- `db/`: Only documentable model definitions, no helpers
|
|
23
18
|
- `executor/`: Async executors as factory functions (see executors.md)
|
|
24
|
-
- `
|
|
19
|
+
- `command/`: Domain commands + co-located tests, no utilities
|
|
25
20
|
- `lib/`: Internal errors and types (not documented)
|
|
26
21
|
- `testing/`: Fixtures for tests only
|
|
27
22
|
- Run `pnpm generate` after modifying `db/` models
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
# Module Directory Structure
|
|
2
|
+
|
|
3
|
+
```
|
|
4
|
+
src/
|
|
5
|
+
├── db/ # Database models (one file per model)
|
|
6
|
+
├── executor/ # Async executors (record triggers, job functions)
|
|
7
|
+
├── command/ # Domain commands + tests (*.test.ts co-located)
|
|
8
|
+
├── lib/ # Internal shared code (errors.ts, types.ts)
|
|
9
|
+
├── testing/ # Test fixtures and helpers
|
|
10
|
+
├── generated/ # Auto-generated code (do not edit)
|
|
11
|
+
├── index.ts # Public exports
|
|
12
|
+
└── module.ts # Module definition
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## Rules
|
|
16
|
+
|
|
17
|
+
- `db/`: Only documentable model definitions, no helpers
|
|
18
|
+
- `executor/`: Async executors as factory functions (see executors.md)
|
|
19
|
+
- `command/`: Domain commands + co-located tests, no utilities
|
|
20
|
+
- `lib/`: Internal errors and types (not documented)
|
|
21
|
+
- `testing/`: Fixtures for tests only
|
|
22
|
+
- Run `pnpm generate` after modifying `db/` models
|
|
@@ -228,3 +228,9 @@ For each command doc:
|
|
|
228
228
|
| "Deactivate X" | deactivateX |
|
|
229
229
|
| "Assign X to Y" | assignXToY |
|
|
230
230
|
| "Remove X from Y" | removeXFromY |
|
|
231
|
+
|
|
232
|
+
## References
|
|
233
|
+
|
|
234
|
+
- [Model patterns](references/models.md)
|
|
235
|
+
- [Command patterns](references/commands.md)
|
|
236
|
+
- [Testing patterns](references/testing.md)
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
---
|
|
2
|
+
paths:
|
|
3
|
+
- "packages/erp-kit/src/modules/*/command/*.ts"
|
|
4
|
+
- "!packages/erp-kit/src/modules/*/command/*.test.ts"
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
# Command Implementation
|
|
8
|
+
|
|
9
|
+
## defineCommand Pattern
|
|
10
|
+
|
|
11
|
+
Commands that don't need custom fields use `defineCommand` directly:
|
|
12
|
+
|
|
13
|
+
```typescript
|
|
14
|
+
export const myCommand = defineCommand(permissions.myCommand, async (db: DB, input: Input) => { ... });
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
## Factory Function Pattern (custom fields)
|
|
18
|
+
|
|
19
|
+
Commands that insert into a table with user-extensible fields use a `makeCreateX<CF>()` factory:
|
|
20
|
+
|
|
21
|
+
- Export only `makeCreateX` — no default instance
|
|
22
|
+
- Generic `CF extends Record<string, unknown>` for custom fields
|
|
23
|
+
- Input type: `CreateXInput & CF`
|
|
24
|
+
- Destructure known fields, rest-spread custom fields into `.values()` before known fields
|
|
25
|
+
- Cast custom fields: `...(customFields as Record<string, unknown>)`
|
|
26
|
+
- `module.ts` wires with `makeCreateX<FieldsToInsertable<F>>()`
|
|
27
|
+
|
|
28
|
+
## Implementation Considerations
|
|
29
|
+
|
|
30
|
+
- **Validation**: Check referenced entities exist before operating
|
|
31
|
+
- **Idempotency**: For assign/revoke, return existing instead of throwing
|
|
32
|
+
- **Return format**: Wrap in object `{ entity }` not just `entity`
|
|
33
|
+
|
|
34
|
+
## Conventions
|
|
35
|
+
|
|
36
|
+
- Input types: exported interfaces (`export interface MyFunctionInput`)
|
|
37
|
+
- Use `.executeTakeFirst()` for single results
|
|
38
|
+
- Include JSDoc: `/** Function: name \n Description */`
|
|
39
|
+
|
|
40
|
+
## State Transitions
|
|
41
|
+
|
|
42
|
+
For commands that transition between statuses, accept `from?: string[]` with a default:
|
|
43
|
+
|
|
44
|
+
```typescript
|
|
45
|
+
from?: string[]; // Default: ["ACTIVE"]
|
|
46
|
+
|
|
47
|
+
const validFromStatuses = input.from ?? ["ACTIVE"];
|
|
48
|
+
if (!validFromStatuses.includes(user.status)) {
|
|
49
|
+
throw new InvalidStatusTransitionError(user.status, targetStatus);
|
|
50
|
+
}
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
- Default `from` contains the base valid source status
|
|
54
|
+
- Parent modules can override to allow transitions from additional statuses
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
# Database Models
|
|
2
|
+
|
|
3
|
+
## Factory Function Pattern
|
|
4
|
+
|
|
5
|
+
Each model is a `createXType<const F>(params)` factory:
|
|
6
|
+
|
|
7
|
+
- Params interface generic: `F extends Record<string, TailorAnyDBField>`
|
|
8
|
+
- `fields?: F` — optional custom fields from parent modules
|
|
9
|
+
- Spread as `...(params.fields ?? {}) as F` to preserve type information
|
|
10
|
+
- Include `...db.fields.timestamps()`
|
|
11
|
+
- Use `.description()` for field docs
|
|
12
|
+
- Apply permissions at model level
|
|
13
|
+
- Export a default instance: `export const x = createXType({})`
|
|
14
|
+
|
|
15
|
+
## Stateful Model Enums
|
|
16
|
+
|
|
17
|
+
Status enums are tied to the module's state machine. Base module defines core statuses; parent callers can only **extend, not replace**.
|
|
18
|
+
|
|
19
|
+
```typescript
|
|
20
|
+
// Good: extension only
|
|
21
|
+
const BASE_STATUSES = ["PENDING", "ACTIVE", "INACTIVE"] as const;
|
|
22
|
+
const statuses = [...BASE_STATUSES, ...(params.additionalStatuses ?? [])];
|
|
23
|
+
|
|
24
|
+
// Bad: allows replacement
|
|
25
|
+
const statuses = params.statuses ?? ["PENDING", "ACTIVE", "INACTIVE"];
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
- Name parameter `additionalX` to signal extension-only
|
|
29
|
+
- Parent modules handle their additional status transitions
|
|
@@ -24,27 +24,35 @@ MODELS → ERRORS → FIXTURES → TESTS → IMPLEMENT → EXPORT → VERIFY
|
|
|
24
24
|
|
|
25
25
|
### 1. Database Models (`src/db/`)
|
|
26
26
|
|
|
27
|
-
|
|
27
|
+
**Read [models rules](references/models.md) and [db-relations rules](references/db-relations.md) before writing any model.**
|
|
28
|
+
|
|
29
|
+
Key patterns: factory function, `db.fields.timestamps()`, `.description()` on fields, `.relation()` for foreign keys.
|
|
28
30
|
|
|
29
31
|
### 2. Error Classes (`src/lib/errors.ts`)
|
|
30
32
|
|
|
31
|
-
|
|
33
|
+
**Read [errors rules](references/errors.md) before defining errors.**
|
|
34
|
+
|
|
35
|
+
Key patterns: `name` as const = class name, `code` as const = SCREAMING_SNAKE_CASE.
|
|
32
36
|
|
|
33
37
|
### 3. Test Fixtures (`src/testing/fixtures.ts`)
|
|
34
38
|
|
|
35
|
-
|
|
39
|
+
**Read [testing rules](references/testing.md) before writing fixtures or tests.**
|
|
36
40
|
|
|
37
41
|
### 4. Write Tests First (`src/command/*.test.ts`)
|
|
38
42
|
|
|
39
|
-
|
|
43
|
+
Same reference: [testing rules](references/testing.md).
|
|
44
|
+
|
|
45
|
+
Key patterns: `createMockDb<DB>()`, fixed IDs `"entity-1"`, cover all doc branches.
|
|
40
46
|
|
|
41
47
|
### 5. Implement Commands (`src/command/*.ts`)
|
|
42
48
|
|
|
43
|
-
|
|
49
|
+
**Read [commands rules](references/commands.md) before implementing.**
|
|
50
|
+
|
|
51
|
+
Key patterns: dual function (`_fn` internal + `fn<T>` public), validate → query → mutate.
|
|
44
52
|
|
|
45
53
|
### 6. Export (`src/index.ts`)
|
|
46
54
|
|
|
47
|
-
|
|
55
|
+
**Read [exports rules](references/exports.md) for export order.**
|
|
48
56
|
|
|
49
57
|
### 7. Verify
|
|
50
58
|
|
|
@@ -54,3 +62,13 @@ pnpm lint # Check code style
|
|
|
54
62
|
pnpm typecheck # Verify TypeScript types
|
|
55
63
|
pnpm test # Run all tests
|
|
56
64
|
```
|
|
65
|
+
|
|
66
|
+
## References
|
|
67
|
+
|
|
68
|
+
- [Module structure](references/structure.md)
|
|
69
|
+
- [Model patterns](references/models.md)
|
|
70
|
+
- [Database relations](references/db-relations.md)
|
|
71
|
+
- [Command patterns](references/commands.md)
|
|
72
|
+
- [Error patterns](references/errors.md)
|
|
73
|
+
- [Testing patterns](references/testing.md)
|
|
74
|
+
- [Export order](references/exports.md)
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
# Command Implementation
|
|
2
|
+
|
|
3
|
+
## Dual Function Pattern
|
|
4
|
+
|
|
5
|
+
Two functions per operation: internal `_fn` and public `fn`.
|
|
6
|
+
|
|
7
|
+
**Internal function (`_myFunction`):**
|
|
8
|
+
|
|
9
|
+
- Takes concrete `DB` type from `generated/kysely-tailordb`
|
|
10
|
+
- Return type uses `Schema` from `lib/types`: `Promise<{ entity: Entity<Schema> }>`
|
|
11
|
+
- Contains actual implementation
|
|
12
|
+
|
|
13
|
+
**Public function (`myFunction`):**
|
|
14
|
+
|
|
15
|
+
- Generic signature: `<T extends { Entity: object }>(db: Kysely<T>, ...)`
|
|
16
|
+
- Return type uses generic `T`: `Promise<{ entity: Entity<T> }>`
|
|
17
|
+
- Delegates to internal with type casting: `_fn(db as unknown as DB, ...) as unknown as Result`
|
|
18
|
+
|
|
19
|
+
## Implementation Considerations
|
|
20
|
+
|
|
21
|
+
- **Validation**: Check referenced entities exist before operating
|
|
22
|
+
- **Idempotency**: For assign/revoke, return existing instead of throwing
|
|
23
|
+
- **Return format**: Wrap in object `{ entity }` not just `entity`
|
|
24
|
+
|
|
25
|
+
## Conventions
|
|
26
|
+
|
|
27
|
+
- Input types: exported interfaces (`export interface MyFunctionInput`)
|
|
28
|
+
- Use `.executeTakeFirst()` for single results
|
|
29
|
+
- Include JSDoc: `/** Function: name \n Description */`
|
|
30
|
+
|
|
31
|
+
## State Transitions
|
|
32
|
+
|
|
33
|
+
For commands that transition between statuses, accept `from?: string[]` with a default:
|
|
34
|
+
|
|
35
|
+
```typescript
|
|
36
|
+
from?: string[]; // Default: ["ACTIVE"]
|
|
37
|
+
|
|
38
|
+
const validFromStatuses = input.from ?? ["ACTIVE"];
|
|
39
|
+
if (!validFromStatuses.includes(user.status)) {
|
|
40
|
+
throw new InvalidStatusTransitionError(user.status, targetStatus);
|
|
41
|
+
}
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
- Default `from` contains the base valid source status
|
|
45
|
+
- Parent modules can override to allow transitions from additional statuses
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
# Database Models
|
|
2
|
+
|
|
3
|
+
## Factory Function Pattern
|
|
4
|
+
|
|
5
|
+
```typescript
|
|
6
|
+
export function createEntityType(params: {
|
|
7
|
+
fields?: Record<string, unknown>;
|
|
8
|
+
additionalStatuses?: string[];
|
|
9
|
+
});
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
- Include `...db.fields.timestamps()`
|
|
13
|
+
- Use `.description()` for field docs
|
|
14
|
+
- Apply permissions at model level
|
|
15
|
+
|
|
16
|
+
## Stateful Model Enums
|
|
17
|
+
|
|
18
|
+
Status enums are tied to the module's state machine. Base module defines core statuses; parent callers can only **extend, not replace**.
|
|
19
|
+
|
|
20
|
+
```typescript
|
|
21
|
+
// Good: extension only
|
|
22
|
+
const BASE_STATUSES = ["PENDING", "ACTIVE", "INACTIVE"] as const;
|
|
23
|
+
const statuses = [...BASE_STATUSES, ...(params.additionalStatuses ?? [])];
|
|
24
|
+
|
|
25
|
+
// Bad: allows replacement
|
|
26
|
+
const statuses = params.statuses ?? ["PENDING", "ACTIVE", "INACTIVE"];
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
- Name parameter `additionalX` to signal extension-only
|
|
30
|
+
- Parent modules handle their additional status transitions
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
# Module Directory Structure
|
|
2
|
+
|
|
3
|
+
```
|
|
4
|
+
src/
|
|
5
|
+
├── db/ # Database models (one file per model)
|
|
6
|
+
├── executor/ # Async executors (record triggers, job functions)
|
|
7
|
+
├── command/ # Domain commands + tests (*.test.ts co-located)
|
|
8
|
+
├── lib/ # Internal shared code (errors.ts, types.ts)
|
|
9
|
+
├── testing/ # Test fixtures and helpers
|
|
10
|
+
├── generated/ # Auto-generated code (do not edit)
|
|
11
|
+
├── index.ts # Public exports
|
|
12
|
+
└── module.ts # Module definition
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## Rules
|
|
16
|
+
|
|
17
|
+
- `db/`: Only documentable model definitions, no helpers
|
|
18
|
+
- `executor/`: Async executors as factory functions (see executors.md)
|
|
19
|
+
- `command/`: Domain commands + co-located tests, no utilities
|
|
20
|
+
- `lib/`: Internal errors and types (not documented)
|
|
21
|
+
- `testing/`: Fixtures for tests only
|
|
22
|
+
- Run `pnpm generate` after modifying `db/` models
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
# Testing Patterns
|
|
2
|
+
|
|
3
|
+
## Test Coverage Goal
|
|
4
|
+
|
|
5
|
+
Tests should cover all paths in the corresponding `docs/commands/*.md`:
|
|
6
|
+
|
|
7
|
+
- **Process Flow**: Each branch in the mermaid flowchart = one test case
|
|
8
|
+
- **Error Scenarios**: Each error code listed = one test case
|
|
9
|
+
- **Idempotent paths**: If flowchart shows "Already exists? → Return existing"
|
|
10
|
+
|
|
11
|
+
## Mock Database
|
|
12
|
+
|
|
13
|
+
```typescript
|
|
14
|
+
const { db, spies } = createMockDb<DB>();
|
|
15
|
+
|
|
16
|
+
// Single return
|
|
17
|
+
spies.select.mockReturnValue(entity);
|
|
18
|
+
|
|
19
|
+
// Sequential returns (in query execution order)
|
|
20
|
+
spies.select.mockReturnValueOnce(first).mockReturnValueOnce(second);
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
## Custom Fields Passthrough
|
|
24
|
+
|
|
25
|
+
For `makeCreateX` factory commands, add one test verifying custom fields reach `.values()`:
|
|
26
|
+
|
|
27
|
+
- Instantiate with concrete type: `makeCreateX<{ myField: string }>()`
|
|
28
|
+
- Assert with `spies.values`: `expect(spies.values).toHaveBeenNthCalledWith(1, expect.objectContaining({ myField: "value" }))`
|
|
29
|
+
- Use `toHaveBeenNthCalledWith(n, ...)` when multiple inserts occur (e.g., audit events)
|
|
30
|
+
|
|
31
|
+
## Fixtures (`src/testing/fixtures.ts`)
|
|
32
|
+
|
|
33
|
+
- Import `Schema` from `lib/types` (not `Namespace` from generated code)
|
|
34
|
+
- Pattern: `export const baseEntity = { ... } as const satisfies Entity<Schema>`
|
|
35
|
+
- Fixed IDs for traceability: `"entity-1"`
|
|
36
|
+
- Consistent timestamp: `new Date("2024-01-01T00:00:00.000Z")`
|
|
37
|
+
- `updatedAt: null` for base fixtures
|
|
@@ -398,3 +398,11 @@ describe("myCommand", () => {
|
|
|
398
398
|
it("throws NOT_FOUND when missing"); // Check: error scenario
|
|
399
399
|
});
|
|
400
400
|
```
|
|
401
|
+
|
|
402
|
+
## References
|
|
403
|
+
|
|
404
|
+
- [Model patterns](references/models.md)
|
|
405
|
+
- [Command patterns](references/commands.md)
|
|
406
|
+
- [Error patterns](references/errors.md)
|
|
407
|
+
- [Testing patterns](references/testing.md)
|
|
408
|
+
- [Export order](references/exports.md)
|