Skip to content

Commit

Permalink
add helpers export
Browse files Browse the repository at this point in the history
  • Loading branch information
bbr-yp authored and BibiFock committed Jun 4, 2020
1 parent 171924c commit 7edc438
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 21 deletions.
21 changes: 21 additions & 0 deletions .github/workflows/npm-publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: Node.js Package

on:
push:
branches: [master]
pull_request:
branches: [master]
release:
types: [created]

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
with:
node-version: 12
- run: npm ci
- run: npm run build
- run: npm run validate
24 changes: 23 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Simple ORM for nodejs, based on [sql-template-string](https://github.com/felixfb
- [Model](#usage-model)
- [Definition](#usage-model-definition)
- [Usage](#usage-model-usage)
- [Helpers](#usage-helpers)
- [Devlopment](#dev)
- [Installation](#dev-install)
- [Tests](#dev-tests)
Expand All @@ -24,7 +25,7 @@ Simple ORM for nodejs, based on [sql-template-string](https://github.com/felixfb
const vegeData = VegeData({ filename: <your sqlite filename> });
```

## Get connection
## Connection

__db__ is an [sqlite](https://github.com/kriasoft/node-sqlite#readme) object

Expand Down Expand Up @@ -69,6 +70,27 @@ model.save(data);
models.saveAll(datas)
```


## Helpers

```js
import { helpers } from 'vege-data';

// concat value
model.queries.select()
.append(' WHERE id IN (')
.append(helpers.concatValue([1, 2, 3]))

// custom element
const items = [{ id: 1}, { id: 2 }];
model.queries.select()
.append(' WHERE id IN (')
.append(helpers.concatValue(items, ({ id }) => id))
.append(')');

```


# Devlopment
## Installation

Expand Down
19 changes: 14 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
},
"homepage": "https://github.com/BibiFock/vege-data#readme",
"dependencies": {
"eslint-plugin-node": "11.1.0",
"sql-template-strings": "2.2.2",
"sqlite": "4.0.9",
"sqlite3": "4.2.0"
Expand All @@ -58,6 +57,7 @@
"eslint-config-standard": "14.1.1",
"eslint-plugin-import": "2.20.2",
"eslint-plugin-jest": "23.13.2",
"eslint-plugin-node": "11.1.0",
"eslint-plugin-promise": "4.2.1",
"jest": "26.0.1",
"rimraf": "3.0.2"
Expand Down
21 changes: 21 additions & 0 deletions src/helpers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import SQL from 'sql-template-strings';

/**
* transform array of items.
* [1, 2, 3] => SQL'${1}, ${2}, ${3}';
*
* @param {array} items
* @param {func} each function call for each elements
*
* @return object sql template string
*/
export const concatValues = (items, each) => {
const query = SQL``;
items.forEach(
(item, index) => query
.append(index > 0 ? ', ' : '')
.append(each ? each(item) : SQL`${item}`)
);
return query;
};

8 changes: 5 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ import { open } from 'sqlite';

import model from './model/model';

import { concatValues } from './helpers';

export { concatValues };

export default ({ filename }) => {
const dbParams = {
filename,
Expand All @@ -21,9 +25,7 @@ export default ({ filename }) => {
}

const migrate = () => connect()
.then(db => db.migrate({ force: 'last' }))
// eslint-disable-next-line no-console
.catch(e => console.error('Failed: ' + e.message));
.then(db => db.migrate({ force: 'last' }));

return {
connect,
Expand Down
12 changes: 2 additions & 10 deletions src/model/model.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
import SQL from 'sql-template-strings';

const concatValues = (items, each) => {
const query = SQL``;
items.forEach(
(item, index) => query
.append(index > 0 ? ', ' : '')
.append(each ? each(item) : SQL`${item}`)
);
return query;
};
import { concatValues } from '../helpers';

const makeExec = (connect) => (action, query) => connect()
.then(db => db[action](query));
Expand Down Expand Up @@ -83,7 +75,7 @@ const init = (connect) => (config) => {
const {
fields,
table,
primaryKey = 'rowId',
primaryKey = 'rowid',
} = config;
const exec = makeExec(connect)
const queries = makeQueries({ fields, table });
Expand Down
5 changes: 4 additions & 1 deletion tests/features/model/getter/getter.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { getTmpDb } from 'utils';

describe('Model->getters', () => {
let vegeData, db, model;
beforeEach(async () => {
beforeAll(async () => {
vegeData = VegeData({ filename: getTmpDb() });

db = await vegeData.connect();
Expand All @@ -24,6 +24,9 @@ describe('Model->getters', () => {
('sangoku', 'sayan'),
('bulma', 'human');
`);
});

beforeEach(async () => {

model = vegeData.model.init({
table: 'characters',
Expand Down

0 comments on commit 7edc438

Please sign in to comment.