Prisma
Prisma works out of the box with JSTime. First, create a directory and initialize it with jstime init.
mkdir prisma-appcd prisma-appjstime initThen add Prisma as a dependency.
jspm add prismaWe’ll use the Prisma CLI with jstimex to initialize our schema and migration directory. For simplicity we’ll be using an in-memory SQLite database.
jstimex prisma init --datasource-provider sqliteOpen prisma/schema.prisma and add a simple User model.
generator client { provider = "prisma-client-js" }
datasource db { provider = "sqlite" url = env("DATABASE_URL") }
+ model User {+ id Int @id @default(autoincrement())+ email String @unique+ name String?+ }Then generate and run initial migration.
This will generate a .sql migration file in prisma/migrations, create a new SQLite instance, and execute the migration against the new instance.
jstimex prisma migrate dev --name initPrisma automatically generates our Prisma client whenever we execute a new migration. The client provides a fully typed API for reading and writing from our database.
It can be imported from @prisma/client.
import {PrismaClient} from "@prisma/client";Let’s write a simple script to create a new user, then count the number of users in the database.
import { PrismaClient } from "@prisma/client";
const prisma = new PrismaClient();
// create a new userawait prisma.user.create({ data: { name: "John Dough", email: `john-${Math.random()}@example.com`, },});
// count the number of usersconst count = await prisma.user.count();console.log(`There are ${count} users in the database.`);Let’s run this script with jstime run. Each time we run it, a new user is created.
$ jstime run index.tsCreated john-0.12802932895402364@example.comThere are 1 users in the database.$ jstime run index.tsCreated john-0.8671308799782803@example.comThere are 2 users in the database.$ jstime run index.tsCreated john-0.4465968383115295@example.comThere are 3 users in the database.That’s it! Now that you’ve set up Prisma using JSTime, we recommend referring to the official Prisma docs as you continue to develop your application.