Skip to content

Commit

Permalink
v0.0.4 (#9)
Browse files Browse the repository at this point in the history
* Test improvement

added reading parallel schema for postgres

* Update devDependencies

* Update .travis.yml

Using PostgreSQL 9.5

* Refactored reading methods

Reading different types of database objects with a single call to the 'read()' method

Remove 'read.tables()' and 'read.sequences()' methods

* Update README
  • Loading branch information
multum authored Jan 13, 2020
1 parent 8dfb2d8 commit a91edec
Show file tree
Hide file tree
Showing 27 changed files with 303 additions and 253 deletions.
6 changes: 5 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
dist: trusty
language: node_js
node_js:
- "9"
addons:
postgresql: "9.5"
services:
- mysql
- postgresql
install:
- npm install
- npm install codecov nyc
Expand Down
24 changes: 16 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
<br/>

![](https://multum.github.io/metalize/logo.svg)

Node.js tool for easy reading **database metadata**
Expand All @@ -10,7 +12,7 @@ Node.js tool for easy reading **database metadata**
## Features

- **PostgreSQL** and **MySQL** dialects
- Reading **tables** (columns, indexes, primary key, unique, foreign keys, checks)
- Reading **tables** (columns, indexes, primary keys, unique, foreign keys, checks)
- Reading **sequences** (start, max, min, cycle, increment)

## Documentation
Expand Down Expand Up @@ -42,8 +44,10 @@ const metalize = new Metalize({
},
});

const tables = await metalize.read.tables(['public.users', 'public.events']);
console.log(tables);
metalize
.read({ tables: ['public.users', 'public.events'] })
.then(result => console.log(result.tables));

/**
Map {
'public.users' => {
Expand All @@ -64,7 +68,7 @@ Map {

- **[Node.js](https://nodejs.org)** **v8.10** or more
- **[PostgreSQL server](https://www.postgresql.org/download)** **v9.2** or more
- **[node-postgres](https://github.com/brianc/node-postgres)**
- **[node-postgres](https://github.com/brianc/node-postgres)** **v7.1** or more

```bash
npm install metalize pg
Expand All @@ -84,8 +88,10 @@ const metalize = new Metalize({
},
});

const tables = await metalize.read.tables(['public.users', 'public.events']);
console.log(tables);
metalize
.read({ tables: ['public.users', 'public.events'] })
.then(result => console.log(result.tables));

/**
Map {
'public.users' => {
Expand All @@ -100,8 +106,10 @@ Map {
}
*/

const sequences = await metalize.read.sequences(['public.users_seq']);
console.log(sequences);
metalize
.read({ sequences: ['public.users_seq'] })
.then(result => console.log(result.sequences));

/**
Map {
'public.users_seq' => {
Expand Down
6 changes: 6 additions & 0 deletions docs/metadata/check.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Check

## name

- Type: `String`

Constraint name

## condition

- Type: `string`
6 changes: 6 additions & 0 deletions docs/metadata/foreign-key.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# ForeignKey

## name

- Type: `String`

Constraint name

## columns

- Type: `Array<String>`
Expand Down
6 changes: 6 additions & 0 deletions docs/metadata/index.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Index

## name

- Type: `String`

Index name

## columns

- Type: `Array<String>`
Expand Down
6 changes: 6 additions & 0 deletions docs/metadata/primary-key.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# PrimaryKey

## name

- Type: `String`

Constraint name

## columns

- Type: `Array<String>`
Expand Down
6 changes: 6 additions & 0 deletions docs/metadata/unique.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# unique

## name

- Type: `String`

Constraint name

## columns

- Type: `Array<String>`
Expand Down
15 changes: 5 additions & 10 deletions docs/methods.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
# Methods

## read.tables
## read

- Arguments: (names: `Array<String>`)
- Returns: [`Promise<TableMetadata>`](metadata/table.md)
- Arguments: (`{ ['tables' | 'sequences']: Array<String> }`)
- Returns: `Promise<{ ['tables' | 'sequences']: Map<String, Metadata | undefined> }>`

Getting the metadata of an existing tables
> see [table metadata](metadata/table.md) and [sequence metadata](metadata/sequence.md)
## read.sequences

- Arguments: (names: `Array<String>`)
- Returns: [`Promise<SequenceMetadata>`](metadata/sequence.md)

Getting the metadata of an existing sequences
Getting the metadata of an existing objects
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/
'use strict';

class AbstractClient {
class BaseConnectionManager {
_loadDialectModule(name) {
try {
return require(name);
Expand All @@ -23,4 +23,4 @@ class AbstractClient {
_end() {}
}

module.exports = AbstractClient;
module.exports = BaseConnectionManager;
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,12 @@
*/
'use strict';

class AbstractSequenceReader {
constructor(metalize) {
this.metalize = metalize;
}

async read() {
class BaseSequenceReader {
static async read(metalize) {
throw new Error(
`read.sequences(names) method for the \`${this.metalize.dialect}\` dialect is not supported`
`read({ sequences: [...] }) method for the \`${metalize.dialect}\` dialect is not supported`
);
}
}

module.exports = AbstractSequenceReader;
module.exports = BaseSequenceReader;
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
'use strict';

const helpers = require('../../helpers');
const AbstractClient = require('../abstract/client');
const BaseConnectionManager = require('../base/connectionManeger');

let lib;

class MySqlClient extends AbstractClient {
class ConnectionManager extends BaseConnectionManager {
constructor(metalize) {
super();
this.metalize = metalize;
Expand Down Expand Up @@ -54,4 +54,4 @@ class MySqlClient extends AbstractClient {
}
}

module.exports = MySqlClient;
module.exports = ConnectionManager;
12 changes: 6 additions & 6 deletions lib/dialects/mysql/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
*/
'use strict';

const Client = require('./client');
const Table = require('./tables');
const Sequence = require('./sequence');
const TableInterface = require('./tables');
const SequenceInterface = require('./sequences');
const ConnectionManager = require('./connectionManager');

/**
*
Expand All @@ -18,9 +18,9 @@ const Sequence = require('./sequence');

class MySqlDialect {
constructor() {
this.Client = Client;
this.Table = Table;
this.Sequence = Sequence;
this.TableInterface = TableInterface;
this.SequenceInterface = SequenceInterface;
this.ConnectionManager = ConnectionManager;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,8 @@
*/
'use strict';

const AbstractSequenceReader = require('../../abstract/sequence');
const BaseSequenceReader = require('../../base/sequence');

class SequenceReader extends AbstractSequenceReader {
constructor(metalize, options) {
super(metalize);
this.options = options;
}
}
class SequenceReader extends BaseSequenceReader {}

module.exports = SequenceReader;
40 changes: 15 additions & 25 deletions lib/dialects/mysql/tables/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,46 +11,36 @@ const parser = require('./parser');
const utils = require('../../../utils');

class TableReader {
constructor(metalize, options) {
this.metalize = metalize;
this.names = options.names;
}

getColumns() {
return this.metalize._client
.query(queries.getColumns(this.names))
static getColumns(metalize, names) {
return metalize._client
.query(queries.getColumns(names))
.then(utils.group('table'))
.then(utils.map(parser.columns));
}

getConstraints() {
return this.metalize._client
.query(queries.getConstraints(this.names))
static getConstraints(metalize, names) {
return metalize._client
.query(queries.getConstraints(names))
.then(utils.group('table'))
.then(
utils.map(
utils.pipe(
parser.constraints,
utils.cleanableGroup('type')
)
)
utils.map(utils.pipe(parser.constraints, utils.cleanableGroup('type')))
);
}

getIndexes() {
return this.metalize._client
.query(queries.getIndexes(this.names))
static getIndexes(metalize, names) {
return metalize._client
.query(queries.getIndexes(names))
.then(utils.group('table'))
.then(utils.map(parser.indexes));
}

async read() {
static async read(metalize, names) {
const [columns, constraints, indexes] = await Promise.all([
this.getColumns(),
this.getConstraints(),
this.getIndexes(),
TableReader.getColumns(metalize, names),
TableReader.getConstraints(metalize, names),
TableReader.getIndexes(metalize, names),
]);
return this.names.reduce((acc, name) => {
return names.reduce((acc, name) => {
const structure = utils.get([name, 0], columns)
? {
name,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
*/
'use strict';

const AbstractClient = require('../abstract/client');
const BaseConnectionManager = require('../base/connectionManeger');

let lib;

class PostgresClient extends AbstractClient {
class ConnectionManager extends BaseConnectionManager {
constructor(metalize) {
super();
this.metalize = metalize;
Expand All @@ -36,12 +36,12 @@ class PostgresClient extends AbstractClient {
}
}

async query(sql) {
query(sql) {
if (!this._client) {
await this._connect();
this._connect();
}
return this._client.query(sql);
}
}

module.exports = PostgresClient;
module.exports = ConnectionManager;
12 changes: 6 additions & 6 deletions lib/dialects/postgres/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
*/
'use strict';

const Client = require('./client');
const Table = require('./tables');
const Sequence = require('./sequence');
const TableInterface = require('./tables');
const SequenceInterface = require('./sequences');
const ConnectionManager = require('./connectionManager');

/**
*
Expand All @@ -18,9 +18,9 @@ const Sequence = require('./sequence');

class PostgresDialect {
constructor() {
this.Client = Client;
this.Table = Table;
this.Sequence = Sequence;
this.TableInterface = TableInterface;
this.SequenceInterface = SequenceInterface;
this.ConnectionManager = ConnectionManager;
}
}

Expand Down
Loading

0 comments on commit a91edec

Please sign in to comment.