You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: README.md
+38-14
Original file line number
Diff line number
Diff line change
@@ -17,7 +17,7 @@ export class Fruit {
17
17
@AvroInt()
18
18
id:number
19
19
20
-
@AvroString({ description: 'The name of the fruit' })
20
+
@AvroString({ fieldDoc: 'The name of the fruit' })
21
21
name:string
22
22
}
23
23
```
@@ -40,7 +40,7 @@ you need to setup the following
40
40
// reference your models here
41
41
{ class: Fruit }
42
42
],
43
-
// each referenced model will be written to a file
43
+
// each referenced model will be written to a avsc file
44
44
outDir: 'src/example/schemas'
45
45
}
46
46
@@ -63,9 +63,9 @@ you need to setup the following
63
63
## Configuration
64
64
`Avro Decorators` requires a configuration file written in TypeScript, to ensure the models have applied the decorators accordingly to read the required metadata.
65
65
66
-
The `models` array is mandatory. Each model requires `class` - a reference to the TypeScript class, and an optional filename `avscFileName` to name the schema output file.
66
+
The `models` array in the config is mandatory. Each model requires `class` - a reference to the TypeScript class, and an optional filename `avscFileName` to name the schema output file.
67
67
68
-
Additionaly, an output directory `outDir` can be declared. If it is not specified, the generated schemas will be printed to stdout instead.
68
+
Additionally, an output directory `outDir` can be declared. If it is not specified, the generated schemas will be printed to stdout instead.
69
69
70
70
### Locating config not in root
71
71
By default, `Avro Decorators` will check the current working directory for the file `avro-decorators.config.ts`. If your config is located in a different folder, pass it to the program using the flag `--config <path>` or `-c <path>`.
@@ -74,7 +74,7 @@ By default, `Avro Decorators` will check the current working directory for the f
74
74
### Namespace
75
75
Declare a namespace for a record as seen in the following example. If you want to use a model name different than the class name, you can use the `name` property.
76
76
77
-
For enum fields you can also declare them in the decorator.
77
+
For enum and fixed fields you can also declare them in the decorator.
78
78
```ts
79
79
@Record({
80
80
namespace: 'fruits.meta',
@@ -89,14 +89,14 @@ export class Fruit {
89
89
fruitType:FruitType
90
90
}
91
91
```
92
-
### Different field or record name
93
-
To use a different field name in the schema than in the class, you the decorator property `avroOverrideName`:
92
+
### Different field or record name in schema than in class
93
+
To use a different field name in the schema than in the class, you can use the decorator property `fieldName`:
To use a record inside another record on a field type, you should declare both records independently and then reference it on the field:
99
+
To use a record inside another record on a field type, you should declare both records independently and then reference it on the field. It will then be inlined in the schema avsc file:
100
100
```ts
101
101
@Record()
102
102
exportclassAddress {
@@ -111,7 +111,7 @@ export class User {
111
111
}
112
112
```
113
113
### Reference schema by name
114
-
Referencing by name works using the
114
+
Referencing by name works using `@AvroReferenceByName`:
115
115
```ts
116
116
@Record()
117
117
exportclassFruit {
@@ -134,11 +134,35 @@ This will result in the schema
134
134
]
135
135
}
136
136
```
137
-
Note that there is no validation if that referenced type actually exists anywhere in this library.
137
+
Note that there is no validation if that referenced type actually exists anywhere.
138
+
139
+
### Unions
140
+
Note that if you just want to add a `null` type to a field, you can always use the `nullable` property:
141
+
```ts
142
+
@Record()
143
+
exportclassFruit {
144
+
@AvroString({ nullable: true })
145
+
field:string|null
146
+
}
147
+
```
148
+
To express more complex unions, use the `@AvroUnion` decorator.
149
+
It requires a second argument, which is an array of all referenced union types.
150
+
```ts
151
+
@Record()
152
+
exportclassAddress { /* ... */ }
153
+
154
+
@Record()
155
+
exportclassModel {
156
+
@AvroUnion(
157
+
{ fieldDoc: 'Can be an int, a string, an address or null' },
158
+
['int', 'string', 'null', () =>Address]
159
+
)
160
+
field:number|string|null|Address
161
+
}
162
+
```
138
163
139
164
## Features not supported yet
140
-
* Union
141
-
* Top-level non-record (e. g. enum)
165
+
* Top-level non-record (e. g. enum or fixed)
142
166
* Validation of `name` and `namespace` according to [specification](https://avro.apache.org/docs/current/spec.html#names)
0 commit comments