json-rest-api 1.0.22 → 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.
- package/CLAUDE.md +1 -2
- package/JSKIT_BACKEND_MIGRATION_TRACKER.txt +180 -0
- package/SINGLE_VALIDATION_CONTRACT_TODO.txt +172 -0
- package/docs/API.md +51 -14
- package/docs/COMPARISON.md +2 -2
- package/docs/GUIDE/GUIDE_1_Initial_Setup.md +28 -1
- package/docs/GUIDE/GUIDE_2_2_Manipulating_And_Searching_Tables.md +5 -1
- package/docs/GUIDE/GUIDE_2_7_Pagination_And_Ordering.md +3 -1
- package/docs/GUIDE/GUIDE_2_8_Effects_of_PUT_and_PATCH.md +12 -60
- package/docs/GUIDE/GUIDE_2_Data_And_Relations.md +2 -2
- package/docs/GUIDE/GUIDE_3_Field_Transformations.md +5 -1
- package/docs/GUIDE/GUIDE_7_Hooks_Data_Management_And_Plugins.md +2 -0
- package/docs/GUIDE/GUIDE_X_Autofiltering.md +355 -0
- package/docs/GUIDE/GUIDE_X_Fastify.md +78 -0
- package/docs/GUIDE/GUIDE_X_Knex_Schema_And_Migrations.md +250 -0
- package/docs/GUIDE/GUIDE_X_Query_Projections.md +159 -0
- package/docs/GUIDE/GUIDE_X_Writing_Plugins.md +199 -0
- package/docs/GUIDE/index.md +18 -9
- package/docs/QUICKSTART.md +5 -1
- package/docs/index.md +1 -1
- package/index.js +3 -2
- package/package.json +2 -2
- package/plugins/core/connectors/express-plugin.js +97 -184
- package/plugins/core/connectors/fastify-plugin.js +206 -0
- package/plugins/core/connectors/lib/connector-core.js +195 -0
- package/plugins/core/connectors/lib/transport-http-helpers.js +122 -0
- package/plugins/core/connectors/lib/transport-route-schemas.js +92 -0
- package/plugins/core/lib/dbIntrospection.js +1078 -0
- package/plugins/core/lib/dbTablesOperations.js +1104 -160
- package/plugins/core/lib/querying/knex-json-api-transformers-querying.js +5 -2
- package/plugins/core/lib/querying/knex-process-includes.js +3 -4
- package/plugins/core/lib/querying/knex-query-helpers.js +17 -71
- package/plugins/core/lib/querying/knex-relationship-includes.js +466 -206
- package/plugins/core/lib/querying/query-builder-utils.js +19 -0
- package/plugins/core/lib/querying/query-field-sort-helpers.js +135 -0
- package/plugins/core/lib/querying/sort-helpers.js +33 -0
- package/plugins/core/lib/querying/storage-adapter-utils.js +51 -0
- package/plugins/core/lib/querying/url-helpers.js +8 -2
- package/plugins/core/lib/querying-writing/compile-schemas.js +12 -1
- package/plugins/core/lib/querying-writing/knex-field-helpers.js +83 -13
- package/plugins/core/lib/querying-writing/knex-json-api-transformers.js +13 -12
- package/plugins/core/lib/querying-writing/query-field-helpers.js +88 -0
- package/plugins/core/lib/querying-writing/request-contracts.js +590 -0
- package/plugins/core/lib/storage/canonical-storage-mapping.js +113 -0
- package/plugins/core/lib/storage/storage-adapter.js +79 -2
- package/plugins/core/lib/storage/storage-mapping.js +204 -0
- package/plugins/core/rest-api-anyapi-knex-plugin.js +355 -228
- package/plugins/core/rest-api-autofilter-plugin.js +414 -0
- package/plugins/core/rest-api-knex-plugin.js +249 -211
- package/plugins/core/rest-api-plugin-hooks/register-relationship-routes.js +10 -5
- package/plugins/core/rest-api-plugin-hooks/register-scope-routes.js +12 -6
- package/plugins/core/rest-api-plugin-methods/apply-query-filters.js +41 -0
- package/plugins/core/rest-api-plugin-methods/common.js +121 -83
- package/plugins/core/rest-api-plugin-methods/enrich-attributes.js +6 -1
- package/plugins/core/rest-api-plugin-methods/get.js +23 -14
- package/plugins/core/rest-api-plugin-methods/patch.js +28 -12
- package/plugins/core/rest-api-plugin-methods/post.js +13 -8
- package/plugins/core/rest-api-plugin-methods/put.js +45 -21
- package/plugins/core/rest-api-plugin-methods/query.js +15 -53
- package/plugins/core/rest-api-plugin.js +2 -0
- package/plugins/core/rest-api-positioning-plugin.js +1 -1
- package/plugins/core/rest-api-query-projections-plugin.js +130 -0
- package/plugins/core/socketio-plugin.js +1 -3
- package/tests/autofilter.test.js +463 -0
- package/tests/custom-id-simplified-post.test.js +75 -0
- package/tests/custom-idproperty-relationships.test.js +33 -0
- package/tests/db-introspection.test.js +518 -0
- package/tests/db-table-operations.test.js +781 -0
- package/tests/fastify-plugin.test.js +141 -0
- package/tests/field-getters.test.js +1 -2
- package/tests/field-setters.test.js +0 -4
- package/tests/fixtures/api-configs.js +188 -219
- package/tests/helpers/fake-fastify.js +99 -0
- package/tests/http-connectors-parity.test.js +235 -0
- package/tests/projected-fields.test.js +319 -0
- package/tests/relationships.test.js +3 -3
- package/tests/searchschema-knex-behavior.test.js +187 -0
- package/tests/searchschema-merge.test.js +21 -69
- package/tests/storage-mapping.test.js +210 -0
- package/audit/00-charter.md +0 -103
- package/audit/01-scope-map.md +0 -84
- package/audit/02-coverage-matrix.csv +0 -174
- package/audit/03-invariants.md +0 -123
- package/audit/04-standards-rubric.md +0 -56
- package/audit/05-competitor-rubric.md +0 -77
- package/audit/06-findings.md +0 -160
- package/audit/07-risk-register.csv +0 -10
- package/audit/08-remediation-roadmap.md +0 -177
- package/audit/09-signoff-check.md +0 -23
- package/audit/10-final-synthesis.md +0 -29
- package/audit/evidence/code/all-files-json-rest-api.txt +0 -168
- package/audit/evidence/code/all-files-json-rest-schema.txt +0 -12
- package/audit/evidence/code/boundary-leak-checks.txt +0 -72
- package/audit/evidence/code/critical-pass1/json-rest-api__plugins__core__bulk-operations-plugin.js.txt +0 -480
- package/audit/evidence/code/critical-pass1/json-rest-api__plugins__core__connectors__express-plugin.js.txt +0 -532
- package/audit/evidence/code/critical-pass1/json-rest-api__plugins__core__connectors__lib__busboy-detector.js.txt +0 -175
- package/audit/evidence/code/critical-pass1/json-rest-api__plugins__core__connectors__lib__formidable-detector.js.txt +0 -138
- package/audit/evidence/code/critical-pass1/json-rest-api__plugins__core__connectors__lib__request-helpers.js.txt +0 -222
- package/audit/evidence/code/critical-pass1/json-rest-api__plugins__core__file-handling-plugin.js.txt +0 -342
- package/audit/evidence/code/critical-pass1/json-rest-api__plugins__core__lib__dbTablesOperations.js.txt +0 -304
- package/audit/evidence/code/critical-pass1/json-rest-api__plugins__core__lib__fractional-positioning.js.txt +0 -188
- package/audit/evidence/code/critical-pass1/json-rest-api__plugins__core__lib__querying-writing__compile-schemas.js.txt +0 -336
- package/audit/evidence/code/critical-pass1/json-rest-api__plugins__core__lib__querying-writing__connectors-query-parser.js.txt +0 -165
- package/audit/evidence/code/critical-pass1/json-rest-api__plugins__core__lib__querying-writing__database-capabilities.js.txt +0 -265
- package/audit/evidence/code/critical-pass1/json-rest-api__plugins__core__lib__querying-writing__database-value-normalizers.js.txt +0 -369
- package/audit/evidence/code/critical-pass1/json-rest-api__plugins__core__lib__querying-writing__default-data-helpers.js.txt +0 -72
- package/audit/evidence/code/critical-pass1/json-rest-api__plugins__core__lib__querying-writing__field-utils.js.txt +0 -139
- package/audit/evidence/code/critical-pass1/json-rest-api__plugins__core__lib__querying-writing__knex-constants.js.txt +0 -45
- package/audit/evidence/code/critical-pass1/json-rest-api__plugins__core__lib__querying-writing__knex-field-helpers.js.txt +0 -396
- package/audit/evidence/code/critical-pass1/json-rest-api__plugins__core__lib__querying-writing__knex-json-api-transformers.js.txt +0 -162
- package/audit/evidence/code/critical-pass1/json-rest-api__plugins__core__lib__querying-writing__payload-validators.js.txt +0 -1314
- package/audit/evidence/code/critical-pass1/json-rest-api__plugins__core__lib__querying-writing__schema-helpers.js.txt +0 -488
- package/audit/evidence/code/critical-pass1/json-rest-api__plugins__core__lib__querying-writing__scope-validations.js.txt +0 -484
- package/audit/evidence/code/critical-pass1/json-rest-api__plugins__core__lib__querying-writing__simplified-helpers.js.txt +0 -857
- package/audit/evidence/code/critical-pass1/json-rest-api__plugins__core__lib__querying__knex-cross-table-search.js.txt +0 -565
- package/audit/evidence/code/critical-pass1/json-rest-api__plugins__core__lib__querying__knex-json-api-transformers-querying.js.txt +0 -485
- package/audit/evidence/code/critical-pass1/json-rest-api__plugins__core__lib__querying__knex-pagination-helpers.js.txt +0 -560
- package/audit/evidence/code/critical-pass1/json-rest-api__plugins__core__lib__querying__knex-process-includes.js.txt +0 -167
- package/audit/evidence/code/critical-pass1/json-rest-api__plugins__core__lib__querying__knex-query-helpers-base.js.txt +0 -109
- package/audit/evidence/code/critical-pass1/json-rest-api__plugins__core__lib__querying__knex-query-helpers.js.txt +0 -1064
- package/audit/evidence/code/critical-pass1/json-rest-api__plugins__core__lib__querying__knex-relationship-includes.js.txt +0 -1983
- package/audit/evidence/code/critical-pass1/json-rest-api__plugins__core__lib__querying__knex-window-queries.js.txt +0 -328
- package/audit/evidence/code/critical-pass1/json-rest-api__plugins__core__lib__querying__url-helpers.js.txt +0 -119
- package/audit/evidence/code/critical-pass1/json-rest-api__plugins__core__lib__storage__storage-adapter.js.txt +0 -160
- package/audit/evidence/code/critical-pass1/json-rest-api__plugins__core__lib__writing__many-to-many-manipulations.js.txt +0 -300
- package/audit/evidence/code/critical-pass1/json-rest-api__plugins__core__lib__writing__relationship-processor.js.txt +0 -245
- package/audit/evidence/code/critical-pass1/json-rest-api__plugins__core__rest-api-access.js.txt +0 -459
- package/audit/evidence/code/critical-pass1/json-rest-api__plugins__core__rest-api-cors-plugin.js.txt +0 -237
- package/audit/evidence/code/critical-pass1/json-rest-api__plugins__core__rest-api-knex-plugin.js.txt +0 -1135
- package/audit/evidence/code/critical-pass1/json-rest-api__plugins__core__rest-api-multihome-plugin.js.txt +0 -314
- package/audit/evidence/code/critical-pass1/json-rest-api__plugins__core__rest-api-plugin-hooks__compile-resource-schemas.js.txt +0 -7
- package/audit/evidence/code/critical-pass1/json-rest-api__plugins__core__rest-api-plugin-hooks__register-relationship-routes.js.txt +0 -71
- package/audit/evidence/code/critical-pass1/json-rest-api__plugins__core__rest-api-plugin-hooks__register-scope-routes.js.txt +0 -102
- package/audit/evidence/code/critical-pass1/json-rest-api__plugins__core__rest-api-plugin-hooks__turn-scope-init-into-vars.js.txt +0 -49
- package/audit/evidence/code/critical-pass1/json-rest-api__plugins__core__rest-api-plugin-hooks__validate-include-configurations.js.txt +0 -39
- package/audit/evidence/code/critical-pass1/json-rest-api__plugins__core__rest-api-plugin-methods__add-route.js.txt +0 -23
- package/audit/evidence/code/critical-pass1/json-rest-api__plugins__core__rest-api-plugin-methods__check-permissions.js.txt +0 -13
- package/audit/evidence/code/critical-pass1/json-rest-api__plugins__core__rest-api-plugin-methods__common.js.txt +0 -639
- package/audit/evidence/code/critical-pass1/json-rest-api__plugins__core__rest-api-plugin-methods__delete-relationship.js.txt +0 -117
- package/audit/evidence/code/critical-pass1/json-rest-api__plugins__core__rest-api-plugin-methods__delete.js.txt +0 -104
- package/audit/evidence/code/critical-pass1/json-rest-api__plugins__core__rest-api-plugin-methods__enrich-attributes.js.txt +0 -199
- package/audit/evidence/code/critical-pass1/json-rest-api__plugins__core__rest-api-plugin-methods__get-related.js.txt +0 -353
- package/audit/evidence/code/critical-pass1/json-rest-api__plugins__core__rest-api-plugin-methods__get-relationship.js.txt +0 -61
- package/audit/evidence/code/critical-pass1/json-rest-api__plugins__core__rest-api-plugin-methods__get.js.txt +0 -184
- package/audit/evidence/code/critical-pass1/json-rest-api__plugins__core__rest-api-plugin-methods__patch-relationship.js.txt +0 -55
- package/audit/evidence/code/critical-pass1/json-rest-api__plugins__core__rest-api-plugin-methods__patch.js.txt +0 -191
- package/audit/evidence/code/critical-pass1/json-rest-api__plugins__core__rest-api-plugin-methods__post-relationship.js.txt +0 -110
- package/audit/evidence/code/critical-pass1/json-rest-api__plugins__core__rest-api-plugin-methods__post.js.txt +0 -168
- package/audit/evidence/code/critical-pass1/json-rest-api__plugins__core__rest-api-plugin-methods__put.js.txt +0 -253
- package/audit/evidence/code/critical-pass1/json-rest-api__plugins__core__rest-api-plugin-methods__query.js.txt +0 -207
- package/audit/evidence/code/critical-pass1/json-rest-api__plugins__core__rest-api-plugin-methods__release.js.txt +0 -12
- package/audit/evidence/code/critical-pass1/json-rest-api__plugins__core__rest-api-plugin.js.txt +0 -165
- package/audit/evidence/code/critical-pass1/json-rest-api__plugins__core__rest-api-positioning-plugin.js.txt +0 -668
- package/audit/evidence/code/critical-pass1/json-rest-api__plugins__core__socketio-plugin.js.txt +0 -621
- package/audit/evidence/code/critical-pass1/json-rest-api__plugins__storage__local-storage.js.txt +0 -218
- package/audit/evidence/code/critical-pass1/json-rest-api__plugins__storage__s3-storage.js.txt +0 -124
- package/audit/evidence/code/critical-pass1/json-rest-schema__src__CorePlugin.js.txt +0 -269
- package/audit/evidence/code/critical-pass1/json-rest-schema__src__Schema.js.txt +0 -242
- package/audit/evidence/code/critical-pass1/json-rest-schema__src__index.js.txt +0 -77
- package/audit/evidence/code/critical-pass1-risk-keywords.txt +0 -238
- package/audit/evidence/code/critical-pass1-summary.csv +0 -67
- package/audit/evidence/code/critical-pass2-summary.csv +0 -67
- package/audit/evidence/code/early-silent-zones-no-test-token.txt +0 -57
- package/audit/evidence/code/error-model-signals.txt +0 -98
- package/audit/evidence/code/git-files-json-rest-api.txt +0 -171
- package/audit/evidence/code/git-files-json-rest-schema.txt +0 -27
- package/audit/evidence/code/hook-flow-signals.txt +0 -282
- package/audit/evidence/code/idproperty-signals.txt +0 -157
- package/audit/evidence/code/import-cycles-non-anyapi.txt +0 -3
- package/audit/evidence/code/import-hotspots-non-anyapi.txt +0 -96
- package/audit/evidence/code/imports-json-rest-api.txt +0 -354
- package/audit/evidence/code/imports-json-rest-schema.txt +0 -15
- package/audit/evidence/code/in-scope-files-json-rest-api.txt +0 -160
- package/audit/evidence/code/in-scope-files-json-rest-schema.txt +0 -13
- package/audit/evidence/code/non-anyapi-core-files.txt +0 -101
- package/audit/evidence/code/permission-signals.txt +0 -193
- package/audit/evidence/code/query-include-signals.txt +0 -687
- package/audit/evidence/code/transaction-signals.txt +0 -28
- package/audit/evidence/code/validation-signals.txt +0 -650
- package/audit/evidence/code/write-path-signals.txt +0 -61
- package/audit/evidence/docs/docs-claims-signals.txt +0 -1075
- package/audit/evidence/docs/schema-docs-claims-signals.txt +0 -179
- package/audit/evidence/external/corpus-freeze.md +0 -21
- package/audit/evidence/external/npm-competitor-metadata.txt +0 -70
- package/audit/evidence/external/npm-download-signals.txt +0 -23
- package/audit/evidence/external/npm-jsonapi-server.txt +0 -8
- package/audit/evidence/external/npm-modern-jsonapi-metadata.txt +0 -38
- package/audit/evidence/external/readme-fortune-signals.txt +0 -26
- package/audit/evidence/external/readme-fortune.md +0 -165
- package/audit/evidence/external/readme-json-api-serializer-signals.txt +0 -80
- package/audit/evidence/external/readme-json-api-serializer.md +0 -690
- package/audit/evidence/external/readme-jsonapi-serde-server-signals.txt +0 -0
- package/audit/evidence/external/readme-jsonapi-serde-server.md +0 -22
- package/audit/evidence/external/readme-jsonapi-serializer-signals.txt +0 -63
- package/audit/evidence/external/readme-jsonapi-serializer.md +0 -315
- package/audit/evidence/external/readme-jsonapi-server-signals.txt +0 -19
- package/audit/evidence/external/readme-jsonapi-server.md +0 -104
- package/audit/evidence/external/readme-nestjs-json-api-signals.txt +0 -4
- package/audit/evidence/external/readme-nestjs-json-api.md +0 -94
- package/audit/evidence/external/readme-yaser-json-api-nestjs-signals.txt +0 -47
- package/audit/evidence/external/readme-yaser-json-api-nestjs.md +0 -447
- package/audit/evidence/external/standard-jsonapi-snippets.txt +0 -84
- package/audit/evidence/external/standard-rfc-snippets.txt +0 -168
- package/audit/evidence/external/standards-sources.txt +0 -6
- package/audit/evidence/runtime/baseline-commits.txt +0 -2
- package/audit/evidence/runtime/db-adapter-assumptions.txt +0 -3
- package/audit/evidence/runtime/environment.txt +0 -4
- package/audit/evidence/runtime/invariant-runtime-mapping.md +0 -38
- package/audit/evidence/runtime/probe-patch-relationship-error.exitcode +0 -1
- package/audit/evidence/runtime/probe-patch-relationship-error.log +0 -76
- package/audit/evidence/runtime/probe-patch-relationship-error.mjs +0 -67
- package/audit/evidence/runtime/probe-patch-relationship-referenceerror.exitcode +0 -1
- package/audit/evidence/runtime/probe-patch-relationship-referenceerror.log +0 -62
- package/audit/evidence/runtime/probe-patch-relationship-referenceerror.mjs +0 -47
- package/audit/evidence/runtime/test-exitcodes.txt +0 -4
- package/audit/evidence/runtime/test-summary.md +0 -107
- package/audit/evidence/runtime/trace-anchors.txt +0 -61
- package/audit/evidence/tests/access-multitenancy-test-signals.txt +0 -125
- package/audit/evidence/tests/failing-tests-summary.txt +0 -25
- package/audit/evidence/tests/npm-test-json-rest-api.exitcode +0 -1
- package/audit/evidence/tests/npm-test-json-rest-api.log +0 -5885
- package/audit/evidence/tests/npm-test-json-rest-schema.exitcode +0 -1
- package/audit/evidence/tests/npm-test-json-rest-schema.log +0 -356
- package/audit/evidence/tests/query-include-test-signals.txt +0 -948
- package/audit/evidence/tests/relationship-test-signals.txt +0 -223
- package/audit/evidence/tests/skipped-tests.txt +0 -10
- package/audit/evidence/tests/test-suite-list-non-anyapi.txt +0 -30
- package/audit/evidence/tests/test-suite-list.txt +0 -32
- package/audit/subsystems/docs-drift.md +0 -49
- package/audit/subsystems/pass2-cross-review.md +0 -131
- package/audit/subsystems/plugins-access.md +0 -51
- package/audit/subsystems/plugins-bulk.md +0 -50
- package/audit/subsystems/plugins-cors.md +0 -48
- package/audit/subsystems/plugins-file.md +0 -53
- package/audit/subsystems/plugins-multihome.md +0 -50
- package/audit/subsystems/plugins-positioning.md +0 -54
- package/audit/subsystems/plugins-socketio.md +0 -50
- package/audit/subsystems/query-engine.md +0 -61
- package/audit/subsystems/relationships.md +0 -55
- package/audit/subsystems/rest-orchestration.md +0 -66
- package/audit/subsystems/schema-core.md +0 -64
- package/audit/subsystems/storage-knex.md +0 -57
- package/audit/subsystems/tests.md +0 -48
- package/audit/subsystems/transport-express.md +0 -55
- package/audit/traces/trace-read-get.md +0 -22
- package/audit/traces/trace-read-query.md +0 -25
- package/audit/traces/trace-relationship-endpoints.md +0 -31
- package/audit/traces/trace-write-delete.md +0 -17
- package/audit/traces/trace-write-patch.md +0 -26
- package/audit/traces/trace-write-post.md +0 -22
- package/audit/traces/trace-write-put.md +0 -23
- package/docs/GUIDE/GUIDE_4_Authentication_And_Permissions.md +0 -707
- package/docs/GUIDE/GUIDE_X_Multihome.md +0 -666
- package/plugins/core/lib/querying-writing/payload-validators.js +0 -1314
- package/plugins/core/rest-api-access.js +0 -459
- package/plugins/core/rest-api-multihome-plugin.js +0 -314
- package/tests/multihome.test.js +0 -553
- package/tests/multihome.test.js.bak +0 -550
- package/tests/ownership-optout.test.js +0 -76
- 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
|
+
})
|