json-rest-api 1.0.21 → 1.0.23

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 (88) hide show
  1. package/CLAUDE.md +1 -2
  2. package/JSKIT_BACKEND_MIGRATION_TRACKER.txt +180 -0
  3. package/SINGLE_VALIDATION_CONTRACT_TODO.txt +172 -0
  4. package/docs/API.md +51 -14
  5. package/docs/COMPARISON.md +2 -2
  6. package/docs/GUIDE/GUIDE_1_Initial_Setup.md +28 -1
  7. package/docs/GUIDE/GUIDE_2_2_Manipulating_And_Searching_Tables.md +5 -1
  8. package/docs/GUIDE/GUIDE_2_7_Pagination_And_Ordering.md +3 -1
  9. package/docs/GUIDE/GUIDE_2_8_Effects_of_PUT_and_PATCH.md +12 -60
  10. package/docs/GUIDE/GUIDE_2_Data_And_Relations.md +2 -2
  11. package/docs/GUIDE/GUIDE_3_Field_Transformations.md +5 -1
  12. package/docs/GUIDE/GUIDE_7_Hooks_Data_Management_And_Plugins.md +2 -0
  13. package/docs/GUIDE/GUIDE_X_Autofiltering.md +355 -0
  14. package/docs/GUIDE/GUIDE_X_Fastify.md +78 -0
  15. package/docs/GUIDE/GUIDE_X_Knex_Schema_And_Migrations.md +250 -0
  16. package/docs/GUIDE/GUIDE_X_Query_Projections.md +159 -0
  17. package/docs/GUIDE/GUIDE_X_Writing_Plugins.md +199 -0
  18. package/docs/GUIDE/index.md +18 -9
  19. package/docs/QUICKSTART.md +5 -1
  20. package/docs/index.md +1 -1
  21. package/index.js +3 -2
  22. package/package.json +4 -4
  23. package/plugins/core/connectors/express-plugin.js +97 -184
  24. package/plugins/core/connectors/fastify-plugin.js +206 -0
  25. package/plugins/core/connectors/lib/connector-core.js +195 -0
  26. package/plugins/core/connectors/lib/transport-http-helpers.js +122 -0
  27. package/plugins/core/connectors/lib/transport-route-schemas.js +92 -0
  28. package/plugins/core/lib/dbIntrospection.js +1078 -0
  29. package/plugins/core/lib/dbTablesOperations.js +1104 -160
  30. package/plugins/core/lib/querying/knex-json-api-transformers-querying.js +5 -2
  31. package/plugins/core/lib/querying/knex-process-includes.js +3 -4
  32. package/plugins/core/lib/querying/knex-query-helpers.js +17 -71
  33. package/plugins/core/lib/querying/knex-relationship-includes.js +466 -206
  34. package/plugins/core/lib/querying/query-builder-utils.js +19 -0
  35. package/plugins/core/lib/querying/query-field-sort-helpers.js +135 -0
  36. package/plugins/core/lib/querying/sort-helpers.js +33 -0
  37. package/plugins/core/lib/querying/storage-adapter-utils.js +51 -0
  38. package/plugins/core/lib/querying/url-helpers.js +8 -2
  39. package/plugins/core/lib/querying-writing/compile-schemas.js +12 -1
  40. package/plugins/core/lib/querying-writing/knex-field-helpers.js +83 -13
  41. package/plugins/core/lib/querying-writing/knex-json-api-transformers.js +13 -12
  42. package/plugins/core/lib/querying-writing/query-field-helpers.js +88 -0
  43. package/plugins/core/lib/querying-writing/request-contracts.js +590 -0
  44. package/plugins/core/lib/storage/canonical-storage-mapping.js +113 -0
  45. package/plugins/core/lib/storage/storage-adapter.js +79 -2
  46. package/plugins/core/lib/storage/storage-mapping.js +204 -0
  47. package/plugins/core/rest-api-anyapi-knex-plugin.js +355 -228
  48. package/plugins/core/rest-api-autofilter-plugin.js +414 -0
  49. package/plugins/core/rest-api-knex-plugin.js +249 -211
  50. package/plugins/core/rest-api-plugin-hooks/register-relationship-routes.js +10 -5
  51. package/plugins/core/rest-api-plugin-hooks/register-scope-routes.js +12 -6
  52. package/plugins/core/rest-api-plugin-methods/apply-query-filters.js +41 -0
  53. package/plugins/core/rest-api-plugin-methods/common.js +121 -83
  54. package/plugins/core/rest-api-plugin-methods/enrich-attributes.js +6 -1
  55. package/plugins/core/rest-api-plugin-methods/get.js +23 -14
  56. package/plugins/core/rest-api-plugin-methods/patch.js +28 -12
  57. package/plugins/core/rest-api-plugin-methods/post.js +13 -8
  58. package/plugins/core/rest-api-plugin-methods/put.js +45 -21
  59. package/plugins/core/rest-api-plugin-methods/query.js +15 -53
  60. package/plugins/core/rest-api-plugin.js +2 -0
  61. package/plugins/core/rest-api-positioning-plugin.js +1 -1
  62. package/plugins/core/rest-api-query-projections-plugin.js +130 -0
  63. package/plugins/core/socketio-plugin.js +1 -3
  64. package/tests/autofilter.test.js +463 -0
  65. package/tests/custom-id-simplified-post.test.js +75 -0
  66. package/tests/custom-idproperty-relationships.test.js +33 -0
  67. package/tests/db-introspection.test.js +518 -0
  68. package/tests/db-table-operations.test.js +781 -0
  69. package/tests/fastify-plugin.test.js +141 -0
  70. package/tests/field-getters.test.js +1 -2
  71. package/tests/field-setters.test.js +0 -4
  72. package/tests/fixtures/api-configs.js +188 -219
  73. package/tests/helpers/fake-fastify.js +99 -0
  74. package/tests/http-connectors-parity.test.js +235 -0
  75. package/tests/projected-fields.test.js +319 -0
  76. package/tests/relationships.test.js +3 -3
  77. package/tests/searchschema-knex-behavior.test.js +187 -0
  78. package/tests/searchschema-merge.test.js +21 -69
  79. package/tests/storage-mapping.test.js +210 -0
  80. package/docs/GUIDE/GUIDE_4_Authentication_And_Permissions.md +0 -707
  81. package/docs/GUIDE/GUIDE_X_Multihome.md +0 -666
  82. package/plugins/core/lib/querying-writing/payload-validators.js +0 -1314
  83. package/plugins/core/rest-api-access.js +0 -459
  84. package/plugins/core/rest-api-multihome-plugin.js +0 -314
  85. package/tests/multihome.test.js +0 -553
  86. package/tests/multihome.test.js.bak +0 -550
  87. package/tests/ownership-optout.test.js +0 -76
  88. package/tests/rest-api-access.test.js +0 -206
@@ -0,0 +1,781 @@
1
+ import { describe, it, before, after, beforeEach, afterEach } from 'node:test'
2
+ import assert from 'node:assert/strict'
3
+ import knexLib from 'knex'
4
+ import { Api } from 'hooked-api'
5
+ import { RestApiPlugin, RestApiKnexPlugin } from '../index.js'
6
+ import {
7
+ createKnexTable,
8
+ addKnexFields,
9
+ alterKnexFields,
10
+ generateKnexMigration,
11
+ generateKnexMigrationDiff
12
+ } from '../plugins/core/lib/dbTablesOperations.js'
13
+ import { introspectKnexTableSnapshot } from '../plugins/core/lib/dbIntrospection.js'
14
+
15
+ function makeSchemaInfo (tableName, idProperty = 'id') {
16
+ return { tableName, idProperty }
17
+ }
18
+
19
+ function makeTableSchema (structure, metadata = {}) {
20
+ return { structure, ...metadata }
21
+ }
22
+
23
+ let db
24
+
25
+ describe('dbTablesOperations.createKnexTable', () => {
26
+ beforeEach(() => {
27
+ db = knexLib({
28
+ client: 'better-sqlite3',
29
+ connection: {
30
+ filename: ':memory:'
31
+ },
32
+ useNullAsDefault: true
33
+ })
34
+ })
35
+
36
+ afterEach(async () => {
37
+ await db.destroy()
38
+ })
39
+
40
+ it('creates a simple table with string fields', async () => {
41
+ const schema = makeTableSchema({
42
+ name: { type: 'string', required: true, maxLength: 100 },
43
+ email: { type: 'string', unique: true },
44
+ bio: { type: 'string', nullable: true }
45
+ })
46
+
47
+ await createKnexTable(db, makeSchemaInfo('users'), schema)
48
+
49
+ const exists = await db.schema.hasTable('users')
50
+ assert.equal(exists, true)
51
+
52
+ const info = await db('users').columnInfo()
53
+ assert.ok(info.id)
54
+ assert.ok(info.name)
55
+ assert.ok(info.email)
56
+ assert.ok(info.bio)
57
+
58
+ await db('users').insert({
59
+ name: 'Test User',
60
+ email: 'test@example.com',
61
+ bio: null
62
+ })
63
+
64
+ const users = await db('users').select()
65
+ assert.equal(users.length, 1)
66
+ assert.equal(users[0].name, 'Test User')
67
+ })
68
+
69
+ it('handles number fields with precision and defaults', async () => {
70
+ const schema = makeTableSchema({
71
+ price: { type: 'number', precision: 10, scale: 2 },
72
+ quantity: { type: 'number', defaultTo: 0 },
73
+ rating: { type: 'number', nullable: true }
74
+ })
75
+
76
+ await createKnexTable(db, makeSchemaInfo('products'), schema)
77
+
78
+ await db('products').insert({
79
+ price: 99.99,
80
+ rating: 4.5
81
+ })
82
+
83
+ const products = await db('products').select()
84
+ assert.equal(products[0].price, 99.99)
85
+ assert.equal(products[0].quantity, 0)
86
+ assert.equal(products[0].rating, 4.5)
87
+ })
88
+
89
+ it('adds timestamps when requested', async () => {
90
+ const schema = makeTableSchema({
91
+ title: { type: 'string' }
92
+ })
93
+
94
+ await createKnexTable(db, makeSchemaInfo('posts'), schema, {
95
+ timestamps: true
96
+ })
97
+
98
+ const info = await db('posts').columnInfo()
99
+ assert.ok(info.created_at)
100
+ assert.ok(info.updated_at)
101
+ })
102
+
103
+ it('handles foreign key references', async () => {
104
+ const userSchema = makeTableSchema({
105
+ username: { type: 'string', unique: true }
106
+ })
107
+ await createKnexTable(db, makeSchemaInfo('users'), userSchema)
108
+
109
+ const postSchema = makeTableSchema({
110
+ title: { type: 'string', required: true },
111
+ userId: {
112
+ type: 'id',
113
+ required: true,
114
+ references: { table: 'users', onDelete: 'CASCADE' }
115
+ }
116
+ })
117
+ await createKnexTable(db, makeSchemaInfo('posts'), postSchema)
118
+
119
+ await db('users').insert({ username: 'testuser' })
120
+ const user = await db('users').where('username', 'testuser').first()
121
+ await db('posts').insert({ title: 'Test Post', userId: user.id })
122
+
123
+ const posts = await db('posts')
124
+ .join('users', 'posts.userId', 'users.id')
125
+ .select('posts.title', 'users.username')
126
+
127
+ assert.equal(posts.length, 1)
128
+ assert.equal(posts[0].title, 'Test Post')
129
+ assert.equal(posts[0].username, 'testuser')
130
+ })
131
+
132
+ it('respects a primary key defined in the schema', async () => {
133
+ const schema = makeTableSchema({
134
+ isbn: { type: 'string', primary: true, maxLength: 13 },
135
+ title: { type: 'string', required: true }
136
+ })
137
+
138
+ await createKnexTable(db, makeSchemaInfo('books'), schema, {
139
+ autoIncrement: false
140
+ })
141
+
142
+ const info = await db('books').columnInfo()
143
+ assert.ok(!info.id)
144
+ assert.ok(info.isbn)
145
+
146
+ await db('books').insert({
147
+ isbn: '9781234567890',
148
+ title: 'Test Book'
149
+ })
150
+
151
+ const books = await db('books').select()
152
+ assert.equal(books[0].isbn, '9781234567890')
153
+ })
154
+
155
+ it('enforces unique constraints', async () => {
156
+ const schema = makeTableSchema({
157
+ email: { type: 'string', unique: true },
158
+ username: { type: 'string', unique: true }
159
+ })
160
+
161
+ await createKnexTable(db, makeSchemaInfo('accounts'), schema)
162
+
163
+ await db('accounts').insert({
164
+ email: 'test@example.com',
165
+ username: 'testuser'
166
+ })
167
+
168
+ await assert.rejects(async () => {
169
+ await db('accounts').insert({
170
+ email: 'test@example.com',
171
+ username: 'otheruser'
172
+ })
173
+ })
174
+ })
175
+
176
+ it('uses storage column overrides when creating tables', async () => {
177
+ const schema = makeTableSchema({
178
+ displayName: { type: 'string', required: true, storage: { column: 'display_name' } },
179
+ lastSeenAt: { type: 'dateTime', storage: { column: 'last_seen_at' } }
180
+ })
181
+
182
+ await createKnexTable(db, makeSchemaInfo('mapped_profiles'), schema)
183
+
184
+ const info = await db('mapped_profiles').columnInfo()
185
+ assert.ok(info.id)
186
+ assert.ok(info.display_name)
187
+ assert.ok(info.last_seen_at)
188
+ assert.equal(info.displayName, undefined)
189
+ })
190
+
191
+ it('supports logical id fields mapped to custom storage columns', async () => {
192
+ const schema = makeTableSchema({
193
+ id: { type: 'id', primary: true, storage: { column: 'book_id' } },
194
+ title: { type: 'string', required: true }
195
+ })
196
+
197
+ await createKnexTable(db, makeSchemaInfo('books', 'book_id'), schema)
198
+
199
+ const info = await db('books').columnInfo()
200
+ assert.ok(info.book_id)
201
+ assert.equal(info.id, undefined)
202
+
203
+ await db('books').insert({
204
+ book_id: 101,
205
+ title: 'Mapped ID Book'
206
+ })
207
+
208
+ const books = await db('books').select()
209
+ assert.equal(books.length, 1)
210
+ assert.equal(books[0].book_id, 101)
211
+ })
212
+
213
+ it('supports named composite indexes, foreign keys, checks, and enum columns', async () => {
214
+ await db.raw('PRAGMA foreign_keys = ON')
215
+ await db.raw(`
216
+ CREATE TABLE workspace_users (
217
+ workspace_id INTEGER NOT NULL,
218
+ user_id INTEGER NOT NULL,
219
+ PRIMARY KEY (workspace_id, user_id)
220
+ )
221
+ `)
222
+
223
+ const schema = makeTableSchema({
224
+ workspaceId: { type: 'id', required: true, storage: { column: 'workspace_id' } },
225
+ userId: { type: 'id', required: true, storage: { column: 'user_id' } },
226
+ role: { type: 'string', enum: ['owner', 'member'], required: true, defaultTo: 'member' },
227
+ noteCount: { type: 'number', defaultTo: 0, storage: { column: 'note_count' } }
228
+ }, {
229
+ indexes: [
230
+ {
231
+ name: 'uq_memberships_workspace_user',
232
+ unique: true,
233
+ columns: ['workspaceId', 'userId']
234
+ }
235
+ ],
236
+ foreignKeys: [
237
+ {
238
+ name: 'fk_memberships_workspace_user',
239
+ columns: ['workspaceId', 'userId'],
240
+ referencedTableName: 'workspace_users',
241
+ referencedColumns: ['workspace_id', 'user_id'],
242
+ deleteRule: 'CASCADE',
243
+ updateRule: 'RESTRICT'
244
+ }
245
+ ],
246
+ checkConstraints: [
247
+ {
248
+ name: 'chk_memberships_note_count_non_negative',
249
+ clause: 'note_count >= 0'
250
+ }
251
+ ]
252
+ })
253
+
254
+ await createKnexTable(db, makeSchemaInfo('memberships'), schema)
255
+
256
+ const snapshot = await introspectKnexTableSnapshot(db, {
257
+ tableName: 'memberships',
258
+ idColumn: 'id'
259
+ })
260
+
261
+ const membershipIndex = snapshot.indexes.find((index) => index.name === 'uq_memberships_workspace_user')
262
+ assert.ok(membershipIndex)
263
+ assert.equal(membershipIndex.unique, true)
264
+ assert.deepEqual(membershipIndex.columns, ['workspace_id', 'user_id'])
265
+
266
+ const membershipForeignKey = snapshot.foreignKeys.find((foreignKey) => foreignKey.name === 'fk_memberships_workspace_user')
267
+ assert.ok(membershipForeignKey)
268
+ assert.equal(membershipForeignKey.referencedTableName, 'workspace_users')
269
+ assert.equal(membershipForeignKey.deleteRule, 'CASCADE')
270
+ assert.equal(membershipForeignKey.updateRule, 'RESTRICT')
271
+ assert.deepEqual(
272
+ membershipForeignKey.columns.map((entry) => [entry.name, entry.referencedName]),
273
+ [['workspace_id', 'workspace_id'], ['user_id', 'user_id']]
274
+ )
275
+
276
+ const membershipCheck = snapshot.checkConstraints.find((constraint) => constraint.name === 'chk_memberships_note_count_non_negative')
277
+ assert.ok(membershipCheck)
278
+ assert.equal(membershipCheck.clause, 'note_count >= 0')
279
+
280
+ const roleColumn = snapshot.columns.find((column) => column.name === 'role')
281
+ assert.ok(roleColumn)
282
+ assert.deepEqual(roleColumn.enumValues, ['owner', 'member'])
283
+ })
284
+ })
285
+
286
+ describe('dbTablesOperations.generateKnexMigration', () => {
287
+ beforeEach(() => {
288
+ db = knexLib({
289
+ client: 'better-sqlite3',
290
+ connection: {
291
+ filename: ':memory:'
292
+ },
293
+ useNullAsDefault: true
294
+ })
295
+ })
296
+
297
+ afterEach(async () => {
298
+ await db.destroy()
299
+ })
300
+
301
+ it('generates executable migrations', async () => {
302
+ const schema = makeTableSchema({
303
+ name: { type: 'string', required: true },
304
+ age: { type: 'number', min: 0 },
305
+ email: { type: 'string', unique: true }
306
+ })
307
+
308
+ const migration = generateKnexMigration('users', schema)
309
+ const migrationModule = {}
310
+ const migrationCode = migration
311
+ .replace(/exports\.up/g, 'migrationModule.up')
312
+ .replace(/exports\.down/g, 'migrationModule.down')
313
+
314
+ new Function('migrationModule', migrationCode)(migrationModule)
315
+
316
+ await migrationModule.up(db)
317
+
318
+ const exists = await db.schema.hasTable('users')
319
+ assert.equal(exists, true)
320
+
321
+ await db('users').insert({
322
+ name: 'Test User',
323
+ age: 25,
324
+ email: 'test@example.com'
325
+ })
326
+
327
+ await migrationModule.down(db)
328
+
329
+ const existsAfter = await db.schema.hasTable('users')
330
+ assert.equal(existsAfter, false)
331
+ })
332
+
333
+ it('includes the expected schema features in generated migrations', () => {
334
+ const schema = makeTableSchema({
335
+ id: { type: 'id', primary: true },
336
+ title: { type: 'string', required: true, maxLength: 200 },
337
+ price: { type: 'number', precision: 10, scale: 2 },
338
+ inStock: { type: 'boolean', defaultTo: true },
339
+ categoryId: {
340
+ type: 'id',
341
+ nullable: true,
342
+ references: { table: 'categories', onDelete: 'SET NULL' }
343
+ },
344
+ tags: { type: 'array', defaultTo: [] }
345
+ })
346
+
347
+ const migration = generateKnexMigration('products', schema, {
348
+ timestamps: true
349
+ })
350
+
351
+ assert.ok(migration.includes("table.integer('id').unsigned().primary()"))
352
+ assert.ok(migration.includes("table.string('title', 200).notNullable()"))
353
+ assert.ok(migration.includes("table.decimal('price', 10, 2)"))
354
+ assert.ok(migration.includes(".defaultTo(true)"))
355
+ assert.ok(migration.includes("table.foreign(['categoryId'], 'products_categoryId_foreign').references(['id']).inTable('categories').onDelete('SET NULL')"))
356
+ assert.ok(migration.includes('table.timestamps(true, true)'))
357
+ })
358
+
359
+ it('uses storage column overrides in generated migrations', () => {
360
+ const schema = makeTableSchema({
361
+ displayName: { type: 'string', required: true, storage: { column: 'display_name' } },
362
+ loginCount: { type: 'number', defaultTo: 0, storage: { column: 'login_count' } }
363
+ })
364
+
365
+ const migration = generateKnexMigration('mapped_profiles', schema)
366
+
367
+ assert.ok(migration.includes("table.string('display_name').notNullable()"))
368
+ assert.ok(migration.includes("table.float('login_count').defaultTo(0)"))
369
+ assert.ok(!migration.includes("table.string('displayName')"))
370
+ })
371
+
372
+ it('supports mapped logical id fields in generated migrations', () => {
373
+ const schema = makeTableSchema({
374
+ id: { type: 'id', primary: true, storage: { column: 'book_id' } },
375
+ title: { type: 'string', required: true }
376
+ })
377
+
378
+ const migration = generateKnexMigration('books', schema)
379
+
380
+ assert.ok(migration.includes("table.integer('book_id').unsigned().primary()"))
381
+ assert.ok(migration.includes("table.string('title').notNullable()"))
382
+ assert.equal((migration.match(/book_id/g) || []).length, 1)
383
+ })
384
+
385
+ it('includes named composite indexes, foreign keys, checks, enum, and set columns in generated migrations', () => {
386
+ const schema = makeTableSchema({
387
+ workspaceId: { type: 'id', required: true, storage: { column: 'workspace_id' } },
388
+ userId: { type: 'id', required: true, storage: { column: 'user_id' } },
389
+ role: { type: 'string', enum: ['owner', 'member'], required: true, defaultTo: 'member' },
390
+ flags: { type: 'string', setValues: ['featured', 'archived'] },
391
+ noteCount: { type: 'number', defaultTo: 0, storage: { column: 'note_count' } }
392
+ }, {
393
+ indexes: [
394
+ {
395
+ name: 'uq_memberships_workspace_user',
396
+ unique: true,
397
+ columns: ['workspaceId', 'userId']
398
+ }
399
+ ],
400
+ foreignKeys: [
401
+ {
402
+ name: 'fk_memberships_workspace_user',
403
+ columns: ['workspaceId', 'userId'],
404
+ referencedTableName: 'workspace_users',
405
+ referencedColumns: ['workspace_id', 'user_id'],
406
+ deleteRule: 'CASCADE',
407
+ updateRule: 'RESTRICT'
408
+ }
409
+ ],
410
+ checkConstraints: [
411
+ {
412
+ name: 'chk_memberships_note_count_non_negative',
413
+ clause: 'note_count >= 0'
414
+ }
415
+ ]
416
+ })
417
+
418
+ const migration = generateKnexMigration('memberships', schema, {
419
+ dialect: 'mysql2'
420
+ })
421
+
422
+ assert.ok(migration.includes("table.enu('role', ['owner', 'member']).notNullable().defaultTo('member')"))
423
+ assert.ok(migration.includes("table.specificType('flags', 'set(\\'featured\\', \\'archived\\')')"))
424
+ assert.ok(migration.includes("table.unique(['workspace_id', 'user_id'], 'uq_memberships_workspace_user')"))
425
+ assert.ok(migration.includes("table.foreign(['workspace_id', 'user_id'], 'fk_memberships_workspace_user').references(['workspace_id', 'user_id']).inTable('workspace_users').onDelete('CASCADE').onUpdate('RESTRICT')"))
426
+ assert.ok(migration.includes("table.check('note_count >= 0', [], 'chk_memberships_note_count_non_negative')"))
427
+ })
428
+
429
+ it('includes indexType for non-unique indexes in generated migrations', () => {
430
+ const schema = makeTableSchema({
431
+ body: { type: 'string' }
432
+ }, {
433
+ indexes: [
434
+ {
435
+ name: 'idx_articles_body_fulltext',
436
+ columns: ['body'],
437
+ indexType: 'FULLTEXT'
438
+ }
439
+ ]
440
+ })
441
+
442
+ const migration = generateKnexMigration('articles', schema)
443
+
444
+ assert.ok(migration.includes("table.index(['body'], 'idx_articles_body_fulltext', { \"indexType\": 'FULLTEXT' })"))
445
+ })
446
+
447
+ it('rejects indexType on unique index metadata', () => {
448
+ const schema = makeTableSchema({
449
+ email: { type: 'string' }
450
+ }, {
451
+ indexes: [
452
+ {
453
+ name: 'uq_accounts_email',
454
+ unique: true,
455
+ columns: ['email'],
456
+ indexType: 'BTREE'
457
+ }
458
+ ]
459
+ })
460
+
461
+ assert.throws(
462
+ () => generateKnexMigration('accounts', schema),
463
+ /cannot define indexType/i
464
+ )
465
+ })
466
+ })
467
+
468
+ describe('dbTablesOperations.generateKnexMigrationDiff', () => {
469
+ beforeEach(() => {
470
+ db = knexLib({
471
+ client: 'better-sqlite3',
472
+ connection: {
473
+ filename: ':memory:'
474
+ },
475
+ useNullAsDefault: true
476
+ })
477
+ })
478
+
479
+ afterEach(async () => {
480
+ await db.destroy()
481
+ })
482
+
483
+ it('generates additive alter migrations from a live snapshot', async () => {
484
+ await db.raw('PRAGMA foreign_keys = ON')
485
+ await db.raw(`
486
+ CREATE TABLE workspace_users (
487
+ workspace_id INTEGER NOT NULL,
488
+ user_id INTEGER NOT NULL,
489
+ PRIMARY KEY (workspace_id, user_id)
490
+ )
491
+ `)
492
+ await db.raw(`
493
+ CREATE TABLE memberships (
494
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
495
+ workspace_id INTEGER NOT NULL,
496
+ user_id INTEGER NOT NULL
497
+ )
498
+ `)
499
+
500
+ const currentSnapshot = await introspectKnexTableSnapshot(db, {
501
+ tableName: 'memberships',
502
+ idColumn: 'id'
503
+ })
504
+ const desiredSchema = makeTableSchema({
505
+ workspaceId: { type: 'id', required: true, storage: { column: 'workspace_id' } },
506
+ userId: { type: 'id', required: true, storage: { column: 'user_id' } },
507
+ role: { type: 'string', enum: ['owner', 'member'], required: true, defaultTo: 'member' },
508
+ noteCount: { type: 'number', defaultTo: 0, storage: { column: 'note_count' } }
509
+ }, {
510
+ indexes: [
511
+ {
512
+ name: 'uq_memberships_workspace_user',
513
+ unique: true,
514
+ columns: ['workspaceId', 'userId']
515
+ }
516
+ ],
517
+ foreignKeys: [
518
+ {
519
+ name: 'fk_memberships_workspace_user',
520
+ columns: ['workspaceId', 'userId'],
521
+ referencedTableName: 'workspace_users',
522
+ referencedColumns: ['workspace_id', 'user_id'],
523
+ deleteRule: 'CASCADE',
524
+ updateRule: 'RESTRICT'
525
+ }
526
+ ]
527
+ })
528
+
529
+ const diff = generateKnexMigrationDiff('memberships', currentSnapshot, desiredSchema, {
530
+ dialect: 'better-sqlite3'
531
+ })
532
+
533
+ assert.deepEqual(diff.warnings, [])
534
+ assert.equal(Object.hasOwn(diff.plan, 'dropCheckConstraints'), false)
535
+ assert.deepEqual(diff.plan.addColumns.map((column) => column.name), ['note_count', 'role'])
536
+ assert.deepEqual(diff.plan.addIndexes.map((index) => index.name), ['uq_memberships_workspace_user'])
537
+ assert.deepEqual(diff.plan.addForeignKeys.map((foreignKey) => foreignKey.name), ['fk_memberships_workspace_user'])
538
+ assert.ok(diff.migration.includes("table.float('note_count').defaultTo(0)"))
539
+ assert.ok(diff.migration.includes("table.enu('role', ['owner', 'member']).notNullable().defaultTo('member')"))
540
+
541
+ const migrationModule = {}
542
+ new Function('migrationModule', diff.migration
543
+ .replace(/exports\.up/g, 'migrationModule.up')
544
+ .replace(/exports\.down/g, 'migrationModule.down'))(migrationModule)
545
+
546
+ await migrationModule.up(db)
547
+
548
+ const afterSnapshot = await introspectKnexTableSnapshot(db, {
549
+ tableName: 'memberships',
550
+ idColumn: 'id'
551
+ })
552
+
553
+ const roleColumn = afterSnapshot.columns.find((column) => column.name === 'role')
554
+ assert.ok(roleColumn)
555
+ assert.deepEqual(roleColumn.enumValues, ['owner', 'member'])
556
+ assert.equal(roleColumn.defaultValue, 'member')
557
+ assert.ok(afterSnapshot.columns.some((column) => column.name === 'note_count'))
558
+ assert.ok(afterSnapshot.indexes.some((index) => index.name === 'uq_memberships_workspace_user'))
559
+ assert.ok(afterSnapshot.foreignKeys.some((foreignKey) => foreignKey.name === 'fk_memberships_workspace_user'))
560
+
561
+ const secondDiff = generateKnexMigrationDiff('memberships', afterSnapshot, desiredSchema, {
562
+ dialect: 'better-sqlite3'
563
+ })
564
+
565
+ assert.deepEqual(secondDiff.warnings, [])
566
+ assert.equal(secondDiff.plan.addColumns.length, 0)
567
+ assert.equal(secondDiff.plan.alterColumns.length, 0)
568
+ assert.equal(secondDiff.plan.dropColumns.length, 0)
569
+ assert.equal(secondDiff.plan.addIndexes.length, 0)
570
+ assert.equal(secondDiff.plan.dropIndexes.length, 0)
571
+ assert.equal(secondDiff.plan.addForeignKeys.length, 0)
572
+ assert.equal(secondDiff.plan.dropForeignKeys.length, 0)
573
+ })
574
+
575
+ it('recreates indexes when indexType changes', async () => {
576
+ await db.raw(`
577
+ CREATE TABLE articles (
578
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
579
+ body TEXT
580
+ )
581
+ `)
582
+
583
+ const currentSnapshot = await introspectKnexTableSnapshot(db, {
584
+ tableName: 'articles',
585
+ idColumn: 'id'
586
+ })
587
+ currentSnapshot.indexes.push({
588
+ name: 'idx_articles_body',
589
+ unique: false,
590
+ columns: ['body'],
591
+ indexType: ''
592
+ })
593
+
594
+ const desiredSchema = makeTableSchema({
595
+ body: { type: 'string' }
596
+ }, {
597
+ indexes: [
598
+ {
599
+ name: 'idx_articles_body',
600
+ columns: ['body'],
601
+ indexType: 'FULLTEXT'
602
+ }
603
+ ]
604
+ })
605
+
606
+ const diff = generateKnexMigrationDiff('articles', currentSnapshot, desiredSchema, {
607
+ dialect: 'mysql2'
608
+ })
609
+
610
+ assert.deepEqual(diff.plan.addIndexes.map((index) => index.name), ['idx_articles_body'])
611
+ assert.deepEqual(diff.plan.dropIndexes.map((index) => index.name), ['idx_articles_body'])
612
+ assert.ok(diff.migration.includes("table.dropIndex(['body'], 'idx_articles_body');"))
613
+ assert.ok(diff.migration.includes("table.index(['body'], 'idx_articles_body', { \"indexType\": 'FULLTEXT' });"))
614
+ })
615
+
616
+ it('warns about destructive column changes and skipped drops by default', async () => {
617
+ await db.raw(`
618
+ CREATE TABLE documents (
619
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
620
+ title VARCHAR(255),
621
+ legacy_code TEXT
622
+ )
623
+ `)
624
+
625
+ const currentSnapshot = await introspectKnexTableSnapshot(db, {
626
+ tableName: 'documents',
627
+ idColumn: 'id'
628
+ })
629
+ const desiredSchema = makeTableSchema({
630
+ title: { type: 'string', required: true, maxLength: 50 }
631
+ }, {
632
+ checkConstraints: [
633
+ {
634
+ name: 'chk_documents_title_non_empty',
635
+ clause: 'length(title) > 0'
636
+ }
637
+ ]
638
+ })
639
+
640
+ const diff = generateKnexMigrationDiff('documents', currentSnapshot, desiredSchema, {
641
+ dialect: 'sqlite'
642
+ })
643
+
644
+ assert.ok(diff.warnings.some((warning) => warning.includes("Column 'legacy_code' exists in the live table but not in the desired schema.")))
645
+ assert.ok(diff.warnings.some((warning) => warning.includes("Column 'title' changes from nullable to not-null")))
646
+ assert.ok(diff.warnings.some((warning) => warning.includes("Column 'title' reduces maxLength from 255 to 50")))
647
+ assert.ok(diff.warnings.some((warning) => warning.includes("Skipping check constraint 'chk_documents_title_non_empty'")))
648
+ assert.equal(diff.plan.dropColumns.length, 0)
649
+ assert.deepEqual(diff.plan.alterColumns.map((column) => column.name), ['title'])
650
+ })
651
+
652
+ it('warns when setValues are diffed on a non-MySQL dialect', async () => {
653
+ await db.raw(`
654
+ CREATE TABLE articles (
655
+ id INTEGER PRIMARY KEY AUTOINCREMENT
656
+ )
657
+ `)
658
+
659
+ const currentSnapshot = await introspectKnexTableSnapshot(db, {
660
+ tableName: 'articles',
661
+ idColumn: 'id'
662
+ })
663
+ const desiredSchema = makeTableSchema({
664
+ flags: { type: 'string', setValues: ['featured', 'archived'] }
665
+ })
666
+
667
+ const diff = generateKnexMigrationDiff('articles', currentSnapshot, desiredSchema, {
668
+ dialect: 'sqlite'
669
+ })
670
+
671
+ assert.ok(diff.warnings.some((warning) => warning.includes("Skipping setValues column 'flags'")))
672
+ assert.equal(diff.plan.addColumns.length, 0)
673
+ })
674
+ })
675
+
676
+ describe('dbTablesOperations field-only helpers', () => {
677
+ beforeEach(() => {
678
+ db = knexLib({
679
+ client: 'better-sqlite3',
680
+ connection: {
681
+ filename: ':memory:'
682
+ },
683
+ useNullAsDefault: true
684
+ })
685
+ })
686
+
687
+ afterEach(async () => {
688
+ await db.destroy()
689
+ })
690
+
691
+ it('rejects top-level metadata in addKnexFields', async () => {
692
+ await assert.rejects(
693
+ () => addKnexFields(db, 'accounts', makeTableSchema({
694
+ email: { type: 'string' }
695
+ }, {
696
+ indexes: [{ name: 'idx_accounts_email', columns: ['email'] }]
697
+ })),
698
+ /does not accept top-level indexes/i
699
+ )
700
+ })
701
+
702
+ it('rejects top-level metadata in alterKnexFields', async () => {
703
+ await assert.rejects(
704
+ () => alterKnexFields(db, 'accounts', makeTableSchema({
705
+ email: { type: 'string' }
706
+ }, {
707
+ checkConstraints: [{ name: 'chk_accounts_email', clause: "email <> ''" }]
708
+ })),
709
+ /does not accept top-level checkConstraints/i
710
+ )
711
+ })
712
+ })
713
+
714
+ describe('RestApiKnexPlugin migration scope methods', () => {
715
+ let api
716
+
717
+ before(async () => {
718
+ db = knexLib({
719
+ client: 'better-sqlite3',
720
+ connection: {
721
+ filename: ':memory:'
722
+ },
723
+ useNullAsDefault: true
724
+ })
725
+
726
+ api = new Api({
727
+ name: 'migration-scope-test-api',
728
+ log: { level: 'warn' }
729
+ })
730
+
731
+ await api.use(RestApiPlugin, {
732
+ simplifiedApi: false,
733
+ simplifiedTransport: false
734
+ })
735
+ await api.use(RestApiKnexPlugin, { knex: db })
736
+
737
+ await api.addResource('memberships', {
738
+ schema: {
739
+ id: { type: 'id' },
740
+ workspaceId: { type: 'id', required: true, storage: { column: 'workspace_id' } },
741
+ userId: { type: 'id', required: true, storage: { column: 'user_id' } },
742
+ role: { type: 'string', enum: ['owner', 'member'], required: true, defaultTo: 'member' }
743
+ },
744
+ indexes: [
745
+ {
746
+ name: 'uq_scope_memberships_workspace_user',
747
+ unique: true,
748
+ columns: ['workspaceId', 'userId']
749
+ }
750
+ ],
751
+ checkConstraints: [
752
+ {
753
+ name: 'chk_scope_memberships_workspace_positive',
754
+ clause: 'workspace_id > 0'
755
+ }
756
+ ],
757
+ tableName: 'scope_memberships'
758
+ })
759
+
760
+ await api.resources.memberships.createKnexTable()
761
+ })
762
+
763
+ after(async () => {
764
+ await db.destroy()
765
+ })
766
+
767
+ it('exposes create and diff migration helpers through table-backed resource scopes', async () => {
768
+ const createMigration = await api.resources.memberships.generateKnexMigration()
769
+
770
+ assert.ok(createMigration.includes("table.unique(['workspace_id', 'user_id'], 'uq_scope_memberships_workspace_user')"))
771
+ assert.ok(createMigration.includes("table.check('workspace_id > 0', [], 'chk_scope_memberships_workspace_positive')"))
772
+
773
+ const diff = await api.resources.memberships.generateKnexMigrationDiff()
774
+
775
+ assert.deepEqual(diff.warnings, [])
776
+ assert.equal(diff.plan.addColumns.length, 0)
777
+ assert.equal(diff.plan.alterColumns.length, 0)
778
+ assert.equal(diff.plan.addIndexes.length, 0)
779
+ assert.equal(diff.plan.addCheckConstraints.length, 0)
780
+ })
781
+ })