@openreachtech/hora-skills-ort-renchan 0.3.0 → 0.5.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/dist/skills/hor-backend-testing/SKILL.md +4 -3
- package/dist/skills/hor-backend-testing/references/running-tests.md +9 -1
- package/dist/skills/hor-build-e2e-test-environment/SKILL.md +17 -8
- package/dist/skills/hor-build-e2e-test-environment/references/compose-definition.md +11 -7
- package/dist/skills/hor-build-e2e-test-environment/references/runner-and-lifecycle.md +1 -1
- package/dist/skills/hor-cookie-authentication/SKILL.md +4 -2
- package/dist/skills/hor-cookie-authentication/references/environment.md +4 -2
- package/dist/skills/hor-database-design/references/column-types.md +25 -9
- package/dist/skills/hor-database-design/references/datetimes.md +7 -3
- package/dist/skills/hor-graphql-schema/SKILL.md +10 -8
- package/dist/skills/hor-graphql-server-engine/SKILL.md +544 -45
- package/dist/skills/hor-light-rag/SKILL.md +6 -1
- package/dist/skills/hor-light-rag/references/retrieval-internals.md +3 -1
- package/dist/skills/hor-multi-llm-provider/SKILL.md +158 -20
- package/dist/skills/hor-multi-llm-provider/references/access-routes.md +230 -0
- package/dist/skills/hor-multi-llm-provider/references/provider-internals.md +9 -3
- package/dist/skills/hor-mutation-resolver/SKILL.md +6 -2
- package/dist/skills/hor-mutation-resolver/references/resolve-flow.md +10 -6
- package/dist/skills/hor-oidc-sign-in/SKILL.md +61 -0
- package/dist/skills/hor-oidc-sign-in/references/environment.md +94 -0
- package/dist/skills/hor-oidc-sign-in/references/id-token-verification.md +98 -0
- package/dist/skills/hor-oidc-sign-in/references/resolvers.md +134 -0
- package/dist/skills/hor-oidc-sign-in/references/storage.md +118 -0
- package/dist/skills/hor-query-resolver/SKILL.md +2 -0
- package/dist/skills/hor-query-resolver/references/anatomy.md +7 -7
- package/dist/skills/hor-query-resolver/references/data-access.md +17 -0
- package/dist/skills/hor-renchan-job-bullmq/SKILL.md +15 -6
- package/dist/skills/hor-renchan-job-bullmq/references/daemon-and-scheduler.md +4 -2
- package/dist/skills/hor-renchan-job-bullmq/references/engine-and-infra.md +39 -5
- package/dist/skills/hor-renchan-job-bullmq/references/job-triple.md +57 -0
- package/dist/skills/hor-resolver-validator/SKILL.md +5 -6
- package/dist/skills/hor-resolver-validator/references/validator-pattern.md +5 -5
- package/dist/skills/hor-restfulapi-architecture/SKILL.md +33 -7
- package/dist/skills/hor-restfulapi-architecture/references/behind-iap.md +116 -0
- package/dist/skills/hor-restfulapi-architecture/references/credential-routes.md +173 -0
- package/dist/skills/hor-restfulapi-architecture/references/engine-and-context.md +11 -5
- package/dist/skills/hor-restfulapi-architecture/references/renderers.md +4 -0
- package/dist/skills/hor-sequelize-migration/SKILL.md +30 -15
- package/dist/skills/hor-sequelize-migration/references/alter-table.md +26 -13
- package/dist/skills/hor-sequelize-migration/references/foreign-keys.md +5 -2
- package/dist/skills/hor-sequelize-migration/references/indexes.md +41 -2
- package/dist/skills/hor-sequelize-migration/references/notation.md +1 -1
- package/dist/skills/hor-sequelize-model/references/associations.md +2 -1
- package/dist/skills/hor-sequelize-model/references/notation.md +1 -1
- package/dist/skills/hor-sequelize-seeder/SKILL.md +9 -4
- package/dist/skills/hor-sequelize-seeder/references/notation.md +59 -0
- package/dist/skills/hor-type-interface/SKILL.md +48 -32
- package/docs/skills.ja.md +6 -5
- package/docs/skills.md +6 -5
- package/package.json +1 -1
|
@@ -15,9 +15,11 @@ A skill for writing the `sequelize/migrations/*.cjs` files. It defines the **phy
|
|
|
15
15
|
when you add a column in the migration, add the matching attribute in the model (and vice versa).
|
|
16
16
|
The conventions are split across the detail files below.
|
|
17
17
|
|
|
18
|
-
This repo does **not** `sync`. The schema is **built entirely by migrations
|
|
19
|
-
staging = MySQL / live = MariaDB
|
|
20
|
-
|
|
18
|
+
This repo does **not** `sync`. The schema is **built entirely by migrations**. The databases these
|
|
19
|
+
skills assume are dev = SQLite / staging = MySQL / live = MariaDB, and the dialects differ; a
|
|
20
|
+
PostgreSQL project substitutes its own databases, and each rule below names PostgreSQL where it
|
|
21
|
+
differs. A model's `unique: true` and the like are only declarations; **the real object is always
|
|
22
|
+
created by the migration**.
|
|
21
23
|
|
|
22
24
|
## Grand principle: a migration is a filled-in template
|
|
23
25
|
|
|
@@ -132,8 +134,8 @@ module.exports = {
|
|
|
132
134
|
- **Each column** is declared with a camelCase key + `field: COLUMN_NAME.X` (the snake physical
|
|
133
135
|
name). **Always** declare `allowNull`, and add `defaultValue` when the column has a default. The
|
|
134
136
|
type is `Sequelize.X` (`BIGINT` / `INTEGER` / `STRING(n)` / `TEXT` (long items: `TEXT('medium')`) /
|
|
135
|
-
`DATE(3)` / `BOOLEAN` / `JSON` / `DECIMAL(p,s)` / `BLOB('long')`). Avoid
|
|
136
|
-
constant + `STRING(n)`.
|
|
137
|
+
`DATE(3)` / `BOOLEAN` / `JSON` (`JSONB` on PostgreSQL) / `DECIMAL(p,s)` / `BLOB('long')`). Avoid
|
|
138
|
+
`ENUM`; use a domain constant + `STRING(n)`.
|
|
137
139
|
Datetimes default to `DATE(3)`. Keep `allowNull` / `defaultValue` in sync with the same-named
|
|
138
140
|
column on the model side.
|
|
139
141
|
|
|
@@ -210,7 +212,8 @@ migrations).
|
|
|
210
212
|
|
|
211
213
|
- **Why**: dev = SQLite / staging = MySQL / live = MariaDB differ, and there is no `sync`. A DB FK
|
|
212
214
|
constraint pins insert order and makes cloning into backup (`*_bk`) tables and handling circular
|
|
213
|
-
references awkward.
|
|
215
|
+
references awkward. Those costs come from the constraint, not from a dialect, so the rule holds on
|
|
216
|
+
PostgreSQL unchanged. Referential integrity is enforced in the **app layer** (associations,
|
|
214
217
|
transactions, existence checks). See [foreign-keys.md](./references/foreign-keys.md); for the
|
|
215
218
|
column notation see [notation.md](./references/notation.md).
|
|
216
219
|
|
|
@@ -246,9 +249,10 @@ await Promise.all([
|
|
|
246
249
|
|
|
247
250
|
### Shortening a long index name (over ~50 chars)
|
|
248
251
|
|
|
249
|
-
The
|
|
250
|
-
|
|
251
|
-
|
|
252
|
+
The identifier limit is 64 characters on MariaDB and MySQL and 63 bytes on PostgreSQL (SQLite sets
|
|
253
|
+
none). As a safety margin, shorten once a name would run past **~50 characters**; the names are
|
|
254
|
+
ASCII snake_case, so a byte is a character. Shorten in the **following priority order**, taking the
|
|
255
|
+
**initial of each word**:
|
|
252
256
|
|
|
253
257
|
1. **First shorten the column name(s)** (`SHORT_COLUMN_NAME`), keeping the table name in full.
|
|
254
258
|
e.g. `content_generations` + `cgji` (= content_generation_job_id) + `unique`
|
|
@@ -257,6 +261,12 @@ word**:
|
|
|
257
261
|
e.g. `cgrf` (= content_generation_requirement_files) + `cgji` + `index`
|
|
258
262
|
→ `cgrf_cgji_index`
|
|
259
263
|
|
|
264
|
+
Index names are unique per table on MariaDB and MySQL, but per schema on PostgreSQL and per
|
|
265
|
+
database on SQLite, so the same name on two tables is an error there. PostgreSQL also truncates a
|
|
266
|
+
name past 63 bytes instead of rejecting it, so two long names that differ only at the end collide.
|
|
267
|
+
Keep the `<table>_` part (or a `SHORT_TABLE_NAME` unique across the schema). See
|
|
268
|
+
[indexes.md](./references/indexes.md).
|
|
269
|
+
|
|
260
270
|
Initialism examples: `content_generation_job_id`→`cgji`, `chat_room_id`→`cri`,
|
|
261
271
|
`customer_id`→`ci`, `content_generation_requirement_files`→`cgrf`, `content_plan_rates`→`err`.
|
|
262
272
|
Put the abbreviations in `SHORT_COLUMN_NAME` / `SHORT_TABLE_NAME` constants and reference them from
|
|
@@ -268,9 +278,13 @@ the `addIndex` `name`. Details and a table of real abbreviations are in
|
|
|
268
278
|
Add a column to an existing table with `addColumn`, and drop the same column symmetrically in
|
|
269
279
|
`down`. The filename is `{timestamp}-{seq}-alter_table-<table>-<column>.cjs`.
|
|
270
280
|
|
|
271
|
-
- **
|
|
272
|
-
MariaDB
|
|
273
|
-
(in `up` or `down
|
|
281
|
+
- **On MariaDB, do not use `removeColumn` to drop a column.** Sequelize's `removeColumn` does not
|
|
282
|
+
work on MariaDB, so when MariaDB is the live database run raw SQL `ALTER TABLE ... DROP COLUMN`
|
|
283
|
+
via `queryInterface.sequelize.query` (in `up` or `down`). Quote each identifier with
|
|
284
|
+
`queryInterface.quoteIdentifier()`, which writes backticks on MariaDB, MySQL and SQLite and
|
|
285
|
+
double quotes on PostgreSQL; a hand-written backtick is a syntax error on PostgreSQL.
|
|
286
|
+
- **On PostgreSQL (and SQLite), `queryInterface.removeColumn` works.** A project whose databases
|
|
287
|
+
are only those two drops a column with `removeColumn` and writes no raw SQL.
|
|
274
288
|
- `up` / `down` are **symmetric** (drop in `down` what `up` added). Column adds can be grouped with
|
|
275
289
|
`Promise.all`, but drops are run as one raw statement per column, sequentially.
|
|
276
290
|
- Put a `/* ... */` block at the top of the file describing the **intent** (ticket number, spec
|
|
@@ -305,8 +319,8 @@ module.exports = {
|
|
|
305
319
|
) {
|
|
306
320
|
// removeColumn does not work on MariaDB, so drop via raw SQL DROP COLUMN.
|
|
307
321
|
await queryInterface.sequelize.query(`
|
|
308
|
-
ALTER TABLE
|
|
309
|
-
DROP COLUMN
|
|
322
|
+
ALTER TABLE ${queryInterface.quoteIdentifier(TABLE_NAME)}
|
|
323
|
+
DROP COLUMN ${queryInterface.quoteIdentifier(COLUMN_NAME)}
|
|
310
324
|
`)
|
|
311
325
|
},
|
|
312
326
|
}
|
|
@@ -333,4 +347,5 @@ scripts:
|
|
|
333
347
|
- [indexes.md](./references/indexes.md) — index naming, UNIQUE, `Promise.all`, composite indexes,
|
|
334
348
|
the shortening algorithm for long names with a table of real examples (§4)
|
|
335
349
|
- [alter-table.md](./references/alter-table.md) — `addColumn`, dropping a column with raw
|
|
336
|
-
`DROP COLUMN` (because `removeColumn` fails
|
|
350
|
+
`DROP COLUMN` on MariaDB (because `removeColumn` fails there) and with `removeColumn` on
|
|
351
|
+
PostgreSQL, identifier quoting, symmetric up/down, intent comment (§5)
|
|
@@ -4,15 +4,21 @@ How to write migrations that add or remove columns on an existing table. Referen
|
|
|
4
4
|
`SKILL.md`. The column type / `field` / `allowNull` notation is shared with
|
|
5
5
|
[notation.md](./notation.md).
|
|
6
6
|
|
|
7
|
-
## Add with addColumn, drop with raw SQL DROP COLUMN (symmetric up/down)
|
|
7
|
+
## Add with addColumn, drop with raw SQL DROP COLUMN on MariaDB (symmetric up/down)
|
|
8
8
|
|
|
9
9
|
`addColumn` in `up`, and drop the same column in `down` to keep them symmetric. The filename is
|
|
10
10
|
`{timestamp}-{seq}-alter_table-<table>-<column>.cjs`.
|
|
11
11
|
|
|
12
|
-
**
|
|
13
|
-
on MariaDB
|
|
14
|
-
`ALTER TABLE ... DROP COLUMN` via `queryInterface.sequelize.query`. Quote
|
|
15
|
-
(
|
|
12
|
+
**On MariaDB, do not use `removeColumn` to drop a column.** Sequelize's `removeColumn` does not work
|
|
13
|
+
correctly on MariaDB, so when MariaDB is the live database, drop a column (in `up` or `down`) with
|
|
14
|
+
raw SQL `ALTER TABLE ... DROP COLUMN` via `queryInterface.sequelize.query`. Quote every identifier
|
|
15
|
+
with `queryInterface.quoteIdentifier()`, never with a hand-written backtick (see
|
|
16
|
+
[Quoting identifiers](#quoting-identifiers-in-raw-sql)).
|
|
17
|
+
|
|
18
|
+
**On PostgreSQL (and SQLite), `queryInterface.removeColumn` works.** A project whose databases are
|
|
19
|
+
only those two drops a column with `removeColumn(TABLE_NAME, COLUMN_NAME)` in `up` or `down` and
|
|
20
|
+
writes no raw SQL. The raw form below stays the one to write whenever MariaDB is among the
|
|
21
|
+
databases, because it runs unchanged on all of them.
|
|
16
22
|
|
|
17
23
|
```js
|
|
18
24
|
'use strict'
|
|
@@ -43,8 +49,8 @@ module.exports = {
|
|
|
43
49
|
) {
|
|
44
50
|
// removeColumn does not work on MariaDB, so drop via raw SQL DROP COLUMN.
|
|
45
51
|
await queryInterface.sequelize.query(`
|
|
46
|
-
ALTER TABLE
|
|
47
|
-
DROP COLUMN
|
|
52
|
+
ALTER TABLE ${queryInterface.quoteIdentifier(TABLE_NAME)}
|
|
53
|
+
DROP COLUMN ${queryInterface.quoteIdentifier(COLUMN_NAME)}
|
|
48
54
|
`)
|
|
49
55
|
},
|
|
50
56
|
}
|
|
@@ -57,6 +63,13 @@ module.exports = {
|
|
|
57
63
|
dialect-independent, run `ALTER TABLE ... DROP COLUMN` directly. The add side (`addColumn`) is
|
|
58
64
|
fine, so keep using it.
|
|
59
65
|
|
|
66
|
+
### Quoting identifiers in raw SQL
|
|
67
|
+
|
|
68
|
+
`queryInterface.quoteIdentifier(name)` returns the name quoted the way the connected dialect
|
|
69
|
+
expects: backticks on MariaDB, MySQL and SQLite, double quotes on PostgreSQL. A hand-written
|
|
70
|
+
backtick runs on the first three and is a syntax error on PostgreSQL, so a migration that has a
|
|
71
|
+
backtick in it cannot be moved to PostgreSQL without being rewritten.
|
|
72
|
+
|
|
60
73
|
## Write an intent comment (`/* ... */`) at the top
|
|
61
74
|
|
|
62
75
|
A column-adding migration is really about "why is this column being added", so put a `/* ... */`
|
|
@@ -111,16 +124,16 @@ module.exports = {
|
|
|
111
124
|
) {
|
|
112
125
|
// removeColumn does not work on MariaDB, so drop per column with raw SQL DROP COLUMN.
|
|
113
126
|
await queryInterface.sequelize.query(`
|
|
114
|
-
ALTER TABLE
|
|
115
|
-
DROP COLUMN
|
|
127
|
+
ALTER TABLE ${queryInterface.quoteIdentifier(TABLE_NAME)}
|
|
128
|
+
DROP COLUMN ${queryInterface.quoteIdentifier(COLUMN_NAME.WIZARD_ANSWERS_JSON)}
|
|
116
129
|
`)
|
|
117
130
|
await queryInterface.sequelize.query(`
|
|
118
|
-
ALTER TABLE
|
|
119
|
-
DROP COLUMN
|
|
131
|
+
ALTER TABLE ${queryInterface.quoteIdentifier(TABLE_NAME)}
|
|
132
|
+
DROP COLUMN ${queryInterface.quoteIdentifier(COLUMN_NAME.WIZARD_TEMPLATE_ID)}
|
|
120
133
|
`)
|
|
121
134
|
await queryInterface.sequelize.query(`
|
|
122
|
-
ALTER TABLE
|
|
123
|
-
DROP COLUMN
|
|
135
|
+
ALTER TABLE ${queryInterface.quoteIdentifier(TABLE_NAME)}
|
|
136
|
+
DROP COLUMN ${queryInterface.quoteIdentifier(COLUMN_NAME.WIZARD_TEMPLATE_VERSION)}
|
|
124
137
|
`)
|
|
125
138
|
|
|
126
139
|
return Promise.resolve()
|
|
@@ -42,6 +42,9 @@ CustomerId: {
|
|
|
42
42
|
DB FK constraint (1) pins insert / delete order, (2) makes cloning into backup (`*_bk`) tables
|
|
43
43
|
hard, and (3) cannot express circular references. Referential integrity is enforced in the **app
|
|
44
44
|
layer** (model associations and domain logic).
|
|
45
|
+
- **PostgreSQL gets the same rule.** The three costs belong to the constraint itself, and PostgreSQL
|
|
46
|
+
enforces a constraint as strictly as MariaDB and MySQL do, so a project on PostgreSQL writes no
|
|
47
|
+
`references` either.
|
|
45
48
|
|
|
46
49
|
## Create the FK *column*; express the relation on the model side
|
|
47
50
|
|
|
@@ -88,8 +91,8 @@ ContentGenerationJobId: {
|
|
|
88
91
|
## A set of ids you don't want as FKs → a JSON array
|
|
89
92
|
|
|
90
93
|
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
|
|
92
|
-
integrity in the app layer.
|
|
94
|
+
hold them in a `JSON`-typed array column (`JSONB` on PostgreSQL). Same idea as having no FK
|
|
95
|
+
constraint: check referential integrity in the app layer.
|
|
93
96
|
|
|
94
97
|
```js
|
|
95
98
|
// An array of ids without an FK.
|
|
@@ -70,8 +70,10 @@ name: [SHORT_TABLE_NAME, 'job_package', 'unique'].join('_')
|
|
|
70
70
|
|
|
71
71
|
## Shortening a long index name (over ~50 chars)
|
|
72
72
|
|
|
73
|
-
The
|
|
74
|
-
|
|
73
|
+
The identifier limit is 64 characters on MariaDB and MySQL and 63 bytes on PostgreSQL, so write
|
|
74
|
+
for the stricter 63. SQLite sets none. As a **safety margin, shorten once a name would run past
|
|
75
|
+
~50 characters**. Index names are ASCII snake_case, so a byte is a character. Shorten in stages, in
|
|
76
|
+
the **following priority order**.
|
|
75
77
|
|
|
76
78
|
### Priority 1: shorten the column name(s) first (keep the table name in full)
|
|
77
79
|
|
|
@@ -134,12 +136,49 @@ character** of each word.
|
|
|
134
136
|
from the `addIndex` `name` (do not inline). If abbreviations collide within a table, add a second
|
|
135
137
|
letter (etc.) to keep them unique while staying meaningful.
|
|
136
138
|
|
|
139
|
+
### What the database does with a name that is too long or taken
|
|
140
|
+
|
|
141
|
+
| Database | Over-long name | Same name on two tables |
|
|
142
|
+
| --- | --- | --- |
|
|
143
|
+
| MariaDB / MySQL | error past 64 characters | allowed, names are unique per table |
|
|
144
|
+
| PostgreSQL | cut to 63 bytes with a NOTICE, no error | error `relation ... already exists`, names are unique per schema |
|
|
145
|
+
| SQLite | no limit | error, names are unique per database |
|
|
146
|
+
|
|
147
|
+
- **PostgreSQL's silent cut is the trap.** Two names that differ only after byte 63 become the same
|
|
148
|
+
name, and the second `addIndex` fails with `already exists` although both names looked distinct in
|
|
149
|
+
the file. Shortening to ~50 characters keeps that from happening.
|
|
150
|
+
- **The `<table>_` part is what keeps names unique across tables** on PostgreSQL and SQLite. Keep
|
|
151
|
+
it in full (Priority 1 above), and when `SHORT_TABLE_NAME` is needed, check that the abbreviation
|
|
152
|
+
is unique across the whole schema, not only within the file. If abbreviations collide, add a
|
|
153
|
+
second letter.
|
|
154
|
+
|
|
137
155
|
### Shorten only when needed
|
|
138
156
|
|
|
139
157
|
If it fits within ~50 characters, do not shorten. `.join('_')` the full `TABLE_NAME` / `COLUMN_NAME`
|
|
140
158
|
as in `customers_registered_at_index`. **Do not mechanically initialize just because you can** (it
|
|
141
159
|
hurts readability).
|
|
142
160
|
|
|
161
|
+
## A UNIQUE index compares with the column's collation
|
|
162
|
+
|
|
163
|
+
A UNIQUE index rejects what the column's collation calls equal. On MariaDB and MySQL with a `_ci`
|
|
164
|
+
collation, `Taro` and `taro` are duplicates, and the second insert fails. PostgreSQL compares
|
|
165
|
+
case-sensitively by default, so both rows fit and a case-insensitive natural key (an email, a login
|
|
166
|
+
name) is not enforced. On PostgreSQL, normalize the value to lower case before it is saved, or
|
|
167
|
+
index `lower(<column>)`, instead of relying on the collation. `addIndex` may not accept an
|
|
168
|
+
expression, so write the index as raw SQL, guarded for PostgreSQL:
|
|
169
|
+
|
|
170
|
+
```js
|
|
171
|
+
const INDEX_NAME = 'customers_email_lower_unique'
|
|
172
|
+
|
|
173
|
+
if (queryInterface.sequelize.getDialect() === 'postgres') {
|
|
174
|
+
await queryInterface.sequelize.query(
|
|
175
|
+
`CREATE UNIQUE INDEX ${INDEX_NAME} ON ${queryInterface.quoteIdentifier(TABLE_NAME)} (lower(email))`
|
|
176
|
+
)
|
|
177
|
+
}
|
|
178
|
+
```
|
|
179
|
+
|
|
180
|
+
`down` runs `DROP INDEX IF EXISTS ${INDEX_NAME}` under the same guard.
|
|
181
|
+
|
|
143
182
|
## A UNIQUE named index is "the real constraint"
|
|
144
183
|
|
|
145
184
|
The uniqueness of a 1:1 relation's FK or a natural key is **actually enforced by this UNIQUE named
|
|
@@ -177,7 +177,7 @@ role: {
|
|
|
177
177
|
| `TEXT` / `TEXT('medium')` | long text (body, message, error). For an item that can grow long, use `TEXT('medium')` |
|
|
178
178
|
| `DATE(3)` | millisecond-precision datetime (`registeredAt` / `expiresAt`, etc.) |
|
|
179
179
|
| `BOOLEAN` | truth value (`isActive`, etc.) |
|
|
180
|
-
| `JSON` | structured data (`resultJson`; an FK-less id array `optionIdsJson`) |
|
|
180
|
+
| `JSON` | structured data (`resultJson`; an FK-less id array `optionIdsJson`). On PostgreSQL use `JSONB`, because `json` has no equality operator |
|
|
181
181
|
| `DECIMAL(p, s)` | money / rate (`dailyRate` as `DECIMAL(14, 2)`, etc.) |
|
|
182
182
|
| `BLOB('long')` | binary (uploaded file body) |
|
|
183
183
|
| `ENUM(...)` | **avoid by default**; use a domain constant + `STRING(n)` |
|
|
@@ -214,5 +214,6 @@ Express relations with Sequelize associations **only**. Do not add DB foreign-ke
|
|
|
214
214
|
(`hor-sequelize-migration`).
|
|
215
215
|
|
|
216
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.
|
|
217
|
+
Not adding constraints keeps migration ordering, record deletion, and sqlite tests simple. The
|
|
218
|
+
rule is the same on MariaDB, MySQL and PostgreSQL; sqlite is only the test database. A
|
|
218
219
|
model's `belongsTo` etc. are **declarations for JOIN and include**, independent of DB constraints.
|
|
@@ -116,7 +116,7 @@ Use the `DataTypes` in the table below.
|
|
|
116
116
|
| `TEXT` | long text (body, message) |
|
|
117
117
|
| `DATE(3)` | millisecond-precision datetime (`registeredAt` / `savedAt`, etc.) |
|
|
118
118
|
| `BOOLEAN` | truth value (`isActive`, etc.) |
|
|
119
|
-
| `JSON` | structured data (`questionsJson` / `resultJson`) |
|
|
119
|
+
| `JSON` | structured data (`questionsJson` / `resultJson`). On PostgreSQL use `DataTypes.JSONB`, matching the migration |
|
|
120
120
|
| `DECIMAL(p, s)` | money / rate (`dailyRate`, etc.) |
|
|
121
121
|
| `BLOB('long')` | binary (uploaded file body) |
|
|
122
122
|
| `ENUM(...)` | **avoid by default**. Use a domain constant + `STRING(n)` instead |
|
|
@@ -122,12 +122,17 @@ example — are in [directory-structure.md](./references/directory-structure.md)
|
|
|
122
122
|
always sees its parent row, and deletes don't orphan. (There are no DB FK constraints —
|
|
123
123
|
`hor-sequelize-migration` — but ordering still keeps the data
|
|
124
124
|
coherent and mirrors real usage.)
|
|
125
|
+
- **On PostgreSQL, advance the id sequence after `bulkInsert`.** Explicit ids do not move a
|
|
126
|
+
PostgreSQL sequence (MariaDB, MySQL and SQLite do move their counter), so run
|
|
127
|
+
`setval(pg_get_serial_sequence(<table>, 'id'), MAX(id))` right after the insert, guarded by
|
|
128
|
+
`getDialect() === 'postgres'`.
|
|
125
129
|
- **Async fulfillment**: when a value must be computed (e.g. hashing a password via `Encipher`),
|
|
126
130
|
build the fulfilled array with `await Promise.all(seeds.map(fulfillX))` inside `up` before
|
|
127
131
|
`bulkInsert`; keep the plain `seeds` array for `down`'s id list.
|
|
128
132
|
|
|
129
|
-
The full skeleton, the suite example (parent → child insert / child → parent delete),
|
|
130
|
-
async-fulfillment example (e.g. password hashing) are in
|
|
133
|
+
The full skeleton, the suite example (parent → child insert / child → parent delete), the
|
|
134
|
+
`setval` step for PostgreSQL, and the async-fulfillment example (e.g. password hashing) are in
|
|
135
|
+
[notation.md](./references/notation.md).
|
|
131
136
|
|
|
132
137
|
## 3. Naming
|
|
133
138
|
|
|
@@ -264,8 +269,8 @@ the matching script directly (`npm run db:seed:dev-master`, `db:seed:dev`, or `d
|
|
|
264
269
|
/ development, `.directorykeeper`, the `db:seed:*` scripts and `db:refresh` flow, the re-export
|
|
265
270
|
DRY pattern (§1)
|
|
266
271
|
- [notation.md](./references/notation.md) — file skeleton, `TimestampSeedsSupplier`, `TABLE_NAME`
|
|
267
|
-
string vs object, `seeds` arrays, `bulkInsert` / `bulkDelete`,
|
|
268
|
-
async fulfillment, timestamps (§2, §5)
|
|
272
|
+
string vs object, `seeds` arrays, `bulkInsert` / `bulkDelete`, the PostgreSQL `setval` step,
|
|
273
|
+
suites and insert/delete order, async fulfillment, timestamps (§2, §5)
|
|
269
274
|
- [naming.md](./references/naming.md) — `{timestamp}-{6-digit-seq}-{table_name}.cjs`, timestamp /
|
|
270
275
|
creation-order ordering, `_suite`, release-dir naming (§3)
|
|
271
276
|
- [id-numbering.md](./references/id-numbering.md) — the 10,000-wide id-block rule, the block tables,
|
|
@@ -91,6 +91,65 @@ module.exports = {
|
|
|
91
91
|
- `down` deletes exactly the rows `up` inserted, by their explicit id list
|
|
92
92
|
(`{ id: seeds.map(it => it.id) }`). This is why every row needs a stable, explicit `id`.
|
|
93
93
|
|
|
94
|
+
## PostgreSQL: advance the id sequence after seeding explicit ids
|
|
95
|
+
|
|
96
|
+
MariaDB, MySQL and SQLite move an `AUTO_INCREMENT` counter past an explicit id on insert.
|
|
97
|
+
**PostgreSQL does not**: the sequence behind a `BIGSERIAL` id stays at its start, because
|
|
98
|
+
`bulkInsert` with explicit ids never calls `nextval()`. The first row the app creates then asks the
|
|
99
|
+
sequence for `1`, which seeded row `1` already holds, and the insert fails with
|
|
100
|
+
`duplicate key value violates unique constraint "<table>_pkey"`. Master ids `1`...`7` collide at
|
|
101
|
+
once; a `100001` block collides after 100,000 inserts. Advance the sequence for every seeded table.
|
|
102
|
+
|
|
103
|
+
```js
|
|
104
|
+
module.exports = {
|
|
105
|
+
up: async (queryInterface, Sequelize) => {
|
|
106
|
+
await queryInterface.bulkInsert(TABLE_NAME, TimestampSeedsSupplier.supplyAll(seeds), {})
|
|
107
|
+
|
|
108
|
+
if (queryInterface.sequelize.getDialect() === 'postgres') {
|
|
109
|
+
await queryInterface.sequelize.query(`
|
|
110
|
+
SELECT setval(
|
|
111
|
+
pg_get_serial_sequence('${queryInterface.quoteIdentifier(TABLE_NAME)}', 'id'),
|
|
112
|
+
(SELECT MAX(id) FROM ${queryInterface.quoteIdentifier(TABLE_NAME)})
|
|
113
|
+
)
|
|
114
|
+
`)
|
|
115
|
+
}
|
|
116
|
+
},
|
|
117
|
+
|
|
118
|
+
down: async (queryInterface, Sequelize) => {
|
|
119
|
+
await queryInterface.bulkDelete(TABLE_NAME, { id: seeds.map(it => it.id) })
|
|
120
|
+
},
|
|
121
|
+
}
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
- **`MAX(id)` of the whole table, not of `seeds`**, so a table that already holds a larger id is not
|
|
125
|
+
moved backwards. The next generated id is `MAX(id) + 1`.
|
|
126
|
+
- **The sequence name is passed as quoted identifier text** (`'"Table"'`), so a mixed-case table name
|
|
127
|
+
resolves the same way as it does in the `MAX(id)` subquery.
|
|
128
|
+
- **A suite repeats the statement once per table**, right after that table's `bulkInsert`. Its
|
|
129
|
+
`TABLE_NAME` is an object of table names, so each table passes its own entry:
|
|
130
|
+
|
|
131
|
+
```js
|
|
132
|
+
const TABLE_NAME = {
|
|
133
|
+
customer: 'Customers',
|
|
134
|
+
order: 'Orders',
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
const tableNames = Object.values(TABLE_NAME)
|
|
138
|
+
|
|
139
|
+
await Promise.all(
|
|
140
|
+
tableNames.map(tableName =>
|
|
141
|
+
queryInterface.sequelize.query(`
|
|
142
|
+
SELECT setval(
|
|
143
|
+
pg_get_serial_sequence('${queryInterface.quoteIdentifier(tableName)}', 'id'),
|
|
144
|
+
(SELECT MAX(id) FROM ${queryInterface.quoteIdentifier(tableName)})
|
|
145
|
+
)
|
|
146
|
+
`)),
|
|
147
|
+
)
|
|
148
|
+
```
|
|
149
|
+
- **`down` does nothing to the sequence.** Deleted ids are simply skipped; no id is reused.
|
|
150
|
+
- **The guard keeps one file valid on every database.** A project that runs only on PostgreSQL may
|
|
151
|
+
drop the `if`; the dialects without sequences must not run the statement.
|
|
152
|
+
|
|
94
153
|
## Suites: insert parent → child, delete child → parent
|
|
95
154
|
|
|
96
155
|
When a file fills several related tables, insert **parent before child** in `up` (a child's FK
|
|
@@ -3,19 +3,19 @@ name: hor-type-interface
|
|
|
3
3
|
description: >
|
|
4
4
|
Define TypeScript type interfaces in .d.ts declaration files: model interfaces (one file per
|
|
5
5
|
model/table under types/models/, in the global `model` namespace) and resolver input/output
|
|
6
|
-
types (
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
another shared global type.
|
|
6
|
+
types (all in types/graphql.d.ts, in the global `graphql.<category>` namespace, as
|
|
7
|
+
<Resolver>Input and <Resolver>Result). Use this skill whenever the user asks to add or change a
|
|
8
|
+
model interface, a resolver's Input/Result types, or another shared global type.
|
|
10
9
|
---
|
|
11
10
|
|
|
12
11
|
# Type Interface Definition
|
|
13
12
|
|
|
14
13
|
A skill for the **`.d.ts` type declarations** that describe an app's data shapes: the **model
|
|
15
|
-
interfaces** (one per table) and the **resolver input/output types** (
|
|
16
|
-
declared as **ambient global namespaces**, so any JSDoc can reference them (`model.User`,
|
|
17
|
-
`graphql.user.CreateOrderInput`) without an import.
|
|
18
|
-
|
|
14
|
+
interfaces** (one per table) and the **resolver input/output types** (a pair per resolver). Both
|
|
15
|
+
are declared as **ambient global namespaces**, so any JSDoc can reference them (`model.User`,
|
|
16
|
+
`graphql.user.CreateOrderInput`) without an import. Each model gets **its own file**, merged into
|
|
17
|
+
one namespace by TypeScript's declaration merging, and every resolver type lives in the one file
|
|
18
|
+
`types/graphql.d.ts`.
|
|
19
19
|
|
|
20
20
|
> This skill states a **general, project-independent rule** — a refined best practice, not a
|
|
21
21
|
> description of any one project's code, so it need not match a given repo's existing notation. The
|
|
@@ -24,16 +24,20 @@ model, one resolver — merged into a single namespace by TypeScript's declarati
|
|
|
24
24
|
> project's own model and resolver names and never bake its domain concepts or file paths into the
|
|
25
25
|
> rule.
|
|
26
26
|
|
|
27
|
-
## Grand principle: one file per
|
|
27
|
+
## Grand principle: one file per model, one file for resolver types, all in global namespaces
|
|
28
28
|
|
|
29
|
-
Every model
|
|
30
|
-
|
|
31
|
-
|
|
29
|
+
Every model gets its **own `.d.ts` file**, and every resolver type goes into the single file
|
|
30
|
+
`types/graphql.d.ts`. Each file declares types into a **shared global namespace**. TypeScript's
|
|
31
|
+
**declaration merging** combines the same-named `namespace` blocks into one, so the split costs
|
|
32
|
+
nothing at the access site.
|
|
32
33
|
|
|
33
|
-
- **Why one file per
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
34
|
+
- **Why one file per model.** A monolithic `model.d.ts` is a constant merge-conflict point, because
|
|
35
|
+
every model change touches the same file. Splitting to `types/models/<ModelName>.d.ts` means two
|
|
36
|
+
changes collide **only when they touch the same model**. Adding a model = adding a file.
|
|
37
|
+
- **Why one file for resolver types.** A resolver's `Input` / `Result` pair mirrors the schema
|
|
38
|
+
(SDL) one-to-one, so it is written in the same change as the SDL and read beside the other
|
|
39
|
+
resolver types of its category. One file, `types/graphql.d.ts`, is the place to look, and there is
|
|
40
|
+
no per-resolver path to keep in step with the resolver's own file name.
|
|
37
41
|
- **Why it still reads as one namespace.** `declare global { namespace model { … } }` in
|
|
38
42
|
`User.d.ts` and the same block in `Order.d.ts` **merge** into a single `model` namespace. Consumers
|
|
39
43
|
write `model.User` / `model.Order` regardless of which file declared them — the file split is
|
|
@@ -67,7 +71,7 @@ declare global {
|
|
|
67
71
|
- **`declare global { … }`** puts the namespace in the ambient global scope — visible everywhere with
|
|
68
72
|
no import.
|
|
69
73
|
- The **namespace name is fixed by kind**: `model` for model interfaces ([§2](#2-model-interfaces-one-file-per-table)),
|
|
70
|
-
`graphql.<category>` for resolver types ([§3](#3-resolver-inputoutput-types-
|
|
74
|
+
`graphql.<category>` for resolver types ([§3](#3-resolver-inputoutput-types-in-typesgraphqldts)).
|
|
71
75
|
|
|
72
76
|
## 2. Model interfaces (one file per table)
|
|
73
77
|
|
|
@@ -118,15 +122,15 @@ declare global {
|
|
|
118
122
|
- Optionally, a model interface may `extend` a framework base-model interface (if the architecture
|
|
119
123
|
provides one for the common columns); keep that base generic, not a hard-coded app path.
|
|
120
124
|
|
|
121
|
-
## 3. Resolver input/output types (
|
|
125
|
+
## 3. Resolver input/output types (in `types/graphql.d.ts`)
|
|
122
126
|
|
|
123
|
-
|
|
124
|
-
`
|
|
125
|
-
|
|
126
|
-
|
|
127
|
+
Every resolver's types go into the one file `types/graphql.d.ts`, declared into
|
|
128
|
+
`namespace graphql.<category>`, where `<category>` is the API/endpoint group (`user`, `admin`,
|
|
129
|
+
`portal`, …). Each resolver declares its **`<Resolver>Input`** and its **`<Resolver>Result`** (the
|
|
130
|
+
output / response), written as a pair.
|
|
127
131
|
|
|
128
132
|
```ts
|
|
129
|
-
// types/
|
|
133
|
+
// types/graphql.d.ts
|
|
130
134
|
export {}
|
|
131
135
|
|
|
132
136
|
declare global {
|
|
@@ -140,18 +144,31 @@ declare global {
|
|
|
140
144
|
order: model.Order
|
|
141
145
|
}
|
|
142
146
|
}
|
|
147
|
+
|
|
148
|
+
namespace graphql.admin {
|
|
149
|
+
interface OrdersInput {
|
|
150
|
+
limit: number
|
|
151
|
+
offset: number
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
interface OrdersResult {
|
|
155
|
+
orders: model.Order[]
|
|
156
|
+
}
|
|
157
|
+
}
|
|
143
158
|
}
|
|
144
159
|
```
|
|
145
160
|
|
|
146
161
|
- **Input** is the resolver's argument shape; **Result** is what it returns (the response). A
|
|
147
162
|
query uses the same `Input` / `Result` pairing.
|
|
163
|
+
- **One `namespace graphql.<category>` block per category**, inside the single `declare global`.
|
|
164
|
+
Keep a resolver's `Input` and `Result` next to each other.
|
|
148
165
|
- **Reuse model interfaces in the output** — a Result field is typed as `model.<ModelName>`
|
|
149
166
|
(`order: model.Order`). The `model` and `graphql.<category>` namespaces are both global, so one
|
|
150
167
|
references the other with no import.
|
|
151
|
-
- **Resolver-local helper types** (a nested filter input, a row sub-shape)
|
|
152
|
-
|
|
153
|
-
- **Category = the resolver's endpoint group.** The same resolver name under a different endpoint is
|
|
154
|
-
|
|
168
|
+
- **Resolver-local helper types** (a nested filter input, a row sub-shape) sit in the same block,
|
|
169
|
+
beside the pair they belong to, named so they cannot collide with another resolver's.
|
|
170
|
+
- **Category = the resolver's endpoint group.** The same resolver name under a different endpoint is
|
|
171
|
+
a different namespace (`graphql.user.FooInput` vs `graphql.admin.FooInput`), in the same file.
|
|
155
172
|
|
|
156
173
|
## 4. Accessing the types
|
|
157
174
|
|
|
@@ -184,17 +201,16 @@ async resolve ({
|
|
|
184
201
|
| Kind | File | Namespace | Interface names | Access |
|
|
185
202
|
| --- | --- | --- | --- | --- |
|
|
186
203
|
| Model | `types/models/<ModelName>.d.ts` | `model` | `<ModelName>` | `model.<ModelName>` |
|
|
187
|
-
| Resolver | `types/
|
|
204
|
+
| Resolver | `types/graphql.d.ts` | `graphql.<category>` | `<Resolver>Input`, `<Resolver>Result` | `graphql.<category>.<Resolver>Input` |
|
|
188
205
|
|
|
189
|
-
-
|
|
190
|
-
(matching the resolver, camelCase) for resolvers.
|
|
206
|
+
- A model's file base name is `<ModelName>` (PascalCase); resolver types share the one file.
|
|
191
207
|
- Avoid the denied vague identifiers in field names (`data` / `info` / `list` / …); name a field for
|
|
192
208
|
what it holds.
|
|
193
209
|
|
|
194
210
|
## Finishing checklist
|
|
195
211
|
|
|
196
|
-
- [ ] The type lives in
|
|
212
|
+
- [ ] The type lives in the right file: one model → its own `types/models/<ModelName>.d.ts`; a resolver's types → `types/graphql.d.ts` ([§1](#1-the-dts-scaffold)).
|
|
197
213
|
- [ ] File envelope is `export {}` → `declare global` → the correct `namespace` (`model` or `graphql.<category>`).
|
|
198
214
|
- [ ] Model interface is 1:1 with a table; fields mirror columns; nullable is `| null`; a foreign-key field starts uppercase ([§2](#2-model-interfaces-one-file-per-table)).
|
|
199
|
-
- [ ]
|
|
215
|
+
- [ ] `types/graphql.d.ts` declares `<Resolver>Input` + `<Resolver>Result` in the resolver's `graphql.<category>` block, reuses `model.<ModelName>` in outputs, and keeps resolver-local helper types beside the pair ([§3](#3-resolver-inputoutput-types-in-typesgraphqldts)).
|
|
200
216
|
- [ ] The `types/**` directory is included in the project's TS config so the ambient declarations resolve.
|
package/docs/skills.ja.md
CHANGED
|
@@ -22,7 +22,7 @@
|
|
|
22
22
|
| スキル | 概要 |
|
|
23
23
|
| :-- | :-- |
|
|
24
24
|
| `hor-graphql-schema` | renchan サーバの GraphQL SDL(`.graphql`)を書きます。オーディエンス別スキーマ、ドメイン別の番号付きファイル、カスタムスカラー、命名・null 許容・enum・ページネーションの規約。 |
|
|
25
|
-
| `hor-graphql-server-engine` | エンドポイントごとの `*GraphqlServerEngine` を実装・起動します。URL、スキーマパス、リゾルバディレクトリ、Share/Context の DI
|
|
25
|
+
| `hor-graphql-server-engine` | エンドポイントごとの `*GraphqlServerEngine` を実装・起動します。URL、スキーマパス、リゾルバディレクトリ、Share/Context の DI、認証フィルタ、ミドルウェア、スカラー、エラーコード、WebSocket サーバーの制限。 |
|
|
26
26
|
| `hor-query-resolver` | `BaseQueryResolver` を継承した GraphQL Query リゾルバを書きます。ページネーション、関連の include、ドメインエラーの throw、actual と stub のペア。 |
|
|
27
27
|
| `hor-mutation-resolver` | `BaseMutationResolver` を継承した GraphQL Mutation リゾルバを実装します。状態を変更する操作と、それが動く単一トランザクション。 |
|
|
28
28
|
| `hor-subscription-resolver` | GraphQL subscription リゾルバを実装します。操作の宣言、購読者ごとのチャンネルのスコープ、購読可否のゲート、イベントを push する publish 側の配線。 |
|
|
@@ -34,13 +34,14 @@
|
|
|
34
34
|
|
|
35
35
|
| スキル | 概要 |
|
|
36
36
|
| :-- | :-- |
|
|
37
|
-
| `hor-restfulapi-architecture` | renchan バックエンドの REST 層。`server/restfulapi/` 配下のレンダラーアーキテクチャ、ルートとバージョン、`render()
|
|
37
|
+
| `hor-restfulapi-architecture` | renchan バックエンドの REST 層。`server/restfulapi/` 配下のレンダラーアーキテクチャ、ルートとバージョン、`render()`、レスポンス/エラーハッシュ、認証フィルタ、フラッシャー、リクエストが運ぶ認証情報の読み取り(Cookie、Bearer、IAP JWT、HMAC)、IAP の背後で動くアプリケーション。 |
|
|
38
38
|
|
|
39
39
|
### 認証
|
|
40
40
|
|
|
41
41
|
| スキル | 概要 |
|
|
42
42
|
| :-- | :-- |
|
|
43
|
-
| `hor-cookie-authentication` | renchan
|
|
43
|
+
| `hor-cookie-authentication` | renchan バックエンドのアクター別のパスワードサインインと Cookie ベースのセッション。認証情報とトークンのモデル、アクセストークンとローテーションするリフレッシュトークン(再利用検知つき)、HttpOnly のリフレッシュ Cookie、signIn / signUp / signOut / renewAccessToken の各リゾルバー。 |
|
|
44
|
+
| `hor-oidc-sign-in` | 外部の OpenID Connect プロバイダ経由でアクターをサインインさせます。PKCE つき認可コードフロー、state と nonce、ID トークンの検証、セッションを開く前のプロバイダの subject とアクターの紐付け。 |
|
|
44
45
|
|
|
45
46
|
### 実行場所
|
|
46
47
|
|
|
@@ -56,7 +57,7 @@
|
|
|
56
57
|
| :-- | :-- |
|
|
57
58
|
| `hor-ai-agent-structure` | `mentsu-agent-loop-core` 上にアプリ側 AI エージェントを構成します。`app/agents/<name>/` に `ProceduralAgentLoop` サブクラスとステップごとの `BaseAgentAction` サブクラスを置きます。 |
|
|
58
59
|
| `hor-agent-loop` | `@openreachtech/mentsu-agent-loop` の 3 パッケージで LLM エージェントループを構築します。core(反復エンジン)、BullMQ ジョブ実行、GraphQL の起動 mutation と進捗 subscription。 |
|
|
59
|
-
| `hor-multi-llm-provider` | Claude / OpenAI / Gemini を 1
|
|
60
|
+
| `hor-multi-llm-provider` | Claude / OpenAI / Gemini を 1 つの抽象の下で扱います。抽象モデルプロセッサ、ベンダーごとの基底、モデルごとの具象クラス、モデル名で選択するローダー、モデルへの到達方法ごとのアクセスルート(ベンダー API キー、サービスアカウントによる Vertex AI、Amazon Bedrock)。 |
|
|
60
61
|
| `hor-light-rag` | ベクタ DB なしで AI エージェント向けの軽量 RAG を追加します。ローカル埋め込みによる vector-first / LLM フォールバックのランキングと、MySQL の n-gram 全文検索インデックス。 |
|
|
61
62
|
| `hor-ai-prompt-document-store` | エージェントの設定・指示文・ドキュメント・ツールスキーマをコードに埋め込まず DB に保持し、リクエスト時にプロンプトへ組み立て、バックアップテーブルでバージョン管理します。 |
|
|
62
63
|
|
|
@@ -70,7 +71,7 @@
|
|
|
70
71
|
|
|
71
72
|
| スキル | 概要 |
|
|
72
73
|
| :-- | :-- |
|
|
73
|
-
| `hor-type-interface` | `.d.ts` で型インターフェースを定義します。`types/models/` のモデルインターフェース(global `model`)と、`types/
|
|
74
|
+
| `hor-type-interface` | `.d.ts` で型インターフェースを定義します。`types/models/` のモデルインターフェース(global `model`)と、`types/graphql.d.ts` のリゾルバ Input/Result 型(global `graphql.<category>`)。 |
|
|
74
75
|
| `hor-constant-definition` | アプリ定数は必ず 2 ファイルで定義します。`constants/` の CommonJS マスター(単一の情報源)と、それを再 export する `app/constants/` の ESM ブリッジ。 |
|
|
75
76
|
|
|
76
77
|
### 設計パターン
|