Skip to content

Commit d2b17eb

Browse files
committed
Initial commit
0 parents  commit d2b17eb

27 files changed

+1743
-0
lines changed

.eslintrc.js

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
module.exports = {
2+
parser: '@typescript-eslint/parser', // Specifies the ESLint parser
3+
extends: [
4+
'plugin:@typescript-eslint/recommended',
5+
'prettier/@typescript-eslint',
6+
],
7+
parserOptions: {
8+
ecmaFeatures: {
9+
jsx: true,
10+
},
11+
},
12+
rules: {
13+
'@typescript-eslint/explicit-function-return-type': 'off',
14+
},
15+
};

.gitignore

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
.DS_store
2+
3+
generated
4+
5+
# Binaries for programs and plugins
6+
*.exe
7+
*.exe~
8+
*.dll
9+
*.so
10+
*.dylib
11+
12+
# Test binary, built with `go test -c`
13+
*.test
14+
15+
# Output of the go coverage tool, specifically when used with LiteIDE
16+
*.out
17+
18+
# Logs
19+
logs
20+
*.log
21+
npm-debug.log*
22+
yarn-debug.log*
23+
yarn-error.log*
24+
lerna-debug.log*
25+
26+
# Dependency directories
27+
node_modules/
28+
29+
# Yarn Integrity file
30+
.yarn-integrity
31+
32+
# dotenv environment variables file
33+
.env
34+
.env.test

LICENSE

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
The MIT License
3+
4+
Copyright (c) 2010-2019 Google, Inc. http://angularjs.org
5+
6+
Permission is hereby granted, free of charge, to any person obtaining a copy
7+
of this software and associated documentation files (the "Software"), to deal
8+
in the Software without restriction, including without limitation the rights
9+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
copies of the Software, and to permit persons to whom the Software is
11+
furnished to do so, subject to the following conditions:
12+
13+
The above copyright notice and this permission notice shall be included in
14+
all copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
THE SOFTWARE.

README.md

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Read Me
2+
3+
Experiment to investigate generating typescript objection and join-monster (graphql) models
4+
5+
## Requirements
6+
7+
- go 1.12
8+
- node >= 10
9+
- docker
10+
- docker-compose
11+
12+
## Set-up
13+
14+
docker-compose up # This will run the gnorm.sql script
15+
yarn install # Prettier can be run on the files after creation
16+
17+
## Assumptions
18+
19+
Database tables and columes are in lower snake case
20+
21+
# Objection
22+
23+
Ideally this should be run as db migrations are added and ammend exiting models with overwriting hand written code
24+
25+
# Join-monster
26+
27+
This is meant as a one-off to get you going quickly. I expect graphql models to drift the db models are the project progresses
28+
29+
# Generate
30+
31+
./scripts/run.sh

docker-compose.yaml

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
version: "3"
2+
services:
3+
postgres:
4+
image: postgres:11.2
5+
environment:
6+
POSTGRES_USER: postgres
7+
POSTGRES_PASSWORD: password
8+
POSTGRES_INITDB_ARGS: --data-checksums
9+
ports:
10+
- 5432:5432
11+
volumes:
12+
- postgres-data:/var/lib/postgresql/data
13+
- ./gnorm.sql:/docker-entrypoint-initdb.d/gnorm.sql
14+
15+
volumes:
16+
postgres-data:

gnorm.sql

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
--
2+
-- PostgreSQL database dump
3+
--
4+
5+
CREATE EXTENSION IF NOT EXISTS "uuid-ossp" WITH SCHEMA public;
6+
7+
CREATE TYPE public.book_type AS ENUM (
8+
'FICTION',
9+
'NONFICTION'
10+
);
11+
12+
CREATE TABLE public.authors (
13+
id uuid DEFAULT public.uuid_generate_v4() NOT NULL,
14+
name text NOT NULL
15+
);
16+
17+
CREATE TABLE public.books (
18+
id integer NOT NULL,
19+
author_id uuid NOT NULL,
20+
isbn character(32) NOT NULL,
21+
booktype public.book_type NOT NULL,
22+
title text NOT NULL,
23+
pages integer NOT NULL,
24+
summary text,
25+
available timestamp with time zone DEFAULT '2017-09-04 21:43:39.197538-04'::timestamp with time zone NOT NULL
26+
);
27+
28+
CREATE SEQUENCE public.books_id_seq
29+
START WITH 1
30+
INCREMENT BY 1
31+
NO MINVALUE
32+
NO MAXVALUE
33+
CACHE 1;
34+
35+
36+
ALTER TABLE ONLY public.books ALTER COLUMN id SET DEFAULT nextval('public.books_id_seq'::regclass);
37+
38+
39+
ALTER TABLE ONLY public.authors
40+
ADD CONSTRAINT authors_pkey PRIMARY KEY (id);
41+
42+
ALTER TABLE ONLY public.books
43+
ADD CONSTRAINT books_isbn_key UNIQUE (isbn);
44+
45+
ALTER TABLE ONLY public.books
46+
ADD CONSTRAINT books_pkey PRIMARY KEY (id);
47+
48+
CREATE INDEX authors_name_idx ON public.authors USING btree (name);
49+
50+
CREATE INDEX books_title_idx ON public.books USING btree (author_id, title);
51+
52+
ALTER TABLE ONLY public.books
53+
ADD CONSTRAINT books_author_id_fkey FOREIGN KEY (author_id) REFERENCES public.authors(id);

go.mod

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
module gitlab.com/stackworx.io/metcon
2+
3+
go 1.12
4+
5+
require (
6+
github.com/BurntSushi/toml v0.3.1 // indirect
7+
github.com/codemodus/kace v0.5.1 // indirect
8+
github.com/gnormal/postgres-go v0.0.0-20180928203058-37c7f33f5cab
9+
github.com/go-sql-driver/mysql v1.4.1 // indirect
10+
github.com/gofrs/uuid v3.2.0+incompatible
11+
github.com/lib/pq v1.0.0
12+
github.com/mattn/go-runewidth v0.0.4 // indirect
13+
github.com/olekukonko/tablewriter v0.0.1 // indirect
14+
github.com/pkg/errors v0.8.1
15+
github.com/spf13/cobra v0.0.3 // indirect
16+
github.com/spf13/pflag v1.0.3 // indirect
17+
gnorm.org/gnorm v1.0.0
18+
gopkg.in/yaml.v2 v2.2.2 // indirect
19+
)

go.sum

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
2+
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
3+
github.com/codemodus/kace v0.5.1 h1:4OCsBlE2c/rSJo375ggfnucv9eRzge/U5LrrOZd47HA=
4+
github.com/codemodus/kace v0.5.1/go.mod h1:coddaHoX1ku1YFSe4Ip0mL9kQjJvKkzb9CfIdG1YR04=
5+
github.com/gnormal/postgres-go v0.0.0-20180928203058-37c7f33f5cab h1:oeKV3W5HFSDjd14gYXWc9uMX3tHskCWwsGMeJ7NOFGs=
6+
github.com/gnormal/postgres-go v0.0.0-20180928203058-37c7f33f5cab/go.mod h1:WiHF2Kr7xkJ9/WVC3R3wVs2+JipT0yVtki+KOXh6COs=
7+
github.com/go-sql-driver/mysql v1.4.1 h1:g24URVg0OFbNUTx9qqY1IRZ9D9z3iPyi5zKhQZpNwpA=
8+
github.com/go-sql-driver/mysql v1.4.1/go.mod h1:zAC/RDZ24gD3HViQzih4MyKcchzm+sOG5ZlKdlhCg5w=
9+
github.com/gofrs/uuid v3.2.0+incompatible h1:y12jRkkFxsd7GpqdSZ+/KCs/fJbqpEXSGd4+jfEaewE=
10+
github.com/gofrs/uuid v3.2.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM=
11+
github.com/lib/pq v1.0.0 h1:X5PMW56eZitiTeO7tKzZxFCSpbFZJtkMMooicw2us9A=
12+
github.com/lib/pq v1.0.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
13+
github.com/mattn/go-runewidth v0.0.4 h1:2BvfKmzob6Bmd4YsL0zygOqfdFnK7GR4QL06Do4/p7Y=
14+
github.com/mattn/go-runewidth v0.0.4/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU=
15+
github.com/olekukonko/tablewriter v0.0.1 h1:b3iUnf1v+ppJiOfNX4yxxqfWKMQPZR5yoh8urCTFX88=
16+
github.com/olekukonko/tablewriter v0.0.1/go.mod h1:vsDQFd/mU46D+Z4whnwzcISnGGzXWMclvtLoiIKAKIo=
17+
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
18+
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
19+
github.com/spf13/cobra v0.0.3 h1:ZlrZ4XsMRm04Fr5pSFxBgfND2EBVa1nLpiy1stUsX/8=
20+
github.com/spf13/cobra v0.0.3/go.mod h1:1l0Ry5zgKvJasoi3XT1TypsSe7PqH0Sj9dhYf7v3XqQ=
21+
github.com/spf13/pflag v1.0.3 h1:zPAT6CGy6wXeQ7NtTnaTerfKOsV6V6F8agHXFiazDkg=
22+
github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4=
23+
gnorm.org/gnorm v1.0.0 h1:mnI5m9qNgR8khQ8+4CkB3SJsfvUlxHmt3HLDLRfo3ck=
24+
gnorm.org/gnorm v1.0.0/go.mod h1:JlN3YrPLm6ZD40iWFgfn9/kLzf9p8Z2GkQYVov973kY=
25+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
26+
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
27+
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=

join-monster/gnorm.toml

+98
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
# ConnStr is the connection string for the database. Any environment variables
2+
# in this string will be expanded, so for example dbname=$MY_DDB will do the
3+
# right thing.
4+
# MySQL example:
5+
# ConnStr = "root:admin@tcp/"
6+
# Postgres example:
7+
ConnStr = "dbname=postgres host=127.0.0.1 sslmode=disable user=postgres password=password"
8+
9+
DBType = "postgres"
10+
11+
Schemas = ["public"]
12+
13+
# TODO: add linting
14+
PostRun = ["../scripts/post-process.sh", "join-monster", "$GNORMFILE"]
15+
16+
NameConversion = "{{camel .}}"
17+
18+
# This will output the files in a subdirectory called "gnorm". You can change
19+
# this, but if you do, change the RootPkg definition below to match what the
20+
# package name should be.
21+
OutputDir = "./generated"
22+
23+
ExcludeTables = ["knex_migrations", "knex_migrations_lock"]
24+
25+
# PluginDirs = ["./templates/plugin"]
26+
27+
[Params]
28+
# RootPkg is the package declaration for the output dir. It should match the
29+
# directory name above. You may change this as appropriate.
30+
# RootPkg = "generated"
31+
32+
# RootImport = "gitlab.com/stackworx.io/metcon/generated"
33+
34+
[SchemaPaths]
35+
# "fields.go" = "templates/fields.gotmpl"
36+
# "db.go" = "templates/db.gotmpl"
37+
"{{toLower .Schema}}/index.ts" = "templates/table_index.gotmpl"
38+
"{{toLower .Schema}}/enums/index.ts" = "templates/enum_index.gotmpl"
39+
40+
[TablePaths]
41+
# "{{toLower .Schema}}/{{toLower .Table}}/{{toLower .Table}}.ts" = "templates/table.gotmpl"
42+
"{{toLower .Schema}}/{{pascal .Table}}.graphql.ts" = "templates/table.gotmpl"
43+
44+
[EnumPaths]
45+
# "{{toLower .Schema}}/enum/{{toLower .Enum}}.ts" = "templates/enum.gotmpl"
46+
"{{toLower .Schema}}/enums/{{pascal .Enum}}.ts" = "templates/enum.gotmpl"
47+
48+
[TypeMap]
49+
"timestamp with time zone" = "GraphQLNonNull(GraphQLDateTime)"
50+
"timestamptz" = "GraphQLNonNull(GraphQLDateTime"
51+
"varchar" = "GraphQLNonNull(GraphQLString"
52+
"text" = "GraphQLNonNull(GraphQLString)"
53+
"boolean" = "GraphQLNonNull(boolean)"
54+
"uuid" = "GraphQLNonNull(GraphQLString)"
55+
"character varying" = "GraphQLNonNull(GraphQLString)"
56+
"character" = "GraphQLNonNull(GraphQLString)"
57+
"smallint" = "GraphQLNonNull(GraphQLInt)"
58+
"integer" = "GraphQLNonNull(GraphQLInt)"
59+
"int4" = "GraphQLNonNull(GraphQLInt)"
60+
"int8" = "GraphQLNonNull(GraphQLInt)"
61+
"numeric" = "GraphQLNonNull(GraphQLFloat)"
62+
"bigint" = "GraphQLNonNull(GraphQLInt)"
63+
# TODO
64+
"hstore" = "GraphQLNonNull(GraphQLString)"
65+
# TODO
66+
"jsonb" = "GraphQLNonNull(GraphQLString)"
67+
# TODO
68+
"bytea" = "GraphQLNonNull(GraphQLString)"
69+
# TODO: real column
70+
"tstzrange" = "GraphQLNonNull(GraphQLString)"
71+
# Replace with your own enums
72+
"book_type" = "GraphQLNonNull(enums.BookType)"
73+
74+
[NullableTypeMap]
75+
"timestamp with time zone" = "GraphQLDateTime"
76+
"timestamptz" = "GraphQLDateTime"
77+
"varchar" = "GraphQLString"
78+
"text" = "GraphQLString"
79+
"boolean" = "boolean"
80+
"uuid" = "GraphQLString"
81+
"character varying" = "GraphQLString"
82+
"character" = "GraphQLString"
83+
"smallint" = "GraphQLInt"
84+
"integer" = "GraphQLInt"
85+
"int4" = "GraphQLInt"
86+
"int8" = "GraphQLInt"
87+
"numeric" = "GraphQLFloat"
88+
"bigint" = "GraphQLInt"
89+
# TODO
90+
"hstore" = "GraphQLString"
91+
# TODO
92+
"jsonb" = "GraphQLString"
93+
# TODO
94+
"bytea" = "GraphQLString"
95+
# TODO: real column
96+
"tstzrange" = "GraphQLString"
97+
# Replace with your own enums
98+
"book_type" = "enums.BookType"

join-monster/templates/enum.gotmpl

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// Code generated by gnorm, DO NOT EDIT!
2+
{{- $type := .Enum.Name -}}
3+
// {{ $type }} is the '{{ .Enum.DBName }}' enum type from schema '{{ .Enum.Schema.Name }}'.
4+
5+
export enum {{ pascal .Enum.DBName }} {
6+
{{- range .Enum.Values }}
7+
{{ pascal .Name }} = '{{ .DBName }}',
8+
{{- end }}
9+
}
+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// TODO: remove duplicates
2+
{{range .Schema.Enums.Names -}}
3+
export * from './{{pascal .}}';
4+
{{end}}

0 commit comments

Comments
 (0)