pocketbase-zod-schema 0.2.1 → 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.
Files changed (48) hide show
  1. package/CHANGELOG.md +7 -0
  2. package/dist/cli/index.cjs +42 -9
  3. package/dist/cli/index.cjs.map +1 -1
  4. package/dist/cli/index.js +42 -9
  5. package/dist/cli/index.js.map +1 -1
  6. package/dist/cli/migrate.cjs +42 -9
  7. package/dist/cli/migrate.cjs.map +1 -1
  8. package/dist/cli/migrate.js +42 -9
  9. package/dist/cli/migrate.js.map +1 -1
  10. package/dist/index.cjs +48 -11
  11. package/dist/index.cjs.map +1 -1
  12. package/dist/index.d.cts +1 -1
  13. package/dist/index.d.ts +1 -1
  14. package/dist/index.js +48 -11
  15. package/dist/index.js.map +1 -1
  16. package/dist/migration/analyzer.cjs +36 -7
  17. package/dist/migration/analyzer.cjs.map +1 -1
  18. package/dist/migration/analyzer.d.cts +26 -3
  19. package/dist/migration/analyzer.d.ts +26 -3
  20. package/dist/migration/analyzer.js +36 -8
  21. package/dist/migration/analyzer.js.map +1 -1
  22. package/dist/migration/diff.cjs +7 -2
  23. package/dist/migration/diff.cjs.map +1 -1
  24. package/dist/migration/diff.js +7 -2
  25. package/dist/migration/diff.js.map +1 -1
  26. package/dist/migration/index.cjs +42 -9
  27. package/dist/migration/index.cjs.map +1 -1
  28. package/dist/migration/index.js +42 -9
  29. package/dist/migration/index.js.map +1 -1
  30. package/dist/migration/snapshot.cjs.map +1 -1
  31. package/dist/migration/snapshot.js.map +1 -1
  32. package/dist/mutator.cjs +9 -3
  33. package/dist/mutator.cjs.map +1 -1
  34. package/dist/mutator.d.cts +3 -1
  35. package/dist/mutator.d.ts +3 -1
  36. package/dist/mutator.js +9 -3
  37. package/dist/mutator.js.map +1 -1
  38. package/dist/schema.cjs +9 -3
  39. package/dist/schema.cjs.map +1 -1
  40. package/dist/schema.d.cts +32 -3
  41. package/dist/schema.d.ts +32 -3
  42. package/dist/schema.js +9 -3
  43. package/dist/schema.js.map +1 -1
  44. package/dist/types.d.cts +1 -1
  45. package/dist/types.d.ts +1 -1
  46. package/dist/{user-DTJQIj4K.d.cts → user-BnFWg5tw.d.cts} +13 -1
  47. package/dist/{user-DTJQIj4K.d.ts → user-BnFWg5tw.d.ts} +13 -1
  48. package/package.json +1 -1
package/dist/index.d.cts CHANGED
@@ -3,7 +3,7 @@ export { UserMutator } from './mutator.cjs';
3
3
  export { ProjectInputType, ProjectType, TypedPocketBase, UserInputType, UserType } from './types.cjs';
4
4
  export { CollectionConfig, PermissionTemplates, PermissionValidationResult, RelationConfig, RelationField, RelationsConfig, RelationsField, baseImageFileSchema, baseSchema, baseSchemaWithTimestamps, boolField, createPermissions, dateField, defineCollection, editorField, emailField, extractRelationMetadata, fileField, filesField, geoPointField, inputImageFileSchema, isPermissionSchema, isTemplateConfig, jsonField, mergePermissions, numberField, omitImageFilesSchema, resolveTemplate, selectField, textField, urlField, validatePermissionConfig, validateRuleExpression, withIndexes, withPermissions } from './schema.cjs';
5
5
  export { A as APIRuleType, P as PermissionSchema, a as PermissionTemplate, b as PermissionTemplateConfig, R as RuleExpression } from './permissions-ZHafVSIx.cjs';
6
- export { b as ProjectCollection, P as ProjectInputSchema, a as ProjectSchema, e as UserCollection, c as UserCollectionSchema, U as UserInputSchema, d as UserSchema } from './user-DTJQIj4K.cjs';
6
+ export { P as ProjectCollection, a as ProjectInputSchema, b as ProjectSchema, U as UserCollection, d as UserCollectionSchema, c as UserInputSchema, e as UserSchema } from './user-BnFWg5tw.cjs';
7
7
  export { SchemaAnalyzer, SchemaAnalyzerConfig, buildFieldDefinition, buildSchemaDefinition, convertZodSchemaToCollectionSchema, discoverSchemaFiles, extractFieldDefinitions, extractIndexes, extractSchemaDefinitions, getCollectionNameFromFile, importSchemaModule, isAuthCollection, parseSchemaFiles, selectSchemaForCollection } from './migration/analyzer.cjs';
8
8
  export { SnapshotConfig, SnapshotManager, convertPocketBaseMigration, findLatestSnapshot, getSnapshotPath, getSnapshotVersion, loadBaseMigration, loadSnapshot, loadSnapshotIfExists, loadSnapshotWithMigrations, mergeSnapshots, saveSnapshot, snapshotExists, validateSnapshot } from './migration/snapshot.cjs';
9
9
  export { ChangeSummary, DestructiveChange, DiffEngine, DiffEngineConfig, aggregateChanges, categorizeChangesBySeverity, compare, compareFieldConstraints, compareFieldOptions, compareFieldTypes, comparePermissions, compareRelationConfigurations, detectDestructiveChanges, detectFieldChanges, filterSystemCollections, findNewCollections, findNewFields, findRemovedCollections, findRemovedFields, generateChangeSummary, getUsersSystemFields, isSystemCollection, matchCollectionsByName, matchFieldsByName, requiresForceFlag } from './migration/diff.cjs';
package/dist/index.d.ts CHANGED
@@ -3,7 +3,7 @@ export { UserMutator } from './mutator.js';
3
3
  export { ProjectInputType, ProjectType, TypedPocketBase, UserInputType, UserType } from './types.js';
4
4
  export { CollectionConfig, PermissionTemplates, PermissionValidationResult, RelationConfig, RelationField, RelationsConfig, RelationsField, baseImageFileSchema, baseSchema, baseSchemaWithTimestamps, boolField, createPermissions, dateField, defineCollection, editorField, emailField, extractRelationMetadata, fileField, filesField, geoPointField, inputImageFileSchema, isPermissionSchema, isTemplateConfig, jsonField, mergePermissions, numberField, omitImageFilesSchema, resolveTemplate, selectField, textField, urlField, validatePermissionConfig, validateRuleExpression, withIndexes, withPermissions } from './schema.js';
5
5
  export { A as APIRuleType, P as PermissionSchema, a as PermissionTemplate, b as PermissionTemplateConfig, R as RuleExpression } from './permissions-ZHafVSIx.js';
6
- export { b as ProjectCollection, P as ProjectInputSchema, a as ProjectSchema, e as UserCollection, c as UserCollectionSchema, U as UserInputSchema, d as UserSchema } from './user-DTJQIj4K.js';
6
+ export { P as ProjectCollection, a as ProjectInputSchema, b as ProjectSchema, U as UserCollection, d as UserCollectionSchema, c as UserInputSchema, e as UserSchema } from './user-BnFWg5tw.js';
7
7
  export { SchemaAnalyzer, SchemaAnalyzerConfig, buildFieldDefinition, buildSchemaDefinition, convertZodSchemaToCollectionSchema, discoverSchemaFiles, extractFieldDefinitions, extractIndexes, extractSchemaDefinitions, getCollectionNameFromFile, importSchemaModule, isAuthCollection, parseSchemaFiles, selectSchemaForCollection } from './migration/analyzer.js';
8
8
  export { SnapshotConfig, SnapshotManager, convertPocketBaseMigration, findLatestSnapshot, getSnapshotPath, getSnapshotVersion, loadBaseMigration, loadSnapshot, loadSnapshotIfExists, loadSnapshotWithMigrations, mergeSnapshots, saveSnapshot, snapshotExists, validateSnapshot } from './migration/snapshot.js';
9
9
  export { ChangeSummary, DestructiveChange, DiffEngine, DiffEngineConfig, aggregateChanges, categorizeChangesBySeverity, compare, compareFieldConstraints, compareFieldOptions, compareFieldTypes, comparePermissions, compareRelationConfigurations, detectDestructiveChanges, detectFieldChanges, filterSystemCollections, findNewCollections, findNewFields, findRemovedCollections, findRemovedFields, generateChangeSummary, getUsersSystemFields, isSystemCollection, matchCollectionsByName, matchFieldsByName, requiresForceFlag } from './migration/diff.js';
package/dist/index.js CHANGED
@@ -19,7 +19,9 @@ var baseSchema = {
19
19
  id: z.string().describe("unique id"),
20
20
  collectionId: z.string().describe("collection id"),
21
21
  collectionName: z.string().describe("collection name"),
22
- expand: z.record(z.any()).describe("expandable fields")
22
+ expand: z.record(z.any()).describe("expandable fields"),
23
+ created: z.string().describe("creation timestamp"),
24
+ updated: z.string().describe("last update timestamp")
23
25
  };
24
26
  var baseSchemaWithTimestamps = {
25
27
  ...baseSchema,
@@ -150,10 +152,13 @@ function withIndexes(schema, indexes) {
150
152
  return schema.describe(JSON.stringify(metadata));
151
153
  }
152
154
  function defineCollection(config) {
153
- const { collectionName, schema, permissions, indexes, ...futureOptions } = config;
155
+ const { collectionName, schema, permissions, indexes, type, ...futureOptions } = config;
154
156
  const metadata = {
155
157
  collectionName
156
158
  };
159
+ if (type) {
160
+ metadata.type = type;
161
+ }
157
162
  if (permissions) {
158
163
  metadata.permissions = permissions;
159
164
  }
@@ -422,7 +427,8 @@ var UserCollectionSchema = z.object({
422
427
  });
423
428
  var UserSchema = UserCollectionSchema.extend(baseSchema);
424
429
  var UserCollection = defineCollection({
425
- collectionName: "Users",
430
+ collectionName: "users",
431
+ type: "auth",
426
432
  schema: UserSchema,
427
433
  permissions: {
428
434
  // Users can list their own profile
@@ -1987,7 +1993,7 @@ var DEFAULT_CONFIG = {
1987
1993
  "permission-templates.js"
1988
1994
  ],
1989
1995
  includeExtensions: [".ts", ".js"],
1990
- schemaPatterns: ["Schema", "InputSchema"],
1996
+ schemaPatterns: ["Schema", "InputSchema", "Collection"],
1991
1997
  useCompiledFiles: true
1992
1998
  };
1993
1999
  function mergeConfig(config) {
@@ -2124,14 +2130,35 @@ function extractCollectionNameFromSchema(zodSchema) {
2124
2130
  }
2125
2131
  return null;
2126
2132
  }
2127
- function extractSchemaDefinitions(module, patterns = ["Schema", "InputSchema"]) {
2133
+ function extractCollectionTypeFromSchema(zodSchema) {
2134
+ if (!zodSchema.description) {
2135
+ return null;
2136
+ }
2137
+ try {
2138
+ const metadata = JSON.parse(zodSchema.description);
2139
+ if (metadata.type === "base" || metadata.type === "auth") {
2140
+ return metadata.type;
2141
+ }
2142
+ } catch {
2143
+ }
2144
+ return null;
2145
+ }
2146
+ function extractSchemaDefinitions(module, patterns = ["Schema", "InputSchema", "Collection"]) {
2128
2147
  const result = {};
2148
+ if (module.default instanceof z.ZodObject) {
2149
+ result.schema = module.default;
2150
+ }
2129
2151
  for (const [key, value] of Object.entries(module)) {
2152
+ if (key === "default") continue;
2130
2153
  if (value instanceof z.ZodObject) {
2131
2154
  if (patterns.includes("InputSchema") && key.endsWith("InputSchema")) {
2132
2155
  result.inputSchema = value;
2133
- } else if (patterns.includes("Schema") && key.endsWith("Schema") && !key.endsWith("InputSchema")) {
2134
- result.schema = value;
2156
+ } else if (!result.schema) {
2157
+ if (patterns.includes("Collection") && key.endsWith("Collection")) {
2158
+ result.schema = value;
2159
+ } else if (patterns.includes("Schema") && key.endsWith("Schema") && !key.endsWith("InputSchema")) {
2160
+ result.schema = value;
2161
+ }
2135
2162
  }
2136
2163
  }
2137
2164
  }
@@ -2220,8 +2247,13 @@ function extractIndexes(schema) {
2220
2247
  }
2221
2248
  function convertZodSchemaToCollectionSchema(collectionName, zodSchema) {
2222
2249
  const rawFields = extractFieldDefinitions(zodSchema);
2223
- const collectionType = isAuthCollection(rawFields) ? "auth" : "base";
2224
- const fields = rawFields.map(({ name, zodType }) => buildFieldDefinition(name, zodType));
2250
+ const explicitType = extractCollectionTypeFromSchema(zodSchema);
2251
+ const collectionType = explicitType ?? (isAuthCollection(rawFields) ? "auth" : "base");
2252
+ let fields = rawFields.map(({ name, zodType }) => buildFieldDefinition(name, zodType));
2253
+ if (collectionType === "auth") {
2254
+ const authSystemFieldNames = ["email", "emailVisibility", "verified", "password", "tokenKey"];
2255
+ fields = fields.filter((field) => !authSystemFieldNames.includes(field.name));
2256
+ }
2225
2257
  const indexes = extractIndexes(zodSchema) || [];
2226
2258
  const permissionAnalyzer = new PermissionAnalyzer();
2227
2259
  let permissions = void 0;
@@ -3205,9 +3237,14 @@ function matchCollectionsByName(currentSchema, previousSnapshot) {
3205
3237
  if (!previousSnapshot) {
3206
3238
  return matches;
3207
3239
  }
3240
+ const previousCollectionsLower = /* @__PURE__ */ new Map();
3241
+ for (const [name, collection] of previousSnapshot.collections) {
3242
+ previousCollectionsLower.set(name.toLowerCase(), [name, collection]);
3243
+ }
3208
3244
  for (const [collectionName, currentCollection] of currentSchema.collections) {
3209
- const previousCollection = previousSnapshot.collections.get(collectionName);
3210
- if (previousCollection) {
3245
+ const previousEntry = previousCollectionsLower.get(collectionName.toLowerCase());
3246
+ if (previousEntry) {
3247
+ const [, previousCollection] = previousEntry;
3211
3248
  matches.push([currentCollection, previousCollection]);
3212
3249
  }
3213
3250
  }