Skip to content

Commit

Permalink
collection names constant object generation
Browse files Browse the repository at this point in the history
  • Loading branch information
himanshurajora committed Dec 11, 2022
1 parent 71897b7 commit b750e97
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 43 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "pocketbase-to-types",
"version": "1.0.12",
"version": "1.0.14",
"description": "",
"main": "index.js",
"scripts": {
Expand Down
20 changes: 11 additions & 9 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
export const InputFileArg = '--input'
export const OutputDirArg = '--outputDir'
export const InputFileArg = '--input';
export const OutputDirArg = '--outputDir';

export const InputFileSortArg = '-i';
export const OutputDirSortArg = '-o';

export const AllPTTArgs = [
InputFileArg,
InputFileSortArg,
OutputDirArg,
OutputDirSortArg
]
InputFileArg,
InputFileSortArg,
OutputDirArg,
OutputDirSortArg,
];

export const ValueDoesNotExistIndex = -1;

export const ExportInterfaceToken = 'export interface'
export const ExpandRelationToken = 'expand'
export const ExportInterfaceToken = 'export interface';
export const ExpandRelationToken = 'expand';
export const ExportConstantObjectForCollectionNames =
'export const Collections =';
34 changes: 23 additions & 11 deletions src/parse-pocketbase-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ import { existsSync, mkdir, mkdirSync, writeFileSync } from 'fs';
import _ from 'lodash';
import path from 'path';
import { clearConfigCache, format } from 'prettier';
import { ExpandRelationToken, ExportInterfaceToken } from './constants';
import {
ExpandRelationToken,
ExportConstantObjectForCollectionNames,
ExportInterfaceToken,
} from './constants';
import {
convertToOptionalProperty,
convertToInterfaceName,
Expand Down Expand Up @@ -34,18 +38,26 @@ export function parsePocketBaseSchema(
}

export const interfaceGenerator = (collectionSchemas: CollectionSchema[]) => {
return _.join(
const interfaces = _.map(collectionSchemas, (collectionSchema) => {
const { name } = collectionSchema;
const interfaceStart = interfaceStartGenerator(name);
const parsedProperties = propertiesGenerator(
collectionSchema,
collectionSchemas
);
return `${interfaceStart}${parsedProperties}\n`;
});

const collectionNameConstants = `${ExportConstantObjectForCollectionNames}{${_.join(
_.map(collectionSchemas, (collectionSchema) => {
const { name } = collectionSchema;
const interfaceStart = interfaceStartGenerator(name);
const parsedProperties = propertiesGenerator(
collectionSchema,
collectionSchemas
);
return `${interfaceStart}${parsedProperties}\n`;
return `${convertToInterfaceName(collectionSchema.name)}: '${
collectionSchema.name
}'`;
}),
''
);
','
)}}`;

return _.join([...interfaces, collectionNameConstants], '');
};
export function interfaceStartGenerator(collectionSchemaName: string) {
const name = convertToInterfaceName(collectionSchemaName);
Expand Down
58 changes: 36 additions & 22 deletions types/index.ts
Original file line number Diff line number Diff line change
@@ -1,38 +1,52 @@
export interface Users {
name?: string;
avatar?: string;
expand: {};
role: "student" | "teacher" | "departmentAdmin" | "collegeAdmin" | "guest";
code: string;
expand: {
department?: Departments;
"colleges(admin)"?: Colleges[];
"departments(admin)"?: Departments[];
"attendances(student)"?: Attendances[];
};
}
export interface Colleges {
name?: string;
code?: string;
name: string;
code: string;
expand: {
field: Test;
"departments(_college)": Departments;
admin?: Users;
"departments(college)"?: Departments[];
};
}
export interface Departments {
name?: string;
code?: string;
name: string;
code: string;
expand: {
_college: Colleges;
"test(department)": Test;
admin?: Users;
college?: Colleges;
"users(department)"?: Users[];
"classes(department)"?: Classes[];
};
}
export interface Test {
field?: boolean;
field1?: "one" | "two" | "three";
field2?: any;
field3?: string;
export interface Classes {
name: string;
code: string;
expand: {
department: Departments;
"colleges(field)": Colleges;
department?: Departments;
"attendances(class)"?: Attendances[];
};
}
export interface AllFields {
field?: number;
field1?: string;
field2?: Date;
field3?: any;
expand: {};
export interface Attendances {
present?: boolean;
expand: {
class?: Classes;
student?: Users;
};
}
export const Collections = {
Users: "users",
Colleges: "colleges",
Departments: "departments",
Classes: "classes",
Attendances: "attendances",
};

0 comments on commit b750e97

Please sign in to comment.