Repository navigation
Expand file tree
/
Copy pathquickTest.js
More file actions
120 lines (105 loc) · 4.16 KB
/
Copy pathquickTest.js
File metadata and controls
120 lines (105 loc) · 4.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import { RestApiPlugin, RestApiKnexPlugin, ExpressPlugin, JsonRestApi } from './index.js'
import knexLib from 'knex'
import util from 'util'
import express from 'express'
// Utility used throughout this guide
const inspect = (obj) => util.inspect(obj, { depth: 8 })
// Create a Knex instance connected to SQLite in-memory database
const knex = knexLib({
client: 'better-sqlite3',
connection: {
filename: ':memory:'
},
useNullAsDefault: true
})
// Create API instance
const api = new JsonRestApi({ name: 'book-catalog-api' })
// Install plugins
await api.use(RestApiPlugin, { format: 'plain', returning: 'full' })
await api.use(RestApiKnexPlugin, { knex })
await api.use(ExpressPlugin, { mountPath: '/api' })
// Define publishers resource
await api.addResource('publishers', {
schema: {
name: { type: 'string', required: true, max: 255, search: true, indexed: true },
},
relationships: {
// A publisher has many authors
authors: { type: 'hasMany', target: 'authors', foreignKey: 'publisher_id' },
},
searchSchema: { // Adding search schema for publishers
name: { type: 'string', filterOperator: 'like' }
}
})
await api.resources.publishers.createKnexTable()
// Define authors resource, which belongs to a publisher
await api.addResource('authors', {
schema: {
name: { type: 'string', required: true, max: 100, search: true },
surname: { type: 'string', required: true, max: 100, search: true },
publisher_id: { type: 'id', belongsTo: 'publishers', as: 'publisher', nullable: true }
},
searchSchema: { // Adding search schema for authors
name: { type: 'string', filterOperator: 'like' },
surname: { type: 'string', filterOperator: 'like' },
publisher: { type: 'id', actualField: 'publisher_id', nullable: true },
publisherName: { type: 'string', actualField: 'publishers.name', filterOperator: 'like' } // Cross-table search
}
})
await api.resources.authors.createKnexTable()
// Plain resource values belong in the data argument.
const penguinResult = await api.resources.publishers.post({ format: 'plain', data: { name: 'Penguin Random House' } })
console.log('Created publisher:', inspect(penguinResult))
// Calls can use the plugin defaults.
const harperResult = await api.resources.publishers.post({
data: {
name: 'HarperCollins'
}
})
console.log('Created second publisher:', inspect(harperResult))
// The document argument selects input; format independently selects output.
const oxfordResult = await api.resources.publishers.post({
document: {
data: {
type: 'publishers',
attributes: {
name: 'Oxford University Press'
}
}
},
format: 'jsonapi'
})
console.log('JSON:API response:', inspect(oxfordResult))
// Plain relationship input uses the relationship name.
const authorResult = await api.resources.authors.post({ format: 'plain', data: { name: 'George', surname: 'Orwell', publisher: penguinResult.id } })
console.log('Created author:', inspect(authorResult))
// Get all publishers
const allPublishers = await api.resources.publishers.query({})
console.log('All publishers:', inspect(allPublishers))
// Get publisher with included authors
const publisherWithAuthors = await api.resources.publishers.get({
id: penguinResult.id,
queryParams: { include: ['authors'] }
})
console.log('Publisher with authors:', inspect(publisherWithAuthors))
// Search authors by name
const searchResult = await api.resources.authors.query({
queryParams: { filters: { name: 'George' } }
})
console.log('Search results:', inspect(searchResult))
// Update an author
const updateResult = await api.resources.authors.patch({ id: authorResult.id, format: 'plain', data: { surname: 'Orwell (Eric Blair)' } })
console.log('Updated author:', inspect(updateResult))
const app = express()
api.http.express.mount(app)
const server = app.listen(Number(process.env.PORT || 3000), '127.0.0.1', () => {
console.log(`API available at http://127.0.0.1:${server.address().port}/api`)
})
server.on('error', async (error) => {
console.error('Failed to start server:', error)
await knex.destroy()
process.exitCode = 1
})
const close = () => { server.close(() => { knex.destroy() }) }
process.once('SIGINT', close)
process.once('SIGTERM', close)