|
3 | 3 | // This test script initializes a SQLite3 database in memory, |
4 | 4 | // creates a small test table, and runs various queries to demonstrate functionality. |
5 | 5 |
|
6 | | -// import path from 'path'; |
7 | | -// import { fileURLToPath } from 'url'; |
| 6 | +import stringify from 'safe-stable-stringify'; |
| 7 | +import { ColorSafeStringify } from 'tiny-essentials'; |
8 | 8 | import PuddySql from '../dist/index.mjs'; |
9 | 9 |
|
10 | | -// 🌐 Path Setup |
11 | | -// const __filename = fileURLToPath(import.meta.url); |
12 | | -// const __dirname = path.dirname(__filename); |
| 10 | +const colorJsonSafe = new ColorSafeStringify(); |
| 11 | +const colorSafeStringify = (json, space = 0) => |
| 12 | + colorJsonSafe.colorize(stringify(json, null, space)); |
13 | 13 |
|
14 | 14 | // 🚀 Create SQL Engine Instance |
15 | 15 | const db = new PuddySql.Instance(); |
@@ -38,90 +38,129 @@ const db = new PuddySql.Instance(); |
38 | 38 |
|
39 | 39 | // 📤 Fetching All Records |
40 | 40 | console.log('\n📃 All Records:\n'); |
41 | | - const allItems = await table.getAll(); |
42 | | - console.table(allItems); |
| 41 | + console.table(await table.getAll()); |
43 | 42 |
|
44 | 43 | // 🔍 Find by ID |
45 | 44 | console.log('\n🔍 Getting record with ID = 1\n'); |
46 | | - const oneItem = await table.get('1'); |
47 | | - console.dir(oneItem); |
| 45 | + console.log(colorSafeStringify(await table.get('1'))); |
48 | 46 |
|
49 | 47 | // 🔁 Update an Entry |
50 | 48 | console.log('\n📝 Updating ID = 4 (yay = true)\n'); |
51 | 49 | await table.set('4', { yay: true }); |
52 | | - const updated = await table.get('4'); |
53 | | - console.dir(updated); |
| 50 | + console.log(colorSafeStringify(await table.get('4'))); |
54 | 51 |
|
55 | 52 | console.log('\n📝 Updating ID = 4 (yay = false)\n'); |
56 | 53 | await table.set('4', { yay: false }); |
57 | | - const updated2 = await table.get('4'); |
58 | | - console.dir(updated2); |
| 54 | + console.log(colorSafeStringify(await table.get('4'))); |
59 | 55 |
|
60 | 56 | // ❌ Delete an Entry |
61 | 57 | console.log('\n🗑️ Deleting ID = 2\n'); |
62 | 58 | await table.delete('2'); |
63 | | - const afterDelete = await table.getAll(); |
64 | | - console.table(afterDelete); |
| 59 | + console.table(await table.getAll()); |
65 | 60 |
|
66 | 61 | // 🔎 Advanced Search: prompt = pudding |
67 | 62 | console.log('\n🔎 Search: prompt = pudding\n'); |
68 | | - const search1 = await table.search({ |
69 | | - q: { |
70 | | - group: 'AND', |
71 | | - conditions: [{ column: 'prompt', value: '🍮 pudding' }], |
72 | | - }, |
73 | | - }); |
74 | | - console.dir(search1); |
| 63 | + console.log( |
| 64 | + colorSafeStringify( |
| 65 | + await table.search({ |
| 66 | + q: { |
| 67 | + group: 'AND', |
| 68 | + conditions: [{ column: 'prompt', value: '🍮 pudding' }], |
| 69 | + }, |
| 70 | + }), |
| 71 | + 1, |
| 72 | + ), |
| 73 | + ); |
75 | 74 |
|
76 | 75 | // 🔎 OR Search: prompt = pudding OR yay = false |
77 | 76 | console.log('\n🔎 OR Search: pudding or yay = false\n'); |
78 | | - const search2 = await table.search({ |
79 | | - q: { |
80 | | - group: 'OR', |
81 | | - conditions: [ |
82 | | - { column: 'prompt', value: '🍮 pudding' }, |
83 | | - { column: 'yay', value: false }, |
84 | | - ], |
85 | | - }, |
86 | | - }); |
87 | | - console.dir(search2); |
| 77 | + console.log( |
| 78 | + colorSafeStringify( |
| 79 | + await table.search({ |
| 80 | + q: { |
| 81 | + group: 'OR', |
| 82 | + conditions: [ |
| 83 | + { column: 'prompt', value: '🍮 pudding' }, |
| 84 | + { column: 'yay', value: false }, |
| 85 | + ], |
| 86 | + }, |
| 87 | + }), |
| 88 | + 1, |
| 89 | + ), |
| 90 | + ); |
88 | 91 |
|
89 | 92 | // 📚 Paginated Search |
90 | 93 | console.log('\n📚 Paginated Search (2 per page)\n'); |
91 | | - const paged = await table.search({ |
92 | | - q: {}, |
93 | | - perPage: 2, |
94 | | - page: 1, |
95 | | - order: 'id ASC', |
96 | | - }); |
97 | | - console.table(paged.items); |
| 94 | + const page1 = await table.search({ q: {}, perPage: 2, page: 1, order: 'id ASC' }); |
| 95 | + console.table(page1.items); |
98 | 96 |
|
99 | 97 | console.log('\n📚 Paginated Search (Page 2)\n'); |
100 | | - const paged2 = await table.search({ |
101 | | - q: {}, |
102 | | - perPage: 2, |
103 | | - page: 2, |
104 | | - order: 'id ASC', |
105 | | - }); |
106 | | - console.table(paged2.items); |
107 | | - console.table(paged2); |
| 98 | + const page2 = await table.search({ q: {}, perPage: 2, page: 2, order: 'id ASC' }); |
| 99 | + console.table(page2.items); |
| 100 | + console.table({ totalPages: page2.totalPages, totalItems: page2.totalItems }); |
108 | 101 |
|
109 | 102 | // 📌 Get amount (first 3) |
110 | 103 | console.log('\n📌 Getting first 3 records\n'); |
111 | | - const few = await table.getAmount(3); |
112 | | - console.table(few); |
| 104 | + console.table(await table.getAmount(3)); |
113 | 105 |
|
114 | 106 | // 🧹 Advanced delete: delete all yay = false |
115 | 107 | console.log('\n🧹 Deleting all yay = false...\n'); |
116 | | - const deleted = await table.advancedDelete({ |
117 | | - yay: { value: false }, |
118 | | - }); |
| 108 | + const deleted = await table.advancedDelete({ yay: { value: false } }); |
119 | 109 | console.log(`Deleted rows: ${deleted}`); |
120 | 110 |
|
121 | 111 | // 🧾 Final state |
122 | 112 | console.log('\n🧾 Final Records:\n'); |
123 | | - const final = await table.getAll(); |
124 | | - console.table(final); |
| 113 | + console.table(await table.getAll()); |
125 | 114 |
|
| 115 | + // 🏷️ Tags System Tests |
| 116 | + console.log('\n🏷️ Creating tagged_posts table...\n'); |
| 117 | + const tagTable = await db.initTable({ name: 'tagged_posts', id: 'id' }, [ |
| 118 | + ['id', 'TEXT', 'PRIMARY KEY'], |
| 119 | + ['title', 'TEXT'], |
| 120 | + ['tags', 'TAGS'], |
| 121 | + ]); |
| 122 | + |
| 123 | + const tagManager = tagTable.getTagEditor('tags'); |
| 124 | + |
| 125 | + await tagTable.set('a1', { title: 'Post 1', tags: ['cute', 'funny'] }); |
| 126 | + await tagTable.set('a2', { title: 'Post 2', tags: ['serious'] }); |
| 127 | + await tagTable.set('a3', { title: 'Post 3', tags: ['cute', 'deep'] }); |
| 128 | + const tagItems = await tagTable.getAll(); |
| 129 | + console.table(tagItems); |
| 130 | + |
| 131 | + console.log('\n🔖 Search: has tag "cute"\n'); |
| 132 | + console.table( |
| 133 | + await tagTable.search({ |
| 134 | + tagsQ: { column: 'tags', include: ['cute'] }, |
| 135 | + }), |
| 136 | + ); |
| 137 | + |
| 138 | + console.log('\n🧠 Advanced Tag Search: cute AND not serious\n'); |
| 139 | + console.table( |
| 140 | + await tagTable.search({ |
| 141 | + tagsQ: { column: 'tags', include: ['cute', '!serious'] } |
| 142 | + }), |
| 143 | + ); |
| 144 | + |
| 145 | + console.log('\n⚡ Boosted Tag Search (cute *2, deep *3)\n'); |
| 146 | + console.table( |
| 147 | + await tagTable.search({ |
| 148 | + select: { |
| 149 | + boost: { |
| 150 | + alias: 'p', |
| 151 | + value: [ |
| 152 | + { columns: ['tags'], value: 'deep', weight: 3 }, |
| 153 | + { columns: ['tags'], value: 'cute', weight: 2 }, |
| 154 | + ] |
| 155 | + } |
| 156 | + }, |
| 157 | + tagsQ: { |
| 158 | + column: 'tags', |
| 159 | + include: ['cute', 'deep'], |
| 160 | + }, |
| 161 | + }), |
| 162 | + ); |
| 163 | + |
| 164 | + console.log('\n✅ All tag tests done.\n'); |
126 | 165 | console.log('\n✅ Done.\n'); |
127 | 166 | })(); |
0 commit comments