@cyklang/cli 0.5.6 → 0.6.0

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.
@@ -1,5 +1,14 @@
1
1
  #!/usr/bin/env ts-node
2
+ import { DBManager } from "@cyklang/core";
3
+ import loglevel from 'loglevel';
2
4
  import { Command } from "commander";
5
+ export declare const logger: loglevel.Logger;
3
6
  export declare class TableCommand extends Command {
4
7
  constructor(name: string);
5
8
  }
9
+ /**
10
+ * function dropTable
11
+ * @param table
12
+ * @param dbManager
13
+ */
14
+ export declare function dropTable(table: string, dbManager: DBManager): Promise<void>;
@@ -27,7 +27,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
27
27
  return (mod && mod.__esModule) ? mod : { "default": mod };
28
28
  };
29
29
  Object.defineProperty(exports, "__esModule", { value: true });
30
- exports.TableCommand = void 0;
30
+ exports.dropTable = exports.TableCommand = exports.logger = void 0;
31
31
  const fs = __importStar(require("fs"));
32
32
  const loglevel_1 = __importDefault(require("loglevel"));
33
33
  const commander_1 = require("commander");
@@ -35,8 +35,9 @@ const child_process_1 = require("child_process");
35
35
  const Cmd_1 = require("./Cmd");
36
36
  const DBClient_1 = require("./DBClient");
37
37
  const form_data_1 = __importDefault(require("form-data"));
38
- const logger = loglevel_1.default.getLogger("TableCommand.ts");
39
- logger.setLevel("debug");
38
+ const inquirer_1 = __importDefault(require("inquirer"));
39
+ exports.logger = loglevel_1.default.getLogger("TableCommand.ts");
40
+ exports.logger.setLevel("debug");
40
41
  class TableCommand extends commander_1.Command {
41
42
  constructor(name) {
42
43
  super(name);
@@ -45,8 +46,7 @@ class TableCommand extends commander_1.Command {
45
46
  this.addCommand(new TableImportCmd());
46
47
  this.addCommand(new TableList('list', 'list database tables'));
47
48
  this.addCommand(new TableList('l', '(l)ist database tables'));
48
- this.addCommand(new TableData('data', 'table (d)ata'));
49
- this.addCommand(new TableData('d', 'table (d)ata'));
49
+ this.addCommand(new TableDropCommand('drop', 'drop table'));
50
50
  this.addCommand(new TableUpdate('update', 'update table properties'));
51
51
  this.addCommand(new TableUpdate('u', 'shortcut for (u)pdate table properties'));
52
52
  }
@@ -84,7 +84,7 @@ class TableUpdate extends Cmd_1.Cmd {
84
84
  await this.updateAuthAccess('cyk_table', 'table', dbTable.id || '', options);
85
85
  }
86
86
  catch (err) {
87
- logger.error(err);
87
+ exports.logger.error(err);
88
88
  }
89
89
  }
90
90
  }
@@ -115,7 +115,7 @@ class TableExportCmd extends Cmd_1.Cmd {
115
115
  const tableName = tables[indi];
116
116
  const dbTable = await this.dbManager.dbTableExist(tableName);
117
117
  if (dbTable === undefined) {
118
- logger.info('table ' + tableName + ' not found');
118
+ exports.logger.info('table ' + tableName + ' not found');
119
119
  continue;
120
120
  }
121
121
  const filename = buildFilename(dbTable.name, options);
@@ -123,7 +123,7 @@ class TableExportCmd extends Cmd_1.Cmd {
123
123
  }
124
124
  }
125
125
  catch (err) {
126
- logger.error(err);
126
+ exports.logger.error(err);
127
127
  }
128
128
  }
129
129
  async copyTo(dbTable, filename) {
@@ -134,15 +134,15 @@ class TableExportCmd extends Cmd_1.Cmd {
134
134
  const response = await this.dbRemote.apiServer.get('/admin/export/' + dbTable.name, { responseType: 'stream' });
135
135
  const stream = response;
136
136
  stream.on('data', (data) => {
137
- logger.debug('**** DATA ****', data.length);
137
+ exports.logger.debug('**** DATA ****', data.length);
138
138
  const textDecoder = new TextDecoder();
139
139
  const chunk = (textDecoder.decode(data));
140
140
  writeStream.write(chunk);
141
141
  });
142
- stream.on('end', () => logger.debug('**** DONE ****'));
142
+ stream.on('end', () => exports.logger.debug('**** DONE ****'));
143
143
  }
144
144
  catch (err) {
145
- logger.error(err);
145
+ exports.logger.error(err);
146
146
  }
147
147
  }
148
148
  }
@@ -173,7 +173,7 @@ class TableImportCmd extends Cmd_1.Cmd {
173
173
  const tableName = tables[indi];
174
174
  const dbTable = await this.dbManager.dbTableExist(tableName);
175
175
  if (dbTable === undefined) {
176
- logger.info('table ' + tableName + ' not found');
176
+ exports.logger.info('table ' + tableName + ' not found');
177
177
  continue;
178
178
  }
179
179
  const filename = buildFilename(dbTable.name, options);
@@ -181,7 +181,7 @@ class TableImportCmd extends Cmd_1.Cmd {
181
181
  }
182
182
  }
183
183
  catch (err) {
184
- logger.error(err);
184
+ exports.logger.error(err);
185
185
  }
186
186
  }
187
187
  async copyFrom(dbTable, filename) {
@@ -194,11 +194,11 @@ class TableImportCmd extends Cmd_1.Cmd {
194
194
  const route = '/admin/import/' + dbTable.name;
195
195
  const resp = await this.dbRemote.apiServer.post(route, form);
196
196
  if (resp.status === 200) {
197
- logger.debug(filename + ' uploaded');
197
+ exports.logger.debug(filename + ' uploaded');
198
198
  }
199
199
  }
200
200
  catch (err) {
201
- logger.error(err);
201
+ exports.logger.error(err);
202
202
  }
203
203
  }
204
204
  }
@@ -226,7 +226,7 @@ function buildFilename(tableName, options) {
226
226
  return filename;
227
227
  }
228
228
  const commandImportExport = async (dbManager, direction, tables, options) => {
229
- logger.debug('tables : ', tables, 'options : ', options);
229
+ exports.logger.debug('tables : ', tables, 'options : ', options);
230
230
  try {
231
231
  if (options.file === undefined && options.dir === undefined)
232
232
  throw '--file or --dir mandatory';
@@ -240,14 +240,14 @@ const commandImportExport = async (dbManager, direction, tables, options) => {
240
240
  const tableName = tables[indi];
241
241
  const dbTable = await dbManager.dbTableExist(tableName);
242
242
  if (dbTable === undefined) {
243
- logger.info('table ' + tableName + ' not found');
243
+ exports.logger.info('table ' + tableName + ' not found');
244
244
  continue;
245
245
  }
246
246
  psqlCmd(dbTable, direction, options);
247
247
  }
248
248
  }
249
249
  catch (err) {
250
- logger.error(err);
250
+ exports.logger.error(err);
251
251
  }
252
252
  };
253
253
  //--------------------------------------------------------------------------------------------------------------------
@@ -282,7 +282,7 @@ function psqlCmd(dbTable, direction, options) {
282
282
  else if (fs.existsSync(options.dir) === false)
283
283
  throw 'directory ' + options.dir + ' does not exist';
284
284
  else if (fs.lstatSync(options.dir).isFile()) {
285
- logger.debug("--dir " + options.dir + " is a file ");
285
+ exports.logger.debug("--dir " + options.dir + " is a file ");
286
286
  }
287
287
  else {
288
288
  let directory = options.dir.trim();
@@ -293,7 +293,7 @@ function psqlCmd(dbTable, direction, options) {
293
293
  }
294
294
  let copy_cmd = "\\copy " + dbTable.name + lcols + direction + " '" + filename + "' csv header";
295
295
  const shell_cmd = psql_cmd + '"' + copy_cmd + '"';
296
- logger.debug(shell_cmd);
296
+ exports.logger.debug(shell_cmd);
297
297
  const child = (0, child_process_1.spawn)(shell_cmd.trim(), { stdio: 'inherit', shell: true });
298
298
  }
299
299
  //--------------------------------------------------------------------------------------------------------------------
@@ -316,57 +316,43 @@ class TableList extends Cmd_1.Cmd {
316
316
  dbClient.selectFromTable('List of Tables', 'cyk_table', { fields: 'table_id,table_name,table_auth,table_access,table_description', sort: options.sort || '1' });
317
317
  }
318
318
  }
319
- //--------------------------------------------------------------------------------------------------------------------
320
- // class TableData
321
- //--------------------------------------------------------------------------------------------------------------------
322
- class TableData extends Cmd_1.Cmd {
319
+ class TableDropCommand extends Cmd_1.Cmd {
323
320
  constructor(name, description) {
324
321
  super(name);
325
322
  this.description(description)
326
323
  .argument('<table>', 'table name')
327
- .option('--where <clause_where_sql>', 'criteria in SQL syntax')
328
- .option('-s --sort <columns>', 'sort by columns positions (begins by 0) separated by comma')
329
- .option('-w --width <colwidth>', 'columns widths separated by comma')
330
324
  .action(async (table, options) => {
331
- this.commandData(table, options);
325
+ await this.commandDrop(table, options);
332
326
  });
333
327
  }
334
- async commandData(table, options) {
328
+ async commandDrop(table, options) {
335
329
  try {
336
330
  await this.prologue(options);
337
- if (this.dbManager === undefined)
331
+ if (!this.dbManager)
338
332
  throw 'dbManager undefined';
339
333
  const dbTable = await this.dbManager.dbTableExist(table);
340
- if (dbTable === undefined)
341
- throw 'table ' + table + ' not found';
342
- let fields = '';
343
- for (let ind = 0; ind < dbTable.columns.length; ind++) {
344
- const dbColumn = dbTable.columns[ind];
345
- let ok = false;
346
- let fieldName = dbColumn.name;
347
- if (dbColumn.dbType === 'text' || dbColumn.dbType === 'bytea') {
348
- ok = false;
349
- fieldName = 'length(' + dbColumn.name + ')';
350
- }
351
- else {
352
- ok = true;
353
- }
354
- if (ok === true) {
355
- if (fields !== '')
356
- fields += ',';
357
- fields += fieldName;
358
- }
334
+ if (!dbTable) {
335
+ console.log('table ' + table + ' does not exist');
336
+ return;
359
337
  }
360
- logger.debug('commandQuery', table);
361
- const dbClient = new DBClient_1.DBClient(this.dbManager);
362
- let title = 'Table ' + table;
363
- if (options.where !== undefined) {
364
- title += ' where ' + options.where;
338
+ const reply = await inquirer_1.default.prompt({ type: 'confirm', name: 'confirm', message: 'Drop table ' + table + '?' });
339
+ if (reply.confirm && this.dbManager) {
340
+ await dropTable(table, this.dbManager);
365
341
  }
366
- dbClient.selectFromTable(title, table, { fields: fields, width: options.width, sort: options.sort || '1', where: options.where });
367
342
  }
368
343
  catch (err) {
369
- logger.error(err);
344
+ exports.logger.error(err);
370
345
  }
371
346
  }
372
347
  }
348
+ /**
349
+ * function dropTable
350
+ * @param table
351
+ * @param dbManager
352
+ */
353
+ async function dropTable(table, dbManager) {
354
+ const dbTable = await dbManager.dbTableExist(table);
355
+ if (dbTable)
356
+ await dbManager.dbTableDelete(dbTable);
357
+ }
358
+ exports.dropTable = dropTable;
@@ -0,0 +1,5 @@
1
+ import { Cmd } from "./Cmd";
2
+ export declare class TableDataCommand extends Cmd {
3
+ constructor(name: string, description: string);
4
+ commandData(table: string, options: any): Promise<void>;
5
+ }
@@ -0,0 +1,61 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.TableDataCommand = void 0;
4
+ const Cmd_1 = require("./Cmd");
5
+ const DBClient_1 = require("./DBClient");
6
+ const TableCommand_1 = require("./TableCommand");
7
+ //--------------------------------------------------------------------------------------------------------------------
8
+ // class TableData
9
+ //--------------------------------------------------------------------------------------------------------------------
10
+ class TableDataCommand extends Cmd_1.Cmd {
11
+ constructor(name, description) {
12
+ super(name);
13
+ this.description(description)
14
+ .argument('<table>', 'table name')
15
+ .option('--where <clause_where_sql>', 'criteria in SQL syntax')
16
+ .option('-s --sort <columns>', 'sort by columns positions (begins by 0) separated by comma')
17
+ .option('-w --width <colwidth>', 'columns widths separated by comma')
18
+ .action(async (table, options) => {
19
+ this.commandData(table, options);
20
+ });
21
+ }
22
+ async commandData(table, options) {
23
+ try {
24
+ await this.prologue(options);
25
+ if (this.dbManager === undefined)
26
+ throw 'dbManager undefined';
27
+ const dbTable = await this.dbManager.dbTableExist(table);
28
+ if (dbTable === undefined)
29
+ throw 'table ' + table + ' not found';
30
+ let fields = '';
31
+ for (let ind = 0; ind < dbTable.columns.length; ind++) {
32
+ const dbColumn = dbTable.columns[ind];
33
+ let ok = false;
34
+ let fieldName = dbColumn.name;
35
+ if (dbColumn.dbType === 'text' || dbColumn.dbType === 'bytea') {
36
+ ok = false;
37
+ fieldName = 'length(' + dbColumn.name + ')';
38
+ }
39
+ else {
40
+ ok = true;
41
+ }
42
+ if (ok === true) {
43
+ if (fields !== '')
44
+ fields += ',';
45
+ fields += fieldName;
46
+ }
47
+ }
48
+ TableCommand_1.logger.debug('commandQuery', table);
49
+ const dbClient = new DBClient_1.DBClient(this.dbManager);
50
+ let title = 'Table ' + table;
51
+ if (options.where !== undefined) {
52
+ title += ' where ' + options.where;
53
+ }
54
+ dbClient.selectFromTable(title, table, { fields: fields, width: options.width, sort: options.sort || '1', where: options.where });
55
+ }
56
+ catch (err) {
57
+ TableCommand_1.logger.error(err);
58
+ }
59
+ }
60
+ }
61
+ exports.TableDataCommand = TableDataCommand;
@@ -0,0 +1,5 @@
1
+ import { Cmd } from "./Cmd";
2
+ export declare class TableDataCommand extends Cmd {
3
+ constructor(name: string, description: string);
4
+ commandData(table: string, options: any): Promise<void>;
5
+ }
@@ -0,0 +1,61 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.TableDataCommand = void 0;
4
+ const Cmd_1 = require("./Cmd");
5
+ const DBClient_1 = require("./DBClient");
6
+ const TableCommand_1 = require("./TableCommand");
7
+ //--------------------------------------------------------------------------------------------------------------------
8
+ // class TableData
9
+ //--------------------------------------------------------------------------------------------------------------------
10
+ class TableDataCommand extends Cmd_1.Cmd {
11
+ constructor(name, description) {
12
+ super(name);
13
+ this.description(description)
14
+ .argument('<table>', 'table name')
15
+ .option('--where <clause_where_sql>', 'criteria in SQL syntax')
16
+ .option('-s --sort <columns>', 'sort by columns positions (begins by 0) separated by comma')
17
+ .option('-w --width <colwidth>', 'columns widths separated by comma')
18
+ .action(async (table, options) => {
19
+ this.commandData(table, options);
20
+ });
21
+ }
22
+ async commandData(table, options) {
23
+ try {
24
+ await this.prologue(options);
25
+ if (this.dbManager === undefined)
26
+ throw 'dbManager undefined';
27
+ const dbTable = await this.dbManager.dbTableExist(table);
28
+ if (dbTable === undefined)
29
+ throw 'table ' + table + ' not found';
30
+ let fields = '';
31
+ for (let ind = 0; ind < dbTable.columns.length; ind++) {
32
+ const dbColumn = dbTable.columns[ind];
33
+ let ok = false;
34
+ let fieldName = dbColumn.name;
35
+ if (dbColumn.dbType === 'text' || dbColumn.dbType === 'bytea') {
36
+ ok = false;
37
+ fieldName = 'length(' + dbColumn.name + ')';
38
+ }
39
+ else {
40
+ ok = true;
41
+ }
42
+ if (ok === true) {
43
+ if (fields !== '')
44
+ fields += ',';
45
+ fields += fieldName;
46
+ }
47
+ }
48
+ TableCommand_1.logger.debug('commandQuery', table);
49
+ const dbClient = new DBClient_1.DBClient(this.dbManager);
50
+ let title = 'Table ' + table;
51
+ if (options.where !== undefined) {
52
+ title += ' where ' + options.where;
53
+ }
54
+ dbClient.selectFromTable(title, table, { fields: fields, width: options.width, sort: options.sort || '1', where: options.where });
55
+ }
56
+ catch (err) {
57
+ TableCommand_1.logger.error(err);
58
+ }
59
+ }
60
+ }
61
+ exports.TableDataCommand = TableDataCommand;
package/build/index.js CHANGED
@@ -16,11 +16,12 @@ const InstallCommand_1 = require("./InstallCommand");
16
16
  const AssetCommand_1 = require("./AssetCommand");
17
17
  const OpenCommand_1 = require("./OpenCommand");
18
18
  const UninstallCommand_1 = require("./UninstallCommand");
19
+ const TableDataCommand_1 = require("./TableDataCommand");
19
20
  const logger = loglevel_1.default.getLogger("index.ts");
20
21
  logger.setLevel("debug");
21
22
  const program = new commander_1.Command();
22
23
  program.name('cyk').description('cyklang CLI')
23
- .version('0.5.6');
24
+ .version('0.6.0');
24
25
  program.addCommand(new InstallCommand_1.InstallCommand('install'));
25
26
  program.addCommand(new InstallCommand_1.InstallCommand('i'));
26
27
  program.addCommand(new UninstallCommand_1.UninstallCommand('uninstall'));
@@ -34,6 +35,8 @@ program.addCommand(new QueryCommand_1.QueryCommand('query'));
34
35
  program.addCommand(new QueryCommand_1.QueryCommand('q'));
35
36
  program.addCommand(new TableCommand_1.TableCommand('table'));
36
37
  program.addCommand(new TableCommand_1.TableCommand('t'));
38
+ program.addCommand(new TableDataCommand_1.TableDataCommand('data', 'show table data'));
39
+ program.addCommand(new TableDataCommand_1.TableDataCommand('d', 'show table data'));
37
40
  program.addCommand(new TestCommand_1.TestCommand());
38
41
  program.addCommand(new RunCommand_1.RunCommand('run'));
39
42
  program.addCommand(new RunCommand_1.RunCommand('r'));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cyklang/cli",
3
- "version": "0.5.6",
3
+ "version": "0.6.0",
4
4
  "description": "cyklang CLI",
5
5
  "main": "build/index.js",
6
6
  "scripts": {
@@ -17,7 +17,7 @@
17
17
  "author": "",
18
18
  "license": "ISC",
19
19
  "dependencies": {
20
- "@cyklang/core": "^0.5.3",
20
+ "@cyklang/core": "^0.5.4",
21
21
  "commander": "^9.4.0",
22
22
  "dotenv": "^16.0.1",
23
23
  "form-data": "^4.0.0",
@@ -8,8 +8,10 @@ import { Cmd } from "./Cmd"
8
8
  import { DBClient } from "./DBClient"
9
9
  import { Stream } from "form-data"
10
10
  import FormData from 'form-data'
11
+ import inquirer from 'inquirer'
12
+ import { TableDataCommand } from "./TableDataCommand"
11
13
 
12
- const logger = loglevel.getLogger("TableCommand.ts")
14
+ export const logger = loglevel.getLogger("TableCommand.ts")
13
15
  logger.setLevel("debug")
14
16
 
15
17
  export class TableCommand extends Command {
@@ -20,8 +22,7 @@ export class TableCommand extends Command {
20
22
  this.addCommand(new TableImportCmd())
21
23
  this.addCommand(new TableList('list', 'list database tables'))
22
24
  this.addCommand(new TableList('l', '(l)ist database tables'))
23
- this.addCommand(new TableData('data', 'table (d)ata'))
24
- this.addCommand(new TableData('d', 'table (d)ata'))
25
+ this.addCommand(new TableDropCommand('drop', 'drop table'))
25
26
  this.addCommand(new TableUpdate('update', 'update table properties'))
26
27
  this.addCommand(new TableUpdate('u', 'shortcut for (u)pdate table properties'))
27
28
  }
@@ -40,12 +41,12 @@ class TableUpdate extends Cmd {
40
41
  constructor(name: string, description: string) {
41
42
  super(name)
42
43
  this.description(description)
43
- .option('-n --name <table_name>', 'name of the table whose properties are to be changed')
44
- .option('--auth <auth_schema>', 'authentication schema : basic | token | cookie | any | none, default is none')
45
- .option('--access <access>', 'access rights required')
46
- .action(async (options: any) => {
47
- await this.commandUpdate(options)
48
- })
44
+ .option('-n --name <table_name>', 'name of the table whose properties are to be changed')
45
+ .option('--auth <auth_schema>', 'authentication schema : basic | token | cookie | any | none, default is none')
46
+ .option('--access <access>', 'access rights required')
47
+ .action(async (options: any) => {
48
+ await this.commandUpdate(options)
49
+ })
49
50
  }
50
51
 
51
52
  async commandUpdate(options: any) {
@@ -53,10 +54,10 @@ class TableUpdate extends Cmd {
53
54
  await this.prologue(options)
54
55
  if (this.dbManager === undefined) throw 'dbManager undefined'
55
56
 
56
- if ( ! options.name ) throw '<table_name> is missing'
57
+ if (!options.name) throw '<table_name> is missing'
57
58
 
58
59
  const dbTable = await this.dbManager.dbTableExist(options.name)
59
- if ( ! dbTable ) throw 'Table ' + options.name + ' not found'
60
+ if (!dbTable) throw 'Table ' + options.name + ' not found'
60
61
 
61
62
  await this.updateAuthAccess('cyk_table', 'table', dbTable.id || '', options)
62
63
  }
@@ -319,63 +320,46 @@ class TableList extends Cmd {
319
320
  }
320
321
  }
321
322
 
322
- //--------------------------------------------------------------------------------------------------------------------
323
- // class TableData
324
- //--------------------------------------------------------------------------------------------------------------------
325
-
326
- class TableData extends Cmd {
327
-
323
+ class TableDropCommand extends Cmd {
328
324
  constructor(name: string, description: string) {
329
325
  super(name)
330
326
  this.description(description)
331
327
  .argument('<table>', 'table name')
332
- .option('--where <clause_where_sql>', 'criteria in SQL syntax')
333
- .option('-s --sort <columns>', 'sort by columns positions (begins by 0) separated by comma')
334
- .option('-w --width <colwidth>', 'columns widths separated by comma')
335
328
  .action(async (table, options) => {
336
- this.commandData(table, options)
329
+ await this.commandDrop(table, options)
337
330
  })
338
331
  }
339
332
 
340
- async commandData(table: string, options: any) {
333
+ async commandDrop(table: string, options: any) {
341
334
  try {
342
335
  await this.prologue(options)
343
- if (this.dbManager === undefined) throw 'dbManager undefined'
344
-
336
+ if (!this.dbManager) throw 'dbManager undefined'
337
+
345
338
  const dbTable = await this.dbManager.dbTableExist(table)
346
- if (dbTable === undefined) throw 'table ' + table + ' not found'
347
-
348
- let fields = ''
349
-
350
- for (let ind = 0; ind < dbTable.columns.length; ind++) {
351
- const dbColumn = dbTable.columns[ind]
352
- let ok = false
353
- let fieldName = dbColumn.name
354
- if (dbColumn.dbType === 'text' || dbColumn.dbType === 'bytea') {
355
- ok = false
356
- fieldName = 'length(' + dbColumn.name + ')'
357
- }
358
- else {
359
- ok = true
360
- }
361
- if (ok === true) {
362
- if (fields !== '') fields += ','
363
- fields += fieldName
364
- }
339
+ if (!dbTable) {
340
+ console.log('table ' + table + ' does not exist')
341
+ return
365
342
  }
366
-
367
- logger.debug('commandQuery', table)
368
- const dbClient = new DBClient(this.dbManager)
369
- let title = 'Table ' + table
370
- if (options.where !== undefined) {
371
- title += ' where ' + options.where
343
+ const reply = await inquirer.prompt({ type: 'confirm', name: 'confirm', message: 'Drop table ' + table + '?' })
344
+ if (reply.confirm && this.dbManager) {
345
+ await dropTable(table, this.dbManager)
372
346
  }
373
- dbClient.selectFromTable(title, table,
374
- { fields: fields, width: options.width, sort: options.sort || '1', where: options.where })
375
347
  }
376
- catch (err) {
348
+ catch(err) {
377
349
  logger.error(err)
378
350
  }
379
351
  }
380
352
  }
381
353
 
354
+ /**
355
+ * function dropTable
356
+ * @param table
357
+ * @param dbManager
358
+ */
359
+ export async function dropTable(table: string, dbManager: DBManager) {
360
+ const dbTable = await dbManager.dbTableExist(table)
361
+ if (dbTable)
362
+ await dbManager.dbTableDelete(dbTable)
363
+ }
364
+
365
+
@@ -0,0 +1,62 @@
1
+ import { Cmd } from "./Cmd";
2
+ import { DBClient } from "./DBClient";
3
+ import { logger } from "./TableCommand";
4
+
5
+ //--------------------------------------------------------------------------------------------------------------------
6
+ // class TableData
7
+ //--------------------------------------------------------------------------------------------------------------------
8
+ export class TableDataCommand extends Cmd {
9
+
10
+ constructor(name: string, description: string) {
11
+ super(name);
12
+ this.description(description)
13
+ .argument('<table>', 'table name')
14
+ .option('--where <clause_where_sql>', 'criteria in SQL syntax')
15
+ .option('-s --sort <columns>', 'sort by columns positions (begins by 0) separated by comma')
16
+ .option('-w --width <colwidth>', 'columns widths separated by comma')
17
+ .action(async (table, options) => {
18
+ this.commandData(table, options);
19
+ });
20
+ }
21
+
22
+ async commandData(table: string, options: any) {
23
+ try {
24
+ await this.prologue(options);
25
+ if (this.dbManager === undefined) throw 'dbManager undefined';
26
+
27
+ const dbTable = await this.dbManager.dbTableExist(table);
28
+ if (dbTable === undefined) throw 'table ' + table + ' not found';
29
+
30
+ let fields = '';
31
+
32
+ for (let ind = 0; ind < dbTable.columns.length; ind++) {
33
+ const dbColumn = dbTable.columns[ind];
34
+ let ok = false;
35
+ let fieldName = dbColumn.name;
36
+ if (dbColumn.dbType === 'text' || dbColumn.dbType === 'bytea') {
37
+ ok = false;
38
+ fieldName = 'length(' + dbColumn.name + ')';
39
+ }
40
+ else {
41
+ ok = true;
42
+ }
43
+ if (ok === true) {
44
+ if (fields !== '') fields += ',';
45
+ fields += fieldName;
46
+ }
47
+ }
48
+
49
+ logger.debug('commandQuery', table);
50
+ const dbClient = new DBClient(this.dbManager);
51
+ let title = 'Table ' + table;
52
+ if (options.where !== undefined) {
53
+ title += ' where ' + options.where;
54
+ }
55
+ dbClient.selectFromTable(title, table,
56
+ { fields: fields, width: options.width, sort: options.sort || '1', where: options.where });
57
+ }
58
+ catch (err) {
59
+ logger.error(err);
60
+ }
61
+ }
62
+ }
package/src/index.ts CHANGED
@@ -11,12 +11,13 @@ import { InstallCommand } from './InstallCommand'
11
11
  import { AssetCommand } from './AssetCommand'
12
12
  import { OpenCommand } from './OpenCommand'
13
13
  import { UninstallCommand } from './UninstallCommand'
14
+ import { TableDataCommand } from './TableDataCommand'
14
15
  const logger = loglevel.getLogger("index.ts")
15
16
  logger.setLevel("debug")
16
17
 
17
18
  const program = new Command()
18
19
  program.name('cyk').description('cyklang CLI')
19
- .version('0.5.6')
20
+ .version('0.6.0')
20
21
 
21
22
  program.addCommand(new InstallCommand('install'))
22
23
  program.addCommand(new InstallCommand('i'))
@@ -31,6 +32,8 @@ program.addCommand(new QueryCommand('query'))
31
32
  program.addCommand(new QueryCommand('q'))
32
33
  program.addCommand(new TableCommand('table'))
33
34
  program.addCommand(new TableCommand('t'))
35
+ program.addCommand(new TableDataCommand('data', 'show table data'))
36
+ program.addCommand(new TableDataCommand('d', 'show table data'))
34
37
  program.addCommand(new TestCommand())
35
38
  program.addCommand(new RunCommand('run'))
36
39
  program.addCommand(new RunCommand('r'))