json-rest-api 1.0.23 → 1.0.24
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/README.md +22 -1
- package/docs/API.md +145 -24
- package/docs/GUIDE/GUIDE_1_Initial_Setup.md +83 -4
- package/docs/GUIDE/GUIDE_X_Knex_Schema_And_Migrations.md +122 -2
- package/lib/enhanced-logger.js +1 -1
- package/package.json +6 -2
- package/plugins/core/bulk-operations-plugin.js +26 -34
- package/plugins/core/connectors/express-plugin.js +0 -6
- package/plugins/core/connectors/lib/busboy-detector.js +4 -4
- package/plugins/core/lib/anyapi/schema-utils.js +43 -4
- package/plugins/core/lib/anyapi/utils/descriptor-helpers.js +4 -1
- package/plugins/core/lib/dbIntrospection.js +1 -1
- package/plugins/core/lib/dbTablesOperations.js +10 -3
- package/plugins/core/lib/querying/knex-cross-table-search.js +0 -7
- package/plugins/core/lib/querying/knex-json-api-transformers-querying.js +0 -1
- package/plugins/core/lib/querying/knex-query-helpers.js +5 -6
- package/plugins/core/lib/querying/knex-relationship-includes.js +11 -18
- package/plugins/core/lib/querying-writing/compile-schemas.js +10 -6
- package/plugins/core/lib/querying-writing/knex-field-helpers.js +1 -2
- package/plugins/core/lib/querying-writing/resource-id-normalization.js +131 -0
- package/plugins/core/lib/querying-writing/schema-helpers.js +0 -2
- package/plugins/core/lib/querying-writing/simplified-helpers.js +4 -11
- package/plugins/core/lib/storage/canonical-storage-mapping.js +22 -2
- package/plugins/core/lib/storage/storage-adapter.js +3 -2
- package/plugins/core/lib/storage/storage-mapping.js +86 -16
- package/plugins/core/lib/writing/many-to-many-manipulations.js +4 -2
- package/plugins/core/rest-api-anyapi-knex-plugin.js +194 -102
- package/plugins/core/rest-api-knex-plugin.js +23 -16
- package/plugins/core/rest-api-plugin-hooks/turn-scope-init-into-vars.js +1 -0
- package/plugins/core/rest-api-plugin-methods/common.js +27 -9
- package/plugins/core/rest-api-plugin-methods/delete-relationship.js +11 -2
- package/plugins/core/rest-api-plugin-methods/delete.js +6 -1
- package/plugins/core/rest-api-plugin-methods/get-related.js +7 -2
- package/plugins/core/rest-api-plugin-methods/get-relationship.js +7 -2
- package/plugins/core/rest-api-plugin-methods/get.js +7 -1
- package/plugins/core/rest-api-plugin-methods/patch-relationship.js +10 -3
- package/plugins/core/rest-api-plugin-methods/patch.js +31 -3
- package/plugins/core/rest-api-plugin-methods/post-relationship.js +11 -2
- package/plugins/core/rest-api-plugin-methods/post.js +8 -0
- package/plugins/core/rest-api-plugin-methods/put.js +31 -3
- package/plugins/core/rest-api-plugin-methods/release.js +0 -2
- package/plugins/core/rest-api-plugin.js +4 -0
- package/plugins/core/rest-api-positioning-plugin.js +1 -4
- package/plugins/core/socketio-plugin.js +0 -1
- package/plugins/storage/local-storage.js +1 -1
- package/plugins/storage/s3-storage.js +5 -2
- package/quickTest.js +1 -0
- package/scripts/run-docs.js +80 -84
- package/test-debug.js +1 -1
- package/tests/README.md +7 -0
- package/tests/anyapi-basic.test.js +3 -0
- package/tests/custom-id-mysql-returning-fallback.test.js +128 -0
- package/tests/db-introspection.test.js +4 -1
- package/tests/db-table-operations.test.js +25 -20
- package/tests/fastify-plugin.test.js +21 -6
- package/tests/fixtures/api-configs.js +133 -0
- package/tests/id-normalization.test.js +444 -0
- package/tests/logical-id-write-validation.test.js +151 -0
- package/tests/pagination-cursor-multifield.test.js +11 -11
- package/tests/storage-mapping.test.js +55 -6
package/scripts/run-docs.js
CHANGED
|
@@ -1,30 +1,23 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
|
|
3
|
-
/**
|
|
4
|
-
* Run the documentation site
|
|
5
|
-
*
|
|
6
|
-
* This script:
|
|
7
|
-
* 1. Syncs documentation files
|
|
8
|
-
* 2. Builds the Jekyll site
|
|
9
|
-
* 3. Starts the server
|
|
10
|
-
* 4. Opens the browser
|
|
11
|
-
*/
|
|
12
|
-
|
|
13
3
|
import { spawn } from 'child_process'
|
|
14
|
-
import { promises as fs } from 'fs'
|
|
15
4
|
import path from 'path'
|
|
16
5
|
import { fileURLToPath } from 'url'
|
|
17
6
|
|
|
18
7
|
const __filename = fileURLToPath(import.meta.url)
|
|
19
8
|
const __dirname = path.dirname(__filename)
|
|
20
9
|
const projectRoot = path.join(__dirname, '..')
|
|
10
|
+
const docsRoot = path.join(projectRoot, 'docs')
|
|
11
|
+
const LOCAL_URL = 'http://localhost:4000/json-rest-api/'
|
|
12
|
+
const BROWSER_DELAY_MS = 3000
|
|
21
13
|
|
|
22
|
-
|
|
23
|
-
|
|
14
|
+
function parseMode (argv = []) {
|
|
15
|
+
if (argv.includes('--serve') || argv.includes('--dev')) {
|
|
16
|
+
return 'serve'
|
|
17
|
+
}
|
|
24
18
|
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
const GITHUB_PAGES_URL = 'https://mobily-enterprises.github.io/json-rest-api/'
|
|
19
|
+
return 'build'
|
|
20
|
+
}
|
|
28
21
|
|
|
29
22
|
async function runCommand (command, args, options = {}) {
|
|
30
23
|
return new Promise((resolve, reject) => {
|
|
@@ -38,100 +31,103 @@ async function runCommand (command, args, options = {}) {
|
|
|
38
31
|
proc.on('exit', (code) => {
|
|
39
32
|
if (code === 0) {
|
|
40
33
|
resolve()
|
|
41
|
-
|
|
42
|
-
reject(new Error(`Command failed with code ${code}`))
|
|
34
|
+
return
|
|
43
35
|
}
|
|
36
|
+
|
|
37
|
+
reject(new Error(`Command failed with code ${code}`))
|
|
44
38
|
})
|
|
45
39
|
})
|
|
46
40
|
}
|
|
47
41
|
|
|
42
|
+
async function ensureBundlerDependencies () {
|
|
43
|
+
try {
|
|
44
|
+
await runCommand('bundle', ['check'], { cwd: docsRoot })
|
|
45
|
+
} catch {
|
|
46
|
+
console.log('Installing Jekyll dependencies...')
|
|
47
|
+
await runCommand('bundle', ['install'], { cwd: docsRoot })
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
48
51
|
async function openBrowser (url) {
|
|
49
52
|
const platform = process.platform
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
command = 'start'
|
|
56
|
-
} else {
|
|
57
|
-
// Linux
|
|
58
|
-
command = 'xdg-open'
|
|
59
|
-
}
|
|
53
|
+
const command = platform === 'darwin'
|
|
54
|
+
? 'open'
|
|
55
|
+
: platform === 'win32'
|
|
56
|
+
? 'start'
|
|
57
|
+
: 'xdg-open'
|
|
60
58
|
|
|
61
59
|
try {
|
|
62
60
|
await runCommand(command, [url])
|
|
63
|
-
console.log(
|
|
64
|
-
} catch
|
|
65
|
-
console.log(
|
|
61
|
+
console.log(`Opened browser at ${url}`)
|
|
62
|
+
} catch {
|
|
63
|
+
console.log(`Visit ${url} in your browser`)
|
|
66
64
|
}
|
|
67
65
|
}
|
|
68
66
|
|
|
69
|
-
async function
|
|
70
|
-
console.log('
|
|
67
|
+
async function buildDocs () {
|
|
68
|
+
console.log('Building documentation site...')
|
|
69
|
+
await runCommand('bundle', ['exec', 'jekyll', 'build'], { cwd: docsRoot })
|
|
70
|
+
}
|
|
71
71
|
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
72
|
+
async function serveDocs () {
|
|
73
|
+
console.log('Starting documentation server...')
|
|
74
|
+
|
|
75
|
+
const jekyll = spawn('bundle', ['exec', 'jekyll', 'serve', '--watch'], {
|
|
76
|
+
cwd: docsRoot,
|
|
77
|
+
stdio: 'pipe',
|
|
78
|
+
shell: true
|
|
79
|
+
})
|
|
80
|
+
|
|
81
|
+
jekyll.stdout.on('data', (data) => {
|
|
82
|
+
const output = data.toString()
|
|
83
|
+
process.stdout.write(output)
|
|
84
|
+
|
|
85
|
+
if (output.includes('Server running') || output.includes('Server address:')) {
|
|
86
|
+
setTimeout(() => openBrowser(LOCAL_URL), 1000)
|
|
84
87
|
}
|
|
88
|
+
})
|
|
85
89
|
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
cwd: path.join(projectRoot, 'docs'),
|
|
90
|
-
stdio: 'pipe',
|
|
91
|
-
shell: true
|
|
92
|
-
})
|
|
90
|
+
jekyll.stderr.on('data', (data) => {
|
|
91
|
+
process.stderr.write(data)
|
|
92
|
+
})
|
|
93
93
|
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
94
|
+
process.on('SIGINT', () => {
|
|
95
|
+
console.log('\nShutting down Jekyll server...')
|
|
96
|
+
jekyll.kill()
|
|
97
|
+
process.exit(0)
|
|
98
|
+
})
|
|
98
99
|
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
})
|
|
100
|
+
jekyll.on('error', (err) => {
|
|
101
|
+
console.error('Jekyll server error:', err)
|
|
102
|
+
process.exit(1)
|
|
103
|
+
})
|
|
104
104
|
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
105
|
+
jekyll.on('exit', (code) => {
|
|
106
|
+
if (code !== 0 && code !== null) {
|
|
107
|
+
console.error(`Jekyll server exited with code ${code}`)
|
|
108
|
+
process.exit(code)
|
|
109
|
+
}
|
|
110
|
+
})
|
|
108
111
|
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
console.log('\n\n👋 Shutting down Jekyll server...')
|
|
112
|
-
jekyll.kill()
|
|
113
|
-
process.exit(0)
|
|
114
|
-
})
|
|
112
|
+
setTimeout(() => openBrowser(LOCAL_URL), BROWSER_DELAY_MS)
|
|
113
|
+
}
|
|
115
114
|
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
process.exit(1)
|
|
119
|
-
})
|
|
115
|
+
async function main () {
|
|
116
|
+
const mode = parseMode(process.argv.slice(2))
|
|
120
117
|
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
118
|
+
try {
|
|
119
|
+
await ensureBundlerDependencies()
|
|
120
|
+
|
|
121
|
+
if (mode === 'serve') {
|
|
122
|
+
await serveDocs()
|
|
123
|
+
return
|
|
124
|
+
}
|
|
127
125
|
|
|
128
|
-
|
|
129
|
-
setTimeout(() => openBrowser(LOCAL_URL), BROWSER_DELAY)
|
|
126
|
+
await buildDocs()
|
|
130
127
|
} catch (error) {
|
|
131
|
-
console.error('
|
|
128
|
+
console.error('Error:', error.message)
|
|
132
129
|
process.exit(1)
|
|
133
130
|
}
|
|
134
131
|
}
|
|
135
132
|
|
|
136
|
-
// Run the script
|
|
137
133
|
main()
|
package/test-debug.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { createBasicApi } from './tests/fixtures/api-configs.js'
|
|
2
|
-
import { cleanTables, createJsonApiDocument
|
|
2
|
+
import { cleanTables, createJsonApiDocument } from './tests/helpers/test-utils.js'
|
|
3
3
|
import knex from 'knex'
|
|
4
4
|
|
|
5
5
|
const dbConfig = {
|
package/tests/README.md
CHANGED
|
@@ -163,9 +163,16 @@ const result = await api.resources.books.query({
|
|
|
163
163
|
# Run all tests
|
|
164
164
|
npm test
|
|
165
165
|
|
|
166
|
+
# Run the focused normalizeId verification path plus docs rebuild
|
|
167
|
+
npm run verify
|
|
168
|
+
|
|
166
169
|
# Run all tests against the AnyAPI storage backend
|
|
167
170
|
npm run test:anyapi
|
|
168
171
|
|
|
172
|
+
# Run only the normalizeId contract suites
|
|
173
|
+
npm run test:id-contracts
|
|
174
|
+
npm run test:id-contracts:anyapi
|
|
175
|
+
|
|
169
176
|
# Windows PowerShell (AnyAPI run)
|
|
170
177
|
$Env:JSON_REST_API_STORAGE = 'anyapi'; npm test
|
|
171
178
|
|
|
@@ -70,6 +70,7 @@ describe('AnyAPI Knex Plugin - Basic Attributes', () => {
|
|
|
70
70
|
assert.equal(result.data.attributes.name, 'Australia')
|
|
71
71
|
|
|
72
72
|
const stored = await knex('any_records').first()
|
|
73
|
+
assert.equal(stored.logical_id, result.data.id)
|
|
73
74
|
assert.equal(stored.string_1, 'Australia')
|
|
74
75
|
assert.equal(stored.string_2, 'AU')
|
|
75
76
|
|
|
@@ -83,11 +84,13 @@ describe('AnyAPI Knex Plugin - Basic Attributes', () => {
|
|
|
83
84
|
|
|
84
85
|
it('lists records', async () => {
|
|
85
86
|
await knex('any_records').insert([{
|
|
87
|
+
logical_id: '1',
|
|
86
88
|
tenant_id: 'default',
|
|
87
89
|
resource: 'countries',
|
|
88
90
|
string_1: 'Canada',
|
|
89
91
|
string_2: 'CA',
|
|
90
92
|
}, {
|
|
93
|
+
logical_id: '2',
|
|
91
94
|
tenant_id: 'default',
|
|
92
95
|
resource: 'countries',
|
|
93
96
|
string_1: 'Brazil',
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
import { describe, it, before, after, beforeEach } from 'node:test'
|
|
2
|
+
import assert from 'node:assert/strict'
|
|
3
|
+
import knexLib from 'knex'
|
|
4
|
+
import { Api } from 'hooked-api'
|
|
5
|
+
import { RestApiPlugin } from '../plugins/core/rest-api-plugin.js'
|
|
6
|
+
import { RestApiKnexPlugin } from '../plugins/core/rest-api-knex-plugin.js'
|
|
7
|
+
import {
|
|
8
|
+
cleanTables,
|
|
9
|
+
validateJsonApiStructure
|
|
10
|
+
} from './helpers/test-utils.js'
|
|
11
|
+
|
|
12
|
+
const knex = knexLib({
|
|
13
|
+
client: 'better-sqlite3',
|
|
14
|
+
connection: {
|
|
15
|
+
filename: ':memory:'
|
|
16
|
+
},
|
|
17
|
+
useNullAsDefault: true
|
|
18
|
+
})
|
|
19
|
+
|
|
20
|
+
let api
|
|
21
|
+
|
|
22
|
+
function createMysqlStyleInsertDb (baseKnex) {
|
|
23
|
+
return (tableName) => {
|
|
24
|
+
const query = baseKnex(tableName)
|
|
25
|
+
const originalInsert = query.insert.bind(query)
|
|
26
|
+
|
|
27
|
+
query.insert = (...args) => {
|
|
28
|
+
const insertBuilder = originalInsert(...args)
|
|
29
|
+
const originalReturning = insertBuilder.returning.bind(insertBuilder)
|
|
30
|
+
|
|
31
|
+
insertBuilder.returning = async (...returnArgs) => {
|
|
32
|
+
await originalReturning(...returnArgs)
|
|
33
|
+
return [0]
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
return insertBuilder
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
return query
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
describe('Custom id POST fallback when insert returning is unusable', () => {
|
|
44
|
+
before(async () => {
|
|
45
|
+
api = new Api({
|
|
46
|
+
name: 'custom-id-mysql-returning-fallback-test'
|
|
47
|
+
})
|
|
48
|
+
|
|
49
|
+
await api.use(RestApiPlugin, {
|
|
50
|
+
simplifiedApi: true,
|
|
51
|
+
simplifiedTransport: false,
|
|
52
|
+
returnRecordApi: {
|
|
53
|
+
post: 'full',
|
|
54
|
+
put: 'full',
|
|
55
|
+
patch: 'full'
|
|
56
|
+
}
|
|
57
|
+
})
|
|
58
|
+
await api.use(RestApiKnexPlugin, { knex })
|
|
59
|
+
|
|
60
|
+
await api.addResource('user_settings', {
|
|
61
|
+
tableName: 'mysqlish_user_settings',
|
|
62
|
+
idProperty: 'user_id',
|
|
63
|
+
searchSchema: {
|
|
64
|
+
id: { type: 'id', actualField: 'id' }
|
|
65
|
+
},
|
|
66
|
+
schema: {
|
|
67
|
+
theme: { type: 'string', required: true, max: 32 }
|
|
68
|
+
}
|
|
69
|
+
})
|
|
70
|
+
await api.resources.user_settings.createKnexTable()
|
|
71
|
+
})
|
|
72
|
+
|
|
73
|
+
after(async () => {
|
|
74
|
+
await knex.destroy()
|
|
75
|
+
})
|
|
76
|
+
|
|
77
|
+
beforeEach(async () => {
|
|
78
|
+
await cleanTables(knex, ['mysqlish_user_settings'])
|
|
79
|
+
})
|
|
80
|
+
|
|
81
|
+
it('returns the explicit id for simplified POST when the insert result is 0', async () => {
|
|
82
|
+
const mysqlStyleDb = createMysqlStyleInsertDb(knex)
|
|
83
|
+
|
|
84
|
+
const created = await api.resources.user_settings.post({
|
|
85
|
+
inputRecord: {
|
|
86
|
+
id: '6',
|
|
87
|
+
theme: 'dark'
|
|
88
|
+
},
|
|
89
|
+
simplified: true,
|
|
90
|
+
transaction: mysqlStyleDb
|
|
91
|
+
})
|
|
92
|
+
|
|
93
|
+
assert.equal(created.id, '6')
|
|
94
|
+
assert.equal(created.theme, 'dark')
|
|
95
|
+
|
|
96
|
+
const dbRow = await knex('mysqlish_user_settings').where('user_id', 6).first()
|
|
97
|
+
assert.ok(dbRow)
|
|
98
|
+
assert.equal(String(dbRow.user_id), '6')
|
|
99
|
+
assert.equal(dbRow.theme, 'dark')
|
|
100
|
+
})
|
|
101
|
+
|
|
102
|
+
it('returns the explicit id for non-simplified POST when the insert result is 0', async () => {
|
|
103
|
+
const mysqlStyleDb = createMysqlStyleInsertDb(knex)
|
|
104
|
+
|
|
105
|
+
const created = await api.resources.user_settings.post({
|
|
106
|
+
inputRecord: {
|
|
107
|
+
data: {
|
|
108
|
+
type: 'user_settings',
|
|
109
|
+
id: '8',
|
|
110
|
+
attributes: {
|
|
111
|
+
theme: 'light'
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
},
|
|
115
|
+
simplified: false,
|
|
116
|
+
transaction: mysqlStyleDb
|
|
117
|
+
})
|
|
118
|
+
|
|
119
|
+
validateJsonApiStructure(created)
|
|
120
|
+
assert.equal(created.data.id, '8')
|
|
121
|
+
assert.equal(created.data.attributes.theme, 'light')
|
|
122
|
+
|
|
123
|
+
const dbRow = await knex('mysqlish_user_settings').where('user_id', 8).first()
|
|
124
|
+
assert.ok(dbRow)
|
|
125
|
+
assert.equal(String(dbRow.user_id), '8')
|
|
126
|
+
assert.equal(dbRow.theme, 'light')
|
|
127
|
+
})
|
|
128
|
+
})
|
|
@@ -5,6 +5,7 @@ import knexLib from 'knex'
|
|
|
5
5
|
import { introspectKnexTableSnapshot } from '../plugins/core/lib/dbIntrospection.js'
|
|
6
6
|
import { createKnexTable } from '../plugins/core/lib/dbTablesOperations.js'
|
|
7
7
|
import { createBasicApi } from './fixtures/api-configs.js'
|
|
8
|
+
import { storageMode } from './helpers/storage-mode.js'
|
|
8
9
|
|
|
9
10
|
function createMysqlKnexRawDouble ({
|
|
10
11
|
schemaName = 'appdb',
|
|
@@ -484,7 +485,9 @@ describe('dbIntrospection.introspectKnexTableSnapshot (mysql raw double)', () =>
|
|
|
484
485
|
})
|
|
485
486
|
})
|
|
486
487
|
|
|
487
|
-
|
|
488
|
+
const describeKnexIntrospection = storageMode.isAnyApi() ? describe.skip : describe
|
|
489
|
+
|
|
490
|
+
describeKnexIntrospection('RestApiKnexPlugin introspection scope method', () => {
|
|
488
491
|
let db
|
|
489
492
|
let api
|
|
490
493
|
|
|
@@ -118,10 +118,10 @@ describe('dbTablesOperations.createKnexTable', () => {
|
|
|
118
118
|
|
|
119
119
|
await db('users').insert({ username: 'testuser' })
|
|
120
120
|
const user = await db('users').where('username', 'testuser').first()
|
|
121
|
-
await db('posts').insert({ title: 'Test Post',
|
|
121
|
+
await db('posts').insert({ title: 'Test Post', user_id: user.id })
|
|
122
122
|
|
|
123
123
|
const posts = await db('posts')
|
|
124
|
-
.join('users', 'posts.
|
|
124
|
+
.join('users', 'posts.user_id', 'users.id')
|
|
125
125
|
.select('posts.title', 'users.username')
|
|
126
126
|
|
|
127
127
|
assert.equal(posts.length, 1)
|
|
@@ -173,10 +173,11 @@ describe('dbTablesOperations.createKnexTable', () => {
|
|
|
173
173
|
})
|
|
174
174
|
})
|
|
175
175
|
|
|
176
|
-
it('
|
|
176
|
+
it('defaults storage columns to snake_case and keeps per-field overrides', async () => {
|
|
177
177
|
const schema = makeTableSchema({
|
|
178
|
-
displayName: { type: 'string', required: true
|
|
179
|
-
lastSeenAt: { type: 'dateTime'
|
|
178
|
+
displayName: { type: 'string', required: true },
|
|
179
|
+
lastSeenAt: { type: 'dateTime' },
|
|
180
|
+
externalRef: { type: 'string', storage: { column: 'legacy_ref' } }
|
|
180
181
|
})
|
|
181
182
|
|
|
182
183
|
await createKnexTable(db, makeSchemaInfo('mapped_profiles'), schema)
|
|
@@ -185,7 +186,9 @@ describe('dbTablesOperations.createKnexTable', () => {
|
|
|
185
186
|
assert.ok(info.id)
|
|
186
187
|
assert.ok(info.display_name)
|
|
187
188
|
assert.ok(info.last_seen_at)
|
|
189
|
+
assert.ok(info.legacy_ref)
|
|
188
190
|
assert.equal(info.displayName, undefined)
|
|
191
|
+
assert.equal(info.external_ref, undefined)
|
|
189
192
|
})
|
|
190
193
|
|
|
191
194
|
it('supports logical id fields mapped to custom storage columns', async () => {
|
|
@@ -221,10 +224,10 @@ describe('dbTablesOperations.createKnexTable', () => {
|
|
|
221
224
|
`)
|
|
222
225
|
|
|
223
226
|
const schema = makeTableSchema({
|
|
224
|
-
workspaceId: { type: 'id', required: true
|
|
225
|
-
userId: { type: 'id', required: true
|
|
227
|
+
workspaceId: { type: 'id', required: true },
|
|
228
|
+
userId: { type: 'id', required: true },
|
|
226
229
|
role: { type: 'string', enum: ['owner', 'member'], required: true, defaultTo: 'member' },
|
|
227
|
-
noteCount: { type: 'number', defaultTo: 0
|
|
230
|
+
noteCount: { type: 'number', defaultTo: 0 }
|
|
228
231
|
}, {
|
|
229
232
|
indexes: [
|
|
230
233
|
{
|
|
@@ -352,20 +355,22 @@ describe('dbTablesOperations.generateKnexMigration', () => {
|
|
|
352
355
|
assert.ok(migration.includes("table.string('title', 200).notNullable()"))
|
|
353
356
|
assert.ok(migration.includes("table.decimal('price', 10, 2)"))
|
|
354
357
|
assert.ok(migration.includes(".defaultTo(true)"))
|
|
355
|
-
assert.ok(migration.includes("table.foreign(['
|
|
358
|
+
assert.ok(migration.includes("table.foreign(['category_id'], 'products_category_id_foreign').references(['id']).inTable('categories').onDelete('SET NULL')"))
|
|
356
359
|
assert.ok(migration.includes('table.timestamps(true, true)'))
|
|
357
360
|
})
|
|
358
361
|
|
|
359
|
-
it('uses
|
|
362
|
+
it('uses default snake_case columns in generated migrations and keeps per-field overrides', () => {
|
|
360
363
|
const schema = makeTableSchema({
|
|
361
|
-
displayName: { type: 'string', required: true
|
|
362
|
-
loginCount: { type: 'number', defaultTo: 0
|
|
364
|
+
displayName: { type: 'string', required: true },
|
|
365
|
+
loginCount: { type: 'number', defaultTo: 0 },
|
|
366
|
+
externalRef: { type: 'string', storage: { column: 'legacy_ref' } }
|
|
363
367
|
})
|
|
364
368
|
|
|
365
369
|
const migration = generateKnexMigration('mapped_profiles', schema)
|
|
366
370
|
|
|
367
371
|
assert.ok(migration.includes("table.string('display_name').notNullable()"))
|
|
368
372
|
assert.ok(migration.includes("table.float('login_count').defaultTo(0)"))
|
|
373
|
+
assert.ok(migration.includes("table.string('legacy_ref')"))
|
|
369
374
|
assert.ok(!migration.includes("table.string('displayName')"))
|
|
370
375
|
})
|
|
371
376
|
|
|
@@ -384,11 +389,11 @@ describe('dbTablesOperations.generateKnexMigration', () => {
|
|
|
384
389
|
|
|
385
390
|
it('includes named composite indexes, foreign keys, checks, enum, and set columns in generated migrations', () => {
|
|
386
391
|
const schema = makeTableSchema({
|
|
387
|
-
workspaceId: { type: 'id', required: true
|
|
388
|
-
userId: { type: 'id', required: true
|
|
392
|
+
workspaceId: { type: 'id', required: true },
|
|
393
|
+
userId: { type: 'id', required: true },
|
|
389
394
|
role: { type: 'string', enum: ['owner', 'member'], required: true, defaultTo: 'member' },
|
|
390
395
|
flags: { type: 'string', setValues: ['featured', 'archived'] },
|
|
391
|
-
noteCount: { type: 'number', defaultTo: 0
|
|
396
|
+
noteCount: { type: 'number', defaultTo: 0 }
|
|
392
397
|
}, {
|
|
393
398
|
indexes: [
|
|
394
399
|
{
|
|
@@ -502,10 +507,10 @@ describe('dbTablesOperations.generateKnexMigrationDiff', () => {
|
|
|
502
507
|
idColumn: 'id'
|
|
503
508
|
})
|
|
504
509
|
const desiredSchema = makeTableSchema({
|
|
505
|
-
workspaceId: { type: 'id', required: true
|
|
506
|
-
userId: { type: 'id', required: true
|
|
510
|
+
workspaceId: { type: 'id', required: true },
|
|
511
|
+
userId: { type: 'id', required: true },
|
|
507
512
|
role: { type: 'string', enum: ['owner', 'member'], required: true, defaultTo: 'member' },
|
|
508
|
-
noteCount: { type: 'number', defaultTo: 0
|
|
513
|
+
noteCount: { type: 'number', defaultTo: 0 }
|
|
509
514
|
}, {
|
|
510
515
|
indexes: [
|
|
511
516
|
{
|
|
@@ -737,8 +742,8 @@ describe('RestApiKnexPlugin migration scope methods', () => {
|
|
|
737
742
|
await api.addResource('memberships', {
|
|
738
743
|
schema: {
|
|
739
744
|
id: { type: 'id' },
|
|
740
|
-
workspaceId: { type: 'id', required: true
|
|
741
|
-
userId: { type: 'id', required: true
|
|
745
|
+
workspaceId: { type: 'id', required: true },
|
|
746
|
+
userId: { type: 'id', required: true },
|
|
742
747
|
role: { type: 'string', enum: ['owner', 'member'], required: true, defaultTo: 'member' }
|
|
743
748
|
},
|
|
744
749
|
indexes: [
|
|
@@ -8,6 +8,20 @@ import {
|
|
|
8
8
|
findFastifyRoute
|
|
9
9
|
} from './helpers/fake-fastify.js'
|
|
10
10
|
|
|
11
|
+
function resolveSchemaNode (rootSchema, schemaNode) {
|
|
12
|
+
if (!schemaNode || !Array.isArray(schemaNode.allOf) || schemaNode.allOf.length !== 1) {
|
|
13
|
+
return schemaNode
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
const ref = schemaNode.allOf[0]?.$ref || ''
|
|
17
|
+
if (!ref.startsWith('#/definitions/')) {
|
|
18
|
+
return schemaNode
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const definitionName = ref.slice('#/definitions/'.length)
|
|
22
|
+
return rootSchema?.definitions?.[definitionName] || schemaNode
|
|
23
|
+
}
|
|
24
|
+
|
|
11
25
|
async function createFastifyConnectorApi () {
|
|
12
26
|
const app = new FakeFastifyApp()
|
|
13
27
|
const api = new Api({
|
|
@@ -70,7 +84,7 @@ describe('Fastify Plugin', () => {
|
|
|
70
84
|
assert.ok(patchRelationshipRoute?.schema?.body, 'PATCH relationship route should have a body schema')
|
|
71
85
|
assert.ok(deleteRelationshipRoute?.schema?.body, 'DELETE relationship route should have a body schema')
|
|
72
86
|
|
|
73
|
-
const postDataSchema = postRoute.schema.body.properties.data
|
|
87
|
+
const postDataSchema = resolveSchemaNode(postRoute.schema.body, postRoute.schema.body.properties.data)
|
|
74
88
|
const postAttributes = postDataSchema.properties.attributes
|
|
75
89
|
|
|
76
90
|
assert.deepEqual(postDataSchema.required, ['type'])
|
|
@@ -78,14 +92,15 @@ describe('Fastify Plugin', () => {
|
|
|
78
92
|
assert.equal(postAttributes.properties.author_id, undefined, 'belongsTo foreign key should not be accepted in attributes')
|
|
79
93
|
assert.equal(postAttributes.properties.internal_summary, undefined, 'computed fields should not be accepted in attributes')
|
|
80
94
|
assert.equal(postAttributes.properties.id, undefined, 'resource ids should not appear in attributes')
|
|
81
|
-
|
|
95
|
+
const postRelationships = resolveSchemaNode(postRoute.schema.body, postDataSchema.properties.relationships)
|
|
96
|
+
assert.ok(postRelationships.properties.author, 'declared belongsTo relationships should be represented in the transport schema')
|
|
82
97
|
|
|
83
|
-
const putDataSchema = putRoute.schema.body.properties.data
|
|
98
|
+
const putDataSchema = resolveSchemaNode(putRoute.schema.body, putRoute.schema.body.properties.data)
|
|
84
99
|
assert.ok(putDataSchema.required.includes('id'), 'PUT body schema should require data.id')
|
|
85
100
|
|
|
86
|
-
const
|
|
87
|
-
assert.ok(Array.isArray(
|
|
88
|
-
assert.deepEqual(
|
|
101
|
+
const patchDataSchemaNode = patchRoute.schema.body.properties.data
|
|
102
|
+
assert.ok(Array.isArray(patchDataSchemaNode.anyOf), 'PATCH body schema should require at least one writable section')
|
|
103
|
+
assert.deepEqual(patchDataSchemaNode.anyOf, [
|
|
89
104
|
{ required: ['attributes'] },
|
|
90
105
|
{ required: ['relationships'] }
|
|
91
106
|
])
|