Skip to content

Commit

Permalink
Introducing special functions
Browse files Browse the repository at this point in the history
  • Loading branch information
chrissmejia committed Sep 24, 2018
1 parent 11d0530 commit d22df03
Show file tree
Hide file tree
Showing 14 changed files with 116 additions and 39 deletions.
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -183,9 +183,6 @@ Query executed:
* Key/Value object used to filter the query
* Array of Key/Value objects will generate a multiple filters separated by an "OR".

### Special values
* `now()` Insert a mysql now() function.

## Delete

```typescript
Expand Down Expand Up @@ -357,6 +354,11 @@ DELETE FROM `usersTwo` INNER JOIN `users` ON `usersTwo`.`user` = `users`.`id` WH

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

## Special values
Supported out the box mysql functions as where and set values.

* `now()` Insert a mysql now() function.

## Advanced

### Literal strings
Expand Down
1 change: 1 addition & 0 deletions model.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export declare class Model {
private fields;
private joins;
private plainQuery;
private specialFunctions;
/**
* Create a table object.
*
Expand Down
23 changes: 18 additions & 5 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.3",
"version": "0.1.4",
"description": "Unicoderns Object/Relational Mapping",
"author": {
"name": "Unicoderns SA",
Expand Down
16 changes: 12 additions & 4 deletions sources/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export class Model {
private fields: Map<string, string> | undefined = undefined;
private joins: Models.Join[] = [];
private plainQuery: boolean = false;
private specialFunctions = ["now()"];

/**
* Create a table object.
Expand Down Expand Up @@ -295,16 +296,23 @@ export class Model {
sql = sql + "`" + this.tableName + "`.`" + item + "` = " + String(where[item]).substring(1);
} else {
let joinkeys = item.split("__");
// string literal
if (joinkeys.length == 2) {
sql = sql + "`" + joinkeys[0] + "`.`" + joinkeys[1];
} else {
sql = sql + "`" + this.tableName + "`.`" + item;
}
joinkeys = String(where[item]).split("__");
// joined column
if (joinkeys.length == 2) {
sql = sql + "` = `" + joinkeys[0] + "`.`" + joinkeys[1] + "`";
} else {
sql = sql + "` = ?"
// special functiom
if (this.specialFunctions.indexOf(where[item]) >= 0) {
sql = sql + "` = " + where[item];
} else {
sql = sql + "` = ?"
}
}
}
if (id < array.length - 1) {
Expand All @@ -314,7 +322,7 @@ export class Model {
// getting values
filteredKeys.forEach((item: string) => {
let joinkeys = String(where[item]).split("__");
if ((String(where[item]).charAt(0) != "\\") && (joinkeys.length != 2)) {
if ((String(where[item]).charAt(0) != "\\") && (joinkeys.length != 2) && (this.specialFunctions.indexOf(where[item]) < 0)) {
values.push(where[item]);
}
});
Expand Down Expand Up @@ -560,8 +568,8 @@ export class Model {
let joinkeys = String(data[key]).split("__");
if (joinkeys.length == 2) {
fields.push("`" + this.tableName + "`.`" + key + "` = " + "`" + joinkeys[0] + "`.`" + joinkeys[1] + "`");
} else if (data[key] == "now()") {
fields.push("`" + this.tableName + "`.`" + key + "` = now()");
} else if (this.specialFunctions.indexOf(data[key]) >= 0) {
fields.push("`" + this.tableName + "`.`" + key + "` = " + data[key]);
} else {
fields.push("`" + this.tableName + "`.`" + key + "` = ?");
values.push(data[key]);
Expand Down
2 changes: 2 additions & 0 deletions sources/tests/update.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,8 @@ describe('Update', () => {
});
});

// Special mysql functions

it('1 field 1 where spacial now() function', () => {
var expected = {
sql: 'UPDATE `users` SET `users`.`created` = now() WHERE `users`.`id` = ?;',
Expand Down
19 changes: 19 additions & 0 deletions sources/tests/where.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,4 +219,23 @@ describe('Get general', () => {
});
});


// Special mysql functions
it('Simple with now() function', () => {
var expected = {
sql: 'SELECT `users`.`id`, `users`.`created`, `users`.`username`, `users`.`email`, `users`.`firstName` AS `first_name`, `users`.`lastName` AS `last_name`, `users`.`admin`, `users`.`verified`, `users`.`active` FROM `users` WHERE (`users`.`id` = ?) OR (`users`.`created` = now());',
values: [3]
};
usersTable.returnQuery().getAll({
where: [
{ id: 3 },
{ created: "now()" }
]
}).then((query: Models.Query) => {
expect(query).toEqual(expected);
}).catch((err: any) => {
console.error(err)
});
});

});
34 changes: 16 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.

Loading

0 comments on commit d22df03

Please sign in to comment.