update schema gen

+ support for PocketBase v0.22.23
+ support for Deno 2
+ now depends on pocketbase npm package
This commit is contained in:
michal-kapala
2024-11-06 16:17:06 +01:00
parent 8692f25e71
commit ce03fd9991
9 changed files with 130 additions and 86 deletions

View File

@ -8,13 +8,12 @@ import {
ParserOptions,
RawCsvRow,
} from "../types/csv.ts";
// @deno-types="https://unpkg.com/pocketbase@0.12.0/dist/pocketbase.es.d.mts"
import { SchemaField } from "https://unpkg.com/pocketbase@0.12.0/dist/pocketbase.es.mjs";
import {
POCKETBASE_SYSFIELD,
POCKETBASE_TYPE,
PocketbaseRowSchema,
PocketbaseType,
SchemaField
} from "../types/pocketbase.ts";
import { createSchemaField, generateRowSchema } from "./pocketbase.ts";
import { isBool, isDate, isEmail, isJson, isNumber, isUrl } from "./regex.ts";
@ -56,7 +55,7 @@ export async function readCsv(filename: string, options: CsvOptions) {
async function parseCsv(
filename: string | null,
csvOptions: ParserOptions,
): Promise<RawCsvRow[] | null> {
) {
const data: RawCsvRow[] = [];
try {
@ -85,7 +84,7 @@ async function parseCsv(
* @param value Raw string value
* @returns
*/
export function parseBool(value: string): boolean {
export function parseBool(value: string) {
return ["true", "1"].includes(value);
}
@ -95,7 +94,7 @@ export function parseBool(value: string): boolean {
* @param prop - Column name
* @returns `SchemaField`
*/
export function addSchemaField(data: RawCsvRow[], prop: string): SchemaField {
export function addSchemaField(data: RawCsvRow[], prop: string) {
// The new column is prefixed with underscore if it conflicts with a system field
const targetProp = POCKETBASE_SYSFIELD.includes(prop.toLowerCase())
? `_${prop}`
@ -139,7 +138,7 @@ export function addSchemaField(data: RawCsvRow[], prop: string): SchemaField {
export function parseData(
data: RawCsvRow[],
schema: SchemaField[],
): ParsedRow[] {
) {
const rows: ParsedRow[] = [];
// create a row schema for the collection
@ -159,7 +158,7 @@ export function parseData(
* @param schema - Row type template
* @returns
*/
function parseRow(rawRow: RawCsvRow, schema: PocketbaseRowSchema): ParsedRow {
function parseRow(rawRow: RawCsvRow, schema: PocketbaseRowSchema) {
let parsedRow: ParsedRow = {};
const keys = Object.keys(rawRow);

View File

@ -1,5 +1,3 @@
// @deno-types="https://unpkg.com/pocketbase@0.12.0/dist/pocketbase.es.d.mts"
import { SchemaField } from "https://unpkg.com/pocketbase@0.12.0/dist/pocketbase.es.mjs";
import { RawJsonRow } from "../types/json.ts";
import { POCKETBASE_SYSFIELD } from "../types/pocketbase.ts";
import { createSchemaField } from "./pocketbase.ts";
@ -59,7 +57,7 @@ async function parseJson(filename: string) {
* @param prop Column name.
* @returns `SchemaField`
*/
export function addSchemaField(data: RawJsonRow[], prop: string): SchemaField {
export function addSchemaField(data: RawJsonRow[], prop: string) {
// The new column is prefixed with underscore if it conflicts with a system field
const targetProp = POCKETBASE_SYSFIELD.includes(prop.toLowerCase())
? `_${prop}`
@ -111,7 +109,7 @@ export function addSchemaField(data: RawJsonRow[], prop: string): SchemaField {
* @param data Data rows.
* @returns
*/
export function resolveConflicts(data: RawJsonRow[]): RawJsonRow[] {
export function resolveConflicts(data: RawJsonRow[]) {
const rows: RawJsonRow[] = [];
for (const r of data) {

View File

@ -1,11 +1,10 @@
// @deno-types="https://unpkg.com/pocketbase@0.12.0/dist/pocketbase.es.d.mts"
import { SchemaField } from "https://unpkg.com/pocketbase@0.12.0/dist/pocketbase.es.mjs";
import { RawCsvRow } from "../types/csv.ts";
import { RawJsonRow } from "../types/json.ts";
import {
POCKETBASE_TYPE,
PocketbaseRowSchema,
PocketbaseType,
SchemaField
} from "../types/pocketbase.ts";
import { addSchemaField as addCsvSchemaField } from "./csv.ts";
import { addSchemaField as addJsonSchemaField } from "./json.ts";
@ -29,6 +28,7 @@ export function getSchemaType(
"color: red",
);
Deno.exit(-1);
return "text"
}
switch (schemaField.type) {
@ -59,6 +59,7 @@ export function getSchemaType(
"color: red",
);
Deno.exit(-2);
return "text"
}
}
@ -74,84 +75,94 @@ export function createSchemaField(
): SchemaField {
switch (type) {
case POCKETBASE_TYPE.BOOL:
return new SchemaField({
return {
name,
type,
system: false,
required: false,
presentable: false,
unique: false,
options: {},
});
};
case POCKETBASE_TYPE.NUMBER:
return new SchemaField({
return {
name,
type,
system: false,
required: false,
presentable: false,
unique: false,
options: {
min: null,
max: null,
noDecimal: false,
},
});
};
case POCKETBASE_TYPE.PLAIN_TEXT:
return new SchemaField({
return {
name,
type,
system: false,
required: false,
presentable: false,
unique: false,
options: {
min: null,
max: null,
pattern: "",
},
});
};
case POCKETBASE_TYPE.EMAIL:
return new SchemaField({
name,
type,
system: false,
required: false,
unique: false,
options: {
min: null,
max: null,
},
});
case POCKETBASE_TYPE.JSON:
return new SchemaField({
name,
type,
system: false,
required: false,
unique: false,
options: {},
});
case POCKETBASE_TYPE.DATETIME:
return new SchemaField({
name,
type,
system: false,
required: false,
unique: false,
options: {
min: null,
max: null,
},
});
case POCKETBASE_TYPE.URL:
return new SchemaField({
return {
name,
type,
system: false,
required: false,
presentable: false,
unique: false,
options: {
exceptDomains: null,
onlyDomains: null,
},
});
};
case POCKETBASE_TYPE.JSON:
return {
name,
type,
system: false,
required: false,
presentable: false,
unique: false,
options: {
maxSize: 2000000
},
};
case POCKETBASE_TYPE.DATETIME:
return {
name,
type,
system: false,
required: false,
presentable: false,
unique: false,
options: {
min: "",
max: "",
},
};
case POCKETBASE_TYPE.URL:
return {
name,
type,
system: false,
required: false,
presentable: false,
unique: false,
options: {
exceptDomains: null,
onlyDomains: null,
},
};
}
}
@ -182,7 +193,7 @@ export function createSchema(
data: { [key: string]: any },
stringifyId: boolean,
inputFormat: "csv" | "json",
): SchemaField[] {
) {
const schema: SchemaField[] = [];
// Seeks patterns in up to 1k records to avoid poor performance on large datasets