Skip to content

Commit b6e91bb

Browse files
Merge pull request #1 from toru-kurahashi2/add-pg-settings
PostgreSQLの設定ファイルを追加
2 parents 3d6f738 + 7fe227f commit b6e91bb

7 files changed

Lines changed: 493 additions & 2 deletions

File tree

db/knex.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
const environment = "development";
2+
const config = require("../knexfile.js")[environment];
3+
const knex = require("knex")(config);
4+
5+
module.exports = knex;
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* @param { import("knex").Knex } knex
3+
* @returns { Promise<void> }
4+
*/
5+
exports.up = function(knex) {
6+
return knex.schema.hasTable('users').then(function(exists) {
7+
if (!exists) {
8+
return knex.schema.createTable('users', function(t) {
9+
t.increments('id').primary().unsigned().notNullable();
10+
t.string('name').unique();
11+
t.string('password');
12+
});
13+
}else{
14+
return new Error("The table already exists");
15+
}
16+
});
17+
};
18+
19+
/**
20+
* @param { import("knex").Knex } knex
21+
* @returns { Promise<void> }
22+
*/
23+
exports.down = function(knex) {
24+
return knex.schema.hasTable('users').then(function(exists) {
25+
if (exists) {
26+
return knex.schema.dropTable('users');
27+
}
28+
});
29+
};
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* @param { import("knex").Knex } knex
3+
* @returns { Promise<void> }
4+
*/
5+
exports.up = function(knex) {
6+
return knex.schema.hasTable('tasks').then(function(exists) {
7+
if (!exists) {
8+
return knex.schema.createTable('tasks', function(t) {
9+
t.increments('id').primary().unsigned().notNullable();
10+
t.integer('user_id').notNullable().references('id').inTable('users');
11+
t.string('content').notNullable();
12+
});
13+
}else{
14+
return new Error("The table already exists");
15+
}
16+
});
17+
};
18+
19+
/**
20+
* @param { import("knex").Knex } knex
21+
* @returns { Promise<void> }
22+
*/
23+
exports.down = function(knex) {
24+
return knex.schema.hasTable('tasks').then(function(exists) {
25+
if (exists) {
26+
return knex.schema.dropTable('tasks');
27+
}
28+
});
29+
};

db/seeds/test_user_and_task.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/**
2+
* @param { import("knex").Knex } knex
3+
* @returns { Promise<void> }
4+
*/
5+
exports.seed = async function(knex) {
6+
// Deletes ALL existing entries
7+
await knex('users').del();
8+
await knex('tasks').del()
9+
await knex('users').insert([
10+
{id: 1, name: 'user01', password: 'password'}
11+
]);
12+
await knex('tasks').insert([
13+
{id: 1, user_id: 1, content: 'テスト'}
14+
]);
15+
};

knexfile.js

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
// Update with your config settings.
2+
3+
/**
4+
* @type { Object.<string, import("knex").Knex.Config> }
5+
*/
6+
module.exports = {
7+
8+
development: {
9+
client: 'pg',
10+
connection: {
11+
database: 'todo_app',
12+
user: 'root',
13+
password: 'postgres'
14+
},
15+
pool: {
16+
min: 2,
17+
max: 10
18+
},
19+
migrations: {
20+
directory: './db/migrations',
21+
tableName: 'knex_migrations'
22+
},
23+
seeds: {
24+
directory: './db/seeds',
25+
}
26+
},
27+
28+
staging: {
29+
client: 'pg',
30+
connection: {
31+
database: 'todo_app',
32+
user: 'root',
33+
password: 'postgres'
34+
},
35+
pool: {
36+
min: 2,
37+
max: 10
38+
},
39+
migrations: {
40+
directory: './db/migrations',
41+
tableName: 'knex_migrations'
42+
},
43+
seeds: {
44+
directory: './db/seeds',
45+
}
46+
},
47+
48+
production: {
49+
client: 'pg',
50+
connection: {
51+
database: 'todo_app',
52+
user: 'root',
53+
password: 'postgres'
54+
},
55+
pool: {
56+
min: 2,
57+
max: 10
58+
},
59+
migrations: {
60+
directory: './db/migrations',
61+
tableName: 'knex_migrations'
62+
},
63+
seeds: {
64+
directory: './db/seeds',
65+
}
66+
}
67+
68+
};

0 commit comments

Comments
 (0)