@suss/packs 0.34.0 → 0.34.1

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/prisma.js CHANGED
@@ -6,7 +6,7 @@ import {
6
6
  } from "ts-morph";
7
7
  import { z } from "zod";
8
8
  import { receiverTypesOf, writtenNodeOf } from "@suss/adapter-typescript";
9
- import { storageBinding } from "@suss/behavioral-ir";
9
+ import { storageBinding as storageBinding2 } from "@suss/behavioral-ir";
10
10
  import { scopeOption, storageSystemOption } from "@suss/extractor";
11
11
  import {
12
12
  compile,
@@ -16,8 +16,50 @@ import {
16
16
  } from "@suss/recognize";
17
17
  import fs from "fs";
18
18
  import path from "path";
19
- import { getSchema } from "@mrleebo/prisma-ast";
19
+ import { getSchema as getSchema2 } from "@mrleebo/prisma-ast";
20
20
  import { ts } from "ts-morph";
21
+
22
+ // ../contract/prisma/dist/index.js
23
+ import { getSchema } from "@mrleebo/prisma-ast";
24
+ import { storageBinding } from "@suss/behavioral-ir";
25
+ var PROVIDER_TO_SYSTEM = {
26
+ postgresql: "postgresql",
27
+ postgres: "postgresql",
28
+ mysql: "mysql",
29
+ sqlite: "sqlite"
30
+ };
31
+ function prismaStorageSystem(source) {
32
+ try {
33
+ return storageSystemIn(getSchema(source).list);
34
+ } catch {
35
+ return null;
36
+ }
37
+ }
38
+ function storageSystemIn(list) {
39
+ for (const node of list) {
40
+ if (node.type !== "datasource") {
41
+ continue;
42
+ }
43
+ const provider = readProviderString(node);
44
+ if (provider !== null && provider in PROVIDER_TO_SYSTEM) {
45
+ return PROVIDER_TO_SYSTEM[provider];
46
+ }
47
+ }
48
+ return null;
49
+ }
50
+ function readProviderString(ds) {
51
+ for (const a of ds.assignments) {
52
+ if (a.key !== "provider") {
53
+ continue;
54
+ }
55
+ if (typeof a.value === "string") {
56
+ return a.value.replace(/^"|"$/g, "");
57
+ }
58
+ }
59
+ return null;
60
+ }
61
+
62
+ // ../framework/prisma/dist/index.js
21
63
  var CONFIG_FILES = [
22
64
  "prisma.config.ts",
23
65
  "prisma.config.mts",
@@ -30,22 +72,40 @@ var CONFIG_FILES = [
30
72
  var DEFAULT_SCHEMAS = [path.join("prisma", "schema.prisma"), "schema.prisma"];
31
73
  var readProjects = /* @__PURE__ */ new Map();
32
74
  function generatedClientDirs(fromDir) {
75
+ return readProjectAt(fromDir)?.dirs ?? [];
76
+ }
77
+ function schemaStorageSystem(fromDir) {
78
+ return readProjectAt(fromDir)?.storageSystem ?? null;
79
+ }
80
+ function readProjectAt(fromDir) {
33
81
  const root = projectRootOf(fromDir);
34
82
  if (root === null) {
35
- return [];
83
+ return null;
36
84
  }
37
85
  const known = readProjects.get(root);
38
86
  if (known !== void 0 && unchanged(known.readFrom)) {
39
- return known.dirs;
87
+ return known;
40
88
  }
41
89
  const schemaFiles = schemaFilesOf(root);
42
90
  const readFrom = /* @__PURE__ */ new Map();
43
91
  for (const file of [...projectFilesOf(root), ...schemaFiles]) {
44
92
  readFrom.set(file, changedAt(file));
45
93
  }
46
- const dirs = schemaFiles.flatMap(outputDirsIn);
47
- readProjects.set(root, { dirs, readFrom });
48
- return dirs;
94
+ const sources = schemaFiles.map((file) => ({ file, text: textOf(file) }));
95
+ const project = {
96
+ dirs: sources.flatMap(outputDirsIn),
97
+ storageSystem: sources.map(({ text }) => prismaStorageSystem(text)).find((system) => system !== null) ?? null,
98
+ readFrom
99
+ };
100
+ readProjects.set(root, project);
101
+ return project;
102
+ }
103
+ function textOf(file) {
104
+ try {
105
+ return fs.readFileSync(file, "utf8");
106
+ } catch {
107
+ return "";
108
+ }
49
109
  }
50
110
  function isInGeneratedClient(filePath) {
51
111
  return generatedClientDirs(path.dirname(filePath)).some(
@@ -171,10 +231,11 @@ function packageJsonSchema(root) {
171
231
  return null;
172
232
  }
173
233
  }
174
- function outputDirsIn(schemaFile) {
234
+ function outputDirsIn(schema) {
235
+ const schemaFile = schema.file;
175
236
  let list;
176
237
  try {
177
- list = getSchema(fs.readFileSync(schemaFile, "utf8")).list;
238
+ list = getSchema2(schema.text).list;
178
239
  } catch {
179
240
  return [];
180
241
  }
@@ -219,9 +280,9 @@ var PRISMA_WRITE_METHODS = /* @__PURE__ */ new Set([
219
280
  ]);
220
281
  var optionsSchema = z.object({
221
282
  /**
222
- * The storage system the calls target, `"postgresql"` when unset. It
223
- * has to match the `storageSystem` on the schema reader's summaries,
224
- * or the calls do not pair.
283
+ * The storage system the calls target. When unset, the project
284
+ * schema's datasource decides, then `"postgresql"`. The README says
285
+ * why it has to match the schema reader's summaries.
225
286
  */
226
287
  storageSystem: storageSystemOption.optional(),
227
288
  /**
@@ -231,10 +292,18 @@ var optionsSchema = z.object({
231
292
  scope: scopeOption.optional()
232
293
  }).strict();
233
294
  function makeRecognizer(opts) {
234
- const storageSystem = opts.storageSystem ?? "postgresql";
235
295
  const scope = opts.scope ?? "default";
236
- return (call, ctx) => recognizePrismaCall(call, ctx, storageSystem, scope);
296
+ return (call, ctx) => recognizePrismaCall(
297
+ call,
298
+ ctx,
299
+ storageSystemFor(opts, call.getSourceFile()),
300
+ scope
301
+ );
237
302
  }
303
+ function storageSystemFor(opts, file) {
304
+ return opts.storageSystem ?? schemaStorageSystem(path2.dirname(file.getFilePath())) ?? DEFAULT_STORAGE_SYSTEM;
305
+ }
306
+ var DEFAULT_STORAGE_SYSTEM = "postgresql";
238
307
  function recognizePrismaCall(call, ctx, storageSystem, scope) {
239
308
  const callNode = call;
240
309
  const recognizerCtx = ctx;
@@ -266,7 +335,7 @@ function recognizePrismaCall(call, ctx, storageSystem, scope) {
266
335
  const optionsArg = readObjectArg(argsShape[0]);
267
336
  const fields = extractFields(optionsArg, kind);
268
337
  const selector = extractSelector(optionsArg);
269
- const binding = storageBinding({
338
+ const binding = storageBinding2({
270
339
  recognition: "@suss/framework-prisma",
271
340
  storageSystem,
272
341
  scope,
@@ -614,8 +683,7 @@ function extractSelector(optionsArg) {
614
683
  return keys.length > 0 ? keys : null;
615
684
  }
616
685
  var STATEMENT = { statement: { at: 0 } };
617
- function rawStatements(options) {
618
- const provider = options.storageSystem ?? "postgresql";
686
+ function rawStatements(options, provider) {
619
687
  return sqlStatements({
620
688
  system: provider,
621
689
  dialect: provider,
@@ -628,8 +696,26 @@ function rawStatements(options) {
628
696
  $executeRawUnsafe: STATEMENT
629
697
  }).example('prisma.$queryRawUnsafe("SELECT id, email FROM users")');
630
698
  }
699
+ function rawRecognizer(options) {
700
+ const bySystem = /* @__PURE__ */ new Map();
701
+ return (call, ctx) => {
702
+ const system = storageSystemFor(options, call.getSourceFile());
703
+ let recognizer = bySystem.get(system);
704
+ if (recognizer === void 0) {
705
+ recognizer = compile(
706
+ rawStatements(options, system).declared,
707
+ "@suss/framework-prisma"
708
+ );
709
+ bySystem.set(system, recognizer);
710
+ }
711
+ return recognizer(call, ctx);
712
+ };
713
+ }
631
714
  function prismaFramework(options = {}) {
632
- const raw = rawStatements(options);
715
+ const raw = rawStatements(
716
+ options,
717
+ options.storageSystem ?? DEFAULT_STORAGE_SYSTEM
718
+ );
633
719
  return {
634
720
  name: "prisma",
635
721
  protocol: "in-process",
@@ -648,7 +734,7 @@ function prismaFramework(options = {}) {
648
734
  invocationRecognizers: [makeRecognizer(options)],
649
735
  // A tagged template is not an invocation, so the raw chain runs on
650
736
  // the access walk, which visits calls as well.
651
- accessRecognizers: [compile(raw.declared, "@suss/framework-prisma")],
737
+ accessRecognizers: [rawRecognizer(options)],
652
738
  declarations: declarationsIn([raw])
653
739
  };
654
740
  }