pocketbase-zod-schema 0.7.0 → 0.7.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.
Files changed (70) hide show
  1. package/CHANGELOG.md +14 -0
  2. package/dist/cli/index.cjs +449 -50
  3. package/dist/cli/index.cjs.map +1 -1
  4. package/dist/cli/index.d.cts +1 -1
  5. package/dist/cli/index.d.ts +1 -1
  6. package/dist/cli/index.js +449 -50
  7. package/dist/cli/index.js.map +1 -1
  8. package/dist/cli/migrate.cjs +455 -53
  9. package/dist/cli/migrate.cjs.map +1 -1
  10. package/dist/cli/migrate.js +455 -53
  11. package/dist/cli/migrate.js.map +1 -1
  12. package/dist/cli/utils/index.cjs +8 -1
  13. package/dist/cli/utils/index.cjs.map +1 -1
  14. package/dist/cli/utils/index.d.cts +1 -1
  15. package/dist/cli/utils/index.d.ts +1 -1
  16. package/dist/cli/utils/index.js +8 -1
  17. package/dist/cli/utils/index.js.map +1 -1
  18. package/dist/index.cjs +90 -1
  19. package/dist/index.cjs.map +1 -1
  20. package/dist/index.d.cts +1 -1
  21. package/dist/index.d.ts +1 -1
  22. package/dist/index.js +87 -2
  23. package/dist/index.js.map +1 -1
  24. package/dist/migration/analyzer.cjs +87 -14
  25. package/dist/migration/analyzer.cjs.map +1 -1
  26. package/dist/migration/analyzer.d.cts +12 -4
  27. package/dist/migration/analyzer.d.ts +12 -4
  28. package/dist/migration/analyzer.js +87 -15
  29. package/dist/migration/analyzer.js.map +1 -1
  30. package/dist/migration/diff.cjs +64 -9
  31. package/dist/migration/diff.cjs.map +1 -1
  32. package/dist/migration/diff.d.cts +12 -2
  33. package/dist/migration/diff.d.ts +12 -2
  34. package/dist/migration/diff.js +64 -10
  35. package/dist/migration/diff.js.map +1 -1
  36. package/dist/migration/generator.cjs +206 -39
  37. package/dist/migration/generator.cjs.map +1 -1
  38. package/dist/migration/generator.d.cts +54 -2
  39. package/dist/migration/generator.d.ts +54 -2
  40. package/dist/migration/generator.js +203 -40
  41. package/dist/migration/generator.js.map +1 -1
  42. package/dist/migration/index.cjs +501 -63
  43. package/dist/migration/index.cjs.map +1 -1
  44. package/dist/migration/index.d.cts +2 -2
  45. package/dist/migration/index.d.ts +2 -2
  46. package/dist/migration/index.js +501 -63
  47. package/dist/migration/index.js.map +1 -1
  48. package/dist/migration/snapshot.cjs +147 -1
  49. package/dist/migration/snapshot.cjs.map +1 -1
  50. package/dist/migration/snapshot.d.cts +3 -1
  51. package/dist/migration/snapshot.d.ts +3 -1
  52. package/dist/migration/snapshot.js +147 -1
  53. package/dist/migration/snapshot.js.map +1 -1
  54. package/dist/migration/utils/index.d.cts +1 -1
  55. package/dist/migration/utils/index.d.ts +1 -1
  56. package/dist/schema.cjs +90 -1
  57. package/dist/schema.cjs.map +1 -1
  58. package/dist/schema.d.cts +128 -2
  59. package/dist/schema.d.ts +128 -2
  60. package/dist/schema.js +87 -2
  61. package/dist/schema.js.map +1 -1
  62. package/dist/server.cjs +545 -65
  63. package/dist/server.cjs.map +1 -1
  64. package/dist/server.d.cts +2 -2
  65. package/dist/server.d.ts +2 -2
  66. package/dist/server.js +542 -66
  67. package/dist/server.js.map +1 -1
  68. package/dist/{types-CWHV6ATd.d.cts → types-CnzfX6JH.d.cts} +20 -2
  69. package/dist/{types-C2nGWHLV.d.ts → types-Do3jyFBm.d.ts} +20 -2
  70. package/package.json +1 -1
@@ -1,5 +1,5 @@
1
1
  import { z } from 'zod';
2
- import { F as FieldDefinition, C as CollectionSchema, S as SchemaDefinition } from '../types-CWHV6ATd.cjs';
2
+ import { F as FieldDefinition, C as CollectionSchema, S as SchemaDefinition } from '../types-CnzfX6JH.cjs';
3
3
  import '../fields-B96iGprI.cjs';
4
4
 
5
5
  /**
@@ -111,9 +111,17 @@ declare function extractCollectionNameFromSchema(zodSchema: z.ZodTypeAny): strin
111
111
  * the collection type in the schema description
112
112
  *
113
113
  * @param zodSchema - The Zod schema to extract collection type from
114
- * @returns The collection type ("base" | "auth") if found in metadata, null otherwise
114
+ * @returns The collection type ("base" | "auth" | "view") if found in metadata, null otherwise
115
115
  */
116
- declare function extractCollectionTypeFromSchema(zodSchema: z.ZodTypeAny): "base" | "auth" | null;
116
+ declare function extractCollectionTypeFromSchema(zodSchema: z.ZodTypeAny): "base" | "auth" | "view" | null;
117
+ /**
118
+ * Extracts the SQL query backing a view collection from a Zod schema's metadata
119
+ * Set by defineView() (or defineCollection with type: "view")
120
+ *
121
+ * @param zodSchema - The Zod schema to extract the view query from
122
+ * @returns The SQL query if found in metadata, null otherwise
123
+ */
124
+ declare function extractViewQueryFromSchema(zodSchema: z.ZodTypeAny): string | null;
117
125
  /**
118
126
  * Extracts Zod schema definitions from a module
119
127
  *
@@ -234,4 +242,4 @@ declare class SchemaAnalyzer {
234
242
  convertZodSchemaToCollectionSchema(name: string, schema: z.ZodObject<any>): CollectionSchema;
235
243
  }
236
244
 
237
- export { DEFAULT_CONFIG, SchemaAnalyzer, type SchemaAnalyzerConfig, buildFieldDefinition, buildSchemaDefinition, convertZodSchemaToCollectionSchema, discoverSchemaFiles, extractCollectionNameFromSchema, extractCollectionTypeFromSchema, extractFieldDefinitions, extractIndexes, extractSchemaDefinitions, getCollectionNameFromFile, importSchemaModule, isAuthCollection, mergeConfig, parseSchemaFiles, resolveSchemaDir, selectSchemaForCollection };
245
+ export { DEFAULT_CONFIG, SchemaAnalyzer, type SchemaAnalyzerConfig, buildFieldDefinition, buildSchemaDefinition, convertZodSchemaToCollectionSchema, discoverSchemaFiles, extractCollectionNameFromSchema, extractCollectionTypeFromSchema, extractFieldDefinitions, extractIndexes, extractSchemaDefinitions, extractViewQueryFromSchema, getCollectionNameFromFile, importSchemaModule, isAuthCollection, mergeConfig, parseSchemaFiles, resolveSchemaDir, selectSchemaForCollection };
@@ -1,5 +1,5 @@
1
1
  import { z } from 'zod';
2
- import { F as FieldDefinition, C as CollectionSchema, S as SchemaDefinition } from '../types-C2nGWHLV.js';
2
+ import { F as FieldDefinition, C as CollectionSchema, S as SchemaDefinition } from '../types-Do3jyFBm.js';
3
3
  import '../fields-B96iGprI.js';
4
4
 
5
5
  /**
@@ -111,9 +111,17 @@ declare function extractCollectionNameFromSchema(zodSchema: z.ZodTypeAny): strin
111
111
  * the collection type in the schema description
112
112
  *
113
113
  * @param zodSchema - The Zod schema to extract collection type from
114
- * @returns The collection type ("base" | "auth") if found in metadata, null otherwise
114
+ * @returns The collection type ("base" | "auth" | "view") if found in metadata, null otherwise
115
115
  */
116
- declare function extractCollectionTypeFromSchema(zodSchema: z.ZodTypeAny): "base" | "auth" | null;
116
+ declare function extractCollectionTypeFromSchema(zodSchema: z.ZodTypeAny): "base" | "auth" | "view" | null;
117
+ /**
118
+ * Extracts the SQL query backing a view collection from a Zod schema's metadata
119
+ * Set by defineView() (or defineCollection with type: "view")
120
+ *
121
+ * @param zodSchema - The Zod schema to extract the view query from
122
+ * @returns The SQL query if found in metadata, null otherwise
123
+ */
124
+ declare function extractViewQueryFromSchema(zodSchema: z.ZodTypeAny): string | null;
117
125
  /**
118
126
  * Extracts Zod schema definitions from a module
119
127
  *
@@ -234,4 +242,4 @@ declare class SchemaAnalyzer {
234
242
  convertZodSchemaToCollectionSchema(name: string, schema: z.ZodObject<any>): CollectionSchema;
235
243
  }
236
244
 
237
- export { DEFAULT_CONFIG, SchemaAnalyzer, type SchemaAnalyzerConfig, buildFieldDefinition, buildSchemaDefinition, convertZodSchemaToCollectionSchema, discoverSchemaFiles, extractCollectionNameFromSchema, extractCollectionTypeFromSchema, extractFieldDefinitions, extractIndexes, extractSchemaDefinitions, getCollectionNameFromFile, importSchemaModule, isAuthCollection, mergeConfig, parseSchemaFiles, resolveSchemaDir, selectSchemaForCollection };
245
+ export { DEFAULT_CONFIG, SchemaAnalyzer, type SchemaAnalyzerConfig, buildFieldDefinition, buildSchemaDefinition, convertZodSchemaToCollectionSchema, discoverSchemaFiles, extractCollectionNameFromSchema, extractCollectionTypeFromSchema, extractFieldDefinitions, extractIndexes, extractSchemaDefinitions, extractViewQueryFromSchema, getCollectionNameFromFile, importSchemaModule, isAuthCollection, mergeConfig, parseSchemaFiles, resolveSchemaDir, selectSchemaForCollection };
@@ -1040,6 +1040,36 @@ function unwrapZodType(zodType) {
1040
1040
  }
1041
1041
  return unwrapped;
1042
1042
  }
1043
+ function stripLeadingComments(query) {
1044
+ let remaining = query.trim();
1045
+ while (remaining.length > 0) {
1046
+ if (remaining.startsWith("--")) {
1047
+ const newline = remaining.indexOf("\n");
1048
+ remaining = newline === -1 ? "" : remaining.slice(newline + 1).trim();
1049
+ continue;
1050
+ }
1051
+ if (remaining.startsWith("/*")) {
1052
+ const end = remaining.indexOf("*/");
1053
+ remaining = end === -1 ? "" : remaining.slice(end + 2).trim();
1054
+ continue;
1055
+ }
1056
+ break;
1057
+ }
1058
+ return remaining;
1059
+ }
1060
+ function validateViewQuery(collectionName, viewQuery) {
1061
+ if (typeof viewQuery !== "string" || viewQuery.trim() === "") {
1062
+ throw new Error(
1063
+ `View collection "${collectionName}" requires a non-empty viewQuery. Provide the SQL SELECT statement backing the view.`
1064
+ );
1065
+ }
1066
+ const body = stripLeadingComments(viewQuery);
1067
+ if (!/^(select|with)\b/i.test(body)) {
1068
+ throw new Error(
1069
+ `View collection "${collectionName}" has an invalid viewQuery: it must start with SELECT or WITH. Received: ${body.slice(0, 40)}${body.length > 40 ? "..." : ""}`
1070
+ );
1071
+ }
1072
+ }
1043
1073
  function getCollectionNameFromFile(filePath) {
1044
1074
  const filename = path.basename(filePath).replace(/\.(ts|js)$/, "");
1045
1075
  return toCollectionName(filename);
@@ -1063,13 +1093,26 @@ function extractCollectionTypeFromSchema(zodSchema) {
1063
1093
  }
1064
1094
  try {
1065
1095
  const metadata = JSON.parse(zodSchema.description);
1066
- if (metadata.type === "base" || metadata.type === "auth") {
1096
+ if (metadata.type === "base" || metadata.type === "auth" || metadata.type === "view") {
1067
1097
  return metadata.type;
1068
1098
  }
1069
1099
  } catch {
1070
1100
  }
1071
1101
  return null;
1072
1102
  }
1103
+ function extractViewQueryFromSchema(zodSchema) {
1104
+ if (!zodSchema.description) {
1105
+ return null;
1106
+ }
1107
+ try {
1108
+ const metadata = JSON.parse(zodSchema.description);
1109
+ if (typeof metadata.viewQuery === "string") {
1110
+ return metadata.viewQuery;
1111
+ }
1112
+ } catch {
1113
+ }
1114
+ return null;
1115
+ }
1073
1116
  function extractSchemaDefinitions(module, patterns = ["Schema", "InputSchema", "Collection"]) {
1074
1117
  const result = {};
1075
1118
  if (module.default instanceof z.ZodObject) {
@@ -1228,6 +1271,15 @@ function convertZodSchemaToCollectionSchema(collectionName, zodSchema) {
1228
1271
  const rawFields = extractFieldDefinitions(zodSchema);
1229
1272
  const explicitType = extractCollectionTypeFromSchema(zodSchema);
1230
1273
  const collectionType = explicitType ?? (isAuthCollection(rawFields) ? "auth" : "base");
1274
+ const isView = collectionType === "view";
1275
+ const viewQuery = extractViewQueryFromSchema(zodSchema);
1276
+ if (isView) {
1277
+ validateViewQuery(collectionName, viewQuery);
1278
+ } else if (viewQuery !== null) {
1279
+ console.warn(
1280
+ `[${collectionName}] viewQuery is only used by view collections and will be ignored. Use defineView() or set type: "view" to define a view collection.`
1281
+ );
1282
+ }
1231
1283
  const fields = rawFields.filter((f) => !["created", "updated"].includes(f.name)).map(({ name, zodType }) => buildFieldDefinition(name, zodType));
1232
1284
  if (collectionType === "auth") {
1233
1285
  const authSystemFields = [
@@ -1287,26 +1339,43 @@ function convertZodSchemaToCollectionSchema(collectionName, zodSchema) {
1287
1339
  }
1288
1340
  }
1289
1341
  const indexes = extractIndexes(zodSchema) || [];
1342
+ if (isView && indexes.length > 0) {
1343
+ throw new Error(
1344
+ `View collection "${collectionName}" cannot declare indexes. PocketBase view collections are backed by a SQL query and do not support indexes.`
1345
+ );
1346
+ }
1290
1347
  const permissionAnalyzer = new PermissionAnalyzer();
1291
1348
  let permissions = void 0;
1292
1349
  const schemaDescription = zodSchema.description;
1293
1350
  const extractedPermissions = permissionAnalyzer.extractPermissions(schemaDescription);
1294
1351
  if (extractedPermissions) {
1295
1352
  const resolvedPermissions = permissionAnalyzer.resolvePermissions(extractedPermissions);
1296
- const validationResults = permissionAnalyzer.validatePermissions(
1297
- collectionName,
1298
- resolvedPermissions,
1299
- fields,
1300
- collectionType === "auth"
1301
- );
1302
- for (const [ruleType, result] of validationResults) {
1303
- if (!result.valid) {
1304
- console.error(`[${collectionName}] Permission validation failed for ${ruleType}:`);
1305
- result.errors.forEach((error) => console.error(` - ${error}`));
1353
+ if (isView) {
1354
+ for (const ruleType of ["createRule", "updateRule", "deleteRule", "manageRule"]) {
1355
+ if (resolvedPermissions[ruleType]) {
1356
+ console.warn(
1357
+ `[${collectionName}] ${ruleType} is not supported on view collections and will be set to null.`
1358
+ );
1359
+ }
1360
+ resolvedPermissions[ruleType] = null;
1306
1361
  }
1307
- if (result.warnings.length > 0) {
1308
- console.warn(`[${collectionName}] Permission warnings for ${ruleType}:`);
1309
- result.warnings.forEach((warning) => console.warn(` - ${warning}`));
1362
+ }
1363
+ if (!isView) {
1364
+ const validationResults = permissionAnalyzer.validatePermissions(
1365
+ collectionName,
1366
+ resolvedPermissions,
1367
+ fields,
1368
+ collectionType === "auth"
1369
+ );
1370
+ for (const [ruleType, result] of validationResults) {
1371
+ if (!result.valid) {
1372
+ console.error(`[${collectionName}] Permission validation failed for ${ruleType}:`);
1373
+ result.errors.forEach((error) => console.error(` - ${error}`));
1374
+ }
1375
+ if (result.warnings.length > 0) {
1376
+ console.warn(`[${collectionName}] Permission warnings for ${ruleType}:`);
1377
+ result.warnings.forEach((warning) => console.warn(` - ${warning}`));
1378
+ }
1310
1379
  }
1311
1380
  }
1312
1381
  permissions = permissionAnalyzer.mergeWithDefaults(resolvedPermissions);
@@ -1327,6 +1396,9 @@ function convertZodSchemaToCollectionSchema(collectionName, zodSchema) {
1327
1396
  },
1328
1397
  permissions
1329
1398
  };
1399
+ if (isView) {
1400
+ collectionSchema.viewQuery = viewQuery;
1401
+ }
1330
1402
  return collectionSchema;
1331
1403
  }
1332
1404
  var tsxLoaderRegistered = false;
@@ -1526,6 +1598,6 @@ var SchemaAnalyzer = class {
1526
1598
  }
1527
1599
  };
1528
1600
 
1529
- export { DEFAULT_CONFIG, SchemaAnalyzer, buildFieldDefinition, buildSchemaDefinition, convertZodSchemaToCollectionSchema, discoverSchemaFiles, extractCollectionNameFromSchema, extractCollectionTypeFromSchema, extractFieldDefinitions, extractIndexes, extractSchemaDefinitions, getCollectionNameFromFile, importSchemaModule, isAuthCollection, mergeConfig, parseSchemaFiles, resolveSchemaDir, selectSchemaForCollection };
1601
+ export { DEFAULT_CONFIG, SchemaAnalyzer, buildFieldDefinition, buildSchemaDefinition, convertZodSchemaToCollectionSchema, discoverSchemaFiles, extractCollectionNameFromSchema, extractCollectionTypeFromSchema, extractFieldDefinitions, extractIndexes, extractSchemaDefinitions, extractViewQueryFromSchema, getCollectionNameFromFile, importSchemaModule, isAuthCollection, mergeConfig, parseSchemaFiles, resolveSchemaDir, selectSchemaForCollection };
1530
1602
  //# sourceMappingURL=analyzer.js.map
1531
1603
  //# sourceMappingURL=analyzer.js.map