@objectql/core 1.8.0 → 1.8.2

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.
@@ -0,0 +1,289 @@
1
+ import { convertIntrospectedSchemaToObjects } from '../src/util';
2
+ import { IntrospectedSchema, ObjectConfig } from '@objectql/types';
3
+
4
+ describe('convertIntrospectedSchemaToObjects', () => {
5
+ it('should convert simple table to object config', () => {
6
+ const introspectedSchema: IntrospectedSchema = {
7
+ tables: {
8
+ users: {
9
+ name: 'users',
10
+ columns: [
11
+ {
12
+ name: 'id',
13
+ type: 'varchar',
14
+ nullable: false,
15
+ isPrimary: true
16
+ },
17
+ {
18
+ name: 'name',
19
+ type: 'varchar',
20
+ nullable: false
21
+ },
22
+ {
23
+ name: 'email',
24
+ type: 'varchar',
25
+ nullable: true,
26
+ isUnique: true
27
+ },
28
+ {
29
+ name: 'age',
30
+ type: 'integer',
31
+ nullable: true
32
+ }
33
+ ],
34
+ foreignKeys: [],
35
+ primaryKeys: ['id']
36
+ }
37
+ }
38
+ };
39
+
40
+ const objects = convertIntrospectedSchemaToObjects(introspectedSchema);
41
+
42
+ expect(objects.length).toBe(1);
43
+ expect(objects[0].name).toBe('users');
44
+ expect(objects[0].label).toBe('Users');
45
+
46
+ // Check fields (id, created_at, updated_at should be skipped by default)
47
+ expect(objects[0].fields.name).toBeDefined();
48
+ expect(objects[0].fields.name.type).toBe('text');
49
+ expect(objects[0].fields.name.required).toBe(true);
50
+
51
+ expect(objects[0].fields.email).toBeDefined();
52
+ expect(objects[0].fields.email.unique).toBe(true);
53
+ expect(objects[0].fields.email.required).toBe(false);
54
+
55
+ expect(objects[0].fields.age).toBeDefined();
56
+ expect(objects[0].fields.age.type).toBe('number');
57
+ });
58
+
59
+ it('should convert foreign keys to lookup fields', () => {
60
+ const introspectedSchema: IntrospectedSchema = {
61
+ tables: {
62
+ posts: {
63
+ name: 'posts',
64
+ columns: [
65
+ {
66
+ name: 'id',
67
+ type: 'varchar',
68
+ nullable: false,
69
+ isPrimary: true
70
+ },
71
+ {
72
+ name: 'title',
73
+ type: 'varchar',
74
+ nullable: false
75
+ },
76
+ {
77
+ name: 'author_id',
78
+ type: 'varchar',
79
+ nullable: false
80
+ }
81
+ ],
82
+ foreignKeys: [
83
+ {
84
+ columnName: 'author_id',
85
+ referencedTable: 'users',
86
+ referencedColumn: 'id'
87
+ }
88
+ ],
89
+ primaryKeys: ['id']
90
+ },
91
+ users: {
92
+ name: 'users',
93
+ columns: [
94
+ {
95
+ name: 'id',
96
+ type: 'varchar',
97
+ nullable: false,
98
+ isPrimary: true
99
+ },
100
+ {
101
+ name: 'name',
102
+ type: 'varchar',
103
+ nullable: false
104
+ }
105
+ ],
106
+ foreignKeys: [],
107
+ primaryKeys: ['id']
108
+ }
109
+ }
110
+ };
111
+
112
+ const objects = convertIntrospectedSchemaToObjects(introspectedSchema);
113
+
114
+ const postsObj = objects.find(o => o.name === 'posts');
115
+ expect(postsObj).toBeDefined();
116
+
117
+ expect(postsObj!.fields.author_id).toBeDefined();
118
+ expect(postsObj!.fields.author_id.type).toBe('lookup');
119
+ expect(postsObj!.fields.author_id.reference_to).toBe('users');
120
+ expect(postsObj!.fields.author_id.required).toBe(true);
121
+ });
122
+
123
+ it('should map database types correctly', () => {
124
+ const introspectedSchema: IntrospectedSchema = {
125
+ tables: {
126
+ types_test: {
127
+ name: 'types_test',
128
+ columns: [
129
+ { name: 'text_field', type: 'text', nullable: true },
130
+ { name: 'varchar_field', type: 'varchar', nullable: true },
131
+ { name: 'int_field', type: 'integer', nullable: true },
132
+ { name: 'bigint_field', type: 'bigint', nullable: true },
133
+ { name: 'float_field', type: 'float', nullable: true },
134
+ { name: 'decimal_field', type: 'decimal', nullable: true },
135
+ { name: 'bool_field', type: 'boolean', nullable: true },
136
+ { name: 'date_field', type: 'date', nullable: true },
137
+ { name: 'datetime_field', type: 'datetime', nullable: true },
138
+ { name: 'timestamp_field', type: 'timestamp', nullable: true },
139
+ { name: 'json_field', type: 'json', nullable: true }
140
+ ],
141
+ foreignKeys: [],
142
+ primaryKeys: []
143
+ }
144
+ }
145
+ };
146
+
147
+ const objects = convertIntrospectedSchemaToObjects(introspectedSchema);
148
+ const obj = objects[0];
149
+
150
+ expect(obj.fields.text_field.type).toBe('textarea');
151
+ expect(obj.fields.varchar_field.type).toBe('text');
152
+ expect(obj.fields.int_field.type).toBe('number');
153
+ expect(obj.fields.bigint_field.type).toBe('number');
154
+ expect(obj.fields.float_field.type).toBe('number');
155
+ expect(obj.fields.decimal_field.type).toBe('number');
156
+ expect(obj.fields.bool_field.type).toBe('boolean');
157
+ expect(obj.fields.date_field.type).toBe('date');
158
+ expect(obj.fields.datetime_field.type).toBe('datetime');
159
+ expect(obj.fields.timestamp_field.type).toBe('datetime');
160
+ expect(obj.fields.json_field.type).toBe('object');
161
+ });
162
+
163
+ it('should exclude tables based on options', () => {
164
+ const introspectedSchema: IntrospectedSchema = {
165
+ tables: {
166
+ users: {
167
+ name: 'users',
168
+ columns: [
169
+ { name: 'id', type: 'varchar', nullable: false, isPrimary: true },
170
+ { name: 'name', type: 'varchar', nullable: false }
171
+ ],
172
+ foreignKeys: [],
173
+ primaryKeys: ['id']
174
+ },
175
+ sessions: {
176
+ name: 'sessions',
177
+ columns: [
178
+ { name: 'id', type: 'varchar', nullable: false, isPrimary: true },
179
+ { name: 'token', type: 'varchar', nullable: false }
180
+ ],
181
+ foreignKeys: [],
182
+ primaryKeys: ['id']
183
+ }
184
+ }
185
+ };
186
+
187
+ const objects = convertIntrospectedSchemaToObjects(introspectedSchema, {
188
+ excludeTables: ['sessions']
189
+ });
190
+
191
+ expect(objects.length).toBe(1);
192
+ expect(objects[0].name).toBe('users');
193
+ });
194
+
195
+ it('should include only specified tables', () => {
196
+ const introspectedSchema: IntrospectedSchema = {
197
+ tables: {
198
+ users: {
199
+ name: 'users',
200
+ columns: [
201
+ { name: 'id', type: 'varchar', nullable: false, isPrimary: true },
202
+ { name: 'name', type: 'varchar', nullable: false }
203
+ ],
204
+ foreignKeys: [],
205
+ primaryKeys: ['id']
206
+ },
207
+ posts: {
208
+ name: 'posts',
209
+ columns: [
210
+ { name: 'id', type: 'varchar', nullable: false, isPrimary: true },
211
+ { name: 'title', type: 'varchar', nullable: false }
212
+ ],
213
+ foreignKeys: [],
214
+ primaryKeys: ['id']
215
+ },
216
+ sessions: {
217
+ name: 'sessions',
218
+ columns: [
219
+ { name: 'id', type: 'varchar', nullable: false, isPrimary: true }
220
+ ],
221
+ foreignKeys: [],
222
+ primaryKeys: ['id']
223
+ }
224
+ }
225
+ };
226
+
227
+ const objects = convertIntrospectedSchemaToObjects(introspectedSchema, {
228
+ includeTables: ['users', 'posts']
229
+ });
230
+
231
+ expect(objects.length).toBe(2);
232
+ expect(objects.map(o => o.name)).toContain('users');
233
+ expect(objects.map(o => o.name)).toContain('posts');
234
+ expect(objects.map(o => o.name)).not.toContain('sessions');
235
+ });
236
+
237
+ it('should skip system columns by default', () => {
238
+ const introspectedSchema: IntrospectedSchema = {
239
+ tables: {
240
+ users: {
241
+ name: 'users',
242
+ columns: [
243
+ { name: 'id', type: 'varchar', nullable: false, isPrimary: true },
244
+ { name: 'name', type: 'varchar', nullable: false },
245
+ { name: 'created_at', type: 'timestamp', nullable: true },
246
+ { name: 'updated_at', type: 'timestamp', nullable: true }
247
+ ],
248
+ foreignKeys: [],
249
+ primaryKeys: ['id']
250
+ }
251
+ }
252
+ };
253
+
254
+ const objects = convertIntrospectedSchemaToObjects(introspectedSchema);
255
+
256
+ // System columns should be skipped
257
+ expect(objects[0].fields.id).toBeUndefined();
258
+ expect(objects[0].fields.created_at).toBeUndefined();
259
+ expect(objects[0].fields.updated_at).toBeUndefined();
260
+
261
+ // But regular columns should be present
262
+ expect(objects[0].fields.name).toBeDefined();
263
+ });
264
+
265
+ it('should include system columns when skipSystemColumns is false', () => {
266
+ const introspectedSchema: IntrospectedSchema = {
267
+ tables: {
268
+ users: {
269
+ name: 'users',
270
+ columns: [
271
+ { name: 'id', type: 'varchar', nullable: false, isPrimary: true },
272
+ { name: 'name', type: 'varchar', nullable: false },
273
+ { name: 'created_at', type: 'timestamp', nullable: true }
274
+ ],
275
+ foreignKeys: [],
276
+ primaryKeys: ['id']
277
+ }
278
+ }
279
+ };
280
+
281
+ const objects = convertIntrospectedSchemaToObjects(introspectedSchema, {
282
+ skipSystemColumns: false
283
+ });
284
+
285
+ // System columns should be included
286
+ expect(objects[0].fields.id).toBeDefined();
287
+ expect(objects[0].fields.created_at).toBeDefined();
288
+ });
289
+ });