@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,218 @@
|
|
|
1
|
+
# Associations (how to write associate())
|
|
2
|
+
|
|
3
|
+
Conventions for declaring relations between models in `associate()`. Referenced from `SKILL.md`.
|
|
4
|
+
For method ordering and the `super`-call template see
|
|
5
|
+
[default-methods.md](./default-methods.md#even-unused-extension-points-call-super-and-keep-a-noop);
|
|
6
|
+
for FK column notation see
|
|
7
|
+
[notation.md](./notation.md#fk-like-columns-start-with-an-uppercase-letter-and-carry-a-comment).
|
|
8
|
+
|
|
9
|
+
## Reference the associated model via `this._.<ModelName>`
|
|
10
|
+
|
|
11
|
+
Always reference the associated model via **`this._.<ModelName>`**. `this._` is the
|
|
12
|
+
`sequelize.models` (the hash of registered models) returned by the base `RenchanModel`.
|
|
13
|
+
|
|
14
|
+
- **Why**: models cross-reference each other across files, so a direct import causes a circular
|
|
15
|
+
dependency. `this._` is resolved after all models are registered, so it pulls the associated
|
|
16
|
+
model while avoiding the cycle. The reference name matches the class name = filename
|
|
17
|
+
([notation.md](./notation.md#one-model-per-file-filename-matches-class-name)), so you spell it
|
|
18
|
+
`this._.Customer`.
|
|
19
|
+
- Remember to put `super.associate?.()` first (so a Mixin's relations are not swallowed;
|
|
20
|
+
[default-methods.md](./default-methods.md#even-unused-extension-points-call-super-and-keep-a-noop)).
|
|
21
|
+
|
|
22
|
+
```js
|
|
23
|
+
// Good example
|
|
24
|
+
/**
|
|
25
|
+
* Define model associations
|
|
26
|
+
*/
|
|
27
|
+
static associate () {
|
|
28
|
+
super.associate?.()
|
|
29
|
+
|
|
30
|
+
this.belongsTo(this._.Customer)
|
|
31
|
+
}
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
```js
|
|
35
|
+
// Bad example (directly importing the associated model, inviting a circular dependency)
|
|
36
|
+
import Customer from './Customer.js'
|
|
37
|
+
// ...
|
|
38
|
+
this.belongsTo(Customer)
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
## Choose the association type to match the relationship
|
|
42
|
+
|
|
43
|
+
Pick among Sequelize's four methods by which side holds the FK column in the DB and by
|
|
44
|
+
multiplicity. The FK column is defined on the **side that holds the FK**, following the
|
|
45
|
+
[notation.md](./notation.md#fk-like-columns-start-with-an-uppercase-letter-and-carry-a-comment)
|
|
46
|
+
convention (uppercase start).
|
|
47
|
+
|
|
48
|
+
| Method | Meaning | Side holding the FK |
|
|
49
|
+
| --- | --- | --- |
|
|
50
|
+
| `this.belongsTo(this._.X)` | I reference one X (I hold the FK) | **me** |
|
|
51
|
+
| `this.hasOne(this._.X)` | X references me once (1:1) | the other (X) |
|
|
52
|
+
| `this.hasMany(this._.X)` | X references me many times (1:N) | the other (X) |
|
|
53
|
+
| `this.belongsToMany(this._.X, { through: this._.Y })` | many-to-many with X (through join table Y) | the join table (Y) |
|
|
54
|
+
|
|
55
|
+
- `belongsTo` and `hasOne` / `hasMany` are **two sides of the same coin**. Write `belongsTo` on
|
|
56
|
+
the child side that holds the FK, and `hasOne` / `hasMany` on the parent side (you may declare
|
|
57
|
+
both sides).
|
|
58
|
+
- `belongsToMany` must be given the join model in `through`. The join model itself is an ordinary
|
|
59
|
+
model with a `belongsTo` to each side ([the join-table example](#a-join-table-has-a-belongsto-to-each-side)).
|
|
60
|
+
|
|
61
|
+
```js
|
|
62
|
+
// Good example (parent CustomerOrder side — children hold the FK, so hasOne / hasMany / belongsToMany)
|
|
63
|
+
/**
|
|
64
|
+
* Define model associations
|
|
65
|
+
*/
|
|
66
|
+
static associate () {
|
|
67
|
+
super.associate?.()
|
|
68
|
+
|
|
69
|
+
this.hasOne(this._.CustomerOrderTotalPrice)
|
|
70
|
+
this.hasOne(this._.CustomerOrderLatestStatus)
|
|
71
|
+
|
|
72
|
+
this.hasMany(this._.CustomerOrderProduct)
|
|
73
|
+
|
|
74
|
+
this.belongsToMany(this._.BrandShipment, {
|
|
75
|
+
through: this._.BrandShipmentOrder,
|
|
76
|
+
})
|
|
77
|
+
}
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
```js
|
|
81
|
+
// Good example (child CustomerOrder side — it holds CustomerId, so belongsTo)
|
|
82
|
+
/**
|
|
83
|
+
* Define model associations
|
|
84
|
+
*/
|
|
85
|
+
static associate () {
|
|
86
|
+
super.associate?.()
|
|
87
|
+
|
|
88
|
+
this.belongsTo(this._.Customer)
|
|
89
|
+
}
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
## Pass association options only when the default inference is wrong
|
|
93
|
+
|
|
94
|
+
Sequelize resolves the FK by the convention "`<associated model name>` + `Id`"
|
|
95
|
+
([notation.md](./notation.md#fk-like-columns-start-with-an-uppercase-letter-and-carry-a-comment)).
|
|
96
|
+
When the FK column follows that convention, call the association method with the model alone.
|
|
97
|
+
Pass an options object only when the default inference is wrong — `through` for `belongsToMany`
|
|
98
|
+
(previous section), and `foreignKey` when the FK attribute departs from the convention.
|
|
99
|
+
|
|
100
|
+
- **Why**: restating an option the inference already gets right is noise that hides the real
|
|
101
|
+
signal ("this association is irregular"). Conversely, when a model holds a **role-named FK**
|
|
102
|
+
(or two FKs pointing at the same model), the inferred `<ModelName>Id` does not exist, the
|
|
103
|
+
relation silently binds the wrong column, and `include` fails — so name the FK explicitly.
|
|
104
|
+
|
|
105
|
+
```js
|
|
106
|
+
// Good example (the FK follows the convention; no options)
|
|
107
|
+
this.belongsTo(this._.Customer)
|
|
108
|
+
|
|
109
|
+
// Good example (the child's FK is role-named, so inference fails; name it explicitly)
|
|
110
|
+
this.hasMany(this._.TabGroupDisplayTab, {
|
|
111
|
+
foreignKey: 'DisplayTabOriginCategoryId',
|
|
112
|
+
})
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
```js
|
|
116
|
+
// Bad example (restating the option the inference already resolves)
|
|
117
|
+
this.belongsTo(this._.Customer, {
|
|
118
|
+
foreignKey: 'CustomerId',
|
|
119
|
+
})
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
## A declared association is read off the instance by the model name
|
|
123
|
+
|
|
124
|
+
An association declared in `associate()` determines the property name under which `include`
|
|
125
|
+
attaches the loaded rows: `belongsTo` / `hasOne` attach under the **singular model name**,
|
|
126
|
+
`hasMany` / `belongsToMany` under the **pluralized model name**.
|
|
127
|
+
|
|
128
|
+
- **Why**: the property comes from the association declaration, not from the FK column, so
|
|
129
|
+
knowing this mapping tells you what an `include` produces without inspecting the result. It is
|
|
130
|
+
also why the reference name must match the class name
|
|
131
|
+
([notation.md](./notation.md#one-model-per-file-filename-matches-class-name)).
|
|
132
|
+
|
|
133
|
+
```js
|
|
134
|
+
// The declaration determines the property name on a loaded instance
|
|
135
|
+
const order = await CustomerOrder.findOne({
|
|
136
|
+
include: [
|
|
137
|
+
CustomerOrderTotalPrice,
|
|
138
|
+
CustomerOrderProduct,
|
|
139
|
+
],
|
|
140
|
+
})
|
|
141
|
+
|
|
142
|
+
order.CustomerOrderTotalPrice // hasOne / belongsTo → singular model name
|
|
143
|
+
order.CustomerOrderProducts // hasMany / belongsToMany → pluralized model name
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
## A join table has a belongsTo to each side
|
|
147
|
+
|
|
148
|
+
A many-to-many join-table model writes a **`belongsTo` to each end**. It holds the FK columns
|
|
149
|
+
(each end's `<Model>Id`) as attributes, and adds `paranoid`
|
|
150
|
+
([default-methods.md](./default-methods.md#when-to-add-paranoid)) when it soft-deletes.
|
|
151
|
+
|
|
152
|
+
- **Why**: `belongsToMany(..., { through })` goes through the join table, but the join table
|
|
153
|
+
itself must know its relations to both ends. Linking the join table as an ordinary model with a
|
|
154
|
+
`belongsTo` to each end lets you find the join table directly and include both ends.
|
|
155
|
+
|
|
156
|
+
```js
|
|
157
|
+
// Good example (BrandShipmentOrder — the join table between CustomerOrder and BrandShipment)
|
|
158
|
+
export default class BrandShipmentOrder extends BaseAppRenchanModel {
|
|
159
|
+
/**
|
|
160
|
+
* Define model attributes
|
|
161
|
+
*
|
|
162
|
+
* @param {import('sequelize').DataTypes} DataTypes - Sequelize DataTypes
|
|
163
|
+
* @returns {object} Model attributes
|
|
164
|
+
*/
|
|
165
|
+
static createAttributes (DataTypes) {
|
|
166
|
+
const factory = ModelAttributeFactory.create(DataTypes)
|
|
167
|
+
|
|
168
|
+
return {
|
|
169
|
+
...factory.ID_BIGINT,
|
|
170
|
+
|
|
171
|
+
CustomerOrderId: {
|
|
172
|
+
type: DataTypes.BIGINT,
|
|
173
|
+
allowNull: false,
|
|
174
|
+
},
|
|
175
|
+
BrandShipmentId: {
|
|
176
|
+
type: DataTypes.INTEGER,
|
|
177
|
+
allowNull: false,
|
|
178
|
+
},
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/**
|
|
183
|
+
* Define model options
|
|
184
|
+
*
|
|
185
|
+
* @param {import('sequelize').Sequelize} sequelizeClient - Sequelize instance
|
|
186
|
+
* @returns {object} Model options
|
|
187
|
+
*/
|
|
188
|
+
static createOptions (sequelizeClient) {
|
|
189
|
+
return {
|
|
190
|
+
...super.createOptions(sequelizeClient),
|
|
191
|
+
|
|
192
|
+
paranoid: true, // for deleted_at column
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
/**
|
|
197
|
+
* Define model associations
|
|
198
|
+
*/
|
|
199
|
+
static associate () {
|
|
200
|
+
super.associate?.()
|
|
201
|
+
|
|
202
|
+
this.belongsTo(this._.CustomerOrder)
|
|
203
|
+
this.belongsTo(this._.BrandShipment)
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
// defineScopes / defineSubqueries / setupHooks are noop
|
|
207
|
+
}
|
|
208
|
+
```
|
|
209
|
+
|
|
210
|
+
## Do not add DB foreign-key constraints (enforce in the app layer)
|
|
211
|
+
|
|
212
|
+
Express relations with Sequelize associations **only**. Do not add DB foreign-key constraints
|
|
213
|
+
(`references` / `onDelete` / `onUpdate`)
|
|
214
|
+
(`hor-sequelize-migration`).
|
|
215
|
+
|
|
216
|
+
- **Why**: referential integrity is enforced in the app layer (transactions, existence checks).
|
|
217
|
+
Not adding constraints keeps migration ordering, record deletion, and sqlite tests simple. A
|
|
218
|
+
model's `belongsTo` etc. are **declarations for JOIN and include**, independent of DB constraints.
|
|
@@ -0,0 +1,244 @@
|
|
|
1
|
+
# Default Methods (the six override methods and createOptions)
|
|
2
|
+
|
|
3
|
+
The template for the static methods that form a model's skeleton. Rooted in the
|
|
4
|
+
[grand principle](../SKILL.md#grand-principle-a-model-is-a-filled-in-template-not-free-form-code)
|
|
5
|
+
in `SKILL.md` (keep the extension points, leave a noop rather than deleting). For the contents of
|
|
6
|
+
the attributes see [notation.md](./notation.md); for the details of `associate()` see
|
|
7
|
+
[associations.md](./associations.md).
|
|
8
|
+
|
|
9
|
+
## Lay out the six methods in a fixed order, each with JSDoc
|
|
10
|
+
|
|
11
|
+
Every model defines the following six methods **in this order**, with a **JSDoc block** stating
|
|
12
|
+
the purpose, arguments, and return value immediately above each. Do not delete unused methods;
|
|
13
|
+
keep them with the template (below).
|
|
14
|
+
|
|
15
|
+
1. `createAttributes (DataTypes)`
|
|
16
|
+
2. `createOptions (sequelizeClient)`
|
|
17
|
+
3. `associate ()`
|
|
18
|
+
4. `defineScopes (Op)`
|
|
19
|
+
5. `defineSubqueries ()`
|
|
20
|
+
6. `setupHooks ()`
|
|
21
|
+
|
|
22
|
+
- **Why**: fixing the order across every model puts the same responsibility in the same position
|
|
23
|
+
in every file. The reader never has to hunt for "where does this model wire relations, where
|
|
24
|
+
does it add a scope". A JSDoc that records the purpose, `@param`, and `@returns` on each method
|
|
25
|
+
makes each extension point's responsibility readable even in a declaration-only layer, and the
|
|
26
|
+
`@param` type annotation prevents mixing up `DataTypes` / `Op` / `sequelizeClient`.
|
|
27
|
+
- `createAttributes` is **abstract** — the base `throw`s. Always implement it (if unimplemented,
|
|
28
|
+
it fails in `initWithSequelizeClient`). The other five have a default implementation on the
|
|
29
|
+
base; overriding them is for "adding this model's own declarations".
|
|
30
|
+
|
|
31
|
+
```js
|
|
32
|
+
// Good example (the skeleton; unused extension points are kept as noop)
|
|
33
|
+
export default class CustomerOrder extends BaseAppRenchanModel {
|
|
34
|
+
/**
|
|
35
|
+
* Define model attributes
|
|
36
|
+
*
|
|
37
|
+
* @param {import('sequelize').DataTypes} DataTypes - Sequelize DataTypes
|
|
38
|
+
* @returns {object} Model attributes
|
|
39
|
+
*/
|
|
40
|
+
static createAttributes (DataTypes) {
|
|
41
|
+
const factory = ModelAttributeFactory.create(DataTypes)
|
|
42
|
+
|
|
43
|
+
return {
|
|
44
|
+
...factory.ID_BIGINT,
|
|
45
|
+
// ... attributes (notation.md)
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Define model options
|
|
51
|
+
*
|
|
52
|
+
* @param {import('sequelize').Sequelize} sequelizeClient - Sequelize instance
|
|
53
|
+
* @returns {object} Model options
|
|
54
|
+
*/
|
|
55
|
+
static createOptions (sequelizeClient) {
|
|
56
|
+
return {
|
|
57
|
+
...super.createOptions(sequelizeClient),
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Define model associations
|
|
63
|
+
*/
|
|
64
|
+
static associate () {
|
|
65
|
+
super.associate?.()
|
|
66
|
+
|
|
67
|
+
this.belongsTo(this._.Customer)
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Define model scopes
|
|
72
|
+
*
|
|
73
|
+
* @param {import('sequelize').Op} Op - Sequelize operators
|
|
74
|
+
*/
|
|
75
|
+
static defineScopes (Op) {
|
|
76
|
+
super.defineScopes?.(Op)
|
|
77
|
+
|
|
78
|
+
// noop
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Define subqueries
|
|
83
|
+
*/
|
|
84
|
+
static defineSubqueries () {
|
|
85
|
+
super.defineSubqueries?.()
|
|
86
|
+
|
|
87
|
+
// noop
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Setup model hooks
|
|
92
|
+
*/
|
|
93
|
+
static setupHooks () {
|
|
94
|
+
super.setupHooks?.()
|
|
95
|
+
|
|
96
|
+
// noop
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
## Even unused extension points call super and keep a noop
|
|
102
|
+
|
|
103
|
+
`associate` / `defineScopes` / `defineSubqueries` / `setupHooks` are **not deleted** even when
|
|
104
|
+
this model adds nothing to them. Reduce the body to two lines: `super.xxx?.()` (an optional call)
|
|
105
|
+
plus `// noop`.
|
|
106
|
+
|
|
107
|
+
- **Why**: per the
|
|
108
|
+
[grand principle](../SKILL.md#grand-principle-a-model-is-a-filled-in-template-not-free-form-code),
|
|
109
|
+
to avoid mistaking *a gap* for *not-applicable*. A noop reads as "no relation / no scope — and
|
|
110
|
+
that was considered". Putting `super.xxx?.()` first is so a Mixin ([mixins.md](./mixins.md))
|
|
111
|
+
does not get its base-defined behavior (Backup's `afterSave`, etc.) **swallowed** — each method
|
|
112
|
+
on `RenchanModel` calls the Mixin's same-named handler via `mixinsApplier`, so if the override
|
|
113
|
+
does not call `super`, the Mixin is disabled.
|
|
114
|
+
- Calling with `?.` (optional) is so it passes safely even when the base has no such method.
|
|
115
|
+
|
|
116
|
+
```js
|
|
117
|
+
// Good example (a defineScopes with nothing to add)
|
|
118
|
+
/**
|
|
119
|
+
* Define model scopes
|
|
120
|
+
*
|
|
121
|
+
* @param {import('sequelize').Op} Op - Sequelize operators
|
|
122
|
+
*/
|
|
123
|
+
static defineScopes (Op) {
|
|
124
|
+
super.defineScopes?.(Op)
|
|
125
|
+
|
|
126
|
+
// noop
|
|
127
|
+
}
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
```js
|
|
131
|
+
// Bad example 1 (deleting the whole method — can't tell not-applicable from forgotten)
|
|
132
|
+
// defineScopes is not written
|
|
133
|
+
|
|
134
|
+
// Bad example 2 (not calling super — swallows the Mixin's scope / hook)
|
|
135
|
+
/**
|
|
136
|
+
* Setup model hooks
|
|
137
|
+
*/
|
|
138
|
+
static setupHooks () {
|
|
139
|
+
// noop (without super.setupHooks?.(), Backup's afterSave etc. won't run)
|
|
140
|
+
}
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
## createOptions spreads super.createOptions() and adds only extras
|
|
144
|
+
|
|
145
|
+
`createOptions()` **spreads `...super.createOptions(sequelizeClient)`** and adds only this model's
|
|
146
|
+
own options. The base `RenchanModel` returns
|
|
147
|
+
`{ modelName, sequelize, syncOnAssociation:false, timestamps:true, underscored:true }` as defaults.
|
|
148
|
+
|
|
149
|
+
- **Why**: `timestamps:true` ([timestamps.md](./timestamps.md)) and `underscored:true` (mapping
|
|
150
|
+
camelCase attributes → snake columns;
|
|
151
|
+
[SKILL.md](../SKILL.md#keep-a-one-to-one-correspondence-with-the-migration)) are shared
|
|
152
|
+
assumptions for every model. Do not rewrite them per model; use the base defaults. The two
|
|
153
|
+
below are the typical additions.
|
|
154
|
+
- If you add nothing else, finish in the three lines of the spread alone (most models do this).
|
|
155
|
+
|
|
156
|
+
```js
|
|
157
|
+
// Good example (adds nothing)
|
|
158
|
+
/**
|
|
159
|
+
* Define model options
|
|
160
|
+
*
|
|
161
|
+
* @param {import('sequelize').Sequelize} sequelizeClient - Sequelize instance
|
|
162
|
+
* @returns {object} Model options
|
|
163
|
+
*/
|
|
164
|
+
static createOptions (sequelizeClient) {
|
|
165
|
+
return {
|
|
166
|
+
...super.createOptions(sequelizeClient),
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
### When to add tableName
|
|
172
|
+
|
|
173
|
+
Add an explicit `tableName` only for a table that differs from the "physical name = plural /
|
|
174
|
+
model name = singular" rule. A backup table (`*Bk`) is the typical case.
|
|
175
|
+
|
|
176
|
+
- **Why**: normally Sequelize infers the (plural) table name from the (singular) model name, so no
|
|
177
|
+
`tableName` is needed. But a backup table is named **`<original table name>_bk`** (original
|
|
178
|
+
table `customer_orders` → `customer_orders_bk`), so its tail is `_bk` (singular), differing from
|
|
179
|
+
the rule. Sequelize would infer `customer_orders_bks` from the model `CustomerOrdersBk`, which
|
|
180
|
+
clashes with the real table `customer_orders_bk`. Left alone it queries a nonexistent table, so
|
|
181
|
+
declare `tableName`. Conversely, do not write it for a model whose inference is already correct
|
|
182
|
+
(`CustomerOrder → customer_orders`); that is redundant.
|
|
183
|
+
|
|
184
|
+
```js
|
|
185
|
+
// Good example (Bk table; inferred customer_orders_bks ≠ real customer_orders_bk)
|
|
186
|
+
/**
|
|
187
|
+
* Define model options
|
|
188
|
+
*
|
|
189
|
+
* @param {import('sequelize').Sequelize} sequelizeClient - Sequelize instance
|
|
190
|
+
* @returns {object} Model options
|
|
191
|
+
*/
|
|
192
|
+
static createOptions (sequelizeClient) {
|
|
193
|
+
return {
|
|
194
|
+
...super.createOptions(sequelizeClient),
|
|
195
|
+
|
|
196
|
+
tableName: 'customer_orders_bk',
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
```
|
|
200
|
+
|
|
201
|
+
### When to add paranoid
|
|
202
|
+
|
|
203
|
+
Add `paranoid: true` only for a table that uses `deleted_at` soft delete.
|
|
204
|
+
|
|
205
|
+
- **Why**: with `paranoid` on, Sequelize stamps `deleted_at` on delete and excludes such rows
|
|
206
|
+
from default finds. This works together with the migration creating a `deleted_at` column via
|
|
207
|
+
`...factory.TIMESTAMPS_WITH_DELETED_AT` (`hor-sequelize-migration`). Adding `paranoid` without
|
|
208
|
+
the column makes queries fail.
|
|
209
|
+
|
|
210
|
+
```js
|
|
211
|
+
// Good example (a join table that soft-deletes)
|
|
212
|
+
/**
|
|
213
|
+
* Define model options
|
|
214
|
+
*
|
|
215
|
+
* @param {import('sequelize').Sequelize} sequelizeClient - Sequelize instance
|
|
216
|
+
* @returns {object} Model options
|
|
217
|
+
*/
|
|
218
|
+
static createOptions (sequelizeClient) {
|
|
219
|
+
return {
|
|
220
|
+
...super.createOptions(sequelizeClient),
|
|
221
|
+
|
|
222
|
+
paranoid: true, // for deleted_at column
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
```
|
|
226
|
+
|
|
227
|
+
## defineScopes / defineSubqueries / setupHooks are framework extension points
|
|
228
|
+
|
|
229
|
+
Understand the role of these three extension points (when you use them, add to them while keeping
|
|
230
|
+
`super.xxx?.()` first).
|
|
231
|
+
|
|
232
|
+
| Method | What it is for | API used |
|
|
233
|
+
| --- | --- | --- |
|
|
234
|
+
| `defineScopes(Op)` | add named scopes (reusable units of filtering / ordering) | `this.addScope(name, options)` |
|
|
235
|
+
| `defineSubqueries()` | register correlated subqueries by name so `this.subquery(name, ...)` can use them | `this.addSubquery({ name, generator })` |
|
|
236
|
+
| `setupHooks()` | add lifecycle hooks (before/after save and find) | `this.beforeFind` / `this.afterSave` / `this.addHook(...)` |
|
|
237
|
+
|
|
238
|
+
- The routine behaviors of `defineScopes` / `setupHooks` are better pushed into a **Mixin** than
|
|
239
|
+
hand-written per model ([mixins.md](./mixins.md)). For example, "clone to a history table on
|
|
240
|
+
every save" is not written directly in `setupHooks`; you pass `BackupMixinModel`. Add a scope /
|
|
241
|
+
hook per model only for one-off requirements a Mixin cannot express.
|
|
242
|
+
- `defineSubqueries` is implemented in models that need a correlated subquery (e.g.
|
|
243
|
+
`CustomerOrder.js`; read the physical column name via the attribute's `.field`). Leave it as a
|
|
244
|
+
noop in models that do not use it.
|