This commit is contained in:
2025-10-05 13:50:18 +02:00
commit 41f083d69e
71 changed files with 17398 additions and 0 deletions

1
package/.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
dist

106
package/CHANGELOG.md Normal file
View File

@@ -0,0 +1,106 @@
# zod-pocketbase
## 0.5.0
### Minor Changes
- remove "cache" option for "helpersFrom" and replace it with "fetch" to allow a more generic custom fetch and remove "@11ty/eleventy-fetch" dependency
## 0.4.1
### Patch Changes
- update packages
- fix json import
## 0.4.0
### Minor Changes
- separate files that use node packages in server directory
## 0.3.8
### Patch Changes
- fix cached version
## 0.3.7
### Patch Changes
- d5481dc: fix getRecords return type
## 0.3.6
### Patch Changes
- a8d3098: add recordsListFrom method to construct a records list schema from a record schema
- a8d3098: fix getRecords helper return type
## 0.3.5
### Patch Changes
- optimize SKD calls and options
## 0.3.4
### Patch Changes
- rename out to output
## 0.3.3
### Patch Changes
- refactor code
## 0.3.2
### Patch Changes
- fix syntax
## 0.3.1
### Patch Changes
- fix generated syntax
## 0.3.0
### Minor Changes
- refactor CLI
## 0.2.4
### Patch Changes
- 03555e2: fix optional expand in expand method
## 0.2.3
### Patch Changes
- cdf2b59: fix the transformation when expand is undefined
## 0.2.2
### Patch Changes
- 6980455: fix the case when expand only contains keys with optional schemas
## 0.2.1
### Patch Changes
- be3d479: add cache option to helpers
## 0.2.0
### Minor Changes
- 5435793: rename getHelpers to helpersFrom
- 5435793: add collectionName literal to schema

View File

@@ -0,0 +1,42 @@
import type Pocketbase from "pocketbase";
import type { RecordService } from "pocketbase";
import { z } from "zod";
/******* ENUMS *******/
export const collectionValues = @@_COLLECTION_NAMES_@@ as const;
export const Collection = z.enum(collectionValues);
export type Collection = z.infer<typeof Collection>;
export const COLLECTION = Collection.enum;
@@_ENUMS_@@
/******* BASE *******/
export const BaseModel = z.object({
created: z.string().pipe(z.coerce.date()),
id: z.string(),
updated: z.string().pipe(z.coerce.date()),
});
export type BaseModel = z.infer<typeof BaseModel>;
export const AdminModel = z.object({
...BaseModel.shape,
avatar: z.string(),
email: z.string().email(),
});
export type AdminModel = z.infer<typeof AdminModel>;
export const RecordModel = z.object({
...BaseModel.shape,
collectionId: z.string(),
collectionName: z.string(),
expand: z.any().optional(),
});
export type RecordModel = z.infer<typeof RecordModel>;
/******* RECORDS *******/
@@_RECORDS_@@
/******* CLIENT *******/
export type TypedPocketbase = Pocketbase & {
@@_SERVICES_@@
};

63
package/package.json Normal file
View File

@@ -0,0 +1,63 @@
{
"name": "zod-pocketbase",
"version": "0.5.0",
"description": "",
"author": {
"email": "gregory.bouteiller@niama.re",
"name": "Gregory Bouteiller",
"url": "https://github.com/gbouteiller"
},
"license": "MIT",
"keywords": [
"pocketbase",
"schemas",
"typescript",
"typegen",
"type generation",
"zod"
],
"homepage": "https://github.com/gbouteiller/zod-pocketbase",
"publishConfig": {
"access": "public"
},
"type": "module",
"sideEffects": false,
"main": "dist/index.js",
"bin": {
"zod-pocketbase": "dist/server/cli.js"
},
"exports": {
".": {
"types": "./dist/index.d.ts",
"default": "./dist/index.js"
},
"./server": {
"types": "./dist/server/index.d.ts",
"default": "./dist/server/index.js"
}
},
"files": [
"dist",
"assets"
],
"scripts": {
"dev": "tsup --watch",
"build": "tsup"
},
"dependencies": {
"@clack/prompts": "^0.9.0",
"c12": "^2.0.1",
"citty": "^0.1.6",
"es-toolkit": "^1.30.1"
},
"devDependencies": {
"@types/node": "^22.10.2",
"pocketbase": "<0.22.0",
"tsup": "^8.3.5",
"zod": "^3.24.1"
},
"peerDependencies": {
"pocketbase": "<0.22.0",
"zod": "^3.23.8"
}
}

1209
package/pnpm-lock.yaml generated Normal file

File diff suppressed because it is too large Load Diff

66
package/src/config.ts Normal file
View File

@@ -0,0 +1,66 @@
import { pascalCase, snakeCase } from "es-toolkit";
import { z } from "zod";
/**
* Default config values.
*/
export const defaultConfig = {
ignore: [],
nameEnum: (name: string) => snakeCase(name).toUpperCase(),
nameEnumField: (collectionName: string, fieldName: string) => `${collectionName}${pascalCase(fieldName)}`,
nameEnumSchema: (name: string) => pascalCase(name),
nameEnumType: (name: string) => pascalCase(name),
nameEnumValues: (name: string) => `${name}Values`,
nameRecordSchema: (name: string) => `${pascalCase(name)}Record`,
nameRecordType: (name: string) => `${pascalCase(name)}Record`,
output: "./zod-pocketbase.ts",
};
/**
* Schema for the PocketBase credentials.
*/
export const Credentials = z.object({
adminEmail: z.string().email(),
adminPassword: z.string(),
url: z.string().url(),
});
export type Credentials = z.infer<typeof Credentials>;
/**
* Schema for the config file.
*/
export const Config = z.object({
...Credentials.partial().shape,
ignore: z.string().array().default(defaultConfig.ignore),
nameEnum: z
.function(z.tuple([z.string()]), z.string())
.optional()
.transform((f) => f ?? defaultConfig.nameEnum),
nameEnumField: z
.function(z.tuple([z.string(), z.string()]), z.string())
.optional()
.transform((f) => f ?? defaultConfig.nameEnumField),
nameEnumSchema: z
.function(z.tuple([z.string()]), z.string())
.optional()
.transform((f) => f ?? defaultConfig.nameEnumSchema),
nameEnumType: z
.function(z.tuple([z.string()]), z.string())
.optional()
.transform((f) => f ?? defaultConfig.nameEnumType),
nameEnumValues: z
.function(z.tuple([z.string()]), z.string())
.optional()
.transform((f) => f ?? defaultConfig.nameEnumValues),
nameRecordSchema: z
.function(z.tuple([z.string()]), z.string())
.optional()
.transform((f) => f ?? defaultConfig.nameRecordSchema),
nameRecordType: z
.function(z.tuple([z.string()]), z.string())
.optional()
.transform((f) => f ?? defaultConfig.nameRecordType),
output: z.string().default(defaultConfig.output),
});
export type Config = z.input<typeof Config>;
export type ResolvedConfig = z.infer<typeof Config>;

120
package/src/content.ts Normal file
View File

@@ -0,0 +1,120 @@
import { sortBy } from "es-toolkit";
import type { CollectionModel, SchemaField } from "pocketbase";
import type { GenerateOpts } from "./server/utils.ts";
export function stringifyContent(collections: CollectionModel[], opts: GenerateOpts) {
function getCollectionSelectFields() {
return collections.flatMap((collection) =>
collection.schema
.filter((field) => field.type === "select")
.map((field) => ({ name: opts.nameEnumField(collection.name, field.name), values: (field.options.values ?? []) as string[] })),
);
}
function stringifyEnum({ name, values }: SelectField) {
const valuesName = opts.nameEnumValues(name);
const schemaName = opts.nameEnumSchema(name);
const enumName = opts.nameEnum(name);
const typeName = opts.nameEnumType(name);
return `export const ${valuesName} = [\n\t${values.map((value) => `"${value}"`).join(",\n\t")},\n] as const;\nexport const ${schemaName} = z.enum(${valuesName});\nexport type ${typeName} = z.infer<typeof ${schemaName}>;\nexport const ${enumName} = ${schemaName}.enum;`;
}
function stringifyRecord({ name, schema }: CollectionModel) {
const schemaName = opts.nameRecordSchema(name);
const typeName = opts.nameRecordType(name);
const fields = sortBy(schema, ["name"]).map((field) => stringifyField(field, name));
return `export const ${schemaName} = z.object({\n\t...RecordModel.omit({ expand: true }).shape,\n\tcollectionName: z.literal("${name}"),\n\t${fields.join(",\n\t")},\n});\nexport type ${typeName} = z.infer<typeof ${schemaName}>;`;
}
function stringifyField(field: SchemaField, collectionName: string) {
let schema: string | undefined;
if (field.type === "bool") schema = stringifyBoolField(field);
else if (field.type === "date") schema = stringifyDateField(field);
else if (field.type === "editor") schema = stringifyEditorField(field);
else if (field.type === "email") schema = stringifyEmailField(field);
else if (field.type === "file") schema = stringifyFileField(field);
else if (field.type === "json") schema = stringifyJsonField(field);
else if (field.type === "number") schema = stringifyNumberField(field);
else if (field.type === "relation") schema = stringifyRelationField(field);
else if (field.type === "select") schema = stringifySelectField(field, collectionName);
else if (field.type === "text") schema = stringifyTextField(field);
else if (field.type === "url") schema = stringifyUrlField(field);
// TODO: manage unknown field type
return `${field.name}: ${schema}${field.required ? "" : ".optional()"}`;
}
function stringifyBoolField(_: SchemaField) {
return "z.boolean()";
}
function stringifyDateField(_field: SchemaField) {
// TODO: implement min and max
return "z.string().pipe(z.coerce.date())";
}
function stringifyEditorField(_field: SchemaField) {
// TODO: implement convertUrls
return "z.string()";
}
function stringifyEmailField(_field: SchemaField) {
// TODO: implement exceptDomains and onlyDomains
return "z.string().email()";
}
function stringifyFileField({ options: { maxSelect } }: SchemaField) {
// TODO: implement maxSize, mimeTypes, protected, thumbs
return `z.string()${maxSelect === 1 ? "" : `.array().max(${maxSelect})`}`;
}
function stringifyJsonField(_field: SchemaField) {
// TODO: implement maxSize and json schema
return "z.any()";
}
function stringifyNumberField({ options: { max, min, noDecimal } }: SchemaField) {
return `z.number()${noDecimal ? ".int()" : ""}${min ? `.min(${min})` : ""}${max ? `.max(${max})` : ""}`;
}
function stringifyRelationField({ options, required }: SchemaField) {
const { maxSelect, minSelect } = options;
// TODO: implement cascadeDelete, displayFields
const min = minSelect ? `.min(${minSelect})` : "";
const max = maxSelect ? `.max(${maxSelect})` : "";
const multiple = maxSelect === 1 ? "" : `.array()${min}${max}`;
const isOptional = required || maxSelect !== 1 ? `` : `.transform((id) => id === "" ? undefined : id)`;
return `z.string()${isOptional}${multiple}`;
}
function stringifySelectField({ name, options: { maxSelect } }: SchemaField, collectionName: string) {
// TODO: implement values
return `${opts.nameEnumSchema(opts.nameEnumField(collectionName, name))}${maxSelect === 1 ? "" : `.array().max(${maxSelect})`}`;
}
function stringifyTextField({ options: { max, min } }: SchemaField) {
// TODO: implement pattern
return `z.string()${min ? `.min(${min})` : ""}${max ? `.max(${max})` : ""}`;
}
function stringifyUrlField(_field: SchemaField) {
// TODO: implement exceptDomains and onlyDomains
return "z.string().url()";
}
function stringifySchemasEntry({ name }: CollectionModel) {
return `["${name}", ${opts.nameRecordSchema(name)}]`;
}
function stringifyService({ name }: CollectionModel) {
return `\t\tcollection(idOrName: "${name}"): RecordService<z.input<typeof ${opts.nameRecordSchema(name)}>>;`;
}
return {
collectionNames: `[\n\t${collections.map(({ name }) => `"${name}"`).join(",\n\t")},\n]`,
enums: getCollectionSelectFields().map(stringifyEnum).join("\n\n"),
records: `${collections.map(stringifyRecord).join("\n\n")}\n\nexport const records = new Map<Collection, z.AnyZodObject>([\n\t${collections.map(stringifySchemasEntry).join(",\n\t")},\n]);`,
services: collections.map(stringifyService).join("\n"),
};
}
export type SelectField = { name: string; values: string[] };

29
package/src/helpers.ts Normal file
View File

@@ -0,0 +1,29 @@
import type { default as Pocketbase, SendOptions } from "pocketbase";
import { fullListOptionsFrom, optionsFrom } from "./options.js";
import type { AnyZodRecord, RecordFullListOpts, RecordIdRef, RecordRef, RecordSlugRef } from "./types.ts";
import { AnyRecordsList, type RecordsList } from "./schemas.ts";
export function helpersFrom({ fetch, pocketbase }: HelpersFromOpts) {
async function getRecord<C extends string, S extends AnyZodRecord>(ref: RecordSlugRef<C>, opts: GetRecordOpts<S>): Promise<S["_output"]>;
async function getRecord<C extends string, S extends AnyZodRecord>(ref: RecordIdRef<C>, opts: GetRecordOpts<S>): Promise<S["_output"]>;
async function getRecord<C extends string, S extends AnyZodRecord>(ref: RecordRef<C>, opts: GetRecordOpts<S>) {
const { schema } = opts;
const sdkOpts = { ...optionsFrom(schema), ...(fetch ? { fetch } : {}) };
const unsafeRecord = await ("id" in ref
? pocketbase.collection(ref.collection).getOne(ref.id, sdkOpts)
: pocketbase.collection(ref.collection).getFirstListItem(`slug = "${ref.slug}"`, sdkOpts));
return schema.parseAsync(unsafeRecord);
}
async function getRecords<C extends string, S extends AnyZodRecord>(collection: C, opts: GetRecordsOpts<S>): Promise<RecordsList<S>> {
const { schema, ...otherOpts } = opts;
const sdkOpts = { ...fullListOptionsFrom(schema, otherOpts), ...(fetch ? { fetch } : {}) };
const recordsList = await pocketbase.collection(collection).getList(sdkOpts.page, sdkOpts.perPage, sdkOpts);
return AnyRecordsList.extend({ items: schema.array() }).parseAsync(recordsList);
}
return { getRecord, getRecords };
}
export type GetRecordOpts<S extends AnyZodRecord> = { schema: S };
export type GetRecordsOpts<S extends AnyZodRecord> = RecordFullListOpts<S> & { schema: S };
export type HelpersFromOpts = { fetch?: SendOptions["fetch"]; pocketbase: Pocketbase };

6
package/src/index.ts Normal file
View File

@@ -0,0 +1,6 @@
export * from "./config.js";
export * from "./helpers.js";
export * from "./options.js";
export * from "./schemas.js";
export * from "./types.ts";
export * from "./utils.js";

97
package/src/options.ts Normal file
View File

@@ -0,0 +1,97 @@
import { z, type AnyZodObject, type ZodTypeAny } from "zod";
import type { AnyZodRecord, RecordFullListOpts, RecordListOpts } from "./types.ts";
/**
* Extends the given schema with the given expansion.
* @param schema - The original schema
* @param shape - The shape of the expansion
* @returns A new schema extended with the given expansion
*/
export function expandFrom<S extends AnyZodRecord>(schema: S) {
return expandFromRec(schema)
.sort((k1, k2) => (k1 < k2 ? -1 : 1))
.join(",");
}
/**
* Extends the given schema with the given expansion.
* @param schema - The original schema
* @param shape - The shape of the expansion
* @returns A new schema extended with the given expansion
*/
export function fieldsFrom<S extends AnyZodRecord>(schema: S) {
return fieldsFromRec(schema)
.sort((k1, k2) => (k1 < k2 ? -1 : 1))
.join(",");
}
/**
* Extends the given schema with the given expansion.
* @param schema - The original schema
* @param shape - The shape of the expansion
* @returns A new schema extended with the given expansion
*/
export function optionsFrom<S extends AnyZodRecord>(schema: S) {
return { expand: expandFrom(schema), fields: fieldsFrom(schema) };
}
/**
* Extends the given schema with the given expansion.
* @param schema - The original schema
* @param shape - The shape of the expansion
* @returns A new schema extended with the given expansion
*/
export function listOptionsFrom<S extends AnyZodRecord>(schema: S, opts: RecordListOpts<S>) {
const { page = 1, perPage = 30, ...rest } = opts;
return { ...optionsFrom(schema), page, perPage, ...rest };
}
/**
* Extends the given schema with the given expansion.
* @param schema - The original schema
* @param shape - The shape of the expansion
* @returns A new schema extended with the given expansion
*/
export function fullListOptionsFrom<S extends AnyZodRecord>(schema: S, opts: RecordFullListOpts<S>) {
const { page = 1, perPage = 200, skipTotal = true, ...rest } = opts;
return listOptionsFrom(schema, { page, perPage, skipTotal, ...rest });
}
function expandFromRec<S extends ZodTypeAny>(schema: S, prefix = "") {
let expands: string[] = [];
const shape = getObjectSchemaDescendant(schema)?.shape;
if (!shape || !("expand" in shape)) return [];
for (const [key, value] of Object.entries(getObjectSchemaDescendant(shape.expand)!.shape)) {
expands = [...expands, `${prefix}${key}`];
if (hasObjectSchemaDescendant(value)) expands = [...expands, ...expandFromRec(getObjectSchemaDescendant(value)!, `${prefix}${key}.`)];
}
return expands;
}
function fieldsFromRec<S extends z.ZodTypeAny>(schema: S, prefix = "") {
let fields: string[] = [];
const shape = getObjectSchemaDescendant(schema)?.shape;
if (!shape) return [];
for (const [key, value] of Object.entries(shape)) {
fields = [
...fields,
...(hasObjectSchemaDescendant(value) ? fieldsFromRec(getObjectSchemaDescendant(value)!, `${prefix}${key}.`) : [`${prefix}${key}`]),
];
}
return fields.sort((k1, k2) => (k1 < k2 ? -1 : 1));
}
function hasObjectSchemaDescendant(value: unknown): value is z.ZodTypeAny {
if (value instanceof z.ZodEffects) return hasObjectSchemaDescendant(value.innerType());
if (value instanceof z.ZodArray) return hasObjectSchemaDescendant(value.element);
if (value instanceof z.ZodOptional) return hasObjectSchemaDescendant(value.unwrap());
return value instanceof z.ZodObject;
}
function getObjectSchemaDescendant<S extends z.ZodTypeAny>(schema: S): AnyZodObject | undefined {
if (schema instanceof z.ZodEffects) return getObjectSchemaDescendant(schema.innerType());
if (schema instanceof z.ZodArray) return getObjectSchemaDescendant(schema.element);
if (schema instanceof z.ZodOptional) return getObjectSchemaDescendant(schema.unwrap());
if (schema instanceof z.ZodObject) return schema;
return;
}

71
package/src/schemas.ts Normal file
View File

@@ -0,0 +1,71 @@
import { type AnyZodObject, type objectUtil, z, type ZodEffects, type ZodObject, ZodOptional, type ZodRawShape } from "zod";
import type { AnyZodRecord, HasRequiredKeys, ZodRecordKeys } from "./types.ts";
/**
* Records list schema.
*/
export const AnyRecordsList = z.object({
items: z.any().array(),
page: z.number().int().min(1),
perPage: z.number().int().min(1),
totalItems: z.number().int().min(-1),
totalPages: z.number().int().min(-1),
});
/**
* Extends the given schema with the given expansion.
* @param schema - The original schema
* @param shape - The shape of the expansion
* @returns A new schema extended with the given expansion
*/
export function expand<S extends AnyZodObject, E extends ZodRawShape>(schema: S, shape: E) {
const isExpandOptional = Object.entries(shape).every(([, value]) => value instanceof z.ZodOptional);
return z
.object({ ...schema.shape, expand: isExpandOptional ? z.object(shape).optional() : z.object(shape) })
.transform(({ expand, ...rest }) => ({ ...rest, ...(expand ?? {}) })) as ZodObjectExpand<S, E>;
}
/**
* Picks the given keys from the given schema.
* @param schema - The original schema
* @param keys - The keys to keep
* @returns A new schema with only the given keys
*/
export function pick<S extends AnyZodObject, K extends ZodRecordKeys<S>[]>(schema: S, keys: K) {
return schema.pick(Object.fromEntries(keys.map((key) => [key, true]))) as ZodObjectPick<S, K>;
}
/**
* Picks the given keys from the given schema and extends it with the given expansion.
* @param schema - The original schema
* @param keys - The keys to keep
* @param shape - The shape of the expansion
* @returns A new schema with only the given keys
*/
export function select<S extends AnyZodObject, K extends ZodRecordKeys<S>[], E extends ZodRawShape>(
schema: S,
keys: K,
shape: E,
): ZodObjectExpand<ZodObjectPick<S, K>, E>;
export function select<S extends AnyZodObject, K extends ZodRecordKeys<S>[]>(schema: S, keys: K): ZodObjectPick<S, K>;
export function select<S extends AnyZodObject, K extends ZodRecordKeys<S>[], E extends ZodRawShape | undefined>(
schema: S,
keys: K,
shape?: E,
) {
return shape ? expand(pick(schema, keys), shape) : pick(schema, keys);
}
export type ZodObjectExpand<S extends AnyZodObject, E extends ZodRawShape> =
S extends ZodObject<infer T, infer U, infer C>
? ZodEffects<
ZodObject<objectUtil.extendShape<T, { expand: HasRequiredKeys<E> extends true ? ZodObject<E> : ZodOptional<ZodObject<E>> }>, U, C>,
ZodObject<objectUtil.extendShape<T, E>, U, C>["_output"]
>
: never;
export type ZodObjectPick<S extends AnyZodObject, K extends ZodRecordKeys<S>[]> =
S extends ZodObject<infer T, infer U, infer C> ? ZodObject<Pick<T, K[number]>, U, C> : never;
export type AnyRecordsList = z.infer<typeof AnyRecordsList>;
export type RecordsList<S extends AnyZodRecord> = Omit<AnyRecordsList, "items"> & { items: S["_output"][] };

13
package/src/sdk.ts Normal file
View File

@@ -0,0 +1,13 @@
import Pocketbase from "pocketbase";
import type { Credentials } from "./config.ts";
let adminPocketbase: Pocketbase;
export async function getPocketbase({ adminEmail, adminPassword, url }: Credentials) {
if (!adminPocketbase) {
const pocketbase = new Pocketbase(url);
await pocketbase.admins.authWithPassword(adminEmail, adminPassword);
adminPocketbase = pocketbase;
}
return adminPocketbase;
}

91
package/src/server/cli.ts Normal file
View File

@@ -0,0 +1,91 @@
#!/usr/bin/env node
import { Config, type ResolvedConfig } from "../config.ts";
import pkg from "../../package.json" assert { type: "json" };
import { loadConfig } from "c12";
import { defineCommand, runMain } from "citty";
import { cancel, group, intro, log, outro, confirm, text, spinner, multiselect, isCancel } from "@clack/prompts";
import { fetchCollections } from "../utils.ts";
import { generate } from "./utils.ts";
import { existsSync } from "node:fs";
async function getConfig() {
const { config } = await loadConfig({ name: "zod-pocketbase", rcFile: false, dotenv: true });
const { ZOD_POCKETBASE_ADMIN_EMAIL: adminEmail, ZOD_POCKETBASE_ADMIN_PASSWORD: adminPassword, ZOD_POCKETBASE_URL: url } = process.env;
const result = Config.safeParse({ ...config, adminEmail, adminPassword, url });
if (!result.success) {
log.error("Invalid fields in your config file.");
onCancel();
}
return result.data!;
}
function onCancel() {
cancel("Operation cancelled.");
process.exit(0);
}
async function selectCollections(config: ResolvedConfig) {
const credentialPrompts = {
url: () => text({ message: "What is the url of your pocketbase instance?", initialValue: config.url ?? "" }),
adminEmail: () => text({ message: "What is your admin's email?", initialValue: config.adminEmail ?? "" }),
adminPassword: () => text({ message: "What is your admin's password?", initialValue: config.adminPassword ?? "" }),
};
const credentials = await group(credentialPrompts, { onCancel });
const s = spinner();
s.start("Fetching collections...");
try {
const allCollections = await fetchCollections(credentials);
s.stop("Successfully fetched collections.");
const collectionNames = await multiselect({
message: "What collections do you want to generate schemas for?",
options: allCollections.map(({ name: value }) => ({ value })),
initialValues: allCollections.filter(({ name }) => !config.ignore.includes(name)).map(({ name }) => name),
});
if (isCancel(collectionNames)) onCancel();
return allCollections.filter(({ name }) => (collectionNames as string[]).includes(name));
} catch {
s.stop("Failed to fetch collections.Please check your credentials and try again.");
return selectCollections(config);
}
}
async function setGeneratedFilePath(config: ResolvedConfig) {
const output = await text({
message: "What is the generated file path?",
initialValue: config.output,
validate: (value) => {
if (!value) return "Please enter a path.";
if (value[0] !== ".") return "Please enter a relative path.";
return;
},
});
if (isCancel(output)) onCancel();
if (existsSync(output as string)) {
const confirmed = await confirm({ message: "The file already exists, would you like to overwrite it?" });
if (isCancel(confirmed)) onCancel();
if (!confirmed) return setGeneratedFilePath(config);
}
return output as string;
}
const main = defineCommand({
meta: { name: "zod-pocketbase", version: pkg.version, description: "Generate Zod schemas for your pocketbase instance." },
run: async () => {
intro(`ZOD POCKETBASE`);
const config = await getConfig();
const collections = await selectCollections(config);
const output = await setGeneratedFilePath(config);
const s = spinner();
s.start("Generating your schemas...");
await generate(collections, { ...config, output });
s.stop("Schemas successfully generated.");
outro("Operation completed.");
},
});
runMain(main);

View File

@@ -0,0 +1 @@
export * from "./utils.js";

View File

@@ -0,0 +1,19 @@
import { mkdirSync, readFileSync, writeFileSync } from "node:fs";
import { dirname, resolve } from "node:path";
import { fileURLToPath } from "node:url";
import type { CollectionModel } from "pocketbase";
import { stringifyContent } from "../content.js";
import type { ResolvedConfig } from "../config.ts";
export async function generate(collections: CollectionModel[], opts: GenerateOpts) {
const stub = readFileSync(resolve(dirname(fileURLToPath(import.meta.url)), "../../assets/stubs/index.ts"), "utf-8");
const { collectionNames, enums, records, services } = stringifyContent(collections, opts);
const content = stub
.replace("@@_COLLECTION_NAMES_@@", collectionNames)
.replace("@@_ENUMS_@@", enums)
.replace("@@_RECORDS_@@", records)
.replace("@@_SERVICES_@@", services);
mkdirSync(dirname(opts.output), { recursive: true });
writeFileSync(opts.output, content);
}
export type GenerateOpts = Omit<ResolvedConfig, "adminEmail" | "adminPassword" | "ignore" | "url">;

31
package/src/types.ts Normal file
View File

@@ -0,0 +1,31 @@
import type { AnyZodObject, ZodEffects, ZodOptional, ZodRawShape, ZodTypeAny } from "zod";
export type AnyZodRecord = AnyZodObject | ZodEffects<AnyZodObject>;
export type RecordFullListOpts<S extends AnyZodRecord> = RecordListOpts<S> & { batch?: number };
export type RecordListOpts<S extends AnyZodRecord> = {
filter?: string;
page?: number;
perPage?: number;
skipTotal?: boolean;
sort?: ZodRecordSort<S>;
};
export type RecordIdRef<C extends string> = { collection: C; id: string };
export type RecordSlugRef<C extends string> = { collection: C; slug: string };
export type RecordRef<C extends string> = RecordIdRef<C> | RecordSlugRef<C>;
export type ZodRecordKeys<S extends AnyZodRecord> = Extract<keyof S["_input"], string>;
export type ZodRecordMainKeys<S extends AnyZodRecord> = Exclude<ZodRecordKeys<S>, "expand">;
export type ZodRecordSort<S extends AnyZodRecord> = `${"+" | "-"}${ZodRecordMainKeys<S>}` | "@random";
type RequiredKeysOf<S extends ZodRawShape> = Exclude<
{
[Key in keyof S]: S[Key] extends ZodOptional<ZodTypeAny> ? never : Key;
}[keyof S],
undefined
>;
export type HasRequiredKeys<S extends ZodRawShape> = RequiredKeysOf<S> extends never ? false : true;

10
package/src/utils.ts Normal file
View File

@@ -0,0 +1,10 @@
import { sortBy } from "es-toolkit";
import type { CollectionModel } from "pocketbase";
import { getPocketbase } from "./sdk.js";
import type { Credentials } from "./config.ts";
export async function fetchCollections(credentials: Credentials): Promise<CollectionModel[]> {
const pocketbase = await getPocketbase(credentials);
const collections = await pocketbase.collections.getFullList();
return sortBy(collections, ["name"]);
}

52
package/tsconfig.json Normal file
View File

@@ -0,0 +1,52 @@
{
"$schema": "https://json.schemastore.org/tsconfig",
"compilerOptions": {
// Enable top-level await, and other modern ESM features.
"target": "ESNext",
"module": "NodeNext",
// Enable module resolution without file extensions on relative paths, for things like npm package imports.
"moduleResolution": "Node16",
// Allow importing TypeScript files using their native extension (.ts(x)).
"allowImportingTsExtensions": true,
// Enable JSON imports.
"resolveJsonModule": true,
// Enforce the usage of type-only imports when needed, which helps avoiding bundling issues.
"verbatimModuleSyntax": true,
// Ensure that each file can be transpiled without relying on other imports.
// This is redundant with the previous option, however it ensures that it's on even if someone disable `verbatimModuleSyntax`
"isolatedModules": true,
// Astro directly run TypeScript code, no transpilation needed.
"noEmit": true,
// Report an error when importing a file using a casing different from another import of the same file.
"forceConsistentCasingInFileNames": true,
// Properly support importing CJS modules in ESM
"esModuleInterop": true,
// Skip typechecking libraries and .d.ts files
"skipLibCheck": true,
// Allow JavaScript files to be imported
"allowJs": true,
// Allow JSX files (or files that are internally considered JSX, like Astro files) to be imported inside `.js` and `.ts` files.
"jsx": "preserve",
// Enable strict mode. This enables a few options at a time, see https://www.typescriptlang.org/tsconfig#strict for a list.
"strict": true,
// Report errors for fallthrough cases in switch statements
"noFallthroughCasesInSwitch": true,
// Force functions designed to override their parent class to be specified as `override`.
"noImplicitOverride": true,
// Force functions to specify that they can return `undefined` if a possible code path does not return a value.
"noImplicitReturns": true,
// Report an error when a variable is declared but never used.
"noUnusedLocals": true,
// Report an error when a parameter is declared but never used.
"noUnusedParameters": true,
// Force the usage of the indexed syntax to access fields declared using an index signature.
"noUncheckedIndexedAccess": true,
// Report an error when the value `undefined` is given to an optional property that doesn't specify `undefined` as a valid value.
"exactOptionalPropertyTypes": true,
// Report an error for unreachable code instead of just a warning.
"allowUnreachableCode": false,
// Report an error for unused labels instead of just a warning.
"allowUnusedLabels": false
},
"exclude": ["dist"]
}

19
package/tsup.config.ts Normal file
View File

@@ -0,0 +1,19 @@
import { defineConfig } from "tsup";
import { peerDependencies } from "./package.json";
export default defineConfig((options) => {
const dev = !!options.watch;
return {
entry: ["src/**/*.(ts|js)"],
format: ["esm"],
target: "node18",
bundle: true,
dts: true,
sourcemap: true,
clean: true,
splitting: false,
minify: !dev,
external: [...Object.keys(peerDependencies), "dotenv"],
tsconfig: "tsconfig.json",
};
});