Skip to content

Commit 0f155a0

Browse files
authored
feat: add support for multiple database connections (#146)
* feat: new migrations folder structure for model-defined database connections * fix: do not generate static adapters index * fix: utils require path in migrations template * feat: explicit naming for default databases * fix: access internal sequelize model reference * tests: remove and ignore instance folders * fix: case-insensitive storageType check * feat: updated integration tests toolchain to use yarn workspaces * docs: added new flags to man page * fix: use distro node-jq binary * fix: yarn workspace not cleaning acl_rules * fix: branch checkout should delete generated code * feat: remove yarn and jq dependencies from CLI * feat: add helper for standard logs * fix: remove npm install from restartContainers() * chore: handle branch checkout in workspace * refactor: remove redundant function * fix: increase test 21. Limit check timeout to 10s * chore: remove deprecated sequelize config * fix: remove unused paths from .gitignore * docs: updates and fixes to the project README * fix: change docs from yarn to npm run * fix: -g should not crash if servers are not created * fix: remove ReconstructDb from comment * chore: use logTask for output messages * feat: async check and wait for gql server start * refactor: renamed T1 -> SERVER_CHECK_WAIT_TIME * refactor: do one thing, do it well; better log messages * fix: docker-compose creates empty node_modules * refactor: extract waitForGql() from doTests() * fix: out of place log * refactor: remove redundant checkCode() function * fix: do not attempt to list servers if the workspace has not been set * fix: using -b should checkout, fetch, and reset <branch> * chore: bump 0.2.0 -> 0.3.0
1 parent c4353c3 commit 0f155a0

21 files changed

+711
-527
lines changed

Diff for: README.md

+82-86
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
1-
# Code-generator
1+
<h1 align=center>Code Generator</h1>
22

3-
Command line utility to auto-generate the structure files that [this server](https://github.com/ScienceDb/graphql-server)
4-
will use to perform CRUD operations for each model created.
3+
Command line utility to generate the structure files that Zendro [graphql-server](https://github.com/Zendro-dev/graphql-server) will use to perform CRUD operations for each model created.
54

6-
## Set up:
5+
## Set up
76
Clone the repository and run:
87
```
98
$ npm install -g
109
```
1110
If you only want to install it locally run `npm install` instead
1211

13-
## Usage:
12+
## Usage
1413

1514
To run the unit-test case:
1615
```
@@ -22,82 +21,103 @@ To run the integration-test case
2221
$ npm run test-integration [-- OPTIONS]
2322
```
2423
Note:
25-
Intergation-test case creates a docker-compose ambient with three servers:
24+
Integration-test case creates a docker-compose environment with three servers:
2625

27-
gql_postgres
28-
gql_science_db_graphql_server
29-
gql_ncbi_sim_srv
30-
31-
By default, after the test run, all corresponding Docker images will be completely removed from the docker, this cleanup step can be skiped with __-k__ option.
26+
```
27+
gql_postgres
28+
gql_science_db_graphql_server
29+
gql_ncbi_sim_srv
30+
```
3231

33-
Please run this utility with __-h__ option to see the full documentation in manpage style.
32+
### Examples of use - Test integration
3433

35-
## Examples of use - Test integration:
3634
To see full test-integration info:
37-
```
35+
```bash
3836
$ npm run test-integration -- -h
3937
```
40-
To restart containers:
41-
```
42-
$ npm run test-integration -- -r
38+
39+
To execute a default test run:
40+
```bash
41+
$ npm run test-integration
4342
```
44-
To generate code and start containers:
43+
44+
To test a specific `graphql-server` branch:
45+
```bash
46+
$ npm run test-integration -- b <branch_name>
4547
```
48+
49+
To generate code:
50+
```bash
4651
$ npm run test-integration -- -g
4752
```
48-
To do the tests only and keep the containers running at end:
49-
```
50-
$ npm run test-integration -- -t -k
51-
```
53+
5254
To generate code and do the tests, removing all Docker images at end:
53-
```
54-
$ npm run test-integration -- -T
55-
```
56-
To do a full clean up (removes containers, images and code):
57-
```
55+
```bash
5856
$ npm run test-integration -- -T
5957
```
6058

61-
62-
And to generate the structure files:
59+
To generate code, do the tests, and keep the containers running at end:
60+
```bash
61+
$ npm run test-integration -- -T -k
6362
```
64-
$ code-generator -f <input-json-files> -o <output-directory>
63+
64+
To do the tests only and keep the containers running at end:
65+
```bash
66+
$ npm run test-integration -- -t -k
6567
```
68+
69+
To restart containers:
70+
```bash
71+
$ npm run test-integration -- -r
6672
```
67-
INPUT:
68-
<input-json-files> - directory where json models are stored
69-
<output-directory> - directory where the generated code will be written
73+
74+
To clean generated code, remove containers and volumes:
75+
```bash
76+
$ npm run test-integration -- -C
7077
```
71-
This command will create(if doesn't exist) four folders containing the generated files for each model in the ```input-json-files```:
7278

73-
* models ----> sequelize model
74-
* schemas ----> graphQL schema
75-
* resolvers ----> basic CRUD resolvers
76-
* migrations ----> create and delete table migration file
79+
To do a full clean up (removes containers, images and code):
80+
```bash
81+
$ npm run test-integration -- -c
82+
```
7783

84+
### Examples of use - Code Generator
7885

79-
## Examples of use - Code generator:
8086
In the same directory of this repository run:
8187

82-
```
88+
```bash
89+
# -f <input-json-files> directory where json models are stored
90+
# -o <output-directory> directory where the generated code will be written
8391
$ code-generator -f ./example_json_files -o /your_path_directory
8492
```
85-
If you want to complete the example with the [server](https://github.com/ScienceDb/graphql-server)
86-
make ```/your_path_directory``` the same directory where the server repository is stored.
8793

88-
NOTE: For displaying the explanation about usage we can run the help flag:
89-
```
94+
For help using this command:
95+
96+
```bash
9097
$ code-generator -h
98+
99+
# Code generator for the GraphQL server
100+
#
101+
# Options:
102+
#
103+
# -f, --jsonFiles <filesFolder> Folder containing one json file for each model
104+
# -o, --outputDirectory <directory> Directory where generated code will be written
105+
# -h, --help output usage information
91106
```
92-
```
93-
Code generator for GraphQL server
94107

95-
Options:
108+
This command will create four sub-folders within the `output-directory` folder, containing the generated files for each model:
109+
96110

97-
-f, --jsonFiles <filesFolder> Folder containing one json file for each model
98-
-o, --outputDirectory <directory> Directory where generated code will be written
99-
-h, --help output usage information
100111
```
112+
models/ -> sequelize model
113+
schemas/ -> graphQL schema
114+
resolvers/ -> basic CRUD resolvers
115+
migrations/ -> create and delete table migration file
116+
```
117+
118+
To use the code generator with the [graphql-server](https://github.com/Zendro-dev/graphql-server),
119+
use its path in the `output-directory`.
120+
101121

102122
## JSON files Spec
103123

@@ -107,15 +127,15 @@ For each model we need to specify the following fields in the json file:
107127

108128
Name | Type | Description
109129
------- | ------- | --------------
110-
*model* | String | Name of the model (it is recommended uppercase for the initial character).
111-
*storageType* | String | Type of storage where the model is stored. So far can be one of __sql__ or __Webservice__ or __zendro\_server__
112-
*url* | String | This field is only mandatory for __zendro\_server__ stored models. Indicates the url where the zendro server storing the model is runnning.
113-
*attributes* | Object | The key of each entry is the name of the attribute and theres two options for the value . Either can be a string indicating the type of the attribute or an object where the user can indicates the type of the attribute(in the _type_ field) but also can indicates an attribute's description (in the _description_ field). See the [table](#types-spec) below for allowed types. Example of option one: ```{ "attribute1" : "String", "attribute2: "Int" }``` Example of option two: ``` { "attribute1" : {"type" :"String", "description": "Some description"}, "attribute2: "Int ```
130+
*model* | String | Name of the model (it is recommended to Capitalize the name).
131+
*storageType* | String | Type of storage where the model is stored. Currently supported types are __sql__, __Webservice__, and __zendro\_server__
132+
*url* | String | This field is only mandatory for __zendro\_server__ stored models. Indicates the URL of the Zendro server storing the model.
133+
*attributes* | Object | The key of each entry is the name of the attribute. There are two options for the value: a string indicating the type of the attribute, or an object with two properties: _type_ (the type of the attribute) and _description_ (attribute description). See [types-spec](#types-spec) table below for allowed types. Example of option one: ```{ "attribute1" : "String", "attribute2: "Int" }``` Example of option two: ``` { "attribute1" : {"type" :"String", "description": "Some description"}, "attribute2: "Int ```
114134
*associations* | Object | The key of each entry is the name of the association and the value should be an object describing the associations. See [Associations Spec](associations-spec) section below for the specifications of the associations.
115135

116136
EXAMPLES OF VALID JSON FILES
117137

118-
```
138+
```jsonc
119139
//Dog.json
120140
{
121141
"model" : "Dog",
@@ -139,7 +159,7 @@ EXAMPLES OF VALID JSON FILES
139159

140160
```
141161

142-
```
162+
```jsonc
143163
//Publisher.json
144164
{
145165
"model" : "Publisher",
@@ -175,7 +195,7 @@ Date |
175195
Time |
176196
DateTime |
177197

178-
For more info about `Date`, `Time`, and `DateTime` types, please see this [info](https://github.com/excitement-engineer/graphql-iso-date/blob/HEAD/rfc3339.txt).
198+
For more info about `Date`, `Time`, and `DateTime` types, please see the [graphql-iso-date/rfc3339.txt](https://github.com/excitement-engineer/graphql-iso-date/blob/HEAD/rfc3339.txt).
179199

180200
Example:
181201
* Date: A date string, such as `2007-12-03`.
@@ -211,7 +231,7 @@ name | Type | Description
211231
## NOTE:
212232
Be aware that in the case of this type of association the user is required to describe the cross table used in the field _keysIn_ as a model in its own. For example, if we have a model `User` and a model `Role` and they are associated in a _manytomany_ way, then we also need to describe the `role_to_user` model:
213233

214-
```
234+
```jsonc
215235
//User model
216236
{
217237
"model" : "User",
@@ -235,7 +255,7 @@ Be aware that in the case of this type of association the user is required to de
235255
}
236256
```
237257

238-
```
258+
```jsonc
239259
//Role model
240260
{
241261
"model" : "Role",
@@ -258,7 +278,7 @@ Be aware that in the case of this type of association the user is required to de
258278
}
259279
```
260280

261-
```
281+
```jsonc
262282
//role_to_user model
263283
{
264284
"model" : "role_to_user",
@@ -275,7 +295,7 @@ Be aware that in the case of this type of association the user is required to de
275295
It's important to notice that when a model involves a foreign key for the association, this key should be explicitly written into the attributes field of the given local model.
276296

277297
Example:
278-
```
298+
```jsonc
279299
{
280300
"model" : "book",
281301
"storageType" : "sql",
@@ -301,7 +321,7 @@ THE SAME DATA MODELS DESCRIPTION(.json files) WILL BE USEFUL FOR GENERATING BOTH
301321
302322
Fields *`label`* and *`sublabel`* in the specification are only needed by the GUI generator, but backend generator will only read required information, therefore extra fields such as *`label`* and *`sublabel`* will be ignored by the backend generator.
303323
Example:
304-
```
324+
```jsonc
305325
//book.json
306326
{
307327
"model" : "Book",
@@ -329,27 +349,3 @@ Example:
329349
## Testing
330350
331351
For relevant files see `package.json` (section scripts), directories `.test` and `docker`. Test framework is `mocha` and `chai`.
332-
333-
### Unit tests
334-
335-
Run all existing unit tests with
336-
```
337-
npm run test-unit
338-
```
339-
340-
### Integration tests
341-
342-
#### Requirements
343-
344-
You need to be on a \*nix operating system, and have bash and Docker installed and running.
345-
346-
Integration tests are carried out using Docker to setup a GraphQL web server and generate code for example data models. The last step of the setup is to create databases and migrate schemas. After that the server is started using `localhost:3000`, which can than be accessed using HTTP. Solely via such HTTP connections the generated API (GraphQL web server) is tested, just as a user might be doing with e.g. `curl`.
347-
348-
All related Docker files are stored in `./docker`; especially `docker-compose-test.yml`.
349-
350-
The test pipeline is defined and executed in `./test/integration-test.bash` for reasons of simplicity. The actual integration tests are written using `mocha` and can be found in `./test/mocha_integration_test.js`, which is invoked by the above bash script.
351-
352-
To ecexute the integration tests run
353-
```
354-
npm run test-integration
355-
```

Diff for: docker/Dockerfile.graphql_server

+3-4
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,9 @@ FROM node:14.3.0-stretch-slim
33
# Create app directory
44
WORKDIR /usr/src/app
55

6+
ENV JQ_PATH=/usr/bin/jq
7+
68
# Clone the skeleton project and install dependencies
7-
RUN apt-get update && apt-get install -y git procps autoconf libtool make &&\
8-
git clone --branch master https://github.com/ScienceDb/graphql-server.git . && \
9-
chmod u+x ./migrateDbAndStartServer.sh && \
10-
npm install --save
9+
RUN apt-get update && apt-get install -y git procps autoconf libtool make jq
1110

1211
EXPOSE 3000

Diff for: docker/data_models_storage_config1.json

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"default-sql": {
3+
"storageType": "sql",
4+
"username": "sciencedb",
5+
"password": "sciencedb",
6+
"database": "sciencedb_development",
7+
"host": "gql_postgres1",
8+
"dialect": "postgres",
9+
"operatorsAliases": false
10+
}
11+
}

Diff for: docker/data_models_storage_config2.json

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"default-sql": {
3+
"storageType": "sql",
4+
"username": "sciencedb",
5+
"password": "sciencedb",
6+
"database": "sciencedb_development",
7+
"host": "gql_postgres2",
8+
"dialect": "postgres",
9+
"operatorsAliases": false
10+
}
11+
}

Diff for: docker/docker-compose-test.yml

+6-27
Original file line numberDiff line numberDiff line change
@@ -17,27 +17,16 @@ services:
1717
build:
1818
context: .
1919
dockerfile: Dockerfile.graphql_server
20-
2120
volumes:
22-
- ./integration_test_run/instance1/models/adapters:/usr/src/app/models/adapters
23-
- ./integration_test_run/instance1/schemas:/usr/src/app/schemas
24-
- ./integration_test_run/instance1/resolvers:/usr/src/app/resolvers
25-
- ./integration_test_run/instance1/models/sql:/usr/src/app/models/sql
26-
- ./integration_test_run/instance1/models/distributed:/usr/src/app/models/distributed
27-
- ./integration_test_run/instance1/models/zendro-server:/usr/src/app/models/zendro-server
28-
- ./integration_test_run/instance1/models/generic:/usr/src/app/models/generic
29-
- ./integration_test_run/instance1/migrations:/usr/src/app/migrations
30-
- ./integration_test_run/instance1/validations:/usr/src/app/validations
31-
- ./integration_test_run/instance1/patches:/usr/src/app/patches
32-
- ./sequelize_config_instance1.json:/usr/src/app/config/config.json
33-
21+
- ./integration_test_run/servers/instance1:/usr/src/app
22+
- ./data_models_storage_config1.json:/usr/src/app/config/data_models_storage_config.json
3423
ports:
3524
- "3000:3000"
36-
3725
environment:
3826
PORT: 3000
3927
REQUIRE_SIGN_IN: "false"
4028
LIMIT_RECORDS: 25
29+
JQ_PATH: /usr/bin/jq
4130
# Await POSTGRES role and DB creation, migrate schema, then start web server:
4231
networks:
4332
- integrationtest
@@ -47,7 +36,6 @@ services:
4736
- /bin/sh
4837
- -c
4938
- |
50-
npm install
5139
./migrateDbAndStartServer.sh
5240
5341
gql_ncbi_sim_srv1:
@@ -96,17 +84,8 @@ services:
9684
dockerfile: Dockerfile.graphql_server
9785

9886
volumes:
99-
- ./integration_test_run/instance2/models/adapters:/usr/src/app/models/adapters
100-
- ./integration_test_run/instance2/schemas:/usr/src/app/schemas
101-
- ./integration_test_run/instance2/resolvers:/usr/src/app/resolvers
102-
- ./integration_test_run/instance2/models/sql:/usr/src/app/models/sql
103-
- ./integration_test_run/instance2/models/distributed:/usr/src/app/models/distributed
104-
- ./integration_test_run/instance2/models/zendro-server:/usr/src/app/models/zendro-server
105-
- ./integration_test_run/instance2/models/generic:/usr/src/app/models/generic
106-
- ./integration_test_run/instance2/migrations:/usr/src/app/migrations
107-
- ./integration_test_run/instance2/validations:/usr/src/app/validations
108-
- ./integration_test_run/instance2/patches:/usr/src/app/patches
109-
- ./sequelize_config_instance2.json:/usr/src/app/config/config.json
87+
- ./integration_test_run/servers/instance2:/usr/src/app
88+
- ./data_models_storage_config2.json:/usr/src/app/config/data_models_storage_config.json
11089

11190
ports:
11291
- "3030:3030"
@@ -115,6 +94,7 @@ services:
11594
PORT: 3030
11695
REQUIRE_SIGN_IN: "false"
11796
LIMIT_RECORDS: 25
97+
JQ_PATH: /usr/bin/jq
11898
# Await POSTGRES role and DB creation, migrate schema, then start web server:
11999
networks:
120100
- integrationtest
@@ -123,7 +103,6 @@ services:
123103
- /bin/sh
124104
- -c
125105
- |
126-
npm install
127106
./migrateDbAndStartServer.sh
128107
129108
networks:

0 commit comments

Comments
 (0)