From df56216609d6d55d470ea7a6dd7fb44e074a5a77 Mon Sep 17 00:00:00 2001 From: michal-kapala Date: Tue, 26 Nov 2024 17:39:41 +0100 Subject: [PATCH] update to v0.23.1 + package version bump to 0.22.0 + support for PB v0.23.1 collections --- csv.ts | 7 +- deno.jsonc | 2 +- deno.lock | 8 +-- json.ts | 5 +- types/pocketbase.ts | 69 ++++++++++++++----- utils/pocketbase.ts | 160 +++++++++++++++++++------------------------- 6 files changed, 131 insertions(+), 120 deletions(-) diff --git a/csv.ts b/csv.ts index cfac003..39a1e1d 100644 --- a/csv.ts +++ b/csv.ts @@ -64,7 +64,7 @@ async function importCsv() { const _authResponse = await pb.admins.authWithPassword(adminName, adminPass); // collection schema object - const schema = createSchema(data, options.id, "csv"); + const fields = createSchema(data, options.id, "csv"); const creationDate = new Date().toISOString(); @@ -73,14 +73,13 @@ async function importCsv() { name: collectName, type: "base", system: false, - schema, + fields, indexes: [], listRule: null, viewRule: null, createRule: null, updateRule: null, deleteRule: null, - options: {}, }; // show the submitted collection @@ -96,7 +95,7 @@ async function importCsv() { ); // rows to be sent via PocketBase API - const rows = parseData(data, schema); + const rows = parseData(data, fields); console.log(`[Import] Importing ${rows.length} rows...`); diff --git a/deno.jsonc b/deno.jsonc index 1b137a7..770defe 100644 --- a/deno.jsonc +++ b/deno.jsonc @@ -3,6 +3,6 @@ "dev": "deno run --watch main.ts" }, "imports": { - "pocketbase": "npm:pocketbase@^0.21.5" + "pocketbase": "npm:pocketbase@^0.22.0" } } diff --git a/deno.lock b/deno.lock index 9603783..35830ce 100644 --- a/deno.lock +++ b/deno.lock @@ -1,11 +1,11 @@ { "version": "4", "specifiers": { - "npm:pocketbase@~0.21.5": "0.21.5" + "npm:pocketbase@0.22": "0.22.0" }, "npm": { - "pocketbase@0.21.5": { - "integrity": "sha512-bnI/uinnQps+ElSlzxkc4yvwuSFfKcoszDtXH/4QT2FhGq2mJVUvDlxn+rjRXVntUjPfmMG5LEPZ1eGqV6ssog==" + "pocketbase@0.22.0": { + "integrity": "sha512-jhP0Dcf2Z/4q+SNxqpgV+SJWLZeU0rOJA4TKMxwU7X2olFSBr0jhLu+G6Pc+RIQ40IYrC3WMGwbASPrK6rxQOw==" } }, "remote": { @@ -33,7 +33,7 @@ }, "workspace": { "dependencies": [ - "npm:pocketbase@~0.21.5" + "npm:pocketbase@0.22" ] } } diff --git a/json.ts b/json.ts index f612d97..ae35a76 100644 --- a/json.ts +++ b/json.ts @@ -48,21 +48,20 @@ async function importJson() { const _authResponse = await pb.admins.authWithPassword(adminName, adminPass); // collection schema object - const schema = createSchema(data, options.id, "json"); + const fields = createSchema(data, options.id, "json"); // the new collection const collection: Collection = { name: collectName, type: "base", system: false, - schema, + fields, indexes: [], listRule: null, viewRule: null, createRule: null, updateRule: null, deleteRule: null, - options: {}, }; // show the submitted collection diff --git a/types/pocketbase.ts b/types/pocketbase.ts index 84c3218..dcd4a22 100644 --- a/types/pocketbase.ts +++ b/types/pocketbase.ts @@ -34,32 +34,69 @@ export const POCKETBASE_SYSFIELD = [ "updated", ]; -export type Options = { - [key: string]: any; -}; - -export type SchemaField = { +export interface SchemaField { + hidden: boolean; id?: string; name: string; - type: PocketbaseType; + presentable: boolean; required: boolean; system: boolean; - presentable: boolean; - unique: boolean; - options: Options; + type: PocketbaseType; }; -export type Collection = { +export interface BoolField extends SchemaField { + type: "bool"; +}; + +export interface NumberField extends SchemaField { + max?: number; + min?: number; + onlyInt: boolean; + type: "number"; +}; + +export interface TextField extends SchemaField { + autogeneratePattern: string; + max?: number; + min?: number; + pattern: string; + primaryKey: boolean; + type: "text"; +}; + +export interface EmailField extends SchemaField { + exceptDomains?: string[]; + onlyDomains?: string[]; + type: "email"; +}; + +export interface DateField extends SchemaField { + max: string; + min: string; + type: "date"; +} + +export interface JsonField extends SchemaField { + maxSize: number; + type: "json"; +} + +export interface UrlField extends SchemaField { + exceptDomains?: string[]; + onlyDomains?: string[]; + type: "url"; +} + +export interface Collection { id?: string; name: string; type: string; system: boolean; - schema: SchemaField[]; + fields: SchemaField[]; indexes: string[]; listRule: string | null; - viewRule: string |null; - createRule: string |null; - updateRule: string |null; - deleteRule: string |null; - options: Options; + viewRule: string | null; + createRule: string | null; + updateRule: string | null; + deleteRule: string | null; }; diff --git a/utils/pocketbase.ts b/utils/pocketbase.ts index 3060aa0..ce55d5a 100644 --- a/utils/pocketbase.ts +++ b/utils/pocketbase.ts @@ -4,7 +4,14 @@ import { POCKETBASE_TYPE, PocketbaseRowSchema, PocketbaseType, - SchemaField + SchemaField, + BoolField, + NumberField, + TextField, + EmailField, + JsonField, + DateField, + UrlField } from "../types/pocketbase.ts"; import { addSchemaField as addCsvSchemaField } from "./csv.ts"; import { addSchemaField as addJsonSchemaField } from "./json.ts"; @@ -28,39 +35,19 @@ export function getSchemaType( "color: red", ); Deno.exit(-1); - return "text" + return "text"; } - switch (schemaField.type) { - case POCKETBASE_TYPE.BOOL: - return POCKETBASE_TYPE.BOOL; - - case POCKETBASE_TYPE.NUMBER: - return POCKETBASE_TYPE.NUMBER; - - case POCKETBASE_TYPE.PLAIN_TEXT: - return POCKETBASE_TYPE.PLAIN_TEXT; - - case POCKETBASE_TYPE.EMAIL: - return POCKETBASE_TYPE.EMAIL; - - case POCKETBASE_TYPE.JSON: - return POCKETBASE_TYPE.JSON; - - case POCKETBASE_TYPE.DATETIME: - return POCKETBASE_TYPE.DATETIME; - - case POCKETBASE_TYPE.URL: - return POCKETBASE_TYPE.URL; - - default: - console.error( - `%cPbTypeError: Unsupported type '${schemaField.type}'`, - "color: red", - ); - Deno.exit(-2); - return "text" + if (schemaField.type == null) { + console.error( + `%cSchemaError: Column type missing for '${column}'`, + "color: red", + ); + Deno.exit(-1); + return "text"; } + + return schemaField.type; } /** @@ -76,93 +63,82 @@ export function createSchemaField( switch (type) { case POCKETBASE_TYPE.BOOL: return { + hidden: false, name, - type, - system: false, - required: false, presentable: false, - unique: false, - options: {}, - }; + required: false, + system: false, + type, + } as BoolField; case POCKETBASE_TYPE.NUMBER: return { + hidden: false, + max: undefined, + min: undefined, name, - type, - system: false, - required: false, + onlyInt: false, presentable: false, - unique: false, - options: { - min: null, - max: null, - noDecimal: false, - }, - }; + required: false, + system: false, + type, + } as NumberField; case POCKETBASE_TYPE.PLAIN_TEXT: return { + autogeneratePattern: "", + hidden: false, + max: 0, + min: 0, name, - type, - system: false, - required: false, + pattern: "", presentable: false, - unique: false, - options: { - min: null, - max: null, - pattern: "", - }, - }; + primaryKey: false, + required: false, + system: false, + type, + } as TextField; case POCKETBASE_TYPE.EMAIL: return { + exceptDomains: undefined, + hidden: false, name, - type, - system: false, - required: false, + onlyDomains: undefined, presentable: false, - unique: false, - options: { - exceptDomains: null, - onlyDomains: null, - }, - }; + required: false, + system: false, + type, + } as EmailField; case POCKETBASE_TYPE.JSON: return { + hidden: false, + maxSize: 0, name, - type, - system: false, - required: false, presentable: false, - unique: false, - options: { - maxSize: 2000000 - }, - }; + required: false, + system: false, + type, + } as JsonField; case POCKETBASE_TYPE.DATETIME: return { + hidden: false, + max: "", + min: "", name, - type, - system: false, - required: false, presentable: false, - unique: false, - options: { - min: "", - max: "", - }, - }; + required: false, + system: false, + type, + } as DateField; case POCKETBASE_TYPE.URL: return { + hidden: false, + exceptDomains: undefined, name, - type, - system: false, - required: false, + onlyDomains: undefined, presentable: false, - unique: false, - options: { - exceptDomains: null, - onlyDomains: null, - }, - }; + required: false, + system: false, + type, + } as UrlField; } }