prisma-extension-create-with-slug
Advanced tools
Comparing version
@@ -1,4 +0,4 @@ | ||
import { Prisma } from '@prisma/client/extension'; | ||
import { Prisma } from "@prisma/client/extension"; | ||
type Args = { | ||
sourceField: string; | ||
sourceField: string[] | string; | ||
targetField?: string; | ||
@@ -5,0 +5,0 @@ unique?: boolean; |
@@ -25,11 +25,27 @@ "use strict"; | ||
return __awaiter(this, void 0, void 0, function* () { | ||
if (args.data[args.sourceField] === undefined) { | ||
const errorMessage = "Cannot create slug, sourceField " + args.sourceField + " is missing in create data"; | ||
if (Array.isArray(args.sourceField)) { | ||
args.sourceField.forEach((field) => { | ||
if (args.data[field] === undefined) { | ||
const errorMessage = "Cannot create slug, sourceField " + | ||
field + | ||
" is missing in create data"; | ||
throw new Error(errorMessage); | ||
} | ||
}); | ||
} | ||
else if (args.data[args.sourceField] === undefined) { | ||
const errorMessage = "Cannot create slug, sourceField " + | ||
args.sourceField + | ||
" is missing in create data"; | ||
throw new Error(errorMessage); | ||
} | ||
const sourceField = args.sourceField ? args.sourceField : 'title'; | ||
const targetField = args.targetField ? args.targetField : 'slug'; | ||
const source = Array.isArray(args.sourceField) | ||
? args.sourceField | ||
.map((field) => args.data[field]) | ||
.join("-") | ||
: args.data[args.sourceField]; | ||
const targetField = args.targetField ? args.targetField : "slug"; | ||
const unique = args.unique === true ? true : false; | ||
const ctx = extension_1.Prisma.getExtensionContext(this); | ||
let slug = (0, slugify_1.default)(args.data[sourceField], { | ||
let slug = (0, slugify_1.default)(source, { | ||
lower: true, | ||
@@ -58,3 +74,3 @@ strict: true, | ||
const result = yield ctx.create({ | ||
data: Object.assign(Object.assign({}, args.data), { [targetField]: slug }) | ||
data: Object.assign(Object.assign({}, args.data), { [targetField]: slug }), | ||
}); | ||
@@ -61,0 +77,0 @@ return result; |
import { PrismaClient } from "@prisma/client"; | ||
import { createWithSlugFn } from "../dist"; | ||
const prisma = new PrismaClient().$extends(createWithSlugFn()) | ||
const prisma = new PrismaClient().$extends(createWithSlugFn()); | ||
async function main() { | ||
const post = await prisma.post.createWithSlug({data: | ||
{ | ||
slug: "Hello World!", | ||
const post = await prisma.post.createWithSlug({ | ||
data: { | ||
title: "Hello World!", | ||
name: "test", | ||
updatedAt: new Date(), | ||
}, | ||
sourceField: 'title', | ||
targetField: 'slug', | ||
unique: true} | ||
) | ||
sourceField: "title", | ||
targetField: "slug", | ||
unique: true, | ||
}); | ||
console.log({ post }) | ||
console.log({ post }); | ||
} | ||
main() | ||
main(); |
@@ -7,6 +7,6 @@ { | ||
"dependencies": { | ||
"@prisma/client": "^5.2.0" | ||
"@prisma/client": "^5.5.2" | ||
}, | ||
"devDependencies": { | ||
"prisma": "^5.2.0", | ||
"prisma": "^5.5.2", | ||
"ts-node": "^10.9.1", | ||
@@ -13,0 +13,0 @@ "typescript": "^5.0.0" |
{ | ||
"name": "prisma-extension-create-with-slug", | ||
"version": "0.0.4", | ||
"version": "0.0.5", | ||
"author": "mlmetzner", | ||
@@ -24,6 +24,7 @@ "description": "Prisma Client Extension to automatically generate a unique slug creating a new Database entry. ", | ||
"peerDependencies": { | ||
"@prisma/client": "^5.2.0" | ||
"@prisma/client": "^5.5.2" | ||
}, | ||
"devDependencies": { | ||
"@prisma/client": "latest", | ||
"prettier": "3.0.3", | ||
"prisma": "latest", | ||
@@ -30,0 +31,0 @@ "typescript": "^5.2.2" |
@@ -1,14 +0,12 @@ | ||
import { Prisma } from '@prisma/client/extension'; | ||
import slugify from 'slugify'; | ||
import { Prisma } from "@prisma/client/extension"; | ||
import slugify from "slugify"; | ||
type Args = { | ||
sourceField: string, | ||
targetField?: string, | ||
unique?: boolean, | ||
} | ||
sourceField: string[] | string; | ||
targetField?: string; | ||
unique?: boolean; | ||
}; | ||
// omit generic type F from data | ||
export const createWithSlugFn = () => | ||
@@ -19,21 +17,39 @@ Prisma.defineExtension({ | ||
$allModels: { | ||
async createWithSlug<T,A>( | ||
async createWithSlug<T, A>( | ||
this: T, | ||
args: Omit<Prisma.Args<T, 'create'> & Args, 'data'> & {data: Partial<Prisma.Args<T, 'create'>['data']> } | ||
): Promise<Prisma.Result<T,A, 'create'>> { | ||
if (args.data[args.sourceField] === undefined) { | ||
const errorMessage = "Cannot create slug, sourceField " + args.sourceField + " is missing in create data" | ||
throw new Error(errorMessage) | ||
args: Omit<Prisma.Args<T, "create"> & Args, "data"> & { | ||
data: Partial<Prisma.Args<T, "create">["data"]>; | ||
}, | ||
): Promise<Prisma.Result<T, A, "create">> { | ||
if (Array.isArray(args.sourceField)) { | ||
args.sourceField.forEach((field: string) => { | ||
if (args.data[field] === undefined) { | ||
const errorMessage = | ||
"Cannot create slug, sourceField " + | ||
field + | ||
" is missing in create data"; | ||
throw new Error(errorMessage); | ||
} | ||
}); | ||
} else if (args.data[args.sourceField] === undefined) { | ||
const errorMessage = | ||
"Cannot create slug, sourceField " + | ||
args.sourceField + | ||
" is missing in create data"; | ||
throw new Error(errorMessage); | ||
} | ||
const sourceField = args.sourceField ? args.sourceField : 'title' | ||
const targetField = args.targetField ? args.targetField : 'slug' | ||
const unique = args.unique === true ? true : false | ||
const ctx = Prisma.getExtensionContext(this) | ||
let slug = slugify((args.data as any)[sourceField],{ | ||
const source = Array.isArray(args.sourceField) | ||
? args.sourceField | ||
.map((field: string) => (args.data as any)[field]) | ||
.join("-") | ||
: (args.data as any)[args.sourceField]; | ||
const targetField = args.targetField ? args.targetField : "slug"; | ||
const unique = args.unique === true ? true : false; | ||
const ctx = Prisma.getExtensionContext(this); | ||
let slug = slugify(source, { | ||
lower: true, | ||
strict: true, | ||
}) | ||
}); | ||
if (unique) { | ||
let number = 0 | ||
let number = 0; | ||
let count = await (ctx as any).count({ | ||
@@ -43,5 +59,5 @@ where: { | ||
}, | ||
}) | ||
}); | ||
while (count > 0) { | ||
number += 1 | ||
number += 1; | ||
count = await (ctx as any).count({ | ||
@@ -51,8 +67,7 @@ where: { | ||
}, | ||
}) | ||
}); | ||
} | ||
if (number > 0) { | ||
slug = `${slug}-${number}` | ||
slug = `${slug}-${number}`; | ||
} | ||
} | ||
@@ -63,7 +78,8 @@ const result = await (ctx as any).create({ | ||
[targetField]: slug, | ||
}}) | ||
return result | ||
}, | ||
}); | ||
return result; | ||
}, | ||
}, | ||
}, | ||
}) | ||
}); |
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
19277
89.18%11
10%445
164.88%4
33.33%1
Infinity%