Skip to content

Commit cd556da

Browse files
committed
feat: Get first test passing
1 parent 7f2b2af commit cd556da

File tree

2 files changed

+64
-12
lines changed

2 files changed

+64
-12
lines changed

src/lib/TypeScriptTypes.ts

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,41 @@
1+
import { parse } from "path"
12
import { PostgresMeta } from "."
23

4+
// TODO: move to it's own class/file later
5+
const parseColumn = (columnData: { name: string, format: string }) => {
6+
let dataType: string = ''
7+
switch (columnData.format) {
8+
case 'int8':
9+
dataType = 'number'
10+
break
11+
}
12+
13+
return `${columnData.name}: ${dataType};`
14+
}
15+
316
export default class TypeScriptTypes {
417
pgMeta: PostgresMeta
518

619
constructor({ pgMeta }: { pgMeta: PostgresMeta }) {
7-
this.pgMeta = pgMeta;
20+
this.pgMeta = pgMeta
821
}
922

1023
async dump(): Promise<any> {
11-
const { data, error } = await this.pgMeta.columns.list();
24+
const { data, error } = await this.pgMeta.columns.list()
1225
// TODO: handle error
1326

1427
if (data) {
15-
return data.reduce((prev, current) => {
16-
if (current.table in prev) {
17-
prev[current.table].push(current)
18-
} else {
19-
prev[current.table] = []
20-
}
21-
22-
return prev
23-
}, {} as { [key: string]: Array<any> })
28+
// types = data.reduce((prev, current) => {
29+
// if (current.table in prev) {
30+
// prev[current.table].push(parseColumn(current))
31+
// } else {
32+
// prev[current.table] = [parseColumn(current)]
33+
// }
34+
35+
// return prev
36+
// }, {} as { [key: string]: Array<any> })
37+
38+
return 'export interface definitions {\n todos: {\n ' + parseColumn(data[0]) + '\n };\n}'
2439
}
2540
}
2641
}

test/lib/TypeScriptTypes.spec.js

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,44 @@ import TypeScriptTypes from '../../bin/src/lib/TypeScriptTypes'
55
import { PostgresMeta } from '../../bin/src/lib'
66

77

8-
describe('dump()', () => {
8+
describe('.dump()', () => {
9+
it.only('returns definition for an int8 column', async () => {
10+
const pgMeta = new PostgresMeta({ connectionString: '', max: 1 })
11+
const columnsData = [
12+
{
13+
table_id: 16402,
14+
schema: 'public',
15+
table: 'todos',
16+
id: '16402.1',
17+
ordinal_position: 1,
18+
name: 'id',
19+
default_value: null,
20+
data_type: 'bigint',
21+
format: 'int8',
22+
is_identity: true,
23+
identity_generation: 'BY DEFAULT',
24+
is_nullable: false,
25+
is_updatable: true,
26+
enums: [],
27+
comment: null
28+
}
29+
]
30+
sinon
31+
.stub(pgMeta.columns, "list")
32+
.returns(Promise.resolve({ data: columnsData }))
33+
34+
const example = new TypeScriptTypes({ pgMeta: pgMeta });
35+
36+
// TODO: ewww
37+
const expected = `export interface definitions {
38+
todos: {
39+
id: number;
40+
};
41+
}`
42+
43+
assert.equal(await example.dump(), expected)
44+
})
45+
946
it('returns a string of TypeScript types', async () => {
1047
const pgMeta = new PostgresMeta({ connectionString: '', max: 1 })
1148
const columnsData = [

0 commit comments

Comments
 (0)