Skip to content

Commit

Permalink
Convert to typescript
Browse files Browse the repository at this point in the history
  • Loading branch information
dynajoe committed Feb 24, 2017
1 parent c7f1729 commit b849b1e
Show file tree
Hide file tree
Showing 44 changed files with 1,710 additions and 1,263 deletions.
5 changes: 5 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,8 @@ insert_final_newline = true

[*.md]
trim_trailing_whitespace = false
indent_size = 4

[*.sql]
trim_trailing_whitespace = true
indent_size = 3
21 changes: 0 additions & 21 deletions .eslintrc.json

This file was deleted.

4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
node_modules/
npm-debug.log
*.map
src/**/*.js
src/**/*.map
3 changes: 2 additions & 1 deletion .npmignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
sql/
sql/
.vscode/
28 changes: 28 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
// Use IntelliSense to learn about possible Node.js debug attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Test via NPM",
"runtimeExecutable": "npm",
"windows": {
"runtimeExecutable": "npm.cmd"
},
"env": {
"SKIP_LINT": "true",
"MOCHA_BAIL": "false"
},
"runtimeArgs": [
"run-script",
"test-debugging"
],
"port": 5858,
"cwd": "${workspaceRoot}",
"timeout": 60000
}
]
}
15 changes: 15 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"files.exclude": {
"src/**/*.js": true,
"src/**/*.js.map": true,
"node_modules": true,
"node_shrinkwrap": true
},

"search.exclude": {
"src/**/*.js": true,
"src/**/*.js.map": true
}
,
"typescript.tsdk": "./node_modules/typescript/lib"
}
158 changes: 79 additions & 79 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,79 +1,79 @@
# TinyPg

I liked [MassiveJS](https://github.com/robconery/massive-js) but wanted less. This module allows for one to load SQL files from disk and execute using [node-postgres](https://github.com/brianc/node-postgres). It also allows for specifying arguments to SQL files using property names.

## Disclaimer
I created this project to use for my special case. It probably won't work for yours. It also is not very resilient to malformed SQL or variable names that don't match a very simple regex. I don't recommend using this module.

## Usage

```javascript
// assumes file system
// ./sql_files
// fetch_user_by_name.sql
// subdir_name/
// fetch_foo.sql
var Tiny = require('tinypg');

var t = new Tiny({
connection_string: "postgres://postgres@localhost:5432/mydb",
root_dir: './sql_files',
snake: true // camel: true is default
});

// Example 1
t.sql.fetch_user_by_name({ name: 'Joe' })
.then(function () { })
.fail(function () { });

// Example of file in a sub directory
t.sql.subdir_name.fetch_foo({ a: 1, b: 2, c: 'foo' })
.then(function () { })
.fail(function () { });
```

fetch_user_by_email.sql
```sql
SELECT * FROM user WHERE name = :name;
```

subdir_name/fetch_foo.sql
```sql
SELECT *
FROM foo
INNER JOIN bar ON foo.bar_id = bar.id
WHERE bar.a = :a
AND foo.b = :b
AND foo.something = :c;
```
This example requires that there be a folder relative to the current file called sql_files with a file named `fetch_user_by_email.sql`. Subdirectories are represented as nested objects on the `sql` root object.

### Transactions

The `.transaction` method provides a context that ensures every command executed against that
context will be run in the same transaction. Nested transactions are supported (which really just means
that COMMIT/ROLLBACK will be left up to the outermost transaction).

If you do not use the provided context, those queries cannot be guaranteed to use the same transaction.

```javascript
var t = new Tiny({
connection_string: "postgres://postgres@localhost:5432/mydb",
root_dir: './sql_files',
snake: true // camel: true is default
});

t.transaction(function (ctx) {
return ctx.query('INSERT INTO ' + dbName + '.a (text) VALUES (:text)', {
text: '1'
})
.then(function (res) {
return ctx.transaction(function (ctx2) {
return ctx2.query('INSERT INTO ' + dbName + '.a (text) VALUES (:text)', {
text: '2'
});
});
});
})

```
# TinyPg

I liked [MassiveJS](https://github.com/robconery/massive-js) but wanted less. This module allows for one to load SQL files from disk and execute using [node-postgres](https://github.com/brianc/node-postgres). It also allows for specifying arguments to SQL files using property names.

## Disclaimer
I created this project to use for my special case. It probably won't work for yours. It also is not very resilient to malformed SQL or variable names that don't match a very simple regex. I don't recommend using this module.

## Usage

```javascript
// assumes file system
// ./sql_files
// fetch_user_by_name.sql
// subdir_name/
// fetch_foo.sql
var Tiny = require('tinypg');

var t = new Tiny({
connection_string: "postgres://postgres@localhost:5432/mydb",
root_dir: './sql_files',
snake: true // camel: true is default
});

// Example 1
t.sql.fetch_user_by_name({ name: 'Joe' })
.then(function () { })
.fail(function () { });

// Example of file in a sub directory
t.sql.subdir_name.fetch_foo({ a: 1, b: 2, c: 'foo' })
.then(function () { })
.fail(function () { });
```

fetch_user_by_email.sql
```sql
SELECT * FROM user WHERE name = :name;
```

subdir_name/fetch_foo.sql
```sql
SELECT *
FROM foo
INNER JOIN bar ON foo.bar_id = bar.id
WHERE bar.a = :a
AND foo.b = :b
AND foo.something = :c;
```
This example requires that there be a folder relative to the current file called sql_files with a file named `fetch_user_by_email.sql`. Subdirectories are represented as nested objects on the `sql` root object.

### Transactions

The `.transaction` method provides a context that ensures every command executed against that
context will be run in the same transaction. Nested transactions are supported (which really just means
that COMMIT/ROLLBACK will be left up to the outermost transaction).

If you do not use the provided context, those queries cannot be guaranteed to use the same transaction.

```javascript
var t = new Tiny({
connection_string: "postgres://postgres@localhost:5432/mydb",
root_dir: './sql_files',
snake: true // camel: true is default
});

t.transaction(function (ctx) {
return ctx.query('INSERT INTO ' + dbName + '.a (text) VALUES (:text)', {
text: '1'
})
.then(function (res) {
return ctx.transaction(function (ctx2) {
return ctx2.query('INSERT INTO ' + dbName + '.a (text) VALUES (:text)', {
text: '2'
});
});
});
})

```
2 changes: 1 addition & 1 deletion examples/sql_files/fetch_user_by_name.sql
Original file line number Diff line number Diff line change
@@ -1 +1 @@
SELECT * FROM users WHERE name = :name;
SELECT * FROM users WHERE name = :name
18 changes: 0 additions & 18 deletions gulpfile.js

This file was deleted.

29 changes: 22 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,18 @@
"version": "1.1.2",
"description": "Easy way to call sql files using postgres.",
"main": "index.js",
"config": {
"reporter": "spec",
"reporter_options": ""
},
"typescript": {
"definition": "index.d.ts"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"test": "npm run build && NODE_ENV=test ./node_modules/mocha/bin/mocha 'src/test/**/*.js' --full-trace -t 15000 $MOCHA_FLAGS --require source-map-support/register --reporter $npm_package_config_reporter $npm_package_config_reporter_options",
"test-debugging": "npm run build && NODE_ENV=test ./node_modules/mocha/bin/mocha 'src/test/**/*.js' --debug-brk=5858 --no-timeouts --colors --full-trace -t 15000 $MOCHA_FLAGS --require source-map-support/register",
"clean": "rimraf 'src/**/*.js' 'src/**/*.map'",
"build": "npm run clean && tsc"
},
"repository": {
"type": "git",
Expand All @@ -30,14 +40,19 @@
"glob": "^6.0.1",
"lodash": "4.0.0",
"node-uuid": "^1.4.7",
"pg": "^4.4.3",
"pg-format": "^1.0.1",
"q": "^1.4.1"
"pg": "6.1.2",
"pg-format": "^1.0.1"
},
"devDependencies": {
"@types/chai": "3.4.34",
"@types/lodash": "4.14.52",
"@types/mocha": "2.2.39",
"@types/node": "7.0.5",
"@types/pg": "6.1.36",
"chai": "^3.4.1",
"gulp": "^3.9.0",
"gulp-spawn-mocha": "^2.2.2",
"mocha": "^2.3.4"
"mocha": "^3.2.0",
"rimraf": "^2.6.0",
"source-map-support": "0.4.11",
"typescript": "2.1.5"
}
}
Loading

0 comments on commit b849b1e

Please sign in to comment.