Преглед на файлове

:sparkles: first pass at initial db migration | uupdated docs

master
TOJ преди 5 години
родител
ревизия
e19e893372
променени са 6 файла, в които са добавени 108 реда и са изтрити 0 реда
  1. 1
    0
      backend/README.md
  2. 3
    0
      backend/db/knex.js
  3. 64
    0
      backend/db/migrations/20210524105357_create_initial_tables.js
  4. 24
    0
      backend/knexfile.js
  5. 14
    0
      backend/package-lock.json
  6. 2
    0
      backend/package.json

+ 1
- 0
backend/README.md Целия файл

@@ -16,6 +16,7 @@ You will need...
16 16
 1. Install dependencies with `npm install`
17 17
 2. Fill out your database connection details in `./config/dev.js`
18 18
 3. A test database container is provided and can be started with `docker-compose up -d`
19
+4. Populate some basic tables for your database using `npx knex migrate:latest`
19 20
 
20 21
 ## :electric_plug: Run
21 22
 

+ 3
- 0
backend/db/knex.js Целия файл

@@ -0,0 +1,3 @@
1
+const environment = process.env.NODE_ENV || 'development'
2
+const config = require('../knexfile')[environment]
3
+module.exports = require('knex')(config)

+ 64
- 0
backend/db/migrations/20210524105357_create_initial_tables.js Целия файл

@@ -0,0 +1,64 @@
1
+exports.up = function(knex) {
2
+    return knex.schema
3
+        .createTable('users', function(table) {
4
+            table.increments('user_id').primary()
5
+            table.string('user_name', 32).notNullable()
6
+            table.string('user_email', 90).notNullable()
7
+            table.timestamps(false, true) // Precision to the second
8
+        })
9
+        .createTable('accounts', function(table) {
10
+            table.increments('account_id').primary()
11
+            table.integer('user_id').notNullable()
12
+            table.timestamps(false, true) // Precision to the second
13
+        })
14
+        .createTable('logins', function(table) {
15
+            table.increments('login_id').primary()
16
+            table.integer('account_id').notNullable()
17
+            table.string('login_email', 90).notNullable()
18
+            table.string('login_password', 128).notNullable()
19
+            table.timestamps(false, true) // Precision to the second
20
+        })
21
+        .createTable('profiles', function(table) {
22
+            table.increments('profile_id').primary()
23
+            table.integer('account_id').notNullable()
24
+            table.string('profile_name', 128).notNullable()
25
+            table.json('profile_data').notNullable()
26
+            table.timestamps(false, true) // Precision to the second
27
+        })
28
+        .createTable('groupings', function(table) {
29
+            table.increments('grouping_id').primary()
30
+            table.string('grouping_name', 128).notNullable()
31
+            table.string('grouping_type', 128).notNullable() // Don't over normalize
32
+            table.timestamps(false, true) // Precision to the second
33
+        })
34
+        .createTable('memberships', function(table) {
35
+            table.increments('membership_id').primary()
36
+            table.integer('account_id').notNullable() // From
37
+            table.integer('group_id').notNullable() // To
38
+            table.string('membership_type', 128).notNullable() // Don't over normalize
39
+            table.string('membership_role', 128).notNullable() // Don't over normalize
40
+        })
41
+        .createTable('chats', function(table) {
42
+            table.increments('chat_id').primary()
43
+            table.integer('membership_id').notNullable()
44
+            table.timestamps(false, true) // Precision to the second
45
+        })
46
+        .createTable('messages', function(table) {
47
+            table.increments('message_id').primary()
48
+            table.integer('chat_id').notNullable()
49
+            table.integer('message_account_id').notNullable()
50
+            table.string('message', 255).notNullable()
51
+        })
52
+}
53
+
54
+exports.down = function(knex) {
55
+    return knex.schema
56
+        .dropTable('users')
57
+        .dropTable('accounts')
58
+        .dropTable('logins')
59
+        .dropTable('profiles')
60
+        .dropTable('memberships')
61
+        .dropTable('groupings')
62
+        .dropTable('chats')
63
+        .dropTable('messages')
64
+}

+ 24
- 0
backend/knexfile.js Целия файл

@@ -0,0 +1,24 @@
1
+require('dotenv').config()
2
+
3
+module.exports = {
4
+    development: {
5
+        client: 'mysql',
6
+        connection: {
7
+            host: 'localhost',
8
+            port: 3306,
9
+            user: 'root',
10
+            password: 'root',
11
+            database: 'test',
12
+        },
13
+        pool: {
14
+            min: 2,
15
+            max: 10
16
+        },
17
+        migrations: {
18
+            directory: './db/migrations'
19
+        },
20
+        seeds: {
21
+            directory: './db/seeds'
22
+        }
23
+    },
24
+};

+ 14
- 0
backend/package-lock.json Целия файл

@@ -13,6 +13,7 @@
13 13
         "@hapi/inert": "^6.0.3",
14 14
         "@hapi/vision": "^6.0.1",
15 15
         "@hapipal/schwifty": "^6.0.0",
16
+        "dotenv": "^10.0.0",
16 17
         "hapi-swagger": "^14.1.3",
17 18
         "joi": "^17.4.0",
18 19
         "knex": "^0.21.19",
@@ -1125,6 +1126,14 @@
1125 1126
         "node": ">=8"
1126 1127
       }
1127 1128
     },
1129
+    "node_modules/dotenv": {
1130
+      "version": "10.0.0",
1131
+      "resolved": "https://registry.npmjs.org/dotenv/-/dotenv-10.0.0.tgz",
1132
+      "integrity": "sha512-rlBi9d8jpv9Sf1klPjNfFAuWDjKLwTIJJ/VxtoTwIR6hnZxcEOQCZg2oIL3MWBYw5GpUDKOEnND7LXTbIpQ03Q==",
1133
+      "engines": {
1134
+        "node": ">=10"
1135
+      }
1136
+    },
1128 1137
     "node_modules/duplexer3": {
1129 1138
       "version": "0.1.4",
1130 1139
       "resolved": "https://registry.npmjs.org/duplexer3/-/duplexer3-0.1.4.tgz",
@@ -4839,6 +4848,11 @@
4839 4848
         "is-obj": "^2.0.0"
4840 4849
       }
4841 4850
     },
4851
+    "dotenv": {
4852
+      "version": "10.0.0",
4853
+      "resolved": "https://registry.npmjs.org/dotenv/-/dotenv-10.0.0.tgz",
4854
+      "integrity": "sha512-rlBi9d8jpv9Sf1klPjNfFAuWDjKLwTIJJ/VxtoTwIR6hnZxcEOQCZg2oIL3MWBYw5GpUDKOEnND7LXTbIpQ03Q=="
4855
+    },
4842 4856
     "duplexer3": {
4843 4857
       "version": "0.1.4",
4844 4858
       "resolved": "https://registry.npmjs.org/duplexer3/-/duplexer3-0.1.4.tgz",

+ 2
- 0
backend/package.json Целия файл

@@ -5,6 +5,7 @@
5 5
   "main": "index.js",
6 6
   "scripts": {
7 7
     "start": "nodemon src/index.js",
8
+    "migrate": "knex migrate:latest",
8 9
     "test": "echo \"Error: no test specified\" && exit 1"
9 10
   },
10 11
   "author": "TOJ",
@@ -14,6 +15,7 @@
14 15
     "@hapi/inert": "^6.0.3",
15 16
     "@hapi/vision": "^6.0.1",
16 17
     "@hapipal/schwifty": "^6.0.0",
18
+    "dotenv": "^10.0.0",
17 19
     "hapi-swagger": "^14.1.3",
18 20
     "joi": "^17.4.0",
19 21
     "knex": "^0.21.19",

Loading…
Отказ
Запис