omni-rest 0.2.0 → 0.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/cli.mjs CHANGED
@@ -3,7 +3,6 @@ import * as fs3 from 'fs';
3
3
  import fs3__default from 'fs';
4
4
  import * as path from 'path';
5
5
  import path__default from 'path';
6
- import { Prisma } from '@prisma/client';
7
6
  import * as readline from 'readline';
8
7
 
9
8
  var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
@@ -12,6 +11,8 @@ var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require
12
11
  if (typeof require !== "undefined") return require.apply(this, arguments);
13
12
  throw Error('Dynamic require of "' + x + '" is not supported');
14
13
  });
14
+
15
+ // src/introspect.ts
15
16
  function getModels(prisma) {
16
17
  let raw;
17
18
  if (prisma?._runtimeDataModel?.models) {
@@ -26,9 +27,13 @@ function getModels(prisma) {
26
27
  }));
27
28
  }
28
29
  if (!raw) {
29
- const dmmfModels = Prisma?.dmmf?.datamodel?.models || __require("@prisma/client")?.Prisma?.dmmf?.datamodel?.models;
30
- if (dmmfModels) {
31
- raw = dmmfModels;
30
+ try {
31
+ const prismaModule = __require("@prisma/client");
32
+ const dmmfModels = prismaModule?.Prisma?.dmmf?.datamodel?.models;
33
+ if (dmmfModels) {
34
+ raw = dmmfModels;
35
+ }
36
+ } catch {
32
37
  }
33
38
  }
34
39
  if (!raw) {
@@ -1503,9 +1508,11 @@ function write(filePath, content) {
1503
1508
  console.log(COLORS2.green(` \u2713 Written: ${filePath}`));
1504
1509
  }
1505
1510
  function createPrismaClient() {
1511
+ const customClient = tryLoadFromSchemaOutput();
1512
+ if (customClient) return customClient;
1506
1513
  try {
1507
- const PrismaClientPath = path__default.resolve(cwd, "node_modules/@prisma/client");
1508
- const { PrismaClient } = __require(PrismaClientPath);
1514
+ const clientPath = __require.resolve("@prisma/client", { paths: [cwd] });
1515
+ const { PrismaClient } = __require(clientPath);
1509
1516
  return new PrismaClient();
1510
1517
  } catch {
1511
1518
  throw new Error(
@@ -1513,6 +1520,53 @@ function createPrismaClient() {
1513
1520
  );
1514
1521
  }
1515
1522
  }
1523
+ function tryLoadFromSchemaOutput() {
1524
+ const schemaPaths = [
1525
+ path__default.resolve(cwd, "prisma/schema.prisma"),
1526
+ path__default.resolve(cwd, "schema.prisma")
1527
+ ];
1528
+ for (const schemaPath of schemaPaths) {
1529
+ if (!fs3__default.existsSync(schemaPath)) continue;
1530
+ try {
1531
+ const schema = fs3__default.readFileSync(schemaPath, "utf-8");
1532
+ const match = schema.match(/output\s*=\s*["']([^"']+)["']/);
1533
+ if (!match) continue;
1534
+ const outputDir = path__default.resolve(path__default.dirname(schemaPath), match[1]);
1535
+ const indexPath = path__default.join(outputDir, "index.js");
1536
+ if (!fs3__default.existsSync(indexPath)) continue;
1537
+ const mod = __require(indexPath);
1538
+ const PrismaClient = mod.PrismaClient ?? mod.default?.PrismaClient;
1539
+ if (!PrismaClient) continue;
1540
+ const providerMatch = schema.match(/provider\s*=\s*["']([^"']+)["']/);
1541
+ const provider = providerMatch?.[1] ?? "postgresql";
1542
+ const dummyUrl = getDummyUrl(provider);
1543
+ try {
1544
+ return new PrismaClient({ datasourceUrl: dummyUrl });
1545
+ } catch {
1546
+ return new PrismaClient();
1547
+ }
1548
+ } catch {
1549
+ continue;
1550
+ }
1551
+ }
1552
+ return null;
1553
+ }
1554
+ function getDummyUrl(provider) {
1555
+ switch (provider) {
1556
+ case "mysql":
1557
+ return "mysql://root:root@localhost:3306/dummy";
1558
+ case "sqlite":
1559
+ return "file:./dummy.db";
1560
+ case "sqlserver":
1561
+ return "sqlserver://localhost:1433;database=dummy;user=sa;password=dummy";
1562
+ case "mongodb":
1563
+ return "mongodb://localhost:27017/dummy";
1564
+ case "cockroachdb":
1565
+ return "postgresql://root@localhost:26257/dummy";
1566
+ default:
1567
+ return "postgresql://postgres:postgres@localhost:5432/dummy";
1568
+ }
1569
+ }
1516
1570
  var USAGE = `
1517
1571
  Usage:
1518
1572
  npx omni-rest generate Generate both Zod schemas and OpenAPI spec