From 256cce952ee52cdf2c12b01ec6f3234b541c62a9 Mon Sep 17 00:00:00 2001 From: Jake Runzer Date: Tue, 24 Nov 2020 15:23:23 -0700 Subject: [PATCH] Cleanup examples (#16) - Remove serverless example (it was basically dup of with-postgres) - Add plugin (e.g. postgres) to example name - Update all javascript examples and make sure they work - Cleanup a bit of the ui in nextjs example --- .prettierrc | 6 + javascript/serverless/README.md | 23 -- javascript/serverless/package.json | 19 -- javascript/serverless/src/index.js | 12 - javascript/serverless/yarn.lock | 133 --------- javascript/with-next-js/README.md | 30 --- javascript/with-next-js/pages/index.js | 84 ------ .../.gitignore | 0 javascript/with-nextjs-postgres/README.md | 14 + .../package.json | 3 +- .../pages/_app.js | 0 .../pages/api/time.js} | 7 +- .../with-nextjs-postgres/pages/index.js | 27 ++ .../public/favicon.ico | Bin .../public/vercel.svg | 0 .../styles/Home.module.css | 0 .../styles/globals.css | 0 .../yarn.lock | 12 + javascript/with-postgres/README.md | 13 + javascript/with-postgres/index.js | 14 +- javascript/with-prisma/README.md | 37 ++- javascript/with-prisma/package.json | 1 + .../migrations/20200701145156-init/README.md | 59 ---- .../20200701145156-init/schema.prisma | 27 -- .../migrations/20200701145156-init/steps.json | 252 ------------------ .../migrations/20201124133307/README.md | 41 +++ .../migrations/20201124133307/schema.prisma | 17 ++ .../migrations/20201124133307/steps.json | 113 ++++++++ .../prisma/migrations/migrate.lock | 2 +- javascript/with-prisma/prisma/schema.prisma | 16 +- javascript/with-prisma/src/index.ts | 38 ++- javascript/with-prisma/yarn.lock | 5 + 32 files changed, 321 insertions(+), 684 deletions(-) create mode 100644 .prettierrc delete mode 100644 javascript/serverless/README.md delete mode 100644 javascript/serverless/package.json delete mode 100644 javascript/serverless/src/index.js delete mode 100644 javascript/serverless/yarn.lock delete mode 100644 javascript/with-next-js/README.md delete mode 100644 javascript/with-next-js/pages/index.js rename javascript/{with-next-js => with-nextjs-postgres}/.gitignore (100%) create mode 100644 javascript/with-nextjs-postgres/README.md rename javascript/{with-next-js => with-nextjs-postgres}/package.json (85%) rename javascript/{with-next-js => with-nextjs-postgres}/pages/_app.js (100%) rename javascript/{with-next-js/pages/api/data.js => with-nextjs-postgres/pages/api/time.js} (72%) create mode 100644 javascript/with-nextjs-postgres/pages/index.js rename javascript/{with-next-js => with-nextjs-postgres}/public/favicon.ico (100%) rename javascript/{with-next-js => with-nextjs-postgres}/public/vercel.svg (100%) rename javascript/{with-next-js => with-nextjs-postgres}/styles/Home.module.css (100%) rename javascript/{with-next-js => with-nextjs-postgres}/styles/globals.css (100%) rename javascript/{with-next-js => with-nextjs-postgres}/yarn.lock (99%) create mode 100644 javascript/with-postgres/README.md delete mode 100644 javascript/with-prisma/prisma/migrations/20200701145156-init/README.md delete mode 100644 javascript/with-prisma/prisma/migrations/20200701145156-init/schema.prisma delete mode 100644 javascript/with-prisma/prisma/migrations/20200701145156-init/steps.json create mode 100644 javascript/with-prisma/prisma/migrations/20201124133307/README.md create mode 100644 javascript/with-prisma/prisma/migrations/20201124133307/schema.prisma create mode 100644 javascript/with-prisma/prisma/migrations/20201124133307/steps.json diff --git a/.prettierrc b/.prettierrc new file mode 100644 index 0000000..9aa793a --- /dev/null +++ b/.prettierrc @@ -0,0 +1,6 @@ +{ + "bracketSpacing": true, + "singleQuote": false, + "trailingComma": "all", + "arrowParens": "avoid" +} diff --git a/javascript/serverless/README.md b/javascript/serverless/README.md deleted file mode 100644 index 2c01ca3..0000000 --- a/javascript/serverless/README.md +++ /dev/null @@ -1,23 +0,0 @@ -# serverless - -This project is intended to be deployed on Vercel, Netlify, etc - -It makes use of a railway build command which should only be used in production, during the build, on those sites - -It is NOT intended for local use, and requires a production access token - -If you are not planning to deploy to Vercel, Netlify, etc, please just use any of the regular library templates since they'll have no prioriatary railway library lockin' (And will work with all your Postgres/Mongo/etc libraries out of the box) - -## Usage - -- Install dependencies `yarn install` -- Init a railway project `yarn railway init` -- Open the project in the Railway dashboard `yarn railway open` -- Install the Postgres plugin from the Railway dashboard -- Run the code `yarn start` - -The time should be printed - -So easy! - -## Configuring your own project diff --git a/javascript/serverless/package.json b/javascript/serverless/package.json deleted file mode 100644 index 4112d83..0000000 --- a/javascript/serverless/package.json +++ /dev/null @@ -1,19 +0,0 @@ -{ - "name": "railway-testing", - "version": "1.0.0", - "main": "index.js", - "author": "Jake Runzer", - "license": "MIT", - "scripts": { - "start": "railway run node src/index.js", - "build": "railway build", - "open": "railway open", - "help": "railway --help", - "init": "railway init" - }, - "dependencies": { - "global": "^4.4.0", - "pg": "^8.4.0", - "tslib": "^2.0.0" - } -} diff --git a/javascript/serverless/src/index.js b/javascript/serverless/src/index.js deleted file mode 100644 index 25de207..0000000 --- a/javascript/serverless/src/index.js +++ /dev/null @@ -1,12 +0,0 @@ -const pg = require("pg"); -const client = new pg.Pool(); - -client.query("SELECT now()", (err, res) => { - if (err) { - console.log(err); - } - - console.log(res.rows); -}); - -client.close(); diff --git a/javascript/serverless/yarn.lock b/javascript/serverless/yarn.lock deleted file mode 100644 index 7a85226..0000000 --- a/javascript/serverless/yarn.lock +++ /dev/null @@ -1,133 +0,0 @@ -# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY. -# yarn lockfile v1 - - -buffer-writer@2.0.0: - version "2.0.0" - resolved "https://registry.yarnpkg.com/buffer-writer/-/buffer-writer-2.0.0.tgz#ce7eb81a38f7829db09c873f2fbb792c0c98ec04" - integrity sha512-a7ZpuTZU1TRtnwyCNW3I5dc0wWNC3VR9S++Ewyk2HHZdrO3CQJqSpd+95Us590V6AL7JqUAH2IwZ/398PmNFgw== - -dom-walk@^0.1.0: - version "0.1.2" - resolved "https://registry.yarnpkg.com/dom-walk/-/dom-walk-0.1.2.tgz#0c548bef048f4d1f2a97249002236060daa3fd84" - integrity sha512-6QvTW9mrGeIegrFXdtQi9pk7O/nSK6lSdXW2eqUspN5LWD7UTji2Fqw5V2YLjBpHEoU9Xl/eUWNpDeZvoyOv2w== - -global@^4.4.0: - version "4.4.0" - resolved "https://registry.yarnpkg.com/global/-/global-4.4.0.tgz#3e7b105179006a323ed71aafca3e9c57a5cc6406" - integrity sha512-wv/LAoHdRE3BeTGz53FAamhGlPLhlssK45usmGFThIi4XqnBmjKQ16u+RNbP7WvigRZDxUsM0J3gcQ5yicaL0w== - dependencies: - min-document "^2.19.0" - process "^0.11.10" - -min-document@^2.19.0: - version "2.19.0" - resolved "https://registry.yarnpkg.com/min-document/-/min-document-2.19.0.tgz#7bd282e3f5842ed295bb748cdd9f1ffa2c824685" - integrity sha1-e9KC4/WELtKVu3SM3Z8f+iyCRoU= - dependencies: - dom-walk "^0.1.0" - -packet-reader@1.0.0: - version "1.0.0" - resolved "https://registry.yarnpkg.com/packet-reader/-/packet-reader-1.0.0.tgz#9238e5480dedabacfe1fe3f2771063f164157d74" - integrity sha512-HAKu/fG3HpHFO0AA8WE8q2g+gBJaZ9MG7fcKk+IJPLTGAD6Psw4443l+9DGRbOIh3/aXr7Phy0TjilYivJo5XQ== - -pg-connection-string@^2.4.0: - version "2.4.0" - resolved "https://registry.yarnpkg.com/pg-connection-string/-/pg-connection-string-2.4.0.tgz#c979922eb47832999a204da5dbe1ebf2341b6a10" - integrity sha512-3iBXuv7XKvxeMrIgym7njT+HlZkwZqqGX4Bu9cci8xHZNT+Um1gWKqCsAzcC0d95rcKMU5WBg6YRUcHyV0HZKQ== - -pg-int8@1.0.1: - version "1.0.1" - resolved "https://registry.yarnpkg.com/pg-int8/-/pg-int8-1.0.1.tgz#943bd463bf5b71b4170115f80f8efc9a0c0eb78c" - integrity sha512-WCtabS6t3c8SkpDBUlb1kjOs7l66xsGdKpIPZsg4wR+B3+u9UAum2odSsF9tnvxg80h4ZxLWMy4pRjOsFIqQpw== - -pg-pool@^3.2.1: - version "3.2.1" - resolved "https://registry.yarnpkg.com/pg-pool/-/pg-pool-3.2.1.tgz#5f4afc0f58063659aeefa952d36af49fa28b30e0" - integrity sha512-BQDPWUeKenVrMMDN9opfns/kZo4lxmSWhIqo+cSAF7+lfi9ZclQbr9vfnlNaPr8wYF3UYjm5X0yPAhbcgqNOdA== - -pg-protocol@^1.3.0: - version "1.3.0" - resolved "https://registry.yarnpkg.com/pg-protocol/-/pg-protocol-1.3.0.tgz#3c8fb7ca34dbbfcc42776ce34ac5f537d6e34770" - integrity sha512-64/bYByMrhWULUaCd+6/72c9PMWhiVFs3EVxl9Ct6a3v/U8+rKgqP2w+kKg/BIGgMJyB+Bk/eNivT32Al+Jghw== - -pg-types@^2.1.0: - version "2.2.0" - resolved "https://registry.yarnpkg.com/pg-types/-/pg-types-2.2.0.tgz#2d0250d636454f7cfa3b6ae0382fdfa8063254a3" - integrity sha512-qTAAlrEsl8s4OiEQY69wDvcMIdQN6wdz5ojQiOy6YRMuynxenON0O5oCpJI6lshc6scgAY8qvJ2On/p+CXY0GA== - dependencies: - pg-int8 "1.0.1" - postgres-array "~2.0.0" - postgres-bytea "~1.0.0" - postgres-date "~1.0.4" - postgres-interval "^1.1.0" - -pg@^8.4.0: - version "8.4.0" - resolved "https://registry.yarnpkg.com/pg/-/pg-8.4.0.tgz#7c754e0b907e8dae3af6fff0a0014c77f1418842" - integrity sha512-01LcNrAf+mBI46c78mE86I5o5KkOM942lLiSBdiCfgHTR+oUNIjh1fKClWeoPNHJz2oXe/VUSqtk1vwAQYwWEg== - dependencies: - buffer-writer "2.0.0" - packet-reader "1.0.0" - pg-connection-string "^2.4.0" - pg-pool "^3.2.1" - pg-protocol "^1.3.0" - pg-types "^2.1.0" - pgpass "1.x" - -pgpass@1.x: - version "1.0.2" - resolved "https://registry.yarnpkg.com/pgpass/-/pgpass-1.0.2.tgz#2a7bb41b6065b67907e91da1b07c1847c877b306" - integrity sha1-Knu0G2BltnkH6R2hsHwYR8h3swY= - dependencies: - split "^1.0.0" - -postgres-array@~2.0.0: - version "2.0.0" - resolved "https://registry.yarnpkg.com/postgres-array/-/postgres-array-2.0.0.tgz#48f8fce054fbc69671999329b8834b772652d82e" - integrity sha512-VpZrUqU5A69eQyW2c5CA1jtLecCsN2U/bD6VilrFDWq5+5UIEVO7nazS3TEcHf1zuPYO/sqGvUvW62g86RXZuA== - -postgres-bytea@~1.0.0: - version "1.0.0" - resolved "https://registry.yarnpkg.com/postgres-bytea/-/postgres-bytea-1.0.0.tgz#027b533c0aa890e26d172d47cf9ccecc521acd35" - integrity sha1-AntTPAqokOJtFy1Hz5zOzFIazTU= - -postgres-date@~1.0.4: - version "1.0.5" - resolved "https://registry.yarnpkg.com/postgres-date/-/postgres-date-1.0.5.tgz#710b27de5f27d550f6e80b5d34f7ba189213c2ee" - integrity sha512-pdau6GRPERdAYUQwkBnGKxEfPyhVZXG/JiS44iZWiNdSOWE09N2lUgN6yshuq6fVSon4Pm0VMXd1srUUkLe9iA== - -postgres-interval@^1.1.0: - version "1.2.0" - resolved "https://registry.yarnpkg.com/postgres-interval/-/postgres-interval-1.2.0.tgz#b460c82cb1587507788819a06aa0fffdb3544695" - integrity sha512-9ZhXKM/rw350N1ovuWHbGxnGh/SNJ4cnxHiM0rxE4VN41wsg8P8zWn9hv/buK00RP4WvlOyr/RBDiptyxVbkZQ== - dependencies: - xtend "^4.0.0" - -process@^0.11.10: - version "0.11.10" - resolved "https://registry.yarnpkg.com/process/-/process-0.11.10.tgz#7332300e840161bda3e69a1d1d91a7d4bc16f182" - integrity sha1-czIwDoQBYb2j5podHZGn1LwW8YI= - -split@^1.0.0: - version "1.0.1" - resolved "https://registry.yarnpkg.com/split/-/split-1.0.1.tgz#605bd9be303aa59fb35f9229fbea0ddec9ea07d9" - integrity sha512-mTyOoPbrivtXnwnIxZRFYRrPNtEFKlpB2fvjSnCQUiAA6qAZzqwna5envK4uk6OIeP17CsdF3rSBGYVBsU0Tkg== - dependencies: - through "2" - -through@2: - version "2.3.8" - resolved "https://registry.yarnpkg.com/through/-/through-2.3.8.tgz#0dd4c9ffaabc357960b1b724115d7e0e86a2e1f5" - integrity sha1-DdTJ/6q8NXlgsbckEV1+Doai4fU= - -tslib@^2.0.0: - version "2.0.0" - resolved "https://registry.yarnpkg.com/tslib/-/tslib-2.0.0.tgz#18d13fc2dce04051e20f074cc8387fd8089ce4f3" - integrity sha512-lTqkx847PI7xEDYJntxZH89L2/aXInsyF2luSafe/+0fHOMjlBNXdH6th7f70qxLDhul7KZK0zC8V5ZIyHl0/g== - -xtend@^4.0.0: - version "4.0.2" - resolved "https://registry.yarnpkg.com/xtend/-/xtend-4.0.2.tgz#bb72779f5fa465186b1f438f674fa347fdb5db54" - integrity sha512-LKYU1iAXJXUgAXn9URjiu+MWhyUXHsvfp7mcuYm9dSUKK0/CjtrUwFAxD82/mCWbtLsGjFIad0wIsod4zrTAEQ== diff --git a/javascript/with-next-js/README.md b/javascript/with-next-js/README.md deleted file mode 100644 index 4b5c883..0000000 --- a/javascript/with-next-js/README.md +++ /dev/null @@ -1,30 +0,0 @@ -This is a [Next.js](https://nextjs.org/) project bootstrapped with [`create-next-app`](https://github.com/vercel/next.js/tree/canary/packages/create-next-app). - -## Getting Started - -First, run the development server: - -```bash -npm run dev -# or -yarn dev -``` - -Open [http://localhost:3000](http://localhost:3000) with your browser to see the result. - -You can start editing the page by modifying `pages/index.js`. The page auto-updates as you edit the file. - -## Learn More - -To learn more about Next.js, take a look at the following resources: - -- [Next.js Documentation](https://nextjs.org/docs) - learn about Next.js features and API. -- [Learn Next.js](https://nextjs.org/learn) - an interactive Next.js tutorial. - -You can check out [the Next.js GitHub repository](https://github.com/vercel/next.js/) - your feedback and contributions are welcome! - -## Deploy on Vercel - -The easiest way to deploy your Next.js app is to use the [Vercel Platform](https://vercel.com/import?utm_medium=default-template&filter=next.js&utm_source=create-next-app&utm_campaign=create-next-app-readme) from the creators of Next.js. - -Check out our [Next.js deployment documentation](https://nextjs.org/docs/deployment) for more details. diff --git a/javascript/with-next-js/pages/index.js b/javascript/with-next-js/pages/index.js deleted file mode 100644 index 527c96b..0000000 --- a/javascript/with-next-js/pages/index.js +++ /dev/null @@ -1,84 +0,0 @@ -import Head from "next/head"; -import styles from "../styles/Home.module.css"; -import { useEffect, useState } from "react"; - -const Home = () => { - const [time, setTime] = useState(null); - - useEffect(() => { - (async () => { - try { - const res = await fetch("api/data").then((res) => res.json()); - setTime(res.data.time); - } catch (e) { - alert(e); - } - })(); - }, []); - - return ( -
- - Create Next App - - - -
-

- The current time is {time} -

-

- Welcome to Next.js! -

- -

- Get started by editing{" "} - pages/index.js -

- -
- -

Documentation →

-

Find in-depth information about Next.js features and API.

-
- - -

Learn →

-

Learn about Next.js in an interactive course with quizzes!

-
- - -

Examples →

-

Discover and deploy boilerplate example Next.js projects.

-
- - -

Deploy →

-

- Instantly deploy your Next.js site to a public URL with Vercel. -

-
-
-
- - -
- ); -}; - -export default Home; diff --git a/javascript/with-next-js/.gitignore b/javascript/with-nextjs-postgres/.gitignore similarity index 100% rename from javascript/with-next-js/.gitignore rename to javascript/with-nextjs-postgres/.gitignore diff --git a/javascript/with-nextjs-postgres/README.md b/javascript/with-nextjs-postgres/README.md new file mode 100644 index 0000000..4aaf16e --- /dev/null +++ b/javascript/with-nextjs-postgres/README.md @@ -0,0 +1,14 @@ +# NextJS Postgres Example + +This example is a [NextJS](https://nextjs.org/) app that connects to Postgres +database with [node-pg](https://www.npmjs.com/package/pg). + +## How to use + +- Create a Railway project with the Postgres plugin +- Connect to your Railway project with `railway init` +- Run this example with `railway dev` + +## Notes + +The SQL query being run simply fetches the current time in the database. It is located in `pages/api.time.js` diff --git a/javascript/with-next-js/package.json b/javascript/with-nextjs-postgres/package.json similarity index 85% rename from javascript/with-next-js/package.json rename to javascript/with-nextjs-postgres/package.json index 239205f..ae34023 100644 --- a/javascript/with-next-js/package.json +++ b/javascript/with-nextjs-postgres/package.json @@ -11,6 +11,7 @@ "next": "9.5.4", "pg": "^8.4.0", "react": "16.13.1", - "react-dom": "16.13.1" + "react-dom": "16.13.1", + "swr": "^0.3.9" } } diff --git a/javascript/with-next-js/pages/_app.js b/javascript/with-nextjs-postgres/pages/_app.js similarity index 100% rename from javascript/with-next-js/pages/_app.js rename to javascript/with-nextjs-postgres/pages/_app.js diff --git a/javascript/with-next-js/pages/api/data.js b/javascript/with-nextjs-postgres/pages/api/time.js similarity index 72% rename from javascript/with-next-js/pages/api/data.js rename to javascript/with-nextjs-postgres/pages/api/time.js index 84c8b7e..059006c 100644 --- a/javascript/with-next-js/pages/api/data.js +++ b/javascript/with-nextjs-postgres/pages/api/time.js @@ -1,7 +1,6 @@ // Next.js API route support: https://nextjs.org/docs/api-routes/introduction -// Call Railway anywhere on serverside code. -// Here or getInitialProps +// Use Railway Postgres anywhere in the API routes or in getServerSideProps import pg from "pg"; @@ -12,9 +11,7 @@ export default async (req, res) => { const query = await client.query("SELECT NOW()"); res.json({ - data: { - time: query.rows[0].now, - }, + time: query.rows[0].now, }); } catch (e) { console.log("ERROR", e); diff --git a/javascript/with-nextjs-postgres/pages/index.js b/javascript/with-nextjs-postgres/pages/index.js new file mode 100644 index 0000000..6e2d47e --- /dev/null +++ b/javascript/with-nextjs-postgres/pages/index.js @@ -0,0 +1,27 @@ +import Head from "next/head"; +import useSWR from "swr"; +import styles from "../styles/Home.module.css"; + +const Home = () => { + const { data, error } = useSWR("/api/time"); + + if (error) return
failed to load
; + + return ( +
+ + Railway NextJS Postgres + + + +
+

Current time in Postgres DB is

+ + {data == null &&
loading...
} + {data != null && {data.time}} +
+
+ ); +}; + +export default Home; diff --git a/javascript/with-next-js/public/favicon.ico b/javascript/with-nextjs-postgres/public/favicon.ico similarity index 100% rename from javascript/with-next-js/public/favicon.ico rename to javascript/with-nextjs-postgres/public/favicon.ico diff --git a/javascript/with-next-js/public/vercel.svg b/javascript/with-nextjs-postgres/public/vercel.svg similarity index 100% rename from javascript/with-next-js/public/vercel.svg rename to javascript/with-nextjs-postgres/public/vercel.svg diff --git a/javascript/with-next-js/styles/Home.module.css b/javascript/with-nextjs-postgres/styles/Home.module.css similarity index 100% rename from javascript/with-next-js/styles/Home.module.css rename to javascript/with-nextjs-postgres/styles/Home.module.css diff --git a/javascript/with-next-js/styles/globals.css b/javascript/with-nextjs-postgres/styles/globals.css similarity index 100% rename from javascript/with-next-js/styles/globals.css rename to javascript/with-nextjs-postgres/styles/globals.css diff --git a/javascript/with-next-js/yarn.lock b/javascript/with-nextjs-postgres/yarn.lock similarity index 99% rename from javascript/with-next-js/yarn.lock rename to javascript/with-nextjs-postgres/yarn.lock index a473ab6..2ec6ec0 100644 --- a/javascript/with-next-js/yarn.lock +++ b/javascript/with-nextjs-postgres/yarn.lock @@ -2159,6 +2159,11 @@ define-property@^2.0.2: is-descriptor "^1.0.2" isobject "^3.0.1" +dequal@2.0.2: + version "2.0.2" + resolved "https://registry.yarnpkg.com/dequal/-/dequal-2.0.2.tgz#85ca22025e3a87e65ef75a7a437b35284a7e319d" + integrity sha512-q9K8BlJVxK7hQYqa6XISGmBZbtQQWVXSrRrWreHC94rMt1QL/Impruc+7p2CYSYuVIUr+YCt6hjrs1kkdJRTug== + des.js@^1.0.0: version "1.0.1" resolved "https://registry.yarnpkg.com/des.js/-/des.js-1.0.1.tgz#5382142e1bdc53f85d86d53e5f4aa7deb91e0843" @@ -4494,6 +4499,13 @@ supports-color@^7.1.0: dependencies: has-flag "^4.0.0" +swr@^0.3.9: + version "0.3.9" + resolved "https://registry.yarnpkg.com/swr/-/swr-0.3.9.tgz#a179a795244c7b68684af6a632f1ad579e6a69e0" + integrity sha512-lyN4SjBzpoW4+v3ebT7JUtpzf9XyzrFwXIFv+E8ZblvMa5enSNaUBs4EPkL8gGA/GDMLngEmB53o5LaNboAPfg== + dependencies: + dequal "2.0.2" + tapable@^1.0.0, tapable@^1.1.3: version "1.1.3" resolved "https://registry.yarnpkg.com/tapable/-/tapable-1.1.3.tgz#a1fccc06b58db61fd7a45da2da44f5f3a3e67ba2" diff --git a/javascript/with-postgres/README.md b/javascript/with-postgres/README.md new file mode 100644 index 0000000..740ef61 --- /dev/null +++ b/javascript/with-postgres/README.md @@ -0,0 +1,13 @@ +# Postgres Example + +This example connects to a Postgres database with [node-pg](https://www.npmjs.com/package/pg). + +## How to use + +- Create a Railway project with the Postgres plugin +- Connect to your Railway project with `railway init` +- Run this example with `railway start` + +## Notes + +The SQL query being run simply fetches the current time in the database. It is located in `src/index.js` diff --git a/javascript/with-postgres/index.js b/javascript/with-postgres/index.js index 4487eb3..dd372ef 100644 --- a/javascript/with-postgres/index.js +++ b/javascript/with-postgres/index.js @@ -1,8 +1,18 @@ const pg = require("pg"); +// Connect to the database using the DATABASE_URL environment +// variable injected by Railway const pool = new pg.Pool(); -(async () => { + +const go = async () => { + // Query the database const res = await pool.query("SELECT NOW()"); + + // Print the result console.log(res.rows[0]); + + // Close the database connection pool.end(); -})(); +}; + +go(); diff --git a/javascript/with-prisma/README.md b/javascript/with-prisma/README.md index 9dd0b50..e5ba93d 100644 --- a/javascript/with-prisma/README.md +++ b/javascript/with-prisma/README.md @@ -1,30 +1,41 @@ -# with-prisma +# Prisma Example -This example uses [Prisma](https://www.prisma.io/) to access and interact with the Railway PostgreSQL database. +This example uses [Prisma](https://www.prisma.io/) to access and interact with +the Railway PostgreSQL database. + +## Setup + +- Create a Railway project with the Postgres plugin +- Connect to your Railway project with `railway init` +- Migrate the database `yarn migrate:up` +- Generate Prisma client `yarn generate` ## Usage -- Install dependencies `yarn install` -- Init a Railway project `yarn railway init` -- Generate Prisma `yarn generate` -- Open the project in the Railway dashboard `yarn railway open` -- Run the code `yarn start` +This example is a very basic CLI that can create and get items with Prisma. -Optionally, run `yarn migrate:up` to create a Users and Posts folder and follow Prisma's [Quickstart](https://www.prisma.io/docs/getting-started/quickstart/)! +- Create an item with `yarn start create [value]` +- List all items with `yarn start list` ## Prisma support -Prisma has fantastic TypeScript support. If you open `src/index.ts` in an editor like VSCode, you will get autocompletion and type hints when querying the database. +Prisma has fantastic TypeScript support. If you open `src/index.ts` in an editor +like VSCode, you will get autocompletion and type hints when querying the +database. -All of Prisma features are supported. [Prisma documentation can be found here]([More information](https://www.prisma.io/docs/reference/tools-and-interfaces/prisma-client/). +All of Prisma features are supported. [Prisma documentation can be found +here]([More +information](https://www.prisma.io/docs/reference/tools-and-interfaces/prisma-client/). -You can edit the Prisma schema at `prisma/schema.prisma`. After editing you can run +You can edit the Prisma schema at `prisma/schema.prisma`. After editing you can +run -``` +```shell yarn migrate:save yarn migrate:up ``` to create new migrations and run them against the database. -You should also run `yarn generate` to create updated type definitions for the Prisma client. +You should also run `yarn generate` to create updated type definitions for the +Prisma client. diff --git a/javascript/with-prisma/package.json b/javascript/with-prisma/package.json index f138a6c..048a2f2 100644 --- a/javascript/with-prisma/package.json +++ b/javascript/with-prisma/package.json @@ -13,6 +13,7 @@ }, "devDependencies": { "@prisma/cli": "^2.1.3", + "@types/node": "^14.14.9", "ts-node": "^8.10.2", "typescript": "^3.9.7", "yarpm": "^0.2.1" diff --git a/javascript/with-prisma/prisma/migrations/20200701145156-init/README.md b/javascript/with-prisma/prisma/migrations/20200701145156-init/README.md deleted file mode 100644 index cdde48d..0000000 --- a/javascript/with-prisma/prisma/migrations/20200701145156-init/README.md +++ /dev/null @@ -1,59 +0,0 @@ -# Migration `20200701145156-init` - -This migration has been generated by Jake Runzer at 7/1/2020, 2:51:56 PM. -You can check out the [state of the schema](./schema.prisma) after the migration. - -## Database Steps - -```sql -CREATE TABLE "public"."Post" ( -"authorId" integer NOT NULL ,"content" text ,"createdAt" timestamp(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,"id" SERIAL,"title" text NOT NULL , - PRIMARY KEY ("id")) - -CREATE TABLE "public"."User" ( -"email" text NOT NULL ,"id" SERIAL,"name" text , - PRIMARY KEY ("id")) - -CREATE UNIQUE INDEX "User.email" ON "public"."User"("email") - -ALTER TABLE "public"."Post" ADD FOREIGN KEY ("authorId")REFERENCES "public"."User"("id") ON DELETE CASCADE ON UPDATE CASCADE -``` - -## Changes - -```diff -diff --git schema.prisma schema.prisma -migration ..20200701145156-init ---- datamodel.dml -+++ datamodel.dml -@@ -1,0 +1,27 @@ -+// This is your Prisma schema file, -+// learn more about it in the docs: https://pris.ly/d/prisma-schema -+ -+datasource db { -+ provider = "postgresql" -+ url = "***" -+} -+ -+generator client { -+ provider = "prisma-client-js" -+} -+ -+model Post { -+ id Int @default(autoincrement()) @id -+ createdAt DateTime @default(now()) -+ title String -+ content String? -+ User User @relation(fields: [authorId], references: [id]) -+ authorId Int -+} -+ -+model User { -+ id Int @default(autoincrement()) @id -+ email String @unique -+ name String? -+ Post Post[] -+} -``` - - diff --git a/javascript/with-prisma/prisma/migrations/20200701145156-init/schema.prisma b/javascript/with-prisma/prisma/migrations/20200701145156-init/schema.prisma deleted file mode 100644 index 47d1035..0000000 --- a/javascript/with-prisma/prisma/migrations/20200701145156-init/schema.prisma +++ /dev/null @@ -1,27 +0,0 @@ -// This is your Prisma schema file, -// learn more about it in the docs: https://pris.ly/d/prisma-schema - -datasource db { - provider = "postgresql" - url = "***" -} - -generator client { - provider = "prisma-client-js" -} - -model Post { - id Int @default(autoincrement()) @id - createdAt DateTime @default(now()) - title String - content String? - User User @relation(fields: [authorId], references: [id]) - authorId Int -} - -model User { - id Int @default(autoincrement()) @id - email String @unique - name String? - Post Post[] -} diff --git a/javascript/with-prisma/prisma/migrations/20200701145156-init/steps.json b/javascript/with-prisma/prisma/migrations/20200701145156-init/steps.json deleted file mode 100644 index 84fdced..0000000 --- a/javascript/with-prisma/prisma/migrations/20200701145156-init/steps.json +++ /dev/null @@ -1,252 +0,0 @@ -{ - "version": "0.3.14-fixed", - "steps": [ - { - "tag": "CreateSource", - "source": "db" - }, - { - "tag": "CreateArgument", - "location": { - "tag": "Source", - "source": "db" - }, - "argument": "provider", - "value": "\"postgresql\"" - }, - { - "tag": "CreateArgument", - "location": { - "tag": "Source", - "source": "db" - }, - "argument": "url", - "value": "\"***\"" - }, - { - "tag": "CreateModel", - "model": "Post" - }, - { - "tag": "CreateField", - "model": "Post", - "field": "id", - "type": "Int", - "arity": "Required" - }, - { - "tag": "CreateDirective", - "location": { - "path": { - "tag": "Field", - "model": "Post", - "field": "id" - }, - "directive": "default" - } - }, - { - "tag": "CreateArgument", - "location": { - "tag": "Directive", - "path": { - "tag": "Field", - "model": "Post", - "field": "id" - }, - "directive": "default" - }, - "argument": "", - "value": "autoincrement()" - }, - { - "tag": "CreateDirective", - "location": { - "path": { - "tag": "Field", - "model": "Post", - "field": "id" - }, - "directive": "id" - } - }, - { - "tag": "CreateField", - "model": "Post", - "field": "createdAt", - "type": "DateTime", - "arity": "Required" - }, - { - "tag": "CreateDirective", - "location": { - "path": { - "tag": "Field", - "model": "Post", - "field": "createdAt" - }, - "directive": "default" - } - }, - { - "tag": "CreateArgument", - "location": { - "tag": "Directive", - "path": { - "tag": "Field", - "model": "Post", - "field": "createdAt" - }, - "directive": "default" - }, - "argument": "", - "value": "now()" - }, - { - "tag": "CreateField", - "model": "Post", - "field": "title", - "type": "String", - "arity": "Required" - }, - { - "tag": "CreateField", - "model": "Post", - "field": "content", - "type": "String", - "arity": "Optional" - }, - { - "tag": "CreateField", - "model": "Post", - "field": "User", - "type": "User", - "arity": "Required" - }, - { - "tag": "CreateDirective", - "location": { - "path": { - "tag": "Field", - "model": "Post", - "field": "User" - }, - "directive": "relation" - } - }, - { - "tag": "CreateArgument", - "location": { - "tag": "Directive", - "path": { - "tag": "Field", - "model": "Post", - "field": "User" - }, - "directive": "relation" - }, - "argument": "fields", - "value": "[authorId]" - }, - { - "tag": "CreateArgument", - "location": { - "tag": "Directive", - "path": { - "tag": "Field", - "model": "Post", - "field": "User" - }, - "directive": "relation" - }, - "argument": "references", - "value": "[id]" - }, - { - "tag": "CreateField", - "model": "Post", - "field": "authorId", - "type": "Int", - "arity": "Required" - }, - { - "tag": "CreateModel", - "model": "User" - }, - { - "tag": "CreateField", - "model": "User", - "field": "id", - "type": "Int", - "arity": "Required" - }, - { - "tag": "CreateDirective", - "location": { - "path": { - "tag": "Field", - "model": "User", - "field": "id" - }, - "directive": "default" - } - }, - { - "tag": "CreateArgument", - "location": { - "tag": "Directive", - "path": { - "tag": "Field", - "model": "User", - "field": "id" - }, - "directive": "default" - }, - "argument": "", - "value": "autoincrement()" - }, - { - "tag": "CreateDirective", - "location": { - "path": { - "tag": "Field", - "model": "User", - "field": "id" - }, - "directive": "id" - } - }, - { - "tag": "CreateField", - "model": "User", - "field": "email", - "type": "String", - "arity": "Required" - }, - { - "tag": "CreateDirective", - "location": { - "path": { - "tag": "Field", - "model": "User", - "field": "email" - }, - "directive": "unique" - } - }, - { - "tag": "CreateField", - "model": "User", - "field": "name", - "type": "String", - "arity": "Optional" - }, - { - "tag": "CreateField", - "model": "User", - "field": "Post", - "type": "Post", - "arity": "List" - } - ] -} \ No newline at end of file diff --git a/javascript/with-prisma/prisma/migrations/20201124133307/README.md b/javascript/with-prisma/prisma/migrations/20201124133307/README.md new file mode 100644 index 0000000..d0ae4e1 --- /dev/null +++ b/javascript/with-prisma/prisma/migrations/20201124133307/README.md @@ -0,0 +1,41 @@ +# Migration `20201124133307` + +This migration has been generated by Jake Runzer at 11/24/2020, 1:33:07 PM. +You can check out the [state of the schema](./schema.prisma) after the migration. + +## Database Steps + +```sql +CREATE TABLE "public"."Item" ( +"createdAt" timestamp(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,"id" SERIAL,"value" text NOT NULL , + PRIMARY KEY ("id")) +``` + +## Changes + +```diff +diff --git schema.prisma schema.prisma +migration ..20201124133307 +--- datamodel.dml ++++ datamodel.dml +@@ -1,0 +1,17 @@ ++// This is your Prisma schema file, ++// learn more about it in the docs: https://pris.ly/d/prisma-schema ++ ++datasource db { ++ provider = "postgresql" ++ url = "***" ++} ++ ++generator client { ++ provider = "prisma-client-js" ++} ++ ++model Item { ++ id Int @id @default(autoincrement()) ++ createdAt DateTime @default(now()) ++ value String ++} +``` + + diff --git a/javascript/with-prisma/prisma/migrations/20201124133307/schema.prisma b/javascript/with-prisma/prisma/migrations/20201124133307/schema.prisma new file mode 100644 index 0000000..d57855d --- /dev/null +++ b/javascript/with-prisma/prisma/migrations/20201124133307/schema.prisma @@ -0,0 +1,17 @@ +// This is your Prisma schema file, +// learn more about it in the docs: https://pris.ly/d/prisma-schema + +datasource db { + provider = "postgresql" + url = "***" +} + +generator client { + provider = "prisma-client-js" +} + +model Item { + id Int @id @default(autoincrement()) + createdAt DateTime @default(now()) + value String +} diff --git a/javascript/with-prisma/prisma/migrations/20201124133307/steps.json b/javascript/with-prisma/prisma/migrations/20201124133307/steps.json new file mode 100644 index 0000000..205d24e --- /dev/null +++ b/javascript/with-prisma/prisma/migrations/20201124133307/steps.json @@ -0,0 +1,113 @@ +{ + "version": "0.3.14-fixed", + "steps": [ + { + "tag": "CreateSource", + "source": "db" + }, + { + "tag": "CreateArgument", + "location": { + "tag": "Source", + "source": "db" + }, + "argument": "provider", + "value": "\"postgresql\"" + }, + { + "tag": "CreateArgument", + "location": { + "tag": "Source", + "source": "db" + }, + "argument": "url", + "value": "\"***\"" + }, + { + "tag": "CreateModel", + "model": "Item" + }, + { + "tag": "CreateField", + "model": "Item", + "field": "id", + "type": "Int", + "arity": "Required" + }, + { + "tag": "CreateDirective", + "location": { + "path": { + "tag": "Field", + "model": "Item", + "field": "id" + }, + "directive": "id" + } + }, + { + "tag": "CreateDirective", + "location": { + "path": { + "tag": "Field", + "model": "Item", + "field": "id" + }, + "directive": "default" + } + }, + { + "tag": "CreateArgument", + "location": { + "tag": "Directive", + "path": { + "tag": "Field", + "model": "Item", + "field": "id" + }, + "directive": "default" + }, + "argument": "", + "value": "autoincrement()" + }, + { + "tag": "CreateField", + "model": "Item", + "field": "createdAt", + "type": "DateTime", + "arity": "Required" + }, + { + "tag": "CreateDirective", + "location": { + "path": { + "tag": "Field", + "model": "Item", + "field": "createdAt" + }, + "directive": "default" + } + }, + { + "tag": "CreateArgument", + "location": { + "tag": "Directive", + "path": { + "tag": "Field", + "model": "Item", + "field": "createdAt" + }, + "directive": "default" + }, + "argument": "", + "value": "now()" + }, + { + "tag": "CreateField", + "model": "Item", + "field": "value", + "type": "String", + "arity": "Required" + } + ] +} \ No newline at end of file diff --git a/javascript/with-prisma/prisma/migrations/migrate.lock b/javascript/with-prisma/prisma/migrations/migrate.lock index b5d187f..f9670c7 100644 --- a/javascript/with-prisma/prisma/migrations/migrate.lock +++ b/javascript/with-prisma/prisma/migrations/migrate.lock @@ -1,3 +1,3 @@ # Prisma Migrate lockfile v1 -20200701145156-init \ No newline at end of file +20201124133307 \ No newline at end of file diff --git a/javascript/with-prisma/prisma/schema.prisma b/javascript/with-prisma/prisma/schema.prisma index 2b46ea1..b01facc 100644 --- a/javascript/with-prisma/prisma/schema.prisma +++ b/javascript/with-prisma/prisma/schema.prisma @@ -10,18 +10,8 @@ generator client { provider = "prisma-client-js" } -model Post { - id Int @default(autoincrement()) @id +model Item { + id Int @id @default(autoincrement()) createdAt DateTime @default(now()) - title String - content String? - User User @relation(fields: [authorId], references: [id]) - authorId Int -} - -model User { - id Int @default(autoincrement()) @id - email String @unique - name String? - Post Post[] + value String } diff --git a/javascript/with-prisma/src/index.ts b/javascript/with-prisma/src/index.ts index 05e274b..7f48ae8 100644 --- a/javascript/with-prisma/src/index.ts +++ b/javascript/with-prisma/src/index.ts @@ -2,13 +2,31 @@ import { PrismaClient } from "@prisma/client"; const prisma = new PrismaClient(); -(async () => { - try { - const result = await prisma.queryRaw`SELECT NOW()`; - console.log(result[0]); - } catch (e) { - console.log(e); - } finally { - await prisma.disconnect(); - } -})(); +const args = process.argv.slice(2); + +const createItem = async () => { + const value = args[1]; + + const item = await prisma.item.create({ + data: { value }, + }); + + console.log(`Created item`); + console.log(item); +}; + +const getItems = async () => { + const items = await prisma.item.findMany(); + console.log(`Found ${items.length}`); + console.log(items); +}; + +if (args[0] === "create" && args.length === 2) { + createItem().then(() => process.exit(0)); +} else if (args[0] === "list") { + getItems().then(() => process.exit(0)); +} else { + console.log("Invalid command. Usage:"); + console.log(` Create item: yarn start create [value]`); + console.log(` List items: yarn start list`); +} diff --git a/javascript/with-prisma/yarn.lock b/javascript/with-prisma/yarn.lock index 3e58e39..dd02ad1 100644 --- a/javascript/with-prisma/yarn.lock +++ b/javascript/with-prisma/yarn.lock @@ -14,6 +14,11 @@ dependencies: pkg-up "^3.1.0" +"@types/node@^14.14.9": + version "14.14.9" + resolved "https://registry.yarnpkg.com/@types/node/-/node-14.14.9.tgz#04afc9a25c6ff93da14deabd65dc44485b53c8d6" + integrity sha512-JsoLXFppG62tWTklIoO4knA+oDTYsmqWxHRvd4lpmfQRNhX6osheUOWETP2jMoV/2bEHuMra8Pp3Dmo/stBFcw== + arg@^4.1.0: version "4.1.3" resolved "https://registry.yarnpkg.com/arg/-/arg-4.1.3.tgz#269fc7ad5b8e42cb63c896d5666017261c144089"