// noinspection JSUnusedGlobalSymbols,SpellCheckingInspection import { C6Constants, iC6Object, iRest, removePrefixIfExists, } from "@carbonorm/carbonnode"; import type { C6RestfulModel, iDynamicApiImport, } from "@carbonorm/carbonnode"; {{#RELATIONS}} {{#if READ_ONLY}} import type { i{{TABLE_NAME_SHORT_PASCAL_CASE}} } from "./views/{{TABLE_NAME_SHORT_PASCAL_CASE}}"; {{else}} import type { i{{TABLE_NAME_SHORT_PASCAL_CASE}} } from "./tables/{{TABLE_NAME_SHORT_PASCAL_CASE}}"; {{/if}} {{/RELATIONS}} export const RestTablePrefix = '{{{PREFIX}}}'; export type RestTableNames = {{{RestTableNames}}}; export type RestShortTableNames = {{{RestShortTableNames}}}; export type RestViewNames = {{{RestViewNames}}}; export type RestShortViewNames = {{{RestShortViewNames}}}; export type RestTableInterfaces = {{{RestTableInterfaces}}}; export const TABLES = {} as Record>; export const VIEWS = {} as Record>; export const C6Core: iC6Object = { ...C6Constants, C6VERSION: '{{C6VERSION}}', IMPORT: async (tableName: string): Promise => { tableName = tableName.toLowerCase(); const error = (table: string) => { throw Error('Table (' + table + ') does not exist in the TABLES object. Possible values include (' + Object.keys(TABLES).join(', ') + ')'); }; if (!TABLES[tableName as RestShortTableNames]) { if (!tableName.startsWith(RestTablePrefix.toLowerCase())) { error(tableName); } tableName = removePrefixIfExists(tableName, RestTablePrefix); if (!TABLES[tableName as RestShortTableNames]) { error(tableName); } } const toPascalUnderscore = (name: string) => name .split("_") .map((word) => word.charAt(0).toUpperCase() + word.slice(1)) .join("_"); const relation = TABLES[tableName as RestShortTableNames]; const moduleDir = relation?.RELATION_TYPE === 'VIEW' ? 'views' : 'tables'; return import( /* @vite-ignore */ `./${moduleDir}/${toPascalUnderscore(tableName)}` ); }, PREFIX: RestTablePrefix, TABLES: TABLES as any, VIEWS: VIEWS as any, ORM: {}, }; export type tStatefulApiData = T[] | undefined; // this refers to the value types of the keys above, aka values in the state export interface iRestfulObjectArrayTypes { {{#RELATIONS}} {{TABLE_NAME_SHORT}}: tStatefulApiData, {{/RELATIONS}} } export type tRestfulObjectArrayValues = iRestfulObjectArrayTypes[keyof iRestfulObjectArrayTypes]; export const initialRestfulObjectsState: iRestfulObjectArrayTypes = { {{#RELATIONS}} {{TABLE_NAME_SHORT}}: undefined, {{/RELATIONS}} }; export const COLUMNS = { {{#RELATIONS}}{{#each TYPE_VALIDATION}}'{{@key}}': '{{this.COLUMN_NAME}}',{{/each}} {{/RELATIONS}} }; export const GLOBAL_REST_PARAMETERS: Omit, "requestMethod" | "restModel"> = { C6: C6Core, restURL: {{{REST_URL_EXPRESSION}}}, }; export const registerC6Table = ( shortName: string, bindingName: string, tableModel: Record, restBinding: Record, relationType: 'TABLE' | 'VIEW' = 'TABLE', ) => { (TABLES as Record>)[shortName] = tableModel as C6RestfulModel; if (relationType === 'VIEW') { (VIEWS as Record>)[shortName] = tableModel as C6RestfulModel; } (C6Core as Record)[shortName] = tableModel; (C6Core.ORM as Record)[bindingName] = restBinding; };