|
1 | 1 | # @izumisy/kyrage |
2 | 2 |
|
| 3 | +## 1.5.0 |
| 4 | + |
| 5 | +### Minor Changes |
| 6 | + |
| 7 | +- [#131](https://github.com/IzumiSy/kyrage/pull/131) [`23e21eb`](https://github.com/IzumiSy/kyrage/commit/23e21ebb259d895b7546383ba9e2c2a6e480e23c) Thanks [@IzumiSy](https://github.com/IzumiSy)! - Add foreign key constraint merging to table creation operations with inline control option |
| 8 | + |
| 9 | + Kyrage now automatically merges foreign key constraints into `CREATE TABLE` statements alongside existing primary key and unique constraints. This reduces SQL operations and improves migration performance. Added `inline` option to schema builder's `reference()` method for fine-grained control over constraint merging. |
| 10 | + |
| 11 | + **Example schema with inline foreign key:** |
| 12 | + |
| 13 | + ```typescript |
| 14 | + import { column as c, defineTable as t } from "@izumisy/kyrage"; |
| 15 | + |
| 16 | + const users = t("users", { |
| 17 | + id: c("uuid", { primaryKey: true }), |
| 18 | + }); |
| 19 | + |
| 20 | + const posts = t( |
| 21 | + "posts", |
| 22 | + { |
| 23 | + id: c("uuid", { primaryKey: true }), |
| 24 | + userId: c("uuid"), |
| 25 | + }, |
| 26 | + (t) => [ |
| 27 | + // Merged into CREATE TABLE (default: inline: true) |
| 28 | + t.reference("userId", users, "id"), |
| 29 | + ] |
| 30 | + ); |
| 31 | + ``` |
| 32 | + |
| 33 | + **Example with separate constraint:** |
| 34 | + |
| 35 | + ```typescript |
| 36 | + const posts = t( |
| 37 | + "posts", |
| 38 | + { |
| 39 | + id: c("uuid", { primaryKey: true }), |
| 40 | + userId: c("uuid"), |
| 41 | + }, |
| 42 | + (t) => [ |
| 43 | + // Separate ALTER TABLE statement |
| 44 | + t.reference("userId", users, "id", { inline: false }), |
| 45 | + ] |
| 46 | + ); |
| 47 | + ``` |
| 48 | + |
| 49 | +### Patch Changes |
| 50 | + |
| 51 | +- [#131](https://github.com/IzumiSy/kyrage/pull/131) [`23e21eb`](https://github.com/IzumiSy/kyrage/commit/23e21ebb259d895b7546383ba9e2c2a6e480e23c) Thanks [@IzumiSy](https://github.com/IzumiSy)! - Table operations are now sorted to always respect foreign key and other dependency relationships. Independent tables are ordered alphabetically, ensuring stable and predictable output for migrations and SQL generation. |
| 52 | + |
| 53 | + **Example:** |
| 54 | + |
| 55 | + ```typescript |
| 56 | + import { column as c, defineTable as t } from "@izumisy/kyrage"; |
| 57 | + |
| 58 | + const users = t("users", { |
| 59 | + id: c("uuid", { primaryKey: true }), |
| 60 | + name: c("varchar"), |
| 61 | + }); |
| 62 | + |
| 63 | + const posts = t( |
| 64 | + "posts", |
| 65 | + { |
| 66 | + id: c("uuid", { primaryKey: true }), |
| 67 | + userId: c("uuid"), |
| 68 | + content: c("text"), |
| 69 | + }, |
| 70 | + (t) => [ |
| 71 | + t.reference("userId", users, "id"), // posts depends on users |
| 72 | + ] |
| 73 | + ); |
| 74 | + |
| 75 | + const categories = t("categories", { |
| 76 | + id: c("uuid", { primaryKey: true }), |
| 77 | + label: c("varchar"), |
| 78 | + }); |
| 79 | + ``` |
| 80 | + |
| 81 | + The generated SQL will always be: |
| 82 | + |
| 83 | + ```sql |
| 84 | + CREATE TABLE categories ( |
| 85 | + id uuid PRIMARY KEY, |
| 86 | + label varchar |
| 87 | + ); |
| 88 | + |
| 89 | + CREATE TABLE users ( |
| 90 | + id uuid PRIMARY KEY, |
| 91 | + name varchar |
| 92 | + ); |
| 93 | + |
| 94 | + CREATE TABLE posts ( |
| 95 | + id uuid PRIMARY KEY, |
| 96 | + userId uuid, |
| 97 | + content text, |
| 98 | + CONSTRAINT posts_userId_fkey FOREIGN KEY (userId) REFERENCES users(id) |
| 99 | + ); |
| 100 | + ``` |
| 101 | + |
3 | 102 | ## 1.4.0 |
4 | 103 |
|
5 | 104 | ### Minor Changes |
|
0 commit comments