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 default async function extract_fields_from_collection(
collection_name: string
): Promise<{ name: string; type: string }[]> {
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;
if(["derived-value", "cached-value"].includes(type)){
type = field.virtual_field.typeName
}
fields.push({name: field_name, type})
}
console.log(JSON.stringify(fields));
`;
const extractor_code_path = target_locreq.resolve(
"dist/back/___extract_fields.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,
})
)
)
) {
throw new Error(
"Encountered a problem while extracting the names of fields from collection. Got: " +
stdout
);
}
return ret;
}

File Metadata

Mime Type
text/x-java
Expires
Wed, May 7, 19:43 (1 d, 9 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
630319
Default Alt Text
extract-fields-from-collection.ts (1 KB)

Event Timeline