From 7edc4387ee3adaa8be61455d7f8acf1daea13757 Mon Sep 17 00:00:00 2001 From: bbr-yp Date: Tue, 2 Jun 2020 09:38:03 +0200 Subject: [PATCH] add helpers export --- .github/workflows/npm-publish.yml | 21 +++++++++++++++++++ README.md | 24 +++++++++++++++++++++- package-lock.json | 19 ++++++++++++----- package.json | 2 +- src/helpers.js | 21 +++++++++++++++++++ src/index.js | 8 +++++--- src/model/model.js | 12 ++--------- tests/features/model/getter/getter.test.js | 5 ++++- 8 files changed, 91 insertions(+), 21 deletions(-) create mode 100644 .github/workflows/npm-publish.yml create mode 100644 src/helpers.js diff --git a/.github/workflows/npm-publish.yml b/.github/workflows/npm-publish.yml new file mode 100644 index 0000000..74ff85c --- /dev/null +++ b/.github/workflows/npm-publish.yml @@ -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 diff --git a/README.md b/README.md index bb51b65..33d72a0 100644 --- a/README.md +++ b/README.md @@ -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) @@ -24,7 +25,7 @@ Simple ORM for nodejs, based on [sql-template-string](https://github.com/felixfb const vegeData = VegeData({ filename: }); ``` -## Get connection +## Connection __db__ is an [sqlite](https://github.com/kriasoft/node-sqlite#readme) object @@ -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 diff --git a/package-lock.json b/package-lock.json index ee9e053..7596a0c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -3151,6 +3151,7 @@ "version": "3.0.1", "resolved": "https://registry.npmjs.org/eslint-plugin-es/-/eslint-plugin-es-3.0.1.tgz", "integrity": "sha512-GUmAsJaN4Fc7Gbtl8uOBlayo2DqhwWvEzykMHSCZHU3XdJ+NSzzZcVhXh3VxX5icqQ+oQdIEawXX8xkR3mIFmQ==", + "dev": true, "requires": { "eslint-utils": "^2.0.0", "regexpp": "^3.0.0" @@ -3286,6 +3287,7 @@ "version": "11.1.0", "resolved": "https://registry.npmjs.org/eslint-plugin-node/-/eslint-plugin-node-11.1.0.tgz", "integrity": "sha512-oUwtPJ1W0SKD0Tr+wqu92c5xuCeQqB3hSCHasn/ZgjFdA9iDGNkNf2Zi9ztY7X+hNuMib23LNGRm6+uN+KLE3g==", + "dev": true, "requires": { "eslint-plugin-es": "^3.0.0", "eslint-utils": "^2.0.0", @@ -3298,12 +3300,14 @@ "ignore": { "version": "5.1.8", "resolved": "https://registry.npmjs.org/ignore/-/ignore-5.1.8.tgz", - "integrity": "sha512-BMpfD7PpiETpBl/A6S498BaIJ6Y/ABT93ETbby2fP00v4EbvPBXWEoaR1UBPKs3iR53pJY7EtZk5KACI57i1Uw==" + "integrity": "sha512-BMpfD7PpiETpBl/A6S498BaIJ6Y/ABT93ETbby2fP00v4EbvPBXWEoaR1UBPKs3iR53pJY7EtZk5KACI57i1Uw==", + "dev": true }, "semver": { "version": "6.3.0", "resolved": "https://registry.npmjs.org/semver/-/semver-6.3.0.tgz", - "integrity": "sha512-b39TBaTSfV6yBrapU89p5fKekE2m/NwnDocOVruQFS1/veMgdzuPcnOM34M6CwxW8jH/lxEa5rBoDeUwu5HHTw==" + "integrity": "sha512-b39TBaTSfV6yBrapU89p5fKekE2m/NwnDocOVruQFS1/veMgdzuPcnOM34M6CwxW8jH/lxEa5rBoDeUwu5HHTw==", + "dev": true } } }, @@ -3327,6 +3331,7 @@ "version": "2.0.0", "resolved": "https://registry.npmjs.org/eslint-utils/-/eslint-utils-2.0.0.tgz", "integrity": "sha512-0HCPuJv+7Wv1bACm8y5/ECVfYdfsAm9xmVb7saeFlxjPYALefjhbYoCkBjPdPzGH8wWyTpAez82Fh3VKYEZ8OA==", + "dev": true, "requires": { "eslint-visitor-keys": "^1.1.0" } @@ -3334,7 +3339,8 @@ "eslint-visitor-keys": { "version": "1.1.0", "resolved": "https://registry.npmjs.org/eslint-visitor-keys/-/eslint-visitor-keys-1.1.0.tgz", - "integrity": "sha512-8y9YjtM1JBJU/A9Kc+SbaOV4y29sSWckBwMHa+FGtVj5gN/sbnKDf6xJUl+8g7FAij9LVaP8C24DUiH/f/2Z9A==" + "integrity": "sha512-8y9YjtM1JBJU/A9Kc+SbaOV4y29sSWckBwMHa+FGtVj5gN/sbnKDf6xJUl+8g7FAij9LVaP8C24DUiH/f/2Z9A==", + "dev": true }, "espree": { "version": "7.0.0", @@ -5774,7 +5780,8 @@ "path-parse": { "version": "1.0.6", "resolved": "https://registry.npmjs.org/path-parse/-/path-parse-1.0.6.tgz", - "integrity": "sha512-GSmOT2EbHrINBf9SR7CDELwlJ8AENk3Qn7OikK4nFYAu3Ote2+JYNVvkpAEQm3/TLNEJFD/xZJjzyxg3KBWOzw==" + "integrity": "sha512-GSmOT2EbHrINBf9SR7CDELwlJ8AENk3Qn7OikK4nFYAu3Ote2+JYNVvkpAEQm3/TLNEJFD/xZJjzyxg3KBWOzw==", + "dev": true }, "path-type": { "version": "2.0.0", @@ -6074,7 +6081,8 @@ "regexpp": { "version": "3.1.0", "resolved": "https://registry.npmjs.org/regexpp/-/regexpp-3.1.0.tgz", - "integrity": "sha512-ZOIzd8yVsQQA7j8GCSlPGXwg5PfmA1mrq0JP4nGhh54LaKN3xdai/vHUDu74pKwV8OxseMS65u2NImosQcSD0Q==" + "integrity": "sha512-ZOIzd8yVsQQA7j8GCSlPGXwg5PfmA1mrq0JP4nGhh54LaKN3xdai/vHUDu74pKwV8OxseMS65u2NImosQcSD0Q==", + "dev": true }, "regexpu-core": { "version": "4.7.0", @@ -6231,6 +6239,7 @@ "version": "1.17.0", "resolved": "https://registry.npmjs.org/resolve/-/resolve-1.17.0.tgz", "integrity": "sha512-ic+7JYiV8Vi2yzQGFWOkiZD5Z9z7O2Zhm9XMaTxdJExKasieFCr+yXZ/WmXsckHiKl12ar0y6XiXDx3m4RHn1w==", + "dev": true, "requires": { "path-parse": "^1.0.6" } diff --git a/package.json b/package.json index 6eedcd7..0719033 100644 --- a/package.json +++ b/package.json @@ -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" @@ -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" diff --git a/src/helpers.js b/src/helpers.js new file mode 100644 index 0000000..7ad56d5 --- /dev/null +++ b/src/helpers.js @@ -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; +}; + diff --git a/src/index.js b/src/index.js index a5c39ba..97b60cb 100644 --- a/src/index.js +++ b/src/index.js @@ -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, @@ -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, diff --git a/src/model/model.js b/src/model/model.js index ce148c9..b5b4c47 100644 --- a/src/model/model.js +++ b/src/model/model.js @@ -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)); @@ -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 }); diff --git a/tests/features/model/getter/getter.test.js b/tests/features/model/getter/getter.test.js index cd3d9d2..1bfa60b 100644 --- a/tests/features/model/getter/getter.test.js +++ b/tests/features/model/getter/getter.test.js @@ -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(); @@ -24,6 +24,9 @@ describe('Model->getters', () => { ('sangoku', 'sayan'), ('bulma', 'human'); `); + }); + + beforeEach(async () => { model = vegeData.model.init({ table: 'characters',