Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate types from a json definition #23

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
*
!index.ts
!types.ts
!jest.config.js
!package.json
!package-lock.json
Expand Down
3 changes: 3 additions & 0 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ jobs:
- run: npm run compile

- run: npm run lint-check
- run: npm run _generate-types
env:
ERROR_ON_CHANGES: 'yes'
- run: npm run test
env:
PG_HOST: pg
Expand Down
72 changes: 72 additions & 0 deletions _generate-types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import * as pathlib from 'path';
import * as fs from 'fs';
import * as t from 'io-ts';
import * as eta from 'eta';
import { isLeft } from 'fp-ts/lib/Either';

const PgAttributes = t.record(t.string, t.string);

type PgAttributes = t.TypeOf<typeof PgAttributes>;

const PgProperties = t.type({
required: t.boolean,
attributes: PgAttributes,
});

type PgProperties = t.TypeOf<typeof PgProperties>;

const PgType = t.partial({
attributes: PgAttributes,
properties: PgProperties,
options: PgProperties,
});

type PgType = t.TypeOf<typeof PgType>;

const PgTypes = t.record(t.string, PgType);

type PgTypes = t.TypeOf<typeof PgTypes>;

const main = async () => {
const raw: unknown = JSON.parse(fs.readFileSync(pathlib.join(__dirname, 'types.json'), 'utf8'));
const parse = PgTypes.decode(raw);

if (isLeft(parse)) {
throw new Error('Incorrect shape');
}

const parsed: PgTypes = parse.right;

const template = fs.readFileSync(pathlib.join(__dirname, '_types_template.ts'), 'utf8');

const upperCaseFirst = (str: string): string => {
return str.charAt(0).toUpperCase() + str.slice(1);
}

const typesTs = eta.render(template, {
types: parsed,
upperCaseFirst,
});

if (typeof typesTs !== 'string') {
throw new Error('Rendering failed');
}

const existing = fs.readFileSync(pathlib.join(__dirname, 'types.ts'), 'utf8');

if (process.env.ERROR_ON_CHANGES) {
if (existing !== typesTs) {
console.error('Changes detected, exiting')
process.exit(1);
}
}

fs.writeFileSync(pathlib.join(__dirname, 'types.ts'), typesTs, {
encoding: 'utf8',
});
};

main().catch((err) => {
console.error(err);
process.exit(1);
});
29 changes: 29 additions & 0 deletions _types_template.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Generated from types.json
<% for (const [name, pgType] of Object.entries(it.types)) { %>
<% if (pgType.properties) { %>
export interface <%=it.upperCaseFirst(name) %>Properties {
<% for (const [attrName, attrValue] of Object.entries(pgType.properties.attributes)) { %>
<%= attrName %><%=attrValue.startsWith('?') ? '?' : ''%>: <%~ attrValue.startsWith('?') ? attrValue.slice(1) : attrValue %>;
<% } %>
}

<% } %>
<% if (pgType.options) { %>
export interface <%=it.upperCaseFirst(name) %>Options {
<% for (const [attrName, attrValue] of Object.entries(pgType.options.attributes)) { %>
<%= attrName %><%=attrValue.startsWith('?') ? '?' : ''%>: <%~ attrValue.startsWith('?') ? attrValue.slice(1) : attrValue %>;
<% } %>
}

<% } %>
export interface <%=it.upperCaseFirst(name) %> {
<% for (const [attrName, attrValue] of Object.entries(pgType.attributes)) { %>
<% if (pgType.properties) { %> properties<%=pgType.properties.required ? '' : '?' %>: <%=it.upperCaseFirst(name) %>Properties;
<% } %>
<% if (pgType.options) { %> options<%=pgType.options.required ? '' : '?' %>: <%=it.upperCaseFirst(name) %>Options;
<% } %>
<%= attrName %><%=attrValue.startsWith('?') ? '?' : ''%>: <%~ attrValue.startsWith('?') ? attrValue.slice(1) : attrValue %>;
<% } %>
}

<% } %>
87 changes: 14 additions & 73 deletions index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
export * from './types';
import {
DatabaseProperties,
Database,
RoleProperties,
Role,
GrantOnDatabaseProperties,
GrantOnDatabaseOptions,
GrantOnDatabase,
GrantOnTableProperties,
GrantOnTableOptions,
GrantOnTable,
} from './types';

// eslint-disable-next-line @typescript-eslint/no-explicit-any
export type QueryResultRow = Record<string, any>;

Expand All @@ -12,79 +26,6 @@ export interface Config {
useTransactions?: boolean;
}

export interface DatabaseProperties {
owner?: string;
template?: string;
encoding?: string;
lcCollate?: string;
lcCtype?: string;
tablespace?: string;
connectionLimit?: number;
}

export interface Database {
name: string;
properties?: DatabaseProperties;
}

export interface RoleProperties {
isSuperuser?: boolean;
canCreateDb?: boolean;
canCreateRole?: boolean;
inherit?: boolean;
login?: boolean;
connectionLimit?: number;
password?: string;
passwordValidUntil?: Date;
passwordEncrypted?: boolean;
inRoles?: Array<string>;
roles?: Array<string>;
adminRoles?: Array<string>;
}

export interface Role {
name: string;
properties?: RoleProperties;
}

export interface GrantOnDatabaseProperties {
privileges?: Array<string>;
allPrivileges?: boolean;
noPrivileges?: boolean;
databases: Array<string>;
schemas?: Array<string>;
}

export interface GrantOnDatabaseOptions {
prune?: boolean;
}

export interface GrantOnDatabase {
roles: Array<string>;
properties: GrantOnDatabaseProperties;
options?: GrantOnDatabaseOptions;
}

export interface GrantOnTableProperties {
privileges?: Array<string>;
allPrivileges?: boolean;
noPrivileges?: boolean;
tables?: Array<string>;
allTables?: boolean;
schemas?: Array<string>;
}

export interface GrantOnTableOptions {
prune?: boolean;
alterDefault?: boolean;
}

export interface GrantOnTable {
roles: Array<string>;
properties: GrantOnTableProperties;
options?: GrantOnTableOptions;
}

interface ExecuteQueriesProps {
config: Config;
queries: Array<string>;
Expand Down
Loading