@delma/fylo 1.0.0

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.
Files changed (60) hide show
  1. package/.env.example +16 -0
  2. package/.github/copilot-instructions.md +113 -0
  3. package/.github/prompts/issue.prompt.md +19 -0
  4. package/.github/prompts/pr.prompt.md +18 -0
  5. package/.github/prompts/release.prompt.md +49 -0
  6. package/.github/prompts/review-pr.prompt.md +19 -0
  7. package/.github/prompts/sync-main.prompt.md +14 -0
  8. package/.github/workflows/ci.yml +37 -0
  9. package/.github/workflows/publish.yml +101 -0
  10. package/.prettierrc +7 -0
  11. package/LICENSE +21 -0
  12. package/README.md +230 -0
  13. package/eslint.config.js +28 -0
  14. package/package.json +51 -0
  15. package/src/CLI +37 -0
  16. package/src/adapters/cipher.ts +174 -0
  17. package/src/adapters/redis.ts +71 -0
  18. package/src/adapters/s3.ts +67 -0
  19. package/src/core/directory.ts +418 -0
  20. package/src/core/extensions.ts +19 -0
  21. package/src/core/format.ts +486 -0
  22. package/src/core/parser.ts +876 -0
  23. package/src/core/query.ts +48 -0
  24. package/src/core/walker.ts +167 -0
  25. package/src/index.ts +1088 -0
  26. package/src/types/fylo.d.ts +139 -0
  27. package/src/types/index.d.ts +3 -0
  28. package/src/types/query.d.ts +73 -0
  29. package/tests/collection/truncate.test.ts +56 -0
  30. package/tests/data.ts +110 -0
  31. package/tests/index.ts +19 -0
  32. package/tests/integration/create.test.ts +57 -0
  33. package/tests/integration/delete.test.ts +147 -0
  34. package/tests/integration/edge-cases.test.ts +232 -0
  35. package/tests/integration/encryption.test.ts +176 -0
  36. package/tests/integration/export.test.ts +61 -0
  37. package/tests/integration/join-modes.test.ts +221 -0
  38. package/tests/integration/nested.test.ts +212 -0
  39. package/tests/integration/operators.test.ts +167 -0
  40. package/tests/integration/read.test.ts +203 -0
  41. package/tests/integration/rollback.test.ts +105 -0
  42. package/tests/integration/update.test.ts +130 -0
  43. package/tests/mocks/cipher.ts +55 -0
  44. package/tests/mocks/redis.ts +13 -0
  45. package/tests/mocks/s3.ts +114 -0
  46. package/tests/schemas/album.d.ts +5 -0
  47. package/tests/schemas/album.json +5 -0
  48. package/tests/schemas/comment.d.ts +7 -0
  49. package/tests/schemas/comment.json +7 -0
  50. package/tests/schemas/photo.d.ts +7 -0
  51. package/tests/schemas/photo.json +7 -0
  52. package/tests/schemas/post.d.ts +6 -0
  53. package/tests/schemas/post.json +6 -0
  54. package/tests/schemas/tip.d.ts +7 -0
  55. package/tests/schemas/tip.json +7 -0
  56. package/tests/schemas/todo.d.ts +6 -0
  57. package/tests/schemas/todo.json +6 -0
  58. package/tests/schemas/user.d.ts +23 -0
  59. package/tests/schemas/user.json +23 -0
  60. package/tsconfig.json +19 -0
@@ -0,0 +1,486 @@
1
+ import TTID from '@vyckr/ttid'
2
+
3
+ class Format {
4
+ static table(docs: Record<string, any>) {
5
+ // Calculate the _id column width (considering both the column name and the actual keys)
6
+ const idColumnWidth =
7
+ Math.max(...Object.keys(docs).map((key) => key.length)) + 2; // Add padding
8
+
9
+ const { maxWidths, maxHeight } = this.getHeaderDim(Object.values(docs));
10
+
11
+ let key = Object.keys(docs).shift()!
12
+
13
+ const keys = key.split(',')
14
+
15
+ if(TTID.isTTID(key) || keys.some(key => TTID.isTTID(key ?? ''))) {
16
+ key = '_id'
17
+ } else key = '_key'
18
+
19
+ // Add the _id column to the front of maxWidths
20
+ const fullWidths = {
21
+ [key]: idColumnWidth,
22
+ ...maxWidths,
23
+ };
24
+
25
+ // Render the header
26
+ const header = this.renderHeader(fullWidths, maxHeight, key);
27
+ console.log("\n" + header);
28
+
29
+ // Render the data rows
30
+ const dataRows = this.renderDataRows(docs, fullWidths, key);
31
+ console.log(dataRows);
32
+ }
33
+
34
+ private static getHeaderDim(docs: Record<string, any>[]) {
35
+ let maxWidths: Record<string, any> = {};
36
+ let maxHeight = 1;
37
+
38
+ // Create a copy to avoid mutating the original array
39
+ const docsCopy = [...docs];
40
+
41
+ while (docsCopy.length > 0) {
42
+ const doc = docsCopy.shift()!;
43
+ const widths = this.getValueWidth(doc);
44
+ const height = this.getHeaderHeight(doc); // Fix: get height for this doc
45
+ maxHeight = Math.max(maxHeight, height); // Fix: take maximum height
46
+ maxWidths = this.increaseWidths(maxWidths, widths);
47
+ }
48
+
49
+ return { maxWidths, maxHeight };
50
+ }
51
+
52
+ private static getValueWidth(doc: Record<string, any>) {
53
+ const keyWidths: Record<string, any> = {};
54
+
55
+ for (const key in doc) {
56
+ if (
57
+ typeof doc[key] === "object" &&
58
+ doc[key] !== null &&
59
+ !Array.isArray(doc[key])
60
+ ) {
61
+ keyWidths[key] = this.getValueWidth(doc[key]);
62
+ } else {
63
+ // Consider both the key name length and the value length
64
+ const valueWidth = JSON.stringify(doc[key]).length;
65
+ const keyWidth = key.length;
66
+ // Add padding: 1 space before + content + 1 space after
67
+ keyWidths[key] = Math.max(valueWidth, keyWidth) + 2;
68
+ }
69
+ }
70
+
71
+ return keyWidths;
72
+ }
73
+
74
+ private static increaseWidths(
75
+ oldWidths: Record<string, any>,
76
+ newWidths: Record<string, any>
77
+ ) {
78
+ const increasedWidths: Record<string, any> = { ...oldWidths };
79
+
80
+ for (const key in newWidths) {
81
+ if (
82
+ oldWidths[key] &&
83
+ typeof newWidths[key] === "object" &&
84
+ typeof oldWidths[key] === "object"
85
+ ) {
86
+ increasedWidths[key] = this.increaseWidths(
87
+ oldWidths[key],
88
+ newWidths[key]
89
+ );
90
+ } else if (
91
+ oldWidths[key] &&
92
+ typeof newWidths[key] === "number" &&
93
+ typeof oldWidths[key] === "number"
94
+ ) {
95
+ increasedWidths[key] = Math.max(newWidths[key], oldWidths[key]);
96
+ } else {
97
+ increasedWidths[key] = newWidths[key];
98
+ }
99
+ }
100
+
101
+ // Handle keys that exist in newWidths but not in oldWidths
102
+ for (const key in newWidths) {
103
+ if (!(key in increasedWidths)) {
104
+ increasedWidths[key] = newWidths[key];
105
+ }
106
+ }
107
+
108
+ // Also ensure column family names fit within their total width
109
+ for (const key in increasedWidths) {
110
+ if (
111
+ typeof increasedWidths[key] === "object" &&
112
+ increasedWidths[key] !== null
113
+ ) {
114
+ const totalChildWidth = this.calculateTotalWidth(increasedWidths[key]);
115
+ const keyWidth = key.length;
116
+
117
+ // If the column family name (with padding) is longer than the total child width,
118
+ // we need to adjust the child column widths proportionally
119
+ const keyWidthWithPadding = keyWidth + 2; // Add padding for family name too
120
+ if (keyWidthWithPadding > totalChildWidth) {
121
+ const childKeys = Object.keys(increasedWidths[key]);
122
+ const extraWidth = keyWidthWithPadding - totalChildWidth;
123
+ const widthPerChild = Math.ceil(extraWidth / childKeys.length);
124
+
125
+ for (const childKey of childKeys) {
126
+ if (typeof increasedWidths[key][childKey] === "number") {
127
+ increasedWidths[key][childKey] += widthPerChild;
128
+ }
129
+ }
130
+ }
131
+ }
132
+ }
133
+
134
+ return increasedWidths;
135
+ }
136
+
137
+ private static getHeaderHeight(doc: Record<string, any>): number {
138
+ let maxDepth = 1; // Fix: start with 1 for current level
139
+
140
+ for (const key in doc) {
141
+ if (
142
+ typeof doc[key] === "object" &&
143
+ doc[key] !== null &&
144
+ !Array.isArray(doc[key])
145
+ ) {
146
+ const nestedDepth = 1 + this.getHeaderHeight(doc[key]); // Fix: add 1 for current level
147
+ maxDepth = Math.max(maxDepth, nestedDepth); // Fix: track maximum depth
148
+ }
149
+ }
150
+
151
+ return maxDepth;
152
+ }
153
+
154
+ private static renderHeader(
155
+ widths: Record<string, any>,
156
+ height: number,
157
+ idColumnKey: string
158
+ ): string {
159
+ const lines: string[] = [];
160
+
161
+ // Flatten the structure to get all columns
162
+ const columns = this.flattenColumns(widths);
163
+
164
+ // Add top border
165
+ lines.push(this.renderTopBorder(columns));
166
+
167
+ // Add header content rows
168
+ for (let level = 0; level < height; level++) {
169
+ lines.push(this.renderHeaderRow(widths, level, height, idColumnKey));
170
+
171
+ // Add middle border between levels (except after last level)
172
+ if (level < height - 1) {
173
+ lines.push(this.renderMiddleBorder(columns));
174
+ }
175
+ }
176
+
177
+ // Add bottom border
178
+ lines.push(this.renderBottomBorder(columns));
179
+
180
+ return lines.join("\n");
181
+ }
182
+
183
+ private static renderDataRows<T extends Record<string, any>>(
184
+ docs: Record<string, T>,
185
+ widths: Record<string, any>,
186
+ idColumnKey: string
187
+ ): string {
188
+ const lines: string[] = [];
189
+ const columns = this.flattenColumns(widths);
190
+ const entries = Object.entries(docs);
191
+
192
+ for (let i = 0; i < entries.length; i++) {
193
+ const [docId, doc] = entries[i];
194
+ // Render data row
195
+ lines.push(this.renderDataRow(docId, doc, widths, columns, idColumnKey));
196
+
197
+ // Add separator between rows (except for last row)
198
+ if (i < entries.length - 1) {
199
+ lines.push(this.renderRowSeparator(columns));
200
+ }
201
+ }
202
+
203
+ // Add final bottom border
204
+ lines.push(this.renderBottomBorder(columns));
205
+
206
+ return lines.join("\n");
207
+ }
208
+
209
+ private static renderDataRow(
210
+ docId: string,
211
+ doc: Record<string, any>,
212
+ widths: Record<string, any>,
213
+ columns: Array<{ name: string; width: number; path: string[] }>,
214
+ idColumnKey: string
215
+ ): string {
216
+ let line = "│";
217
+
218
+ // Handle the ID column (could be _id or another key)
219
+ if (idColumnKey in widths && typeof widths[idColumnKey] === "number") {
220
+ const contentWidth = widths[idColumnKey] - 2;
221
+ const content = docId;
222
+ const padding = Math.max(0, contentWidth - content.length);
223
+ const leftPad = Math.floor(padding / 2);
224
+ const rightPad = padding - leftPad;
225
+
226
+ line += " " + " ".repeat(leftPad) + content + " ".repeat(rightPad) + " │";
227
+ }
228
+
229
+ // Handle data columns
230
+ for (const column of columns) {
231
+ // Skip the ID column as it's handled separately
232
+ if (column.name === idColumnKey) continue;
233
+
234
+ const value = this.getNestedValue(doc, column.path);
235
+ const stringValue = this.formatValue(value);
236
+ const contentWidth = column.width - 2; // Subtract padding
237
+
238
+ // Truncate if value is too long
239
+ const truncatedValue =
240
+ stringValue.length > contentWidth
241
+ ? stringValue.substring(0, contentWidth - 3) + "..."
242
+ : stringValue;
243
+
244
+ const padding = Math.max(0, contentWidth - truncatedValue.length);
245
+ const leftPad = Math.floor(padding / 2);
246
+ const rightPad = padding - leftPad;
247
+
248
+ line +=
249
+ " " +
250
+ " ".repeat(leftPad) +
251
+ truncatedValue +
252
+ " ".repeat(rightPad) +
253
+ " │";
254
+ }
255
+
256
+ return line;
257
+ }
258
+
259
+ private static getNestedValue(obj: Record<string, any>, path: string[]): any {
260
+ let current = obj;
261
+
262
+ for (const key of path) {
263
+ if (
264
+ current === null ||
265
+ current === undefined ||
266
+ typeof current !== "object"
267
+ ) {
268
+ return undefined;
269
+ }
270
+ current = current[key];
271
+ }
272
+
273
+ return current;
274
+ }
275
+
276
+ private static formatValue(value: any): string {
277
+ if (value === null) return "null";
278
+ if (value === undefined) return "";
279
+ if (Array.isArray(value)) return JSON.stringify(value);
280
+ if (typeof value === "object") return JSON.stringify(value);
281
+ if (typeof value === "string") return value;
282
+ return String(value);
283
+ }
284
+
285
+ private static renderRowSeparator(
286
+ columns: Array<{ name: string; width: number; path: string[] }>
287
+ ): string {
288
+ let line = "├";
289
+
290
+ for (let i = 0; i < columns.length; i++) {
291
+ line += "─".repeat(columns[i].width);
292
+ if (i < columns.length - 1) {
293
+ line += "┼";
294
+ }
295
+ }
296
+
297
+ line += "┤";
298
+ return line;
299
+ }
300
+
301
+ private static flattenColumns(
302
+ widths: Record<string, any>,
303
+ path: string[] = []
304
+ ): Array<{ name: string; width: number; path: string[] }> {
305
+ const columns: Array<{ name: string; width: number; path: string[] }> = [];
306
+
307
+ for (const key in widths) {
308
+ const currentPath = [...path, key];
309
+
310
+ if (typeof widths[key] === "object" && widths[key] !== null) {
311
+ // Recursively flatten nested objects
312
+ columns.push(...this.flattenColumns(widths[key], currentPath));
313
+ } else {
314
+ // This is a leaf column
315
+ columns.push({
316
+ name: key,
317
+ width: widths[key],
318
+ path: currentPath,
319
+ });
320
+ }
321
+ }
322
+
323
+ return columns;
324
+ }
325
+
326
+ private static renderHeaderRow(
327
+ widths: Record<string, any>,
328
+ currentLevel: number,
329
+ totalHeight: number,
330
+ idColumnKey: string
331
+ ): string {
332
+ let line = "│";
333
+
334
+ // Handle the ID column specially (could be _id or another key)
335
+ if (idColumnKey in widths && typeof widths[idColumnKey] === "number") {
336
+ if (currentLevel === 0) {
337
+ // Show the ID column header at the top level
338
+ const contentWidth = widths[idColumnKey] - 2;
339
+ const headerText = idColumnKey === '_id' ? '_id' : idColumnKey;
340
+ const padding = Math.max(0, contentWidth - headerText.length);
341
+ const leftPad = Math.floor(padding / 2);
342
+ const rightPad = padding - leftPad;
343
+
344
+ line += " " + " ".repeat(leftPad) + headerText + " ".repeat(rightPad) + " │";
345
+ } else {
346
+ // Empty cell for other levels
347
+ line += " ".repeat(widths[idColumnKey]) + "│";
348
+ }
349
+ }
350
+
351
+ const processLevel = (
352
+ obj: Record<string, any>,
353
+ level: number,
354
+ targetLevel: number
355
+ ): string => {
356
+ let result = "";
357
+
358
+ for (const key in obj) {
359
+ // Skip the ID column as it's handled separately
360
+ if (key === idColumnKey) continue;
361
+
362
+ if (typeof obj[key] === "object" && obj[key] !== null) {
363
+ if (level === targetLevel) {
364
+ // This is a column family at the target level
365
+ const totalWidth = this.calculateTotalWidth(obj[key]);
366
+ const contentWidth = totalWidth - 2; // Subtract padding
367
+ const padding = Math.max(0, contentWidth - key.length);
368
+ const leftPad = Math.floor(padding / 2);
369
+ const rightPad = padding - leftPad;
370
+
371
+ // Add 1 space padding + centered content + 1 space padding
372
+ result +=
373
+ " " + " ".repeat(leftPad) + key + " ".repeat(rightPad) + " │";
374
+ } else if (level < targetLevel) {
375
+ // Recurse deeper
376
+ result += processLevel(obj[key], level + 1, targetLevel);
377
+ }
378
+ } else {
379
+ if (level === targetLevel) {
380
+ // This is a leaf column at the target level
381
+ const contentWidth = obj[key] - 2; // Subtract padding
382
+ const padding = Math.max(0, contentWidth - key.length);
383
+ const leftPad = Math.floor(padding / 2);
384
+ const rightPad = padding - leftPad;
385
+
386
+ // Add 1 space padding + centered content + 1 space padding
387
+ result +=
388
+ " " + " ".repeat(leftPad) + key + " ".repeat(rightPad) + " │";
389
+ } else if (level < targetLevel) {
390
+ // Empty cell - span the full width
391
+ result += " ".repeat(obj[key]) + "│";
392
+ }
393
+ }
394
+ }
395
+
396
+ return result;
397
+ };
398
+
399
+ line += processLevel(widths, 0, currentLevel);
400
+ return line;
401
+ }
402
+
403
+ private static calculateTotalWidth(obj: Record<string, any>): number {
404
+ let total = 0;
405
+ let columnCount = 0;
406
+
407
+ for (const key in obj) {
408
+ if (typeof obj[key] === "object" && obj[key] !== null) {
409
+ total += this.calculateTotalWidth(obj[key]);
410
+ columnCount += this.countLeafColumns(obj[key]);
411
+ } else {
412
+ total += obj[key];
413
+ columnCount++;
414
+ }
415
+ }
416
+
417
+ // Add space for separators between columns (one less than column count)
418
+ return total + Math.max(0, columnCount - 1);
419
+ }
420
+
421
+ private static countLeafColumns(obj: Record<string, any>): number {
422
+ let count = 0;
423
+
424
+ for (const key in obj) {
425
+ if (typeof obj[key] === "object" && obj[key] !== null) {
426
+ count += this.countLeafColumns(obj[key]);
427
+ } else {
428
+ count++;
429
+ }
430
+ }
431
+
432
+ return count;
433
+ }
434
+
435
+ private static renderTopBorder(
436
+ columns: Array<{ name: string; width: number; path: string[] }>
437
+ ): string {
438
+ let line = "┌";
439
+
440
+ for (let i = 0; i < columns.length; i++) {
441
+ line += "─".repeat(columns[i].width);
442
+ if (i < columns.length - 1) {
443
+ line += "┬";
444
+ }
445
+ }
446
+
447
+ line += "┐";
448
+ return line;
449
+ }
450
+
451
+ private static renderMiddleBorder(
452
+ columns: Array<{ name: string; width: number; path: string[] }>
453
+ ): string {
454
+ let line = "├";
455
+
456
+ for (let i = 0; i < columns.length; i++) {
457
+ line += "─".repeat(columns[i].width);
458
+ if (i < columns.length - 1) {
459
+ line += "┼";
460
+ }
461
+ }
462
+
463
+ line += "┤";
464
+ return line;
465
+ }
466
+
467
+ private static renderBottomBorder(
468
+ columns: Array<{ name: string; width: number; path: string[] }>
469
+ ): string {
470
+ let line = "└";
471
+
472
+ for (let i = 0; i < columns.length; i++) {
473
+ line += "─".repeat(columns[i].width);
474
+ if (i < columns.length - 1) {
475
+ line += "┴";
476
+ }
477
+ }
478
+
479
+ line += "┘";
480
+ return line;
481
+ }
482
+ }
483
+
484
+ console.format = function(docs: Record<string, any>) {
485
+ Format.table(docs)
486
+ }