@api-client/core 0.11.1 → 0.11.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -24,8 +24,11 @@ interface IDataDefinitions {
24
24
 
25
25
  interface DataDefinitions {
26
26
  models: DataModel[]
27
+ // @todo: This should be a map of entities with a key of the entity key.
27
28
  entities: DataEntity[]
29
+ // @todo: This should be a map of properties with a key of the property key.
28
30
  properties: DataProperty[]
31
+ // @todo: This should be a map of associations with a key of the association key.
29
32
  associations: DataAssociation[]
30
33
  namespaces: DataNamespace[]
31
34
  /**
@@ -41,7 +44,7 @@ interface DataDefinitions {
41
44
 
42
45
  /**
43
46
  * Data definition for a foreign namespace.
44
- * Each foreigh namespace is resolved to a specific version.
47
+ * Each foreign namespace is resolved to a specific version.
45
48
  * This makes sure that the local data are always referencing an existing
46
49
  * entity as breaking changes should be resolved when upgrading a version.
47
50
  */
@@ -432,6 +435,14 @@ export class DataNamespace extends DataNamespaceParent {
432
435
  return result
433
436
  }
434
437
 
438
+ /**
439
+ * Checks if this is the root namespace.
440
+ * @returns True if this is the root namespace.
441
+ */
442
+ isRoot(): boolean {
443
+ return this.root === undefined
444
+ }
445
+
435
446
  /**
436
447
  * Finds a parent namespace for the given namespace.
437
448
  * @param key The namespace key to find the parent for.
@@ -672,6 +683,16 @@ export class DataNamespace extends DataNamespaceParent {
672
683
  return definitions.properties.find((i) => i.key === key)
673
684
  }
674
685
 
686
+ /**
687
+ * Finds an association by its key.
688
+ * @param key The key of the property to find
689
+ * @returns The property or undefined if not found.
690
+ */
691
+ findAssociation(key: string): DataAssociation | undefined {
692
+ const { definitions } = this.root || this
693
+ return definitions.associations.find((i) => i.key === key)
694
+ }
695
+
675
696
  /**
676
697
  * Searches for entities for association targets.
677
698
  * This is a helper function to discover entities in the current and foreign namespaces.
@@ -702,6 +723,29 @@ export class DataNamespace extends DataNamespaceParent {
702
723
  return result
703
724
  }
704
725
 
726
+ /**
727
+ * Finds an associated entity in the current or foreign namespace.
728
+ * This is a helper function to discover entities in the current and foreign namespaces.
729
+ *
730
+ * @param key The key of the entity to find.
731
+ * @param namespace The optional namespace to search in.
732
+ * If not set, the current namespace is used.
733
+ * This is used to find entities in foreign namespaces.
734
+ * @returns The entity or undefined if not found.
735
+ */
736
+ findAssociatedEntity(key: string, namespace?: string): DataEntity | undefined {
737
+ let ns: DataNamespace | undefined
738
+ if (namespace) {
739
+ ns = this.foreign.find((i) => i.key === namespace)
740
+ } else {
741
+ ns = this
742
+ }
743
+ if (!ns) {
744
+ return undefined
745
+ }
746
+ return ns.findEntity(key)
747
+ }
748
+
705
749
  addForeign(ns: DataNamespace): void {
706
750
  const exists = this.foreign.some((i) => i.key === ns.key)
707
751
  if (exists) {
@@ -0,0 +1,134 @@
1
+ # Data Modeling
2
+
3
+ ## Core Concepts
4
+
5
+ At the heart of this data modeling system are the following fundamental concepts.
6
+
7
+ ### DataNamespace
8
+
9
+ This is the top-level container, representing a logical grouping of data. Think of it as a "domain" or a "schema" in a database. It can contain:
10
+
11
+ - `DataModels`: Logical groupings of entities.
12
+ - `DataEntities`: The basic building blocks, representing individual data structures.
13
+ - `DataProperties`: Attributes of entities (e.g., name, age, address).
14
+ - `DataAssociations`: Relationships between entities (e.g., a user has an address).
15
+ - `Sub-namespaces`: Namespaces can be nested within each other, creating a hierarchical structure.
16
+ - `Foreign Namespaces`: References to external namespaces, enabling the use of entities defined elsewhere.
17
+ - `Tags`: Common tags for the entire namespace.
18
+
19
+ ### DataModel
20
+
21
+ A logical grouping of `DataEntity` instances. It represents a specific data structure, like a "Product" or "User" model. A `DataModel` can contain multiple `DataEntity` instances.
22
+
23
+ ### DataEntity
24
+
25
+ The fundamental building block of the data model. It represents a specific type of data, like a "User," "Product," or "Address."
26
+
27
+ - `Properties`: DataProperty instances that describe the attributes of the entity.
28
+ - `Associations`: DataAssociation instances that define relationships to other entities.
29
+ - `Parents`: An entity can inherit from other entities, creating a hierarchy.
30
+ - `Fields`: Ordered list of properties and associations.
31
+ - `Tags`: Optional tags for the UI.
32
+ - `Taxonomy`: Reserved for future use.
33
+ - `Deprecated`: Whether the entity is deprecated.
34
+ - `Schema`: The schema allowing to translate the model into a specific format (like JSON, RAML, XML, etc.)
35
+
36
+ ### DataProperty
37
+
38
+ Represents an attribute of a `DataEntity`. It has a name, a data type (e.g., string, number, boolean), and optional constraints (e.g., required, multiple, min/max values).
39
+
40
+ - `Type`: The data type of the property.
41
+ - `Schema`: The general schema definition of this property.
42
+ - `Bindings`: The list of bindings for this property.
43
+ - `Tags`: Optional tags for the UI.
44
+ - `Taxonomy`: Reserved for future use.
45
+ - `Deprecated`: Whether the property is deprecated.
46
+ - `Primary`: Whether this property describes a primary key of the entity.
47
+ - `Index`: Whether this property describes an indexed property of the entity.
48
+ - `ReadOnly`: Whether the property is read only in the schema.
49
+ - `WriteOnly`: Whether the property is write only in the schema.
50
+
51
+ ### DataAssociation
52
+
53
+ Defines a relationship between `DataEntity` instances. It specifies the target entities and the nature of the relationship (e.g., one-to-one, one-to-many).
54
+
55
+ - `Targets`: The list of target entities.
56
+ - `Multiple`: Whether the association allows multiple target entities.
57
+ - `Required`: Whether the association is required.
58
+ - `Schema`: The definition of the database/API schema
59
+ - `Bindings`: The list of bindings allowing to translate the model into a specific format (like JSON, RAML, XML, etc.)
60
+ - `Hidden`: Defines if this association is a part of the schema or not.
61
+
62
+ ### Bindings
63
+
64
+ Defines a translation from a data model to a specific format (like JSON, RAML, XML, etc.)
65
+
66
+ ## How a Data Domain is Structured
67
+
68
+ Here's how these components work together to structure a data domain:
69
+
70
+ 1. **Root Namespace**: You start with a `DataNamespace`, which acts as the root of your data domain. This namespace is the top-level container for all other elements.
71
+
72
+ 1. **Sub-namespaces (Optional)**: You can create sub-namespaces within the root namespace to further organize your data. This is useful for large domains with many entities and relationships.
73
+
74
+ 1. **Data Models**: Within a namespace (or sub-namespace), you define `DataModel` instances. Each `DataModel` represents a specific area of your domain. For example, you might have a "User Management" `DataModel`, a "Product Catalog" `DataModel`, and an "Order Processing" `DataModel`.
75
+
76
+ 1. **Data Entities**: Inside each `DataModel`, you define `DataEntity` instances. These are the core data structures. For example, in the "User Management" `DataModel`, you might have `DataEntity` instances for "User," "Role," and "Permission."
77
+
78
+ 1. **Data Properties**: Each `DataEntity` has `DataProperty` instances that describe its attributes. For example, the "User" `DataEntity` might have `DataProperty` instances for "firstName" (string), "lastName" (string), "email" (string), "age" (number), etc.
79
+
80
+ 1. **Data Associations**: You use `DataAssociation` instances to define relationships between `DataEntity` instances. For example:
81
+
82
+ - A "User" has an "Address" (one-to-one).
83
+ - A "User" has multiple "Roles" (one-to-many).
84
+ - A "Product" belongs to a "Category" (many-to-one).
85
+ - An "Order" contains multiple "Order Items" (one-to-many).
86
+
87
+ 1. **Inheritance**: `DataEntity` instances can inherit from other `DataEntity` instances using the parents property. This allows you to create a hierarchy of entities and reuse common properties and associations.
88
+
89
+ 1. **Foreign Namespaces**: You can reference entities from other namespaces using the foreign property of the root namespace. This allows you to reuse data structures defined elsewhere.
90
+
91
+ 1. **Bindings**: You can define bindings for properties and associations to translate the data model into specific formats (e.g., JSON, RAML, XML, Protocol Buffers).
92
+
93
+ ## Example Scenario
94
+
95
+ Let's imagine a simple e-commerce domain:
96
+
97
+ - **Root Namespace**: `ECommerce`
98
+ - **Sub-namespace**: `Catalog`
99
+ - **Data Model**: `ProductCatalog`
100
+ - **Data Entity**: `Product`
101
+ - **Data Properties**: `name` (string), `description` (string), `price` (number), `sku` (string)
102
+ - **Data Association**: `category` (references `Category` entity)
103
+ - **Data Entity**: `Category`
104
+ - **Data Properties**: `name` (string), `description` (string)
105
+ - **Sub-namespace**: `UserManagement`
106
+ - **Data Model**: `UserManagement`
107
+ - **Data Entity**: `User`
108
+ - **Data Properties**: `firstName` (string), `lastName` (string), `email` (string), `password` (string)
109
+ - **Data Association**: `address` (references `Address` entity)
110
+ - **Data Entity**: `Address`
111
+ - **Data Properties**: `street` (string), `city` (string), `zipCode` (string), `country` (string)
112
+ - **Foreign Namespace**: `Taxonomy` (defined elsewhere)
113
+ - **Data Model**: `Taxonomy`
114
+ - **Data Entity**: `TaxonomyItem`
115
+
116
+ ### Key Relationships
117
+
118
+ - **Namespace-Model**: A `DataNamespace` contains `DataModel` instances.
119
+ - **Model-Entity**: A `DataModel` contains `DataEntity` instances.
120
+ - **Entity-Property**: A `DataEntity` has `DataProperty` instances.
121
+ - **Entity-Association**: A `DataEntity` has `DataAssociation` instances.
122
+ - **Entity-Parent**: A `DataEntity` can have `DataEntity` instances as parents.
123
+ - **Namespace-Foreign**: A `DataNamespace` can have references to `DataNamespace` instances.
124
+
125
+ ## Summary
126
+
127
+ This data modeling system provides a flexible and powerful way to define complex data domains. It allows you to:
128
+
129
+ - **Organize**: Group related data into namespaces and models.
130
+ - **Structure**: Define entities with properties and relationships.
131
+ - **Reuse**: Inherit from other entities and reference foreign namespaces.
132
+ - **Translate**: Define bindings to map the model to different formats.
133
+ - **Validate**: Validate the data model definition.
134
+ - **Generate**: Generate AMF shapes and examples.