Steve Jenkins
06/29/2020, 7:59 PMRyan
06/30/2020, 7:06 AMSteve Jenkins
06/30/2020, 2:01 PMRyan
06/30/2020, 2:06 PMSteve Jenkins
06/30/2020, 2:06 PMSteve Jenkins
06/30/2020, 2:08 PMRyan
06/30/2020, 2:10 PMSteve Jenkins
06/30/2020, 2:11 PMSteve Jenkins
06/30/2020, 2:12 PMRyan
06/30/2020, 2:16 PMprisma.queryRaw API to fetch data from the database. Apollo Client is for the frontend πSteve Jenkins
06/30/2020, 2:16 PMRyan
06/30/2020, 2:19 PMSteve Jenkins
06/30/2020, 2:20 PMSteve Jenkins
06/30/2020, 2:21 PMRyan
06/30/2020, 2:21 PMSteve Jenkins
06/30/2020, 2:22 PMSteve Jenkins
07/01/2020, 5:57 PM-- Create PostGIS extensions if they don't exist
CREATE EXTENSION IF NOT EXISTS postgis;
CREATE EXTENSION IF NOT EXISTS postgis_topology;
-- User location data
CREATE TABLE user_location (
user_id INTEGER PRIMARY KEY,
location GEOGRAPHY(Point)
);
-- Landmark location data
CREATE TABLE landmark (
id SERIAL PRIMARY KEY,
name TEXT,
type TEXT,
location GEOGRAPHY(Point)
);
And I run
npx prisma introspect
However, I get the following warning:
*** WARNING ***
These fields were commented out because we currently do not support their types.
- Model "landmark", field: "location", original data type: "geography"
- Model "user_location", field: "location", original data type: "geography"
The schema.prisma looks like this:
model landmark {
id Int @default(autoincrement()) @id
// This type is currently not supported.
// location geography?
name String?
type String?
}
model spatial_ref_sys {
auth_name String?
auth_srid Int?
proj4text String?
srid Int @id
srtext String?
}
model user_location {
// This type is currently not supported.
// location geography?
user_id Int @id
}
I have my db hosted on heroku (with Postgres extension installed). My .env looks like:
DATABASE_URL="<postgresql://fdkbxsadlorxmc:<mypasswordishere>@ec2-34-225-162-157.compute-1.amazonaws.com:5432/deoq5qsn6u8k07>"
And at this point I'm kind of stuck. A couple of questions come up: 0. How can I query if the type geography is not supported? 1. Am I on the right track? 2. How do I write queries and mutations if I cannot connect to the server like I did when I set up prisma alongside graphql yoga? 3. I planned to use apollo. Is that an alternative to @prisma/client or are they related? THANK YOU for your help πRyan
07/02/2020, 7:07 AMHow can I query if the type geography is not supported?You cannot currently. This is a limitation in Prisma currently and so you can only perform any operations on the type but cannot fetch it for the time being.
Am I on the right track?Yes. You can check this out if it helps (still not final though) π
How do I write queries and mutations if I cannot connect to the server like I did when I set up prisma alongside graphql yoga?You can use the same example above. Although it uses Express, you can use any server like Apollo Server or GrapQL Yoga.
I planned to use apollo. Is that an alternative to @prisma/client or are they related?
@prisma/client is just a tool that helps you query your database. Apollo is a server that you use to serve your GraphQL API. Both work well together so you wouldn't have any issues there.Steve Jenkins
07/02/2020, 3:55 PMSteve Jenkins
07/02/2020, 3:56 PMSteve Jenkins
07/07/2020, 3:30 AMcreate table "public"."Store" (
id serial primary key,
"name" text not null
);
then I run:
npm run seed
and my database has the columns. Unless, I'm missing something, It seems to defeat the purpose of building relations in the schema.prisma file, because for example, I'd like to create a store model with relations to storeOwners like so:
model Store {
id Int @default(autoincrement()) @id
name String
owners [StoreOwner]
}
And if the table column does not exist, then it obviously will not work.
My questions are:
1. Is there a better way to doing this so that my schema.prisma can inform my development, rather than my seed.sql
2. If not, can you point me to an example of a seed.sql file that handles relationships like the one I'm hoping to constuct in my model Store {...}
I liked how in prisma1, I could run prisma deploy and didn't have to bother with writing relationships between data and the tables would just appear in my database. I imagine that's available in prisma2, but can't see it (or perhaps it's not possible with my use case where I need to include PostGIS functionality)
Thanks!! πRyan
07/07/2020, 7:24 AMIs there a better way to doing this so that my schema.prisma can inform my development, rather than my seed.sqlUnfortunately as Prisma doesn't support the Geography type of Postgres natively, you would have to do that in SQL as of now as the Prisma schema wouldn't understand that datatype.
If not, can you point me to an example of a seed.sql file that handles relationships like the one I'm hoping to constuct in my model Store {...}I could help you model that if you could share what are you looking for π
Steve Jenkins
07/07/2020, 1:48 PMmodel Item {
createdAt DateTime?
description String?
id Int @default(autoincrement()) @id
image String?
largeImage String?
price Int?
title String
updatedAt DateTime?
}
model Store {
id Int @default(autoincrement()) @id
name String
items [Item]
}
I'm using the seed.sql file to seed the database and imagine I need to write something like this:
create table "public"."Item" (
id serial primary key,
"title" text not null,
"description" text,
"price" int,
"image" text,
"largeImage" text,
"createdAt" timestamp,
"updatedAt" timestamp
< ADD RELATION TO STORE HERE >
);
create table "public"."Store" (
id serial primary key,
"name" text not null
< ADD RELATION TO ITEMS HERE >
);
Any nudge in the right direction would be super helpful. Also, I'm dropping the table each time I need to update the database because I read the migrations were experimental and not to be used in production. Is this the correct way to go about it?Ryan
07/07/2020, 2:58 PMAlso, I'm dropping the table each time I need to update the database because I read the migrations were experimental and not to be used in productionYou don't need to drop the tables every time to perform a migration. Also as you will be using Postgis, I would suggest doing it with SQL files itself for the time being instead of Migrate. To create the above relation, you would need to do the following:
create table "public"."Store" (
id serial primary key,
"name" text not null
);
create table "public"."Item" (
id serial primary key,
"title" text not null,
"description" text,
"price" int,
"image" text,
"largeImage" text,
"createdAt" timestamp,
"updatedAt" timestamp,
"storeId" int references "public"."Store" ("id")
);
On seeding the above SQL and then running prisma introspect you should be able to see the models created in your schema.prisma.
Also regarding dropping tables that you have mentioned above, I would suggest creating different seed files for migrations and keeping them in a folder like migrations.
So when you create the first tables, you could name the file seed0001.sql and then run the SQL file.
After that, if you need to add another table you can create a seed0002.sql and create your table and then run this one.
If you need to alter a table, don't edit the previous file in which the table was created but create a new seed file with an increased number and run that.
Think of migrations as incremental changes that you add to your database. So you do not need to keep a single SQL file. One file for every change πSteve Jenkins
07/07/2020, 11:36 PM