-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
342 lines (301 loc) · 9.75 KB
/
index.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
/* eslint-disable @typescript-eslint/no-explicit-any */
/**
* FieldKind
* Represents the types that can be used in SurrealDB.
*/
type GenericType = '$$generic';
type BasicType =
| GenericType
| 'any'
| 'null'
| 'bool'
| 'bytes'
| 'datetime'
| 'decimal'
| 'duration'
| 'float'
| 'int'
| 'number'
| 'object'
| 'point'
| 'string'
| 'uuid';
type TypedType = 'geometry' | 'object' | 'option' | 'set' | 'array' | 'record';
type FieldType = BasicType | TypedType;
type GeometryDataTypes =
| 'feature'
| 'point'
| 'LineString'
| 'polygon'
| 'multipoint'
| 'multiline'
| 'multipolygon'
| 'collection';
/**
* FieldSchemaProperty
* Represents a property within a SurrealDB schema.
*/
interface FieldSchemaProperty {
type?: BasicType | TypedType;
default?: string;
value?: string;
assertion?: string;
indexed?: boolean;
unique?: boolean;
primary?: boolean;
optional?: boolean;
}
interface PrimaryFieldConfig extends FieldSchemaProperty {
primary: boolean;
}
interface BasicFieldConfig extends FieldSchemaProperty {
type: BasicType;
}
interface TypedFieldConfig extends FieldSchemaProperty {
type: TypedType;
typed?: string | BasicType | typeof TableSchema | GeometryDataTypes;
}
interface GeometryFieldConfig extends TypedFieldConfig {
type: 'geometry';
typed: GeometryDataTypes;
}
interface ObjectFieldConfig extends TypedFieldConfig {
type: 'option' | 'set' | 'array' | 'record' | 'object';
typed?: string | BasicType | typeof TableSchema;
properties?: PropertiesTypes;
}
type FieldConfig = PrimaryFieldConfig | BasicFieldConfig | ObjectFieldConfig | GeometryFieldConfig;
// properties types
interface PropertiesTypes {
[keys: string]: FieldConfig;
}
/**
* ObjectSchema
* Represents a schema for a SurrealDB object.
*/
interface IndexSchema {
name?: string;
fields: string[];
unique?: boolean;
}
type TableGenericConfig = {
name: string;
generic: string;
};
type TableConfig = {
tables: (string | TableGenericConfig)[];
schemaMode?: 'SCHEMAFULL' | 'SCHEMALESS';
indexes?: IndexSchema[];
};
export interface SchemaObject extends TableConfig {
properties: PropertiesTypes;
}
export class TableSchema {
static SurrealdbSchema: SchemaObject;
}
function initializeSchema(constructor: any): void {
if (!constructor.SurrealdbSchema) {
constructor.SurrealdbSchema = {
tables: [],
properties: {},
};
} else if (!constructor.SurrealdbSchema.properties) {
constructor.SurrealdbSchema.properties = {};
}
// Check if the class has a parent class with SurrealdbSchema
const parentConstructor = Object.getPrototypeOf(constructor);
if (parentConstructor?.SurrealdbSchema) {
// Merge the parent's properties into the child's properties
constructor.SurrealdbSchema.properties = {
...parentConstructor.SurrealdbSchema.properties,
...constructor.SurrealdbSchema.properties,
};
}
}
export function Field(kind: FieldType): PropertyDecorator;
export function Field(config: FieldConfig): PropertyDecorator;
export function Field(args: FieldType | FieldConfig): PropertyDecorator {
return (target: any, propertyKey: string | symbol) => {
if (!target || typeof propertyKey === 'undefined') {
throw new Error('Invalid target or propertyKey in Field decorator.');
}
let field: FieldConfig;
if (typeof args === 'string') {
field = { type: args as BasicType };
} else {
field = { ...args };
}
if (
(field.type === 'option' ||
field.type === 'set' ||
field.type === 'array' ||
field.type === 'record' ||
field.type === 'object') &&
'typed' in field &&
field.typed
) {
if (typeof field.typed !== 'string') {
field.properties = field.typed.SurrealdbSchema.properties || {};
}
}
initializeSchema(target.constructor);
target.constructor.SurrealdbSchema.properties[propertyKey as string] = field;
};
}
/**
* This will add the "name" field to the static schema object. While the name could be inferred from the class's
* constructor, it is required because obfuscating the JS bundle in production builds changes the class names, and thus
* produces inconsistent or duplicate SurrealDB table names.
* @param name The table name in SurrealDB. This can be different from the class name
*/
export function Table(config: string | string[] | TableConfig): ClassDecorator {
return (constructor: any) => {
initializeSchema(constructor);
if (typeof config === 'string') {
constructor.SurrealdbSchema.tables = [config];
constructor.SurrealdbSchema.schemaMode = 'SCHEMALESS';
} else if (Array.isArray(config)) {
constructor.SurrealdbSchema.tables = config;
constructor.SurrealdbSchema.schemaMode = 'SCHEMALESS';
} else {
constructor.SurrealdbSchema.tables = config.tables;
constructor.SurrealdbSchema.schemaMode = config.schemaMode ?? 'SCHEMALESS';
constructor.SurrealdbSchema.indexes = config.indexes;
}
};
}
export function generateSurqlSchema<T extends typeof TableSchema>(entities: T[], comments = false) {
const schemaParts: string[] = [];
//
function addFieldDefinition(
tableName: string,
tableGeneric: string | undefined,
fieldName: string,
fieldConfig: FieldSchemaProperty,
) {
const fieldDefinition = [`DEFINE FIELD ${fieldName} ON ${tableName}`];
let fieldtype: string;
if (fieldConfig.primary) {
fieldtype = `record<${tableName}>`;
} else if (
fieldConfig.type === 'option' ||
fieldConfig.type === 'set' ||
fieldConfig.type === 'array' ||
fieldConfig.type === 'record' ||
fieldConfig.type === 'object'
) {
if ('typed' in fieldConfig && fieldConfig.typed) {
if (typeof fieldConfig.typed === 'string') {
const fieldtyped =
fieldConfig.typed === '$$generic' ? (tableGeneric ?? 'any') : fieldConfig.typed;
fieldtype = `${fieldConfig.type}<${fieldtyped}>`;
} else {
if (fieldConfig.type === 'object') {
fieldtype = fieldConfig.type;
} else {
fieldtype = `${fieldConfig.type}<object>`;
}
}
} else {
fieldtype = fieldConfig.type;
}
} else {
fieldtype =
fieldConfig.type === '$$generic' ? (tableGeneric ?? 'any') : (fieldConfig.type ?? 'any');
}
if (fieldConfig.optional) {
fieldDefinition.push(`TYPE option<${fieldtype}>`);
} else {
fieldDefinition.push(`TYPE ${fieldtype}`);
}
if (fieldConfig.default) {
fieldDefinition.push(`DEFAULT ${fieldConfig.default}`);
}
if (fieldConfig.value) {
fieldDefinition.push(`VALUE ${fieldConfig.value}`);
}
if (fieldConfig.assertion) {
fieldDefinition.push(`ASSERT ${fieldConfig.assertion}`);
}
schemaParts.push(`${fieldDefinition.join(' ')};`);
// Recursively define nested fields
if (
(fieldConfig.type === 'option' ||
fieldConfig.type === 'set' ||
fieldConfig.type === 'array' ||
fieldConfig.type === 'record' ||
fieldConfig.type === 'object') &&
'properties' in fieldConfig
) {
for (const [nestedFieldName, nestedFieldConfig] of Object.entries(
fieldConfig.properties as PropertiesTypes,
)) {
const parentFieldName =
fieldConfig.type === 'set' || fieldConfig.type === 'array'
? `${fieldName}.*.${nestedFieldName}`
: `${fieldName}.${nestedFieldName}`;
addFieldDefinition(tableName, tableGeneric, `${parentFieldName}`, nestedFieldConfig);
addIndexDefinition(tableName, `${parentFieldName}`, nestedFieldConfig);
}
}
}
//
function addIndexDefinition(
tableName: string,
fieldName: string,
fieldConfig: FieldSchemaProperty,
) {
if (fieldConfig.primary || fieldConfig.indexed || fieldConfig.unique) {
const indexDefinition = [
`DEFINE INDEX idx_${tableName}_${fieldName.replace(/[\*\.]/g, '_')} ON ${tableName} FIELDS ${fieldName}`,
];
if (fieldConfig.primary || fieldConfig.unique) indexDefinition.push('UNIQUE');
schemaParts.push(`${indexDefinition.join(' ')};`);
}
}
//
function addTableIndexes(tableName: string, indexes?: IndexSchema[]) {
indexes?.forEach(index => {
const indexDefinition = [
`DEFINE INDEX ${index.name ?? `idx_${tableName}_${index.fields.join('_')}`} ON ${tableName} FIELDS ${index.fields.join(', ')}`,
];
if (index.unique) indexDefinition.push('UNIQUE');
schemaParts.push(`${indexDefinition.join(' ')};`);
});
}
// start schema generation
for (const entity of entities) {
const { tables, schemaMode, indexes, properties } = entity.SurrealdbSchema;
for (const table of tables) {
let tableName: string;
let tableGeneric: string | undefined;
if (typeof table === 'string') {
tableName = table;
tableGeneric = undefined;
} else {
tableName = table.name;
tableGeneric = table.generic;
}
if (comments) {
schemaParts.push('-- ------------------------------');
schemaParts.push(`-- TABLE: ${tableName}`);
schemaParts.push('-- ------------------------------\n');
}
// Begin table definition
schemaParts.push(`DEFINE TABLE ${tableName} ${schemaMode};`);
if (comments) schemaParts.push('');
// Define fields & indexes
for (const [fieldName, fieldConfig] of Object.entries(properties)) {
// Define field
addFieldDefinition(tableName, tableGeneric, fieldName, fieldConfig);
// Define index
addIndexDefinition(tableName, fieldName, fieldConfig);
}
// Define table indexes
addTableIndexes(tableName, indexes);
// Separate each table definition
schemaParts.push('\n');
}
}
return schemaParts.join('\n');
}