Skip to content

Commit

Permalink
feat: init
Browse files Browse the repository at this point in the history
  • Loading branch information
BlackGlory committed Feb 25, 2021
0 parents commit 27a5d8b
Show file tree
Hide file tree
Showing 70 changed files with 7,320 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .eslintignore
@@ -0,0 +1,6 @@
node_modules
coverage

# Build
dist
lib
19 changes: 19 additions & 0 deletions .eslintrc.js
@@ -0,0 +1,19 @@
module.exports = {
root: true
, parser: '@typescript-eslint/parser'
, plugins: [
'@typescript-eslint'
]
, extends: [
'eslint:recommended'
, 'plugin:@typescript-eslint/recommended'
]
, rules: {
'no-constant-condition': 'off'
, 'no-async-promise-executor': 'off'
, '@typescript-eslint/no-inferrable-types': 'off'
, '@typescript-eslint/ban-types': 'off'
, '@typescript-eslint/no-empty-function': 'off'
, '@typescript-eslint/ban-ts-comment': 'off'
}
}
25 changes: 25 additions & 0 deletions .github/workflows/nodejs.yml
@@ -0,0 +1,25 @@
name: Node.js CI

on: [push]

jobs:
build:

runs-on: ubuntu-latest

strategy:
matrix:
node-version: [12.x]

steps:
- uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}
- run: npm install -g yarn
- run: yarn install
- run: yarn lint
- run: yarn test
env:
CI: true
62 changes: 62 additions & 0 deletions .gitignore
@@ -0,0 +1,62 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# nyc test coverage
.nyc_output

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules/
jspm_packages/

# Typescript v1 declaration files
typings/

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# dotenv environment variables file
.env

# Build
lib
dist
21 changes: 21 additions & 0 deletions LICENSE
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2021 BlackGlory <woshenmedoubuzhidao@blackglory.me>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
64 changes: 64 additions & 0 deletions README.md
@@ -0,0 +1,64 @@
# extra-sql-builder

## Install

```sh
npm install --save extra-sql-builder
# or
yarn add extra-sql-builder
```

## Motivation

It is difficult to create SQLs dynamically:
- Concatenating strings is ugly
- Traditional SQL Builders are not intuitive.
- ORMs are not intuitive, and have poor performance.

## Usage

```ts
import { INSERT_INTO, VALUES, text, sql } from 'extra-sql-builder'

// INSERT INTO my_table (id, value)
// VALUES (1, 'hello'), (2, 'world');

const cond = true

const result1 = sql(
INSERT_INTO('my_table', ['id', 'value'])
, VALUES(
['1', text('hello')],
cond && ['2', text('world')]
)
)

// or
const result2 = sql(
'INSERT INTO my_table (id, value)'
, VALUES(
['1', text('hello')],
cond && ['2', text('world')]
)
)

// or
const result3 = `
INSERT INTO my_table (id, value)
${VALUES(['1', text('hello')],
cond && ['2', text('world')])};
`

// or
const values = VALUES(['1', text('hello')])
if (cond) values.values.push(['2', text('world')])
const result4 = `
INSERT INTO my_table (id, value)
${values};
`
```

## What about SQL injection?

As long as you don't take user input as a parameter,
there will be no SQL injection vulnerability.
10 changes: 10 additions & 0 deletions __tests__/fragments/and.spec.ts
@@ -0,0 +1,10 @@
import { AND, And } from '@fragments/and'

describe('AND(condition: string) => And', () => {
it('return And', () => {
const result = AND(`id = 'test'`)

expect(result).toBeInstanceOf(And)
expect(`${result}`).toBe(`AND id = 'test'`)
})
})
10 changes: 10 additions & 0 deletions __tests__/fragments/delete-from.spec.ts
@@ -0,0 +1,10 @@
import { DELETE_FROM, DeleteFrom } from '@fragments/delete-from'

describe('DELETE_FROM(table: string) => DeleteFrom', () => {
it('return DeleteFrom', () => {
const result = DELETE_FROM('my_table')

expect(result).toBeInstanceOf(DeleteFrom)
expect(`${result}`).toBe('DELETE FROM my_table')
})
})
21 changes: 21 additions & 0 deletions __tests__/fragments/from.spec.ts
@@ -0,0 +1,21 @@
import { FROM, From } from '@fragments/from'

describe('FROM(...tables: string[]) => From', () => {
describe('one table', () => {
it('return From', () => {
const result = FROM('my_table')

expect(result).toBeInstanceOf(From)
expect(`${result}`).toBe('FROM my_table')
})
})

describe('more than one table', () => {
it('return From', () => {
const result = FROM('my_table1', 'my_table2')

expect(result).toBeInstanceOf(From)
expect(`${result}`).toBe('FROM my_table1, my_table2')
})
})
})
10 changes: 10 additions & 0 deletions __tests__/fragments/full-outer-join.spec.ts
@@ -0,0 +1,10 @@
import { FULL_OUTER_JOIN, FullOuterJoin } from '@fragments/full-outer-join'

describe('FULL_OUTER_JOIN(table: string): FullOuterJoin', () => {
it('return FullOuterJoin', () => {
const result = FULL_OUTER_JOIN('my_table')

expect(result).toBeInstanceOf(FullOuterJoin)
expect(`${result}`).toBe('FULL OUTER JOIN my_table')
})
})
19 changes: 19 additions & 0 deletions __tests__/fragments/group-by.spec.ts
@@ -0,0 +1,19 @@
import { GROUP_BY, GroupBy } from '@fragments/group-by'

describe('GROUP_BY(...fields: string[]): GroupBy', () => {
describe('one field', () => {
it('return GroupBy', () => {
const result = GROUP_BY('id')

expect(result).toBeInstanceOf(GroupBy)
expect(`${result}`).toBe('GROUP BY id')
})
})

describe('more than one field', () => {
const result = GROUP_BY('id1', 'id2')

expect(result).toBeInstanceOf(GroupBy)
expect(`${result}`).toBe('GROUP BY id1, id2')
})
})
10 changes: 10 additions & 0 deletions __tests__/fragments/having.spec.ts
@@ -0,0 +1,10 @@
import { HAVING, Having } from '@fragments/having'

describe('HAVING(condition: string): Having', () => {
it('return Having', () => {
const result = HAVING(`id = 'id'`)

expect(result).toBeInstanceOf(Having)
expect(`${result}`).toBe(`HAVING id = 'id'`)
})
})
10 changes: 10 additions & 0 deletions __tests__/fragments/inner-join.spec.ts
@@ -0,0 +1,10 @@
import { INNER_JOIN, InnerJoin } from '@fragments/inner-join'

describe('INNER_JOIN(table: string): InnerJoin', () => {
it('return InnerJoin', () => {
const result = INNER_JOIN('my_table')

expect(result).toBeInstanceOf(InnerJoin)
expect(`${result}`).toBe('INNER JOIN my_table')
})
})
21 changes: 21 additions & 0 deletions __tests__/fragments/insert-into.spec.ts
@@ -0,0 +1,21 @@
import { INSERT_INTO, InsertInto } from '@fragments/insert-into'

describe('INSERT_INTO(table, fields: string[]): InsertInto', () => {
describe('one field', () => {
it('return InsertInto', () => {
const result = INSERT_INTO('my_table', ['id'])

expect(result).toBeInstanceOf(InsertInto)
expect(`${result}`).toBe('INSERT INTO my_table (id)')
})
})

describe('more than one field', () => {
it('return InsertInto', () => {
const result = INSERT_INTO('my_table', ['id1', 'id2'])

expect(result).toBeInstanceOf(InsertInto)
expect(`${result}`).toBe('INSERT INTO my_table (id1, id2)')
})
})
})
10 changes: 10 additions & 0 deletions __tests__/fragments/into.spec.ts
@@ -0,0 +1,10 @@
import { INTO, Into } from '@fragments/into'

describe('INTO(table: string): Into', () => {
it('return Into', () => {
const result = INTO('my_table')

expect(result).toBeInstanceOf(Into)
expect(`${result}`).toBe('INTO my_table')
})
})
10 changes: 10 additions & 0 deletions __tests__/fragments/left-outer-join.spec.ts
@@ -0,0 +1,10 @@
import { LEFT_OUTER_JOIN, LeftOuterJoin } from '@fragments/left-outer-join'

describe('LEFT_OUTER_JOIN(table: string): LeftOuterJoin', () => {
it('return LeftOuterJoin', () => {
const result = LEFT_OUTER_JOIN('my_table')

expect(result).toBeInstanceOf(LeftOuterJoin)
expect(`${result}`).toBe('LEFT OUTER JOIN my_table')
})
})
10 changes: 10 additions & 0 deletions __tests__/fragments/limit.spec.ts
@@ -0,0 +1,10 @@
import { LIMIT, Limit } from '@fragments/limit'

describe('LIMIT(limit: number): Limit', () => {
it('return Limit', () => {
const result = LIMIT(10)

expect(result).toBeInstanceOf(Limit)
expect(`${result}`).toBe('LIMIT 10')
})
})
10 changes: 10 additions & 0 deletions __tests__/fragments/offset.spec.ts
@@ -0,0 +1,10 @@
import { OFFSET, Offset } from '@fragments/offset'

describe('OFFSET(offset: number): Offset', () => {
it('return Offset', () => {
const result = OFFSET(10)

expect(result).toBeInstanceOf(Offset)
expect(`${result}`).toBe('OFFSET 10')
})
})
10 changes: 10 additions & 0 deletions __tests__/fragments/on.spec.ts
@@ -0,0 +1,10 @@
import { ON, On } from '@fragments/on'

describe('ON(condition: string): On', () => {
it('return On', () => {
const result = ON(`id = 'id'`)

expect(result).toBeInstanceOf(On)
expect(`${result}`).toBe(`ON id = 'id'`)
})
})
10 changes: 10 additions & 0 deletions __tests__/fragments/or.spec.ts
@@ -0,0 +1,10 @@
import { OR, Or } from '@fragments/or'

describe('OR(condition: string): Or', () => {
it('return Or', () => {
const result = OR(`id = 'id'`)

expect(result).toBeInstanceOf(Or)
expect(`${result}`).toBe(`OR id = 'id'`)
})
})

0 comments on commit 27a5d8b

Please sign in to comment.