first commit

This commit is contained in:
2024-04-02 22:22:47 +03:00
commit 4016f41435
21 changed files with 16378 additions and 0 deletions

BIN
prisma/demo-app.db Normal file

Binary file not shown.

View File

@ -0,0 +1,58 @@
-- CreateTable
CREATE TABLE "User" (
"id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
"email" TEXT NOT NULL,
"password" TEXT NOT NULL,
"createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP
);
-- CreateTable
CREATE TABLE "Role" (
"id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
"name" TEXT NOT NULL,
"createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP
);
-- CreateTable
CREATE TABLE "Permission" (
"id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
"name" TEXT NOT NULL,
"createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
"roleId" INTEGER NOT NULL,
CONSTRAINT "Permission_roleId_fkey" FOREIGN KEY ("roleId") REFERENCES "Role" ("id") ON DELETE RESTRICT ON UPDATE CASCADE
);
-- CreateTable
CREATE TABLE "UsersRoles" (
"userId" INTEGER NOT NULL,
"roleId" INTEGER NOT NULL,
"assignedAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
"assignedBy" TEXT NOT NULL,
PRIMARY KEY ("userId", "roleId"),
CONSTRAINT "UsersRoles_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User" ("id") ON DELETE RESTRICT ON UPDATE CASCADE,
CONSTRAINT "UsersRoles_roleId_fkey" FOREIGN KEY ("roleId") REFERENCES "Role" ("id") ON DELETE RESTRICT ON UPDATE CASCADE
);
-- CreateTable
CREATE TABLE "Product" (
"id" TEXT NOT NULL PRIMARY KEY,
"createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" DATETIME NOT NULL,
"title" TEXT NOT NULL,
"published" BOOLEAN NOT NULL DEFAULT false,
"authorId" INTEGER NOT NULL,
CONSTRAINT "Product_authorId_fkey" FOREIGN KEY ("authorId") REFERENCES "User" ("id") ON DELETE RESTRICT ON UPDATE CASCADE
);
-- CreateIndex
CREATE UNIQUE INDEX "User_email_key" ON "User"("email");
-- CreateIndex
CREATE UNIQUE INDEX "Role_name_key" ON "Role"("name");
-- CreateIndex
CREATE UNIQUE INDEX "Permission_name_key" ON "Permission"("name");

View File

@ -0,0 +1,3 @@
# Please do not edit this file manually
# It should be added in your version-control system (i.e. Git)
provider = "sqlite"

72
prisma/schema.prisma Normal file
View File

@ -0,0 +1,72 @@
//////////////////////////////////////////////////////////////////////////////////////////////
// DO NOT MODIFY THIS FILE //
// This file is automatically generated by ZenStack CLI and should not be manually updated. //
//////////////////////////////////////////////////////////////////////////////////////////////
datasource db {
provider = "sqlite"
url = env("DATABASE_URL")
}
generator client {
provider = "prisma-client-js"
}
generator typegraphql {
provider = "typegraphql-prisma-nestjs"
output = "../src/type-graphql/generated"
}
/// @@allow('create,read', true)
/// @@allow('update', auth() == this && future().email == email)
model User {
id Int @id() @default(autoincrement())
email String @unique()
/// @omit
password String
createdAt DateTime @default(now())
updatedAt DateTime @default(now())
role UsersRoles[]
product Product[]
}
model Role {
id Int @id() @default(autoincrement())
name String @unique()
createdAt DateTime @default(now())
updatedAt DateTime @default(now())
permission Permission[]
user UsersRoles[]
}
model Permission {
id Int @id() @default(autoincrement())
name String @unique()
createdAt DateTime @default(now())
updatedAt DateTime @default(now())
role Role @relation(fields: [roleId], references: [id])
roleId Int
}
model UsersRoles {
user User @relation(fields: [userId], references: [id])
userId Int
role Role @relation(fields: [roleId], references: [id])
roleId Int
assignedAt DateTime @default(now())
assignedBy String
@@id([userId, roleId])
}
/// @@allow('all', auth() == author)
/// @@allow('read', auth() != null && published)
model Product {
id String @id() @default(cuid())
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt()
title String
published Boolean @default(false)
author User @relation(fields: [authorId], references: [id])
authorId Int
}

88
prisma/seed.ts Normal file
View File

@ -0,0 +1,88 @@
import { PrismaClient } from '@prisma/client';
import * as bcrypt from 'bcrypt';
const prisma = new PrismaClient();
async function main() {
const password = await bcrypt.hash('asdf', 10);
// bcrypt.hash('asdf', 10, async (err, password) => {
const clinetRole = await prisma.role.upsert({
where: { name: 'client' },
update: {},
create: {
name: 'client',
},
});
const adminRole = await prisma.role.upsert({
where: { name: 'client' },
update: {},
create: {
name: 'admin',
},
});
const customer = await prisma.user.upsert({
where: { email: 'client@example.com' },
update: {},
create: {
email: 'client@example.com',
password: password,
product: {
create: [
{
title: 'Product 1',
published: true,
},
{
title: 'Product 2',
published: true,
},
],
},
},
});
const admin = await prisma.user.upsert({
where: { email: 'admin@example.com' },
update: {},
create: {
email: 'admin@example.com',
password: password,
},
});
await prisma.usersRoles.upsert({
where: {
userId_roleId: {
userId: customer.id,
roleId: clinetRole.id,
},
},
update: {},
create: {
roleId: clinetRole.id,
userId: customer.id,
assignedBy: 'seed.ts',
},
});
await prisma.usersRoles.upsert({
where: {
userId_roleId: {
userId: admin.id,
roleId: adminRole.id,
},
},
update: {},
create: {
roleId: adminRole.id,
userId: admin.id,
assignedBy: 'seed.ts',
},
});
console.log({ customer, admin });
// }
}
main()
.then(async () => {
await prisma.$disconnect();
})
.catch(async (e) => {
console.error(e);
await prisma.$disconnect();
process.exit(1);
});