Skip to content

Commit

Permalink
Join columns as value in Wheres v0.1.2
Browse files Browse the repository at this point in the history
  • Loading branch information
chrissmejia committed Sep 23, 2018
1 parent 3f0f35c commit 2bf748d
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 52 deletions.
33 changes: 30 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -298,10 +298,37 @@ DELETE FROM `sessions` INNER JOIN `users` ON `sessions`.`user` = `users`.`id` WH

`kind` Type of Join to apply E.g.: INNER, LEFT.

### Advanced

#### Literal strings ####
You can send an unprepared strings as values in Wheres adding a double `\\` at the start of the condition:
### Delete on joined condition

```typescript
sessionsTable.join([{
keyField: usersTwoTable.user,
fields: ["username"],
kind: "INNER"
}]).delete({
username: "users__username"
}).then((data: any) => {
console.log(data);
}).catch((err: any) => {
console.error(err)
});
```

Query executed:
```sql
DELETE FROM `usersTwo` INNER JOIN `users` ON `usersTwo`.`user` = `users`.`id` WHERE `usersTwo`.`username` = `users`.`username`;
```

#### Params ####
`keyField` Model foreign key.

`kind` Type of Join to apply E.g.: INNER, LEFT.

## Advanced

### Literal strings
You can send an unprepared strings as values in Wheres adding a double `\\` at the start of the value:

```typescript
sessionsTable.getAll({
Expand Down
14 changes: 11 additions & 3 deletions model.js

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

2 changes: 1 addition & 1 deletion model.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@unicoderns/orm",
"version": "0.1.1",
"version": "0.1.2",
"description": "Unicoderns Object/Relational Mapping",
"author": {
"name": "Unicoderns SA",
Expand Down
13 changes: 10 additions & 3 deletions sources/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -296,9 +296,15 @@ export class Model {
} else {
let joinkeys = item.split("__");
if (joinkeys.length == 2) {
sql = sql + "`" + joinkeys[0] + "`.`" + joinkeys[1] + "` = ?";
sql = sql + "`" + joinkeys[0] + "`.`" + joinkeys[1];
} else {
sql = sql + "`" + this.tableName + "`.`" + item + "` = ?";
sql = sql + "`" + this.tableName + "`.`" + item;
}
joinkeys = String(where[item]).split("__");
if (joinkeys.length == 2) {
sql = sql + "` = `" + joinkeys[0] + "`.`" + joinkeys[1] + "`";
} else {
sql = sql + "` = ?"
}
}
if (id < array.length - 1) {
Expand All @@ -307,7 +313,8 @@ export class Model {
});
// getting values
filteredKeys.forEach((item: string) => {
if (String(where[item]).charAt(0) != "\\") {
let joinkeys = String(where[item]).split("__");
if ((String(where[item]).charAt(0) != "\\") && (joinkeys.length != 2)) {
values.push(where[item]);
}
});
Expand Down
39 changes: 17 additions & 22 deletions sources/tests/join.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,29 +163,24 @@ describe('Joins', () => {
console.error(err)
});
});

/*
it('INNER join update usersTwo with literal from users', () => {
var expected = {
sql: 'SELECT `sessions`.`id`, `sessions`.`created`, `sessions`.`ip`, `sessions`.`user`, `users`.`username` AS `users__username`, `users`.`email` AS `users__email`, `users`.`firstName` AS `users__firstName`, `users`.`lastName` AS `users__lastName` FROM `sessions` LEFT JOIN `users` ON `sessions`.`user` = `users`.`id` WHERE `users`.`id` = ?;',
values: [3]
};
usersTwoTable.returnQuery().join([{
keyField: usersTwoTable.user,
fields: ["username"],
kind: "INNER"
}]).update({
data: {
username: "users__username"
},
where: "*"
}).then((query: Models.Query) => {
expect(query).toEqual(expected);
}).catch((err: any) => {
console.error(err)
});

it('INNER join delete usersTwo with literal from users', () => {
var expected = {
sql: 'DELETE FROM `usersTwo` INNER JOIN `users` ON `usersTwo`.`user` = `users`.`id` WHERE `usersTwo`.`username` = `users`.`username`;',
values: []
};
usersTwoTable.returnQuery().join([{
keyField: usersTwoTable.user,
fields: ["username"],
kind: "INNER"
}]).delete({
username: "users__username"
}).then((query: Models.Query) => {
expect(query).toEqual(expected);
}).catch((err: any) => {
console.error(err)
});
*/
});

/*
it('INNER join update usersTwo with literal from users', () => {
Expand Down
31 changes: 13 additions & 18 deletions tests/join.test.js

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

2 changes: 1 addition & 1 deletion tests/join.test.js.map

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

0 comments on commit 2bf748d

Please sign in to comment.