hekireki 0.2.6 → 0.2.7
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/README.md +18 -4
- package/dist/generator/ecto/generator/ecto.js +55 -25
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -191,9 +191,15 @@ erDiagram
|
|
|
191
191
|
```elixir
|
|
192
192
|
defmodule DBSchema.User do
|
|
193
193
|
use Ecto.Schema
|
|
194
|
-
|
|
194
|
+
|
|
195
|
+
@primary_key {:id, :binary_id, autogenerate: true}
|
|
196
|
+
|
|
197
|
+
@type t :: %__MODULE__{
|
|
198
|
+
id: Ecto.UUID.t(),
|
|
199
|
+
name: String.t()
|
|
200
|
+
}
|
|
201
|
+
|
|
195
202
|
schema "user" do
|
|
196
|
-
field(:id, :binary_id, primary_key: true)
|
|
197
203
|
field(:name, :string)
|
|
198
204
|
end
|
|
199
205
|
end
|
|
@@ -202,9 +208,17 @@ end
|
|
|
202
208
|
```elixir
|
|
203
209
|
defmodule DBSchema.Post do
|
|
204
210
|
use Ecto.Schema
|
|
205
|
-
|
|
211
|
+
|
|
212
|
+
@primary_key {:id, :binary_id, autogenerate: true}
|
|
213
|
+
|
|
214
|
+
@type t :: %__MODULE__{
|
|
215
|
+
id: Ecto.UUID.t(),
|
|
216
|
+
title: String.t(),
|
|
217
|
+
content: String.t(),
|
|
218
|
+
userId: String.t()
|
|
219
|
+
}
|
|
220
|
+
|
|
206
221
|
schema "post" do
|
|
207
|
-
field(:id, :binary_id, primary_key: true)
|
|
208
222
|
field(:title, :string)
|
|
209
223
|
field(:content, :string)
|
|
210
224
|
field(:userId, :string)
|
|
@@ -20,6 +20,48 @@ function getPrimaryKeyConfig(field) {
|
|
|
20
20
|
omitIdFieldInSchema: false,
|
|
21
21
|
};
|
|
22
22
|
}
|
|
23
|
+
function getFieldDefaultOption(field) {
|
|
24
|
+
const def = field.default;
|
|
25
|
+
if (def === undefined || def === null)
|
|
26
|
+
return null;
|
|
27
|
+
if (typeof def === 'string')
|
|
28
|
+
return `default: "${def}"`;
|
|
29
|
+
if (typeof def === 'number' || typeof def === 'boolean')
|
|
30
|
+
return `default: ${def}`;
|
|
31
|
+
return null;
|
|
32
|
+
}
|
|
33
|
+
function ectoTypeToTypespec(type) {
|
|
34
|
+
switch (type) {
|
|
35
|
+
case 'string': return 'String.t()';
|
|
36
|
+
case 'integer': return 'integer()';
|
|
37
|
+
case 'float': return 'float()';
|
|
38
|
+
case 'boolean': return 'boolean()';
|
|
39
|
+
case 'binary_id': return 'Ecto.UUID.t()';
|
|
40
|
+
case 'naive_datetime': return 'NaiveDateTime.t()';
|
|
41
|
+
case 'utc_datetime': return 'DateTime.t()';
|
|
42
|
+
default: return 'term()';
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
function buildTimestampsLine(fields) {
|
|
46
|
+
const insertedAliases = ['inserted_at', 'created_at', 'createdAt'];
|
|
47
|
+
const updatedAliases = ['updated_at', 'modified_at', 'updatedAt', 'modifiedAt'];
|
|
48
|
+
const inserted = fields.find((f) => insertedAliases.includes(f.name));
|
|
49
|
+
const updated = fields.find((f) => updatedAliases.includes(f.name));
|
|
50
|
+
const exclude = new Set();
|
|
51
|
+
if (inserted)
|
|
52
|
+
exclude.add(inserted.name);
|
|
53
|
+
if (updated)
|
|
54
|
+
exclude.add(updated.name);
|
|
55
|
+
if (!(inserted || updated))
|
|
56
|
+
return { line: null, exclude };
|
|
57
|
+
if (inserted?.name === 'inserted_at' && updated?.name === 'updated_at') {
|
|
58
|
+
return { line: ' timestamps()', exclude };
|
|
59
|
+
}
|
|
60
|
+
return {
|
|
61
|
+
line: ` timestamps(inserted_at: :${inserted?.name ?? 'inserted_at'}, updated_at: :${updated?.name ?? 'updated_at'})`,
|
|
62
|
+
exclude,
|
|
63
|
+
};
|
|
64
|
+
}
|
|
23
65
|
export function ectoSchemas(models, app) {
|
|
24
66
|
return models
|
|
25
67
|
.map((model) => {
|
|
@@ -27,10 +69,14 @@ export function ectoSchemas(models, app) {
|
|
|
27
69
|
if (!idField)
|
|
28
70
|
return '';
|
|
29
71
|
const pk = getPrimaryKeyConfig(idField);
|
|
30
|
-
const fields = model.fields.
|
|
72
|
+
const fields = model.fields.map((f) => ({ ...f }));
|
|
73
|
+
const { line: timestampsLine, exclude: timestampsExclude } = buildTimestampsLine(fields);
|
|
74
|
+
const schemaFieldsRaw = fields.filter((f) => !(f.relationName ||
|
|
75
|
+
(f.isId && pk.omitIdFieldInSchema) ||
|
|
76
|
+
timestampsExclude.has(f.name)));
|
|
31
77
|
const typeSpecFields = [
|
|
32
78
|
`id: ${pk.typeSpec}`,
|
|
33
|
-
...
|
|
79
|
+
...schemaFieldsRaw.map((f) => `${f.name}: ${ectoTypeToTypespec(prismaTypeToEctoType(f.type))}`),
|
|
34
80
|
];
|
|
35
81
|
const typeSpecLines = [
|
|
36
82
|
' @type t :: %__MODULE__{',
|
|
@@ -40,9 +86,12 @@ export function ectoSchemas(models, app) {
|
|
|
40
86
|
}),
|
|
41
87
|
' }',
|
|
42
88
|
];
|
|
43
|
-
const schemaFields =
|
|
44
|
-
const type = prismaTypeToEctoType(f.type);
|
|
45
|
-
|
|
89
|
+
const schemaFields = schemaFieldsRaw.map((f) => {
|
|
90
|
+
const type = f.isId ? 'binary_id' : prismaTypeToEctoType(f.type);
|
|
91
|
+
const primary = f.isId && !pk.omitIdFieldInSchema ? ', primary_key: true' : '';
|
|
92
|
+
const defaultOpt = getFieldDefaultOption(f);
|
|
93
|
+
const defaultClause = defaultOpt ? `, ${defaultOpt}` : '';
|
|
94
|
+
return ` field(:${f.name}, :${type}${primary}${defaultClause})`;
|
|
46
95
|
});
|
|
47
96
|
const lines = [
|
|
48
97
|
`defmodule ${app}.${model.name} do`,
|
|
@@ -54,6 +103,7 @@ export function ectoSchemas(models, app) {
|
|
|
54
103
|
'',
|
|
55
104
|
` schema "${snakeCase(model.name)}" do`,
|
|
56
105
|
...schemaFields,
|
|
106
|
+
...(timestampsLine ? [timestampsLine] : []),
|
|
57
107
|
' end',
|
|
58
108
|
'end',
|
|
59
109
|
];
|
|
@@ -62,26 +112,6 @@ export function ectoSchemas(models, app) {
|
|
|
62
112
|
.filter(Boolean)
|
|
63
113
|
.join('\n\n');
|
|
64
114
|
}
|
|
65
|
-
function ectoTypeToTypespec(type) {
|
|
66
|
-
switch (type) {
|
|
67
|
-
case 'string':
|
|
68
|
-
return 'String.t()';
|
|
69
|
-
case 'integer':
|
|
70
|
-
return 'integer()';
|
|
71
|
-
case 'float':
|
|
72
|
-
return 'float()';
|
|
73
|
-
case 'boolean':
|
|
74
|
-
return 'boolean()';
|
|
75
|
-
case 'binary_id':
|
|
76
|
-
return 'Ecto.UUID.t()';
|
|
77
|
-
case 'naive_datetime':
|
|
78
|
-
return 'NaiveDateTime.t()';
|
|
79
|
-
case 'utc_datetime':
|
|
80
|
-
return 'DateTime.t()';
|
|
81
|
-
default:
|
|
82
|
-
return 'term()';
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
115
|
export async function writeEctoSchemasToFiles(models, app, outDir) {
|
|
86
116
|
await fsp.mkdir(outDir, { recursive: true });
|
|
87
117
|
for (const model of models) {
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "hekireki",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.2.
|
|
4
|
+
"version": "0.2.7",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"description": "Hekireki is a tool that generates validation schemas for Zod and Valibot, as well as ER diagrams, from Prisma schemas annotated with comments.",
|
|
7
7
|
"keywords": [
|