-
-
Notifications
You must be signed in to change notification settings - Fork 79
Expand file tree
/
Copy pathqueries.ts
More file actions
825 lines (704 loc) · 23.2 KB
/
queries.ts
File metadata and controls
825 lines (704 loc) · 23.2 KB
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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
import {chance} from './utils';
import {
isLibsql,
open,
// openRemote,
// openSync,
type DB,
type SQLBatchTuple,
} from '@op-engineering/op-sqlite';
import {
expect,
afterEach,
beforeEach,
describe,
it,
} from '@op-engineering/op-test';
// import pkg from '../../package.json'
describe('Queries tests', () => {
let db: DB;
beforeEach(async () => {
db = open({
name: 'queries.sqlite',
encryptionKey: 'test',
});
await db.execute('DROP TABLE IF EXISTS User;');
await db.execute('DROP TABLE IF EXISTS T1;');
await db.execute('DROP TABLE IF EXISTS T2;');
await db.execute(
'CREATE TABLE User (id INT PRIMARY KEY, name TEXT NOT NULL, age INT, networth REAL, nickname TEXT) STRICT;',
);
});
afterEach(() => {
if (db) {
db.delete();
// @ts-ignore
db = null;
}
});
if (isLibsql()) {
// itOnly('Remote open a turso database', async () => {
// const remoteDb = openRemote({
// url: 'libsql://foo-ospfranco.turso.io',
// authToken:
// 'eyJhbGciOiJFZERTQSIsInR5cCI6IkpXVCJ9.eyJhIjoicnciLCJpYXQiOjE3MTY5NTc5OTUsImlkIjoiZmJkNzZmMjYtZTliYy00MGJiLTlmYmYtMDczZjFmMjdjOGY4In0.U3cAWBOvcdiqoPN3MB81sco7x8CGOjjtZ1ZEf30uo2iPcAmOuJzcnAznmDlZ6SpQd4qzuJxE4mAIoRlOkpzgBQ',
// });
// console.log('Running select 1');
// const res = await remoteDb.execute('SELECT 1');
// console.log('after select 1;');
// expect(res.rowsAffected).toEqual(0);
// });
// it('Open a libsql database replicated to turso', async () => {
// const remoteDb = openSync({
// url: 'libsql://foo-ospfranco.turso.io',
// authToken:
// 'eyJhbGciOiJFZERTQSIsInR5cCI6IkpXVCJ9.eyJhIjoicnciLCJpYXQiOjE3MTY5NTc5OTUsImlkIjoiZmJkNzZmMjYtZTliYy00MGJiLTlmYmYtMDczZjFmMjdjOGY4In0.U3cAWBOvcdiqoPN3MB81sco7x8CGOjjtZ1ZEf30uo2iPcAmOuJzcnAznmDlZ6SpQd4qzuJxE4mAIoRlOkpzgBQ',
// name: 'my replica',
// libsqlSyncInterval: 1000,
// encryptionKey: 'blah',
// });
// const res = await remoteDb.execute('SELECT 1');
// remoteDb.sync();
// expect(res.rowsAffected).toEqual(0);
// });
}
it('Can create multiple connections to same db', async () => {
const db2 = open({
name: 'queries.sqlite',
encryptionKey: 'test',
});
const db3 = open({
name: 'queries.sqlite',
encryptionKey: 'test',
});
let promises = [
db.execute('SELECT 1'),
db2.execute('SELECT 1'),
db3.execute('SELECT 1'),
];
let res = await Promise.all(promises);
res.forEach(r => {
expect(r.rowsAffected).toEqual(0);
expect(r.rows[0]!['1']).toEqual(1);
});
});
it('Trying to pass object as param should throw', async () => {
try {
// @ts-ignore
await db.execute('SELECT ?', [{foo: 'bar'}]);
} catch (e: any) {
expect(
e.message.includes(
'Exception in HostFunction: Object is not an ArrayBuffer, cannot bind to SQLite',
),
).toEqual(true);
}
});
it('executeSync', () => {
const res = db.executeSync('SELECT 1');
expect(res.rowsAffected).toEqual(0);
const id = chance.integer();
const name = chance.name();
const age = chance.integer();
const networth = chance.floating();
const res2 = db.executeSync(
'INSERT INTO "User" (id, name, age, networth) VALUES(?, ?, ?, ?)',
[id, name, age, networth],
);
expect(res2.rowsAffected).toEqual(1);
expect(res2.insertId).toEqual(1);
// expect(res2.rows).toBe([]);
expect(res2.rows?.length).toEqual(0);
});
it('Insert', async () => {
const id = chance.integer();
const name = chance.name();
const age = chance.integer();
const networth = chance.floating();
const res = await db.execute(
'INSERT INTO "User" (id, name, age, networth) VALUES(?, ?, ?, ?)',
[id, name, age, networth],
);
expect(res.rowsAffected).toEqual(1);
expect(res.insertId).toEqual(1);
// expect(res.metadata).toEqual([]);
expect(res.rows).toDeepEqual([]);
expect(res.rows?.length).toEqual(0);
});
it('Casts booleans to ints correctly', async () => {
await db.execute(`SELECT ?`, [1]);
await db.execute(`SELECT ?`, [true]);
});
it('Insert and query with host objects', async () => {
const id = chance.integer();
const name = chance.name();
const age = chance.integer();
const networth = chance.floating();
const res = await db.executeWithHostObjects(
'INSERT INTO "User" (id, name, age, networth) VALUES(?, ?, ?, ?)',
[id, name, age, networth],
);
expect(res.rowsAffected).toEqual(1);
expect(res.insertId).toEqual(1);
// expect(res.metadata).toEqual([]);
expect(res.rows).toDeepEqual([]);
expect(res.rows?.length).toEqual(0);
const queryRes = await db.executeWithHostObjects('SELECT * FROM User');
expect(queryRes.rowsAffected).toEqual(1);
expect(queryRes.insertId).toEqual(1);
expect(queryRes.rows).toDeepEqual([
{
id,
name,
age,
networth,
nickname: null,
},
]);
});
it('Query without params', async () => {
const id = chance.integer();
const name = chance.name();
const age = chance.integer();
const networth = chance.floating();
await db.execute(
'INSERT INTO User (id, name, age, networth) VALUES(?, ?, ?, ?)',
[id, name, age, networth],
);
const res = await db.execute('SELECT * FROM User');
expect(res.rowsAffected).toEqual(1);
expect(res.insertId).toEqual(1);
expect(res.rows).toDeepEqual([
{
id,
name,
age,
networth,
nickname: null,
},
]);
});
it('Query with params', async () => {
const id = chance.integer();
const name = chance.name();
const age = chance.integer();
const networth = chance.floating();
await db.execute(
'INSERT INTO User (id, name, age, networth) VALUES(?, ?, ?, ?)',
[id, name, age, networth],
);
const res = await db.execute('SELECT * FROM User WHERE id = ?', [id]);
expect(res.rowsAffected).toEqual(1);
expect(res.insertId).toEqual(1);
expect(res.rows).toDeepEqual([
{
id,
name,
age,
networth,
nickname: null,
},
]);
});
it('Query with sqlite functions', async () => {
const id = chance.integer();
const name = chance.name();
const age = chance.integer();
const networth = chance.floating();
// COUNT(*)
await db.execute(
'INSERT INTO User (id, name, age, networth) VALUES(?, ?, ?, ?)',
[id, name, age, networth],
);
const countRes = await db.execute('SELECT COUNT(*) as count FROM User');
expect(countRes.rows?.length).toEqual(1);
expect(countRes.rows?.[0]?.count).toEqual(1);
// SUM(age)
const id2 = chance.integer();
const name2 = chance.name();
const age2 = chance.integer();
const networth2 = chance.floating();
await db.execute(
'INSERT INTO User (id, name, age, networth) VALUES(?, ?, ?, ?)',
[id2, name2, age2, networth2],
);
const sumRes = await db.execute('SELECT SUM(age) as sum FROM User;');
expect(sumRes.rows[0]!.sum).toEqual(age + age2);
const maxRes = await db.execute('SELECT MAX(networth) as `max` FROM User;');
const minRes = await db.execute('SELECT MIN(networth) as `min` FROM User;');
const maxNetworth = Math.max(networth, networth2);
const minNetworth = Math.min(networth, networth2);
expect(maxRes.rows[0]!.max).toEqual(maxNetworth);
expect(minRes.rows[0]!.min).toEqual(minNetworth);
});
it('Executes all the statements in a single string', async () => {
if (isLibsql()) {
return;
}
await db.execute(
`CREATE TABLE T1 ( id INT PRIMARY KEY) STRICT;
CREATE TABLE T2 ( id INT PRIMARY KEY) STRICT;`,
);
let t1name = await db.execute(
"SELECT name FROM sqlite_master WHERE type='table' AND name='T1';",
);
expect(t1name.rows[0]!.name).toEqual('T1');
let t2name = await db.execute(
"SELECT name FROM sqlite_master WHERE type='table' AND name='T2';",
);
expect(t2name.rows[0]!.name).toEqual('T2');
});
it('Failed insert', async () => {
const id = chance.string();
const name = chance.name();
const age = chance.string();
const networth = chance.string();
try {
await db.execute(
'INSERT INTO User (id, name, age, networth) VALUES(?, ?, ?, ?)',
[id, name, age, networth],
);
} catch (e: any) {
expect(typeof e).toEqual('object');
expect(
e.message.includes('cannot store TEXT value in INT column User.id'),
).toEqual(true);
}
});
it('Transaction, auto commit', async () => {
const id = chance.integer();
const name = chance.name();
const age = chance.integer();
const networth = chance.floating();
await db.transaction(async tx => {
const res = await tx.execute(
'INSERT INTO "User" (id, name, age, networth) VALUES(?, ?, ?, ?)',
[id, name, age, networth],
);
expect(res.rowsAffected).toEqual(1);
expect(res.insertId).toEqual(1);
// expect(res.metadata).toEqual([]);
expect(res.rows).toDeepEqual([]);
expect(res.rows?.length).toEqual(0);
});
const res = await db.execute('SELECT * FROM User');
expect(res.rows).toDeepEqual([
{
id,
name,
age,
networth,
nickname: null,
},
]);
});
it('Transaction, manual commit', async () => {
const id = chance.integer();
const name = chance.name();
const age = chance.integer();
const networth = chance.floating();
await db.transaction(async tx => {
const res = await tx.execute(
'INSERT INTO "User" (id, name, age, networth) VALUES(?, ?, ?, ?)',
[id, name, age, networth],
);
expect(res.rowsAffected).toEqual(1);
expect(res.insertId).toEqual(1);
expect(res.rows).toDeepEqual([]);
expect(res.rows?.length).toEqual(0);
await tx.commit();
});
const res = await db.execute('SELECT * FROM User');
// console.log(res);
expect(res.rows).toDeepEqual([
{
id,
name,
age,
networth,
nickname: null,
},
]);
});
it('Transaction, executed in order', async () => {
const xs = 10;
const actual: unknown[] = [];
// ARRANGE: Generate expected data
const id = chance.integer();
const name = chance.name();
const age = chance.integer();
// ACT: Start multiple transactions to upsert and select the same record
const promises = [];
for (let i = 1; i <= xs; i++) {
const promised = db.transaction(async tx => {
// ACT: Upsert statement to create record / increment the value
await tx.execute(
`
INSERT OR REPLACE INTO [User] ([id], [name], [age], [networth])
SELECT ?, ?, ?,
IFNULL((
SELECT [networth] + 1000
FROM [User]
WHERE [id] = ?
), 0)
`,
[id, name, age, id],
);
// ACT: Select statement to get incremented value and store it for checking later
const results = await tx.execute(
'SELECT [networth] FROM [User] WHERE [id] = ?',
[id],
);
actual.push(results.rows[0]!.networth);
});
promises.push(promised);
}
// ACT: Wait for all transactions to complete
await Promise.all(promises);
// ASSERT: That the expected values where returned
const expected = Array(xs)
.fill(0)
.map((_, index) => index * 1000);
expect(actual).toDeepEqual(expected);
});
it('Transaction, cannot execute after commit', async () => {
const id = chance.integer();
const name = chance.name();
const age = chance.integer();
const networth = chance.floating();
await db.transaction(async tx => {
const res = await tx.execute(
'INSERT INTO "User" (id, name, age, networth) VALUES(?, ?, ?, ?)',
[id, name, age, networth],
);
expect(res.rowsAffected).toEqual(1);
expect(res.insertId).toEqual(1);
// expect(res.metadata).toEqual([]);
expect(res.rows).toDeepEqual([]);
expect(res.rows.length).toEqual(0);
await tx.commit();
try {
await tx.execute('SELECT * FROM "User"');
} catch (e) {
expect(!!e).toEqual(true);
}
});
const res = await db.execute('SELECT * FROM User');
expect(res.rows).toDeepEqual([
{
id,
name,
age,
networth,
nickname: null,
},
]);
});
it('Incorrect transaction, manual rollback', async () => {
const id = chance.string();
const name = chance.name();
const age = chance.integer();
const networth = chance.floating();
await db.transaction(async tx => {
try {
await tx.execute(
'INSERT INTO "User" (id, name, age, networth) VALUES(?, ?, ?, ?)',
[id, name, age, networth],
);
} catch (e) {
await tx.rollback();
}
});
const res = await db.execute('SELECT * FROM User');
expect(res.rows).toDeepEqual([]);
});
it('Correctly throws', async () => {
const id = chance.string();
const name = chance.name();
const age = chance.integer();
const networth = chance.floating();
try {
await db.execute(
'INSERT INTO "User" (id, name, age, networth) VALUES(?, ?, ?, ?)',
[id, name, age, networth],
);
} catch (e: any) {
expect(!!e).toEqual(true);
}
});
it('Rollback', async () => {
const id = chance.integer();
const name = chance.name();
const age = chance.integer();
const networth = chance.floating();
await db.transaction(async tx => {
await tx.execute(
'INSERT INTO "User" (id, name, age, networth) VALUES(?, ?, ?, ?)',
[id, name, age, networth],
);
await tx.rollback();
const res = await db.execute('SELECT * FROM User');
expect(res.rows).toDeepEqual([]);
});
});
it('Execute raw sync should return just an array of objects', async () => {
const id = chance.integer();
const name = chance.name();
const age = chance.integer();
const networth = chance.floating();
await db.execute(
'INSERT INTO User (id, name, age, networth) VALUES(?, ?, ?, ?)',
[id, name, age, networth],
);
const res = db.executeRawSync('SELECT id, name, age, networth FROM User');
expect(res).toDeepEqual([[id, name, age, networth]]);
});
it('Transaction, rejects on callback error', async () => {
const promised = db.transaction(() => {
throw new Error('Error from callback');
});
// ASSERT: should return a promise that eventually rejects
expect(typeof promised == 'object');
try {
await promised;
// expect.fail('Should not resolve');
} catch (e) {
// expect(e).to.be.a.instanceof(Error);
expect((e as Error)?.message).toEqual('Error from callback');
}
});
it('Transaction, rejects on invalid query', async () => {
const promised = db.transaction(async tx => {
await tx.execute('SELECT * FROM [tableThatDoesNotExist];');
});
// ASSERT: should return a promise that eventually rejects
// expect(promised).to.have.property('then').that.is.a('function');
try {
await promised;
// expect.fail('Should not resolve');
} catch (e) {
// expect(e).to.be.a.instanceof(Error);
expect(
(e as Error)?.message.includes('no such table: tableThatDoesNotExist'),
).toBe(true);
}
});
it('Transaction, handle async callback', async () => {
let ranCallback = false;
const promised = db.transaction(async tx => {
await new Promise<void>(done => {
setTimeout(() => done(), 50);
});
tx.execute('SELECT * FROM User;');
ranCallback = true;
});
// ASSERT: should return a promise that eventually rejects
// expect(promised).to.have.property('then').that.is.a('function');
await promised;
expect(ranCallback).toEqual(true);
});
it('executeBatch', async () => {
const id1 = chance.integer();
const name1 = chance.name();
const age1 = chance.integer();
const networth1 = chance.floating();
const id2 = chance.integer();
const name2 = chance.name();
const age2 = chance.integer();
const networth2 = chance.floating();
const commands: SQLBatchTuple[] = [
['SELECT * FROM "User"', []],
['SELECT * FROM "User"'],
[
'INSERT INTO "User" (id, name, age, networth) VALUES(?, ?, ?, ?)',
[id1, name1, age1, networth1],
],
[
'INSERT INTO "User" (id, name, age, networth) VALUES(?, ?, ?, ?)',
[[id2, name2, age2, networth2]],
],
];
await db.executeBatch(commands);
const res = await db.execute('SELECT * FROM User');
expect(res.rows).toDeepEqual([
{id: id1, name: name1, age: age1, networth: networth1, nickname: null},
{
id: id2,
name: name2,
age: age2,
networth: networth2,
nickname: null,
},
]);
});
it('Batch execute with BLOB', async () => {
let db = open({
name: 'queries.sqlite',
encryptionKey: 'test',
});
await db.execute('DROP TABLE IF EXISTS User;');
await db.execute(
'CREATE TABLE IF NOT EXISTS User (id TEXT PRIMARY KEY NOT NULL, name TEXT NOT NULL, age INT, networth BLOB, nickname TEXT) STRICT;',
);
const id1 = '1';
const name1 = 'name1';
const age1 = 12;
const networth1 = new Uint8Array([1, 2, 3]);
const id2 = '2';
const name2 = 'name2';
const age2 = 17;
const networth2 = new Uint8Array([3, 2, 1]);
const commands: SQLBatchTuple[] = [
[
'INSERT OR REPLACE INTO "User" (id, name, age, networth) VALUES(?, ?, ?, ?)',
[id1, name1, age1, networth1],
],
[
'INSERT OR REPLACE INTO "User" (id, name, age, networth) VALUES(?, ?, ?, ?)',
[[id2, name2, age2, networth2]],
],
];
// bomb~ (NOBRIDGE) ERROR Error: Exception in HostFunction: <unknown>
await db.executeBatch(commands);
});
it('DumbHostObject allows to write known props', async () => {
const id = chance.integer();
const name = chance.name();
const age = chance.integer();
const networth = chance.floating();
await db.execute(
'INSERT INTO User (id, name, age, networth) VALUES(?, ?, ?, ?)',
[id, name, age, networth],
);
const res = await db.executeWithHostObjects('SELECT * FROM User');
expect(res.insertId).toEqual(1);
expect(res.rows).toDeepEqual([
{
id,
name,
age,
networth,
nickname: null,
},
]);
res.rows[0]!.name = 'quack_changed';
expect(res.rows[0]!.name).toEqual('quack_changed');
});
it('DumbHostObject allows to write new props', async () => {
const id = chance.integer();
const name = chance.name();
const age = chance.integer();
const networth = chance.floating();
await db.execute(
'INSERT INTO User (id, name, age, networth) VALUES(?, ?, ?, ?)',
[id, name, age, networth],
);
const res = await db.executeWithHostObjects('SELECT * FROM User');
expect(res.rowsAffected).toEqual(1);
expect(res.insertId).toEqual(1);
expect(res.rows!).toDeepEqual([
{
id,
name,
age,
networth,
nickname: null,
},
]);
res.rows[0]!.myWeirdProp = 'quack_changed';
expect(res.rows[0]!.myWeirdProp).toEqual('quack_changed');
});
it('Execute raw should return just an array of objects', async () => {
const id = chance.integer();
const name = chance.name();
const age = chance.integer();
const networth = chance.floating();
await db.execute(
'INSERT INTO User (id, name, age, networth) VALUES(?, ?, ?, ?)',
[id, name, age, networth],
);
const res = await db.executeRaw('SELECT id, name, age, networth FROM User');
expect(res).toDeepEqual([[id, name, age, networth]]);
});
it('Create fts5 virtual table', async () => {
await db.execute(
'CREATE VIRTUAL TABLE fts5_table USING fts5(name, content);',
);
await db.execute('INSERT INTO fts5_table (name, content) VALUES(?, ?)', [
'test',
'test content',
]);
const res = await db.execute('SELECT * FROM fts5_table');
expect(res.rows).toDeepEqual([{name: 'test', content: 'test content'}]);
});
it('Various queries', async () => {
await db.execute('SELECT 1 ');
await db.execute('SELECT 1 ');
await db.execute('SELECT 1; ', []);
await db.execute('SELECT ?; ', [1]);
});
it('Handles concurrent transactions correctly', async () => {
const id = chance.integer();
const name = chance.name();
const age = chance.integer();
const networth = chance.floating();
const transaction1 = db.transaction(async tx => {
await tx.execute(
'INSERT INTO "User" (id, name, age, networth) VALUES(?, ?, ?, ?)',
[id, name, age, networth],
);
});
const transaction2 = db.transaction(async tx => {
await tx.execute(
'INSERT INTO "User" (id, name, age, networth) VALUES(?, ?, ?, ?)',
[id + 1, name, age, networth],
);
});
await Promise.all([transaction1, transaction2]);
const res = await db.execute('SELECT * FROM User');
expect(res.rows.length).toEqual(2);
});
it('Pragma user_version', () => {
const res = db.executeSync('PRAGMA user_version');
expect(res.rows).toDeepEqual([{user_version: 0}]);
});
// const sqliteVecEnabled = pkg?.['op-sqlite']?.sqliteVec === true;
// if (sqliteVecEnabled) {
// it('sqlite-vec extension: vector similarity search', async () => {
// // Create a virtual table for storing vectors
// await db.execute(`
// CREATE VIRTUAL TABLE vec_items USING vec0(
// embedding FLOAT[8]
// )
// `);
// // Insert some sample vectors
// await db.execute(`
// INSERT INTO vec_items(rowid, embedding)
// VALUES
// (1, '[-0.200, 0.250, 0.341, -0.211, 0.645, 0.935, -0.316, -0.924]'),
// (2, '[0.443, -0.501, 0.355, -0.771, 0.707, -0.708, -0.185, 0.362]'),
// (3, '[0.716, -0.927, 0.134, 0.052, -0.669, 0.793, -0.634, -0.162]'),
// (4, '[-0.710, 0.330, 0.656, 0.041, -0.990, 0.726, 0.385, -0.958]')
// `);
// // Perform KNN query to find the 2 nearest neighbors
// const queryVector = '[0.890, 0.544, 0.825, 0.961, 0.358, 0.0196, 0.521, 0.175]';
// const result = await db.execute(`
// SELECT rowid, distance
// FROM vec_items
// WHERE embedding MATCH ?
// ORDER BY distance
// LIMIT 2
// `, [queryVector]);
// // Verify results
// expect(result.rows.length).toEqual(2);
// expect(result.rows[0]!.rowid).toEqual(2);
// expect(result.rows[1]!.rowid).toEqual(1);
// // Verify distances are positive numbers
// const distance0 = result.rows[0]!.distance as number;
// const distance1 = result.rows[1]!.distance as number;
// expect(typeof distance0).toEqual('number');
// expect(distance0 > 0).toBeTruthy();
// expect(distance1 > 0).toBeTruthy();
// });
// }
});