@ja-platform/parser-schema-xml 0.0.0-stage → 1.0.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 (99) hide show
  1. package/dist/@types/SchemaNode.d.ts +13 -0
  2. package/dist/@types/XmlField.d.ts +10 -0
  3. package/dist/@types/XmlModel.d.ts +16 -0
  4. package/dist/@types/XmlParam.d.ts +7 -0
  5. package/dist/@types/XmlSchema.d.ts +20 -0
  6. package/dist/@types/attributes/SchemaDatabaseAttribute.d.ts +11 -0
  7. package/dist/@types/attributes/SchemaEnumAttribute.d.ts +6 -0
  8. package/dist/@types/attributes/SchemaFieldTypeAttribute.d.ts +9 -0
  9. package/dist/@types/attributes/SchemaIndexAttribute.d.ts +9 -0
  10. package/dist/@types/attributes/SchemaNameAttribute.d.ts +4 -0
  11. package/dist/@types/attributes/SchemaOptionKeyAttribute.d.ts +16 -0
  12. package/dist/@types/expressions/SchemaDatabasesExpression.d.ts +10 -0
  13. package/dist/@types/expressions/SchemaFieldTypeExpression.d.ts +14 -0
  14. package/dist/@types/expressions/SchemaIndexExpression.d.ts +14 -0
  15. package/dist/@types/index.d.ts +10 -0
  16. package/dist/@types/loadSchemaXml.d.ts +8 -0
  17. package/dist/@types/nodes/SchemaBelongsToNode.d.ts +9 -0
  18. package/dist/@types/nodes/SchemaDisplayNode.d.ts +8 -0
  19. package/dist/@types/nodes/SchemaFieldNode.d.ts +21 -0
  20. package/dist/@types/nodes/SchemaHasManyNode.d.ts +10 -0
  21. package/dist/@types/nodes/SchemaIndexNode.d.ts +12 -0
  22. package/dist/@types/nodes/SchemaModelNode.d.ts +26 -0
  23. package/dist/@types/nodes/SchemaNotificationNode.d.ts +12 -0
  24. package/dist/@types/nodes/SchemaOptionNode.d.ts +10 -0
  25. package/dist/@types/nodes/SchemaWebhookFieldNode.d.ts +12 -0
  26. package/dist/@types/nodes/SchemaWebhookNode.d.ts +14 -0
  27. package/dist/@types/parsed/ParsedSchemaFieldNode.d.ts +7 -0
  28. package/dist/@types/parsed/ParsedSchemaModelNode.d.ts +15 -0
  29. package/dist/@types/parsed/ParsedSchemaNode.d.ts +10 -0
  30. package/dist/@types/parsed/sourceElement.d.ts +4 -0
  31. package/dist/@types/schemaSerializer.d.ts +11 -0
  32. package/dist/@types/tokens/SchemaIndexKeyToken.d.ts +14 -0
  33. package/dist/SchemaNode.js +20 -0
  34. package/dist/SchemaNode.js.map +1 -0
  35. package/dist/XmlField.js +12 -0
  36. package/dist/XmlField.js.map +1 -0
  37. package/dist/XmlModel.js +8 -0
  38. package/dist/XmlModel.js.map +1 -0
  39. package/dist/XmlParam.js +12 -0
  40. package/dist/XmlParam.js.map +1 -0
  41. package/dist/XmlSchema.js +29 -0
  42. package/dist/XmlSchema.js.map +1 -0
  43. package/dist/attributes/SchemaDatabaseAttribute.js +12 -0
  44. package/dist/attributes/SchemaDatabaseAttribute.js.map +1 -0
  45. package/dist/attributes/SchemaEnumAttribute.js +27 -0
  46. package/dist/attributes/SchemaEnumAttribute.js.map +1 -0
  47. package/dist/attributes/SchemaFieldTypeAttribute.js +12 -0
  48. package/dist/attributes/SchemaFieldTypeAttribute.js.map +1 -0
  49. package/dist/attributes/SchemaIndexAttribute.js +12 -0
  50. package/dist/attributes/SchemaIndexAttribute.js.map +1 -0
  51. package/dist/attributes/SchemaNameAttribute.js +23 -0
  52. package/dist/attributes/SchemaNameAttribute.js.map +1 -0
  53. package/dist/attributes/SchemaOptionKeyAttribute.js +30 -0
  54. package/dist/attributes/SchemaOptionKeyAttribute.js.map +1 -0
  55. package/dist/expressions/SchemaDatabasesExpression.js +28 -0
  56. package/dist/expressions/SchemaDatabasesExpression.js.map +1 -0
  57. package/dist/expressions/SchemaFieldTypeExpression.js +50 -0
  58. package/dist/expressions/SchemaFieldTypeExpression.js.map +1 -0
  59. package/dist/expressions/SchemaIndexExpression.js +30 -0
  60. package/dist/expressions/SchemaIndexExpression.js.map +1 -0
  61. package/dist/index.js +27 -0
  62. package/dist/index.js.map +1 -0
  63. package/dist/loadSchemaXml.js +46 -0
  64. package/dist/loadSchemaXml.js.map +1 -0
  65. package/dist/nodes/SchemaBelongsToNode.js +14 -0
  66. package/dist/nodes/SchemaBelongsToNode.js.map +1 -0
  67. package/dist/nodes/SchemaDisplayNode.js +17 -0
  68. package/dist/nodes/SchemaDisplayNode.js.map +1 -0
  69. package/dist/nodes/SchemaFieldNode.js +30 -0
  70. package/dist/nodes/SchemaFieldNode.js.map +1 -0
  71. package/dist/nodes/SchemaHasManyNode.js +15 -0
  72. package/dist/nodes/SchemaHasManyNode.js.map +1 -0
  73. package/dist/nodes/SchemaIndexNode.js +22 -0
  74. package/dist/nodes/SchemaIndexNode.js.map +1 -0
  75. package/dist/nodes/SchemaModelNode.js +34 -0
  76. package/dist/nodes/SchemaModelNode.js.map +1 -0
  77. package/dist/nodes/SchemaNotificationNode.js +21 -0
  78. package/dist/nodes/SchemaNotificationNode.js.map +1 -0
  79. package/dist/nodes/SchemaOptionNode.js +20 -0
  80. package/dist/nodes/SchemaOptionNode.js.map +1 -0
  81. package/dist/nodes/SchemaWebhookFieldNode.js +22 -0
  82. package/dist/nodes/SchemaWebhookFieldNode.js.map +1 -0
  83. package/dist/nodes/SchemaWebhookNode.js +20 -0
  84. package/dist/nodes/SchemaWebhookNode.js.map +1 -0
  85. package/dist/parsed/ParsedSchemaFieldNode.js +56 -0
  86. package/dist/parsed/ParsedSchemaFieldNode.js.map +1 -0
  87. package/dist/parsed/ParsedSchemaModelNode.js +113 -0
  88. package/dist/parsed/ParsedSchemaModelNode.js.map +1 -0
  89. package/dist/parsed/ParsedSchemaNode.js +29 -0
  90. package/dist/parsed/ParsedSchemaNode.js.map +1 -0
  91. package/dist/parsed/sourceElement.js +12 -0
  92. package/dist/parsed/sourceElement.js.map +1 -0
  93. package/dist/schemaSerializer.js +181 -0
  94. package/dist/schemaSerializer.js.map +1 -0
  95. package/dist/tokens/SchemaIndexKeyToken.js +39 -0
  96. package/dist/tokens/SchemaIndexKeyToken.js.map +1 -0
  97. package/dist/tsconfig.tsbuildinfo +1 -0
  98. package/package.json +41 -3
  99. package/README.md +0 -3
@@ -0,0 +1,20 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.SchemaWebhookNode = void 0;
4
+ const parser_core_1 = require("@ja-platform/parser-core");
5
+ const parser_schema_1 = require("@ja-platform/parser-schema");
6
+ const SchemaNameAttribute_1 = require("../attributes/SchemaNameAttribute");
7
+ const SchemaEnumAttribute_1 = require("../attributes/SchemaEnumAttribute");
8
+ const SchemaWebhookFieldNode_1 = require("./SchemaWebhookFieldNode");
9
+ class SchemaWebhookNode extends parser_core_1.Node {
10
+ constructor() {
11
+ super({ name: 'webhook', key: 'webhooks', isMultiple: true });
12
+ this.addAttribute(new SchemaNameAttribute_1.SchemaNameAttribute({ name: 'name' }));
13
+ this.addAttribute(new SchemaEnumAttribute_1.SchemaEnumAttribute({ name: 'type', values: Object.values(parser_schema_1.WebhookType), required: true }));
14
+ this.addAttribute(new SchemaEnumAttribute_1.SchemaEnumAttribute({ name: 'receiver', values: Object.values(parser_schema_1.WebhookReceiver) }));
15
+ this.addAttribute(new parser_core_1.ConstantTextAttribute({ name: 'action' }));
16
+ this.addNode(new SchemaWebhookFieldNode_1.SchemaWebhookFieldNode());
17
+ }
18
+ }
19
+ exports.SchemaWebhookNode = SchemaWebhookNode;
20
+ //# sourceMappingURL=SchemaWebhookNode.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"SchemaWebhookNode.js","sourceRoot":"","sources":["../../src/nodes/SchemaWebhookNode.ts"],"names":[],"mappings":";;;AAAA,0DAOkC;AAElC,8DAA0E;AAC1E,2EAAwE;AACxE,2EAAwE;AACxE,qEAA2F;AAU3F,uBAA+B,SAAQ,kBAAsC;IAC3E;QACE,KAAK,CAAC,EAAE,IAAI,EAAE,SAAS,EAAE,GAAG,EAAE,UAAU,EAAE,UAAU,EAAE,IAAI,EAAE,CAAC,CAAC;QAC9D,IAAI,CAAC,YAAY,CAAC,IAAI,yCAAmB,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,CAAC,CAAC,CAAC;QAC7D,IAAI,CAAC,YAAY,CAAC,IAAI,yCAAmB,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,CAAC,MAAM,CAAC,2BAAW,CAAC,EAAE,QAAQ,EAAE,IAAI,EAAE,CAAC,CAAC,CAAC;QACjH,IAAI,CAAC,YAAY,CAAC,IAAI,yCAAmB,CAAC,EAAE,IAAI,EAAE,UAAU,EAAE,MAAM,EAAE,MAAM,CAAC,MAAM,CAAC,+BAAe,CAAC,EAAE,CAAC,CAAC,CAAC;QACzG,IAAI,CAAC,YAAY,CAAC,IAAI,mCAAqB,CAAC,EAAE,IAAI,EAAE,QAAQ,EAAE,CAAC,CAAC,CAAC;QACjE,IAAI,CAAC,OAAO,CAAC,IAAI,+CAAsB,EAAE,CAAC,CAAC;IAC7C,CAAC;CACF","sourcesContent":["import {\n Node,\n NodeGenerics,\n ParsedNodeShape,\n ParsedNode,\n ParsedArrayNode,\n ConstantTextAttribute\n} from '@ja-platform/parser-core';\nimport { ConstantExpression } from '@ja-platform/parser-expressions';\nimport { WebhookReceiver, WebhookType } from '@ja-platform/parser-schema';\nimport { SchemaNameAttribute } from '../attributes/SchemaNameAttribute';\nimport { SchemaEnumAttribute } from '../attributes/SchemaEnumAttribute';\nimport { SchemaWebhookFieldNode, SchemaWebhookFieldShape } from './SchemaWebhookFieldNode';\n\nexport interface SchemaWebhookShape extends ParsedNodeShape {\n name?: ConstantExpression<string>;\n type: ConstantExpression<WebhookType>;\n receiver?: ConstantExpression<WebhookReceiver>;\n action?: ConstantExpression<string>;\n fields: ParsedArrayNode<ParsedNode<SchemaWebhookFieldShape>>;\n}\n\nexport class SchemaWebhookNode extends Node<NodeGenerics<SchemaWebhookShape>> {\n constructor() {\n super({ name: 'webhook', key: 'webhooks', isMultiple: true });\n this.addAttribute(new SchemaNameAttribute({ name: 'name' }));\n this.addAttribute(new SchemaEnumAttribute({ name: 'type', values: Object.values(WebhookType), required: true }));\n this.addAttribute(new SchemaEnumAttribute({ name: 'receiver', values: Object.values(WebhookReceiver) }));\n this.addAttribute(new ConstantTextAttribute({ name: 'action' }));\n this.addNode(new SchemaWebhookFieldNode());\n }\n}\n"]}
@@ -0,0 +1,56 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ParsedSchemaFieldNode = void 0;
4
+ const sourceElement_1 = require("./sourceElement");
5
+ const parser_core_1 = require("@ja-platform/parser-core");
6
+ const parser_schema_1 = require("@ja-platform/parser-schema");
7
+ const parser_schema_2 = require("@ja-platform/parser-schema");
8
+ class ParsedSchemaFieldNode extends parser_core_1.ParsedNode {
9
+ toField(schema) {
10
+ var _a, _b, _c, _d;
11
+ var _e, _f, _g, _h;
12
+ const declaration = this.attrExpression('type');
13
+ const type = schema.primitive(declaration.primitive);
14
+ const field = schema.field(this.attrExpression('name').value(), type);
15
+ field.label = this.attrExpression('label').value();
16
+ field.sourceTypeName = declaration.raw();
17
+ field.sourceElement = (0, sourceElement_1.sourceElement)(this);
18
+ field.parsedNode = this;
19
+ if (parser_schema_2.TextType.isInstanceOf(type)) {
20
+ type.spec = (_e = (_a = this.attrExpression('spec')) === null || _a === void 0 ? void 0 : _a.value()) !== null && _e !== void 0 ? _e : null;
21
+ if (declaration.subtype) {
22
+ type.subType = declaration.subtype;
23
+ type.spec = parser_schema_1.textSubtypeSpecs[declaration.subtype];
24
+ }
25
+ }
26
+ if (parser_schema_2.DateType.isInstanceOf(type)) {
27
+ type.isDay = true;
28
+ }
29
+ if (parser_schema_2.AttachmentType.isInstanceOf(type)) {
30
+ if (declaration.primitive === parser_schema_2.PrimitiveType.ATTACHMENT) {
31
+ type.media = (_f = (_b = this.attrExpression('media')) === null || _b === void 0 ? void 0 : _b.value()) !== null && _f !== void 0 ? _f : null;
32
+ }
33
+ type.autoDownload = (_g = (_c = this.attrExpression('autoDownload')) === null || _c === void 0 ? void 0 : _c.value()) !== null && _g !== void 0 ? _g : this.attribute('autoDownload').definition.defaultValue;
34
+ }
35
+ if (parser_schema_2.ChoiceType.isInstanceOf(type)) {
36
+ const options = this.node('options').nodes();
37
+ const integerKeys = declaration.primitive === parser_schema_2.PrimitiveType.SINGLE_CHOICE_INTEGER ||
38
+ declaration.primitive === parser_schema_2.PrimitiveType.MULTIPLE_CHOICE_INTEGER;
39
+ let largest = -1;
40
+ for (const [index, option] of options.entries()) {
41
+ const key = (_h = (_d = option.attrExpression('key')) === null || _d === void 0 ? void 0 : _d.value()) !== null && _h !== void 0 ? _h : (integerKeys ? largest + 1 : null);
42
+ const choice = type.addOption(key, option.attrExpression('label').value(), index);
43
+ field.optionSources.set(String(choice.value), (0, sourceElement_1.sourceElement)(option));
44
+ if (integerKeys)
45
+ largest = Math.max(largest, key);
46
+ }
47
+ if (declaration.primitive === parser_schema_2.PrimitiveType.BOOLEAN && options.length === 0) {
48
+ type.addOption(false, 'No', 0);
49
+ type.addOption(true, 'Yes', 1);
50
+ }
51
+ }
52
+ return field;
53
+ }
54
+ }
55
+ exports.ParsedSchemaFieldNode = ParsedSchemaFieldNode;
56
+ //# sourceMappingURL=ParsedSchemaFieldNode.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ParsedSchemaFieldNode.js","sourceRoot":"","sources":["../../src/parsed/ParsedSchemaFieldNode.ts"],"names":[],"mappings":";;;AAAA,mDAAgD;AAChD,0DAAsD;AAEtD,8DAA8D;AAC9D,8DAA2G;AAI3G,2BAAmC,SAAQ,wBAA4B;IACrE,OAAO,CAAC,MAAc;;;QACpB,MAAM,WAAW,GAAG,IAAI,CAAC,cAAc,CAAC,MAAM,CAAC,CAAC;QAChD,MAAM,IAAI,GAAG,MAAM,CAAC,SAAS,CAAC,WAAW,CAAC,SAAS,CAAC,CAAC;QACrD,MAAM,KAAK,GAAG,MAAM,CAAC,KAAK,CAAC,IAAI,CAAC,cAAc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AAC;QACD,OAAO,KAAK,CAAC;IACf,CAAC;CACF","sourcesContent":["import { sourceElement } from './sourceElement';\nimport { ParsedNode } from '@ja-platform/parser-core';\nimport { XmlSchema as Schema } from '../XmlSchema';\nimport { textSubtypeSpecs } from '@ja-platform/parser-schema';\nimport { AttachmentType, ChoiceType, DateType, PrimitiveType, TextType } from '@ja-platform/parser-schema';\nimport { XmlField as Field } from '../XmlField';\nimport type { SchemaFieldShape } from '../nodes/SchemaFieldNode';\n\nexport class ParsedSchemaFieldNode extends ParsedNode<SchemaFieldShape> {\n toField(schema: Schema): Field {\n const declaration = this.attrExpression('type');\n const type = schema.primitive(declaration.primitive);\n const field = schema.field(this.attrExpression('name').value(), type);\n field.label = this.attrExpression('label').value();\n field.sourceTypeName = declaration.raw();\n field.sourceElement = sourceElement(this);\n field.parsedNode = this;\n\n if (TextType.isInstanceOf(type)) {\n type.spec = this.attrExpression('spec')?.value() ?? null;\n if (declaration.subtype) {\n type.subType = declaration.subtype;\n type.spec = textSubtypeSpecs[declaration.subtype];\n }\n }\n if (DateType.isInstanceOf(type)) {\n type.isDay = true;\n }\n if (AttachmentType.isInstanceOf(type)) {\n if (declaration.primitive === PrimitiveType.ATTACHMENT) {\n type.media = this.attrExpression('media')?.value() ?? null;\n }\n type.autoDownload =\n this.attrExpression('autoDownload')?.value() ?? this.attribute('autoDownload').definition.defaultValue;\n }\n\n if (ChoiceType.isInstanceOf(type)) {\n const options = this.node('options').nodes();\n const integerKeys =\n declaration.primitive === PrimitiveType.SINGLE_CHOICE_INTEGER ||\n declaration.primitive === PrimitiveType.MULTIPLE_CHOICE_INTEGER;\n let largest = -1;\n for (const [index, option] of options.entries()) {\n const key = option.attrExpression('key')?.value() ?? (integerKeys ? largest + 1 : null);\n const choice = type.addOption(key, option.attrExpression('label').value(), index);\n field.optionSources.set(String(choice.value), sourceElement(option));\n if (integerKeys) largest = Math.max(largest, key as number);\n }\n if (declaration.primitive === PrimitiveType.BOOLEAN && options.length === 0) {\n type.addOption(false, 'No', 0);\n type.addOption(true, 'Yes', 1);\n }\n }\n return field;\n }\n}\n"]}
@@ -0,0 +1,113 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ParsedSchemaModelNode = void 0;
4
+ const sourceElement_1 = require("./sourceElement");
5
+ const parser_core_1 = require("@ja-platform/parser-core");
6
+ const parser_schema_1 = require("@ja-platform/parser-schema");
7
+ const parser_schema_2 = require("@ja-platform/parser-schema");
8
+ class ParsedSchemaModelNode extends parser_core_1.ParsedNode {
9
+ toModel(schema) {
10
+ var _a;
11
+ const model = schema.newObjectType(this.attrExpression('name').value());
12
+ model.label = this.attrExpression('label').value();
13
+ model.sourceElement = (0, sourceElement_1.sourceElement)(this);
14
+ model.parsedNode = this;
15
+ const fields = this.node('fields')
16
+ .nodes()
17
+ .map((field) => field.toField(schema));
18
+ for (const field of fields)
19
+ model.addAttribute(field);
20
+ const display = this.node('display');
21
+ model.displayFormat = (_a = display === null || display === void 0 ? void 0 : display.attrExpression('format')) !== null && _a !== void 0 ? _a : null;
22
+ model.displaySource = (0, sourceElement_1.sourceElement)(display);
23
+ model.allIndexes = this.node('indexes')
24
+ .nodes()
25
+ .map((index) => {
26
+ var _a, _b;
27
+ var _c, _d;
28
+ const expression = index.attrExpression('keys');
29
+ const keys = [...expression.keys];
30
+ return {
31
+ name: (_c = (_a = index.attrExpression('name')) === null || _a === void 0 ? void 0 : _a.value()) !== null && _c !== void 0 ? _c : keys.map((key) => key.field).join('_'),
32
+ on: expression.items.map((item) => item.raw()),
33
+ keys,
34
+ databases: [
35
+ ...((_d = (_b = index.attrExpression('databases')) === null || _b === void 0 ? void 0 : _b.databases) !== null && _d !== void 0 ? _d : index.attribute('databases').definition.defaultValue)
36
+ ]
37
+ };
38
+ });
39
+ model.indexes = model.allIndexes.filter((index) => index.databases.includes(parser_schema_1.IndexDatabase.APP));
40
+ model.notifyUsers = this.node('notifications')
41
+ .nodes()
42
+ .map((notification) => { var _a; var _b; return ({
43
+ message: notification.attrExpression('message'),
44
+ recipient: notification.attrExpression('recipient').value(),
45
+ received: notification.attrExpression('received').value(),
46
+ badgeCount: (_b = (_a = notification.attrExpression('badgeCount')) === null || _a === void 0 ? void 0 : _a.value()) !== null && _b !== void 0 ? _b : null
47
+ }); });
48
+ model.webhooks = this.node('webhooks')
49
+ .nodes()
50
+ .map((webhook) => {
51
+ var _a, _b, _c;
52
+ var _d, _e, _f;
53
+ const type = webhook.attrExpression('type').value();
54
+ const receiver = (_d = (_a = webhook.attrExpression('receiver')) === null || _a === void 0 ? void 0 : _a.value()) !== null && _d !== void 0 ? _d : null;
55
+ const action = (_e = (_b = webhook.attrExpression('action')) === null || _b === void 0 ? void 0 : _b.value()) !== null && _e !== void 0 ? _e : null;
56
+ return {
57
+ name: (_f = (_c = webhook.attrExpression('name')) === null || _c === void 0 ? void 0 : _c.value()) !== null && _f !== void 0 ? _f : (receiver ? `${receiver}_${model.name}_${type}_${action}` : `${model.name}_${type}`),
58
+ type,
59
+ receiver,
60
+ action,
61
+ sourceElement: (0, sourceElement_1.sourceElement)(webhook),
62
+ fields: webhook
63
+ .node('fields')
64
+ .nodes()
65
+ .map((field) => { var _a, _b, _c; var _d, _e, _f; return ({
66
+ name: field.attrExpression('name').value(),
67
+ required: (_d = (_a = field.attrExpression('required')) === null || _a === void 0 ? void 0 : _a.value()) !== null && _d !== void 0 ? _d : field.attribute('required').definition.defaultValue,
68
+ embed: (_e = (_b = field.attrExpression('embed')) === null || _b === void 0 ? void 0 : _b.value()) !== null && _e !== void 0 ? _e : field.attribute('embed').definition.defaultValue,
69
+ state: (_f = (_c = field.attrExpression('state')) === null || _c === void 0 ? void 0 : _c.value()) !== null && _f !== void 0 ? _f : field.attribute('state').definition.defaultValue
70
+ }); })
71
+ };
72
+ });
73
+ return model;
74
+ }
75
+ resolveBelongsTo({ schema, model }) {
76
+ var _a;
77
+ var _b;
78
+ for (const declaration of this.node('belongsTo').nodes()) {
79
+ const type = declaration.attrExpression('model').value();
80
+ const name = (_b = (_a = declaration.attrExpression('name')) === null || _a === void 0 ? void 0 : _a.value()) !== null && _b !== void 0 ? _b : type;
81
+ model.addBelongsTo({ schema, name, type });
82
+ model.belongsToVars[name].sourceElement = (0, sourceElement_1.sourceElement)(declaration);
83
+ }
84
+ }
85
+ resolveHasMany({ schema, model }) {
86
+ var _a;
87
+ for (const declaration of this.node('hasMany').nodes()) {
88
+ const typeName = declaration.attrExpression('model').value();
89
+ const target = schema.objects[typeName];
90
+ if (!target)
91
+ throw new Error(`Model '${typeName}' is not defined`);
92
+ const name = declaration.attrExpression('name').value();
93
+ const inverse = (_a = declaration.attrExpression('inverseOf')) === null || _a === void 0 ? void 0 : _a.value();
94
+ const candidates = inverse == null
95
+ ? Object.values(target.belongsTo).filter((relationship) => relationship.foreignType === model)
96
+ : [target.belongsTo[inverse]].filter((relationship) => (relationship === null || relationship === void 0 ? void 0 : relationship.foreignType) === model);
97
+ if (candidates.length !== 1) {
98
+ throw new Error(`Cannot resolve relationship '${model.name}.${name}' to a single belongs-to on '${typeName}'`);
99
+ }
100
+ const relationship = candidates[0];
101
+ if (relationship.foreignName != null || Object.hasOwn(model.hasMany, name)) {
102
+ throw new Error(`Relationship '${model.name}.${name}' is already linked`);
103
+ }
104
+ relationship.foreignName = name;
105
+ model.hasMany[name] = relationship;
106
+ const variable = schema.field(name, new parser_schema_2.QueryType(target));
107
+ variable.sourceElement = (0, sourceElement_1.sourceElement)(declaration);
108
+ model.hasManyVars[name] = variable;
109
+ }
110
+ }
111
+ }
112
+ exports.ParsedSchemaModelNode = ParsedSchemaModelNode;
113
+ //# sourceMappingURL=ParsedSchemaModelNode.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ParsedSchemaModelNode.js","sourceRoot":"","sources":["../../src/parsed/ParsedSchemaModelNode.ts"],"names":[],"mappings":";;;AAAA,mDAAgD;AAChD,0DAAsD;AAEtD,8DAA2D;AAE3D,8DAAuD;AAGvD,2BAAmC,SAAQ,wB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} from '@ja-platform/parser-core';\nimport { XmlSchema as Schema } from '../XmlSchema';\nimport { IndexDatabase } from '@ja-platform/parser-schema';\nimport { XmlModel as SchemaModelType } from '../XmlModel';\nimport { QueryType } from '@ja-platform/parser-schema';\nimport type { SchemaModelShape } from '../nodes/SchemaModelNode';\n\nexport class ParsedSchemaModelNode extends ParsedNode<SchemaModelShape> {\n toModel(schema: Schema): SchemaModelType {\n const model = schema.newObjectType(this.attrExpression('name').value());\n model.label = this.attrExpression('label').value();\n model.sourceElement = sourceElement(this);\n model.parsedNode = this;\n const fields = this.node('fields')\n .nodes()\n .map((field) => field.toField(schema));\n for (const field of fields) model.addAttribute(field);\n\n const display = this.node<SchemaModelShape['display']>('display');\n model.displayFormat = display?.attrExpression('format') ?? null;\n model.displaySource = sourceElement(display);\n model.allIndexes = this.node('indexes')\n .nodes()\n .map((index) => {\n const expression = index.attrExpression('keys');\n const keys = [...expression.keys];\n return {\n name: index.attrExpression('name')?.value() ?? keys.map((key) => key.field).join('_'),\n on: expression.items.map((item) => item.raw()),\n keys,\n databases: [\n ...(index.attrExpression('databases')?.databases ?? index.attribute('databases').definition.defaultValue)\n ]\n };\n });\n model.indexes = model.allIndexes.filter((index) => index.databases.includes(IndexDatabase.APP));\n model.notifyUsers = this.node('notifications')\n .nodes()\n .map((notification) => ({\n message: notification.attrExpression('message'),\n recipient: notification.attrExpression('recipient').value(),\n received: notification.attrExpression('received').value(),\n badgeCount: notification.attrExpression('badgeCount')?.value() ?? null\n }));\n model.webhooks = this.node('webhooks')\n .nodes()\n .map((webhook) => {\n const type = webhook.attrExpression('type').value();\n const receiver = webhook.attrExpression('receiver')?.value() ?? null;\n const action = webhook.attrExpression('action')?.value() ?? null;\n return {\n name:\n webhook.attrExpression('name')?.value() ??\n (receiver ? `${receiver}_${model.name}_${type}_${action}` : `${model.name}_${type}`),\n type,\n receiver,\n action,\n sourceElement: sourceElement(webhook),\n fields: webhook\n .node('fields')\n .nodes()\n .map((field) => ({\n name: field.attrExpression('name').value(),\n required:\n field.attrExpression('required')?.value() ?? field.attribute('required').definition.defaultValue,\n embed: field.attrExpression('embed')?.value() ?? field.attribute('embed').definition.defaultValue,\n state: field.attrExpression('state')?.value() ?? field.attribute('state').definition.defaultValue\n }))\n };\n });\n return model;\n }\n\n resolveBelongsTo({ schema, model }: { schema: Schema; model: SchemaModelType }): void {\n for (const declaration of this.node('belongsTo').nodes()) {\n const type = declaration.attrExpression('model').value();\n const name = declaration.attrExpression('name')?.value() ?? type;\n model.addBelongsTo({ schema, name, type });\n model.belongsToVars[name].sourceElement = sourceElement(declaration);\n }\n }\n\n resolveHasMany({ schema, model }: { schema: Schema; model: SchemaModelType }): void {\n for (const declaration of this.node('hasMany').nodes()) {\n const typeName = declaration.attrExpression('model').value();\n const target = schema.objects[typeName];\n if (!target) throw new Error(`Model '${typeName}' is not defined`);\n const name = declaration.attrExpression('name').value();\n const inverse = declaration.attrExpression('inverseOf')?.value();\n const candidates =\n inverse == null\n ? Object.values(target.belongsTo).filter((relationship) => relationship.foreignType === model)\n : [target.belongsTo[inverse]].filter((relationship) => relationship?.foreignType === model);\n if (candidates.length !== 1) {\n throw new Error(`Cannot resolve relationship '${model.name}.${name}' to a single belongs-to on '${typeName}'`);\n }\n const relationship = candidates[0];\n if (relationship.foreignName != null || Object.hasOwn(model.hasMany, name)) {\n throw new Error(`Relationship '${model.name}.${name}' is already linked`);\n }\n relationship.foreignName = name;\n model.hasMany[name] = relationship;\n const variable = schema.field(name, new QueryType(target));\n variable.sourceElement = sourceElement(declaration);\n model.hasManyVars[name] = variable;\n }\n }\n}\n"]}
@@ -0,0 +1,29 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ParsedSchemaNode = void 0;
4
+ const sourceElement_1 = require("./sourceElement");
5
+ const parser_core_1 = require("@ja-platform/parser-core");
6
+ const XmlSchema_1 = require("../XmlSchema");
7
+ class ParsedSchemaNode extends parser_core_1.ParsedNode {
8
+ /**
9
+ * Construct a fresh schema from the parsed declarations, then link its relationships.
10
+ * Validation remains an explicit, separate operation on the parsed tree.
11
+ */
12
+ toSchema() {
13
+ const schema = new XmlSchema_1.XmlSchema();
14
+ schema.sourceElement = (0, sourceElement_1.sourceElement)(this);
15
+ schema.parsedNode = this;
16
+ const models = this.node('models').nodes();
17
+ const objects = models.map((model) => model.toModel(schema));
18
+ schema.objects = Object.fromEntries(objects.map((model) => [model.name, model]));
19
+ for (const [index, model] of models.entries()) {
20
+ model.resolveBelongsTo({ schema, model: objects[index] });
21
+ }
22
+ for (const [index, model] of models.entries()) {
23
+ model.resolveHasMany({ schema, model: objects[index] });
24
+ }
25
+ return schema;
26
+ }
27
+ }
28
+ exports.ParsedSchemaNode = ParsedSchemaNode;
29
+ //# sourceMappingURL=ParsedSchemaNode.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ParsedSchemaNode.js","sourceRoot":"","sources":["../../src/parsed/ParsedSchemaNode.ts"],"names":[],"mappings":";;;AAAA,mDAAgD;AAChD,0DAAsD;AACtD,4CAAmD;AAGnD,sBAA8B,SAAQ,wBAAuB;IAC3D;;;OAGG;IACH,QAAQ;QACN,MAAM,MAAM,GAAG,IAAI,qBAAM,EAAE,CAAC;QAC5B,MAAM,CAAC,aAAa,GAAG,IAAA,6BAAa,EAAC,IAAI,CAAC,CAAC;QAC3C,MAAM,CAAC,UAAU,GAAG,IAAI,CAAC;QACzB,MAAM,MAAM,GAAG,IAAI,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC,KAAK,EAAE,CAAC;QAC3C,MAAM,OAAO,GAAG,MAAM,CAAC,GAAG,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,KAAK,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC,CAAC;QAC7D,MAAM,CAAC,OAAO,GAAG,MAAM,CAAC,WAAW,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,CAAC,KAAK,CAAC,IAAI,EAAE,KAAK,CAAC,CAAC,CAAC,CAAC;QACjF,KAAK,MAAM,CAAC,KAAK,EAAE,KAAK,CAAC,IAAI,MAAM,CAAC,OAAO,EAAE,EAAE,CAAC;YAC9C,KAAK,CAAC,gBAAgB,CAAC,EAAE,MAAM,EAAE,KAAK,EAAE,OAAO,CAAC,KAAK,CAAC,EAAE,CAAC,CAAC;QAC5D,CAAC;QACD,KAAK,MAAM,CAAC,KAAK,EAAE,KAAK,CAAC,IAAI,MAAM,CAAC,OAAO,EAAE,EAAE,CAAC;YAC9C,KAAK,CAAC,cAAc,CAAC,EAAE,MAAM,EAAE,KAAK,EAAE,OAAO,CAAC,KAAK,CAAC,EAAE,CAAC,CAAC;QAC1D,CAAC;QACD,OAAO,MAAM,CAAC;IAChB,CAAC;CACF","sourcesContent":["import { sourceElement } from './sourceElement';\nimport { ParsedNode } from '@ja-platform/parser-core';\nimport { XmlSchema as Schema } from '../XmlSchema';\nimport type { SchemaShape } from '../SchemaNode';\n\nexport class ParsedSchemaNode extends ParsedNode<SchemaShape> {\n /**\n * Construct a fresh schema from the parsed declarations, then link its relationships.\n * Validation remains an explicit, separate operation on the parsed tree.\n */\n toSchema(): Schema {\n const schema = new Schema();\n schema.sourceElement = sourceElement(this);\n schema.parsedNode = this;\n const models = this.node('models').nodes();\n const objects = models.map((model) => model.toModel(schema));\n schema.objects = Object.fromEntries(objects.map((model) => [model.name, model]));\n for (const [index, model] of models.entries()) {\n model.resolveBelongsTo({ schema, model: objects[index] });\n }\n for (const [index, model] of models.entries()) {\n model.resolveHasMany({ schema, model: objects[index] });\n }\n return schema;\n }\n}\n"]}
@@ -0,0 +1,12 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.sourceElement = sourceElement;
4
+ const parser_document_xml_1 = require("@ja-platform/parser-document-xml");
5
+ /** Recover the original XML node for incremental schema serialization. */
6
+ function sourceElement(node) {
7
+ var _a;
8
+ const source = (_a = node === null || node === void 0 ? void 0 : node.input) === null || _a === void 0 ? void 0 : _a.source;
9
+ if (source instanceof parser_document_xml_1.XMLSourceReference)
10
+ return source.element;
11
+ }
12
+ //# sourceMappingURL=sourceElement.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"sourceElement.js","sourceRoot":"","sources":["../../src/parsed/sourceElement.ts"],"names":[],"mappings":";;;AACA,0EAAsE;AAGtE,0EAA0E;AAC1E,uBAA8B,IAAiB;;IAC7C,MAAM,MAAM,GAAG,MAAA,IAAI,aAAJ,IAAI,uBAAJ,IAAI,CAAE,KAAK,0CAAE,MAAM,CAAC;IACnC,IAAI,MAAM,YAAY,wCAAkB;QAAE,OAAO,MAAM,CAAC,OAAqB,CAAC;AAChF,CAAC","sourcesContent":["import type { ParsedNode } from '@ja-platform/parser-core';\nimport { XMLSourceReference } from '@ja-platform/parser-document-xml';\nimport type { XMLElement } from '@journeyapps/domparser/types';\n\n/** Recover the original XML node for incremental schema serialization. */\nexport function sourceElement(node?: ParsedNode): XMLElement | undefined {\n const source = node?.input?.source;\n if (source instanceof XMLSourceReference) return source.element as XMLElement;\n}\n"]}
@@ -0,0 +1,181 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
35
+ Object.defineProperty(exports, "__esModule", { value: true });
36
+ exports.toDOM = toDOM;
37
+ exports.updateOptions = updateOptions;
38
+ const common_xml_1 = require("@journeyapps/common-xml");
39
+ const xml = __importStar(require("@journeyapps/common-xml"));
40
+ const parser_schema_1 = require("@ja-platform/parser-schema");
41
+ function toDOM(schema) {
42
+ let doc = xml.createDocument('data-model');
43
+ let root = doc.documentElement;
44
+ doc.insertBefore(doc.createProcessingInstruction('xml', 'version="1.0" encoding="UTF-8"'), root);
45
+ const documentDiff = new common_xml_1.OrderedIncrementalUpdater(schema.sourceElement, ['model']);
46
+ for (let type in schema.objects) {
47
+ let model = schema.objects[type];
48
+ const updateModel = (modelElement) => {
49
+ (0, common_xml_1.setAttributes)(modelElement, {
50
+ name: model.name,
51
+ label: model.label
52
+ });
53
+ const diff = new common_xml_1.OrderedIncrementalUpdater(model.sourceElement, ['field', 'belongs-to', 'has-many', 'display']);
54
+ for (let attributeName in model.attributes) {
55
+ const attribute = model.attributes[attributeName];
56
+ diff.append(attribute.sourceElement, {
57
+ tagName: 'field',
58
+ update(fieldElement) {
59
+ (0, common_xml_1.setAttributes)(fieldElement, {
60
+ name: attribute.name,
61
+ label: attribute.label,
62
+ type: attribute.sourceTypeName || attribute.type.stringify(),
63
+ 'auto-download': parser_schema_1.AttachmentType.isInstanceOf(attribute.type) && attribute.type.autoDownload ? 'true' : null
64
+ });
65
+ // single-choice, single-choice-integer and boolean fields all may contain <option> children.
66
+ // For other elements, we just keep the children as is.
67
+ const fieldDiff = new common_xml_1.OrderedIncrementalUpdater(attribute.sourceElement, ['option']);
68
+ updateOptions(fieldDiff, attribute);
69
+ fieldDiff.update(fieldElement);
70
+ },
71
+ cloneDeep: false
72
+ });
73
+ }
74
+ for (let fieldName in model.belongsTo) {
75
+ const field = model.belongsToVars[fieldName];
76
+ const rel = model.belongsTo[fieldName];
77
+ diff.append(field.sourceElement, {
78
+ tagName: 'belongs-to',
79
+ update(fieldElement) {
80
+ (0, common_xml_1.setAttributes)(fieldElement, {
81
+ model: rel.foreignType.name,
82
+ name: rel.name
83
+ }, { name: rel.foreignType.name });
84
+ },
85
+ cloneDeep: true
86
+ });
87
+ }
88
+ for (let fieldName in model.hasMany) {
89
+ const rel = model.hasMany[fieldName];
90
+ const field = model.hasManyVars[fieldName];
91
+ diff.append(field.sourceElement, {
92
+ tagName: 'has-many',
93
+ update(fieldElement) {
94
+ (0, common_xml_1.setAttributes)(fieldElement, {
95
+ model: rel.objectType.name,
96
+ name: rel.foreignName
97
+ });
98
+ },
99
+ cloneDeep: true
100
+ });
101
+ }
102
+ diff.append(model.displaySource, {
103
+ tagName: 'display',
104
+ update(displayElement) {
105
+ var _a;
106
+ var _b;
107
+ displayElement.textContent = (_b = (_a = model.displayFormat) === null || _a === void 0 ? void 0 : _a.raw()) !== null && _b !== void 0 ? _b : '';
108
+ },
109
+ cloneDeep: true
110
+ });
111
+ diff.update(modelElement);
112
+ };
113
+ documentDiff.append(model.sourceElement, {
114
+ tagName: 'model',
115
+ update: updateModel,
116
+ cloneDeep: false
117
+ });
118
+ }
119
+ documentDiff.update(root);
120
+ return doc;
121
+ }
122
+ /**
123
+ * Update the options for single-choice, single-choice-integer and boolean fields.
124
+ *
125
+ * No-op for other fields.
126
+ */
127
+ function updateOptions(fieldDiff, attribute) {
128
+ if (!parser_schema_1.ChoiceType.isInstanceOf(attribute.type)) {
129
+ return;
130
+ }
131
+ const options = attribute.type.options;
132
+ const keys = Object.keys(options);
133
+ // Special case for default boolean options, which doesn't have to be specified explicitly.
134
+ // <option key="false">No</option>
135
+ // <option key="true">Yes</option>
136
+ let explicitOptions = true;
137
+ if (attribute.type.name == 'boolean' &&
138
+ keys.length == 2 &&
139
+ keys[0] == 'false' &&
140
+ keys[1] == 'true' &&
141
+ options['false'].label == 'No' &&
142
+ options['true'].label == 'Yes') {
143
+ // Options match the default options. Use implicit ones.
144
+ if (attribute.sourceElement && attribute.sourceElement.childNodes.length > 0) {
145
+ // Source contained elements inside the tags. Could have been comments on the options.
146
+ // Keep explicit options in this case.
147
+ explicitOptions = true;
148
+ }
149
+ else {
150
+ explicitOptions = false;
151
+ }
152
+ }
153
+ if (!explicitOptions) {
154
+ // No options needed.
155
+ return;
156
+ }
157
+ // For single-choice-integer, keep track of the last key.
158
+ let lastKey = -1;
159
+ for (let key of keys) {
160
+ const option = options[key];
161
+ // If the key is equal to the previous one + 1, we don't have to specify it.
162
+ // However, if the source had an explicit key, we keep it.
163
+ const defaultValue = '' + (lastKey + 1);
164
+ if (typeof option.value == 'number') {
165
+ lastKey = option.value;
166
+ }
167
+ fieldDiff.append(attribute.optionSources.get(String(option.value)), {
168
+ tagName: 'option',
169
+ update(element) {
170
+ (0, common_xml_1.setAttributes)(element, {
171
+ key: '' + option.value
172
+ }, {
173
+ key: defaultValue
174
+ });
175
+ element.textContent = option.label;
176
+ },
177
+ cloneDeep: true
178
+ });
179
+ }
180
+ }
181
+ //# sourceMappingURL=schemaSerializer.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"schemaSerializer.js","sourceRoot":"","sources":["../src/schemaSerializer.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AACA,wDAAmF;AAEnF,MAAY,GAAG,oDAAgC;AAE/C,8DAAwE;AAExE,eAAsB,MAAc;IAClC,IAAI,GAAG,GAAG,GAAG,CAAC,cAAc,CAAC,YAAY,CAAC,CAAC;IAC3C,IAAI,IAAI,GAAG,GAAG,CAAC,eAAe,CAAC;IAC/B,GAAG,CAAC,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AI;SAChB,CAAC,CAAC;IACL,CAAC;AACH,CAAC","sourcesContent":["import { XmlSchema as Schema } from './XmlSchema';\nimport { setAttributes, OrderedIncrementalUpdater } from '@journeyapps/common-xml';\nimport { XMLElement, XMLDocument } from '@journeyapps/domparser/types';\nimport * as xml from '@journeyapps/common-xml';\nimport { XmlField as Field } from './XmlField';\nimport { AttachmentType, ChoiceType } from '@ja-platform/parser-schema';\n\nexport function toDOM(schema: Schema): XMLDocument {\n let doc = xml.createDocument('data-model');\n let root = doc.documentElement;\n doc.insertBefore(doc.createProcessingInstruction('xml', 'version=\"1.0\" encoding=\"UTF-8\"'), root);\n\n const documentDiff = new OrderedIncrementalUpdater(schema.sourceElement, ['model']);\n\n for (let type in schema.objects) {\n let model = schema.objects[type];\n\n const updateModel = (modelElement: XMLElement) => {\n setAttributes(modelElement, {\n name: model.name,\n label: model.label\n });\n\n const diff = new OrderedIncrementalUpdater(model.sourceElement, ['field', 'belongs-to', 'has-many', 'display']);\n\n for (let attributeName in model.attributes) {\n const attribute = model.attributes[attributeName];\n diff.append(attribute.sourceElement, {\n tagName: 'field',\n update(fieldElement) {\n setAttributes(fieldElement, {\n name: attribute.name,\n label: attribute.label,\n type: attribute.sourceTypeName || attribute.type.stringify(),\n 'auto-download':\n AttachmentType.isInstanceOf(attribute.type) && attribute.type.autoDownload ? 'true' : null\n });\n\n // single-choice, single-choice-integer and boolean fields all may contain <option> children.\n // For other elements, we just keep the children as is.\n const fieldDiff = new OrderedIncrementalUpdater(attribute.sourceElement, ['option']);\n updateOptions(fieldDiff, attribute);\n fieldDiff.update(fieldElement);\n },\n cloneDeep: false\n });\n }\n\n for (let fieldName in model.belongsTo) {\n const field = model.belongsToVars[fieldName];\n const rel = model.belongsTo[fieldName];\n diff.append(field.sourceElement, {\n tagName: 'belongs-to',\n update(fieldElement) {\n setAttributes(\n fieldElement,\n {\n model: rel.foreignType.name,\n name: rel.name\n },\n { name: rel.foreignType.name }\n );\n },\n cloneDeep: true\n });\n }\n\n for (let fieldName in model.hasMany) {\n const rel = model.hasMany[fieldName];\n const field = model.hasManyVars[fieldName];\n diff.append(field.sourceElement, {\n tagName: 'has-many',\n update(fieldElement) {\n setAttributes(fieldElement, {\n model: rel.objectType.name,\n name: rel.foreignName\n });\n },\n cloneDeep: true\n });\n }\n\n diff.append(model.displaySource, {\n tagName: 'display',\n update(displayElement) {\n displayElement.textContent = model.displayFormat?.raw() ?? '';\n },\n cloneDeep: true\n });\n\n diff.update(modelElement);\n };\n\n documentDiff.append(model.sourceElement, {\n tagName: 'model',\n update: updateModel,\n cloneDeep: false\n });\n }\n\n documentDiff.update(root);\n return doc;\n}\n\n/**\n * Update the options for single-choice, single-choice-integer and boolean fields.\n *\n * No-op for other fields.\n */\nexport function updateOptions(fieldDiff: OrderedIncrementalUpdater, attribute: Field) {\n if (!ChoiceType.isInstanceOf(attribute.type)) {\n return;\n }\n const options = attribute.type.options;\n const keys = Object.keys(options);\n\n // Special case for default boolean options, which doesn't have to be specified explicitly.\n // <option key=\"false\">No</option>\n // <option key=\"true\">Yes</option>\n let explicitOptions = true;\n if (\n attribute.type.name == 'boolean' &&\n keys.length == 2 &&\n keys[0] == 'false' &&\n keys[1] == 'true' &&\n options['false'].label == 'No' &&\n options['true'].label == 'Yes'\n ) {\n // Options match the default options. Use implicit ones.\n if (attribute.sourceElement && attribute.sourceElement.childNodes.length > 0) {\n // Source contained elements inside the tags. Could have been comments on the options.\n // Keep explicit options in this case.\n explicitOptions = true;\n } else {\n explicitOptions = false;\n }\n }\n\n if (!explicitOptions) {\n // No options needed.\n return;\n }\n\n // For single-choice-integer, keep track of the last key.\n let lastKey = -1;\n for (let key of keys) {\n const option = options[key];\n // If the key is equal to the previous one + 1, we don't have to specify it.\n // However, if the source had an explicit key, we keep it.\n const defaultValue = '' + (lastKey + 1);\n if (typeof option.value == 'number') {\n lastKey = option.value;\n }\n fieldDiff.append(attribute.optionSources.get(String(option.value)), {\n tagName: 'option',\n update(element) {\n setAttributes(\n element,\n {\n key: '' + option.value\n },\n {\n key: defaultValue\n }\n );\n element.textContent = option.label;\n },\n cloneDeep: true\n });\n }\n}\n"]}
@@ -0,0 +1,39 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.SchemaIndexKeyToken = void 0;
4
+ const parser_expressions_1 = require("@ja-platform/parser-expressions");
5
+ const common_xml_1 = require("@journeyapps/common-xml");
6
+ const parser_schema_1 = require("@ja-platform/parser-schema");
7
+ /**
8
+ * An index field reference with its optional sort direction.
9
+ */
10
+ class SchemaIndexKeyToken extends parser_expressions_1.AbstractToken {
11
+ constructor(source, start = 0) {
12
+ super(start);
13
+ this.source = source;
14
+ Object.freeze(this);
15
+ }
16
+ get field() {
17
+ return /^[+-]/.test(this.source) ? this.source.slice(1) : this.source;
18
+ }
19
+ get direction() {
20
+ return this.source.startsWith('-') ? parser_schema_1.IndexDirection.DESCENDING : parser_schema_1.IndexDirection.ASCENDING;
21
+ }
22
+ isValid() {
23
+ try {
24
+ common_xml_1.attribute.name(this.field);
25
+ return true;
26
+ }
27
+ catch (_a) {
28
+ return false;
29
+ }
30
+ }
31
+ raw() {
32
+ return this.source;
33
+ }
34
+ clone() {
35
+ return this;
36
+ }
37
+ }
38
+ exports.SchemaIndexKeyToken = SchemaIndexKeyToken;
39
+ //# sourceMappingURL=SchemaIndexKeyToken.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"SchemaIndexKeyToken.js","sourceRoot":"","sources":["../../src/tokens/SchemaIndexKeyToken.ts"],"names":[],"mappings":";;;AAAA,wEAAgE;AAChE,wDAAoD;AACpD,8DAA4D;AAE5D;;GAEG;AACH,yBAAiC,SAAQ,kCAAa;IACpD,YACW,MAAc,EACvB,KAAK,GAAG,CAAC;QAET,KAAK,CAAC,KAAK,CAAC,CAAC;sBAHJ,MAAM;QAIf,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;IACtB,CAAC;IAED,IAAI,KAAK;QACP,OAAO,OAAO,CAAC,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,MAAM,CAAC;IACxE,CAAC;IAED,IAAI,SAAS;QACX,OAAO,IAAI,CAAC,MAAM,CAAC,UAAU,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,8BAAc,CAAC,UAAU,CAAC,CAAC,CAAC,8BAAc,CAAC,SAAS,CAAC;IAC5F,CAAC;IAED,OAAO;QACL,IAAI,CAAC;YACH,sBAAS,CAAC,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;YAC3B,OAAO,IAAI,CAAC;QACd,CAAC;mBAAO,CAAC;YACP,OAAO,KAAK,CAAC;QACf,CAAC;IACH,CAAC;IAED,GAAG;QACD,OAAO,IAAI,CAAC,MAAM,CAAC;IACrB,CAAC;IAED,KAAK;QACH,OAAO,IAAI,CAAC;IACd,CAAC;CACF","sourcesContent":["import { AbstractToken } from '@ja-platform/parser-expressions';\nimport { attribute } from '@journeyapps/common-xml';\nimport { IndexDirection } from '@ja-platform/parser-schema';\n\n/**\n * An index field reference with its optional sort direction.\n */\nexport class SchemaIndexKeyToken extends AbstractToken {\n constructor(\n readonly source: string,\n start = 0\n ) {\n super(start);\n Object.freeze(this);\n }\n\n get field(): string {\n return /^[+-]/.test(this.source) ? this.source.slice(1) : this.source;\n }\n\n get direction(): IndexDirection {\n return this.source.startsWith('-') ? IndexDirection.DESCENDING : IndexDirection.ASCENDING;\n }\n\n isValid(): boolean {\n try {\n attribute.name(this.field);\n return true;\n } catch {\n return false;\n }\n }\n\n raw(): string {\n return this.source;\n }\n\n clone(): this {\n return this;\n }\n}\n"]}