sizuku 0.0.7 → 0.2.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.
Files changed (228) hide show
  1. package/README.md +96 -344
  2. package/dist/cli/main.d.ts +10 -0
  3. package/dist/cli/main.js +61 -0
  4. package/dist/cli.d.ts +2 -0
  5. package/dist/cli.js +51 -0
  6. package/dist/config/index.d.ts +18 -0
  7. package/dist/config/index.js +13 -0
  8. package/dist/config/loader.d.ts +19 -0
  9. package/dist/config/loader.js +30 -0
  10. package/dist/generator/engine.d.ts +3 -0
  11. package/dist/generator/engine.js +63 -0
  12. package/dist/generator/mermaid-er/config/index.d.ts +4 -4
  13. package/dist/generator/mermaid-er/config/index.js +4 -10
  14. package/dist/generator/mermaid-er/core/extract-relations.d.ts +1 -1
  15. package/dist/generator/mermaid-er/core/extract-relations.js +3 -6
  16. package/dist/generator/mermaid-er/core.d.ts +6 -0
  17. package/dist/generator/mermaid-er/core.js +54 -0
  18. package/dist/generator/mermaid-er/generator/er-content.d.ts +20 -0
  19. package/dist/generator/mermaid-er/generator/{generate-er-content.js → er-content.js} +3 -7
  20. package/dist/generator/mermaid-er/generator/index.d.ts +2 -0
  21. package/dist/generator/mermaid-er/generator/index.js +2 -0
  22. package/dist/generator/mermaid-er/generator/relation-line.d.ts +12 -0
  23. package/dist/generator/mermaid-er/generator/{generate-relation-line.js → relation-line.js} +3 -6
  24. package/dist/generator/mermaid-er/generator.d.ts +3 -0
  25. package/dist/generator/mermaid-er/generator.js +14 -0
  26. package/dist/generator/mermaid-er/index.d.ts +12 -5
  27. package/dist/generator/mermaid-er/index.js +30 -74
  28. package/dist/generator/mermaid-er/relationship/build-relation-line.js +3 -6
  29. package/dist/generator/mermaid-er/types.js +1 -0
  30. package/dist/generator/mermaid-er/validator/index.d.ts +8 -0
  31. package/dist/generator/mermaid-er/validator/index.js +71 -0
  32. package/dist/generator/mermaid-er/validator/is-relationship.d.ts +1 -1
  33. package/dist/generator/mermaid-er/validator/is-relationship.js +1 -4
  34. package/dist/generator/mermaid-er/validator/parse-relation-line.js +1 -4
  35. package/dist/generator/mermaid-er/validator/parse-table-info.d.ts +1 -7
  36. package/dist/generator/mermaid-er/validator/parse-table-info.js +69 -89
  37. package/dist/generator/mermaid-er/validator/remove-duplicate-relations.js +1 -4
  38. package/dist/generator/valibot/config/index.d.ts +2 -2
  39. package/dist/generator/valibot/config/index.js +6 -12
  40. package/dist/generator/valibot/core/extract-schema.d.ts +2 -5
  41. package/dist/generator/valibot/core/extract-schema.js +162 -81
  42. package/dist/generator/valibot/core.d.ts +5 -0
  43. package/dist/generator/valibot/core.js +39 -0
  44. package/dist/generator/valibot/generator/infer-input.d.ts +5 -0
  45. package/dist/generator/valibot/generator/infer-input.js +8 -0
  46. package/dist/generator/valibot/generator/relation-valibot-code.d.ts +13 -0
  47. package/dist/generator/valibot/generator/relation-valibot-code.js +19 -0
  48. package/dist/generator/valibot/generator/valibot-code.d.ts +15 -0
  49. package/dist/generator/valibot/generator/valibot-code.js +16 -0
  50. package/dist/generator/valibot/generator/valibot.d.ts +14 -0
  51. package/dist/generator/valibot/generator/valibot.js +15 -0
  52. package/dist/generator/valibot/generator.d.ts +3 -0
  53. package/dist/generator/valibot/generator.js +14 -0
  54. package/dist/generator/valibot/index.d.ts +14 -3
  55. package/dist/generator/valibot/index.js +46 -73
  56. package/dist/generator/zod/config/index.d.ts +2 -2
  57. package/dist/generator/zod/config/index.js +6 -12
  58. package/dist/generator/zod/core/extract-schema.d.ts +1 -2
  59. package/dist/generator/zod/core/extract-schema.js +228 -81
  60. package/dist/generator/zod/core.d.ts +5 -0
  61. package/dist/generator/zod/core.js +39 -0
  62. package/dist/generator/zod/generator/infer.d.ts +5 -0
  63. package/dist/generator/zod/generator/infer.js +8 -0
  64. package/dist/generator/zod/generator/relation-zod-code.d.ts +13 -0
  65. package/dist/generator/zod/generator/relation-zod-code.js +19 -0
  66. package/dist/generator/zod/generator/zod-code.d.ts +17 -0
  67. package/dist/generator/zod/generator/zod-code.js +18 -0
  68. package/dist/generator/zod/generator/zod.d.ts +16 -0
  69. package/dist/generator/zod/generator/zod.js +16 -0
  70. package/dist/generator/zod/generator.d.ts +3 -0
  71. package/dist/generator/zod/generator.js +14 -0
  72. package/dist/generator/zod/index.d.ts +15 -3
  73. package/dist/generator/zod/index.js +50 -73
  74. package/dist/index.d.ts +8 -0
  75. package/dist/index.js +71 -0
  76. package/dist/shared/config/index.d.ts +13 -0
  77. package/dist/{common → shared}/config/index.js +1 -4
  78. package/dist/shared/format/index.d.ts +15 -0
  79. package/dist/shared/format/index.js +24 -0
  80. package/dist/shared/fs/index.d.ts +7 -0
  81. package/dist/shared/fs/index.js +16 -0
  82. package/dist/shared/fsp/index.d.ts +27 -0
  83. package/dist/shared/fsp/index.js +38 -0
  84. package/dist/shared/generator/field-definitions.d.ts +12 -0
  85. package/dist/shared/generator/field-definitions.js +12 -0
  86. package/dist/shared/helper/ast-parser.d.ts +3 -0
  87. package/dist/shared/helper/ast-parser.js +202 -0
  88. package/dist/shared/helper/build-schema-extractor.d.ts +25 -0
  89. package/dist/shared/helper/build-schema-extractor.js +33 -0
  90. package/dist/shared/helper/create-extract-field-from-property.d.ts +15 -0
  91. package/dist/shared/helper/create-extract-field-from-property.js +20 -0
  92. package/dist/shared/helper/create-extract-fields-from-call-expression.d.ts +14 -0
  93. package/dist/shared/helper/create-extract-fields-from-call-expression.js +14 -0
  94. package/dist/shared/helper/create-extract-relation-field-from-property.d.ts +12 -0
  95. package/dist/shared/helper/create-extract-relation-field-from-property.js +27 -0
  96. package/dist/shared/helper/extract-schemas.d.ts +133 -0
  97. package/dist/shared/helper/extract-schemas.js +445 -0
  98. package/dist/shared/helper/file-writer.d.ts +3 -0
  99. package/dist/shared/helper/file-writer.js +25 -0
  100. package/dist/shared/helper/find-object-literal-expression.d.ts +12 -0
  101. package/dist/shared/helper/find-object-literal-expression.js +31 -0
  102. package/dist/shared/helper/find-object-literalIn-args.d.ts +2 -0
  103. package/dist/shared/helper/find-object-literalIn-args.js +8 -0
  104. package/dist/shared/helper/is-relation-function.d.ts +10 -0
  105. package/dist/shared/helper/is-relation-function.js +16 -0
  106. package/dist/shared/types.js +1 -0
  107. package/dist/{common/text → shared/utils}/capitalize.js +1 -4
  108. package/dist/shared/utils/compose.d.ts +101 -0
  109. package/dist/shared/utils/compose.js +124 -0
  110. package/dist/shared/utils/file.d.ts +92 -0
  111. package/dist/shared/utils/file.js +177 -0
  112. package/dist/shared/utils/functional.d.ts +118 -0
  113. package/dist/shared/utils/functional.js +96 -0
  114. package/dist/shared/utils/index.d.ts +20 -0
  115. package/dist/shared/utils/index.js +48 -0
  116. package/dist/shared/utils/string-utils.d.ts +8 -0
  117. package/dist/shared/utils/string-utils.js +28 -0
  118. package/dist/shared/utils/types.d.ts +32 -0
  119. package/dist/shared/utils/types.js +2 -0
  120. package/dist/shared/utils/validation-utils.d.ts +8 -0
  121. package/dist/shared/utils/validation-utils.js +25 -0
  122. package/dist/src/config/index.d.ts +18 -0
  123. package/dist/src/config/index.js +13 -0
  124. package/dist/src/generator/mermaid-er/core/extract-relations.d.ts +8 -0
  125. package/dist/src/generator/mermaid-er/core/extract-relations.js +12 -0
  126. package/dist/src/generator/mermaid-er/generator/er-content.d.ts +9 -0
  127. package/dist/src/generator/mermaid-er/generator/er-content.js +25 -0
  128. package/dist/src/generator/mermaid-er/generator/index.d.ts +2 -0
  129. package/dist/src/generator/mermaid-er/generator/index.js +2 -0
  130. package/dist/src/generator/mermaid-er/generator/relation-line.d.ts +8 -0
  131. package/dist/src/generator/mermaid-er/generator/relation-line.js +14 -0
  132. package/dist/src/generator/mermaid-er/index.d.ts +6 -0
  133. package/dist/src/generator/mermaid-er/index.js +16 -0
  134. package/dist/src/generator/mermaid-er/relationship/build-relation-line.d.ts +15 -0
  135. package/dist/src/generator/mermaid-er/relationship/build-relation-line.js +35 -0
  136. package/dist/src/generator/mermaid-er/types.d.ts +21 -0
  137. package/dist/src/generator/mermaid-er/types.js +1 -0
  138. package/dist/src/generator/mermaid-er/validator/index.d.ts +4 -0
  139. package/dist/src/generator/mermaid-er/validator/index.js +4 -0
  140. package/dist/src/generator/mermaid-er/validator/is-relationship.d.ts +8 -0
  141. package/dist/src/generator/mermaid-er/validator/is-relationship.js +9 -0
  142. package/dist/src/generator/mermaid-er/validator/parse-relation-line.d.ts +13 -0
  143. package/dist/src/generator/mermaid-er/validator/parse-relation-line.js +21 -0
  144. package/dist/src/generator/mermaid-er/validator/parse-table-info.d.ts +8 -0
  145. package/dist/src/generator/mermaid-er/validator/parse-table-info.js +91 -0
  146. package/dist/src/generator/mermaid-er/validator/remove-duplicate-relations.d.ts +7 -0
  147. package/dist/src/generator/mermaid-er/validator/remove-duplicate-relations.js +9 -0
  148. package/dist/src/generator/valibot/generator/infer-input.d.ts +5 -0
  149. package/dist/src/generator/valibot/generator/infer-input.js +8 -0
  150. package/dist/src/generator/valibot/generator/valibot-code.d.ts +14 -0
  151. package/dist/src/generator/valibot/generator/valibot-code.js +16 -0
  152. package/dist/src/generator/valibot/generator/valibot.d.ts +13 -0
  153. package/dist/src/generator/valibot/generator/valibot.js +11 -0
  154. package/dist/src/generator/valibot/index.d.ts +9 -0
  155. package/dist/src/generator/valibot/index.js +34 -0
  156. package/dist/src/generator/zod/generator/infer.d.ts +5 -0
  157. package/dist/src/generator/zod/generator/infer.js +8 -0
  158. package/dist/{generator/zod/generator/generate-zod-code.d.ts → src/generator/zod/generator/zod-code.d.ts} +8 -3
  159. package/dist/src/generator/zod/generator/zod-code.js +18 -0
  160. package/dist/{generator/zod/generator/generate-zod-schema.d.ts → src/generator/zod/generator/zod.d.ts} +8 -4
  161. package/dist/src/generator/zod/generator/zod.js +12 -0
  162. package/dist/src/generator/zod/index.d.ts +10 -0
  163. package/dist/src/generator/zod/index.js +40 -0
  164. package/dist/src/index.d.ts +10 -0
  165. package/dist/src/index.js +35 -0
  166. package/dist/src/shared/format/index.d.ts +2 -0
  167. package/dist/src/shared/format/index.js +10 -0
  168. package/dist/src/shared/fs/index.d.ts +2 -0
  169. package/dist/src/shared/fs/index.js +10 -0
  170. package/dist/src/shared/fsp/index.d.ts +3 -0
  171. package/dist/src/shared/fsp/index.js +8 -0
  172. package/dist/src/shared/generator/field-definitions.d.ts +12 -0
  173. package/dist/src/shared/generator/field-definitions.js +12 -0
  174. package/dist/src/shared/helper/build-schema-extractor.d.ts +25 -0
  175. package/dist/src/shared/helper/build-schema-extractor.js +33 -0
  176. package/dist/src/shared/helper/create-extract-field-from-property.d.ts +15 -0
  177. package/dist/src/shared/helper/create-extract-field-from-property.js +20 -0
  178. package/dist/src/shared/helper/create-extract-fields-from-call-expression.d.ts +14 -0
  179. package/dist/src/shared/helper/create-extract-fields-from-call-expression.js +14 -0
  180. package/dist/src/shared/helper/create-extract-relation-field-from-property.d.ts +12 -0
  181. package/dist/src/shared/helper/create-extract-relation-field-from-property.js +27 -0
  182. package/dist/src/shared/helper/extract-schemas.d.ts +16 -0
  183. package/dist/src/shared/helper/extract-schemas.js +19 -0
  184. package/dist/src/shared/helper/find-object-literal-expression.d.ts +12 -0
  185. package/dist/src/shared/helper/find-object-literal-expression.js +31 -0
  186. package/dist/src/shared/helper/find-object-literalIn-args.d.ts +2 -0
  187. package/dist/src/shared/helper/find-object-literalIn-args.js +8 -0
  188. package/dist/src/shared/helper/is-relation-function.d.ts +10 -0
  189. package/dist/src/shared/helper/is-relation-function.js +16 -0
  190. package/dist/src/shared/utils/index.d.ts +33 -0
  191. package/dist/src/shared/utils/index.js +61 -0
  192. package/dist/utils/index.d.ts +144 -0
  193. package/dist/utils/index.js +301 -0
  194. package/package.json +10 -13
  195. package/dist/common/config/index.d.ts +0 -13
  196. package/dist/common/format/index.d.ts +0 -1
  197. package/dist/common/format/index.js +0 -12
  198. package/dist/common/generator/generate-field-definitions.d.ts +0 -8
  199. package/dist/common/generator/generate-field-definitions.js +0 -16
  200. package/dist/common/helper/get-camel-case-schema-name-helper.d.ts +0 -7
  201. package/dist/common/helper/get-camel-case-schema-name-helper.js +0 -14
  202. package/dist/common/helper/get-pascal-case-schema-name-helper.d.ts +0 -8
  203. package/dist/common/helper/get-pascal-case-schema-name-helper.js +0 -15
  204. package/dist/common/helper/get-variable-name-helper.d.ts +0 -9
  205. package/dist/common/helper/get-variable-name-helper.js +0 -15
  206. package/dist/common/helper/get-variable-schema-name-helper.d.ts +0 -9
  207. package/dist/common/helper/get-variable-schema-name-helper.js +0 -17
  208. package/dist/common/text/decapitalize.d.ts +0 -17
  209. package/dist/common/text/decapitalize.js +0 -22
  210. package/dist/common/type/index.js +0 -2
  211. package/dist/generator/mermaid-er/generator/generate-er-content.d.ts +0 -9
  212. package/dist/generator/mermaid-er/generator/generate-relation-line.d.ts +0 -7
  213. package/dist/generator/mermaid-er/type/index.js +0 -2
  214. package/dist/generator/mermaid-er/validator/is-relation.d.ts +0 -7
  215. package/dist/generator/mermaid-er/validator/is-relation.js +0 -40
  216. package/dist/generator/valibot/generator/generate-valibot-code.d.ts +0 -11
  217. package/dist/generator/valibot/generator/generate-valibot-code.js +0 -21
  218. package/dist/generator/valibot/generator/generate-valibot-infer-input.d.ts +0 -9
  219. package/dist/generator/valibot/generator/generate-valibot-infer-input.js +0 -16
  220. package/dist/generator/valibot/generator/generate-valibot-schema.d.ts +0 -9
  221. package/dist/generator/valibot/generator/generate-valibot-schema.js +0 -16
  222. package/dist/generator/zod/generator/generate-z-infer.d.ts +0 -11
  223. package/dist/generator/zod/generator/generate-z-infer.js +0 -18
  224. package/dist/generator/zod/generator/generate-zod-code.js +0 -21
  225. package/dist/generator/zod/generator/generate-zod-schema.js +0 -17
  226. /package/dist/generator/mermaid-er/{type/index.d.ts → types.d.ts} +0 -0
  227. /package/dist/{common/type/index.d.ts → shared/types.d.ts} +0 -0
  228. /package/dist/{common/text → shared/utils}/capitalize.d.ts +0 -0
package/README.md CHANGED
@@ -21,434 +21,186 @@ npm install -D sizuku
21
21
 
22
22
  ### Example
23
23
 
24
- input:
24
+ Prepare schema.ts:
25
25
 
26
26
  ```ts
27
- import { sql } from 'drizzle-orm'
28
- import { mysqlTable, varchar, timestamp, unique } from 'drizzle-orm/mysql-core'
27
+ import { relations } from 'drizzle-orm'
28
+ import { mysqlTable, varchar } from 'drizzle-orm/mysql-core'
29
29
 
30
30
  export const user = mysqlTable('user', {
31
- /// Unique identifier for the user.
32
- /// @z.string().uuid()
31
+ /// Primary key
32
+ /// @z.uuid()
33
33
  /// @v.pipe(v.string(), v.uuid())
34
34
  id: varchar('id', { length: 36 }).primaryKey(),
35
- /// Username of the user.
36
- /// @z.string()
37
- /// @v.string()
38
- username: varchar('username', { length: 255 }).notNull(),
39
- /// Email address of the user.
40
- /// @z.string().email()
41
- /// @v.pipe(v.string(), v.email())
42
- email: varchar('email', { length: 255 }).notNull().unique(),
43
- /// Password for the user.
44
- /// @z.string().min(8).max(100)
45
- /// @v.pipe(v.string(), v.minLength(8), v.maxLength(100))
46
- password: varchar('password', { length: 100 }).notNull(),
47
- /// Timestamp when the user was created.
48
- /// @z.date()
49
- /// @v.date()
50
- createdAt: timestamp('created_at', { mode: 'string' }).notNull().default(sql`CURRENT_TIMESTAMP`),
51
- /// Timestamp when the user was last updated.
52
- /// @z.date()
53
- /// @v.date()
54
- updatedAt: timestamp('updated_at', { mode: 'string' })
55
- .notNull()
56
- .default(sql`CURRENT_TIMESTAMP`)
57
- .$onUpdate(() => sql`CURRENT_TIMESTAMP`),
35
+ /// Display name
36
+ /// @z.string().min(1).max(50)
37
+ /// @v.pipe(v.string(), v.minLength(1), v.maxLength(50))
38
+ name: varchar('name', { length: 50 }).notNull(),
58
39
  })
59
40
 
60
41
  /// @relation user.id post.userId one-to-many
61
42
  export const post = mysqlTable('post', {
62
- /// Unique identifier for the post.
63
- /// @z.string().uuid()
43
+ /// Primary key
44
+ /// @z.uuid()
64
45
  /// @v.pipe(v.string(), v.uuid())
65
46
  id: varchar('id', { length: 36 }).primaryKey(),
66
- /// ID of the user who created the post.
67
- /// @z.string().uuid()
47
+ /// Article title
48
+ /// @z.string().min(1).max(100)
49
+ /// @v.pipe(v.string(), v.minLength(1), v.maxLength(100))
50
+ title: varchar('title', { length: 100 }).notNull(),
51
+ /// Body content (no length limit)
52
+ /// @z.string().min(1).max(65535)
53
+ /// @v.pipe(v.string(), v.minLength(1), v.maxLength(65535))
54
+ content: varchar('content', { length: 65535 }).notNull(),
55
+ /// Foreign key referencing User.id
56
+ /// @z.uuid()
68
57
  /// @v.pipe(v.string(), v.uuid())
69
- userId: varchar('user_id', { length: 36 })
70
- .notNull()
71
- .references(() => user.id, { onDelete: 'cascade' }),
72
- /// Content of the post.
73
- /// @z.string()
74
- /// @v.string()
75
- content: varchar('content', { length: 500 }).notNull(),
76
- /// Timestamp when the post was created.
77
- /// @z.date()
78
- /// @v.date()
79
- createdAt: timestamp('created_at', { mode: 'string' }).notNull().default(sql`CURRENT_TIMESTAMP`),
80
- /// Timestamp when the post was last updated.
81
- /// @z.date()
82
- /// @v.date()
83
- updatedAt: timestamp('updated_at', { mode: 'string' })
84
- .notNull()
85
- .default(sql`CURRENT_TIMESTAMP`)
86
- .$onUpdate(() => sql`CURRENT_TIMESTAMP`),
58
+ userId: varchar('user_id', { length: 36 }).notNull(),
87
59
  })
88
60
 
89
- /// @relation post.id likes.postId one-to-many
90
- /// @relation user.id likes.userId one-to-many
91
- export const likes = mysqlTable(
92
- 'likes',
93
- {
94
- /// Unique identifier for the like.
95
- /// @z.string().uuid()
96
- /// @v.pipe(v.string(), v.uuid())
97
- id: varchar('id', { length: 36 }).primaryKey(),
98
- /// ID of the post that is liked.
99
- /// @z.string().uuid()
100
- /// @v.pipe(v.string(), v.uuid())
101
- postId: varchar('post_id', { length: 36 })
102
- .notNull()
103
- .references(() => post.id, { onDelete: 'cascade' }),
104
- /// ID of the user who liked the post.
105
- /// @z.string().uuid()
106
- /// @v.pipe(v.string(), v.uuid())
107
- userId: varchar('user_id', { length: 36 })
108
- .notNull()
109
- .references(() => user.id, { onDelete: 'cascade' }),
110
- /// Timestamp when the like was created.
111
- /// @z.date()
112
- /// @v.date()
113
- createdAt: timestamp('created_at', { mode: 'string' })
114
- .notNull()
115
- .default(sql`CURRENT_TIMESTAMP`),
61
+ export const userRelations = relations(user, ({ many }) => ({
62
+ posts: many(post),
63
+ }))
64
+
65
+ export const postRelations = relations(post, ({ one }) => ({
66
+ user: one(user, {
67
+ fields: [post.userId],
68
+ references: [user.id],
69
+ }),
70
+ }))
71
+ ```
72
+
73
+ Prepare sizuku.json:
74
+
75
+ ```json
76
+ {
77
+ "input": "db/schema.ts",
78
+ "zod": {
79
+ "output": "zod/index.ts",
80
+ "comment": true,
81
+ "type": true
82
+ },
83
+ "valibot": {
84
+ "output": "valibot/index.ts",
85
+ "comment": true
116
86
  },
117
- (t) => [unique().on(t.userId, t.postId)],
118
- )
87
+ "mermaid": {
88
+ "output": "mermaid-er/ER.md"
89
+ }
90
+ }
119
91
  ```
120
92
 
121
- ### Zod
93
+ Run Sizuku:
122
94
 
123
- ```bash
124
- npx sizuku-zod path/to/db/schema.ts -o path/to/output.ts
95
+ ```sh
96
+ npx sizuku
125
97
  ```
126
98
 
127
- output:
99
+ ### Zod
128
100
 
129
101
  ```ts
130
- import { z } from 'zod'
102
+ import * as z from 'zod'
131
103
 
132
104
  export const UserSchema = z.object({
133
105
  /**
134
- * Unique identifier for the user.
106
+ * Primary key
135
107
  */
136
- id: z.string().uuid(),
108
+ id: z.uuid(),
137
109
  /**
138
- * Username of the user.
110
+ * Display name
139
111
  */
140
- username: z.string(),
141
- /**
142
- * Email address of the user.
143
- */
144
- email: z.string().email(),
145
- /**
146
- * Password for the user.
147
- */
148
- password: z.string().min(8).max(100),
149
- /**
150
- * Timestamp when the user was created.
151
- */
152
- createdAt: z.date(),
153
- /**
154
- * Timestamp when the user was last updated.
155
- */
156
- updatedAt: z.date(),
112
+ name: z.string().min(1).max(50),
157
113
  })
158
114
 
115
+ export type User = z.infer<typeof UserSchema>
116
+
159
117
  export const PostSchema = z.object({
160
118
  /**
161
- * Unique identifier for the post.
162
- */
163
- id: z.string().uuid(),
164
- /**
165
- * ID of the user who created the post.
119
+ * Primary key
166
120
  */
167
- userId: z.string().uuid(),
121
+ id: z.uuid(),
168
122
  /**
169
- * Content of the post.
123
+ * Article title
170
124
  */
171
- content: z.string(),
125
+ title: z.string().min(1).max(100),
172
126
  /**
173
- * Timestamp when the post was created.
127
+ * Body content (no length limit)
174
128
  */
175
- createdAt: z.date(),
129
+ content: z.string().min(1).max(65535),
176
130
  /**
177
- * Timestamp when the post was last updated.
131
+ * Foreign key referencing User.id
178
132
  */
179
- updatedAt: z.date(),
133
+ userId: z.uuid(),
180
134
  })
181
135
 
182
- export const LikesSchema = z.object({
183
- /**
184
- * Unique identifier for the like.
185
- */
186
- id: z.string().uuid(),
187
- /**
188
- * ID of the post that is liked.
189
- */
190
- postId: z.string().uuid(),
191
- /**
192
- * ID of the user who liked the post.
193
- */
194
- userId: z.string().uuid(),
195
- /**
196
- * Timestamp when the like was created.
197
- */
198
- createdAt: z.date(),
199
- })
200
- ```
136
+ export type Post = z.infer<typeof PostSchema>
201
137
 
202
- ### Valibot
138
+ export const UserRelationsSchema = z.object({ ...UserSchema.shape, posts: z.array(PostSchema) })
203
139
 
204
- ```bash
205
- npx sizuku-valibot path/to/db/schema.ts -o path/to/output.ts
140
+ export type UserRelations = z.infer<typeof UserRelationsSchema>
141
+
142
+ export const PostRelationsSchema = z.object({ ...PostSchema.shape, user: UserSchema })
143
+
144
+ export type PostRelations = z.infer<typeof PostRelationsSchema>
206
145
  ```
207
146
 
208
- output:
147
+ ### Valibot
209
148
 
210
149
  ```ts
211
150
  import * as v from 'valibot'
212
151
 
213
152
  export const UserSchema = v.object({
214
153
  /**
215
- * Unique identifier for the user.
154
+ * Primary key
216
155
  */
217
156
  id: v.pipe(v.string(), v.uuid()),
218
157
  /**
219
- * Username of the user.
220
- */
221
- username: v.string(),
222
- /**
223
- * Email address of the user.
224
- */
225
- email: v.pipe(v.string(), v.email()),
226
- /**
227
- * Password for the user.
228
- */
229
- password: v.pipe(v.string(), v.minLength(8), v.maxLength(100)),
230
- /**
231
- * Timestamp when the user was created.
158
+ * Display name
232
159
  */
233
- createdAt: v.date(),
234
- /**
235
- * Timestamp when the user was last updated.
236
- */
237
- updatedAt: v.date(),
160
+ name: v.pipe(v.string(), v.minLength(1), v.maxLength(50)),
238
161
  })
239
162
 
240
163
  export const PostSchema = v.object({
241
164
  /**
242
- * Unique identifier for the post.
165
+ * Primary key
243
166
  */
244
167
  id: v.pipe(v.string(), v.uuid()),
245
168
  /**
246
- * ID of the user who created the post.
247
- */
248
- userId: v.pipe(v.string(), v.uuid()),
249
- /**
250
- * Content of the post.
251
- */
252
- content: v.string(),
253
- /**
254
- * Timestamp when the post was created.
169
+ * Article title
255
170
  */
256
- createdAt: v.date(),
257
- /**
258
- * Timestamp when the post was last updated.
259
- */
260
- updatedAt: v.date(),
261
- })
262
-
263
- export const LikesSchema = v.object({
264
- /**
265
- * Unique identifier for the like.
266
- */
267
- id: v.pipe(v.string(), v.uuid()),
171
+ title: v.pipe(v.string(), v.minLength(1), v.maxLength(100)),
268
172
  /**
269
- * ID of the post that is liked.
173
+ * Body content (no length limit)
270
174
  */
271
- postId: v.pipe(v.string(), v.uuid()),
175
+ content: v.pipe(v.string(), v.minLength(1), v.maxLength(65535)),
272
176
  /**
273
- * ID of the user who liked the post.
177
+ * Foreign key referencing User.id
274
178
  */
275
179
  userId: v.pipe(v.string(), v.uuid()),
276
- /**
277
- * Timestamp when the like was created.
278
- */
279
- createdAt: v.date(),
280
180
  })
281
- ```
282
181
 
283
- ### Mermaid ER
182
+ export const UserRelationsSchema = v.object({ ...UserSchema.entries, posts: v.array(PostSchema) })
284
183
 
285
- ```bash
286
- npx sizuku-mermaid-er path/to/db/schema.ts -o path/to/output.md
184
+ export const PostRelationsSchema = v.object({ ...PostSchema.entries, user: UserSchema })
287
185
  ```
288
186
 
289
- output:
187
+ ### Mermaid ER
290
188
 
291
189
  ```mermaid
292
190
  erDiagram
293
191
  user ||--}| post : "(id) - (userId)"
294
- post ||--}| likes : "(id) - (postId)"
295
- user ||--}| likes : "(id) - (userId)"
296
192
  user {
297
- varchar id "(PK) Unique identifier for the user."
298
- varchar username "Username of the user."
299
- varchar email "Email address of the user."
300
- varchar password "Password for the user."
301
- timestamp createdAt "Timestamp when the user was created."
302
- timestamp updatedAt "Timestamp when the user was last updated."
193
+ varchar id "(PK) Primary key"
194
+ varchar name "Display name"
303
195
  }
304
196
  post {
305
- varchar id "(PK) Unique identifier for the post."
306
- varchar userId "(FK) ID of the user who created the post."
307
- varchar content "Content of the post."
308
- timestamp createdAt "Timestamp when the post was created."
309
- timestamp updatedAt "Timestamp when the post was last updated."
310
- }
311
- likes {
312
- varchar id "(PK) Unique identifier for the like."
313
- varchar postId "(FK) ID of the post that is liked."
314
- varchar userId "(FK) ID of the user who liked the post."
315
- timestamp createdAt "Timestamp when the like was created."
197
+ varchar id "(PK) Primary key"
198
+ varchar title "Article title"
199
+ varchar content "Body content (no length limit)"
200
+ varchar userId "Foreign key referencing User.id"
316
201
  }
317
202
  ```
318
203
 
319
- ## Configuration
320
-
321
- ### sizuku-zod.json
322
-
323
- #### Schema Options
324
-
325
- | Option | Type | Default | Description |
326
- |--------|------|---------|-------------|
327
- | `name` | `"PascalCase"` \| `"camelCase"` | `"PascalCase"` | Naming convention for generated schema variables |
328
- | `export` | `boolean` | `false` | When true, exports all schema definitions |
329
-
330
- #### Type Options
331
-
332
- | Option | Type | Default | Description |
333
- |--------|------|---------|-------------|
334
- | `name` | `"PascalCase"` \| `"camelCase"` | `"PascalCase"` | Naming convention for generated type definitions |
335
- | `export` | `boolean` | `false` | When true, exports all type definitions |
336
-
337
- #### Comment Options
338
-
339
- | Option | Type | Default | Description |
340
- |--------|------|---------|-------------|
341
- | `name` | `boolean` | `false` | If enabled, includes the element's original name in TSDoc comments. |
342
-
343
- #### Input and Output
344
-
345
- | Option | Type | Default | Description |
346
- |--------|------|---------|-------------|
347
- | `input` | `string` | `""` | Input file path |
348
- | `output` | `string` | `""` | Output file path |
349
-
350
- #### Example
351
-
352
- ```json
353
- {
354
- "schema": {
355
- "name": "PascalCase"
356
- },
357
- "type": {
358
- "name": "PascalCase",
359
- "export": false
360
- },
361
- "comment": true,
362
- "input": "db/schema.ts",
363
- "output": "zod/index.ts"
364
- }
365
- ```
366
-
367
- > **⚠️** When using a configuration file, command line arguments are not required. The configuration file settings take precedence over command line arguments.
368
- >
369
- > When you have configured `sizuku-zod.json`, you can simply run:
370
- > ```bash
371
- > npx sizuku-zod
372
- > ```
373
-
374
- ### sizuku-valibot.json
375
-
376
- #### Schema Options
377
-
378
- | Option | Type | Default | Description |
379
- |----------|---------------------------------|----------------|--------------------------------------------|
380
- | `name` | `"PascalCase"` \| `"camelCase"` | `"PascalCase"` | Naming convention for generated schema variables |
381
- | `export` | `boolean` | `false` | When true, exports all schema definitions |
382
-
383
- #### Type Options
384
-
385
- | Option | Type | Default | Description |
386
- |----------|---------------------------------|----------------|-----------------------------------------|
387
- | `name` | `"PascalCase"` \| `"camelCase"` | `"PascalCase"` | Naming convention for generated type definitions |
388
- | `export` | `boolean` | `false` | When true, exports all type definitions |
389
-
390
- #### Comment Options
391
-
392
- | Option | Type | Default | Description |
393
- |--------|----------------|---------|---------------------------------------------------------------------|
394
- | `name` | `boolean` | `false` | If enabled, includes the element's original name in TSDoc comments. |
395
-
396
- #### Input and Output
397
-
398
- | Option | Type | Default | Description |
399
- |--------|------|-------------|-------------|
400
- | `input` | `string` | `""` | Input file path |
401
- | `output` | `string` | `""` | Output file path |
402
-
403
- #### Examples
404
-
405
- ```json
406
- {
407
- "schema": {
408
- "name": "PascalCase"
409
- },
410
- "type": {
411
- "name": "PascalCase",
412
- "export": false
413
- },
414
- "comment": true,
415
- "input": "db/schema.ts",
416
- "output": "valibot/index.ts"
417
- }
418
- ```
419
-
420
- > **⚠️** When using a configuration file, command line arguments are not required. The configuration file settings take precedence over command line arguments.
421
- >
422
- > When you have configured `sizuku-valibot.json`, you can simply run:
423
- > ```bash
424
- > npx sizuku-valibot
425
- > ```
426
-
427
- ### sizuku-mermaid-er.json
428
-
429
- | Option | Type | Default | Description |
430
- |----------|----------|------------|------------------|
431
- | `input` | `string` | `""` | Input file path |
432
- | `output` | `string` | `""` | Output file path |
433
-
434
- #### Example
435
-
436
- ```json
437
- {
438
- "input": "db/schema.ts",
439
- "output": "mermaid-er/ER.md"
440
- }
441
- ```
442
-
443
- > **⚠️** When using a configuration file, command line arguments are not required. The configuration file settings take precedence over command line arguments.
444
- >
445
- > When you have configured `sizuku-mermaid-er.json`, you can simply run:
446
- > ```bash
447
- > npx sizuku-mermaid-er
448
- > ```
449
-
450
- This project is in **early development** and being maintained by a developer with about 2 years of experience. While I'm doing my best to create a useful tool:
451
-
452
204
  ### ⚠️ WARNING: Potential Breaking Changes Without Notice
453
205
 
454
206
  **This package is in active development and may introduce breaking changes without prior notice.
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env node
2
+ import type { Result } from 'neverthrow';
3
+ import type { Config } from '../src/config/index.js';
4
+ /**
5
+ * Main CLI function that orchestrates the schema generation process.
6
+ *
7
+ * @param config - The configuration object.
8
+ * @returns A Result indicating success or failure.
9
+ */
10
+ export declare function main(config?: Config): Promise<Result<void, Error>>;
@@ -0,0 +1,61 @@
1
+ #!/usr/bin/env node
2
+ import { err, ok, ResultAsync } from 'neverthrow';
3
+ import { getConfig } from '../src/config/index.js';
4
+ import { sizukuMermaidER } from '../src/generator/mermaid-er/index.js';
5
+ import { sizukuValibot } from '../src/generator/valibot/index.js';
6
+ import { sizukuZod } from '../src/generator/zod/index.js';
7
+ import { readFileSync } from '../src/shared/fs/index.js';
8
+ /**
9
+ * Main CLI function that orchestrates the schema generation process.
10
+ *
11
+ * @param config - The configuration object.
12
+ * @returns A Result indicating success or failure.
13
+ */
14
+ export async function main(config = getConfig()) {
15
+ return ResultAsync.fromPromise(Promise.resolve(), () => new Error('init'))
16
+ .andThen(() => {
17
+ if (!config.input) {
18
+ return err(new Error('input is not found'));
19
+ }
20
+ const contentResult = readFileSync(config.input);
21
+ if (contentResult.isErr()) {
22
+ return err(contentResult.error);
23
+ }
24
+ const content = contentResult.value;
25
+ const lines = content.split('\n');
26
+ const codeStart = lines.findIndex((line) => !line.trim().startsWith('import') && line.trim() !== '');
27
+ return ok(lines.slice(codeStart));
28
+ })
29
+ .andThen((code) => {
30
+ if (config.zod?.output) {
31
+ return ResultAsync.fromPromise(sizukuZod(code, config.zod.output, config.zod.comment, config.zod.type, config.zod.zod), (e) => (e instanceof Error ? e : new Error(String(e)))).map(() => {
32
+ console.log(`Generated Zod schema at: ${config.zod?.output}`);
33
+ return code;
34
+ });
35
+ }
36
+ return ok(code);
37
+ })
38
+ .andThen((code) => {
39
+ if (config.valibot?.output) {
40
+ return ResultAsync.fromPromise(sizukuValibot(code, config.valibot.output, config.valibot.comment, config.valibot.type), (e) => (e instanceof Error ? e : new Error(String(e)))).map(() => {
41
+ console.log(`Generated Valibot schema at: ${config.valibot?.output}`);
42
+ return code;
43
+ });
44
+ }
45
+ return ok(code);
46
+ })
47
+ .andThen((code) => {
48
+ if (config.mermaid?.output) {
49
+ return ResultAsync.fromPromise(sizukuMermaidER(code, config.mermaid.output), (e) => e instanceof Error ? e : new Error(String(e))).map(() => {
50
+ console.log(`Generated Mermaid ER at: ${config.mermaid?.output}`);
51
+ });
52
+ }
53
+ return ok(undefined);
54
+ });
55
+ }
56
+ main().then((result) => {
57
+ if (!result.isOk()) {
58
+ console.error(result.error);
59
+ process.exit(1);
60
+ }
61
+ });
package/dist/cli.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env node
2
+ export {};
package/dist/cli.js ADDED
@@ -0,0 +1,51 @@
1
+ #!/usr/bin/env node
2
+ import { resolve } from 'path';
3
+ import { Result, err, ok } from 'neverthrow';
4
+ import { loadConfig, validateConfig } from './config/loader.js';
5
+ import { generateSchemas } from './generator/engine.js';
6
+ const main = async () => {
7
+ try {
8
+ // コマンドライン引数を解析
9
+ const args = process.argv.slice(2);
10
+ if (args.length === 0) {
11
+ return err(new Error('Usage: sizuku <config-file>'));
12
+ }
13
+ const configPath = resolve(args[0]);
14
+ // 設定ファイルを読み込み
15
+ const configResult = await loadConfig(configPath);
16
+ if (configResult.isErr()) {
17
+ return err(configResult.error);
18
+ }
19
+ // 設定をバリデーション
20
+ const validationResult = validateConfig(configResult.value);
21
+ if (validationResult.isErr()) {
22
+ return err(validationResult.error);
23
+ }
24
+ const config = validationResult.value;
25
+ // スキーマ生成を実行
26
+ const generationResult = await generateSchemas(config);
27
+ if (generationResult.isErr()) {
28
+ return err(generationResult.error);
29
+ }
30
+ console.log('✅ Schema generation completed successfully!');
31
+ return ok(undefined);
32
+ }
33
+ catch (error) {
34
+ return err(new Error(`CLI execution failed: ${error instanceof Error ? error.message : String(error)}`));
35
+ }
36
+ };
37
+ // CLI実行
38
+ if (import.meta.url === `file://${process.argv[1]}`) {
39
+ main()
40
+ .then(result => {
41
+ if (result.isErr()) {
42
+ console.error('❌ Error:', result.error.message);
43
+ process.exit(1);
44
+ }
45
+ process.exit(0);
46
+ })
47
+ .catch(error => {
48
+ console.error('❌ Unexpected error:', error instanceof Error ? error.message : String(error));
49
+ process.exit(1);
50
+ });
51
+ }
@@ -0,0 +1,18 @@
1
+ export type Config = {
2
+ input?: `${string}.ts`;
3
+ zod?: {
4
+ output?: `${string}.ts`;
5
+ comment?: boolean;
6
+ type?: boolean;
7
+ zod?: 'v4' | 'mini' | '@hono/zod-openapi';
8
+ };
9
+ valibot?: {
10
+ output?: `${string}.ts`;
11
+ comment?: boolean;
12
+ type?: boolean;
13
+ };
14
+ mermaid?: {
15
+ output?: string;
16
+ };
17
+ };
18
+ export declare function getConfig(): Config;