dbmodel 0.1.1 → 5.2.2-alpha.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE CHANGED
@@ -1,21 +1,21 @@
1
- MIT License
2
-
3
- Copyright (c) 2019 dbshell
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- SOFTWARE.
1
+ MIT License
2
+
3
+ Copyright (c) 2019 dbshell
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,116 +1,88 @@
1
- # dbmodel
2
- Deploy, load or build script from model of SQL database. Can be used as command-line tool or as javascript functions
3
-
4
- Model is stored as a collection of files:
5
- * tables - stored as YAML files
6
- * columns
7
- * indexes
8
- * primary keys
9
- * foreign keys
10
- * views - stored as SQL file with extension **.view.sql**
11
- * stored procedures - stored as SQL file with extension **.proc.sql**
12
- * functions - stored as SQL file with extension **.func.sql**
13
-
14
- Currently only supported client is **mssql** .
15
-
16
- ## Installation - as global tool
17
-
18
- npm install --global dbmodel
19
-
20
- ## Installation - as regular package
21
-
22
- npm install --save dbmodel
23
-
24
- ## Available commands
25
- * **load** - loads structure of database, saves it to local directory (called *project*). Also can download data of enlisted tables (use --load-data-condition options)
26
- * **deploy** - deploys structure from local directory (*project*) to database. *Deploy does not perform any actions leading to data loss, these changes must be made manually.*
27
- * creates not existing tables
28
- * creates not existing columns of existing tables
29
- * checks column NULL/NOT NULL flag, alters colums
30
- * checks tables, which are in database, but not in project, list of these tables are reported
31
- * checks columns, which are in database, but not in project, list of these columns are reported
32
- * checks indexes and its definitions, indexes are created or recreated, if neccessary (*but not deleted*)
33
- * checks and creates foreign keys
34
- * checks, creates new or changes existing views, stored procedures and functions
35
- * updates and creates static data (included in table yaml files)
36
- * **build** - builds script from project folder. This operation is complete offline, no database connection is needed. Built script makes subset of deploy command. It can be executed on empty database, but also it can convert existing database to current structure (but only using operations below).
37
- * creates not existing tables
38
- * creates not existing columns of existing tables
39
- * creates not existing indexes (checked only by name)
40
- * creates not existing foreign keys
41
- * creates new or changes existing views, stored procedures and functions
42
- * updates and creates static data (included in table yaml files)
43
-
44
- ## Command line interface
45
-
46
- ```sh
47
- # load from existing database
48
- dbmodel load -s localhost -u USERNAME -p PASSWORD -d DATABASE -c mssql OUTPUT_FOLDER
49
-
50
- # deploy project to database
51
- dbmodel deploy -s localhost -u USERNAME -p PASSWORD -d DATABASE -c mssql PROJECT_FOLDER
52
-
53
- # build SQL script from project
54
- dbmodel build -c mssql PROJECT_FOLDER OUTPUT_FILE.sql
55
- ```
56
-
57
- ## JavaScript interface
58
-
59
- ```javascript
60
- const dbmodel = require('dbmodel');
61
-
62
- await dbmodel.deploy({
63
- client: 'mssql',
64
- connection: {
65
- server: '127.0.0.1',
66
- user: process.env.DB_USER,
67
- password: process.env.DB_PASSWORD,
68
- database: 'Chinook_Model',
69
- },
70
- hooks: [dbmodel.hooks.autoIndexForeignKeys], // this hook adds indexes to all foreign keys
71
- projectDir: 'model',
72
- })
73
- ```
74
-
75
- list of dbmodel exported functions:
76
- * build - builds SQL script
77
- * deploy - deploys model to database
78
- * dump - dumps loaded model into directory
79
- * load - loads model from database
80
- * read - reads model from directory
81
- * connect - creates database connection defined in options
82
-
83
- ## Table yaml file documentation
84
-
85
- ```yaml
86
- name: Album # table name
87
- columns:
88
- - name: AlbumId # column name
89
- type: int # data type. is used directly in target SQL engine
90
- autoIncrement: true # column is autoincrement
91
- notNull: true # column is not nullable (default: is nullable)
92
- - name: Title
93
- type: nvarchar
94
- length: 160 # maximum character length
95
- notNull: true
96
- - name: ArtistId
97
- type: int
98
- references: Artist # name of table. Is used for creating foreign key
99
- - name: isDeleted
100
- type: bit
101
- notNull: true
102
- default: 0 # default value
103
- primaryKey:
104
- - AlbumId # list of primary key column names
105
- indexes:
106
- - name: UQ_AlbumTitleArtistId # index name
107
- unique: true # whether index is unique. default=false
108
- columns: # list of index columns
109
- - Title
110
- - ArtistId
111
- filter: isDeleted=0 # if defined, filtered index (with WHERE condition) is created
112
- continueOnError: true # if true and there was error in creating this index, continue (suitable for lately added unique indexes)
113
- data: # static data (only for list tables)
114
- - AlbumId: -1 # values for all columns, which should be filled
115
- Title: Predefined static album
116
- ```
1
+ # dbmodel
2
+ Deploy, load or build script from model of SQL database. Can be used as command-line tool or as javascript functions
3
+
4
+ Model is stored as a collection of files:
5
+ * tables - stored as YAML files
6
+ * columns
7
+ * indexes
8
+ * primary keys
9
+ * foreign keys
10
+ * views - stored as SQL file with extension **.view.sql**
11
+ * stored procedures - stored as SQL file with extension **.proc.sql**
12
+ * functions - stored as SQL file with extension **.func.sql**
13
+
14
+ ## Installation - as global tool
15
+
16
+ npm install --global dbmodel
17
+
18
+ ## Installation - as regular package
19
+
20
+ npm install --save dbmodel
21
+
22
+ ## Available commands
23
+ * **load** - loads structure of database, saves it to local directory (called *project*). Also can download data of enlisted tables (use --load-data-condition options)
24
+ * **deploy** - deploys structure from local directory (*project*) to database. *Deploy does not perform any actions leading to data loss, these changes must be made manually.*
25
+ * creates not existing tables
26
+ * creates not existing columns of existing tables
27
+ * checks column NULL/NOT NULL flag, alters colums
28
+ * checks tables, which are in database, but not in project, list of these tables are reported
29
+ * checks columns, which are in database, but not in project, list of these columns are reported
30
+ * checks indexes and its definitions, indexes are created or recreated, if neccessary (*but not deleted*)
31
+ * checks and creates foreign keys
32
+ * checks, creates new or changes existing views, stored procedures and functions
33
+ * updates and creates static data (included in table yaml files)
34
+ * **build** - builds script from project folder. This operation is complete offline, no database connection is needed. Built script makes subset of deploy command. It can be executed on empty database, but also it can convert existing database to current structure (but only using operations below).
35
+ * creates not existing tables
36
+ * creates not existing columns of existing tables
37
+ * creates not existing indexes (checked only by name)
38
+ * creates not existing foreign keys
39
+ * creates new or changes existing views, stored procedures and functions
40
+ * updates and creates static data (included in table yaml files)
41
+
42
+ ## Command line interface
43
+
44
+ ```sh
45
+ # load from existing database
46
+ dbmodel load -s localhost -u USERNAME -p PASSWORD -d DATABASE -e mssql@dbgate-plugin-mssql OUTPUT_FOLDER
47
+
48
+ # deploy project to database
49
+ dbmodel deploy -s localhost -u USERNAME -p PASSWORD -d DATABASE -e mssql@dbgate-plugin-mssql PROJECT_FOLDER
50
+
51
+ # build SQL script from project
52
+ dbmodel build -e mssql@dbgate-plugin-mssql PROJECT_FOLDER OUTPUT_FILE.sql
53
+ ```
54
+
55
+ ## Table yaml file documentation
56
+
57
+ ```yaml
58
+ name: Album # table name
59
+ columns:
60
+ - name: AlbumId # column name
61
+ type: int # data type. is used directly in target SQL engine
62
+ autoIncrement: true # column is autoincrement
63
+ notNull: true # column is not nullable (default: is nullable)
64
+ - name: Title
65
+ type: nvarchar
66
+ length: 160 # maximum character length
67
+ notNull: true
68
+ - name: ArtistId
69
+ type: int
70
+ references: Artist # name of table. Is used for creating foreign key
71
+ - name: isDeleted
72
+ type: bit
73
+ notNull: true
74
+ default: 0 # default value
75
+ primaryKey:
76
+ - AlbumId # list of primary key column names
77
+ indexes:
78
+ - name: UQ_AlbumTitleArtistId # index name
79
+ unique: true # whether index is unique. default=false
80
+ columns: # list of index columns
81
+ - Title
82
+ - ArtistId
83
+ filter: isDeleted=0 # if defined, filtered index (with WHERE condition) is created
84
+ continueOnError: true # if true and there was error in creating this index, continue (suitable for lately added unique indexes)
85
+ data: # static data (only for list tables)
86
+ - AlbumId: -1 # values for all columns, which should be filled
87
+ Title: Predefined static album
88
+ ```
package/bin/dbmodel.js CHANGED
@@ -1,81 +1,118 @@
1
- #!/usr/bin/env node
2
-
3
- const program = require('commander');
4
- const dbmodel = require('../lib');
5
-
6
- program
7
- .option('-s, --server <server>', 'server host')
8
- .option('-u, --user <user>', 'user name')
9
- .option('-p, --password <password>', 'password')
10
- .option('-d, --database <database>', 'database name')
11
- .option('--auto-index-foreign-keys', 'automatically adds indexes to all foreign keys')
12
- .option(
13
- '--load-data-condition <condition>',
14
- 'regex, which table data will be loaded and stored in model (in load command)'
15
- )
16
- .requiredOption('-c, --client <client>', 'client name, must be mssql');
17
-
18
- program
19
- .command('deploy <projectDir>')
20
- .description('Deploys model to database')
21
- .action(projectDir => {
22
- const { client, server, user, password, database } = program;
23
- const hooks = [];
24
- if (program.autoIndexForeignKeys) hooks.push(dbmodel.hooks.autoIndexForeignKeys);
25
- dbmodel.runAndExit(
26
- dbmodel.deploy({
27
- client,
28
- connection: {
29
- server,
30
- user,
31
- password,
32
- database,
33
- },
34
- hooks,
35
- projectDir,
36
- })
37
- );
38
- });
39
-
40
- program
41
- .command('load <outputDir>')
42
- .description('Loads model from database')
43
- .action(outputDir => {
44
- const { client, server, user, password, database } = program;
45
- const loadDataCondition = program.loadDataCondition
46
- ? table => table.name.match(new RegExp(program.loadDataCondition, 'i'))
47
- : null;
48
- const hooks = [];
49
- dbmodel.runAndExit(
50
- dbmodel.dump({
51
- client,
52
- connection: {
53
- server,
54
- user,
55
- password,
56
- database,
57
- },
58
- hooks,
59
- outputDir,
60
- loadDataCondition,
61
- })
62
- );
63
- });
64
-
65
- program
66
- .command('build <projectDir> <outputFile>')
67
- .description('Builds single SQL script from project')
68
- .action((projectDir, outputFile) => {
69
- const { client } = program;
70
- const hooks = [];
71
- dbmodel.runAndExit(
72
- dbmodel.build({
73
- client,
74
- hooks,
75
- projectDir,
76
- outputFile,
77
- })
78
- );
79
- });
80
-
81
- program.parse(process.argv);
1
+ #!/usr/bin/env node
2
+
3
+ const path = require('path');
4
+ require('dotenv').config();
5
+
6
+ global.API_PACKAGE = path.dirname(path.dirname(require.resolve('dbgate-api')));
7
+ global.PLUGINS_DIR = process.env.DEVMODE
8
+ ? path.join(path.dirname(path.dirname(global.API_PACKAGE)), 'plugins')
9
+ : path.dirname(global.API_PACKAGE);
10
+ global.IS_NPM_DIST = true;
11
+
12
+ const program = require('commander');
13
+ const dbgateApi = require('dbgate-api');
14
+ const { createLogger } = require('pinomin');
15
+
16
+ const logger = createLogger('dbmodel');
17
+
18
+ async function runAndExit(promise) {
19
+ try {
20
+ await promise;
21
+ logger.info('Success');
22
+ process.exit();
23
+ } catch (err) {
24
+ logger.error({ err }, 'Processing failed');
25
+ process.exit(1);
26
+ }
27
+ }
28
+
29
+ program
30
+ .option('-s, --server <server>', 'server host')
31
+ .option('-u, --user <user>', 'user name')
32
+ .option('-p, --password <password>', 'password')
33
+ .option('-d, --database <database>', 'database name')
34
+ .option('--auto-index-foreign-keys', 'automatically adds indexes to all foreign keys')
35
+ .option(
36
+ '--load-data-condition <condition>',
37
+ 'regex, which table data will be loaded and stored in model (in load command)'
38
+ )
39
+ .requiredOption('-e, --engine <engine>', 'engine name, eg. mysql@dbgate-plugin-mysql');
40
+
41
+ program
42
+ .command('deploy <modelFolder>')
43
+ .description('Deploys model to database')
44
+ .action(modelFolder => {
45
+ const { engine, server, user, password, database } = program.opts();
46
+ // const hooks = [];
47
+ // if (program.autoIndexForeignKeys) hooks.push(dbmodel.hooks.autoIndexForeignKeys);
48
+
49
+ runAndExit(
50
+ dbgateApi.deployDb({
51
+ connection: {
52
+ engine,
53
+ server,
54
+ user,
55
+ password,
56
+ database,
57
+ },
58
+ modelFolder,
59
+ })
60
+ );
61
+ });
62
+
63
+ // runAndExit(
64
+ // dbmodel.deploy({
65
+ // connection: {
66
+ // engine,
67
+ // server,
68
+ // user,
69
+ // password,
70
+ // database,
71
+ // },
72
+ // hooks,
73
+ // projectDir,
74
+ // })
75
+ // );
76
+
77
+ program
78
+ .command('load <outputDir>')
79
+ .description('Loads model from database')
80
+ .action(outputDir => {
81
+ const { engine, server, user, password, database } = program.opts();
82
+ // const loadDataCondition = program.loadDataCondition
83
+ // ? table => table.name.match(new RegExp(program.loadDataCondition, 'i'))
84
+ // : null;
85
+ // const hooks = [];
86
+
87
+ runAndExit(
88
+ dbgateApi.loadDatabase({
89
+ connection: {
90
+ engine,
91
+ server,
92
+ user,
93
+ password,
94
+ database,
95
+ },
96
+ outputDir,
97
+ })
98
+ );
99
+ });
100
+
101
+ program
102
+ .command('build <modelFolder> <outputFile>')
103
+ .description('Builds single SQL script from project')
104
+ .action((modelFolder, outputFile) => {
105
+ const { engine } = program.opts();
106
+ // const hooks = [];
107
+ runAndExit(
108
+ dbgateApi.generateModelSql({
109
+ // client,
110
+ // hooks,
111
+ modelFolder,
112
+ outputFile,
113
+ engine,
114
+ })
115
+ );
116
+ });
117
+
118
+ program.parse(process.argv);
package/package.json CHANGED
@@ -1,47 +1,45 @@
1
- {
2
- "name": "dbmodel",
3
- "version": "0.1.1",
4
- "description": "Deploy, load or build script from model of SQL database",
5
- "main": "lib/index.js",
6
- "scripts": {
7
- "test": "echo \"Error: no test specified\" && exit 1",
8
- "lint": "eslint"
9
- },
10
- "repository": {
11
- "type": "git",
12
- "url": "git+https://github.com/dbshell/dbmodel.git"
13
- },
14
- "bin": {
15
- "dbmodel": "./bin/dbmodel.js"
16
- },
17
- "keywords": [
18
- "sql",
19
- "model",
20
- "deploy",
21
- "mssql"
22
- ],
23
- "author": "Jan Prochazka",
24
- "license": "MIT",
25
- "bugs": {
26
- "url": "https://github.com/dbshell/dbmodel/issues"
27
- },
28
- "homepage": "https://github.com/dbshell/dbmodel#readme",
29
- "devDependencies": {
30
- "eslint": "^6.7.0"
31
- },
32
- "files": [
33
- "LICENSE",
34
- "README.md",
35
- "bin",
36
- "lib"
37
- ],
38
- "dependencies": {
39
- "commander": "^4.0.1",
40
- "fs-readdir-recursive": "^1.1.0",
41
- "js-yaml": "^3.13.1",
42
- "json-normalize": "^1.1.2",
43
- "mssql": "^6.0.1",
44
- "source-map-support": "^0.5.16",
45
- "toposort": "^2.0.2"
46
- }
47
- }
1
+ {
2
+ "name": "dbmodel",
3
+ "version": "5.2.2-alpha.11",
4
+ "homepage": "https://dbgate.org/",
5
+ "repository": {
6
+ "type": "git",
7
+ "url": "https://github.com/dbgate/dbgate.git"
8
+ },
9
+ "description": "Deploy, load or build script from model of SQL database",
10
+ "author": "Jan Prochazka",
11
+ "license": "MIT",
12
+ "bin": {
13
+ "dbgate-serve": "./bin/dbmodel.js"
14
+ },
15
+ "keywords": [
16
+ "sql",
17
+ "dbgate",
18
+ "web"
19
+ ],
20
+ "scripts": {
21
+ "dbmodel": "node ./bin/dbmodel.js",
22
+ "dbmodel:load": "cross-env DEVMODE=1 node ./bin/dbmodel.js load testdata/db -e postgres@dbgate-plugin-postgres -s localhost -u postgres -p Pwd2020Db -d zradlo",
23
+ "dbmodel:deploy": "cross-env DEVMODE=1 node ./bin/dbmodel.js deploy testdata/db -e postgres@dbgate-plugin-postgres -s localhost -u postgres -p Pwd2020Db -d deployed",
24
+ "dbmodel:build": "cross-env DEVMODE=1 node ./bin/dbmodel.js build testdata/db testdata/db.sql -e postgres@dbgate-plugin-postgres "
25
+ },
26
+ "files": [
27
+ "LICENSE",
28
+ "README.md",
29
+ "bin"
30
+ ],
31
+ "dependencies": {
32
+ "commander": "^10.0.0",
33
+ "dbgate-api": "^5.2.2-alpha.11",
34
+ "dbgate-plugin-csv": "^5.2.2-alpha.11",
35
+ "dbgate-plugin-excel": "^5.2.2-alpha.11",
36
+ "dbgate-plugin-mongo": "^5.2.2-alpha.11",
37
+ "dbgate-plugin-mssql": "^5.2.2-alpha.11",
38
+ "dbgate-plugin-mysql": "^5.2.2-alpha.11",
39
+ "dbgate-plugin-postgres": "^5.2.2-alpha.11",
40
+ "dbgate-plugin-xml": "^5.2.2-alpha.11",
41
+ "dbgate-web": "^5.2.2-alpha.11",
42
+ "dotenv": "^16.0.0",
43
+ "pinomin": "^1.0.1"
44
+ }
45
+ }
package/lib/build.js DELETED
@@ -1,24 +0,0 @@
1
- var fs = require('fs');
2
- const read = require('./read');
3
- const runHooks = require('./hooks/runHooks');
4
-
5
- async function build(options) {
6
- const { client } = options;
7
- await read(options);
8
-
9
- const { outputFile } = options;
10
- options.outputDescriptor = fs.openSync(outputFile, 'w');
11
-
12
- runHooks(options, 'before-build');
13
-
14
- const buildFunc = require(`./clients/${client}/build`);
15
- await buildFunc(options);
16
-
17
- runHooks(options, 'after-build');
18
-
19
- fs.close(options.outputDescriptor);
20
-
21
- return options;
22
- }
23
-
24
- module.exports = build;