postgres: sets up database for local auth and adds utility scripts and dotenv

This commit is contained in:
@wwwjim 2020-07-16 23:05:58 -07:00
parent afcf54c651
commit fa35810a71
8 changed files with 159 additions and 3 deletions

18
knexfile.js Normal file
View File

@ -0,0 +1,18 @@
if (process.env.NODE_ENV !== "www") {
console.log("[ prototype ] loading dotenv");
require("dotenv").config();
}
module.exports = {
development: {
client: "pg",
connection: {
ssl: true,
port: 5432,
host: process.env.POSTGRES_HOSTNAME,
database: process.env.POSTGRES_DATABASE,
user: process.env.POSTGRES_ADMIN_USERNAME,
password: process.env.POSTGRES_ADMIN_PASSWORD,
},
},
};

View File

@ -1,9 +1,12 @@
const path = require("path");
import path from "path";
export const POLLING_RATE = 5000;
export const POWERGATE_HOST = "http://pow.slate.textile.io:6002";
export const AVATAR_STORAGE_URL = path.join(__dirname, "../public/static/system/");
export const AVATAR_STORAGE_URL = path.join(
__dirname,
"../public/static/system/"
);
export const FILE_STORAGE_URL = path.join(__dirname, "../public/static/files/");
export const GITHUB_URL = "https://github.com/filecoin-project/filecoin-client";

11
node_common/database.js Normal file
View File

@ -0,0 +1,11 @@
if (process.env.NODE_ENV !== "production") {
require("dotenv").config();
}
import configs from "~/knexfile";
import knex from "knex";
const envConfig = configs["development"];
const db = knex(envConfig);
module.exports = db;

View File

@ -15,7 +15,10 @@
"build-system": "rollup -c",
"electron": "electron ./electron/main.js ",
"electron-pack": "electron-builder --dir",
"electron-dist": "electron-builder"
"electron-dist": "electron-builder",
"www-setup-database": "NODE_TLS_REJECT_UNAUTHORIZED=0 node ./scripts setup-database",
"www-seed-database": "NODE_TLS_REJECT_UNAUTHORIZED=0 node ./scripts seed-database",
"www-drop-database": "NODE_TLS_REJECT_UNAUTHORIZED=0 node ./scripts drop-database"
},
"build": {
"appId": "com.slate",
@ -51,8 +54,10 @@
"formidable": "^1.2.2",
"fs-extra": "^9.0.1",
"isomorphic-fetch": "^2.2.1",
"knex": "^0.20.10",
"moment": "^2.27.0",
"next": "^9.4.4",
"pg": "^8.3.0",
"prismjs": "^1.20.0",
"react": "^16.12.0",
"react-dom": "^16.12.0",

15
scripts/drop-database.js Normal file
View File

@ -0,0 +1,15 @@
import configs from "~/knexfile";
import knex from "knex";
const envConfig = configs["development"];
console.log(`SETUP: database`, envConfig);
const db = knex(envConfig);
console.log(`RUNNING: drop-database.js`);
Promise.all([db.schema.dropTable("users"), db.schema.dropTable("slates")]);
console.log(`FINISHED: drop-database.js`);
console.log(` CTRL +C to return to terminal.`);

79
scripts/seed-database.js Normal file
View File

@ -0,0 +1,79 @@
import configs from "~/knexfile";
import knex from "knex";
const envConfig = configs["development"];
console.log(`SETUP: database`, envConfig);
const db = knex(envConfig);
console.log(`RUNNING: seed-database.js`);
// --------------------------
// SCRIPTS
// --------------------------
const createUsersTable = db.schema.createTable("users", function(table) {
table
.uuid("id")
.primary()
.unique()
.notNullable()
.defaultTo(db.raw("uuid_generate_v4()"));
table
.timestamp("created_at")
.notNullable()
.defaultTo(db.raw("now()"));
table
.timestamp("updated_at")
.notNullable()
.defaultTo(db.raw("now()"));
table
.string("username")
.unique()
.nullable();
table
.string("email")
.unique()
.notNullable();
table.string("password").nullable();
table.string("salt").nullable();
table.jsonb("data").nullable();
});
const createSlatesTable = db.schema.createTable("slates", function(table) {
table
.uuid("id")
.primary()
.unique()
.notNullable()
.defaultTo(db.raw("uuid_generate_v4()"));
table
.timestamp("created_at")
.notNullable()
.defaultTo(db.raw("now()"));
table
.timestamp("updated_at")
.notNullable()
.defaultTo(db.raw("now()"));
table.timestamp("published_at").nullable();
table.jsonb("data").nullable();
});
// --------------------------
// RUN
// --------------------------
Promise.all([createUsersTable, createSlatesTable]);
console.log(`FINISHED: seed-database.js`);
console.log(` CTRL +C to return to terminal.`);

15
scripts/setup-database.js Normal file
View File

@ -0,0 +1,15 @@
import configs from "~/knexfile";
import knex from "knex";
const envConfig = configs["development"];
console.log(`SETUP: database`, envConfig);
const db = knex(envConfig);
console.log(`RUNNING: setup-database.js`);
Promise.all([db.raw('CREATE EXTENSION IF NOT EXISTS "uuid-ossp"')]);
console.log(`FINISHED: setup-database.js`);
console.log(` CTRL +C to return to terminal.`);

View File

@ -1,9 +1,16 @@
if (process.env.NODE_ENV !== "www") {
console.log("[ prototype ] loading dotenv");
require("dotenv").config();
}
import * as Middleware from "./common/middleware";
import * as Strings from "./common/strings";
import * as Utilities from "./node_common/utilities";
import * as Constants from "./node_common/constants";
import * as Database from "./node_common/database";
import { createPow, ffs } from "@textile/powergate-client";
// NOTE(jim):
// https://github.com/textileio/js-powergate-client
const PowerGate = createPow({ host: Constants.POWERGATE_HOST });
@ -437,6 +444,9 @@ app.prepare().then(async () => {
console.log(`[ prototype ] client: http://localhost:${port}`);
console.log(`[ prototype ] constants:`, Constants);
console.log(
`[ prototype ] .env postgres hostname: ${process.env.POSTGRES_HOSTNAME}`
);
if (!productionWeb) {
await setIntervalViewerUpdatesUnsafe();