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
This commit is contained in:
Jake Runzer
2020-11-24 15:23:23 -07:00
committed by GitHub
parent fc0356d88c
commit 256cce952e
32 changed files with 321 additions and 684 deletions

6
.prettierrc Normal file
View File

@@ -0,0 +1,6 @@
{
"bracketSpacing": true,
"singleQuote": false,
"trailingComma": "all",
"arrowParens": "avoid"
}

View File

@@ -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

View File

@@ -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"
}
}

View File

@@ -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();

View File

@@ -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==

View File

@@ -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.

View File

@@ -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 (
<div className={styles.container}>
<Head>
<title>Create Next App</title>
<link rel="icon" href="/favicon.ico" />
</Head>
<main className={styles.main}>
<p className={styles.description}>
The current time is <code className={styles.code}>{time}</code>
</p>
<h1 className={styles.title}>
Welcome to <a href="https://nextjs.org">Next.js!</a>
</h1>
<p className={styles.description}>
Get started by editing{" "}
<code className={styles.code}>pages/index.js</code>
</p>
<div className={styles.grid}>
<a href="https://nextjs.org/docs" className={styles.card}>
<h3>Documentation &rarr;</h3>
<p>Find in-depth information about Next.js features and API.</p>
</a>
<a href="https://nextjs.org/learn" className={styles.card}>
<h3>Learn &rarr;</h3>
<p>Learn about Next.js in an interactive course with quizzes!</p>
</a>
<a
href="https://github.com/vercel/next.js/tree/master/examples"
className={styles.card}
>
<h3>Examples &rarr;</h3>
<p>Discover and deploy boilerplate example Next.js projects.</p>
</a>
<a
href="https://vercel.com/import?filter=next.js&utm_source=create-next-app&utm_medium=default-template&utm_campaign=create-next-app"
className={styles.card}
>
<h3>Deploy &rarr;</h3>
<p>
Instantly deploy your Next.js site to a public URL with Vercel.
</p>
</a>
</div>
</main>
<footer className={styles.footer}>
<a
href="https://vercel.com?utm_source=create-next-app&utm_medium=default-template&utm_campaign=create-next-app"
target="_blank"
rel="noopener noreferrer"
>
Powered by{" "}
<img src="/vercel.svg" alt="Vercel Logo" className={styles.logo} />
</a>
</footer>
</div>
);
};
export default Home;

View File

@@ -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`

View File

@@ -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"
}
}

View File

@@ -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);

View File

@@ -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 <div>failed to load</div>;
return (
<div className={styles.container}>
<Head>
<title>Railway NextJS Postgres</title>
<link rel="icon" href="/favicon.ico" />
</Head>
<main className={styles.main}>
<h1 className={styles.description}>Current time in Postgres DB is</h1>
{data == null && <div>loading...</div>}
{data != null && <code className={styles.code}>{data.time}</code>}
</main>
</div>
);
};
export default Home;

View File

Before

Width:  |  Height:  |  Size: 15 KiB

After

Width:  |  Height:  |  Size: 15 KiB

View File

Before

Width:  |  Height:  |  Size: 1.1 KiB

After

Width:  |  Height:  |  Size: 1.1 KiB

View File

@@ -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"

View File

@@ -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`

View File

@@ -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();

View File

@@ -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.

View File

@@ -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"

View File

@@ -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[]
+}
```

View File

@@ -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[]
}

View File

@@ -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"
}
]
}

View File

@@ -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
+}
```

View File

@@ -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
}

View File

@@ -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"
}
]
}

View File

@@ -1,3 +1,3 @@
# Prisma Migrate lockfile v1
20200701145156-init
20201124133307

View File

@@ -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
}

View File

@@ -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`);
}

View File

@@ -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"