Page MenuHomeSealhub

extract-fields-from-collection.ts
No OneTemporary

extract-fields-from-collection.ts

import _locreq from "locreq";
import { exec } from "./utils.js";
const target_locreq = _locreq(process.cwd());
import { promises as fs } from "fs";
import { is, predicates } from "@sealcode/ts-predicates";
export type ExtractedFieldInfo = {
name: string;
type: string;
target: string | null;
referencing_collection: string | null;
referencing_field: string | null;
intermediary_field_that_points_there: string | null;
is_readonly: boolean;
is_required: boolean;
subfields: { name: string }[];
};
const counter = (function* () {
let i = 0;
while (true) {
yield i++;
}
})();
export default async function extract_fields_from_collection(
collection_name: string
): Promise<ExtractedFieldInfo[]> {
const extractor_code = `import {default as the_app} from "./app.js";
const c = new the_app().collections["${collection_name}"];
const fields = [];
for (const field_name in c.fields){
const field = c.fields[field_name];
let type = field.typeName;
let target = null;
let referencing_collection = null;
let referencing_field = null;
let intermediary_field_that_points_there = null;
let is_readonly = false;
let is_required = field.required;
let subfields = [];
if(["derived-value", "cached-value"].includes(type)){
type = field.virtual_field.typeName;
}
if(["derived-value", "cached-value", "reverse-single-reference"].includes(field.typeName)){
is_readonly = true;
}
if(type == "deep-reverse-single-reference" || type =="reverse-single-reference"){
target = field.target_collection;
referencing_collection = field.referencing_collection;
referencing_field = field.referencing_field;
intermediary_field_that_points_there = field.intermediary_field_that_points_there
}
if( type == "structured-array" ) {
subfields = Object.keys(field.subfields).map(key=>({name: key}));
}
fields.push({ name: field_name, type, target, referencing_collection, referencing_field, intermediary_field_that_points_there, is_readonly, is_required, subfields })
}
console.log(JSON.stringify(fields));
`;
const extractor_code_path = target_locreq.resolve(
`dist/back/___extract_fields--${counter.next().value || 0}.js`
);
await fs.writeFile(extractor_code_path, extractor_code);
const { stdout } = await exec("node", [extractor_code_path]);
await fs.unlink(extractor_code_path);
const ret = JSON.parse(stdout) as unknown;
if (
!is(
ret,
predicates.array(
predicates.shape({
name: predicates.string,
type: predicates.string,
is_readonly: predicates.boolean,
})
)
)
) {
throw new Error(
"Encountered a problem while extracting the names of fields from collection. Got: " +
stdout
);
}
return ret as ExtractedFieldInfo[];
}

File Metadata

Mime Type
text/x-java
Expires
Mon, May 19, 00:55 (1 d, 19 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
625715
Default Alt Text
extract-fields-from-collection.ts (2 KB)

Event Timeline