Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 4 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
"no-undef": "off",
"no-use-before-define": "off",
"no-unused-vars": "off",
"@typescript-eslint/no-unused-vars": ["error"]
"@typescript-eslint/no-unused-vars": ["error"],
"max-lines-per-function": "off",
"consistent-return": "off",
"jest/no-if": "off"
}
}
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,6 @@ node_modules/

# Build output directory
dist

# Test coverage directory
coverage
6 changes: 6 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/** @type {import('ts-jest').JestConfigWithTsJest} */
module.exports = {
preset: "ts-jest",
testEnvironment: "node",
testRegex: ".+\\.test\\.ts$",
};
11 changes: 10 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@
"build": "npm-run-all build:cjs build:esm",
"build:cjs": "tsc -p tsconfig.build.json",
"build:esm": "tsc -p tsconfig.esm.json",
"test": "prisma generate && jest",
"lint": "eslint ./src --fix --ext .ts",
"typecheck": "npm run build:cjs -- --noEmit && npm run build:esm -- --noEmit",
"validate": "kcd-scripts validate lint,typecheck",
"validate": "kcd-scripts validate lint,typecheck,test",
"semantic-release": "semantic-release",
"doctoc": "doctoc ."
},
Expand All @@ -32,14 +33,22 @@
"@prisma/client": "*"
},
"devDependencies": {
"@prisma/client": "^4.8.1",
"@types/faker": "^5.5.9",
"@types/jest": "^29.2.5",
"@types/lodash": "^4.14.185",
"@typescript-eslint/eslint-plugin": "^4.14.0",
"@typescript-eslint/parser": "^4.14.0",
"doctoc": "^2.2.0",
"dotenv": "^16.0.3",
"eslint": "^7.6.0",
"faker": "^5.0.0",
"jest": "^29.3.1",
"kcd-scripts": "^5.0.0",
"npm-run-all": "^4.1.5",
"prisma": "^4.8.1",
"semantic-release": "^17.0.2",
"ts-jest": "^29.0.3",
"ts-node": "^9.1.1",
"typescript": "^4.1.3"
},
Expand Down
50 changes: 50 additions & 0 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
generator client {
provider = "prisma-client-js"
}

datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}

model User {
id Int @id @default(autoincrement())
email String @unique
name String?
posts Post[]
profile Profile?
comments Comment[]
}

model Post {
id Int @id @default(autoincrement())
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
published Boolean @default(false)
title String
content String?
author User @relation(fields: [authorId], references: [id])
authorId Int
comments Comment[]
}

model Comment {
id Int @id @default(autoincrement())
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
content String
author User @relation(fields: [authorId], references: [id])
authorId Int
post Post @relation(fields: [postId], references: [id])
postId Int
repliedTo Comment? @relation("replies", fields: [repliedToId], references: [id])
repliedToId Int?
replies Comment[] @relation("replies")
}

model Profile {
id Int @id @default(autoincrement())
bio String?
user User @relation(fields: [userId], references: [id])
userId Int @unique
}
24 changes: 6 additions & 18 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable import/no-unresolved */
// @ts-expect-error unable to generate prisma client before building
// @ts-ignore unable to generate prisma client before building
import { Prisma } from '@prisma/client';

import get from 'lodash/get';
Expand All @@ -9,20 +9,8 @@ if (!Prisma.dmmf) {
throw new Error('Prisma DMMF not found, please generate Prisma client using `npx prisma generate`');
}

type Field = {
kind: "scalar" | "object" | "enum" | "list";
relationName?: string;
name: string;
type: string;
}

type Model = {
name: string;
fields: Field[];
}

const relationsByModel: Record<string, Field[]> = {};
Prisma.dmmf.datamodel.models.forEach((model: Model) => {
const relationsByModel: Record<string, Prisma.DMMF.Field[]> = {};
Prisma.dmmf.datamodel.models.forEach((model: Prisma.DMMF.Model) => {
relationsByModel[model.name] = model.fields.filter(
(field) => field.kind === 'object' && field.relationName
);
Expand Down Expand Up @@ -71,7 +59,7 @@ function isWriteOperation(key: any): key is NestedAction {

function extractWriteInfo(
params: NestedParams,
model: string,
model: Prisma.ModelName,
argPath: string
): WriteInfo[] {
const arg = get(params.args, argPath, {});
Expand All @@ -92,9 +80,9 @@ function extractWriteInfo(

function extractNestedWriteInfo(
params: NestedParams,
relation: Field
relation: Prisma.DMMF.Field
): WriteInfo[] {
const model = relation.type;
const model = relation.type as Prisma.ModelName;

switch (params.action) {
case 'upsert':
Expand Down
Loading