UNPKG

prisma-factory

Version:

Generates factories for your prisma models

126 lines (120 loc) 4.41 kB
var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getOwnPropSymbols = Object.getOwnPropertySymbols; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __propIsEnum = Object.prototype.propertyIsEnumerable; var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value; var __spreadValues = (a, b) => { for (var prop in b || (b = {})) if (__hasOwnProp.call(b, prop)) __defNormalProp(a, prop, b[prop]); if (__getOwnPropSymbols) for (var prop of __getOwnPropSymbols(b)) { if (__propIsEnum.call(b, prop)) __defNormalProp(a, prop, b[prop]); } return a; }; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod)); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/index.ts var src_exports = {}; __export(src_exports, { createFactory: () => createFactory }); module.exports = __toCommonJS(src_exports); // src/createFactory.ts var import_camel_case = require("camel-case"); // src/utils/getAttrs.ts var getAttrs = (attrs) => { return Object.fromEntries(Object.entries(attrs).map(([key, value]) => { if (typeof value === "object") { return [key, getAttrs(value)]; } if (typeof value === "function") { const result = value(); if (typeof result === "object") { return [key, getAttrs(result)]; } return [key, result]; } return [key, value]; })); }; // src/lib/prisma.ts var getPrismaClient = async (client) => { const { PrismaClient } = await Promise.resolve().then(() => __toESM(require(client))); const prisma2 = global.prisma || new PrismaClient(); if (process.env.NODE_ENV !== "production") global.prisma = prisma2; return prisma2; }; function buildPrismaIncludeFromAttrs(attrs) { const include = Object.keys(attrs).reduce((prev, curr) => { const value = attrs[curr]; const isObject = typeof value === "object" && value !== null; const isRelation = isObject && Object.keys(value).find((v) => v.match(/connect|create/)); if (isRelation) { prev[curr] = true; } return prev; }, /* @__PURE__ */ Object.create(null)); const hasInclude = Object.keys(include).length; return hasInclude ? include : void 0; } // src/createFactory.ts function createFactory(modelName, defaultAttrs, options = {}, hooks = {}) { const build = (attrs = {}) => { const mappedAttrs = getAttrs(defaultAttrs); const data = __spreadValues(__spreadValues({}, mappedAttrs), attrs); return data; }; const create = async (attrs = {}) => { var _a; const prismaClientPath = (_a = options.client) != null ? _a : "@prisma/client"; const { PrismaClient } = await getPrismaClient(prismaClientPath); if (!global["prisma"]) { global["prisma"] = new PrismaClient(); } prisma = global["prisma"]; let data = build(attrs); const prismaOptions = {}; const includes = buildPrismaIncludeFromAttrs(attrs); if (includes) prismaOptions.include = includes; if (hooks.beforeCreate) { data = hooks.beforeCreate(data); } const prismaModel = (0, import_camel_case.camelCase)(modelName); let result = await prisma[prismaModel].create(__spreadValues({ data }, prismaOptions)); if (hooks.afterCreate) { result = hooks.afterCreate(result); } return result; }; return { build, create }; } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { createFactory });