Skip to content

Commit

Permalink
* Made more progress on Knex migration-system setup.
Browse files Browse the repository at this point in the history
Backup before switching from snowpack to tsc for Packages/server. (this issue keeps happening when I try to compile the server code: FredKSchott/snowpack#3468)
  • Loading branch information
Venryx committed Jun 22, 2021
1 parent 2ad4c40 commit 8faa47f
Show file tree
Hide file tree
Showing 15 changed files with 289 additions and 72 deletions.
28 changes: 3 additions & 25 deletions Packages/client/Scripts/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,19 @@
"compilerOptions": {
// general
"watch": true,
//"outDir": "Source_JS",
"forceConsistentCasingInFileNames": true, // creates duplicates in import-helper plugins otherwise

// modules/paths
"module": "ES2015",
//"module": "commonjs", // needed, since ts-node does not yet support imports from es2015-modules
"moduleResolution": "node",
"rootDir": ".",
"baseUrl": ".",
//"baseUrl": ".", // we need it at root so we can apply path-overrides, even when requester is outside of Source folder
"paths": { // ("paths" is relative to "baseUrl")
// remove @types/node and such (conflicts with typescript web definitions)
"@types/node": ["../Typings/empty.d.ts"],
// remove @types/react/global.d.ts, as it breaks the auto-import-finder for react-vcomponents/Text
"react/global.d.ts": ["../Typings/empty.d.ts"],
"react-vextensions/node_modules/react/global.d.ts": ["../Typings/empty.d.ts"],
"react-vcomponents/node_modules/react/global.d.ts": ["../Typings/empty.d.ts"]
//"WebpackConfig": ["./Build/WebpackConfig.ts"]
},
//"rootDir": ".",
//"baseUrl": ".",

"resolveJsonModule": true,

// compiler options
"target": "ES2015",
/*"target": "esnext",
"lib": [
"es5",
"es6",
"ESNext",
"dom"
],*/
"jsx": "react",
"noImplicitAny": false,
"alwaysStrict": true, // alternative: "@babel/plugin-transform-strict-mode"; went with this since doesn't require another npm package
Expand All @@ -45,9 +27,5 @@
"../Typings/**/*.d.ts",
"../Scripts/**/*.ts",
"../Scripts/**/*.tsx",
"Config.ts",
"Build/WebpackConfig.ts",
//"../node_modules/js-vextensions/**/*.ts",
],
"compileOnSave": true
]
}
2 changes: 1 addition & 1 deletion Packages/common/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
"main": "./Source/index.ts",
"module": "./Source/index.ts",
"scripts": {
"start": "nps"
"start": "cd ../.. && npx nps"
}
}
4 changes: 4 additions & 0 deletions Packages/server/Knex/@MigrationTemplate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import {Knex} from "knex";

export async function up(knex: Knex) {
}
28 changes: 28 additions & 0 deletions Packages/server/Knex/Migrations/20210622053410_Test1.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import {Knex} from "knex";

// doesn't work because this file's code runs *after* knex checks for the db's existence
/*async function CreateDBIfNotExists(name: string) {
const config = require("../knexfile");
let knex = new Knex.Client({
client: "postgresql",
connection: {
...config.development.connection,
//host: "localhost",
database: null,
},
})
// Lets create our database if it does not exist
await knex.raw("CREATE DATABASE IF NOT EXISTS ??", name)
}*/

export async function up(knex: Knex) {
console.log("Starting");
//CreateDBIfNotExists("debate-map");
knex.schema.createTable("users_draft", t=>{
t.text("id");
t.text("displayName");
t.text("photoURL");
t.bigInteger("joinDate");
});
}
36 changes: 36 additions & 0 deletions Packages/server/Knex/knexfile.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
require("dotenv").config({path: "../../.env"});

module.exports = {
development: {
client: "postgresql",
connection: {
database: "debate-map",
user: process.env.LOCALDB_USERNAME,
password: process.env.LOCALDB_PASSWORD,
},
pool: {
min: 2,
max: 10,
},
migrations: {
tableName: "knex_migrations",
directory: "./Migrations",
},
acquireConnectionTimeout: 3000,
},
/*production: {
client: "postgresql",
connection: {
database: "debate-map",
user: process.env.LOCALDB_USERNAME,
password: process.env.LOCALDB_PASSWORD,
},
pool: {
min: 2,
max: 10,
},
migrations: {
tableName: "knex_migrations",
}
},*/
};
3 changes: 3 additions & 0 deletions Packages/server/Knex/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"type": "commonjs"
}
26 changes: 22 additions & 4 deletions Packages/server/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,32 @@ max_replication_slots = 10

4) Create a Postgres database for this project, by running: `createdb debate-map`

5) Init `debate-map` db in PostgreSQL, by running `yarn start initDB`.

6) Set environment variables for the database's username (PGUSER) and password (PGPASSWORD).
5) Init `debate-map` db in PostgreSQL, by running `yarn start server.initDB`.

### Remote server

TODO

### Environment variables

Create a `.env` file in the repo root, following this template:
```
# GCP web-client key/id
CLIENT_ID=XXX
CLIENT_SECRET=XXX
# local database
LOCALDB_USERNAME=XXX
LOCALDB_PASSWORD=XXX
```

## Editing + running

See here: <https://github.com/debate-map/app#editing--running>
See here: <https://github.com/debate-map/app#editing--running>

## Database migrations

See here for overview: <https://github.com/Venryx/web-vcore/tree/master/Docs/DatabaseMigrations.md>

Actions:
* To create a new migration, run: `npx knex --knexfile Knex/knexfile.ts migrate:make MIGRATION_NAME`
19 changes: 0 additions & 19 deletions Packages/server/Scripts/InitDB.sql

This file was deleted.

52 changes: 52 additions & 0 deletions Packages/server/Scripts/InitDB.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import Knex from "knex";
//import config from "../Knex/knexfile";
import {createRequire} from "module";
const require = createRequire(import.meta.url);

const dbName = "debate-map";

const config = require("../Knex/knexfile");
async function CreateDBIfNotExists(name: string) {
console.log("Test1:");
/*let knex = new Knex.Client({
//client: "postgresql",
client: "pg",
//connection: `postgres://${process.env.PGUSER}:${process.env.PGPASSWORD}@localhost:5432/debate-map`,
//connection: `postgres://${config.development.connection.user}:${config.development.connection.password}@localhost:5432/debate-map`,
//connection: `postgres://${config.development.connection.user}:${config.development.connection.password}@localhost:5432`,
connection: {
...config.development.connection,
//host: "localhost", port: 5432,
host: "127.0.0.1", //port: 5432,
//host: "localhost:5432",
database: null,
},
});*/
const config_final = JSON.parse(JSON.stringify(config.development));
delete config_final.connection.database;
let knex = new Knex.Client(config_final);

console.log("Creating");
await knex.raw("CREATE DATABASE IF NOT EXISTS ??", name);
console.log("Created");
}

async function Main() {
await CreateDBIfNotExists(dbName);
console.log("Created2");

// now that our database confirmed to exist, create another knex object (with db-name specified) so we can run our migrations
const knex = new Knex.Client({
//client: "postgresql",
client: "pg",
connection: {
...config.development.connection,
//host: "localhost",
database: dbName,
},
});

await knex["migrate"].latest();
}

Main().catch(console.log).then(()=>process.exit());
31 changes: 31 additions & 0 deletions Packages/server/Scripts/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"compilerOptions": {
// general
"watch": true,
"forceConsistentCasingInFileNames": true, // creates duplicates in import-helper plugins otherwise

// modules/paths
"module": "ESNext",
//"module": "commonjs", // needed, since ts-node does not yet support imports from es2015-modules
"moduleResolution": "node",
//"rootDir": ".",
//"baseUrl": ".",

"resolveJsonModule": true,

// compiler options
"target": "ES2015",
"jsx": "react",
"noImplicitAny": false,
"alwaysStrict": true, // alternative: "@babel/plugin-transform-strict-mode"; went with this since doesn't require another npm package

"experimentalDecorators": true,
"allowSyntheticDefaultImports": true,
"esModuleInterop": true
},
"include": [
"../Typings/**/*.d.ts",
"../Scripts/**/*.ts",
"../Scripts/**/*.tsx",
]
}
3 changes: 1 addition & 2 deletions Packages/server/Source/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ const postgraphile = postgraphile_["postgraphile"] as typeof import("postgraphil
const makePluginHook = postgraphile_["makePluginHook"] as typeof import("postgraphile").makePluginHook;
import {GeneratePatchesPlugin} from "@pg-lq/postgraphile-plugin";
import {Pool, PoolClient} from "pg";

import {createRequire} from "module";
import {AuthenticationPlugin} from "./Mutations/Authentication";
import {SetUpAuthHandling} from "./AuthHandling";
Expand All @@ -22,7 +21,7 @@ export const variant = launchOpts.variant;

const app = express();

const dbURL = process.env.DATABASE_URL || `postgres://${process.env.PGUSER}:${process.env.PGPASSWORD}@localhost:5432/game-dive`;
const dbURL = process.env.DATABASE_URL || `postgres://${process.env.PGUSER}:${process.env.PGPASSWORD}@localhost:5432/debate-map`;
const dbPort = process.env.PORT || 3105 as number;

const pluginHook = makePluginHook([
Expand Down
3 changes: 2 additions & 1 deletion Packages/server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"type": "module",
"private": true,
"scripts": {
"start": "nps",
"start": "cd ../.. && npx nps",
"preinstall": "npx only-allow yarn"
},
"devDependencies": {
Expand All @@ -25,6 +25,7 @@
"cookie-session": "^1.4.0",
"dotenv": "^10.0.0",
"express": "^4.17.1",
"knex": "^0.95.6",
"passport": "^0.4.1",
"passport-google-oauth20": "^2.0.0",
"postgraphile": "^4.11.0",
Expand Down
5 changes: 4 additions & 1 deletion Packages/server/snowpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const config = {
},*/
exclude: [
`./Scripts/**/*`,
`./Knex/**/*`,
`./Build/**/*`,
`./*.json`,
`./*.js`
Expand All @@ -40,6 +41,7 @@ const config = {
//"fast-json-patch",
//"postgraphile/build/postgraphile/http/mapAsyncIterator".replace(/\//g, "\\"),
//"iterall",
//"util",
],
external: builtinModules.concat(
"express", "postgraphile", "commander",
Expand All @@ -49,8 +51,9 @@ const config = {
"@n1ru4l/graphql-live-query-patch",
"iterall",
"postgraphile/build/postgraphile/http/mapAsyncIterator",
"postgraphile/build/postgraphile/http/mapAsyncIterator.js"
"postgraphile/build/postgraphile/http/mapAsyncIterator.js",
),
polyfillNode: true,
},
devOptions: {
open: "none",
Expand Down
Loading

0 comments on commit 8faa47f

Please sign in to comment.