Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master' into transform-properties
Browse files Browse the repository at this point in the history
  • Loading branch information
mmkal committed Mar 28, 2020
2 parents acaca20 + 87ffb22 commit 91282e0
Show file tree
Hide file tree
Showing 20 changed files with 1,386 additions and 791 deletions.
9 changes: 6 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,20 +51,23 @@
]
},
"homepage": "https://github.com/mmkal/slonik-tools#readme",
"resolutions": {
"minimist": "^1.2.5"
},
"devDependencies": {
"@types/jest": "^24.0.18",
"@types/jest": "^25.1.3",
"@types/node": "^12.6.8",
"@types/slonik": "^22.1.0",
"check-clean": "^0.1.0",
"coveralls": "^3.0.5",
"jest": "^24.9.0",
"jest": "^25.1.0",
"lerna": "^3.16.1",
"nodemon": "^2.0.2",
"npm-run-all": "^4.1.5",
"prettier": "^1.18.2",
"slonik": "^22.4.3",
"ts-expect": "^1.1.0",
"ts-jest": "^24.1.0",
"ts-jest": "^25.2.1",
"ts-node": "^8.3.0",
"typescript": "^3.5.3"
}
Expand Down
23 changes: 23 additions & 0 deletions packages/demo/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,29 @@
All notable changes to this project will be documented in this file.
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.

# [0.4.0](https://github.com/mmkal/slonik-tools/compare/slonik-tools-demo@0.3.0...slonik-tools-demo@0.4.0) (2020-03-23)


### Features

* de-duplicate types ([#157](https://github.com/mmkal/slonik-tools/issues/157)) ([d2ef0fd](https://github.com/mmkal/slonik-tools/commit/d2ef0fd9da3f23a9e906b0c4ead3bd7af78d92e8))





# [0.3.0](https://github.com/mmkal/slonik-tools/compare/slonik-tools-demo@0.2.1...slonik-tools-demo@0.3.0) (2020-03-23)


### Features

* **slonik:** bump slonik to v22 ([#143](https://github.com/mmkal/slonik-tools/issues/143)) ([6d97b00](https://github.com/mmkal/slonik-tools/commit/6d97b00fd15b98d66d400d50b12979bab0e63d87))






## [0.2.1](https://github.com/mmkal/slonik-tools/compare/slonik-tools-demo@0.2.0...slonik-tools-demo@0.2.1) (2020-01-26)

**Note:** Version bump only for package slonik-tools-demo
Expand Down
6 changes: 3 additions & 3 deletions packages/demo/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "slonik-tools-demo",
"version": "0.2.1",
"version": "0.4.0",
"description": "demo project using packages in slonik-tools repo",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down Expand Up @@ -31,8 +31,8 @@
"slonik-interceptor-field-name-transformation": "^1.5.2"
},
"devDependencies": {
"@slonik/migrator": "^0.2.0",
"@slonik/typegen": "^0.3.1",
"@slonik/migrator": "^0.3.0",
"@slonik/typegen": "^0.5.0",
"@types/express": "^4.17.0",
"@types/supertest": "^2.0.7",
"supertest": "^4.0.2"
Expand Down
10 changes: 8 additions & 2 deletions packages/demo/src/generated/db/Message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,21 @@
// tslint:disable
// this file is generated by a tool; don't change it manually.

export interface Message_QueryTypeMap {
["select * from messages\n where id < $1\n order by created_at desc\n limit 10"]: {
export type Message_AllTypes = [
{
/** pg_type.typname: int4 */
id: number
/** pg_type.typname: varchar */
content: string
/** pg_type.typname: timestamptz */
createdAt: Date
}
]
export interface Message_QueryTypeMap {
[`select * from messages
where id < $1
order by created_at desc
limit 10`]: Message_AllTypes[0]
}

export type Message_UnionType = Message_QueryTypeMap[keyof Message_QueryTypeMap]
Expand Down
9 changes: 7 additions & 2 deletions packages/demo/src/generated/db/MessageId.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,16 @@
// tslint:disable
// this file is generated by a tool; don't change it manually.

export interface MessageId_QueryTypeMap {
["insert into messages(content)\n values ($1)\n returning id"]: {
export type MessageId_AllTypes = [
{
/** pg_type.typname: int4 */
id: number
}
]
export interface MessageId_QueryTypeMap {
[`insert into messages(content)
values ($1)
returning id`]: MessageId_AllTypes[0]
}

export type MessageId_UnionType = MessageId_QueryTypeMap[keyof MessageId_QueryTypeMap]
Expand Down
12 changes: 12 additions & 0 deletions packages/migrator/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,18 @@
All notable changes to this project will be documented in this file.
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.

# [0.3.0](https://github.com/mmkal/slonik-tools/compare/@slonik/migrator@0.2.0...@slonik/migrator@0.3.0) (2020-03-23)


### Features

* **slonik:** bump slonik to v22 ([#143](https://github.com/mmkal/slonik-tools/issues/143)) ([6d97b00](https://github.com/mmkal/slonik-tools/commit/6d97b00fd15b98d66d400d50b12979bab0e63d87))






# [0.2.0](https://github.com/mmkal/slonik-tools/compare/@slonik/migrator@0.1.13...@slonik/migrator@0.2.0) (2019-12-24)


Expand Down
2 changes: 1 addition & 1 deletion packages/migrator/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@slonik/migrator",
"version": "0.2.0",
"version": "0.3.0",
"description": "database migration tool using slonik",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
23 changes: 23 additions & 0 deletions packages/typegen/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,29 @@
All notable changes to this project will be documented in this file.
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.

# [0.5.0](https://github.com/mmkal/slonik-tools/compare/@slonik/typegen@0.4.0...@slonik/typegen@0.5.0) (2020-03-23)


### Features

* de-duplicate types ([#157](https://github.com/mmkal/slonik-tools/issues/157)) ([d2ef0fd](https://github.com/mmkal/slonik-tools/commit/d2ef0fd9da3f23a9e906b0c4ead3bd7af78d92e8))





# [0.4.0](https://github.com/mmkal/slonik-tools/compare/@slonik/typegen@0.3.1...@slonik/typegen@0.4.0) (2020-03-23)


### Features

* **slonik:** bump slonik to v22 ([#143](https://github.com/mmkal/slonik-tools/issues/143)) ([6d97b00](https://github.com/mmkal/slonik-tools/commit/6d97b00fd15b98d66d400d50b12979bab0e63d87))






## [0.3.1](https://github.com/mmkal/slonik-tools/compare/@slonik/typegen@0.3.0...@slonik/typegen@0.3.1) (2020-01-26)


Expand Down
2 changes: 1 addition & 1 deletion packages/typegen/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@slonik/typegen",
"version": "0.3.1",
"version": "0.5.0",
"description": "Automatically generates typescript types from slonik queries",
"files": [
"dist",
Expand Down
39 changes: 33 additions & 6 deletions packages/typegen/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,15 @@ export const createCodegenDirectory = (directory: string) => {
fs.writeFileSync(join(directory, 'index.ts'), 'export const knownTypes = {}' + EOL, 'utf8')
}

const writeIfChanged = (path: string, content: string) => {
const trimmed = content.trim()
const existingContent = fs.existsSync(path) ? fs.readFileSync(path).toString().trim() : null
if (trimmed === existingContent) {
return
}
fs.writeFileSync(path, trimmed + EOL, 'utf8')
}

export const resetCodegenDirectory = (directory: string) => {
if (fs.existsSync(directory)) {
fs.readdirSync(directory).forEach(filename => fs.unlinkSync(join(directory, filename)))
Expand Down Expand Up @@ -179,7 +188,7 @@ export const setupSqlGetter = <KnownTypes>(config: TypeGenConfig<KnownTypes>): T
t => `${t.typname}`.replace(/^_/, 'zzz'),
)
_oidToTypeName = fromPairs(types.map(t => [t.oid as number, t.typname as string]))
fs.writeFileSync(
writeIfChanged(
join(config.writeTypes, '_pg_types.ts'),
[
`${header}`,
Expand Down Expand Up @@ -263,13 +272,31 @@ const getFsTypeWriter = (generatedPath: string) => (typeName: string, properties
_entries = orderBy(_entries, e => e.description)
_entries = _entries.filter((e, i, arr) => i === arr.findIndex(x => x.description === e.description))

const contnt = [
const entriesWithTypes = _entries.map(e => ({
...e,
type: codegen.writeInterfaceBody(e.properties),
}))

const uniqueTypes = entriesWithTypes
.map(e => e.type)
.filter((type, i, arr) => i === arr.indexOf(type))

const backtick = '`'
const queryLiteral = (q: string) => q.includes(backtick) ? JSON.stringify(q) : backtick + q + backtick

const tsContent = [
header,
``,
`export type ${typeName}_AllTypes = [`,
uniqueTypes
.map(t => ' ' + t)
.join(',' + EOL)
.replace(/\r?\n/g, EOL + ' '),
`]`,
`export interface ${typeName}_QueryTypeMap {`,
' ' +
_entries
.map(e => `[${JSON.stringify(e.description)}]: ${codegen.writeInterfaceBody(e.properties)}`)
entriesWithTypes
.map(e => `[${queryLiteral(e.description)}]: ${typeName}_AllTypes[${uniqueTypes.indexOf(e.type)}]`)
.join(EOL)
.replace(/\r?\n/g, EOL + ' '),
`}`,
Expand All @@ -285,14 +312,14 @@ const getFsTypeWriter = (generatedPath: string) => (typeName: string, properties
``,
].join(EOL)

void fs.writeFileSync(tsPath, contnt, 'utf8')
void writeIfChanged(tsPath, tsContent)

const knownTypes = fs
.readdirSync(generatedPath)
.filter(filename => filename !== 'index.ts')
.map(filename => basename(filename, '.ts'))

void fs.writeFileSync(
void writeIfChanged(
join(generatedPath, `index.ts`),
[
header,
Expand Down
7 changes: 5 additions & 2 deletions packages/typegen/test/generated/main/Bar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@
// tslint:disable
// this file is generated by a tool; don't change it manually.

export interface Bar_QueryTypeMap {
["select * from bar"]: {
export type Bar_AllTypes = [
{
/** pg_type.typname: direction */
dir: 'up' | 'down'
}
]
export interface Bar_QueryTypeMap {
[`select * from bar`]: Bar_AllTypes[0]
}

export type Bar_UnionType = Bar_QueryTypeMap[keyof Bar_QueryTypeMap]
Expand Down
9 changes: 7 additions & 2 deletions packages/typegen/test/generated/main/CountInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,18 @@
// tslint:disable
// this file is generated by a tool; don't change it manually.

export interface CountInfo_QueryTypeMap {
["select count(*) as a_count, a as a_value\n from foo\n group by a"]: {
export type CountInfo_AllTypes = [
{
/** pg_type.typname: int8 */
a_count: number
/** pg_type.typname: text */
a_value: string
}
]
export interface CountInfo_QueryTypeMap {
[`select count(*) as a_count, a as a_value
from foo
group by a`]: CountInfo_AllTypes[0]
}

export type CountInfo_UnionType = CountInfo_QueryTypeMap[keyof CountInfo_QueryTypeMap]
Expand Down
7 changes: 5 additions & 2 deletions packages/typegen/test/generated/main/Foo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
// tslint:disable
// this file is generated by a tool; don't change it manually.

export interface Foo_QueryTypeMap {
["select * from foo"]: {
export type Foo_AllTypes = [
{
/** pg_type.typname: int4 */
id: number
/** pg_type.typname: text */
Expand All @@ -17,6 +17,9 @@ export interface Foo_QueryTypeMap {
/** pg_type.typname: circle */
e: unknown
}
]
export interface Foo_QueryTypeMap {
[`select * from foo`]: Foo_AllTypes[0]
}

export type Foo_UnionType = Foo_QueryTypeMap[keyof Foo_QueryTypeMap]
Expand Down
20 changes: 13 additions & 7 deletions packages/typegen/test/generated/main/FooSubset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,31 @@
// tslint:disable
// this file is generated by a tool; don't change it manually.

export interface FooSubset_QueryTypeMap {
["select a from foo"]: {
export type FooSubset_AllTypes = [
{
/** pg_type.typname: text */
a: string
}
["select a, b from foo"]: {
},
{
/** pg_type.typname: text */
a: string
/** pg_type.typname: bool */
b: boolean
}
["select a, b, c from foo"]: {
},
{
/** pg_type.typname: text */
a: string
/** pg_type.typname: bool */
b: boolean
/** pg_type.typname: _text */
c: string[]
}
]
export interface FooSubset_QueryTypeMap {
[`select a from foo`]: FooSubset_AllTypes[0]
[`select a, b from foo`]: FooSubset_AllTypes[1]
[`select a, b, c from foo`]: FooSubset_AllTypes[2]
[`select a, b, c from foo where 1 = 1`]: FooSubset_AllTypes[2]
}

export type FooSubset_UnionType = FooSubset_QueryTypeMap[keyof FooSubset_QueryTypeMap]
Expand All @@ -30,4 +36,4 @@ export type FooSubset = {
}
export const FooSubset = {} as FooSubset

export const FooSubset_meta_v0 = [{"properties":[{"name":"a","value":"string","description":"pg_type.typname: text"}],"description":"select a from foo"},{"properties":[{"name":"a","value":"string","description":"pg_type.typname: text"},{"name":"b","value":"boolean","description":"pg_type.typname: bool"}],"description":"select a, b from foo"},{"properties":[{"name":"a","value":"string","description":"pg_type.typname: text"},{"name":"b","value":"boolean","description":"pg_type.typname: bool"},{"name":"c","value":"string[]","description":"pg_type.typname: _text"}],"description":"select a, b, c from foo"}]
export const FooSubset_meta_v0 = [{"properties":[{"name":"a","value":"string","description":"pg_type.typname: text"}],"description":"select a from foo"},{"properties":[{"name":"a","value":"string","description":"pg_type.typname: text"},{"name":"b","value":"boolean","description":"pg_type.typname: bool"}],"description":"select a, b from foo"},{"properties":[{"name":"a","value":"string","description":"pg_type.typname: text"},{"name":"b","value":"boolean","description":"pg_type.typname: bool"},{"name":"c","value":"string[]","description":"pg_type.typname: _text"}],"description":"select a, b, c from foo"},{"properties":[{"name":"a","value":"string","description":"pg_type.typname: text"},{"name":"b","value":"boolean","description":"pg_type.typname: bool"},{"name":"c","value":"string[]","description":"pg_type.typname: _text"}],"description":"select a, b, c from foo where 1 = 1"}]
22 changes: 22 additions & 0 deletions packages/typegen/test/generated/main/WithQuotes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/* eslint-disable */
// tslint:disable
// this file is generated by a tool; don't change it manually.

export type WithQuotes_AllTypes = [
{
/** pg_type.typname: text */
a: string
}
]
export interface WithQuotes_QueryTypeMap {
["select a\n from foo\n where a = 'foo'\n or a = '\"'\n or a = '`'"]: WithQuotes_AllTypes[0]
}

export type WithQuotes_UnionType = WithQuotes_QueryTypeMap[keyof WithQuotes_QueryTypeMap]

export type WithQuotes = {
[K in keyof WithQuotes_UnionType]: WithQuotes_UnionType[K]
}
export const WithQuotes = {} as WithQuotes

export const WithQuotes_meta_v0 = [{"properties":[{"name":"a","value":"string","description":"pg_type.typname: text"}],"description":"select a\n from foo\n where a = 'foo'\n or a = '\"'\n or a = '`'"}]
Loading

0 comments on commit 91282e0

Please sign in to comment.