@itrocks/core-transformers 0.0.1
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/LICENSE +14 -0
- package/README.md +7 -0
- package/cjs/collection-type.d.ts +12 -0
- package/cjs/collection-type.js +110 -0
- package/cjs/container.d.ts +6 -0
- package/cjs/container.js +17 -0
- package/cjs/core-transformers.d.ts +9 -0
- package/cjs/core-transformers.js +38 -0
- package/cjs/primitive.d.ts +18 -0
- package/cjs/primitive.js +110 -0
- package/cjs/store.d.ts +17 -0
- package/cjs/store.js +73 -0
- package/esm/collection-type.d.ts +12 -0
- package/esm/collection-type.js +105 -0
- package/esm/container.d.ts +6 -0
- package/esm/container.js +12 -0
- package/esm/core-transformers.d.ts +9 -0
- package/esm/core-transformers.js +14 -0
- package/esm/primitive.d.ts +18 -0
- package/esm/primitive.js +99 -0
- package/esm/store.d.ts +17 -0
- package/esm/store.js +66 -0
- package/package.json +51 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
ISC License
|
|
2
|
+
Copyright (C) 2024, Baptiste Pillot <baptiste@pillot.fr>
|
|
3
|
+
|
|
4
|
+
Permission to use, copy, modify, and/or distribute this software for any
|
|
5
|
+
purpose with or without fee is hereby granted, provided that the above
|
|
6
|
+
copyright notice and this permission notice appear in all copies.
|
|
7
|
+
|
|
8
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
9
|
+
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
10
|
+
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
11
|
+
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
12
|
+
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
|
|
13
|
+
OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
|
|
14
|
+
CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
[](https://www.npmjs.org/package/@itrocks/core-transformers)
|
|
2
|
+
[](https://www.npmjs.org/package/@itrocks/core-transformers)
|
|
3
|
+
[](https://github.com/itrocks-ts/core-transformers)
|
|
4
|
+
[](https://github.com/itrocks-ts/core-transformers/issues)
|
|
5
|
+
[](https://25.re/ditr)
|
|
6
|
+
|
|
7
|
+
# core-transformers
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { AnyObject } from '@itrocks/class-type';
|
|
2
|
+
import { Type } from '@itrocks/class-type';
|
|
3
|
+
export type Dependencies = {
|
|
4
|
+
displayOf: (object: AnyObject, property: string) => string;
|
|
5
|
+
ignoreTransformedValue: any;
|
|
6
|
+
representativeValueOf: (object: object) => string;
|
|
7
|
+
routeOf: (type: Type) => string;
|
|
8
|
+
tr: (text: string) => string;
|
|
9
|
+
};
|
|
10
|
+
export declare function initCollectionHtmlTransformers(dependencies?: Partial<Dependencies>): void;
|
|
11
|
+
export declare function initCollectionSqlTransformers(): void;
|
|
12
|
+
export declare function initCollectionTransformers(dependencies?: Partial<Dependencies>): void;
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.initCollectionHtmlTransformers = initCollectionHtmlTransformers;
|
|
4
|
+
exports.initCollectionSqlTransformers = initCollectionSqlTransformers;
|
|
5
|
+
exports.initCollectionTransformers = initCollectionTransformers;
|
|
6
|
+
const composition_1 = require("@itrocks/composition");
|
|
7
|
+
const storage_1 = require("@itrocks/storage");
|
|
8
|
+
const class_type_1 = require("@itrocks/class-type");
|
|
9
|
+
const class_type_2 = require("@itrocks/class-type");
|
|
10
|
+
const property_type_1 = require("@itrocks/property-type");
|
|
11
|
+
const reflect_1 = require("@itrocks/reflect");
|
|
12
|
+
const reflect_2 = require("@itrocks/reflect");
|
|
13
|
+
const transformer_1 = require("@itrocks/transformer");
|
|
14
|
+
const transformer_2 = require("@itrocks/transformer");
|
|
15
|
+
const transformer_3 = require("@itrocks/transformer");
|
|
16
|
+
const depends = {
|
|
17
|
+
displayOf: (_object, property) => property,
|
|
18
|
+
ignoreTransformedValue: Symbol('ignoreTransformedValue'),
|
|
19
|
+
representativeValueOf: object => (0, class_type_1.baseType)((0, class_type_2.typeOf)(object)).name,
|
|
20
|
+
routeOf: type => '/' + (0, class_type_1.baseType)(type).name,
|
|
21
|
+
tr: text => text
|
|
22
|
+
};
|
|
23
|
+
const areMayEntityEntries = (entries) => (typeof entries[0]?.[1])[0] === 'o';
|
|
24
|
+
function collectionEdit(values, object, property) {
|
|
25
|
+
const propertyType = new reflect_2.ReflectProperty(object, property).collectionType;
|
|
26
|
+
const fetch = depends.routeOf(propertyType?.elementType) + '/summary';
|
|
27
|
+
const label = `<label for="${property}">${depends.tr(depends.displayOf(object, property))}</label>`;
|
|
28
|
+
const inputs = [];
|
|
29
|
+
for (const object of values) {
|
|
30
|
+
const attrValue = `value="${depends.representativeValueOf(object)}"`;
|
|
31
|
+
inputs.push('<li>' + ((0, storage_1.dataSource)().isObjectConnected(object)
|
|
32
|
+
? `<input id="${property}.${object.id}" name="${property}.${object.id}" ${attrValue}>`
|
|
33
|
+
: `<input id="${property}." name="${property}." ${attrValue}>`) + '</li>');
|
|
34
|
+
}
|
|
35
|
+
return label + `<ul data-multiple-contained-auto-width data-fetch="${fetch}" data-type="objects">`
|
|
36
|
+
+ inputs.join('')
|
|
37
|
+
+ `<li><input id="${property}" name="${property}" placeholder="+"></li>`
|
|
38
|
+
+ '</ul>';
|
|
39
|
+
}
|
|
40
|
+
function collectionInput(values, object, property) {
|
|
41
|
+
const entries = Object.entries(values);
|
|
42
|
+
if (areMayEntityEntries(entries)) {
|
|
43
|
+
Object.assign(object, { [property]: entries.map(([id, value]) => (0, storage_1.dataSource)().connectObject(value, +id)) });
|
|
44
|
+
}
|
|
45
|
+
else {
|
|
46
|
+
delete object[property];
|
|
47
|
+
Object.assign(object, { [property + '_ids']: Object.keys(values).map(id => +id) });
|
|
48
|
+
}
|
|
49
|
+
return depends.ignoreTransformedValue;
|
|
50
|
+
}
|
|
51
|
+
function collectionOutput(values, object, property, askFor) {
|
|
52
|
+
if (!values.length) {
|
|
53
|
+
return '';
|
|
54
|
+
}
|
|
55
|
+
if ((0, composition_1.componentOf)(object, property)) {
|
|
56
|
+
const propertyType = new reflect_2.ReflectProperty(object, property).type;
|
|
57
|
+
if (propertyType instanceof property_type_1.CollectionType) {
|
|
58
|
+
const type = propertyType.elementType;
|
|
59
|
+
const propertyClass = new reflect_1.ReflectClass(type);
|
|
60
|
+
const properties = propertyClass.propertyNames.filter(property => !(0, composition_1.compositeOf)(type, property));
|
|
61
|
+
const html = [];
|
|
62
|
+
html.push('<table>');
|
|
63
|
+
html.push('<tr>' + properties.map(property => '<th>' + depends.tr(property) + '</th>').join('') + '</tr>');
|
|
64
|
+
html.push(...values.map(value => '<tr>' + properties.map(property => '<td>' + value[property] + '</td>').join('') + '</tr>'));
|
|
65
|
+
html.push('</table>');
|
|
66
|
+
return html.join('\n');
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
if (askFor?.container) {
|
|
70
|
+
askFor.container = false;
|
|
71
|
+
return '<ul>' + values.map(object => '<li>' + depends.representativeValueOf(object) + '</li>').join('') + '</ul>';
|
|
72
|
+
}
|
|
73
|
+
return values.map(object => depends.representativeValueOf(object)).join(', ');
|
|
74
|
+
}
|
|
75
|
+
async function collectionSave(values, object, property) {
|
|
76
|
+
const dao = (0, storage_1.dataSource)();
|
|
77
|
+
const newIdsPromise = object[property + '_ids']
|
|
78
|
+
?? values?.map(async (value) => (dao.isObjectConnected(value) ? value : await dao.save(value)).id).sort()
|
|
79
|
+
?? [];
|
|
80
|
+
const previousIdsPromise = dao.isObjectConnected(object)
|
|
81
|
+
? await dao.readCollectionIds(object, property)
|
|
82
|
+
: [];
|
|
83
|
+
return async (object) => {
|
|
84
|
+
const previousIds = await Promise.all(previousIdsPromise);
|
|
85
|
+
const newIds = await Promise.all(newIdsPromise);
|
|
86
|
+
for (const id of previousIds) {
|
|
87
|
+
if (newIds.includes(id))
|
|
88
|
+
continue;
|
|
89
|
+
dao.deleteRelatedId(object, property, id);
|
|
90
|
+
}
|
|
91
|
+
for (const id of newIds) {
|
|
92
|
+
if (previousIds.includes(id))
|
|
93
|
+
continue;
|
|
94
|
+
dao.insertRelatedId(object, property, id);
|
|
95
|
+
}
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
function initCollectionHtmlTransformers(dependencies = {}) {
|
|
99
|
+
Object.assign(depends, dependencies);
|
|
100
|
+
(0, transformer_3.setPropertyTypeTransformer)(property_type_1.CollectionType, transformer_1.HTML, transformer_1.EDIT, collectionEdit);
|
|
101
|
+
(0, transformer_3.setPropertyTypeTransformer)(property_type_1.CollectionType, transformer_1.HTML, transformer_2.INPUT, collectionInput);
|
|
102
|
+
(0, transformer_3.setPropertyTypeTransformer)(property_type_1.CollectionType, transformer_1.HTML, transformer_2.OUTPUT, collectionOutput);
|
|
103
|
+
}
|
|
104
|
+
function initCollectionSqlTransformers() {
|
|
105
|
+
(0, transformer_3.setPropertyTypeTransformer)(property_type_1.CollectionType, transformer_2.SQL, transformer_2.SAVE, collectionSave);
|
|
106
|
+
}
|
|
107
|
+
function initCollectionTransformers(dependencies = {}) {
|
|
108
|
+
initCollectionHtmlTransformers(dependencies);
|
|
109
|
+
initCollectionSqlTransformers();
|
|
110
|
+
}
|
package/cjs/container.js
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.HtmlContainer = void 0;
|
|
4
|
+
exports.initContainerTransformers = initContainerTransformers;
|
|
5
|
+
const transformer_1 = require("@itrocks/transformer");
|
|
6
|
+
class HtmlContainer {
|
|
7
|
+
mandatoryContainer;
|
|
8
|
+
container;
|
|
9
|
+
constructor(mandatoryContainer, container = true) {
|
|
10
|
+
this.mandatoryContainer = mandatoryContainer;
|
|
11
|
+
this.container = container;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
exports.HtmlContainer = HtmlContainer;
|
|
15
|
+
function initContainerTransformers() {
|
|
16
|
+
(0, transformer_1.setFormatTransformer)(transformer_1.HTML, (value, askFor) => (askFor.container && askFor.mandatoryContainer) ? ('<div>' + value + '</div>') : value);
|
|
17
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { Dependencies as CollectionTypeDependencies } from './collection-type';
|
|
2
|
+
import { Dependencies as PrimitiveDependencies } from './primitive';
|
|
3
|
+
import { Dependencies as StoreDependencies } from './store';
|
|
4
|
+
export { HtmlContainer, initContainerTransformers } from './container';
|
|
5
|
+
export { initCollectionHtmlTransformers, initCollectionSqlTransformers, initCollectionTransformers } from './collection-type';
|
|
6
|
+
export { initBigintHtmlTransformers, initBooleanHtmlTransformers, initBooleanSqlTransformers, initDateHtmlTransformers, initDefaultHtmlEditTransformers, initNumberHtmlTransformers, initPrimitiveTransformers, setCorePrimitiveDependencies, setPrimitiveDependencies } from './primitive';
|
|
7
|
+
export { initStoreHtmlTransformers, initStoreSqlTransformers, initStoreTransformers, setStoreDependencies, setStoreSqlDependencies, setStoreHtmlDependencies } from './store';
|
|
8
|
+
type Dependencies = CollectionTypeDependencies & PrimitiveDependencies & StoreDependencies;
|
|
9
|
+
export declare function initCoreTransformers(dependencies: Partial<Dependencies>): void;
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.setStoreHtmlDependencies = exports.setStoreSqlDependencies = exports.setStoreDependencies = exports.initStoreTransformers = exports.initStoreSqlTransformers = exports.initStoreHtmlTransformers = exports.setPrimitiveDependencies = exports.setCorePrimitiveDependencies = exports.initPrimitiveTransformers = exports.initNumberHtmlTransformers = exports.initDefaultHtmlEditTransformers = exports.initDateHtmlTransformers = exports.initBooleanSqlTransformers = exports.initBooleanHtmlTransformers = exports.initBigintHtmlTransformers = exports.initCollectionTransformers = exports.initCollectionSqlTransformers = exports.initCollectionHtmlTransformers = exports.initContainerTransformers = exports.HtmlContainer = void 0;
|
|
4
|
+
exports.initCoreTransformers = initCoreTransformers;
|
|
5
|
+
const collection_type_1 = require("./collection-type");
|
|
6
|
+
const container_1 = require("./container");
|
|
7
|
+
const primitive_1 = require("./primitive");
|
|
8
|
+
const store_1 = require("./store");
|
|
9
|
+
var container_2 = require("./container");
|
|
10
|
+
Object.defineProperty(exports, "HtmlContainer", { enumerable: true, get: function () { return container_2.HtmlContainer; } });
|
|
11
|
+
Object.defineProperty(exports, "initContainerTransformers", { enumerable: true, get: function () { return container_2.initContainerTransformers; } });
|
|
12
|
+
var collection_type_2 = require("./collection-type");
|
|
13
|
+
Object.defineProperty(exports, "initCollectionHtmlTransformers", { enumerable: true, get: function () { return collection_type_2.initCollectionHtmlTransformers; } });
|
|
14
|
+
Object.defineProperty(exports, "initCollectionSqlTransformers", { enumerable: true, get: function () { return collection_type_2.initCollectionSqlTransformers; } });
|
|
15
|
+
Object.defineProperty(exports, "initCollectionTransformers", { enumerable: true, get: function () { return collection_type_2.initCollectionTransformers; } });
|
|
16
|
+
var primitive_2 = require("./primitive");
|
|
17
|
+
Object.defineProperty(exports, "initBigintHtmlTransformers", { enumerable: true, get: function () { return primitive_2.initBigintHtmlTransformers; } });
|
|
18
|
+
Object.defineProperty(exports, "initBooleanHtmlTransformers", { enumerable: true, get: function () { return primitive_2.initBooleanHtmlTransformers; } });
|
|
19
|
+
Object.defineProperty(exports, "initBooleanSqlTransformers", { enumerable: true, get: function () { return primitive_2.initBooleanSqlTransformers; } });
|
|
20
|
+
Object.defineProperty(exports, "initDateHtmlTransformers", { enumerable: true, get: function () { return primitive_2.initDateHtmlTransformers; } });
|
|
21
|
+
Object.defineProperty(exports, "initDefaultHtmlEditTransformers", { enumerable: true, get: function () { return primitive_2.initDefaultHtmlEditTransformers; } });
|
|
22
|
+
Object.defineProperty(exports, "initNumberHtmlTransformers", { enumerable: true, get: function () { return primitive_2.initNumberHtmlTransformers; } });
|
|
23
|
+
Object.defineProperty(exports, "initPrimitiveTransformers", { enumerable: true, get: function () { return primitive_2.initPrimitiveTransformers; } });
|
|
24
|
+
Object.defineProperty(exports, "setCorePrimitiveDependencies", { enumerable: true, get: function () { return primitive_2.setCorePrimitiveDependencies; } });
|
|
25
|
+
Object.defineProperty(exports, "setPrimitiveDependencies", { enumerable: true, get: function () { return primitive_2.setPrimitiveDependencies; } });
|
|
26
|
+
var store_2 = require("./store");
|
|
27
|
+
Object.defineProperty(exports, "initStoreHtmlTransformers", { enumerable: true, get: function () { return store_2.initStoreHtmlTransformers; } });
|
|
28
|
+
Object.defineProperty(exports, "initStoreSqlTransformers", { enumerable: true, get: function () { return store_2.initStoreSqlTransformers; } });
|
|
29
|
+
Object.defineProperty(exports, "initStoreTransformers", { enumerable: true, get: function () { return store_2.initStoreTransformers; } });
|
|
30
|
+
Object.defineProperty(exports, "setStoreDependencies", { enumerable: true, get: function () { return store_2.setStoreDependencies; } });
|
|
31
|
+
Object.defineProperty(exports, "setStoreSqlDependencies", { enumerable: true, get: function () { return store_2.setStoreSqlDependencies; } });
|
|
32
|
+
Object.defineProperty(exports, "setStoreHtmlDependencies", { enumerable: true, get: function () { return store_2.setStoreHtmlDependencies; } });
|
|
33
|
+
function initCoreTransformers(dependencies) {
|
|
34
|
+
(0, collection_type_1.initCollectionTransformers)(dependencies);
|
|
35
|
+
(0, container_1.initContainerTransformers)();
|
|
36
|
+
(0, primitive_1.initPrimitiveTransformers)(dependencies);
|
|
37
|
+
(0, store_1.setStoreDependencies)(dependencies);
|
|
38
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { AnyObject } from '@itrocks/class-type';
|
|
2
|
+
export type CoreDependencies = {
|
|
3
|
+
displayOf: (object: AnyObject, property: string) => string;
|
|
4
|
+
tr: (text: string) => string;
|
|
5
|
+
};
|
|
6
|
+
export type Dependencies = CoreDependencies & {
|
|
7
|
+
formatDate: (date: Date) => string;
|
|
8
|
+
parseDate: (date: string) => Date;
|
|
9
|
+
};
|
|
10
|
+
export declare function initBigintHtmlTransformers(): void;
|
|
11
|
+
export declare function initBooleanHtmlTransformers(): void;
|
|
12
|
+
export declare function initBooleanSqlTransformers(): void;
|
|
13
|
+
export declare function initDateHtmlTransformers(): void;
|
|
14
|
+
export declare function initNumberHtmlTransformers(): void;
|
|
15
|
+
export declare function initDefaultHtmlEditTransformers(): void;
|
|
16
|
+
export declare function initPrimitiveTransformers(dependencies?: Partial<Dependencies>): void;
|
|
17
|
+
export declare function setCorePrimitiveDependencies(dependencies: Partial<CoreDependencies>): void;
|
|
18
|
+
export declare function setPrimitiveDependencies(dependencies: Partial<Dependencies>): void;
|
package/cjs/primitive.js
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.initBigintHtmlTransformers = initBigintHtmlTransformers;
|
|
4
|
+
exports.initBooleanHtmlTransformers = initBooleanHtmlTransformers;
|
|
5
|
+
exports.initBooleanSqlTransformers = initBooleanSqlTransformers;
|
|
6
|
+
exports.initDateHtmlTransformers = initDateHtmlTransformers;
|
|
7
|
+
exports.initNumberHtmlTransformers = initNumberHtmlTransformers;
|
|
8
|
+
exports.initDefaultHtmlEditTransformers = initDefaultHtmlEditTransformers;
|
|
9
|
+
exports.initPrimitiveTransformers = initPrimitiveTransformers;
|
|
10
|
+
exports.setCorePrimitiveDependencies = setCorePrimitiveDependencies;
|
|
11
|
+
exports.setPrimitiveDependencies = setPrimitiveDependencies;
|
|
12
|
+
const transformer_1 = require("@itrocks/transformer");
|
|
13
|
+
const transformer_2 = require("@itrocks/transformer");
|
|
14
|
+
const transformer_3 = require("@itrocks/transformer");
|
|
15
|
+
const lfTab = '\n\t\t\t\t';
|
|
16
|
+
const depends = {
|
|
17
|
+
displayOf: (_object, property) => property,
|
|
18
|
+
formatDate: date => date.toString(),
|
|
19
|
+
parseDate: date => new Date(date),
|
|
20
|
+
tr: text => text
|
|
21
|
+
};
|
|
22
|
+
// Bigint
|
|
23
|
+
function initBigintHtmlTransformers() {
|
|
24
|
+
(0, transformer_1.setPropertyTypeTransformers)(BigInt, [
|
|
25
|
+
{ format: transformer_2.HTML, direction: transformer_2.INPUT, transformer: (value) => BigInt(value) }
|
|
26
|
+
]);
|
|
27
|
+
}
|
|
28
|
+
// Boolean
|
|
29
|
+
function booleanEdit(value, type, property) {
|
|
30
|
+
const label = `<label for="${property}">${depends.tr(depends.displayOf(type, property))}</label>`;
|
|
31
|
+
const name = `id="${property}" name="${property}"`;
|
|
32
|
+
const hidden = `<input name="${property}" type="hidden" value="0">`;
|
|
33
|
+
const checked = value ? 'checked ' : '';
|
|
34
|
+
const checkbox = `<input ${checked}${name} type="checkbox" value="1">`;
|
|
35
|
+
return label + lfTab + hidden + lfTab + checkbox;
|
|
36
|
+
}
|
|
37
|
+
const booleanInput = (value) => !['', '0', 'false', 'no', depends.tr('false'), depends.tr('no')].includes(value);
|
|
38
|
+
function initBooleanHtmlTransformers() {
|
|
39
|
+
(0, transformer_1.setPropertyTypeTransformers)(Boolean, [
|
|
40
|
+
{ format: transformer_2.HTML, direction: transformer_2.EDIT, transformer: booleanEdit },
|
|
41
|
+
{ format: transformer_2.HTML, direction: transformer_2.INPUT, transformer: booleanInput },
|
|
42
|
+
{ format: transformer_2.HTML, direction: transformer_2.OUTPUT, transformer: (value) => value ? depends.tr('yes') : depends.tr('no') }
|
|
43
|
+
]);
|
|
44
|
+
}
|
|
45
|
+
function initBooleanSqlTransformers() {
|
|
46
|
+
(0, transformer_1.setPropertyTypeTransformers)(Boolean, [
|
|
47
|
+
{ format: transformer_3.SQL, direction: transformer_3.READ, transformer: (value) => !!value },
|
|
48
|
+
{ format: transformer_3.SQL, direction: transformer_3.SAVE, transformer: (value) => +value }
|
|
49
|
+
]);
|
|
50
|
+
}
|
|
51
|
+
// Date
|
|
52
|
+
function dateEdit(value, type, property) {
|
|
53
|
+
const label = `<label for="${property}">${depends.tr(depends.displayOf(type, property))}</label>`;
|
|
54
|
+
const name = `id="${property}" name="${property}"`;
|
|
55
|
+
const inputValue = value ? ` value="${depends.formatDate(value)}"` : '';
|
|
56
|
+
const input = `<input data-type="date" ${name}${inputValue}>`;
|
|
57
|
+
return label + lfTab + input;
|
|
58
|
+
}
|
|
59
|
+
const dateInput = (value) => depends.parseDate(value);
|
|
60
|
+
const dateOutput = (value) => value ? depends.formatDate(value) : '';
|
|
61
|
+
function initDateHtmlTransformers() {
|
|
62
|
+
(0, transformer_1.setPropertyTypeTransformers)(Date, [
|
|
63
|
+
{ format: transformer_2.HTML, direction: transformer_2.EDIT, transformer: dateEdit },
|
|
64
|
+
{ format: transformer_2.HTML, direction: transformer_2.INPUT, transformer: dateInput },
|
|
65
|
+
{ format: transformer_2.HTML, direction: transformer_2.OUTPUT, transformer: dateOutput }
|
|
66
|
+
]);
|
|
67
|
+
}
|
|
68
|
+
// Number
|
|
69
|
+
function numberEdit(value, type, property) {
|
|
70
|
+
const label = `<label for="${property}">${depends.tr(depends.displayOf(type, property))}</label>`;
|
|
71
|
+
const name = `id="${property}" name="${property}"`;
|
|
72
|
+
const inputValue = (value !== undefined) ? ` value="${value}"` : '';
|
|
73
|
+
const input = `<input data-type="number" ${name}${inputValue}>`;
|
|
74
|
+
return label + lfTab + input;
|
|
75
|
+
}
|
|
76
|
+
function initNumberHtmlTransformers() {
|
|
77
|
+
(0, transformer_1.setPropertyTypeTransformers)(Number, [
|
|
78
|
+
{ format: transformer_2.HTML, direction: transformer_2.EDIT, transformer: numberEdit },
|
|
79
|
+
{ format: transformer_2.HTML, direction: transformer_2.INPUT, transformer: (value) => +value }
|
|
80
|
+
]);
|
|
81
|
+
}
|
|
82
|
+
// default
|
|
83
|
+
function defaultEdit(value, type, property) {
|
|
84
|
+
const label = `<label for="${property}">${depends.tr(depends.displayOf(type, property))}</label>`;
|
|
85
|
+
const name = `id="${property}" name="${property}"`;
|
|
86
|
+
const inputValue = value ? ` value="${value}"` : '';
|
|
87
|
+
const input = `<input ${name}${inputValue}>`;
|
|
88
|
+
return label + lfTab + input;
|
|
89
|
+
}
|
|
90
|
+
function initDefaultHtmlEditTransformers() {
|
|
91
|
+
(0, transformer_1.setPropertyTypeTransformers)(null, [{ format: transformer_2.HTML, direction: transformer_2.EDIT, transformer: defaultEdit }]);
|
|
92
|
+
}
|
|
93
|
+
// all
|
|
94
|
+
function initPrimitiveTransformers(dependencies = {}) {
|
|
95
|
+
setPrimitiveDependencies(dependencies);
|
|
96
|
+
initBigintHtmlTransformers();
|
|
97
|
+
initBooleanSqlTransformers();
|
|
98
|
+
initBooleanHtmlTransformers();
|
|
99
|
+
initDateHtmlTransformers();
|
|
100
|
+
initNumberHtmlTransformers();
|
|
101
|
+
initDefaultHtmlEditTransformers();
|
|
102
|
+
}
|
|
103
|
+
// Initialize dependencies required by all core primitive transformers except Date
|
|
104
|
+
function setCorePrimitiveDependencies(dependencies) {
|
|
105
|
+
Object.assign(depends, dependencies);
|
|
106
|
+
}
|
|
107
|
+
// Initialize dependencies required by all core primitive transformers, including Date
|
|
108
|
+
function setPrimitiveDependencies(dependencies) {
|
|
109
|
+
Object.assign(depends, dependencies);
|
|
110
|
+
}
|
package/cjs/store.d.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { AnyObject } from '@itrocks/class-type';
|
|
2
|
+
import { Type } from '@itrocks/class-type';
|
|
3
|
+
export type SqlDependencies = {
|
|
4
|
+
ignoreTransformedValue: any;
|
|
5
|
+
};
|
|
6
|
+
export type Dependencies = SqlDependencies & {
|
|
7
|
+
displayOf: (object: AnyObject, property: string) => string;
|
|
8
|
+
representativeValueOf: (object: object) => string;
|
|
9
|
+
routeOf: (type: Type) => string;
|
|
10
|
+
tr: (text: string) => string;
|
|
11
|
+
};
|
|
12
|
+
export declare function initStoreHtmlTransformers(target: Type): void;
|
|
13
|
+
export declare function initStoreSqlTransformers(target: Type): void;
|
|
14
|
+
export declare function initStoreTransformers(target: Type): void;
|
|
15
|
+
export declare function setStoreDependencies(dependencies: Partial<Dependencies>): void;
|
|
16
|
+
export declare const setStoreHtmlDependencies: typeof setStoreDependencies;
|
|
17
|
+
export declare const setStoreSqlDependencies: (dependencies: Partial<SqlDependencies>) => void;
|
package/cjs/store.js
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.setStoreSqlDependencies = exports.setStoreHtmlDependencies = void 0;
|
|
4
|
+
exports.initStoreHtmlTransformers = initStoreHtmlTransformers;
|
|
5
|
+
exports.initStoreSqlTransformers = initStoreSqlTransformers;
|
|
6
|
+
exports.initStoreTransformers = initStoreTransformers;
|
|
7
|
+
exports.setStoreDependencies = setStoreDependencies;
|
|
8
|
+
const class_type_1 = require("@itrocks/class-type");
|
|
9
|
+
const reflect_1 = require("@itrocks/reflect");
|
|
10
|
+
const storage_1 = require("@itrocks/storage");
|
|
11
|
+
const transformer_1 = require("@itrocks/transformer");
|
|
12
|
+
const transformer_2 = require("@itrocks/transformer");
|
|
13
|
+
const transformer_3 = require("@itrocks/transformer");
|
|
14
|
+
const lfTab = '\n\t\t\t\t';
|
|
15
|
+
const depends = {
|
|
16
|
+
displayOf: (_object, property) => property,
|
|
17
|
+
ignoreTransformedValue: Symbol('ignoreTransformedValue'),
|
|
18
|
+
representativeValueOf: object => (0, class_type_1.baseType)((0, class_type_1.typeOf)(object)).name,
|
|
19
|
+
routeOf: type => '/' + (0, class_type_1.baseType)(type).name,
|
|
20
|
+
tr: text => text
|
|
21
|
+
};
|
|
22
|
+
function initStoreHtmlTransformers(target) {
|
|
23
|
+
(0, transformer_1.setPropertyTypeTransformer)(target, transformer_2.HTML, transformer_2.EDIT, storeEdit);
|
|
24
|
+
(0, transformer_1.setPropertyTypeTransformer)(target, transformer_2.HTML, transformer_3.INPUT, storeInput);
|
|
25
|
+
(0, transformer_1.setPropertyTypeTransformer)(target, transformer_2.HTML, transformer_3.OUTPUT, storeOutput);
|
|
26
|
+
}
|
|
27
|
+
function initStoreSqlTransformers(target) {
|
|
28
|
+
(0, transformer_1.setPropertyTypeTransformer)(target, transformer_3.SQL, transformer_3.SAVE, storeSave);
|
|
29
|
+
}
|
|
30
|
+
function initStoreTransformers(target) {
|
|
31
|
+
initStoreHtmlTransformers(target);
|
|
32
|
+
initStoreSqlTransformers(target);
|
|
33
|
+
}
|
|
34
|
+
function setStoreDependencies(dependencies) {
|
|
35
|
+
Object.assign(depends, dependencies);
|
|
36
|
+
}
|
|
37
|
+
exports.setStoreHtmlDependencies = setStoreDependencies;
|
|
38
|
+
exports.setStoreSqlDependencies = setStoreDependencies;
|
|
39
|
+
function storeEdit(value, object, property) {
|
|
40
|
+
const propertyType = new reflect_1.ReflectProperty(object, property).type;
|
|
41
|
+
const representative = value ? depends.representativeValueOf(value) : '';
|
|
42
|
+
const fetch = depends.routeOf(propertyType) + '/summary';
|
|
43
|
+
const label = `<label for="${property}">${depends.tr(depends.displayOf(object, property))}</label>`;
|
|
44
|
+
const name = `id="${property}" name="${property}"`;
|
|
45
|
+
const inputValue = representative.length ? ` value="${representative}"` : '';
|
|
46
|
+
const input = `<input data-fetch="${fetch}" data-type="object" ${name}${inputValue}>`;
|
|
47
|
+
const input_id = `<input id="${property}_id" name="${property}_id" type="hidden" value="${value?.id}">`;
|
|
48
|
+
return label + lfTab + input + input_id;
|
|
49
|
+
}
|
|
50
|
+
function storeInput(value, object, property, data) {
|
|
51
|
+
const property_id = property + '_id';
|
|
52
|
+
if ((property_id in data)
|
|
53
|
+
&& ((property_id in object)
|
|
54
|
+
? (data[property_id] !== object[property_id] + '')
|
|
55
|
+
: (data[property_id] !== value?.id + ''))) {
|
|
56
|
+
delete object[property];
|
|
57
|
+
Object.assign(object, { [property_id]: +data[property_id] });
|
|
58
|
+
}
|
|
59
|
+
return depends.ignoreTransformedValue;
|
|
60
|
+
}
|
|
61
|
+
function storeOutput(value) {
|
|
62
|
+
return value ? depends.representativeValueOf(value) : '';
|
|
63
|
+
}
|
|
64
|
+
async function storeSave(value, _object, property, saveValues) {
|
|
65
|
+
const dao = (0, storage_1.dataSource)();
|
|
66
|
+
if (value && !dao.isObjectConnected(value)) {
|
|
67
|
+
await dao.save(value);
|
|
68
|
+
}
|
|
69
|
+
const property_id = property + '_id';
|
|
70
|
+
const id = (value && dao.isObjectConnected(value)) ? value.id : saveValues[property_id];
|
|
71
|
+
saveValues[property_id] = id ?? null;
|
|
72
|
+
return depends.ignoreTransformedValue;
|
|
73
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { AnyObject } from '@itrocks/class-type';
|
|
2
|
+
import { Type } from '@itrocks/class-type';
|
|
3
|
+
export type Dependencies = {
|
|
4
|
+
displayOf: (object: AnyObject, property: string) => string;
|
|
5
|
+
ignoreTransformedValue: any;
|
|
6
|
+
representativeValueOf: (object: object) => string;
|
|
7
|
+
routeOf: (type: Type) => string;
|
|
8
|
+
tr: (text: string) => string;
|
|
9
|
+
};
|
|
10
|
+
export declare function initCollectionHtmlTransformers(dependencies?: Partial<Dependencies>): void;
|
|
11
|
+
export declare function initCollectionSqlTransformers(): void;
|
|
12
|
+
export declare function initCollectionTransformers(dependencies?: Partial<Dependencies>): void;
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import { componentOf, compositeOf } from '@itrocks/composition';
|
|
2
|
+
import { dataSource } from '@itrocks/storage';
|
|
3
|
+
import { baseType } from '@itrocks/class-type';
|
|
4
|
+
import { typeOf } from '@itrocks/class-type';
|
|
5
|
+
import { CollectionType } from '@itrocks/property-type';
|
|
6
|
+
import { ReflectClass } from '@itrocks/reflect';
|
|
7
|
+
import { ReflectProperty } from '@itrocks/reflect';
|
|
8
|
+
import { EDIT, HTML } from '@itrocks/transformer';
|
|
9
|
+
import { INPUT, OUTPUT, SAVE, SQL } from '@itrocks/transformer';
|
|
10
|
+
import { setPropertyTypeTransformer } from '@itrocks/transformer';
|
|
11
|
+
const depends = {
|
|
12
|
+
displayOf: (_object, property) => property,
|
|
13
|
+
ignoreTransformedValue: Symbol('ignoreTransformedValue'),
|
|
14
|
+
representativeValueOf: object => baseType(typeOf(object)).name,
|
|
15
|
+
routeOf: type => '/' + baseType(type).name,
|
|
16
|
+
tr: text => text
|
|
17
|
+
};
|
|
18
|
+
const areMayEntityEntries = (entries) => (typeof entries[0]?.[1])[0] === 'o';
|
|
19
|
+
function collectionEdit(values, object, property) {
|
|
20
|
+
const propertyType = new ReflectProperty(object, property).collectionType;
|
|
21
|
+
const fetch = depends.routeOf(propertyType?.elementType) + '/summary';
|
|
22
|
+
const label = `<label for="${property}">${depends.tr(depends.displayOf(object, property))}</label>`;
|
|
23
|
+
const inputs = [];
|
|
24
|
+
for (const object of values) {
|
|
25
|
+
const attrValue = `value="${depends.representativeValueOf(object)}"`;
|
|
26
|
+
inputs.push('<li>' + (dataSource().isObjectConnected(object)
|
|
27
|
+
? `<input id="${property}.${object.id}" name="${property}.${object.id}" ${attrValue}>`
|
|
28
|
+
: `<input id="${property}." name="${property}." ${attrValue}>`) + '</li>');
|
|
29
|
+
}
|
|
30
|
+
return label + `<ul data-multiple-contained-auto-width data-fetch="${fetch}" data-type="objects">`
|
|
31
|
+
+ inputs.join('')
|
|
32
|
+
+ `<li><input id="${property}" name="${property}" placeholder="+"></li>`
|
|
33
|
+
+ '</ul>';
|
|
34
|
+
}
|
|
35
|
+
function collectionInput(values, object, property) {
|
|
36
|
+
const entries = Object.entries(values);
|
|
37
|
+
if (areMayEntityEntries(entries)) {
|
|
38
|
+
Object.assign(object, { [property]: entries.map(([id, value]) => dataSource().connectObject(value, +id)) });
|
|
39
|
+
}
|
|
40
|
+
else {
|
|
41
|
+
delete object[property];
|
|
42
|
+
Object.assign(object, { [property + '_ids']: Object.keys(values).map(id => +id) });
|
|
43
|
+
}
|
|
44
|
+
return depends.ignoreTransformedValue;
|
|
45
|
+
}
|
|
46
|
+
function collectionOutput(values, object, property, askFor) {
|
|
47
|
+
if (!values.length) {
|
|
48
|
+
return '';
|
|
49
|
+
}
|
|
50
|
+
if (componentOf(object, property)) {
|
|
51
|
+
const propertyType = new ReflectProperty(object, property).type;
|
|
52
|
+
if (propertyType instanceof CollectionType) {
|
|
53
|
+
const type = propertyType.elementType;
|
|
54
|
+
const propertyClass = new ReflectClass(type);
|
|
55
|
+
const properties = propertyClass.propertyNames.filter(property => !compositeOf(type, property));
|
|
56
|
+
const html = [];
|
|
57
|
+
html.push('<table>');
|
|
58
|
+
html.push('<tr>' + properties.map(property => '<th>' + depends.tr(property) + '</th>').join('') + '</tr>');
|
|
59
|
+
html.push(...values.map(value => '<tr>' + properties.map(property => '<td>' + value[property] + '</td>').join('') + '</tr>'));
|
|
60
|
+
html.push('</table>');
|
|
61
|
+
return html.join('\n');
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
if (askFor?.container) {
|
|
65
|
+
askFor.container = false;
|
|
66
|
+
return '<ul>' + values.map(object => '<li>' + depends.representativeValueOf(object) + '</li>').join('') + '</ul>';
|
|
67
|
+
}
|
|
68
|
+
return values.map(object => depends.representativeValueOf(object)).join(', ');
|
|
69
|
+
}
|
|
70
|
+
async function collectionSave(values, object, property) {
|
|
71
|
+
const dao = dataSource();
|
|
72
|
+
const newIdsPromise = object[property + '_ids']
|
|
73
|
+
?? values?.map(async (value) => (dao.isObjectConnected(value) ? value : await dao.save(value)).id).sort()
|
|
74
|
+
?? [];
|
|
75
|
+
const previousIdsPromise = dao.isObjectConnected(object)
|
|
76
|
+
? await dao.readCollectionIds(object, property)
|
|
77
|
+
: [];
|
|
78
|
+
return async (object) => {
|
|
79
|
+
const previousIds = await Promise.all(previousIdsPromise);
|
|
80
|
+
const newIds = await Promise.all(newIdsPromise);
|
|
81
|
+
for (const id of previousIds) {
|
|
82
|
+
if (newIds.includes(id))
|
|
83
|
+
continue;
|
|
84
|
+
dao.deleteRelatedId(object, property, id);
|
|
85
|
+
}
|
|
86
|
+
for (const id of newIds) {
|
|
87
|
+
if (previousIds.includes(id))
|
|
88
|
+
continue;
|
|
89
|
+
dao.insertRelatedId(object, property, id);
|
|
90
|
+
}
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
export function initCollectionHtmlTransformers(dependencies = {}) {
|
|
94
|
+
Object.assign(depends, dependencies);
|
|
95
|
+
setPropertyTypeTransformer(CollectionType, HTML, EDIT, collectionEdit);
|
|
96
|
+
setPropertyTypeTransformer(CollectionType, HTML, INPUT, collectionInput);
|
|
97
|
+
setPropertyTypeTransformer(CollectionType, HTML, OUTPUT, collectionOutput);
|
|
98
|
+
}
|
|
99
|
+
export function initCollectionSqlTransformers() {
|
|
100
|
+
setPropertyTypeTransformer(CollectionType, SQL, SAVE, collectionSave);
|
|
101
|
+
}
|
|
102
|
+
export function initCollectionTransformers(dependencies = {}) {
|
|
103
|
+
initCollectionHtmlTransformers(dependencies);
|
|
104
|
+
initCollectionSqlTransformers();
|
|
105
|
+
}
|
package/esm/container.js
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { HTML, setFormatTransformer } from '@itrocks/transformer';
|
|
2
|
+
export class HtmlContainer {
|
|
3
|
+
mandatoryContainer;
|
|
4
|
+
container;
|
|
5
|
+
constructor(mandatoryContainer, container = true) {
|
|
6
|
+
this.mandatoryContainer = mandatoryContainer;
|
|
7
|
+
this.container = container;
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
export function initContainerTransformers() {
|
|
11
|
+
setFormatTransformer(HTML, (value, askFor) => (askFor.container && askFor.mandatoryContainer) ? ('<div>' + value + '</div>') : value);
|
|
12
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { Dependencies as CollectionTypeDependencies } from './collection-type';
|
|
2
|
+
import { Dependencies as PrimitiveDependencies } from './primitive';
|
|
3
|
+
import { Dependencies as StoreDependencies } from './store';
|
|
4
|
+
export { HtmlContainer, initContainerTransformers } from './container';
|
|
5
|
+
export { initCollectionHtmlTransformers, initCollectionSqlTransformers, initCollectionTransformers } from './collection-type';
|
|
6
|
+
export { initBigintHtmlTransformers, initBooleanHtmlTransformers, initBooleanSqlTransformers, initDateHtmlTransformers, initDefaultHtmlEditTransformers, initNumberHtmlTransformers, initPrimitiveTransformers, setCorePrimitiveDependencies, setPrimitiveDependencies } from './primitive';
|
|
7
|
+
export { initStoreHtmlTransformers, initStoreSqlTransformers, initStoreTransformers, setStoreDependencies, setStoreSqlDependencies, setStoreHtmlDependencies } from './store';
|
|
8
|
+
type Dependencies = CollectionTypeDependencies & PrimitiveDependencies & StoreDependencies;
|
|
9
|
+
export declare function initCoreTransformers(dependencies: Partial<Dependencies>): void;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { initCollectionTransformers } from './collection-type.js';
|
|
2
|
+
import { initContainerTransformers } from './container.js';
|
|
3
|
+
import { initPrimitiveTransformers } from './primitive.js';
|
|
4
|
+
import { setStoreDependencies } from './store.js';
|
|
5
|
+
export { HtmlContainer, initContainerTransformers } from './container.js';
|
|
6
|
+
export { initCollectionHtmlTransformers, initCollectionSqlTransformers, initCollectionTransformers } from './collection-type.js';
|
|
7
|
+
export { initBigintHtmlTransformers, initBooleanHtmlTransformers, initBooleanSqlTransformers, initDateHtmlTransformers, initDefaultHtmlEditTransformers, initNumberHtmlTransformers, initPrimitiveTransformers, setCorePrimitiveDependencies, setPrimitiveDependencies } from './primitive.js';
|
|
8
|
+
export { initStoreHtmlTransformers, initStoreSqlTransformers, initStoreTransformers, setStoreDependencies, setStoreSqlDependencies, setStoreHtmlDependencies } from './store.js';
|
|
9
|
+
export function initCoreTransformers(dependencies) {
|
|
10
|
+
initCollectionTransformers(dependencies);
|
|
11
|
+
initContainerTransformers();
|
|
12
|
+
initPrimitiveTransformers(dependencies);
|
|
13
|
+
setStoreDependencies(dependencies);
|
|
14
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { AnyObject } from '@itrocks/class-type';
|
|
2
|
+
export type CoreDependencies = {
|
|
3
|
+
displayOf: (object: AnyObject, property: string) => string;
|
|
4
|
+
tr: (text: string) => string;
|
|
5
|
+
};
|
|
6
|
+
export type Dependencies = CoreDependencies & {
|
|
7
|
+
formatDate: (date: Date) => string;
|
|
8
|
+
parseDate: (date: string) => Date;
|
|
9
|
+
};
|
|
10
|
+
export declare function initBigintHtmlTransformers(): void;
|
|
11
|
+
export declare function initBooleanHtmlTransformers(): void;
|
|
12
|
+
export declare function initBooleanSqlTransformers(): void;
|
|
13
|
+
export declare function initDateHtmlTransformers(): void;
|
|
14
|
+
export declare function initNumberHtmlTransformers(): void;
|
|
15
|
+
export declare function initDefaultHtmlEditTransformers(): void;
|
|
16
|
+
export declare function initPrimitiveTransformers(dependencies?: Partial<Dependencies>): void;
|
|
17
|
+
export declare function setCorePrimitiveDependencies(dependencies: Partial<CoreDependencies>): void;
|
|
18
|
+
export declare function setPrimitiveDependencies(dependencies: Partial<Dependencies>): void;
|
package/esm/primitive.js
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import { setPropertyTypeTransformers } from '@itrocks/transformer';
|
|
2
|
+
import { EDIT, HTML, INPUT, OUTPUT } from '@itrocks/transformer';
|
|
3
|
+
import { READ, SAVE, SQL } from '@itrocks/transformer';
|
|
4
|
+
const lfTab = '\n\t\t\t\t';
|
|
5
|
+
const depends = {
|
|
6
|
+
displayOf: (_object, property) => property,
|
|
7
|
+
formatDate: date => date.toString(),
|
|
8
|
+
parseDate: date => new Date(date),
|
|
9
|
+
tr: text => text
|
|
10
|
+
};
|
|
11
|
+
// Bigint
|
|
12
|
+
export function initBigintHtmlTransformers() {
|
|
13
|
+
setPropertyTypeTransformers(BigInt, [
|
|
14
|
+
{ format: HTML, direction: INPUT, transformer: (value) => BigInt(value) }
|
|
15
|
+
]);
|
|
16
|
+
}
|
|
17
|
+
// Boolean
|
|
18
|
+
function booleanEdit(value, type, property) {
|
|
19
|
+
const label = `<label for="${property}">${depends.tr(depends.displayOf(type, property))}</label>`;
|
|
20
|
+
const name = `id="${property}" name="${property}"`;
|
|
21
|
+
const hidden = `<input name="${property}" type="hidden" value="0">`;
|
|
22
|
+
const checked = value ? 'checked ' : '';
|
|
23
|
+
const checkbox = `<input ${checked}${name} type="checkbox" value="1">`;
|
|
24
|
+
return label + lfTab + hidden + lfTab + checkbox;
|
|
25
|
+
}
|
|
26
|
+
const booleanInput = (value) => !['', '0', 'false', 'no', depends.tr('false'), depends.tr('no')].includes(value);
|
|
27
|
+
export function initBooleanHtmlTransformers() {
|
|
28
|
+
setPropertyTypeTransformers(Boolean, [
|
|
29
|
+
{ format: HTML, direction: EDIT, transformer: booleanEdit },
|
|
30
|
+
{ format: HTML, direction: INPUT, transformer: booleanInput },
|
|
31
|
+
{ format: HTML, direction: OUTPUT, transformer: (value) => value ? depends.tr('yes') : depends.tr('no') }
|
|
32
|
+
]);
|
|
33
|
+
}
|
|
34
|
+
export function initBooleanSqlTransformers() {
|
|
35
|
+
setPropertyTypeTransformers(Boolean, [
|
|
36
|
+
{ format: SQL, direction: READ, transformer: (value) => !!value },
|
|
37
|
+
{ format: SQL, direction: SAVE, transformer: (value) => +value }
|
|
38
|
+
]);
|
|
39
|
+
}
|
|
40
|
+
// Date
|
|
41
|
+
function dateEdit(value, type, property) {
|
|
42
|
+
const label = `<label for="${property}">${depends.tr(depends.displayOf(type, property))}</label>`;
|
|
43
|
+
const name = `id="${property}" name="${property}"`;
|
|
44
|
+
const inputValue = value ? ` value="${depends.formatDate(value)}"` : '';
|
|
45
|
+
const input = `<input data-type="date" ${name}${inputValue}>`;
|
|
46
|
+
return label + lfTab + input;
|
|
47
|
+
}
|
|
48
|
+
const dateInput = (value) => depends.parseDate(value);
|
|
49
|
+
const dateOutput = (value) => value ? depends.formatDate(value) : '';
|
|
50
|
+
export function initDateHtmlTransformers() {
|
|
51
|
+
setPropertyTypeTransformers(Date, [
|
|
52
|
+
{ format: HTML, direction: EDIT, transformer: dateEdit },
|
|
53
|
+
{ format: HTML, direction: INPUT, transformer: dateInput },
|
|
54
|
+
{ format: HTML, direction: OUTPUT, transformer: dateOutput }
|
|
55
|
+
]);
|
|
56
|
+
}
|
|
57
|
+
// Number
|
|
58
|
+
function numberEdit(value, type, property) {
|
|
59
|
+
const label = `<label for="${property}">${depends.tr(depends.displayOf(type, property))}</label>`;
|
|
60
|
+
const name = `id="${property}" name="${property}"`;
|
|
61
|
+
const inputValue = (value !== undefined) ? ` value="${value}"` : '';
|
|
62
|
+
const input = `<input data-type="number" ${name}${inputValue}>`;
|
|
63
|
+
return label + lfTab + input;
|
|
64
|
+
}
|
|
65
|
+
export function initNumberHtmlTransformers() {
|
|
66
|
+
setPropertyTypeTransformers(Number, [
|
|
67
|
+
{ format: HTML, direction: EDIT, transformer: numberEdit },
|
|
68
|
+
{ format: HTML, direction: INPUT, transformer: (value) => +value }
|
|
69
|
+
]);
|
|
70
|
+
}
|
|
71
|
+
// default
|
|
72
|
+
function defaultEdit(value, type, property) {
|
|
73
|
+
const label = `<label for="${property}">${depends.tr(depends.displayOf(type, property))}</label>`;
|
|
74
|
+
const name = `id="${property}" name="${property}"`;
|
|
75
|
+
const inputValue = value ? ` value="${value}"` : '';
|
|
76
|
+
const input = `<input ${name}${inputValue}>`;
|
|
77
|
+
return label + lfTab + input;
|
|
78
|
+
}
|
|
79
|
+
export function initDefaultHtmlEditTransformers() {
|
|
80
|
+
setPropertyTypeTransformers(null, [{ format: HTML, direction: EDIT, transformer: defaultEdit }]);
|
|
81
|
+
}
|
|
82
|
+
// all
|
|
83
|
+
export function initPrimitiveTransformers(dependencies = {}) {
|
|
84
|
+
setPrimitiveDependencies(dependencies);
|
|
85
|
+
initBigintHtmlTransformers();
|
|
86
|
+
initBooleanSqlTransformers();
|
|
87
|
+
initBooleanHtmlTransformers();
|
|
88
|
+
initDateHtmlTransformers();
|
|
89
|
+
initNumberHtmlTransformers();
|
|
90
|
+
initDefaultHtmlEditTransformers();
|
|
91
|
+
}
|
|
92
|
+
// Initialize dependencies required by all core primitive transformers except Date
|
|
93
|
+
export function setCorePrimitiveDependencies(dependencies) {
|
|
94
|
+
Object.assign(depends, dependencies);
|
|
95
|
+
}
|
|
96
|
+
// Initialize dependencies required by all core primitive transformers, including Date
|
|
97
|
+
export function setPrimitiveDependencies(dependencies) {
|
|
98
|
+
Object.assign(depends, dependencies);
|
|
99
|
+
}
|
package/esm/store.d.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { AnyObject } from '@itrocks/class-type';
|
|
2
|
+
import { Type } from '@itrocks/class-type';
|
|
3
|
+
export type SqlDependencies = {
|
|
4
|
+
ignoreTransformedValue: any;
|
|
5
|
+
};
|
|
6
|
+
export type Dependencies = SqlDependencies & {
|
|
7
|
+
displayOf: (object: AnyObject, property: string) => string;
|
|
8
|
+
representativeValueOf: (object: object) => string;
|
|
9
|
+
routeOf: (type: Type) => string;
|
|
10
|
+
tr: (text: string) => string;
|
|
11
|
+
};
|
|
12
|
+
export declare function initStoreHtmlTransformers(target: Type): void;
|
|
13
|
+
export declare function initStoreSqlTransformers(target: Type): void;
|
|
14
|
+
export declare function initStoreTransformers(target: Type): void;
|
|
15
|
+
export declare function setStoreDependencies(dependencies: Partial<Dependencies>): void;
|
|
16
|
+
export declare const setStoreHtmlDependencies: typeof setStoreDependencies;
|
|
17
|
+
export declare const setStoreSqlDependencies: (dependencies: Partial<SqlDependencies>) => void;
|
package/esm/store.js
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { baseType, typeOf } from '@itrocks/class-type';
|
|
2
|
+
import { ReflectProperty } from '@itrocks/reflect';
|
|
3
|
+
import { dataSource } from '@itrocks/storage';
|
|
4
|
+
import { setPropertyTypeTransformer } from '@itrocks/transformer';
|
|
5
|
+
import { EDIT, HTML } from '@itrocks/transformer';
|
|
6
|
+
import { INPUT, OUTPUT, SAVE, SQL } from '@itrocks/transformer';
|
|
7
|
+
const lfTab = '\n\t\t\t\t';
|
|
8
|
+
const depends = {
|
|
9
|
+
displayOf: (_object, property) => property,
|
|
10
|
+
ignoreTransformedValue: Symbol('ignoreTransformedValue'),
|
|
11
|
+
representativeValueOf: object => baseType(typeOf(object)).name,
|
|
12
|
+
routeOf: type => '/' + baseType(type).name,
|
|
13
|
+
tr: text => text
|
|
14
|
+
};
|
|
15
|
+
export function initStoreHtmlTransformers(target) {
|
|
16
|
+
setPropertyTypeTransformer(target, HTML, EDIT, storeEdit);
|
|
17
|
+
setPropertyTypeTransformer(target, HTML, INPUT, storeInput);
|
|
18
|
+
setPropertyTypeTransformer(target, HTML, OUTPUT, storeOutput);
|
|
19
|
+
}
|
|
20
|
+
export function initStoreSqlTransformers(target) {
|
|
21
|
+
setPropertyTypeTransformer(target, SQL, SAVE, storeSave);
|
|
22
|
+
}
|
|
23
|
+
export function initStoreTransformers(target) {
|
|
24
|
+
initStoreHtmlTransformers(target);
|
|
25
|
+
initStoreSqlTransformers(target);
|
|
26
|
+
}
|
|
27
|
+
export function setStoreDependencies(dependencies) {
|
|
28
|
+
Object.assign(depends, dependencies);
|
|
29
|
+
}
|
|
30
|
+
export const setStoreHtmlDependencies = setStoreDependencies;
|
|
31
|
+
export const setStoreSqlDependencies = setStoreDependencies;
|
|
32
|
+
function storeEdit(value, object, property) {
|
|
33
|
+
const propertyType = new ReflectProperty(object, property).type;
|
|
34
|
+
const representative = value ? depends.representativeValueOf(value) : '';
|
|
35
|
+
const fetch = depends.routeOf(propertyType) + '/summary';
|
|
36
|
+
const label = `<label for="${property}">${depends.tr(depends.displayOf(object, property))}</label>`;
|
|
37
|
+
const name = `id="${property}" name="${property}"`;
|
|
38
|
+
const inputValue = representative.length ? ` value="${representative}"` : '';
|
|
39
|
+
const input = `<input data-fetch="${fetch}" data-type="object" ${name}${inputValue}>`;
|
|
40
|
+
const input_id = `<input id="${property}_id" name="${property}_id" type="hidden" value="${value?.id}">`;
|
|
41
|
+
return label + lfTab + input + input_id;
|
|
42
|
+
}
|
|
43
|
+
function storeInput(value, object, property, data) {
|
|
44
|
+
const property_id = property + '_id';
|
|
45
|
+
if ((property_id in data)
|
|
46
|
+
&& ((property_id in object)
|
|
47
|
+
? (data[property_id] !== object[property_id] + '')
|
|
48
|
+
: (data[property_id] !== value?.id + ''))) {
|
|
49
|
+
delete object[property];
|
|
50
|
+
Object.assign(object, { [property_id]: +data[property_id] });
|
|
51
|
+
}
|
|
52
|
+
return depends.ignoreTransformedValue;
|
|
53
|
+
}
|
|
54
|
+
function storeOutput(value) {
|
|
55
|
+
return value ? depends.representativeValueOf(value) : '';
|
|
56
|
+
}
|
|
57
|
+
async function storeSave(value, _object, property, saveValues) {
|
|
58
|
+
const dao = dataSource();
|
|
59
|
+
if (value && !dao.isObjectConnected(value)) {
|
|
60
|
+
await dao.save(value);
|
|
61
|
+
}
|
|
62
|
+
const property_id = property + '_id';
|
|
63
|
+
const id = (value && dao.isObjectConnected(value)) ? value.id : saveValues[property_id];
|
|
64
|
+
saveValues[property_id] = id ?? null;
|
|
65
|
+
return depends.ignoreTransformedValue;
|
|
66
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
{
|
|
2
|
+
"author": {
|
|
3
|
+
"name": "Baptiste Pillot",
|
|
4
|
+
"email": "baptiste@pillot.fr"
|
|
5
|
+
},
|
|
6
|
+
"dependencies": {
|
|
7
|
+
"@itrocks/composition": "latest",
|
|
8
|
+
"@itrocks/storage": "latest",
|
|
9
|
+
"@itrocks/class-type": "latest",
|
|
10
|
+
"@itrocks/property-type": "latest",
|
|
11
|
+
"@itrocks/reflect": "latest",
|
|
12
|
+
"@itrocks/transformer": "latest"
|
|
13
|
+
},
|
|
14
|
+
"description": "",
|
|
15
|
+
"devDependencies": {
|
|
16
|
+
"@types/node": "^22.10",
|
|
17
|
+
"typescript": "^5.6"
|
|
18
|
+
},
|
|
19
|
+
"engines": {
|
|
20
|
+
"node": ">=18"
|
|
21
|
+
},
|
|
22
|
+
"exports": {
|
|
23
|
+
"import": "./esm/core-transformers.js",
|
|
24
|
+
"require": "./cjs/core-transformers.js"
|
|
25
|
+
},
|
|
26
|
+
"files": [
|
|
27
|
+
"LICENSE",
|
|
28
|
+
"README.md",
|
|
29
|
+
"*/*.d.ts",
|
|
30
|
+
"*/*.js",
|
|
31
|
+
"!*/esm.*"
|
|
32
|
+
],
|
|
33
|
+
"homepage": "https://it.rocks",
|
|
34
|
+
"keywords": [
|
|
35
|
+
"backend",
|
|
36
|
+
"it.rocks"
|
|
37
|
+
],
|
|
38
|
+
"license": "ISC",
|
|
39
|
+
"name": "@itrocks/core-transformers",
|
|
40
|
+
"repository": {
|
|
41
|
+
"type": "git",
|
|
42
|
+
"url": "git+https://github.com/itrocks-ts/core-transformers.git"
|
|
43
|
+
},
|
|
44
|
+
"scripts": {
|
|
45
|
+
"build": "npm run build:cjs && npm run build:esm",
|
|
46
|
+
"build:cjs": "tsc -p tsconfig.cjs.json",
|
|
47
|
+
"build:esm": "tsc -p tsconfig.esm.json && node esm/esm"
|
|
48
|
+
},
|
|
49
|
+
"types": "./esm/core-transformers.d.ts",
|
|
50
|
+
"version": "0.0.1"
|
|
51
|
+
}
|