first commit

This commit is contained in:
Jeannette Chin
2024-10-21 17:09:40 +01:00
commit 58f45f8947
15 changed files with 1964 additions and 0 deletions

11
db/db.js Normal file
View File

@@ -0,0 +1,11 @@
import knex from "knex";
import { development, production } from "./knexfile.cjs";
const env = process.env.ENVIRONMENT || "development";
console.log(env);
console.log(development);
const db = env === "development" ? knex(development) : knex(production);
export default db;

98
db/knexfile.cjs Normal file
View File

@@ -0,0 +1,98 @@
// Update with your config settings.
const dir = process.cwd();
const env = process.env.ENVIRONMENT || "";
const config =
env === "development"
? require("dotenv").config({ path: dir + "/.env" })
: require("dotenv").config({ path: "../.env" });
//const config = require("dotenv").config({ path: dir + "/.env" });
console.log(config);
/**
* @type { Object.<string, import("knex").Knex.Config> }
*/
exports.development = {
client: "postgresql",
connection: {
database: config.parsed.DEV_DB, // env var: PGDATABASE YOUR UEA username
user: config.parsed.DEV_USER, // env var: PGUSER YOUR UEA username
password: config.parsed.DEV_PASSWORD // env var: PGPASSWORD YOUR UEA password
},
pool: {
min: 2,
max: 10
},
migrations: {
tableName: "knex_migrations"
},
seeds: {
directory: "./seeds"
}
};
exports.production = {
client: "postgresql",
connection: {
host: process.env.DEPLOYBOT_DB_HOST,
database: process.env.DEPLOYBOT_DB_NAME,
user: process.env.DEPLOYBOT_DB_USER,
password: process.env.DEPLOYBOT_DB_PASS,
port: process.env.DEPLOYBOT_DB_PORT
},
pool: {
min: 2,
max: 10
},
migrations: {
tableName: "knex_migrations"
}
};
// module.exports = {
// development: {
// client: "postgresql",
// connection: {
// database: "6057", // env var: PGDATABASE YOUR UEA username
// user: "postgres", // env var: PGUSER YOUR UEA username
// password: "12345" // env var: PGPASSWORD YOUR UEA password
// }
// },
// staging: {
// client: "postgresql",
// connection: {
// database: "my_db",
// user: "username",
// password: "password"
// },
// pool: {
// min: 2,
// max: 10
// },
// migrations: {
// tableName: "knex_migrations"
// }
// },
// production: {
// client: "postgresql",
// connection: {
// database: "my_db",
// user: "username",
// password: "password"
// },
// pool: {
// min: 2,
// max: 10
// },
// migrations: {
// tableName: "knex_migrations"
// }
// }
// };

View File

@@ -0,0 +1,21 @@
/**
* @param { import("knex").Knex } knex
* @returns { Promise<void> }
*/
exports.up = function (knex) {
return knex.schema.createTable("subscribers", (table) => {
table.increments("id");
table.string("firstname", 50).notNullable().defaultTo("");
table.string("lastname", 50).notNullable().defaultTo("");
table.string("email", 100).notNullable().defaultTo("");
table.timestamps(true, true);
});
};
/**
* @param { import("knex").Knex } knex
* @returns { Promise<void> }
*/
exports.down = function (knex) {
return knex.schema.dropTable("subscribers");
};

View File

@@ -0,0 +1,21 @@
/**
* @param { import("knex").Knex } knex
* @returns { Promise<void> }
*/
exports.up = function (knex) {
return knex.schema.createTable("users", (table) => {
table.increments("id");
table.string("firstname", 50).notNullable().defaultTo("");
table.string("lastname", 50).notNullable().defaultTo("");
table.string("email", 100).notNullable().defaultTo("");
table.timestamps(true, true);
});
};
/**
* @param { import("knex").Knex } knex
* @returns { Promise<void> }
*/
exports.down = function (knex) {
return knex.schema.dropTable("users");
};

View File

@@ -0,0 +1,15 @@
/**
* @param { import("knex").Knex } knex
* @returns { Promise<void> }
*/
exports.up = function(knex) {
};
/**
* @param { import("knex").Knex } knex
* @returns { Promise<void> }
*/
exports.down = function(knex) {
};

View File

@@ -0,0 +1,13 @@
/**
* @param { import("knex").Knex } knex
* @returns { Promise<void> }
*/
exports.seed = async function (knex) {
// Deletes ALL existing entries
// Deletes ALL existing entries
await knex("subscribers").del();
await knex("subscribers").insert([
{ firstname: "Jeannette", lastname: "Chin", email: "jc@jc.com" },
{ firstname: "Jo", lastname: "Blog", email: "jo@blog.com" }
]);
};