@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,147 @@
|
|
|
1
|
+
# Alter Table (adding / removing columns)
|
|
2
|
+
|
|
3
|
+
How to write migrations that add or remove columns on an existing table. Referenced from §5 of
|
|
4
|
+
`SKILL.md`. The column type / `field` / `allowNull` notation is shared with
|
|
5
|
+
[notation.md](./notation.md).
|
|
6
|
+
|
|
7
|
+
## Add with addColumn, drop with raw SQL DROP COLUMN (symmetric up/down)
|
|
8
|
+
|
|
9
|
+
`addColumn` in `up`, and drop the same column in `down` to keep them symmetric. The filename is
|
|
10
|
+
`{timestamp}-{seq}-alter_table-<table>-<column>.cjs`.
|
|
11
|
+
|
|
12
|
+
**Do not use `removeColumn` to drop a column.** Sequelize's `removeColumn` does not work correctly
|
|
13
|
+
on MariaDB (live), so drop a column — in `up` or `down` — with raw SQL
|
|
14
|
+
`ALTER TABLE ... DROP COLUMN` via `queryInterface.sequelize.query`. Quote identifiers with backticks
|
|
15
|
+
(valid on both SQLite and MariaDB).
|
|
16
|
+
|
|
17
|
+
```js
|
|
18
|
+
'use strict'
|
|
19
|
+
|
|
20
|
+
/*
|
|
21
|
+
* Add webhook_url to content_generation_jobs (TICKET-1234).
|
|
22
|
+
* The destination that notifies an external system of an integration job's completion / failure.
|
|
23
|
+
* When unset, the Worker falls back to integration_clients.default_webhook_url.
|
|
24
|
+
*/
|
|
25
|
+
|
|
26
|
+
const TABLE_NAME = 'content_generation_jobs'
|
|
27
|
+
const COLUMN_NAME = 'webhook_url'
|
|
28
|
+
|
|
29
|
+
module.exports = {
|
|
30
|
+
async up (
|
|
31
|
+
queryInterface,
|
|
32
|
+
Sequelize
|
|
33
|
+
) {
|
|
34
|
+
await queryInterface.addColumn(TABLE_NAME, COLUMN_NAME, {
|
|
35
|
+
type: Sequelize.STRING(500),
|
|
36
|
+
allowNull: true,
|
|
37
|
+
})
|
|
38
|
+
},
|
|
39
|
+
|
|
40
|
+
async down (
|
|
41
|
+
queryInterface,
|
|
42
|
+
Sequelize
|
|
43
|
+
) {
|
|
44
|
+
// removeColumn does not work on MariaDB, so drop via raw SQL DROP COLUMN.
|
|
45
|
+
await queryInterface.sequelize.query(`
|
|
46
|
+
ALTER TABLE \`${TABLE_NAME}\`
|
|
47
|
+
DROP COLUMN \`${COLUMN_NAME}\`
|
|
48
|
+
`)
|
|
49
|
+
},
|
|
50
|
+
}
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
- **Why (symmetry)**: writing `down` symmetrically lets the migration be rolled back (drop in `down`
|
|
54
|
+
exactly what `up` added). Unlike `createTable`, a column add does not use `MigrationAttributeFactory`
|
|
55
|
+
(the PK / timestamps already exist on the table), so a `require` is often unnecessary.
|
|
56
|
+
- **Why (raw SQL)**: `queryInterface.removeColumn` does not work correctly on MariaDB. To stay
|
|
57
|
+
dialect-independent, run `ALTER TABLE ... DROP COLUMN` directly. The add side (`addColumn`) is
|
|
58
|
+
fine, so keep using it.
|
|
59
|
+
|
|
60
|
+
## Write an intent comment (`/* ... */`) at the top
|
|
61
|
+
|
|
62
|
+
A column-adding migration is really about "why is this column being added", so put a `/* ... */`
|
|
63
|
+
block at the top describing the intent. Leave the ticket number, the spec section, and backward-
|
|
64
|
+
compat notes (e.g. why it does not break the existing flow).
|
|
65
|
+
|
|
66
|
+
- **Why**: even a single-column add cannot be reviewed or rolled back confidently without the
|
|
67
|
+
background (which feature, which spec). The intent lives only in the comment, not in the code. As
|
|
68
|
+
a domain note it may be written in Japanese (see the grand principle in `SKILL.md`).
|
|
69
|
+
|
|
70
|
+
## A single column is a string; multiple columns are a `COLUMN_NAME` object
|
|
71
|
+
|
|
72
|
+
For a single column, `COLUMN_NAME` may be a string. For multiple, make `COLUMN_NAME` an object. The
|
|
73
|
+
`up` `addColumn`s can be grouped with `Promise.all`, but the `down` column drops are run as **one
|
|
74
|
+
raw statement per column, sequentially** (SQLite can only drop one column per statement, so do not
|
|
75
|
+
merge them into one statement — split per column).
|
|
76
|
+
|
|
77
|
+
```js
|
|
78
|
+
const TABLE_NAME = 'content_sessions'
|
|
79
|
+
const COLUMN_NAME = {
|
|
80
|
+
WIZARD_ANSWERS_JSON: 'wizard_answers_json',
|
|
81
|
+
WIZARD_TEMPLATE_ID: 'wizard_template_id',
|
|
82
|
+
WIZARD_TEMPLATE_VERSION: 'wizard_template_version',
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
module.exports = {
|
|
86
|
+
async up (
|
|
87
|
+
queryInterface,
|
|
88
|
+
Sequelize
|
|
89
|
+
) {
|
|
90
|
+
await Promise.all([
|
|
91
|
+
queryInterface.addColumn(TABLE_NAME, COLUMN_NAME.WIZARD_ANSWERS_JSON, {
|
|
92
|
+
type: Sequelize.JSON,
|
|
93
|
+
allowNull: true,
|
|
94
|
+
}),
|
|
95
|
+
queryInterface.addColumn(TABLE_NAME, COLUMN_NAME.WIZARD_TEMPLATE_ID, {
|
|
96
|
+
type: Sequelize.INTEGER,
|
|
97
|
+
allowNull: true,
|
|
98
|
+
}),
|
|
99
|
+
queryInterface.addColumn(TABLE_NAME, COLUMN_NAME.WIZARD_TEMPLATE_VERSION, {
|
|
100
|
+
type: Sequelize.INTEGER,
|
|
101
|
+
allowNull: true,
|
|
102
|
+
}),
|
|
103
|
+
])
|
|
104
|
+
|
|
105
|
+
return Promise.resolve()
|
|
106
|
+
},
|
|
107
|
+
|
|
108
|
+
async down (
|
|
109
|
+
queryInterface,
|
|
110
|
+
Sequelize
|
|
111
|
+
) {
|
|
112
|
+
// removeColumn does not work on MariaDB, so drop per column with raw SQL DROP COLUMN.
|
|
113
|
+
await queryInterface.sequelize.query(`
|
|
114
|
+
ALTER TABLE \`${TABLE_NAME}\`
|
|
115
|
+
DROP COLUMN \`${COLUMN_NAME.WIZARD_ANSWERS_JSON}\`
|
|
116
|
+
`)
|
|
117
|
+
await queryInterface.sequelize.query(`
|
|
118
|
+
ALTER TABLE \`${TABLE_NAME}\`
|
|
119
|
+
DROP COLUMN \`${COLUMN_NAME.WIZARD_TEMPLATE_ID}\`
|
|
120
|
+
`)
|
|
121
|
+
await queryInterface.sequelize.query(`
|
|
122
|
+
ALTER TABLE \`${TABLE_NAME}\`
|
|
123
|
+
DROP COLUMN \`${COLUMN_NAME.WIZARD_TEMPLATE_VERSION}\`
|
|
124
|
+
`)
|
|
125
|
+
|
|
126
|
+
return Promise.resolve()
|
|
127
|
+
},
|
|
128
|
+
}
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
## Columns on a table with existing rows use `allowNull: true` or `defaultValue`
|
|
132
|
+
|
|
133
|
+
When adding a column to a table that already holds data, make it `allowNull: true` or give it a
|
|
134
|
+
`defaultValue`. Do not add a `NOT NULL` column that existing rows cannot satisfy (they would
|
|
135
|
+
violate it and the migration would fail). "All nullable for backward compatibility", as above, is
|
|
136
|
+
the default.
|
|
137
|
+
|
|
138
|
+
- When adding an FK column later, the notation is the same as in `createTable` — type `BIGINT`,
|
|
139
|
+
uppercase-starting key, `// ForeignKey must start with upper case.` (see
|
|
140
|
+
[notation.md](./notation.md)). Do not add a DB FK constraint
|
|
141
|
+
([foreign-keys.md](./foreign-keys.md)). If it is used in lookups, `addIndex` it separately (see
|
|
142
|
+
[indexes.md](./indexes.md)).
|
|
143
|
+
|
|
144
|
+
## Applying changes
|
|
145
|
+
|
|
146
|
+
After adding or changing a column, rebuild the local DB with `npm run r` (`db:refresh`) to apply it
|
|
147
|
+
(see the "Applying changes to the local DB" section of `SKILL.md`).
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
# Foreign Keys (do not add DB FK constraints)
|
|
2
|
+
|
|
3
|
+
How to treat FK-like columns, and the policy of not creating DB-level foreign-key constraints.
|
|
4
|
+
Referenced from §3 of `SKILL.md`. For the FK column notation see [notation.md](./notation.md); for
|
|
5
|
+
the UNIQUE index that enforces 1:1 see [indexes.md](./indexes.md).
|
|
6
|
+
|
|
7
|
+
## Do not write `references` / `onDelete` / `onUpdate` / `addConstraint`
|
|
8
|
+
|
|
9
|
+
Do **not** create DB-level FK constraints. Do not put `references` on a column definition, do not
|
|
10
|
+
attach `onDelete` / `onUpdate`, and do not add an FK constraint via
|
|
11
|
+
`queryInterface.addConstraint(...)` (there is not a single FK constraint in the existing
|
|
12
|
+
migrations).
|
|
13
|
+
|
|
14
|
+
```js
|
|
15
|
+
// Good example (an FK-like column but no constraint — just the column + comment + a lookup index)
|
|
16
|
+
// ForeignKey must start with upper case.
|
|
17
|
+
CustomerId: {
|
|
18
|
+
type: Sequelize.BIGINT,
|
|
19
|
+
field: COLUMN_NAME.CUSTOMER_ID,
|
|
20
|
+
allowNull: false,
|
|
21
|
+
},
|
|
22
|
+
// ...
|
|
23
|
+
await queryInterface.addIndex(TABLE_NAME, [
|
|
24
|
+
COLUMN_NAME.CUSTOMER_ID,
|
|
25
|
+
], {
|
|
26
|
+
name: [TABLE_NAME, SHORT_COLUMN_NAME.CUSTOMER_ID, 'index'].join('_'),
|
|
27
|
+
})
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
```js
|
|
31
|
+
// Bad example (adds a DB FK constraint)
|
|
32
|
+
CustomerId: {
|
|
33
|
+
type: Sequelize.BIGINT,
|
|
34
|
+
field: COLUMN_NAME.CUSTOMER_ID,
|
|
35
|
+
allowNull: false,
|
|
36
|
+
references: { model: 'customers', key: 'id' }, // ← do not write
|
|
37
|
+
onDelete: 'CASCADE', // ← do not write
|
|
38
|
+
},
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
- **Why**: dev = SQLite / staging = MySQL / live = MariaDB differ, and there is no `sync` either. A
|
|
42
|
+
DB FK constraint (1) pins insert / delete order, (2) makes cloning into backup (`*_bk`) tables
|
|
43
|
+
hard, and (3) cannot express circular references. Referential integrity is enforced in the **app
|
|
44
|
+
layer** (model associations and domain logic).
|
|
45
|
+
|
|
46
|
+
## Create the FK *column*; express the relation on the model side
|
|
47
|
+
|
|
48
|
+
Do not add the constraint, but **do create the column** that holds another table's id (`BIGINT`,
|
|
49
|
+
uppercase-starting, `// ForeignKey must start with upper case.`; see [notation.md](./notation.md)).
|
|
50
|
+
Declaring the relation that uses that column is the job of the model's `associate()`
|
|
51
|
+
(`hor-sequelize-model`). The migration goes as far as "column + lookup index"; the meaning of the
|
|
52
|
+
relation belongs to the model.
|
|
53
|
+
|
|
54
|
+
## Enforce 1:1 with a UNIQUE index, not an FK
|
|
55
|
+
|
|
56
|
+
A 1:1 relation (a child that has exactly one parent) enforces uniqueness with a **UNIQUE index** on
|
|
57
|
+
the parent-id column, not with an FK constraint (see [indexes.md](./indexes.md)). Attach the comment
|
|
58
|
+
verbatim.
|
|
59
|
+
|
|
60
|
+
```js
|
|
61
|
+
// A 1:1 relation is enforced by a UNIQUE index (no DB FK).
|
|
62
|
+
await queryInterface.addIndex(TABLE_NAME, [
|
|
63
|
+
COLUMN_NAME.CONTENT_GENERATION_JOB_ID,
|
|
64
|
+
], {
|
|
65
|
+
unique: true,
|
|
66
|
+
name: [TABLE_NAME, SHORT_COLUMN_NAME.CONTENT_GENERATION_JOB_ID, 'unique'].join('_'),
|
|
67
|
+
})
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
## Circular FKs / optional relations: column-only and nullable
|
|
71
|
+
|
|
72
|
+
When two tables reference each other (a circular FK), or the relation is optional, keep just the
|
|
73
|
+
**column**, `allowNull: true`. With no constraint, even a cycle is fine, and an unresolved value can
|
|
74
|
+
be null.
|
|
75
|
+
|
|
76
|
+
```js
|
|
77
|
+
// ForeignKey must start with upper case. (circular FK, column only)
|
|
78
|
+
ContentGenerationJobId: {
|
|
79
|
+
type: Sequelize.BIGINT,
|
|
80
|
+
field: COLUMN_NAME.CONTENT_GENERATION_JOB_ID,
|
|
81
|
+
allowNull: true,
|
|
82
|
+
},
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
- Example: `content_sessions` and `content_generation_jobs` each hold the other's id. Since no
|
|
86
|
+
DB FK is added, both can hold the other as a column.
|
|
87
|
+
|
|
88
|
+
## A set of ids you don't want as FKs → a JSON array
|
|
89
|
+
|
|
90
|
+
When you want to hold a set of ids but not enough to warrant individual FK columns or a join table,
|
|
91
|
+
hold them in a `JSON`-typed array column. Same idea as having no FK constraint: check referential
|
|
92
|
+
integrity in the app layer.
|
|
93
|
+
|
|
94
|
+
```js
|
|
95
|
+
// An array of ids without an FK.
|
|
96
|
+
optionIdsJson: {
|
|
97
|
+
type: Sequelize.JSON,
|
|
98
|
+
field: COLUMN_NAME.OPTION_IDS_JSON,
|
|
99
|
+
allowNull: true,
|
|
100
|
+
},
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
## Index the FK columns
|
|
104
|
+
|
|
105
|
+
Even without a constraint, an FK column is the starting point of joins and filters, so it usually
|
|
106
|
+
gets a **plain index** (see [indexes.md](./indexes.md)). A 1:1 uses a UNIQUE index, a 1:N uses a
|
|
107
|
+
plain index. A join table's composite key uses a composite UNIQUE index.
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
# Indexes (naming, and shortening long names)
|
|
2
|
+
|
|
3
|
+
How to write `addIndex`, how to build the name, and the shortening algorithm for when a name runs
|
|
4
|
+
long. Referenced from §4 of `SKILL.md`. The fact that a UNIQUE index is the real constraint also
|
|
5
|
+
relates to [foreign-keys.md](./foreign-keys.md).
|
|
6
|
+
|
|
7
|
+
## `addIndex(TABLE_NAME, [columns...], { name, unique? })`
|
|
8
|
+
|
|
9
|
+
Add indexes with `queryInterface.addIndex`. The second argument is an array of **physical column
|
|
10
|
+
names** (`COLUMN_NAME.X`). Always give `name` explicitly, built by `.join('_')` on an array.
|
|
11
|
+
|
|
12
|
+
- **Plain index**: end it with `'index'` → `<table>_<column>_index`
|
|
13
|
+
- **UNIQUE index**: add `unique: true` and end it with `'unique'` → `<table>_<column>_unique`
|
|
14
|
+
|
|
15
|
+
```js
|
|
16
|
+
// Good example (UNIQUE)
|
|
17
|
+
await queryInterface.addIndex(TABLE_NAME, [
|
|
18
|
+
COLUMN_NAME.ACCESS_TOKEN,
|
|
19
|
+
], {
|
|
20
|
+
unique: true,
|
|
21
|
+
name: [
|
|
22
|
+
TABLE_NAME,
|
|
23
|
+
COLUMN_NAME.ACCESS_TOKEN,
|
|
24
|
+
'unique',
|
|
25
|
+
].join('_'),
|
|
26
|
+
})
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
- **Why**: omitting `name` lets Sequelize auto-name it, which is inconsistent across dialects and
|
|
30
|
+
renames. The trailing `index` / `unique` makes the kind readable from the name. Taking the column
|
|
31
|
+
name from `COLUMN_NAME` guarantees it matches the column definition's `field:` spelling.
|
|
32
|
+
|
|
33
|
+
## Multiple indexes → `Promise.all`; a single one → `await` directly
|
|
34
|
+
|
|
35
|
+
When adding several indexes to the same table, wrap them in `Promise.all([...])`. For just one,
|
|
36
|
+
`await` it directly.
|
|
37
|
+
|
|
38
|
+
```js
|
|
39
|
+
// Good example (multiple)
|
|
40
|
+
await Promise.all([
|
|
41
|
+
queryInterface.addIndex(TABLE_NAME, [
|
|
42
|
+
COLUMN_NAME.CHAT_ROOM_ID,
|
|
43
|
+
], {
|
|
44
|
+
name: [TABLE_NAME, SHORT_COLUMN_NAME.CHAT_ROOM_ID, 'index'].join('_'),
|
|
45
|
+
}),
|
|
46
|
+
queryInterface.addIndex(TABLE_NAME, [
|
|
47
|
+
COLUMN_NAME.CUSTOMER_ID,
|
|
48
|
+
], {
|
|
49
|
+
name: [TABLE_NAME, SHORT_COLUMN_NAME.CUSTOMER_ID, 'index'].join('_'),
|
|
50
|
+
}),
|
|
51
|
+
])
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
## Composite indexes: list the columns, or fold them into a meaningful label
|
|
55
|
+
|
|
56
|
+
A composite (multi-column) index may list all the columns, or fold them into a single meaningful
|
|
57
|
+
word when that runs long.
|
|
58
|
+
|
|
59
|
+
```js
|
|
60
|
+
// List the columns
|
|
61
|
+
name: [TABLE_NAME, COLUMN_NAME.STATUS, COLUMN_NAME.EXPIRES_AT, 'index'].join('_')
|
|
62
|
+
// → content_sessions_status_expires_at_index
|
|
63
|
+
|
|
64
|
+
// Fold into a meaningful label (a wide composite UNIQUE)
|
|
65
|
+
name: [SHORT_TABLE_NAME, 'plan_tier_currency_effective_at', 'unique'].join('_')
|
|
66
|
+
// → cpr_plan_tier_currency_effective_at_unique
|
|
67
|
+
name: [SHORT_TABLE_NAME, 'job_package', 'unique'].join('_')
|
|
68
|
+
// → cgjp_job_package_unique
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
## Shortening a long index name (over ~50 chars)
|
|
72
|
+
|
|
73
|
+
The DB identifier limit is 64 characters. As a **safety margin, shorten once a name would run past
|
|
74
|
+
~50 characters**. Shorten in stages, in the **following priority order**.
|
|
75
|
+
|
|
76
|
+
### Priority 1: shorten the column name(s) first (keep the table name in full)
|
|
77
|
+
|
|
78
|
+
Define `SHORT_COLUMN_NAME` and swap only the column part of the `name` for it. Keep the table name
|
|
79
|
+
in full.
|
|
80
|
+
|
|
81
|
+
```js
|
|
82
|
+
const SHORT_COLUMN_NAME = {
|
|
83
|
+
CONTENT_GENERATION_JOB_ID: 'cgji',
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
// content_generations_content_generation_job_id_unique (52 chars) is too long
|
|
87
|
+
// → shorten only the column name
|
|
88
|
+
name: [
|
|
89
|
+
TABLE_NAME, // content_generations (full)
|
|
90
|
+
SHORT_COLUMN_NAME.CONTENT_GENERATION_JOB_ID, // cgji
|
|
91
|
+
'unique',
|
|
92
|
+
].join('_')
|
|
93
|
+
// → content_generations_cgji_unique (31 chars)
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
### Priority 2: if still too long, shorten the table name too
|
|
97
|
+
|
|
98
|
+
Only when shortening the column name is not enough (the table name itself is long) do you also
|
|
99
|
+
define `SHORT_TABLE_NAME` and shorten the table name.
|
|
100
|
+
|
|
101
|
+
```js
|
|
102
|
+
const SHORT_TABLE_NAME = 'cgrf' // content_generation_requirement_files
|
|
103
|
+
const SHORT_COLUMN_NAME = {
|
|
104
|
+
CONTENT_GENERATION_JOB_ID: 'cgji',
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
name: [
|
|
108
|
+
SHORT_TABLE_NAME, // cgrf
|
|
109
|
+
SHORT_COLUMN_NAME.CONTENT_GENERATION_JOB_ID, // cgji
|
|
110
|
+
'index',
|
|
111
|
+
].join('_')
|
|
112
|
+
// → cgrf_cgji_index (15 chars)
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
- **Why this order**: the table name is the first thing a reader of an index uses to tell "which
|
|
116
|
+
table is this", so keep it in full as long as possible. Shorten the **less-informative column
|
|
117
|
+
name first**; cut the table name only as a last resort.
|
|
118
|
+
|
|
119
|
+
### How to shorten = take the initial of each word (split on `_`)
|
|
120
|
+
|
|
121
|
+
Build the abbreviation by splitting the snake_case name on `_` and concatenating the **first
|
|
122
|
+
character** of each word.
|
|
123
|
+
|
|
124
|
+
| Original name | Abbreviation |
|
|
125
|
+
| --- | --- |
|
|
126
|
+
| `content_generation_job_id` | `cgji` |
|
|
127
|
+
| `content_generation_requirement_files` | `cgrf` |
|
|
128
|
+
| `content_generation_job_packages` | `cgjp` |
|
|
129
|
+
| `content_plan_rates` | `cpr` |
|
|
130
|
+
| `chat_room_id` | `cri` |
|
|
131
|
+
| `customer_id` | `ci` |
|
|
132
|
+
|
|
133
|
+
- Always put abbreviations in `SHORT_COLUMN_NAME` / `SHORT_TABLE_NAME` constants and reference them
|
|
134
|
+
from the `addIndex` `name` (do not inline). If abbreviations collide within a table, add a second
|
|
135
|
+
letter (etc.) to keep them unique while staying meaningful.
|
|
136
|
+
|
|
137
|
+
### Shorten only when needed
|
|
138
|
+
|
|
139
|
+
If it fits within ~50 characters, do not shorten. `.join('_')` the full `TABLE_NAME` / `COLUMN_NAME`
|
|
140
|
+
as in `customers_registered_at_index`. **Do not mechanically initialize just because you can** (it
|
|
141
|
+
hurts readability).
|
|
142
|
+
|
|
143
|
+
## A UNIQUE named index is "the real constraint"
|
|
144
|
+
|
|
145
|
+
The uniqueness of a 1:1 relation's FK or a natural key is **actually enforced by this UNIQUE named
|
|
146
|
+
index**. A model's `unique: true` (`hor-sequelize-model`) is only a
|
|
147
|
+
reader-facing declaration and does not create a DB constraint (this repo does not `sync`). For the
|
|
148
|
+
policy of not adding DB FK constraints and enforcing 1:1 with a UNIQUE index, see
|
|
149
|
+
[foreign-keys.md](./foreign-keys.md).
|
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
# Notation (file skeleton, constants, column notation)
|
|
2
|
+
|
|
3
|
+
The skeleton of a migration file, the `TABLE_NAME` / `COLUMN_NAME` constants, and how to write a
|
|
4
|
+
column inside `createTable`. Referenced from §1–§3 of `SKILL.md`. For indexes see
|
|
5
|
+
[indexes.md](./indexes.md); for the "no DB FK" rule see [foreign-keys.md](./foreign-keys.md).
|
|
6
|
+
|
|
7
|
+
## One migration = one file, `.cjs` (CommonJS)
|
|
8
|
+
|
|
9
|
+
Place one migration per file directly under `sequelize/migrations/`. The extension is `.cjs`; start
|
|
10
|
+
with `'use strict'` and **`require`** `MigrationAttributeFactory` (unlike the model side's `.js` /
|
|
11
|
+
`import`, a migration is CommonJS).
|
|
12
|
+
|
|
13
|
+
```js
|
|
14
|
+
'use strict'
|
|
15
|
+
|
|
16
|
+
const MigrationAttributeFactory = require('@openreachtech/renchan-sequelize/lib/tools/MigrationAttributeFactory.cjs')
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
- **Why**: sequelize-cli loads migrations as CommonJS. Written as `import` / ESM they will not load.
|
|
20
|
+
The factory `require` path is fixed at `lib/tools/MigrationAttributeFactory.cjs` inside the
|
|
21
|
+
package.
|
|
22
|
+
|
|
23
|
+
## Filename `{timestamp}-{seq}-<operation>-<table>[-<column>].cjs`
|
|
24
|
+
|
|
25
|
+
The filename is itself the execution order (ascending timestamp = creation-time order) and an index
|
|
26
|
+
of the operation.
|
|
27
|
+
|
|
28
|
+
- `{timestamp}` = creation time `YYYYMMDDHHmmss` (14 digits; obtained from `date +%Y%m%d%H%M%S`).
|
|
29
|
+
- `{seq}` = a 6-digit, zero-padded running number (`000001`…). It doubles as the order within the
|
|
30
|
+
same timestamp and as a stable number for humans to refer to.
|
|
31
|
+
- The operation is `create_table` (create a table) / `alter_table` (add / remove / change columns on
|
|
32
|
+
an existing table).
|
|
33
|
+
- Examples: `20260717135803-000001-create_table-content_generations.cjs` /
|
|
34
|
+
`20260717140512-000002-alter_table-content_generation_jobs-webhook_url.cjs`
|
|
35
|
+
- **Why**: the leading timestamp fixes the apply order uniquely in creation-time order, and even
|
|
36
|
+
when several people add migrations around the same time they rarely collide. The 6-digit seq is a
|
|
37
|
+
stable identifier for a human to track "which migration is this".
|
|
38
|
+
|
|
39
|
+
(Older migrations use the `<8-digit-seq>-create_table-...` form. Write new files in the form above.)
|
|
40
|
+
|
|
41
|
+
## Declare `TABLE_NAME` and `COLUMN_NAME` at the top (single source of truth for physical names)
|
|
42
|
+
|
|
43
|
+
Outside `up` / `down`, before `module.exports`, declare `TABLE_NAME` (a string) and `COLUMN_NAME`
|
|
44
|
+
(an object). `COLUMN_NAME` keys are `SCREAMING_SNAKE`, values are the **physical column names
|
|
45
|
+
(snake_case)**. This value is referenced by **both** the column's `field:` and the index name (see
|
|
46
|
+
[indexes.md](./indexes.md)).
|
|
47
|
+
|
|
48
|
+
```js
|
|
49
|
+
const TABLE_NAME = 'content_plan_rates'
|
|
50
|
+
const COLUMN_NAME = {
|
|
51
|
+
PLAN: 'plan',
|
|
52
|
+
TIER: 'tier',
|
|
53
|
+
UNIT_RATE: 'unit_rate',
|
|
54
|
+
EFFECTIVE_AT: 'effective_at',
|
|
55
|
+
IS_ACTIVE: 'is_active',
|
|
56
|
+
}
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
- **Why**: concentrating the physical names in one place keeps the `field:` and the index name from
|
|
60
|
+
drifting apart in spelling. When a physical name changes, editing this one place makes both the
|
|
61
|
+
`field:` and the index name follow. Do not inline string literals into column definitions or
|
|
62
|
+
indexes.
|
|
63
|
+
|
|
64
|
+
## `module.exports = { up, down }`, arguments `(queryInterface, Sequelize)`
|
|
65
|
+
|
|
66
|
+
Both `up` / `down` are `async` and take `(queryInterface, Sequelize)` in that order. Inside `up`,
|
|
67
|
+
build `const factory = MigrationAttributeFactory.create(Sequelize)`.
|
|
68
|
+
|
|
69
|
+
```js
|
|
70
|
+
module.exports = {
|
|
71
|
+
async up (
|
|
72
|
+
queryInterface,
|
|
73
|
+
Sequelize
|
|
74
|
+
) {
|
|
75
|
+
const factory = MigrationAttributeFactory.create(Sequelize)
|
|
76
|
+
|
|
77
|
+
await queryInterface.createTable(TABLE_NAME, {
|
|
78
|
+
...factory.ID_BIGINT,
|
|
79
|
+
// ... columns ...
|
|
80
|
+
...factory.TIMESTAMPS,
|
|
81
|
+
})
|
|
82
|
+
|
|
83
|
+
// ... addIndex ...
|
|
84
|
+
|
|
85
|
+
return Promise.resolve()
|
|
86
|
+
},
|
|
87
|
+
|
|
88
|
+
async down (
|
|
89
|
+
queryInterface,
|
|
90
|
+
Sequelize
|
|
91
|
+
) {
|
|
92
|
+
return queryInterface.dropTable(TABLE_NAME)
|
|
93
|
+
},
|
|
94
|
+
}
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
- The `down` of a create-table migration is always `return queryInterface.dropTable(TABLE_NAME)`
|
|
98
|
+
(drop the whole table). The `down` of a column migration is in [alter-table.md](./alter-table.md).
|
|
99
|
+
- `up` closes with `return Promise.resolve()` (the existing convention).
|
|
100
|
+
|
|
101
|
+
## Spread the PK with `...factory.ID_BIGINT` at the top
|
|
102
|
+
|
|
103
|
+
Put `...factory.ID_BIGINT` at the **top** of the object passed to `createTable`. Use
|
|
104
|
+
`...factory.ID_INTEGER` only when you need an integer PK. Never hand-write `id`.
|
|
105
|
+
|
|
106
|
+
- `factory.ID_BIGINT` returns `{ id: { field:'id', type: BIGINT, allowNull:false,
|
|
107
|
+
autoIncrement:true, primaryKey:true } }`. It is the shorthand that keeps you from getting that
|
|
108
|
+
four-piece set wrong.
|
|
109
|
+
- It corresponds one-to-one with `...factory.ID_BIGINT` on the model side
|
|
110
|
+
(`hor-sequelize-model`).
|
|
111
|
+
|
|
112
|
+
```js
|
|
113
|
+
// Bad example (hand-writing id)
|
|
114
|
+
await queryInterface.createTable(TABLE_NAME, {
|
|
115
|
+
id: {
|
|
116
|
+
type: Sequelize.BIGINT,
|
|
117
|
+
autoIncrement: true,
|
|
118
|
+
primaryKey: true,
|
|
119
|
+
},
|
|
120
|
+
// ...
|
|
121
|
+
})
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
## Spread the timestamps with `...factory.TIMESTAMPS` at the end
|
|
125
|
+
|
|
126
|
+
Put `...factory.TIMESTAMPS` at the **end** of the `createTable` object. Do not hand-write
|
|
127
|
+
`created_at` / `updated_at`. For a table that soft-deletes, use
|
|
128
|
+
`...factory.TIMESTAMPS_WITH_DELETED_AT` (which also creates `deleted_at`), paired with
|
|
129
|
+
`paranoid: true` on the model side. See §2 of `SKILL.md`.
|
|
130
|
+
|
|
131
|
+
| preset | columns created |
|
|
132
|
+
| --- | --- |
|
|
133
|
+
| `factory.ID_BIGINT` | `id` (bigint / autoIncrement / primaryKey / NOT NULL) |
|
|
134
|
+
| `factory.ID_INTEGER` | `id` (integer version) |
|
|
135
|
+
| `factory.TIMESTAMPS` | `created_at` / `updated_at` (`DATE(3)` / NOT NULL) |
|
|
136
|
+
| `factory.TIMESTAMPS_WITH_DELETED_AT` | the above + `deleted_at` (`DATE(3)`) |
|
|
137
|
+
|
|
138
|
+
## Write columns with a camelCase key + `field:` (snake physical name)
|
|
139
|
+
|
|
140
|
+
Declare each column with a camelCase key and give `field:` the value `COLUMN_NAME.X`. The type is
|
|
141
|
+
`Sequelize.X`. **Always** write `allowNull`, and write `defaultValue` when there is a default.
|
|
142
|
+
|
|
143
|
+
```js
|
|
144
|
+
// Good example
|
|
145
|
+
role: {
|
|
146
|
+
type: Sequelize.STRING(32),
|
|
147
|
+
field: COLUMN_NAME.ROLE,
|
|
148
|
+
allowNull: false,
|
|
149
|
+
},
|
|
150
|
+
currency: {
|
|
151
|
+
type: Sequelize.STRING(8),
|
|
152
|
+
field: COLUMN_NAME.CURRENCY,
|
|
153
|
+
allowNull: false,
|
|
154
|
+
defaultValue: 'JPY',
|
|
155
|
+
},
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
```js
|
|
159
|
+
// Bad example (field omitted → physical name becomes camelCase; allowNull omitted → defaults to true)
|
|
160
|
+
role: {
|
|
161
|
+
type: Sequelize.STRING(32),
|
|
162
|
+
},
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
- **Why**: without `field:`, the physical name becomes camelCase and no longer matches the model
|
|
166
|
+
(which maps to snake via `underscored:true`). Omitting `allowNull` falls back to Sequelize's
|
|
167
|
+
default `true`, losing the NOT NULL intent. Keep `allowNull` / `defaultValue` aligned with the
|
|
168
|
+
same-named column on the model side.
|
|
169
|
+
|
|
170
|
+
### DataTypes guide
|
|
171
|
+
|
|
172
|
+
| Type | Use |
|
|
173
|
+
| --- | --- |
|
|
174
|
+
| `BIGINT` | PK / FK-like id |
|
|
175
|
+
| `INTEGER` | small integer PK, quantity, version |
|
|
176
|
+
| `STRING(n)` | variable-length string (identifier `32`, display name / email `191`, etc. — pick by use) |
|
|
177
|
+
| `TEXT` / `TEXT('medium')` | long text (body, message, error). For an item that can grow long, use `TEXT('medium')` |
|
|
178
|
+
| `DATE(3)` | millisecond-precision datetime (`registeredAt` / `expiresAt`, etc.) |
|
|
179
|
+
| `BOOLEAN` | truth value (`isActive`, etc.) |
|
|
180
|
+
| `JSON` | structured data (`resultJson`; an FK-less id array `optionIdsJson`) |
|
|
181
|
+
| `DECIMAL(p, s)` | money / rate (`dailyRate` as `DECIMAL(14, 2)`, etc.) |
|
|
182
|
+
| `BLOB('long')` | binary (uploaded file body) |
|
|
183
|
+
| `ENUM(...)` | **avoid by default**; use a domain constant + `STRING(n)` |
|
|
184
|
+
|
|
185
|
+
- Datetimes default to `DATE(3)` (millisecond precision by default, for ordering comparisons and
|
|
186
|
+
history uniqueness).
|
|
187
|
+
- Do not default string lengths to "255 for now". Choose by use, and keep the length identical on
|
|
188
|
+
the model and migration sides.
|
|
189
|
+
- Plain `TEXT` is capped around 64KB; for a column that may hold long content, use
|
|
190
|
+
`TEXT('medium')` (MEDIUMTEXT, ~16MB). Reach for `TEXT('long')` (LONGTEXT) only when even that is
|
|
191
|
+
not enough.
|
|
192
|
+
|
|
193
|
+
## FK-like columns start with an uppercase letter + a comment (type BIGINT)
|
|
194
|
+
|
|
195
|
+
A column holding another table's id gets an **uppercase-starting** key
|
|
196
|
+
(`ContentGenerationJobId` / `CustomerId`) with `// ForeignKey must start with upper case.` in
|
|
197
|
+
English immediately above it. The type is `BIGINT`.
|
|
198
|
+
|
|
199
|
+
```js
|
|
200
|
+
// Good example
|
|
201
|
+
// ForeignKey must start with upper case.
|
|
202
|
+
ContentGenerationJobId: {
|
|
203
|
+
type: Sequelize.BIGINT,
|
|
204
|
+
field: COLUMN_NAME.CONTENT_GENERATION_JOB_ID,
|
|
205
|
+
allowNull: false,
|
|
206
|
+
},
|
|
207
|
+
```
|
|
208
|
+
|
|
209
|
+
- **Why**: the model's association resolves the FK name by "`<associated model name>` + `Id`", so
|
|
210
|
+
align on an uppercase start (`hor-sequelize-model`). `field:` maps it
|
|
211
|
+
to snake (`..._id`).
|
|
212
|
+
- But **do not add a DB FK constraint**. Keep just the column and the comment, and express the
|
|
213
|
+
relation on the model side ([foreign-keys.md](./foreign-keys.md)). Optional relations / circular
|
|
214
|
+
FKs use `allowNull: true`, with a note such as
|
|
215
|
+
`// ForeignKey must start with upper case. (circular FK, column only)` when helpful.
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: hor-sequelize-model
|
|
3
|
+
description: >
|
|
4
|
+
Write and edit renchan/Sequelize model definitions (sequelize/models/*.js).
|
|
5
|
+
Use this skill whenever the user asks to create or update a Sequelize model
|
|
6
|
+
class — its attributes, createOptions, associations, scopes, hooks, or how to
|
|
7
|
+
wire a MixinModel.
|
|
8
|
+
---
|
|
9
|
+
|
|
10
|
+
# Sequelize Model
|
|
11
|
+
|
|
12
|
+
A skill for writing the `sequelize/models/*.js` model definitions used in a renchan project.
|
|
13
|
+
The physical schema is defined by `hor-sequelize-migration`; this
|
|
14
|
+
skill writes the corresponding **logical declarations on top of Sequelize** (attributes /
|
|
15
|
+
association / scope / hook / Mixin). The conventions are split across the detail files below —
|
|
16
|
+
consult them as needed.
|
|
17
|
+
|
|
18
|
+
## Grand principle: a model is a filled-in template, not free-form code
|
|
19
|
+
|
|
20
|
+
Every model extends `BaseAppRenchanModel` and has the **same skeleton**. The six extension
|
|
21
|
+
points that form that skeleton (`createAttributes` / `createOptions` / `associate` /
|
|
22
|
+
`defineScopes` / `defineSubqueries` / `setupHooks`) are **always laid out in the same order,
|
|
23
|
+
even when unused**, and any that have no content keep `super.xxx?.()` plus `// noop`
|
|
24
|
+
([default-methods.md](./references/default-methods.md)).
|
|
25
|
+
|
|
26
|
+
- **Do not "delete to omit"**. Removing a method entirely makes it impossible to tell whether
|
|
27
|
+
the model "has no such feature (i.e. it was considered)" or someone "forgot to write it".
|
|
28
|
+
Keeping an explicit noop declares "considered, and not applicable". This is the same idea as
|
|
29
|
+
the QA stance in `hoc-jest` ("do not cut coverage using implementation
|
|
30
|
+
knowledge") — to avoid mistaking *a gap* for *not-applicable*, always keep the extension
|
|
31
|
+
points visible.
|
|
32
|
+
- Because the skeleton is identical across every model, review attention goes straight to the
|
|
33
|
+
**model-specific differences** (attributes / associations). When the structure varies from
|
|
34
|
+
model to model, it becomes impossible to read what is intentional and what is an omission.
|
|
35
|
+
- When in doubt, **lean toward the template**. Do not try to write it "cleverly" with custom
|
|
36
|
+
static methods or a changed initialization order. Extend via a Mixin
|
|
37
|
+
([mixins.md](./references/mixins.md)) or a framework extension point.
|
|
38
|
+
|
|
39
|
+
## Write code comments in English
|
|
40
|
+
|
|
41
|
+
Comments inside model code (`.js`) — such as `// ForeignKey must start with upper case.` — are
|
|
42
|
+
written in **English**, to match the other comments in the codebase.
|
|
43
|
+
|
|
44
|
+
- This applies to comments inside the model code generated into `sequelize/models/` (the `//`
|
|
45
|
+
and `/* */` in `.js`). They are artifacts, so English.
|
|
46
|
+
- The explanatory prose of this skill (the body text of each Markdown file) is in English as
|
|
47
|
+
well.
|
|
48
|
+
- The comments inside **this skill's own examples** (```js``` blocks) are also in English.
|
|
49
|
+
|
|
50
|
+
## Keep a one-to-one correspondence with the migration
|
|
51
|
+
|
|
52
|
+
A model's attributes must correspond one-to-one with the columns of the matching
|
|
53
|
+
`hor-sequelize-migration`. The camelCase attribute keys on the model side map
|
|
54
|
+
to the snake_case physical columns (`field:`) on the migration side via `underscored: true`
|
|
55
|
+
([default-methods.md](./references/default-methods.md#createoptions-spreads-supercreateoptions-and-adds-only-extras)).
|
|
56
|
+
|
|
57
|
+
- **Why**: adding only one side produces an inconsistency — an attribute exists but the column
|
|
58
|
+
does not (queries fail), or a column exists but the model cannot touch it. Always add or change
|
|
59
|
+
columns in the migration and the model together.
|
|
60
|
+
- Timestamp columns are the exception; **do not** write them in the model's attributes
|
|
61
|
+
([timestamps.md](./references/timestamps.md)).
|
|
62
|
+
|
|
63
|
+
## Detail files
|
|
64
|
+
|
|
65
|
+
- [notation.md](./references/notation.md) — class skeleton, imports, attribute declarations
|
|
66
|
+
- [default-methods.md](./references/default-methods.md) — the six override methods and `createOptions`
|
|
67
|
+
- [timestamps.md](./references/timestamps.md) — do not include `created_at` / `updated_at` in the model
|
|
68
|
+
- [associations.md](./references/associations.md) — how to write `associate()` (relations via `this._`)
|
|
69
|
+
- [mixins.md](./references/mixins.md) — kinds of MixinModel and how to pass them (all 6)
|