Skip to content

Commit 762fe22

Browse files
author
Pavel Romanov
committed
Usage example
1 parent 306aa61 commit 762fe22

File tree

3 files changed

+72
-45
lines changed

3 files changed

+72
-45
lines changed

.eslintignore

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
/node_modules/
2-
*.development.*
1+
/node_modules/

README.MD

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,76 @@ npm install multi-data-source
2222

2323
## Usage example
2424

25+
### Storage config example
26+
27+
```js
28+
const config = {
29+
'postgres': {
30+
'host': '127.0.0.1',
31+
'port': 5432,
32+
'database': 'postgres',
33+
'user': 'postgres',
34+
'password': '',
35+
'storageType': 'pg',
36+
},
37+
};
38+
39+
const storage = new Storage(config);
40+
```
41+
42+
### Query definition example
43+
44+
```js
45+
const getUsersByScore = {
46+
name: 'getUsersByScore',
47+
sql: `
48+
SELECT id {{nickname}}
49+
FROM users
50+
WHERE score > :score
51+
{{balance}}
52+
`,
53+
addons: {
54+
noGlobalJoin: {
55+
arg: {name: 'global', value: false},
56+
sql: 'INNER JOIN mega_jackpot_halls AS mjh USING(hall_id)',
57+
},
58+
nickname: {
59+
arg: {name: 'needNickname', value: true},
60+
sql: ', nickname',
61+
},
62+
balance: {
63+
arg: {name: 'balanceCondition', value: true},
64+
sql: 'AND balance >= :balance',
65+
},
66+
},
67+
};
68+
```
69+
70+
### Query execution example
71+
72+
```js
73+
let connection;
74+
try {
75+
connection = await storage.getConnection('postgres');
76+
77+
const data = await connection.query(getUsersByScore,
78+
{
79+
score: 100,
80+
balance: 50,
81+
},
82+
{
83+
templateParams: {balanceCondition: true},
84+
}
85+
);
86+
console.log(data);
87+
} catch (error) {
88+
console.error(error);
89+
} finally {
90+
if (connection) {
91+
await connection.release;
92+
}
93+
}
94+
```
2595

2696
## Author
2797

package-lock.json

Lines changed: 1 addition & 43 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)