@itrocks/core-transformers 0.1.1 → 0.1.3

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 (2) hide show
  1. package/README.md +347 -0
  2. package/package.json +13 -4
package/README.md CHANGED
@@ -7,3 +7,350 @@
7
7
  # core-transformers
8
8
 
9
9
  Prefabricated HTML and SQL data transformers for it.rocks primitives and basic types.
10
+
11
+ *This documentation was written by an artificial intelligence and may contain errors or approximations.
12
+ It has not yet been fully reviewed by a human. If anything seems unclear or incomplete,
13
+ please feel free to contact the author of this package.*
14
+
15
+ ## Installation
16
+
17
+ ```bash
18
+ npm i @itrocks/core-transformers
19
+ ```
20
+
21
+ `@itrocks/core-transformers` is usually installed together with the
22
+ it.rocks framework and the low‑level packages `@itrocks/transformer`,
23
+ `@itrocks/storage`, etc. It can also be used on its own in any
24
+ TypeScript/Node.js project where you need consistent HTML and SQL
25
+ conversions for primitive values, collections and stored entities.
26
+
27
+ ## Usage
28
+
29
+ This package does not expose UI components directly. Instead, it
30
+ registers **transformers** into `@itrocks/transformer` so that all
31
+ primitives, collections and stored entities share the same HTML and SQL
32
+ behaviour across your application.
33
+
34
+ You normally call one of the provided initialisers once at application
35
+ startup, then use your usual transformation helpers (for example, those
36
+ provided by `@itrocks/transformer` or higher‑level view packages).
37
+
38
+ ### Minimal example: initialise everything with default behaviour
39
+
40
+ ```ts
41
+ import { initCoreTransformers } from '@itrocks/core-transformers'
42
+
43
+ // At application bootstrap
44
+ initCoreTransformers({})
45
+
46
+ // From this point, any use of the transformer engine for:
47
+ // - primitive types (boolean, number, bigint, Date, string),
48
+ // - collection properties (`CollectionType`),
49
+ // - stored entities (from `@itrocks/storage`),
50
+ // will benefit from the default HTML & SQL transformers configured by
51
+ // this package.
52
+ ```
53
+
54
+ With this minimal setup, the framework knows how to:
55
+
56
+ - render HTML form controls for booleans, numbers, dates, text and
57
+ collections,
58
+ - convert submitted data back to typed values,
59
+ - adapt booleans and related entities when reading/writing SQL.
60
+
61
+ ### Example: customise dependencies and use containers
62
+
63
+ For a real‑world project you will usually configure a few
64
+ infrastructure‑specific helpers so that the generated HTML fits your
65
+ forms (IDs, names, translation, display labels, routes, …) and optionally
66
+ wrap lists into HTML containers.
67
+
68
+ ```ts
69
+ import {
70
+ HtmlContainer,
71
+ initCoreTransformers,
72
+ setCorePrimitiveDependencies,
73
+ setStoreDependencies
74
+ } from '@itrocks/core-transformers'
75
+
76
+ // 1. Configure how labels, IDs, names and translations are built
77
+ setCorePrimitiveDependencies({
78
+ displayOf: (object, property) => /* human label for property */,
79
+ fieldIdOf: property => `field-${property}`,
80
+ fieldNameOf: property => property,
81
+ tr: text => translate(text)
82
+ })
83
+
84
+ setStoreDependencies({
85
+ displayOf: (object, property) => /* label for relations */,
86
+ fieldIdOf: property => `field-${property}`,
87
+ fieldNameOf: property => property,
88
+ representativeValueOf: async object => object.toString(),
89
+ routeOf: type => `/api/${type.name.toLowerCase()}`,
90
+ tr: text => translate(text),
91
+ ignoreTransformedValue: Symbol('ignore')
92
+ })
93
+
94
+ // 2. Register all transformers with the configured dependencies
95
+ initCoreTransformers({})
96
+
97
+ // 3. Later, when asking for HTML output you may request that lists of
98
+ // stored entities are wrapped in containers:
99
+
100
+ import { HTML } from '@itrocks/transformer'
101
+
102
+ async function renderList(value: any, transform: Function) {
103
+ const container = new HtmlContainer(true) // enforce container
104
+ const html = await transform(value, HTML, container)
105
+ return String(html)
106
+ }
107
+ ```
108
+
109
+ The exact way you ask for a transformation (`transform` function,
110
+ pipeline, decorators, …) depends on how you use `@itrocks/transformer`
111
+ or higher‑level libraries, but once the initialisation code above has
112
+ run you get consistent behaviour across the whole application.
113
+
114
+ ## API
115
+
116
+ All exports register or configure transformers in the global
117
+ `@itrocks/transformer` registry. They do not return values; their role is
118
+ to set up the transformation rules used elsewhere.
119
+
120
+ ### Core initialiser
121
+
122
+ #### `initCoreTransformers(dependencies: Partial<Dependencies>): void`
123
+
124
+ Initialises all core transformers in one call:
125
+
126
+ - primitive transformers (booleans, numbers, bigints, dates, default
127
+ text fields),
128
+ - collection transformers (`CollectionType`),
129
+ - store transformers for entities managed by `@itrocks/storage`,
130
+ - container transformer for HTML wrapping.
131
+
132
+ `dependencies` is a composite of the dependency types defined in the
133
+ sub‑modules `collection-type`, `primitive` and `store`. You usually pass
134
+ only the properties you want to override; everything else falls back to
135
+ reasonable defaults.
136
+
137
+ Call this once during application bootstrap.
138
+
139
+ ### Primitive transformers
140
+
141
+ These functions configure transformers for primitive types and the
142
+ default behaviour when no specific type is matched.
143
+
144
+ All of them rely on the dependency helpers defined in
145
+ `setCorePrimitiveDependencies` / `setPrimitiveDependencies`.
146
+
147
+ #### `initPrimitiveTransformers(dependencies?: Partial<Dependencies>): void`
148
+
149
+ Registers all primitive HTML and SQL transformers at once.
150
+
151
+ You normally do not need to call it directly if you use
152
+ `initCoreTransformers`, but it can be useful in tests or very small
153
+ projects that only use primitive values.
154
+
155
+ #### `initBigintHtmlTransformers(): void`
156
+
157
+ Registers an HTML input transformer for JavaScript `BigInt` values.
158
+ Values coming from HTML inputs (strings) are converted to `BigInt`.
159
+
160
+ #### `initBooleanHtmlTransformers(): void`
161
+
162
+ Registers transformers for boolean values in HTML context:
163
+
164
+ - **EDIT**: outputs a label, a hidden `0` field and a checkbox set to
165
+ `1` when checked.
166
+ - **INPUT**: converts variants of "false" (empty string, `0`, localized
167
+ words for "false"/"no") to `false`, everything else to `true`.
168
+ - **OUTPUT**: displays a localized `"yes"` or `"no"` string.
169
+
170
+ #### `initBooleanSqlTransformers(): void`
171
+
172
+ Registers SQL transformers for booleans:
173
+
174
+ - **READ**: converts truthy SQL values to `true` / `false`.
175
+ - **SAVE**: stores booleans as `0` or `1`.
176
+
177
+ #### `initDateHtmlTransformers(): void`
178
+
179
+ Registers HTML transformers for `Date` values:
180
+
181
+ - **EDIT**: renders a `<label>` and an `<input data-type="date">` with
182
+ a formatted value when present.
183
+ - **INPUT**: parses a string using your configured `parseDate` helper.
184
+ - **OUTPUT**: formats a `Date` using `formatDate` (or returns an empty
185
+ string for `undefined`).
186
+
187
+ #### `initNumberHtmlTransformers(): void`
188
+
189
+ Registers transformers for numeric values:
190
+
191
+ - **EDIT**: renders a `<label>` and an `<input data-type="number">`.
192
+ - **INPUT**: accepts localized strings (spaces, comma as decimal
193
+ separator) and compact suffixes such as `"10k"`, `"2M"`, `"1G"`, …
194
+ and converts them to a `number | undefined`.
195
+ - **OUTPUT**: formats the number using the configured precision for the
196
+ property (via `precisionOf`) and `fr-FR` locale.
197
+ - **READ (SQL)**: converts SQL values (`number | string`) to a
198
+ JavaScript `number | undefined`.
199
+
200
+ #### `initDefaultHtmlEditTransformers(): void`
201
+
202
+ Registers a very generic HTML editor used as a fallback when no specific
203
+ primitive transformer applies. It renders a simple `<label>` and text
204
+ `<input>`.
205
+
206
+ #### `setCorePrimitiveDependencies(dependencies: Partial<CoreDependencies>): void`
207
+
208
+ Configures the dependencies shared by all primitive transformers except
209
+ `Date`:
210
+
211
+ - `displayOf(object, property)` – returns the label used in HTML
212
+ `<label>` elements.
213
+ - `fieldIdOf(property)` – returns the HTML `id` attribute for a field.
214
+ - `fieldNameOf(property)` – returns the HTML `name` attribute for a
215
+ field.
216
+ - `tr(text)` – translation function used for labels and boolean
217
+ `"yes"/"no"` values.
218
+
219
+ You can call this before `initPrimitiveTransformers` or
220
+ `initCoreTransformers` to integrate these transformers with your own
221
+ view or i18n layer.
222
+
223
+ #### `setPrimitiveDependencies(dependencies: Partial<Dependencies>): void`
224
+
225
+ Extends `setCorePrimitiveDependencies` with the extra date helpers
226
+ required by the `Date` transformers:
227
+
228
+ - `formatDate(date: Date): string`
229
+ - `parseDate(text: string): Date`
230
+
231
+ Use this if you want to override both core helpers and date formatting
232
+ in one place.
233
+
234
+ ### Collection transformers
235
+
236
+ Helpers dedicated to properties of type `CollectionType` from
237
+ `@itrocks/property-type`.
238
+
239
+ #### `initCollectionHtmlTransformers(dependencies?: Partial<Dependencies>): void`
240
+
241
+ Registers HTML transformers for collections of stored entities.
242
+
243
+ Generated HTML typically consists of:
244
+
245
+ - a `<label>` for the property,
246
+ - an unordered list (`<ul data-type="objects">`) of `<input>` elements
247
+ representing the selected entities,
248
+ - hidden fields storing the internal IDs used to bind back to your
249
+ entities.
250
+
251
+ Dependencies let you control labels, field names/IDs, how each entity is
252
+ turned into text (`representativeValueOf`), and how individual property
253
+ values are rendered inside a table (`propertyOutput`).
254
+
255
+ #### `initCollectionTransformers(dependencies?: Partial<Dependencies>): void`
256
+
257
+ Convenience wrapper that currently delegates to
258
+ `initCollectionHtmlTransformers`. Provided for symmetry and future
259
+ extensions.
260
+
261
+ ### Container transformers
262
+
263
+ #### `class HtmlContainer`
264
+
265
+ Small helper object used as a hint when requesting HTML transformations.
266
+
267
+ - `mandatoryContainer: boolean` – if `true`, a container is required.
268
+ - `container: boolean = true` – whether the container is still desired
269
+ for the current transformation; transformers can set it to `false`
270
+ once they have produced their own container.
271
+
272
+ You typically instantiate `HtmlContainer` and pass it as the
273
+ "askFor"/options argument to your transformation function.
274
+
275
+ #### `initContainerTransformers(): void`
276
+
277
+ Registers a format transformer for `HTML` that wraps simple values into a
278
+ `<div>` when `HtmlContainer.mandatoryContainer` and
279
+ `HtmlContainer.container` are both `true`. For complex objects, it tries
280
+ to find the string property whose value matches `toString()` and wraps
281
+ only this property.
282
+
283
+ This is used internally by other transformers when you need HTML
284
+ containers for values.
285
+
286
+ ### Store transformers
287
+
288
+ These helpers manage the HTML and SQL representations of entities
289
+ handled by `@itrocks/storage`.
290
+
291
+ #### `initStoreHtmlTransformers(target: Type): void`
292
+
293
+ Registers HTML transformers for a given entity `Type`:
294
+
295
+ - **EDIT**: renders an input field with auto‑completion / fetching of
296
+ related entities plus a hidden field storing the selected ID.
297
+ - **INPUT**: updates the owning object from submitted form data,
298
+ deciding whether to keep an existing relation, set an ID field
299
+ (`propertyId`) or instantiate a new object.
300
+ - **OUTPUT**: displays the representative value of the related entity
301
+ (`representativeValueOf`).
302
+
303
+ #### `initStoreSqlTransformers(target: Type): void`
304
+
305
+ Registers SQL save transformers for the given entity type. When saving,
306
+ it ensures that the related entity is persisted and that the foreign key
307
+ `<property>_id` is set on the SQL record.
308
+
309
+ #### `initStoreTransformers(target: Type): void`
310
+
311
+ Convenience helper that calls both `initStoreHtmlTransformers` and
312
+ `initStoreSqlTransformers` for the same type.
313
+
314
+ #### `setStoreDependencies(dependencies: Partial<Dependencies>): void`
315
+
316
+ Configures the helpers used by all store transformers:
317
+
318
+ - `displayOf(object, property)` – label for relation fields.
319
+ - `fieldIdOf(property)` / `fieldNameOf(property)` – build HTML IDs and
320
+ names.
321
+ - `representativeValueOf(object)` – how to get a display string for an
322
+ entity.
323
+ - `routeOf(type)` – route used to fetch summaries for auto‑completion.
324
+ - `tr(text)` – translation function.
325
+ - `ignoreTransformedValue` – sentinel value used internally to indicate
326
+ that an input transformer chose not to alter the original value.
327
+
328
+ You should set these once, before initialising the store transformers.
329
+
330
+ #### `setStoreHtmlDependencies(dependencies: Partial<Dependencies>): void`
331
+
332
+ Alias of `setStoreDependencies`. Provided for readability when you only
333
+ care about the HTML side.
334
+
335
+ #### `setStoreSqlDependencies(dependencies: Partial<SqlDependencies>): void`
336
+
337
+ Alias specialised to the SQL‑related subset of dependencies. In
338
+ practice, this lets you override only `ignoreTransformedValue` when you
339
+ need a custom sentinel.
340
+
341
+ ## Typical use cases
342
+
343
+ - **Unified form rendering** – ensure that all boolean, number, date and
344
+ text fields across your entities share the same labels, IDs, names,
345
+ translations and HTML controls.
346
+ - **Consistent SQL mapping** – centralise how booleans, numbers and
347
+ relations are read from and written to SQL records.
348
+ - **Collection editing widgets** – provide a coherent HTML structure for
349
+ one‑to‑many or many‑to‑many relations, including list display and
350
+ inline tables for components.
351
+ - **Entity relation editors** – generate HTML inputs and auto‑complete
352
+ widgets for relations handled by `@itrocks/storage`, with transparent
353
+ synchronisation of foreign keys.
354
+ - **Framework integration** – plug the dependencies into your own
355
+ routing, translation and display helpers so that the same configuration
356
+ is reused across multiple projects.
package/package.json CHANGED
@@ -14,8 +14,8 @@
14
14
  },
15
15
  "description": "Prefabricated HTML and SQL data transformers for it.rocks primitives and basic types",
16
16
  "devDependencies": {
17
- "@types/node": "^22.10",
18
- "typescript": "~5.8"
17
+ "@types/node": "^24.10",
18
+ "typescript": "~5.9"
19
19
  },
20
20
  "engines": {
21
21
  "node": ">=18"
@@ -35,7 +35,16 @@
35
35
  "homepage": "https://it.rocks",
36
36
  "keywords": [
37
37
  "backend",
38
- "it.rocks"
38
+ "collections",
39
+ "core",
40
+ "entities",
41
+ "form",
42
+ "forms",
43
+ "html",
44
+ "it.rocks",
45
+ "sql",
46
+ "transformer",
47
+ "transformers"
39
48
  ],
40
49
  "license": "ISC",
41
50
  "name": "@itrocks/core-transformers",
@@ -49,5 +58,5 @@
49
58
  "build:esm": "tsc -p tsconfig.esm.json && node esm/esm"
50
59
  },
51
60
  "types": "./esm/core-transformers.d.ts",
52
- "version": "0.1.1"
61
+ "version": "0.1.3"
53
62
  }