-
Notifications
You must be signed in to change notification settings - Fork 81
/
Copy pathschema.ts
174 lines (145 loc) · 6.3 KB
/
schema.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import { createHash } from 'crypto';
import { ulid } from 'ulid'
import Entity from "../entity/entity";
import EntityConstructor from "../entity/entity-constructor";
import IdStrategy from './options/id-strategy';
import DataStructure from './options/data-structure';
import StopWordOptions from './options/stop-word-options';
import SchemaOptions from './options/schema-options';
import SchemaDefinition from './definition/schema-definition';
import FieldDefinition from './definition/field-definition';
import JsonSchemaBuilder from './builders/json-schema-builder';
import HashSchemaBuilder from './builders/hash-schema-builder';
/**
* Defines a schema that determines how an {@link Entity} is mapped to Redis
* data structures. Construct by passing in an {@link EntityConstructor},
* a {@link SchemaDefinition}, and optionally {@link SchemaOptions}:
*
* ```typescript
* const schema = new Schema(Foo, {
* aString: { type: 'string' },
* aNumber: { type: 'number' },
* aBoolean: { type: 'boolean' },
* someText: { type: 'text' },
* aPoint: { type: 'point' },
* aDate: { type: 'date' },
* someStrings: { type: 'string[]' }
* }, {
* dataStructure: 'HASH'
* });
* ```
*
* A Schema is primarily used by a {@link Repository} which requires a Schema in
* its constructor.
*
* @template TEntity The {@link Entity} this Schema defines.
*/
export default class Schema<TEntity extends Entity> {
/**
* The provided {@link EntityConstructor}.
* @internal
*/
readonly entityCtor: EntityConstructor<TEntity>;
/**
* The provided {@link SchemaDefinition}.
* @internal
*/
readonly definition: SchemaDefinition;
private options?: SchemaOptions;
/**
* @template TEntity The {@link Entity} this Schema defines.
* @param ctor A constructor that creates an {@link Entity} of type TEntity.
* @param schemaDef Defines all of the fields for the Schema and how they are mapped to Redis.
* @param options Additional options for this Schema.
*/
constructor(ctor: EntityConstructor<TEntity>, schemaDef: SchemaDefinition, options?: SchemaOptions) {
this.entityCtor = ctor;
this.definition = schemaDef;
this.options = options;
this.validateOptions();
this.defineProperties();
}
/** The configured keyspace prefix in Redis for this Schema. */
get prefix(): string { return this.options?.prefix ?? this.entityCtor.name; }
/** The configured name for the RediSearch index for this Schema. */
get indexName(): string { return this.options?.indexName ?? `${this.prefix}:index`; }
/** The configured name for the RediSearch index hash for this Schema. */
get indexHashName(): string { return this.options?.indexHashName ?? `${this.prefix}:index:hash`; }
/**
* The configured data structure, a string with the value of either `HASH` or `JSON`,
* that this Schema uses to store {@link Entity | Entities} in Redis.
* */
get dataStructure(): DataStructure { return this.options?.dataStructure ?? 'JSON'; }
/**
* The configured usage of stop words, a string with the value of either `OFF`, `DEFAULT`,
* or `CUSTOM`. See {@link SchemaOptions.useStopWords} and {@link SchemaOptions.stopWords}
* for more details.
*/
get useStopWords(): StopWordOptions { return this.options?.useStopWords ?? 'DEFAULT'; }
/**
* The configured stop words. Ignored if {@link Schema.useStopWords} is anything other
* than `CUSTOM`.
*/
get stopWords(): Array<string> { return this.options?.stopWords ?? []; }
/**
* The configured indexed default setting for fields
*/
get indexedDefault(): boolean { return this.options?.indexedDefault ?? true; }
/** The hash value of this index. Stored in Redis under {@link Schema.indexHashName}. */
get indexHash(): string {
const data = JSON.stringify({
definition: this.definition,
prefix: this.prefix,
indexName: this.indexName,
indexHashName: this.indexHashName,
dataStructure: this.dataStructure,
useStopWords: this.useStopWords,
stopWords: this.stopWords,
});
return createHash('sha1').update(data).digest('base64');
}
/** @internal */
get redisSchema(): Array<string> {
if (this.dataStructure === 'HASH') return new HashSchemaBuilder(this).redisSchema;
if (this.dataStructure === 'JSON') return new JsonSchemaBuilder(this).redisSchema;
throw new Error(`'${this.dataStructure}' in an invalid data structure. Valid data structures are 'HASH' and 'JSON'.`);
}
/**
* Generates a unique string using the configured {@link IdStrategy}.
* @returns
*/
generateId(): string {
const ulidStrategy: IdStrategy = () => ulid();
return (this.options?.idStrategy ?? ulidStrategy)();
}
private defineProperties() {
Object.keys(this.definition).forEach(fieldName => {
const fieldDef: FieldDefinition = this.definition[fieldName];
const fieldAlias = fieldDef.alias ?? fieldName;
this.validateFieldDef(fieldName, fieldDef);
Object.defineProperty(this.entityCtor.prototype, fieldName, {
configurable: true,
get: function (): any {
return this.entityFields[fieldAlias].value;
},
set: function (value: any): void {
this.entityFields[fieldAlias].value = value;
}
});
})
}
private validateOptions() {
if (!['HASH', 'JSON'].includes(this.dataStructure))
throw Error(`'${this.dataStructure}' in an invalid data structure. Valid data structures are 'HASH' and 'JSON'.`);
if (!['OFF', 'DEFAULT', 'CUSTOM'].includes(this.useStopWords))
throw Error(`'${this.useStopWords}' in an invalid value for stop words. Valid values are 'OFF', 'DEFAULT', and 'CUSTOM'.`);
if (this.options?.idStrategy && !(this.options.idStrategy instanceof Function))
throw Error("ID strategy must be a function that takes no arguments and returns a string.");
if (this.prefix === '') throw Error(`Prefix must be a non-empty string.`);
if (this.indexName === '') throw Error(`Index name must be a non-empty string.`);
}
private validateFieldDef(field: string, fieldDef: FieldDefinition) {
if (!['boolean', 'date', 'number', 'point', 'string', 'string[]', 'text'].includes(fieldDef.type))
throw Error(`The field '${field}' is configured with a type of '${fieldDef.type}'. Valid types include 'boolean', 'date', 'number', 'point', 'string', 'string[]', and 'text'.`);
}
}