Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0299b3e

Browse files
committed
add raw-queries recipe to new docs
1 parent 7b2bb1b commit 0299b3e

File tree

4 files changed

+65
-16
lines changed

4 files changed

+65
-16
lines changed

newDoc/.vuepress/config.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ module.exports = {
1515
{
1616
text: 'API Reference',
1717
link: '/api/model',
18+
},
19+
20+
{
21+
text: 'Recipe Book',
22+
link: '/recipes/raw-queries',
1823
}
1924
],
2025

@@ -45,6 +50,14 @@ module.exports = {
4550
'query-builder',
4651
'types'
4752
]
53+
}],
54+
55+
'/recipes/': [{
56+
title: 'Recipes',
57+
collapsable: false,
58+
children: [
59+
'raw-queries'
60+
]
4861
}]
4962
}
5063
},

newDoc/guide/contributing.md

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,25 +26,17 @@ For a pull request to get merged it needs to have the following things:
2626

2727
## Development setup
2828

29-
1. Fork objection in github
29+
1. **Fork objection in github**
3030

31-
2. Clone objection
32-
33-
3. Install MySQL and PostgreSQL or alternatively run `docker-compose up` in the repo root
34-
35-
4. Create test users and databases
36-
37-
5. Run `npm test` in objection's root to see if everything works.
38-
39-
6. Run `npm run docs:dev` and goto http://localhost:8080 to see the generated documentation site when you change the markdown files in the `doc` folder.
40-
41-
cloning:
31+
2. **Clone objection**
4232

4333
```shell
4434
git clone [email protected]:<your-account>/objection.js.git objection
4535
```
4636

47-
creating users and databases for the tests:
37+
3. **Install MySQL and PostgreSQL or alternatively run `docker-compose up` in the repo root**
38+
39+
4. **Create test users and databases**
4840

4941
```shell
5042
psql -U postgres -c "CREATE USER objection SUPERUSER"
@@ -54,6 +46,10 @@ mysql -u root -e "GRANT ALL PRIVILEGES ON *.* TO objection"
5446
mysql -u root -e "CREATE DATABASE objection_test"
5547
```
5648

49+
5. **Run `npm test` in objection's root to see if everything works.**
50+
51+
6. **Run `npm run docs:dev` and goto http://localhost:8080 to see the generated documentation site when you change the markdown files in the `doc` folder.**
52+
5753
You can run the tests on a subset of databases by setting the `DATABASES` env variable
5854

5955
```shell

newDoc/recipes/README.md

Lines changed: 0 additions & 3 deletions
This file was deleted.

newDoc/recipes/raw-queries.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Raw queries
2+
3+
To mix raw SQL with queries, use the [raw](/api/objection.html#raw) function from the main module or the [Model.raw()](/api/model.html#static-raw) method of any [Model](/api/model.html) subclass. The only difference between these two is that the [raw](/api/objection.html#raw) function from the main module doesn't depend on knex where as [Model.raw()](/api/model.html#static-raw) will throw if the model doesn't have a knex instance installed. Both of these functions work just like the [knex's raw method](http://knexjs.org/#Raw). And of course you can just use [knex.raw()](http://knexjs.org/#Raw).
4+
5+
There are also some helper methods such as [whereRaw](/api/query-builder.html#whereraw) in the [QueryBuilder](/api/query-builder.html).
6+
7+
## Examples
8+
9+
```js
10+
const { raw } = require('objection');
11+
12+
const childAgeSums = await Person
13+
.query()
14+
.select(raw('coalesce(sum(??), 0) as ??', ['age', 'childAgeSum']))
15+
.where(raw(`?? || ' ' || ??`, 'firstName', 'lastName'), 'Arnold Schwarzenegger')
16+
.orderBy(raw('random()'));
17+
18+
console.log(childAgeSums[0].childAgeSum);
19+
```
20+
21+
```js
22+
const childAgeSums = await Person
23+
.query()
24+
.select(raw('coalesce(sum(??), 0) as ??', ['age', 'childAgeSum']))
25+
.groupBy('parentId');
26+
27+
console.log(childAgeSums[0].childAgeSum);
28+
```
29+
30+
Raw binding arguments can be other [raw](/api/objection.html#raw) instances, [QueryBuilders](/api/query-builder.html) or pretty much anything you can think of.
31+
32+
```js
33+
const { raw, ref } = require('objection');
34+
35+
const childAgeSums = await Person
36+
.query()
37+
.alias('p')
38+
.select(raw('array(?) as childIds', [
39+
Person.query()
40+
.select('id')
41+
.where('id', ref('p.parentId')).
42+
]);
43+
```

0 commit comments

Comments
 (0)