Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wire up a database #8

Merged
merged 7 commits into from
Aug 7, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Clarify README
david-mears-2 committed Aug 1, 2024
commit 43d637ea94d6ab548eaf61697d8db566655f556d
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -69,14 +69,24 @@ The QR code shown will allow you to quickly access the app.

### DB

To create migrations to the database, first update the Prisma schema at ./prisma/schema.prisma as required, then run the below to generate the corresponding SQL migration and to apply it to the database:
Our ORM is [Prisma](https://www.prisma.io/).

To create migrations to the database, first update the Prisma schema at ./prisma/schema.prisma as required, then run the below command to generate the corresponding SQL migration and to apply it to the database. [You should commit both](https://www.prisma.io/docs/orm/prisma-migrate/workflows/team-development#source-control) the Prisma schema and the migration file to Git.

```bash
npx prisma migrate dev
```

The same command is also used to apply migrations that already exist in ./prisma/migrations but which have not been applied to the database.

Prisma ORM can only query the database once you 'generate' the Prisma Client, which generates into `node_modules/.prisma/client` based on the file `prisma/schema.prisma`. This should happen when you install the JS dependencies and whenever you run a migration, but if the Prisma client gets out of sync or doesn't generate, you can manually generate it:

```bash
npx prisma generate
```

More helpful information about Prisma [development workflows](https://www.prisma.io/docs/orm/prisma-migrate/workflows/development-and-production#customizing-migrations) and resolving issues in [production environments](https://www.prisma.io/docs/orm/prisma-migrate/workflows/patching-and-hotfixing#fixing-failed-migrations-with-migrate-diff-and-db-execute).

#### For your IDE

In VSCode, you can use the extension with ID 'Prisma.prisma' to get syntax highlighting etc.

Unchanged files with check annotations Beta

// we still need to use the related setup code that creates a global instance of the Prisma Client,
// so this file is a linted version of https://www.prisma.io/docs/orm/more/help-and-troubleshooting/help-articles/prisma-nuxt-module#option-b-libprismats
import process from "node:process";
import { PrismaClient } from "@prisma/client";

Check warning on line 6 in lib/prisma.ts

Codecov / codecov/patch

lib/prisma.ts#L5-L6

Added lines #L5 - L6 were not covered by tests
function prismaClientSingleton() {
return new PrismaClient();
}

Check warning on line 10 in lib/prisma.ts

Codecov / codecov/patch

lib/prisma.ts#L8-L10

Added lines #L8 - L10 were not covered by tests
declare const globalThis: {
prismaGlobal: ReturnType<typeof prismaClientSingleton>
// eslint-disable-next-line no-restricted-globals
} & typeof global;
const prisma = globalThis.prismaGlobal ?? prismaClientSingleton();

Check warning on line 17 in lib/prisma.ts

Codecov / codecov/patch

lib/prisma.ts#L17

Added line #L17 was not covered by tests
export default prisma;

Check warning on line 19 in lib/prisma.ts

Codecov / codecov/patch

lib/prisma.ts#L19

Added line #L19 was not covered by tests
if (process.env.NODE_ENV !== "production")
globalThis.prismaGlobal = prisma;

Check warning on line 22 in lib/prisma.ts

Codecov / codecov/patch

lib/prisma.ts#L21-L22

Added lines #L21 - L22 were not covered by tests
import prisma from "~/lib/prisma";
export default defineEventHandler(async (_event) => {
const scenarioCount = await prisma.scenario.count();

Check warning on line 4 in server/api/scenarios.ts

Codecov / codecov/patch

server/api/scenarios.ts#L3-L4

Added lines #L3 - L4 were not covered by tests
return {
count: scenarioCount,
};
});

Check warning on line 9 in server/api/scenarios.ts

Codecov / codecov/patch

server/api/scenarios.ts#L6-L9

Added lines #L6 - L9 were not covered by tests