From fa35810a71099b86162c03c1a0ba488db4439506 Mon Sep 17 00:00:00 2001 From: "@wwwjim" Date: Thu, 16 Jul 2020 23:05:58 -0700 Subject: [PATCH] postgres: sets up database for local auth and adds utility scripts and dotenv --- knexfile.js | 18 +++++++++ node_common/constants.js | 7 +++- node_common/database.js | 11 ++++++ package.json | 7 +++- scripts/drop-database.js | 15 ++++++++ scripts/seed-database.js | 79 +++++++++++++++++++++++++++++++++++++++ scripts/setup-database.js | 15 ++++++++ server.js | 10 +++++ 8 files changed, 159 insertions(+), 3 deletions(-) create mode 100644 knexfile.js create mode 100644 node_common/database.js create mode 100644 scripts/drop-database.js create mode 100644 scripts/seed-database.js create mode 100644 scripts/setup-database.js diff --git a/knexfile.js b/knexfile.js new file mode 100644 index 00000000..ae1664ff --- /dev/null +++ b/knexfile.js @@ -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, + }, + }, +}; diff --git a/node_common/constants.js b/node_common/constants.js index a8c302c6..a7c13d3e 100644 --- a/node_common/constants.js +++ b/node_common/constants.js @@ -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"; diff --git a/node_common/database.js b/node_common/database.js new file mode 100644 index 00000000..1d30774f --- /dev/null +++ b/node_common/database.js @@ -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; diff --git a/package.json b/package.json index a6624bea..14820f69 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/scripts/drop-database.js b/scripts/drop-database.js new file mode 100644 index 00000000..781c1e18 --- /dev/null +++ b/scripts/drop-database.js @@ -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.`); diff --git a/scripts/seed-database.js b/scripts/seed-database.js new file mode 100644 index 00000000..2ce3a0a5 --- /dev/null +++ b/scripts/seed-database.js @@ -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.`); diff --git a/scripts/setup-database.js b/scripts/setup-database.js new file mode 100644 index 00000000..bcbdaebe --- /dev/null +++ b/scripts/setup-database.js @@ -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.`); diff --git a/server.js b/server.js index e7fe3c14..1c166785 100644 --- a/server.js +++ b/server.js @@ -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();