This repository has been archived by the owner on Mar 15, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support to execute a query with multiple statements
There is no breaking changes. Executing a query with a single statement still have the same result. Although executing a query with multiple statements will return something like this: ```js { fiels: [ [<fields first query>], [<fields second query>] ], rows: [ [<rows first query>], [<rows second query>] ] } ``` To help with tests I also included the knex module. Otherwise I would have to test the `executeQuery` function using itself.
- Loading branch information
Showing
11 changed files
with
130 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { join } from 'path'; | ||
import { readFileSync } from 'fs'; | ||
import knexInit from 'knex'; | ||
|
||
|
||
export function readSQLScript(client) { | ||
const scriptPath = join(__dirname, `./${client}/truncate-all-tables.sql`); | ||
return readFileSync(scriptPath, { encoding: 'utf8' }); | ||
} | ||
|
||
|
||
export function truncateAllTables(serverInfo, { truncateTablesSQL }) { | ||
const { name: client, ...connection } = serverInfo; | ||
const knex = knexInit({ client, connection }); | ||
|
||
return knex.raw(truncateTablesSQL).then((sql) => { | ||
// with knex depending on the DB client | ||
// the rows may come in a "rows" property | ||
const statements = (sql.rows || sql[0]); | ||
return knex.raw(statements.map((sqlQuery) => { | ||
return sqlQuery.trucate_table_cmd; | ||
}).join('')); | ||
}); | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
SELECT 'SET FOREIGN_KEY_CHECKS = 0;' trucate_table_cmd | ||
UNION | ||
SELECT Concat('TRUNCATE TABLE `' ,table_schema, '`.`', TABLE_NAME, '`;') trucate_table_cmd | ||
FROM INFORMATION_SCHEMA.TABLES | ||
WHERE table_schema = (SELECT database()) | ||
UNION | ||
SELECT 'SET FOREIGN_KEY_CHECKS = 1;' trucate_table_cmd |
4 changes: 2 additions & 2 deletions
4
spec/databases/postgres/schema.sql → spec/databases/postgresql/schema/schema.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,11 @@ | ||
CREATE TABLE users( | ||
id INT PRIMARY KEY NOT NULL, | ||
id SERIAL PRIMARY KEY, | ||
username TEXT NOT NULL, | ||
email TEXT NOT NULL, | ||
password TEXT NOT NULL | ||
); | ||
|
||
CREATE TABLE roles( | ||
id INT PRIMARY KEY NOT NULL, | ||
id SERIAL PRIMARY KEY, | ||
name TEXT NOT NULL | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
SELECT Concat('TRUNCATE TABLE "' ,table_schema, '"."', TABLE_NAME, '" RESTART IDENTITY CASCADE;') trucate_table_cmd | ||
FROM INFORMATION_SCHEMA.TABLES | ||
WHERE table_schema = (SELECT current_schema()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,15 +2,18 @@ import chai, { expect } from 'chai'; | |
import chaiAsPromised from 'chai-as-promised'; | ||
import { db } from '../src'; | ||
import config from './databases/config'; | ||
import * as helper from './databases/helper'; | ||
chai.use(chaiAsPromised); | ||
|
||
|
||
const SUPPORTED_DB_CLIENTS = { | ||
mysql: { | ||
defaulTables: [ 'information_schema' ], | ||
truncateTablesSQL: helper.readSQLScript('mysql'), | ||
}, | ||
postgresql: { | ||
defaulTables: [ 'postgres' ], | ||
truncateTablesSQL: helper.readSQLScript('postgresql'), | ||
}, | ||
}; | ||
|
||
|
@@ -34,13 +37,13 @@ describe('db', () => { | |
}); | ||
|
||
describe('given is already connected', () => { | ||
beforeEach(() => { | ||
const serverInfo = { | ||
...config[dbClient], | ||
name: dbClient, | ||
client: dbClient, | ||
}; | ||
const serverInfo = { | ||
...config[dbClient], | ||
name: dbClient, | ||
client: dbClient, | ||
}; | ||
|
||
beforeEach(() => { | ||
return db.connect(serverInfo, serverInfo.database); | ||
}); | ||
|
||
|
@@ -65,14 +68,57 @@ describe('db', () => { | |
}); | ||
|
||
describe('.executeQuery', () => { | ||
it('should execute a select', async () => { | ||
const wrapQuery = require(`../src/db/clients/${dbClient}`).wrapQuery; | ||
const wrapQuery = require(`../src/db/clients/${dbClient}`).wrapQuery; | ||
|
||
beforeEach(() => { | ||
return Promise.all([ | ||
db.executeQuery(` | ||
INSERT INTO ${wrapQuery('users')} (username, email, password) | ||
VALUES ('maxcnunes', '[email protected]', '123456') | ||
`), | ||
db.executeQuery(` | ||
INSERT INTO ${wrapQuery('roles')} (name) | ||
VALUES ('developer') | ||
`), | ||
]); | ||
}); | ||
|
||
afterEach(() => helper.truncateAllTables(serverInfo, dbClientOpts)); | ||
|
||
it('should execute a single select query', async () => { | ||
const result = await db.executeQuery(`select * from ${wrapQuery('users')}`); | ||
expect(result).to.have.property('rows').to.eql([]); | ||
expect(result).to.have.deep.property('fields[0].name').to.eql('id'); | ||
expect(result).to.have.deep.property('fields[1].name').to.eql('username'); | ||
expect(result).to.have.deep.property('fields[2].name').to.eql('email'); | ||
expect(result).to.have.deep.property('fields[3].name').to.eql('password'); | ||
|
||
expect(result).to.have.deep.property('rows[0].id').to.eql(1); | ||
expect(result).to.have.deep.property('rows[0].username').to.eql('maxcnunes'); | ||
expect(result).to.have.deep.property('rows[0].password').to.eql('123456'); | ||
expect(result).to.have.deep.property('rows[0].email').to.eql('[email protected]'); | ||
}); | ||
|
||
it('should execute multiple select queries', async () => { | ||
const result = await db.executeQuery(` | ||
select * from ${wrapQuery('users')}; | ||
select * from ${wrapQuery('roles')}; | ||
`); | ||
|
||
expect(result).to.have.deep.property('fields[0][0].name').to.eql('id'); | ||
expect(result).to.have.deep.property('fields[0][1].name').to.eql('username'); | ||
expect(result).to.have.deep.property('fields[0][2].name').to.eql('email'); | ||
expect(result).to.have.deep.property('fields[0][3].name').to.eql('password'); | ||
|
||
expect(result).to.have.deep.property('rows[0][0].id').to.eql(1); | ||
expect(result).to.have.deep.property('rows[0][0].username').to.eql('maxcnunes'); | ||
expect(result).to.have.deep.property('rows[0][0].password').to.eql('123456'); | ||
expect(result).to.have.deep.property('rows[0][0].email').to.eql('[email protected]'); | ||
|
||
expect(result).to.have.deep.property('fields[1][0].name').to.eql('id'); | ||
expect(result).to.have.deep.property('fields[1][1].name').to.eql('name'); | ||
|
||
expect(result).to.have.deep.property('rows[1][0].id').to.eql(1); | ||
expect(result).to.have.deep.property('rows[1][0].name').to.eql('developer'); | ||
}); | ||
}); | ||
}); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters