Skip to content

Commit

Permalink
feat!: overhaul everything to be Zod-based
Browse files Browse the repository at this point in the history
  • Loading branch information
rintaun committed Dec 8, 2022
1 parent cf6417c commit d3d2166
Show file tree
Hide file tree
Showing 27 changed files with 1,824 additions and 905 deletions.
511 changes: 283 additions & 228 deletions src/datasource/DBDataSource.ts

Large diffs are not rendered by default.

54 changes: 34 additions & 20 deletions src/datasource/__tests__/DBDataSource.test.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,31 @@
import { z } from 'zod'
import { DBDataSource } from '..'
import { createMockPool } from '../../testing'
import { LoaderFactory } from '../loaders'

interface DummyRowType {
id: number
name: string
code: string
const DummyMetadata = {
id: {
nativeType: 'anything',
nativeName: 'id',
},
name: {
nativeType: 'anything',
nativeName: 'name',
},
code: {
nativeType: 'anything',
nativeName: 'code',
},
}

const columnTypes: Record<keyof DummyRowType, string> = {
id: 'anything',
name: 'anything',
code: 'anything',
}
const DummyRowType = z.object({
id: z.number(),
name: z.string(),
code: z.string(),
})

describe(DBDataSource, () => {
const dummyBatchFn = async (): Promise<DummyRowType[]> => {
const dummyBatchFn = async (): Promise<z.infer<typeof DummyRowType>[]> => {
return [
{ id: 1, name: 'aaa', code: 'abc' },
{ id: 2, name: 'bbb', code: 'def' },
Expand All @@ -33,17 +43,21 @@ describe(DBDataSource, () => {
]
}

const factory = new LoaderFactory<DummyRowType>(dummyBatchFn, dummyBatchFn, {
columnTypes,
})
const factory = new LoaderFactory(dummyBatchFn, dummyBatchFn, DummyMetadata)

class DummyDBDataSource extends DBDataSource<DummyRowType> {
class DummyDBDataSource extends DBDataSource<
typeof DummyMetadata,
typeof DummyRowType,
typeof DummyRowType
> {
constructor() {
super(createMockPool(), 'any_table', {
id: 'any',
name: 'any',
code: 'any',
})
super(
createMockPool(),
'any_table',
DummyMetadata,
DummyRowType /* select */,
DummyRowType /* insert */
)
}

protected get loaders() {
Expand All @@ -66,7 +80,7 @@ describe(DBDataSource, () => {
const dataSource = new TestDataSource()

it('uses the default options', () => {
expect(dataSource.testBuilder.select()).toMatchSnapshot()
expect(dataSource.testBuilder.select().sql).toMatchSnapshot()
})
})
})
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`DBDataSource defaultOptions uses the default options 1`] = `
{
"sql": "
"
SELECT *
FROM "any_table"
ORDER BY "any_table"."id"
",
"type": "SLONIK_TOKEN_SQL",
"values": [],
}
"
`;
Loading

0 comments on commit d3d2166

Please sign in to comment.