Skip to content

Commit

Permalink
add order by default value
Browse files Browse the repository at this point in the history
  • Loading branch information
bbr-yp authored and BibiFock committed Jul 28, 2020
1 parent 1de06d7 commit 6c3e32c
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 17 deletions.
21 changes: 11 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@ Simple ORM for nodejs, based on [sql-template-string](https://github.com/felixfb

<!-- TOC -->
- [Usage](#usage)
- [Connect to database](#usage-connection)
- [Connection](#usage-connection-get)
- [Migrations](#usage-migrations)
- [Model](#usage-model)
- [Definition](#usage-model-definition)
- [Usage](#usage-model-usage)
- [Helpers](#usage-helpers)
- [Devlopment](#dev)
- [Installation](#dev-install)
- [Tests](#dev-tests)
- [Connect to database](#connect-to-database)
- [Connection](#connection)
- [Migrations](#migrations)
- [Model](#model)
- [Definition](#definition)
- [Usage](#usage-1)
- [Helpers](#helpers)
- [Devlopment](#devlopment)
- [Installation](#tnstallation)
- [Tests](#tests)
- [License](#license)

<!-- TOC END -->
Expand Down Expand Up @@ -52,6 +52,7 @@ const model = vegeData.model.init({
fields: ['id', 'name', 'status', ... ],
table: <my table>,
primaryKey: 'rowId' // by default
orderBy: 'rowId ASC' // by default
})
```

Expand Down
2 changes: 1 addition & 1 deletion 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
@@ -1,6 +1,6 @@
{
"name": "vege-data",
"version": "1.0.0",
"version": "1.0.1",
"description": "Simple SQLLite ORM for nodejs based on sql-template-strings and sqlite",
"main": "./lib/index.js",
"scripts": {
Expand Down
11 changes: 8 additions & 3 deletions src/model/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ const makeQueries = ({ fields, table }) => {
};
};

const makeGetter = ({ exec, queries, primaryKey }) => ({
const makeGetter = ({ exec, queries, primaryKey, orderBy }) => ({
all: () => exec(
'all',
queries.select()
queries.select().append(orderBy)
),
get: id => exec('get', queries.select()
.append(' WHERE ' + primaryKey + '=')
Expand All @@ -36,6 +36,7 @@ const makeGetter = ({ exec, queries, primaryKey }) => ({
.append(' WHERE ' + primaryKey + ' IN (')
.append(concatValues(ids))
.append(')')
.append(orderBy);

return exec('all', query);
},
Expand All @@ -44,6 +45,7 @@ const makeGetter = ({ exec, queries, primaryKey }) => ({
queries.select()
.append(' WHERE ' + field + ' =')
.append(SQL`${value}`)
.append(orderBy)
)
});

Expand Down Expand Up @@ -74,6 +76,7 @@ const makeStore = ({ exec, queries, fields }) => {
* @param {array} _.fields - fields list
* @param {string} _.table - table name
* @param {string} [_.primaryKey=rowId] primary key
* @param {string} [_.orderBy=rowId] primary key
*
* @return {object}
*/
Expand All @@ -82,14 +85,16 @@ const init = ({ connect, log }) => (config) => {
fields,
table,
primaryKey = 'rowid',
orderBy = 'rowid'
} = config;
const exec = makeExec({ connect, log })
const defaultOrder = ` ORDER BY ${orderBy}`;
const queries = makeQueries({ fields, table });

return {
config,
queries,
...makeGetter({ exec, queries, primaryKey }),
...makeGetter({ exec, queries, primaryKey, orderBy: defaultOrder }),
...makeStore({ exec, queries, fields })
};
};
Expand Down
33 changes: 31 additions & 2 deletions tests/features/model/getter/getter.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ describe('Model->getters', () => {

describe('getAll', () => {
const couple = [
{ name: 'bulma', type: 'human' },
{ name: 'vegeta', type:'sayan' }
{ name: 'vegeta', type:'sayan' },
{ name: 'bulma', type: 'human' }
];

it('should return all asked ids', () => {
Expand All @@ -90,4 +90,33 @@ describe('Model->getters', () => {
]);
});
});

describe('orderBy, should sort answer for multiple response', () => {
describe.each([
'ASC',
'DESC'
])('%s', (order) => {
let model
beforeAll(() => {
model = vegeData.model.init({
table: 'characters',
fields: ['name', 'type'],
orderBy: `name ${order}`,
primaryKey: 'name'
});
});

it.each([
['all', '', []],
['getAll', `WHERE name IN ('vegeta', 'goku')`, [['vegeta', 'goku']]],
['findBy', `WHERE type = 'sayan'`, ['type','sayan']]
])('%s', async (func, conds, args) => {
const wanted = await db.all(`SELECT name, type FROM characters ${conds} ORDER BY name ${order}`),

result = await model[func](...args);

expect(result).toEqual(wanted);
});
});
});
});

0 comments on commit 6c3e32c

Please sign in to comment.