|
|
@@ -1,14 +1,13 @@
|
|
1
|
1
|
import Hapi from '@hapi/hapi'
|
|
2
|
|
-import Sequelize from 'sequelize'
|
|
|
2
|
+import Schwifty from '@hapipal/schwifty'
|
|
3
|
3
|
|
|
4
|
|
-import hapiSequelizejs from 'hapi-sequelizejs'
|
|
|
4
|
+import { config } from '../config/dev.js'
|
|
5
|
5
|
|
|
6
|
6
|
import { routes } from './routes/index.js'
|
|
7
|
7
|
import { plugins } from './plugins/index.js'
|
|
|
8
|
+import { docs } from './docs.js'
|
|
8
|
9
|
|
|
9
|
|
-import docs from './docs.js'
|
|
10
|
|
-
|
|
11
|
|
-import { config } from '../config/dev.js'
|
|
|
10
|
+import Joi from 'joi'
|
|
12
|
11
|
|
|
13
|
12
|
const server = Hapi.server({
|
|
14
|
13
|
port: config.port,
|
|
|
@@ -18,21 +17,11 @@ const server = Hapi.server({
|
|
18
|
17
|
}
|
|
19
|
18
|
})
|
|
20
|
19
|
|
|
21
|
|
-const dbOpts = {
|
|
22
|
|
- name: config.db, // identifier
|
|
23
|
|
- models: ['./models/**/*.js'], // paths/globs to model files
|
|
24
|
|
- ignoredModels: ['./models/**/*.js'], // OPTIONAL: paths/globs to ignore files
|
|
25
|
|
- sequelize: new Sequelize(
|
|
26
|
|
- config.db, // db name
|
|
27
|
|
- 'root', // U
|
|
28
|
|
- 'root', // P
|
|
29
|
|
- {
|
|
30
|
|
- host: config.host,
|
|
31
|
|
- dialect: config.dbDialect
|
|
32
|
|
- }
|
|
33
|
|
- ),
|
|
34
|
|
- sync: true, // sync models - default false
|
|
35
|
|
- forceSync: false, // force sync (drops tables) - default false
|
|
|
20
|
+const dbConnection = {
|
|
|
21
|
+ host : config.host,
|
|
|
22
|
+ user : 'root',
|
|
|
23
|
+ password : 'root',
|
|
|
24
|
+ database : config.db
|
|
36
|
25
|
}
|
|
37
|
26
|
|
|
38
|
27
|
const init = async () => {
|
|
|
@@ -47,18 +36,51 @@ const init = async () => {
|
|
47
|
36
|
|
|
48
|
37
|
await server.register(docs)
|
|
49
|
38
|
|
|
50
|
|
- /** Register the Sequlize instance as a plugin */
|
|
51
|
|
- await server.register({ plugin: hapiSequelizejs, options: dbOpts })
|
|
52
|
|
-
|
|
53
|
|
- /** Uncomment to print the Sequlize instance */
|
|
54
|
|
- // console.log(server.plugins['hapi-sequelizejs'][config.db])
|
|
55
|
|
-
|
|
56
|
|
- /** Double-check the database connection */
|
|
|
39
|
+ /** Register the database connection */
|
|
57
|
40
|
console.log(`\n[SIIMEE API] Database: ${config.db} | Attempting to connect on port ${config.dbPort}...`)
|
|
58
|
|
- await server.plugins['hapi-sequelizejs'][config.db].sequelize.authenticate()
|
|
|
41
|
+ await server.register({
|
|
|
42
|
+ plugin: Schwifty,
|
|
|
43
|
+ options: {
|
|
|
44
|
+ knex: { client: config.dbDialect, connection: dbConnection }
|
|
|
45
|
+ }
|
|
|
46
|
+ })
|
|
59
|
47
|
console.log(`[SIIMEE API] Database: ${config.db} | Connection has been established on port ${config.dbPort}!`)
|
|
60
|
48
|
|
|
|
49
|
+
|
|
|
50
|
+
|
|
|
51
|
+
|
|
|
52
|
+
|
|
|
53
|
+ server.registerModel(
|
|
|
54
|
+ class Dog extends Schwifty.Model {
|
|
|
55
|
+ static tableName = 'Dog'
|
|
|
56
|
+ static joiSchema = Joi.object({
|
|
|
57
|
+ id: Joi.number(),
|
|
|
58
|
+ name: Joi.string()
|
|
|
59
|
+ })
|
|
|
60
|
+ }
|
|
|
61
|
+ )
|
|
|
62
|
+
|
|
|
63
|
+
|
|
61
|
64
|
/** Lift Off! */
|
|
|
65
|
+ await server.initialize()
|
|
|
66
|
+
|
|
|
67
|
+ const knex = server.knex()
|
|
|
68
|
+ // console.log(knex)
|
|
|
69
|
+ await knex.schema.createTable('Dog', (table) => {
|
|
|
70
|
+ table.increments('id').primary()
|
|
|
71
|
+ table.string('name')
|
|
|
72
|
+ })
|
|
|
73
|
+
|
|
|
74
|
+ // ... then add some records ...
|
|
|
75
|
+
|
|
|
76
|
+ const { Dog } = server.models()
|
|
|
77
|
+
|
|
|
78
|
+ await Promise.all([
|
|
|
79
|
+ Dog.query().insert({ name: 'Guinness' }),
|
|
|
80
|
+ Dog.query().insert({ name: 'Sully' }),
|
|
|
81
|
+ Dog.query().insert({ name: 'Ren' })
|
|
|
82
|
+ ])
|
|
|
83
|
+
|
|
62
|
84
|
await server.start()
|
|
63
|
85
|
console.log('\n[SIIMEE API] Server running on %s', server.info.uri)
|
|
64
|
86
|
}
|