pocketbase-zod-schema 0.1.4 → 0.2.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/CHANGELOG.md +14 -0
- package/README.md +233 -98
- package/dist/cli/index.cjs +45 -11
- package/dist/cli/index.cjs.map +1 -1
- package/dist/cli/index.js +45 -11
- package/dist/cli/index.js.map +1 -1
- package/dist/cli/migrate.cjs +45 -11
- package/dist/cli/migrate.cjs.map +1 -1
- package/dist/cli/migrate.js +45 -11
- package/dist/cli/migrate.js.map +1 -1
- package/dist/index.cjs +86 -27
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +2 -2
- package/dist/index.d.ts +2 -2
- package/dist/index.js +81 -26
- package/dist/index.js.map +1 -1
- package/dist/migration/analyzer.cjs +46 -11
- package/dist/migration/analyzer.cjs.map +1 -1
- package/dist/migration/analyzer.d.cts +11 -1
- package/dist/migration/analyzer.d.ts +11 -1
- package/dist/migration/analyzer.js +46 -12
- package/dist/migration/analyzer.js.map +1 -1
- package/dist/migration/index.cjs +45 -11
- package/dist/migration/index.cjs.map +1 -1
- package/dist/migration/index.js +45 -11
- package/dist/migration/index.js.map +1 -1
- package/dist/migration/snapshot.cjs.map +1 -1
- package/dist/migration/snapshot.js.map +1 -1
- package/dist/mutator.cjs +20 -21
- package/dist/mutator.cjs.map +1 -1
- package/dist/mutator.d.cts +2 -2
- package/dist/mutator.d.ts +2 -2
- package/dist/mutator.js +20 -21
- package/dist/mutator.js.map +1 -1
- package/dist/schema.cjs +41 -16
- package/dist/schema.cjs.map +1 -1
- package/dist/schema.d.cts +98 -8
- package/dist/schema.d.ts +98 -8
- package/dist/schema.js +36 -15
- package/dist/schema.js.map +1 -1
- package/dist/types.d.cts +1 -1
- package/dist/types.d.ts +1 -1
- package/dist/{user-_AM523hb.d.cts → user-DTJQIj4K.d.cts} +31 -5
- package/dist/{user-_AM523hb.d.ts → user-DTJQIj4K.d.ts} +31 -5
- package/package.json +2 -1
|
@@ -980,6 +980,16 @@ function isFieldRequired(zodType) {
|
|
|
980
980
|
}
|
|
981
981
|
|
|
982
982
|
// src/migration/analyzer.ts
|
|
983
|
+
var tsxLoaderRegistered = false;
|
|
984
|
+
async function ensureTsxLoader() {
|
|
985
|
+
if (tsxLoaderRegistered) return;
|
|
986
|
+
try {
|
|
987
|
+
await import('tsx/esm');
|
|
988
|
+
tsxLoaderRegistered = true;
|
|
989
|
+
} catch {
|
|
990
|
+
tsxLoaderRegistered = false;
|
|
991
|
+
}
|
|
992
|
+
}
|
|
983
993
|
var DEFAULT_CONFIG = {
|
|
984
994
|
workspaceRoot: process.cwd(),
|
|
985
995
|
excludePatterns: [
|
|
@@ -1077,24 +1087,37 @@ async function importSchemaModule(filePath, config) {
|
|
|
1077
1087
|
} else {
|
|
1078
1088
|
resolvedPath = jsPath;
|
|
1079
1089
|
}
|
|
1090
|
+
if (resolvedPath.endsWith(".ts")) {
|
|
1091
|
+
await ensureTsxLoader();
|
|
1092
|
+
if (!tsxLoaderRegistered) {
|
|
1093
|
+
throw new SchemaParsingError(
|
|
1094
|
+
`Failed to import TypeScript schema file. The 'tsx' package is required to load TypeScript files.
|
|
1095
|
+
Please install tsx: npm install tsx (or yarn add tsx, or pnpm add tsx)
|
|
1096
|
+
Alternatively, compile your schema files to JavaScript first.`,
|
|
1097
|
+
filePath
|
|
1098
|
+
);
|
|
1099
|
+
}
|
|
1100
|
+
}
|
|
1080
1101
|
const fileUrl = new URL(`file://${path__namespace.resolve(resolvedPath)}`);
|
|
1081
1102
|
const module = await import(fileUrl.href);
|
|
1082
1103
|
return module;
|
|
1083
1104
|
} catch (error) {
|
|
1084
1105
|
const tsPath = `${filePath}.ts`;
|
|
1085
1106
|
const isTypeScriptFile = fs__namespace.existsSync(tsPath);
|
|
1107
|
+
if (isTypeScriptFile && error instanceof SchemaParsingError) {
|
|
1108
|
+
throw error;
|
|
1109
|
+
}
|
|
1086
1110
|
if (isTypeScriptFile) {
|
|
1087
1111
|
throw new SchemaParsingError(
|
|
1088
|
-
`Failed to import TypeScript schema file.
|
|
1089
|
-
Please
|
|
1090
|
-
|
|
1091
|
-
2. Use tsx to run the migration tool (e.g., "npx tsx package/dist/cli/migrate.js status" or "tsx package/dist/cli/migrate.js status")`,
|
|
1112
|
+
`Failed to import TypeScript schema file. The 'tsx' package is required to load TypeScript files.
|
|
1113
|
+
Please install tsx: npm install tsx (or yarn add tsx, or pnpm add tsx)
|
|
1114
|
+
Alternatively, compile your schema files to JavaScript first.`,
|
|
1092
1115
|
filePath,
|
|
1093
1116
|
error
|
|
1094
1117
|
);
|
|
1095
1118
|
}
|
|
1096
1119
|
throw new SchemaParsingError(
|
|
1097
|
-
`Failed to import schema module. Make sure the schema files
|
|
1120
|
+
`Failed to import schema module. Make sure the schema files exist and are valid.`,
|
|
1098
1121
|
filePath,
|
|
1099
1122
|
error
|
|
1100
1123
|
);
|
|
@@ -1104,6 +1127,19 @@ function getCollectionNameFromFile(filePath) {
|
|
|
1104
1127
|
const filename = path__namespace.basename(filePath).replace(/\.(ts|js)$/, "");
|
|
1105
1128
|
return toCollectionName(filename);
|
|
1106
1129
|
}
|
|
1130
|
+
function extractCollectionNameFromSchema(zodSchema) {
|
|
1131
|
+
if (!zodSchema.description) {
|
|
1132
|
+
return null;
|
|
1133
|
+
}
|
|
1134
|
+
try {
|
|
1135
|
+
const metadata = JSON.parse(zodSchema.description);
|
|
1136
|
+
if (metadata.collectionName && typeof metadata.collectionName === "string") {
|
|
1137
|
+
return metadata.collectionName;
|
|
1138
|
+
}
|
|
1139
|
+
} catch {
|
|
1140
|
+
}
|
|
1141
|
+
return null;
|
|
1142
|
+
}
|
|
1107
1143
|
function extractSchemaDefinitions(module, patterns = ["Schema", "InputSchema"]) {
|
|
1108
1144
|
const result = {};
|
|
1109
1145
|
for (const [key, value] of Object.entries(module)) {
|
|
@@ -1178,11 +1214,8 @@ function buildFieldDefinition(fieldName, zodType) {
|
|
|
1178
1214
|
// Default to false, can be configured later
|
|
1179
1215
|
};
|
|
1180
1216
|
if (fieldDef.options) {
|
|
1181
|
-
const { min, max, pattern, ...relationSafeOptions } = fieldDef.options;
|
|
1182
|
-
|
|
1183
|
-
console.log("max", max);
|
|
1184
|
-
console.log("pattern", pattern);
|
|
1185
|
-
fieldDef.options = Object.keys(relationSafeOptions).length > 0 ? relationSafeOptions : void 0;
|
|
1217
|
+
const { min: _min, max: _max, pattern: _pattern, ...relationSafeOptions } = fieldDef.options;
|
|
1218
|
+
fieldDef.options = Object.keys(relationSafeOptions).length ? relationSafeOptions : void 0;
|
|
1186
1219
|
}
|
|
1187
1220
|
}
|
|
1188
1221
|
return fieldDef;
|
|
@@ -1279,7 +1312,8 @@ async function buildSchemaDefinition(config) {
|
|
|
1279
1312
|
console.warn(`No valid schema found in ${filePath}, skipping...`);
|
|
1280
1313
|
continue;
|
|
1281
1314
|
}
|
|
1282
|
-
const
|
|
1315
|
+
const collectionNameFromSchema = extractCollectionNameFromSchema(zodSchema);
|
|
1316
|
+
const collectionName = collectionNameFromSchema ?? getCollectionNameFromFile(filePath);
|
|
1283
1317
|
const collectionSchema = convertZodSchemaToCollectionSchema(collectionName, zodSchema);
|
|
1284
1318
|
collections.set(collectionName, collectionSchema);
|
|
1285
1319
|
} catch (error) {
|
|
@@ -1328,6 +1362,7 @@ exports.buildFieldDefinition = buildFieldDefinition;
|
|
|
1328
1362
|
exports.buildSchemaDefinition = buildSchemaDefinition;
|
|
1329
1363
|
exports.convertZodSchemaToCollectionSchema = convertZodSchemaToCollectionSchema;
|
|
1330
1364
|
exports.discoverSchemaFiles = discoverSchemaFiles;
|
|
1365
|
+
exports.extractCollectionNameFromSchema = extractCollectionNameFromSchema;
|
|
1331
1366
|
exports.extractFieldDefinitions = extractFieldDefinitions;
|
|
1332
1367
|
exports.extractIndexes = extractIndexes;
|
|
1333
1368
|
exports.extractSchemaDefinitions = extractSchemaDefinitions;
|