|
| 1 | +// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * Array/object-valued field types must be stored as JSON columns and |
| 5 | + * round-tripped as arrays/objects. Regression: `multiselect`/`checkboxes`/ |
| 6 | + * `tags`/`repeater`/`vector` were absent from the driver's JSON-field |
| 7 | + * classification, so their array values reached the better-sqlite3 binder |
| 8 | + * un-serialized and crashed with "SQLite3 can only bind numbers, strings, |
| 9 | + * bigints, buffers, and null" — breaking common field types on every write |
| 10 | + * (found driving the showcase field-zoo, which had no seed data to surface it). |
| 11 | + * |
| 12 | + * The classification (`isJsonField`) and the DDL column-type switch now share |
| 13 | + * one `JSON_COLUMN_TYPES` source, plus a `formatInput` safety net stringifies |
| 14 | + * any stray array/object so an unclassified field degrades to a stored string |
| 15 | + * instead of a 500. |
| 16 | + */ |
| 17 | + |
| 18 | +import { describe, it, expect, beforeEach, afterEach } from 'vitest'; |
| 19 | +import { SqlDriver } from '../src/index.js'; |
| 20 | + |
| 21 | +describe('SqlDriver array/object field persistence', () => { |
| 22 | + let driver: SqlDriver; |
| 23 | + |
| 24 | + beforeEach(async () => { |
| 25 | + driver = new SqlDriver({ |
| 26 | + client: 'better-sqlite3', |
| 27 | + connection: { filename: ':memory:' }, |
| 28 | + useNullAsDefault: true, |
| 29 | + }); |
| 30 | + await driver.initObjects([ |
| 31 | + { |
| 32 | + name: 'zoo', |
| 33 | + fields: { |
| 34 | + name: { type: 'string' }, |
| 35 | + tags: { type: 'tags' }, |
| 36 | + ms: { type: 'multiselect' }, |
| 37 | + cbs: { type: 'checkboxes' }, |
| 38 | + rep: { type: 'repeater' }, |
| 39 | + vec: { type: 'vector' }, |
| 40 | + comp: { type: 'composite' }, |
| 41 | + loc: { type: 'location' }, |
| 42 | + }, |
| 43 | + }, |
| 44 | + ]); |
| 45 | + }); |
| 46 | + |
| 47 | + afterEach(async () => { |
| 48 | + await driver.disconnect(); |
| 49 | + }); |
| 50 | + |
| 51 | + it('persists and round-trips array-valued fields as arrays', async () => { |
| 52 | + await driver.create( |
| 53 | + 'zoo', |
| 54 | + { |
| 55 | + id: 'z1', name: 'Specimen', |
| 56 | + tags: ['x', 'y'], |
| 57 | + ms: ['red', 'green'], |
| 58 | + cbs: ['email', 'push'], |
| 59 | + rep: [{ a: 1 }, { a: 2 }], |
| 60 | + vec: [0.1, 0.2, 0.3], |
| 61 | + comp: { k: 'v' }, |
| 62 | + loc: { lat: 1.5, lng: 2.5 }, |
| 63 | + }, |
| 64 | + { bypassTenantAudit: true }, |
| 65 | + ); |
| 66 | + const row = await driver.findOne('zoo', 'z1', { bypassTenantAudit: true }); |
| 67 | + expect(row.tags).toEqual(['x', 'y']); |
| 68 | + expect(row.ms).toEqual(['red', 'green']); |
| 69 | + expect(row.cbs).toEqual(['email', 'push']); |
| 70 | + expect(row.rep).toEqual([{ a: 1 }, { a: 2 }]); |
| 71 | + expect(row.vec).toEqual([0.1, 0.2, 0.3]); |
| 72 | + expect(row.comp).toEqual({ k: 'v' }); |
| 73 | + expect(row.loc).toEqual({ lat: 1.5, lng: 2.5 }); |
| 74 | + }); |
| 75 | + |
| 76 | + it('updates an array field to a new array', async () => { |
| 77 | + await driver.create('zoo', { id: 'z2', name: 'B', tags: ['a'] }, { bypassTenantAudit: true }); |
| 78 | + await driver.update('zoo', 'z2', { tags: ['a', 'b', 'c'] }, { bypassTenantAudit: true }); |
| 79 | + const row = await driver.findOne('zoo', 'z2', { bypassTenantAudit: true }); |
| 80 | + expect(row.tags).toEqual(['a', 'b', 'c']); |
| 81 | + }); |
| 82 | + |
| 83 | + it('does not crash on an empty array', async () => { |
| 84 | + await driver.create('zoo', { id: 'z3', name: 'C', ms: [] }, { bypassTenantAudit: true }); |
| 85 | + const row = await driver.findOne('zoo', 'z3', { bypassTenantAudit: true }); |
| 86 | + expect(row.ms).toEqual([]); |
| 87 | + }); |
| 88 | +}); |
0 commit comments