@openreachtech/hora-skills-ort-renchan 0.0.0
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/LICENSE +201 -0
- package/README.ja.md +135 -0
- package/README.md +135 -0
- package/dist/skills/hor-agent-loop/SKILL.md +123 -0
- package/dist/skills/hor-agent-loop/references/core.md +485 -0
- package/dist/skills/hor-agent-loop/references/graphql-integration.md +178 -0
- package/dist/skills/hor-agent-loop/references/renchan-job-integration.md +368 -0
- package/dist/skills/hor-agent-loop/references/testing.md +492 -0
- package/dist/skills/hor-ai-agent-structure/SKILL.md +403 -0
- package/dist/skills/hor-ai-agent-structure/references/loop-and-actions.md +100 -0
- package/dist/skills/hor-ai-prompt-document-store/SKILL.md +220 -0
- package/dist/skills/hor-ai-prompt-document-store/references/models-and-composition.md +156 -0
- package/dist/skills/hor-backend-testing/SKILL.md +118 -0
- package/dist/skills/hor-backend-testing/references/placement-and-ordering.md +89 -0
- package/dist/skills/hor-backend-testing/references/purity-and-mocks.md +82 -0
- package/dist/skills/hor-backend-testing/references/running-tests.md +186 -0
- package/dist/skills/hor-bank-id/SKILL.md +91 -0
- package/dist/skills/hor-build-e2e-test-environment/SKILL.md +609 -0
- package/dist/skills/hor-build-e2e-test-environment/references/compose-definition.md +368 -0
- package/dist/skills/hor-build-e2e-test-environment/references/edge-and-proxy.md +287 -0
- package/dist/skills/hor-build-e2e-test-environment/references/environment-and-ports.md +201 -0
- package/dist/skills/hor-build-e2e-test-environment/references/runner-and-lifecycle.md +330 -0
- package/dist/skills/hor-build-e2e-test-environment/references/seed-data.md +145 -0
- package/dist/skills/hor-build-e2e-test-environment/references/windows-runner.md +146 -0
- package/dist/skills/hor-constant-definition/SKILL.md +139 -0
- package/dist/skills/hor-cookie-authentication/SKILL.md +33 -0
- package/dist/skills/hor-cookie-authentication/references/auth-filter.md +55 -0
- package/dist/skills/hor-cookie-authentication/references/cookie-context.md +53 -0
- package/dist/skills/hor-cookie-authentication/references/dev-login-seeder.md +22 -0
- package/dist/skills/hor-cookie-authentication/references/environment.md +64 -0
- package/dist/skills/hor-cookie-authentication/references/error-codes.md +24 -0
- package/dist/skills/hor-cookie-authentication/references/migrations.md +18 -0
- package/dist/skills/hor-cookie-authentication/references/resolvers.md +97 -0
- package/dist/skills/hor-cookie-authentication/references/session-clerk.md +41 -0
- package/dist/skills/hor-cookie-authentication/references/token-models.md +125 -0
- package/dist/skills/hor-database-design/SKILL.md +54 -0
- package/dist/skills/hor-database-design/references/column-types.md +92 -0
- package/dist/skills/hor-database-design/references/datetimes.md +88 -0
- package/dist/skills/hor-database-design/references/master-tables.md +117 -0
- package/dist/skills/hor-database-design/references/normalization.md +40 -0
- package/dist/skills/hor-database-design/references/update-history.md +35 -0
- package/dist/skills/hor-execution-placement-pattern/SKILL.md +216 -0
- package/dist/skills/hor-external-api-client/SKILL.md +99 -0
- package/dist/skills/hor-external-api-client/references/architecture.md +127 -0
- package/dist/skills/hor-external-api-client/references/authorization.md +105 -0
- package/dist/skills/hor-external-api-client/references/capsule.md +130 -0
- package/dist/skills/hor-external-api-client/references/conventions.md +68 -0
- package/dist/skills/hor-external-api-client/references/launcher.md +138 -0
- package/dist/skills/hor-external-api-client/references/payload.md +176 -0
- package/dist/skills/hor-external-api-client/references/sdk-wrapper.md +291 -0
- package/dist/skills/hor-external-api-client/references/testing.md +130 -0
- package/dist/skills/hor-external-api-client/references/usage.md +152 -0
- package/dist/skills/hor-graphql-schema/SKILL.md +360 -0
- package/dist/skills/hor-graphql-server-engine/SKILL.md +356 -0
- package/dist/skills/hor-light-rag/SKILL.md +242 -0
- package/dist/skills/hor-light-rag/references/retrieval-internals.md +143 -0
- package/dist/skills/hor-multi-llm-provider/SKILL.md +288 -0
- package/dist/skills/hor-multi-llm-provider/references/provider-internals.md +153 -0
- package/dist/skills/hor-mutation-resolver/SKILL.md +360 -0
- package/dist/skills/hor-mutation-resolver/references/dependency-injection.md +148 -0
- package/dist/skills/hor-mutation-resolver/references/errors.md +120 -0
- package/dist/skills/hor-mutation-resolver/references/resolve-flow.md +275 -0
- package/dist/skills/hor-mutation-resolver/references/testing.md +125 -0
- package/dist/skills/hor-mutation-resolver/references/transaction.md +197 -0
- package/dist/skills/hor-post-worker/SKILL.md +320 -0
- package/dist/skills/hor-query-resolver/SKILL.md +246 -0
- package/dist/skills/hor-query-resolver/references/anatomy.md +384 -0
- package/dist/skills/hor-query-resolver/references/data-access.md +248 -0
- package/dist/skills/hor-query-resolver/references/errors-and-context.md +172 -0
- package/dist/skills/hor-query-resolver/references/testing.md +241 -0
- package/dist/skills/hor-renchan-job-bullmq/SKILL.md +178 -0
- package/dist/skills/hor-renchan-job-bullmq/references/daemon-and-scheduler.md +182 -0
- package/dist/skills/hor-renchan-job-bullmq/references/engine-and-infra.md +106 -0
- package/dist/skills/hor-renchan-job-bullmq/references/job-triple.md +189 -0
- package/dist/skills/hor-renchan-job-bullmq/references/queues.md +74 -0
- package/dist/skills/hor-renchan-job-bullmq/references/subscriptions.md +113 -0
- package/dist/skills/hor-resolver-share/SKILL.md +262 -0
- package/dist/skills/hor-resolver-validator/SKILL.md +194 -0
- package/dist/skills/hor-resolver-validator/references/inspector-api.md +115 -0
- package/dist/skills/hor-resolver-validator/references/validator-pattern.md +281 -0
- package/dist/skills/hor-restfulapi-architecture/SKILL.md +275 -0
- package/dist/skills/hor-restfulapi-architecture/references/engine-and-context.md +192 -0
- package/dist/skills/hor-restfulapi-architecture/references/renderers.md +204 -0
- package/dist/skills/hor-restfulapi-architecture/references/response-and-flusher.md +118 -0
- package/dist/skills/hor-restfulapi-architecture/references/validation.md +229 -0
- package/dist/skills/hor-security-audit/SKILL.md +110 -0
- package/dist/skills/hor-security-audit/references/application-surface.md +90 -0
- package/dist/skills/hor-security-audit/references/auth-and-transport.md +147 -0
- package/dist/skills/hor-security-audit/references/code-safety.md +74 -0
- package/dist/skills/hor-security-audit/references/dependencies.md +72 -0
- package/dist/skills/hor-security-audit/references/network-and-logging.md +56 -0
- package/dist/skills/hor-security-audit/references/secrets.md +95 -0
- package/dist/skills/hor-sequelize-migration/SKILL.md +336 -0
- package/dist/skills/hor-sequelize-migration/references/alter-table.md +147 -0
- package/dist/skills/hor-sequelize-migration/references/foreign-keys.md +107 -0
- package/dist/skills/hor-sequelize-migration/references/indexes.md +149 -0
- package/dist/skills/hor-sequelize-migration/references/notation.md +215 -0
- package/dist/skills/hor-sequelize-model/SKILL.md +69 -0
- package/dist/skills/hor-sequelize-model/references/associations.md +218 -0
- package/dist/skills/hor-sequelize-model/references/default-methods.md +244 -0
- package/dist/skills/hor-sequelize-model/references/mixins.md +247 -0
- package/dist/skills/hor-sequelize-model/references/notation.md +225 -0
- package/dist/skills/hor-sequelize-model/references/timestamps.md +74 -0
- package/dist/skills/hor-sequelize-seeder/SKILL.md +275 -0
- package/dist/skills/hor-sequelize-seeder/references/development-coverage.md +74 -0
- package/dist/skills/hor-sequelize-seeder/references/directory-structure.md +113 -0
- package/dist/skills/hor-sequelize-seeder/references/id-numbering.md +133 -0
- package/dist/skills/hor-sequelize-seeder/references/naming.md +59 -0
- package/dist/skills/hor-sequelize-seeder/references/notation.md +170 -0
- package/dist/skills/hor-sequelize-subquery/SKILL.md +369 -0
- package/dist/skills/hor-strategy-pattern/SKILL.md +371 -0
- package/dist/skills/hor-stub-api/SKILL.md +330 -0
- package/dist/skills/hor-subscription-resolver/SKILL.md +105 -0
- package/dist/skills/hor-subscription-resolver/references/authentication.md +99 -0
- package/dist/skills/hor-subscription-resolver/references/basic-notation.md +116 -0
- package/dist/skills/hor-subscription-resolver/references/design-philosophy.md +68 -0
- package/dist/skills/hor-type-interface/SKILL.md +200 -0
- package/docs/skills.ja.md +40 -0
- package/docs/skills.md +40 -0
- package/lib/equip/CommandLineArguments.js +178 -0
- package/lib/equip/HoraSkillsCli.js +590 -0
- package/lib/equip/SkillsInstaller.js +443 -0
- package/lib/equip/SkillsManifestFile.js +260 -0
- package/lib/equip/constants/CLI_COMMAND.js +8 -0
- package/lib/equip/hora-skills-ort-renchan.js +8 -0
- package/package.json +51 -0
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
# Notation (file skeleton, seeds, bulkInsert / bulkDelete, timestamps)
|
|
2
|
+
|
|
3
|
+
The skeleton of a seeder file, how to write the `seeds` arrays, the `up` / `down` bodies, suites,
|
|
4
|
+
async fulfillment, and how timestamps are supplied. Referenced from §2 and §5 of `SKILL.md`. For
|
|
5
|
+
the directory split see [directory-structure.md](./directory-structure.md); for ids see
|
|
6
|
+
[id-numbering.md](./id-numbering.md).
|
|
7
|
+
|
|
8
|
+
## CommonJS, require the supplier (and any constants)
|
|
9
|
+
|
|
10
|
+
A seeder is a `.cjs` (CommonJS) file. Start with `'use strict'`, then **`require`
|
|
11
|
+
`TimestampSeedsSupplier`**. Optionally `require` domain constants so the seed and the app share one
|
|
12
|
+
source of truth for ids / names.
|
|
13
|
+
|
|
14
|
+
```js
|
|
15
|
+
'use strict'
|
|
16
|
+
|
|
17
|
+
const TimestampSeedsSupplier = require('@openreachtech/renchan-sequelize/lib/tools/TimestampSeedsSupplier.cjs')
|
|
18
|
+
|
|
19
|
+
const {
|
|
20
|
+
AI_PROVIDER,
|
|
21
|
+
} = require('../../../app/constants/aiModel.cjs')
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
- **Why**: sequelize-cli loads seeders as CommonJS. `TimestampSeedsSupplier` injects the audit
|
|
25
|
+
timestamps (see below). Pulling ids / names from `app/constants/*` keeps a canonical value in one
|
|
26
|
+
place instead of hard-coding it in both the app and the seed.
|
|
27
|
+
|
|
28
|
+
## Intent comment, then `TABLE_NAME`
|
|
29
|
+
|
|
30
|
+
Put a `/* ... */` comment near the top saying what the seed is and its design-doc / plan reference
|
|
31
|
+
(domain prose is Japanese in this repo). Declare `TABLE_NAME` as a **string** for a single table, or
|
|
32
|
+
an **object** when one file fills several related tables (a "suite").
|
|
33
|
+
|
|
34
|
+
```js
|
|
35
|
+
// single table
|
|
36
|
+
const TABLE_NAME = 'ai_providers'
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
```js
|
|
40
|
+
// suite (several related tables in one file)
|
|
41
|
+
const TABLE_NAME = {
|
|
42
|
+
CUSTOMERS: 'customers',
|
|
43
|
+
CUSTOMER_BASICS: 'customer_basics',
|
|
44
|
+
CUSTOMER_ACCESS_TOKENS: 'customer_access_tokens',
|
|
45
|
+
}
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
## `seeds` arrays use physical (snake_case) column names + explicit id
|
|
49
|
+
|
|
50
|
+
Each `seeds` array holds plain row objects. Keys are the **physical column names (snake_case)** —
|
|
51
|
+
the migration's `field:` names, **not** the model's camelCase — because `bulkInsert` writes raw
|
|
52
|
+
columns. Every row starts with an explicit `id` ([id-numbering.md](./id-numbering.md)). Do **not**
|
|
53
|
+
put `created_at` / `updated_at` in the rows (see "Timestamps" below).
|
|
54
|
+
|
|
55
|
+
```js
|
|
56
|
+
const seeds = [
|
|
57
|
+
{ id: AI_PROVIDER.DEFAULT.ID, name: AI_PROVIDER.DEFAULT.NAME },
|
|
58
|
+
]
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
```js
|
|
62
|
+
// FK columns are snake_case and hold an id from the referenced table's block
|
|
63
|
+
const seeds = [
|
|
64
|
+
{ id: 530001, software_package_id: 520001, name: 'Email integration', additional_cost: 500000, is_active: true },
|
|
65
|
+
{ id: 530002, software_package_id: 520001, name: 'Advanced reporting', additional_cost: 800000, is_active: true },
|
|
66
|
+
{ id: 530003, software_package_id: 520002, name: 'Multi-currency', additional_cost: 1000000, is_active: true },
|
|
67
|
+
]
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
- **Why snake_case**: the seed is coupled to the physical schema (the migration), not to the model.
|
|
71
|
+
A camelCase key would write the wrong column.
|
|
72
|
+
|
|
73
|
+
## `up` = bulkInsert(supplyAll(seeds)); `down` = bulkDelete by id
|
|
74
|
+
|
|
75
|
+
`module.exports` has an `async up` and `async down`, each taking `(queryInterface, Sequelize)`.
|
|
76
|
+
|
|
77
|
+
```js
|
|
78
|
+
module.exports = {
|
|
79
|
+
up: async (queryInterface, Sequelize) => {
|
|
80
|
+
await queryInterface.bulkInsert(TABLE_NAME, TimestampSeedsSupplier.supplyAll(seeds), {})
|
|
81
|
+
},
|
|
82
|
+
|
|
83
|
+
down: async (queryInterface, Sequelize) => {
|
|
84
|
+
await queryInterface.bulkDelete(TABLE_NAME, { id: seeds.map(it => it.id) })
|
|
85
|
+
},
|
|
86
|
+
}
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
- `up` inserts `TimestampSeedsSupplier.supplyAll(seeds)` (the rows with audit timestamps added). The
|
|
90
|
+
third `{}` is `bulkInsert`'s options.
|
|
91
|
+
- `down` deletes exactly the rows `up` inserted, by their explicit id list
|
|
92
|
+
(`{ id: seeds.map(it => it.id) }`). This is why every row needs a stable, explicit `id`.
|
|
93
|
+
|
|
94
|
+
## Suites: insert parent → child, delete child → parent
|
|
95
|
+
|
|
96
|
+
When a file fills several related tables, insert **parent before child** in `up` (a child's FK
|
|
97
|
+
column must see its parent row) and delete **child before parent** in `down` (reverse order).
|
|
98
|
+
|
|
99
|
+
```js
|
|
100
|
+
module.exports = {
|
|
101
|
+
up: async (queryInterface, Sequelize) => {
|
|
102
|
+
await queryInterface.bulkInsert(TABLE_NAME.CUSTOMERS, TimestampSeedsSupplier.supplyAll(customersSeeds), {})
|
|
103
|
+
await queryInterface.bulkInsert(TABLE_NAME.CUSTOMER_BASICS, TimestampSeedsSupplier.supplyAll(customerBasicsSeeds), {})
|
|
104
|
+
await queryInterface.bulkInsert(TABLE_NAME.CUSTOMER_ACCESS_TOKENS, TimestampSeedsSupplier.supplyAll(customerAccessTokensSeeds), {})
|
|
105
|
+
},
|
|
106
|
+
|
|
107
|
+
down: async (queryInterface, Sequelize) => {
|
|
108
|
+
await queryInterface.bulkDelete(TABLE_NAME.CUSTOMER_ACCESS_TOKENS, { id: customerAccessTokensSeeds.map(it => it.id) })
|
|
109
|
+
await queryInterface.bulkDelete(TABLE_NAME.CUSTOMER_BASICS, { id: customerBasicsSeeds.map(it => it.id) })
|
|
110
|
+
await queryInterface.bulkDelete(TABLE_NAME.CUSTOMERS, { id: customersSeeds.map(it => it.id) })
|
|
111
|
+
},
|
|
112
|
+
}
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
- **Why**: there are no DB FK constraints (`hor-sequelize-migration`),
|
|
116
|
+
but ordering still keeps the data coherent (no child row referencing a not-yet-inserted parent)
|
|
117
|
+
and mirrors how the app writes these rows.
|
|
118
|
+
|
|
119
|
+
## Async fulfillment (computed values such as password hashes)
|
|
120
|
+
|
|
121
|
+
When a value must be computed (hashing a raw password, etc.), build the fulfilled array with
|
|
122
|
+
`await Promise.all(...)` inside `up` before `bulkInsert`. Keep the plain `seeds` array for `down`'s
|
|
123
|
+
id list.
|
|
124
|
+
|
|
125
|
+
```js
|
|
126
|
+
const encipher = Encipher.create()
|
|
127
|
+
|
|
128
|
+
async function fulfillPasswordHash ({ id, customer_id, raw_password, saved_at }) {
|
|
129
|
+
const password_hash = await encipher.hash(raw_password)
|
|
130
|
+
|
|
131
|
+
return { id, customer_id, saved_at, password_hash }
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
module.exports = {
|
|
135
|
+
up: async (queryInterface, Sequelize) => {
|
|
136
|
+
const fulfilled = await Promise.all(
|
|
137
|
+
customerPasswordHashesSeeds.map(it => fulfillPasswordHash(it))
|
|
138
|
+
)
|
|
139
|
+
|
|
140
|
+
await queryInterface.bulkInsert(TABLE_NAME.CUSTOMER_PASSWORD_HASHES, TimestampSeedsSupplier.supplyAll(fulfilled), {})
|
|
141
|
+
},
|
|
142
|
+
|
|
143
|
+
down: async (queryInterface, Sequelize) => {
|
|
144
|
+
await queryInterface.bulkDelete(TABLE_NAME.CUSTOMER_PASSWORD_HASHES, { id: customerPasswordHashesSeeds.map(it => it.id) })
|
|
145
|
+
},
|
|
146
|
+
}
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
- **Why**: seeds are data-only; when a column's value is derived, compute it at seed time rather than
|
|
150
|
+
storing a fake constant. The raw source (`raw_password`) stays in the plain array so `down` can
|
|
151
|
+
still map ids.
|
|
152
|
+
|
|
153
|
+
## Timestamps come from TimestampSeedsSupplier
|
|
154
|
+
|
|
155
|
+
Do **not** write `created_at` / `updated_at` in seed rows. `TimestampSeedsSupplier.supplyAll(seeds)`
|
|
156
|
+
adds both to every row.
|
|
157
|
+
|
|
158
|
+
```js
|
|
159
|
+
// TimestampSeedsSupplier.supplyOne returns:
|
|
160
|
+
{ created_at: now, updated_at: now, ...seed }
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
- Because `...seed` is spread **after** the timestamps, a row **can override** them by including its
|
|
164
|
+
own `created_at` / `updated_at` — but normally you don't. `deleted_at` is left unset (defaults to
|
|
165
|
+
null).
|
|
166
|
+
- **Business datetimes are different.** A column that means something in the domain
|
|
167
|
+
(`registered_at`, `saved_at`, `effective_at`, `generated_at`, `expired_at`) **is** written
|
|
168
|
+
explicitly in the seed — it is not an audit column. This matches the model / migration split where
|
|
169
|
+
audit timestamps come from the framework and business datetimes are their own columns
|
|
170
|
+
(`hor-sequelize-migration`).
|
|
@@ -0,0 +1,369 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: hor-sequelize-subquery
|
|
3
|
+
description: >
|
|
4
|
+
Define and consume named Sequelize subqueries on renchan models (this.addSubquery inside
|
|
5
|
+
defineSubqueries in sequelize/models/*.js, consumed via Model.subquery(name, params) under Op.in
|
|
6
|
+
in a where clause). Use this skill whenever a query must filter rows by a condition on a related
|
|
7
|
+
/ associated table — in this convention that is a subquery, not a JOIN — or whenever the user
|
|
8
|
+
asks to define, name, or test a subquery.
|
|
9
|
+
---
|
|
10
|
+
|
|
11
|
+
# Sequelize Subquery
|
|
12
|
+
|
|
13
|
+
A skill for defining **named, reusable `SELECT`s** attached to a Sequelize model, and consuming
|
|
14
|
+
them from a resolver's `where` clause. A subquery is registered once on the model with
|
|
15
|
+
`this.addSubquery({ name, generator })` and consumed with `Model.subquery(name, params)`, which
|
|
16
|
+
returns a `Sequelize.Literal` (a real SQL subquery) — so you filter one model by the result of a
|
|
17
|
+
`SELECT` against another without writing a join or a raw SQL string at the call site.
|
|
18
|
+
|
|
19
|
+
Registration lives in `static defineSubqueries()`, one of the six extension points of the model
|
|
20
|
+
skeleton defined in `hor-sequelize-model` (an unused `defineSubqueries` stays as a noop there; this
|
|
21
|
+
skill covers what to write when it *is* used). The consuming side (building a resolver's
|
|
22
|
+
`where` clause) belongs to `hor-query-resolver` / `hor-mutation-resolver`; testing follows `hoc-jest`.
|
|
23
|
+
|
|
24
|
+
## Grand principle: the name is the query — a self-describing string registered on the model
|
|
25
|
+
|
|
26
|
+
The subquery API comes from `RenchanModel` in `@openreachtech/renchan-sequelize` — **never roll
|
|
27
|
+
your own** (no raw `Sequelize.literal('SELECT ...')` at a call site, no ad-hoc join to express a
|
|
28
|
+
cross-table filter). Register the `SELECT` on the model that owns it, and give it a name that
|
|
29
|
+
**encodes exactly its `where` conditions and its `select` columns**:
|
|
30
|
+
|
|
31
|
+
```
|
|
32
|
+
?<cond1>[<op1>]?<cond2>[<op2>].<result1>.<result2>
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
- **Condition columns (the `where`)** each start with `?`. The operator goes in square brackets
|
|
36
|
+
`[...]` immediately after the column name; a plain equality has **no** brackets.
|
|
37
|
+
- **Result columns (the `select`)** each start with `.`.
|
|
38
|
+
- **The table name never appears in the name.** The subquery is added to a target model, so the
|
|
39
|
+
table is self-evident from the model — the name encodes only conditions and results.
|
|
40
|
+
- **Why a self-describing name**: the consumer reads `'?CustomerId.id'` and knows, without
|
|
41
|
+
opening the model, that it means "`WHERE customer_id = ?`, `SELECT id`". A free-form name
|
|
42
|
+
(`'ordersOfCustomer'`) forces every reader back to the definition, and drifts silently when
|
|
43
|
+
the definition changes.
|
|
44
|
+
- **Why registered on the model, not inlined at the call site**: the same cross-table filter is
|
|
45
|
+
typically needed by several resolvers. A raw literal written inline duplicates SQL strings,
|
|
46
|
+
escapes nothing into the model layer, and cannot be unit-tested as a pure generator.
|
|
47
|
+
|
|
48
|
+
```js
|
|
49
|
+
// Good: consume a named subquery registered on the model
|
|
50
|
+
const customerOrderSubquery = CustomerOrder.subquery('?CustomerId.id', {
|
|
51
|
+
customerId,
|
|
52
|
+
})
|
|
53
|
+
|
|
54
|
+
const whereClause = {
|
|
55
|
+
CustomerOrderId: {
|
|
56
|
+
[Op.in]: customerOrderSubquery,
|
|
57
|
+
},
|
|
58
|
+
}
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
```js
|
|
62
|
+
// Avoid: raw SQL literal at the call site
|
|
63
|
+
// → duplicates the SELECT wherever it is needed, leaks physical column names
|
|
64
|
+
// into the resolver, and cannot be tested as a pure generator.
|
|
65
|
+
const whereClause = {
|
|
66
|
+
CustomerOrderId: {
|
|
67
|
+
[Op.in]: Sequelize.literal(
|
|
68
|
+
`(SELECT id FROM customer_orders WHERE customer_id = ${customerId})`
|
|
69
|
+
),
|
|
70
|
+
},
|
|
71
|
+
}
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
## Comment language in the sample code
|
|
75
|
+
|
|
76
|
+
Comments in the `js` examples in this SKILL.md follow the prose language (here, **English**),
|
|
77
|
+
because the examples are *explanation* of the skill. Comments inside the **artifact this skill
|
|
78
|
+
produces** (the real model / resolver code) follow the **codebase** language — English — to match
|
|
79
|
+
the surrounding source.
|
|
80
|
+
|
|
81
|
+
## 1. When to reach for a subquery
|
|
82
|
+
|
|
83
|
+
Reach for a subquery when a resolver needs to constrain rows of model A by a condition that
|
|
84
|
+
lives on model B. Typical cases:
|
|
85
|
+
|
|
86
|
+
| Requirement | Shape |
|
|
87
|
+
| --- | --- |
|
|
88
|
+
| "Payments of the orders that belong to this customer" | Filter `OrderPayment` by `CustomerOrderId IN (SELECT id FROM customer_orders WHERE customer_id = ?)` |
|
|
89
|
+
| "Shipments whose **latest status** is in a given set" | Filter `Shipment` by `id IN (SELECT shipment_id FROM shipment_latest_statuses WHERE shipment_status_id IN (...))` |
|
|
90
|
+
| "Coupons valid **at** a point in time" | Filter by `id IN (SELECT coupon_id FROM coupon_settings WHERE valid_from <= ? AND valid_until >= ?)` |
|
|
91
|
+
|
|
92
|
+
- The subquery is defined **on the model that owns the condition columns** (the model whose
|
|
93
|
+
table the inner `SELECT` reads), and its result columns are what the outer `where` matches
|
|
94
|
+
against (usually an id / FK column).
|
|
95
|
+
- If the requirement is "load B *together with* A for output", that is an `include` association
|
|
96
|
+
(`hor-sequelize-model` → associations), not a subquery. A subquery is for **filtering**, not
|
|
97
|
+
eager-loading.
|
|
98
|
+
|
|
99
|
+
## 2. Naming convention and operator table
|
|
100
|
+
|
|
101
|
+
The operator inside `[...]` maps to a Sequelize `Op` symbol in the generator's `where`:
|
|
102
|
+
|
|
103
|
+
| format | meaning |
|
|
104
|
+
| :-- | :-- |
|
|
105
|
+
| (none) | set as value directly (plain equality) |
|
|
106
|
+
| `[=]` | `Op.eq` |
|
|
107
|
+
| `[>]` | `Op.gt` |
|
|
108
|
+
| `[>=]` | `Op.gte` |
|
|
109
|
+
| `[<]` | `Op.lt` |
|
|
110
|
+
| `[<=]` | `Op.lte` |
|
|
111
|
+
| `[in]` | `Op.in` |
|
|
112
|
+
| `[between]` | `Op.between` |
|
|
113
|
+
| `[like]` | `Op.like` |
|
|
114
|
+
|
|
115
|
+
- **"Set as value directly"**: when a condition is a plain equality, omit the brackets and
|
|
116
|
+
assign the value straight onto the field — `?CustomerId.id` with a `where` of
|
|
117
|
+
`{ [CustomerIdField]: customerId }`.
|
|
118
|
+
- Reading examples:
|
|
119
|
+
- `?CustomerId.id` — "`WHERE CustomerId = ?`, `SELECT id`"
|
|
120
|
+
- `?ShipmentStatusId[in].ShipmentId` — "`WHERE ShipmentStatusId IN (...)`, `SELECT ShipmentId`"
|
|
121
|
+
- `?validFrom[<=].validUntil[>=].CouponId` — two conditions, each with its own operator
|
|
122
|
+
- `?OriginObjectCategoryId?jobDivisionNumber.id` — two equality conditions (no brackets), one result
|
|
123
|
+
|
|
124
|
+
## 3. Register the subquery in `defineSubqueries()`
|
|
125
|
+
|
|
126
|
+
Register inside `static defineSubqueries()`. Call `super.defineSubqueries?.()` first (as with
|
|
127
|
+
every extension point in `hor-sequelize-model` — otherwise a Mixin's base behavior is lost),
|
|
128
|
+
read the physical field names off `this.getAttributes()`, then add each subquery with
|
|
129
|
+
`this.addSubquery({ name, generator })`.
|
|
130
|
+
|
|
131
|
+
The `generator` receives a params object and returns `{ attributes, where }`:
|
|
132
|
+
|
|
133
|
+
- `attributes` — the `SELECT` column list, as **DB field names** (take them from
|
|
134
|
+
`this.getAttributes().<key>.field`, never hard-code snake_case strings).
|
|
135
|
+
- `where` — a Sequelize where clause, keyed by DB field names, using the `Op` symbols that the
|
|
136
|
+
subquery name declares.
|
|
137
|
+
|
|
138
|
+
**Basic example** (single equality condition, single result):
|
|
139
|
+
|
|
140
|
+
```js
|
|
141
|
+
/**
|
|
142
|
+
* Define model subqueries
|
|
143
|
+
*
|
|
144
|
+
* @override
|
|
145
|
+
*/
|
|
146
|
+
static defineSubqueries () {
|
|
147
|
+
super.defineSubqueries?.()
|
|
148
|
+
|
|
149
|
+
const allAttributes = this.getAttributes()
|
|
150
|
+
const idField = allAttributes.id.field
|
|
151
|
+
const CustomerIdField = allAttributes.CustomerId.field
|
|
152
|
+
|
|
153
|
+
this.addSubquery({
|
|
154
|
+
name: '?CustomerId.id',
|
|
155
|
+
generator: ({
|
|
156
|
+
customerId,
|
|
157
|
+
}) => {
|
|
158
|
+
const attributes = [
|
|
159
|
+
idField,
|
|
160
|
+
]
|
|
161
|
+
|
|
162
|
+
const whereClause = {
|
|
163
|
+
[CustomerIdField]: customerId,
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
return {
|
|
167
|
+
attributes,
|
|
168
|
+
where: whereClause,
|
|
169
|
+
}
|
|
170
|
+
},
|
|
171
|
+
})
|
|
172
|
+
}
|
|
173
|
+
```
|
|
174
|
+
|
|
175
|
+
**Advanced example** (two conditions, each with its own operator, single result):
|
|
176
|
+
|
|
177
|
+
```js
|
|
178
|
+
this.addSubquery({
|
|
179
|
+
name: '?validFrom[<=].validUntil[>=].CouponId',
|
|
180
|
+
generator: ({
|
|
181
|
+
now,
|
|
182
|
+
}) => {
|
|
183
|
+
const attributes = [
|
|
184
|
+
CouponIdField,
|
|
185
|
+
]
|
|
186
|
+
|
|
187
|
+
const whereClause = {
|
|
188
|
+
[validFromField]: {
|
|
189
|
+
[SequelizeOp.lte]: now,
|
|
190
|
+
},
|
|
191
|
+
[validUntilField]: {
|
|
192
|
+
[SequelizeOp.gte]: now,
|
|
193
|
+
},
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
return {
|
|
197
|
+
attributes,
|
|
198
|
+
where: whereClause,
|
|
199
|
+
}
|
|
200
|
+
},
|
|
201
|
+
})
|
|
202
|
+
```
|
|
203
|
+
|
|
204
|
+
- The `Op` symbols come from the top of the model file:
|
|
205
|
+
|
|
206
|
+
```js
|
|
207
|
+
import {
|
|
208
|
+
Op as SequelizeOp,
|
|
209
|
+
} from 'sequelize'
|
|
210
|
+
```
|
|
211
|
+
|
|
212
|
+
- **The name and the generator must agree.** Every `?` column in the name appears in `where`
|
|
213
|
+
with exactly the operator its brackets declare; every `.` column appears in `attributes`.
|
|
214
|
+
A name that says `[in]` while the generator assigns a direct value is a bug — the name is
|
|
215
|
+
the contract.
|
|
216
|
+
|
|
217
|
+
## 4. Consume via `Model.subquery(name, params)` under `Op.in`
|
|
218
|
+
|
|
219
|
+
Call `Model.subquery(name, params)` — it returns a `Sequelize.Literal` — and drop it into a
|
|
220
|
+
`where` clause, almost always as the right-hand side of an `Op.in`. Typical resolver usage
|
|
221
|
+
(inside a `buildXxxWhereClause()` method, per `hor-query-resolver`):
|
|
222
|
+
|
|
223
|
+
```js
|
|
224
|
+
import {
|
|
225
|
+
Op,
|
|
226
|
+
} from 'sequelize'
|
|
227
|
+
|
|
228
|
+
// ...
|
|
229
|
+
|
|
230
|
+
/**
|
|
231
|
+
* Build where clause for shipments.
|
|
232
|
+
*
|
|
233
|
+
* @param {{
|
|
234
|
+
* tenantBrandId: number
|
|
235
|
+
* }} params - Parameters.
|
|
236
|
+
* @returns {object} Where clause.
|
|
237
|
+
*/
|
|
238
|
+
buildShipmentWhereClause ({
|
|
239
|
+
tenantBrandId,
|
|
240
|
+
}) {
|
|
241
|
+
const shipmentLatestStatusSubquery = ShipmentLatestStatus.subquery(
|
|
242
|
+
'?ShipmentStatusId[in].ShipmentId',
|
|
243
|
+
{
|
|
244
|
+
shipmentStatusIds: [SHIPMENT_STATUS.READY_TO_SHIP.ID],
|
|
245
|
+
}
|
|
246
|
+
)
|
|
247
|
+
|
|
248
|
+
const whereClause = {
|
|
249
|
+
TenantBrandId: tenantBrandId,
|
|
250
|
+
id: {
|
|
251
|
+
[Op.in]: shipmentLatestStatusSubquery,
|
|
252
|
+
},
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
return whereClause
|
|
256
|
+
}
|
|
257
|
+
```
|
|
258
|
+
|
|
259
|
+
Combining two subqueries on the same column uses `Op.and`:
|
|
260
|
+
|
|
261
|
+
```js
|
|
262
|
+
return {
|
|
263
|
+
...whereClause,
|
|
264
|
+
id: {
|
|
265
|
+
[Op.and]: [
|
|
266
|
+
{
|
|
267
|
+
[Op.in]: shipmentLatestStatusSubquery,
|
|
268
|
+
},
|
|
269
|
+
{
|
|
270
|
+
[Op.in]: desiredDeliveryDateSubquery,
|
|
271
|
+
},
|
|
272
|
+
],
|
|
273
|
+
},
|
|
274
|
+
}
|
|
275
|
+
```
|
|
276
|
+
|
|
277
|
+
- Calling `Model.subquery(name, ...)` with a name that was never registered throws
|
|
278
|
+
`invalid subquery ${name} called.` — the name string at the call site must match the
|
|
279
|
+
registered name character for character.
|
|
280
|
+
|
|
281
|
+
## 5. Testing
|
|
282
|
+
|
|
283
|
+
Test the **generator**, not the SQL string. Pull it with
|
|
284
|
+
`Model.getSubqueryOptionsGenerator(name)`, invoke it with `params`, and assert the returned
|
|
285
|
+
`{ attributes, where }` with a single `toEqual`. The generator is pure (no DB access), so the
|
|
286
|
+
test needs no seeder and lives under `tests/__tests__/sequelize/models/` (per `hoc-jest` —
|
|
287
|
+
DB-writing tests are the ones that go elsewhere).
|
|
288
|
+
|
|
289
|
+
Structure: `describe('<Model>')` → `describe('.subquery')` → `describe('<subquery name>')` →
|
|
290
|
+
`test.each`. Import `Op` from `sequelize` so the expected `where` carries the real operator
|
|
291
|
+
symbols.
|
|
292
|
+
|
|
293
|
+
```js
|
|
294
|
+
import {
|
|
295
|
+
Op,
|
|
296
|
+
} from 'sequelize'
|
|
297
|
+
|
|
298
|
+
import ShipmentLatestStatus from '../../../../sequelize/models/ShipmentLatestStatus.js'
|
|
299
|
+
|
|
300
|
+
describe('ShipmentLatestStatus', () => {
|
|
301
|
+
describe('.subquery', () => {
|
|
302
|
+
describe('?ShipmentStatusId[in].ShipmentId', () => {
|
|
303
|
+
const cases = [
|
|
304
|
+
{
|
|
305
|
+
params: {
|
|
306
|
+
shipmentStatusIds: [10001, 10002],
|
|
307
|
+
},
|
|
308
|
+
expected: {
|
|
309
|
+
attributes: ['shipment_id'],
|
|
310
|
+
where: {
|
|
311
|
+
shipment_status_id: {
|
|
312
|
+
[Op.in]: [10001, 10002],
|
|
313
|
+
},
|
|
314
|
+
},
|
|
315
|
+
},
|
|
316
|
+
},
|
|
317
|
+
{
|
|
318
|
+
params: {
|
|
319
|
+
shipmentStatusIds: [10003],
|
|
320
|
+
},
|
|
321
|
+
expected: {
|
|
322
|
+
attributes: ['shipment_id'],
|
|
323
|
+
where: {
|
|
324
|
+
shipment_status_id: {
|
|
325
|
+
[Op.in]: [10003],
|
|
326
|
+
},
|
|
327
|
+
},
|
|
328
|
+
},
|
|
329
|
+
},
|
|
330
|
+
]
|
|
331
|
+
|
|
332
|
+
test.each(cases)('shipmentStatusIds[0]: $params.shipmentStatusIds.0', ({
|
|
333
|
+
params,
|
|
334
|
+
expected,
|
|
335
|
+
}) => {
|
|
336
|
+
const generator = ShipmentLatestStatus.getSubqueryOptionsGenerator('?ShipmentStatusId[in].ShipmentId')
|
|
337
|
+
|
|
338
|
+
const actual = generator(params)
|
|
339
|
+
|
|
340
|
+
expect(actual)
|
|
341
|
+
.toEqual(expected)
|
|
342
|
+
})
|
|
343
|
+
})
|
|
344
|
+
})
|
|
345
|
+
})
|
|
346
|
+
```
|
|
347
|
+
|
|
348
|
+
Conventions to keep (details in `hoc-jest`):
|
|
349
|
+
|
|
350
|
+
- **`test.each`, prefer ≥ 2 cases** (vary the params); a trivial single-condition subquery may
|
|
351
|
+
use one case. Vary exactly one `params` field in the title and keep it unique per case — dot
|
|
352
|
+
into an array element as needed (`$params.shipmentStatusIds.0`).
|
|
353
|
+
- **Cases are `{ params, expected }`** — no other keys unless justified.
|
|
354
|
+
- **One `toEqual`** for the whole returned object; build the full `expected` (including `Op`
|
|
355
|
+
symbols) and compare once.
|
|
356
|
+
- **AAA, no logic in the body** — arrange the generator, act by calling it, assert.
|
|
357
|
+
- The expected `attributes` / `where` keys are the **DB field names** (snake_case) — that is
|
|
358
|
+
what the generator returns, and it verifies the attribute-to-field mapping too.
|
|
359
|
+
- Test a subquery against real seeded data only when exercising it end-to-end through a
|
|
360
|
+
resolver that reads the DB — that is a resolver test, placed per `hoc-jest`'s directory rules.
|
|
361
|
+
|
|
362
|
+
## Finishing checklist
|
|
363
|
+
|
|
364
|
+
- [ ] Does the subquery live in `static defineSubqueries()` on the model that owns the condition columns, added via `this.addSubquery({ name, generator })` (no raw `Sequelize.literal` at call sites)?
|
|
365
|
+
- [ ] Does `defineSubqueries()` call `super.defineSubqueries?.()` first, and take field names from `this.getAttributes()` (no hard-coded snake_case strings)?
|
|
366
|
+
- [ ] Does the name follow `?<condition>[<operator>].<result>` — every `?` column in `where` with exactly the declared operator, every `.` column in `attributes`, no brackets for plain equality, and **no table name**?
|
|
367
|
+
- [ ] Does the generator return `{ attributes, where }` keyed by **DB field names**?
|
|
368
|
+
- [ ] Does the consumer call `Model.subquery(name, params)` under `Op.in` (with `Op.and` when combining two subqueries on the same column), with the name matching the registration exactly?
|
|
369
|
+
- [ ] Is the generator tested via `Model.getSubqueryOptionsGenerator(name)` with `test.each` `{ params, expected }` cases and a single `toEqual` (including the real `Op` symbols), placed under `tests/__tests__/sequelize/models/`?
|