Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Account service without juggler #51

Merged
merged 1 commit into from
Jul 10, 2017
Merged

Conversation

b-admike
Copy link
Member

@b-admike b-admike commented May 23, 2017

@b-admike b-admike added review and removed review labels May 23, 2017
@b-admike b-admike closed this May 23, 2017
@b-admike b-admike force-pushed the validate-repo-without-juggler branch from 230dd41 to c63c953 Compare May 23, 2017 18:27
@b-admike b-admike reopened this May 25, 2017
"required": true
}
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing newline

@b-admike b-admike force-pushed the validate-repo-without-juggler branch from cdb602c to d3d6a79 Compare June 15, 2017 15:29
@b-admike b-admike changed the title Validate repo without juggler [DO NOT MERGE] Validate repo without juggler Jun 19, 2017
@b-admike b-admike force-pushed the validate-repo-without-juggler branch 2 times, most recently from fe897d2 to 8cb7ab4 Compare June 23, 2017 20:00

async deleteAccount(id) {
return await this.repository.deleteById(id);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please fix the indentation.

@@ -0,0 +1,2 @@
#!/usr/bin/env bash
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A better name for accountsanjuggler?

Where
} from 'loopback-next/packages/repository';

export class MySqlConn implements CrudConnector {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please refactor it into a new file.

})
})
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please format the code with correct indentation. If you use VSCode, try prettier javascript formatter.

@b-admike b-admike force-pushed the validate-repo-without-juggler branch 4 times, most recently from 730cc20 to 0c8ba95 Compare June 28, 2017 13:37
@b-admike b-admike changed the title [DO NOT MERGE] Validate repo without juggler Account service without juggler Jun 28, 2017
@@ -0,0 +1,229 @@
const debug = require("debug")("accountsansjuggler");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we change the debug tag to account-without-juggler instead?

expect(getResult).to.not.be.empty();
expect(getResult).have.lengthOf(1);
expect(getResult[0].id).to.be.equal(testAcc.id);
expect(JSON.parse(JSON.stringify(getResult[0])).balance).to.be.equal(2000);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getResult[0].toObject().balance should work.

"extends": [
"./tslint.json"
],
// This configuration files enabled rules which require type checking
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are these comments needed?

@@ -0,0 +1,72 @@
{
// See https://palantir.github.io/tslint/rules/
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think these comments were copied from loopback-next. I added this particular comment in loopback-next to make it easier for people that are not familiar with tslint to find a reference of all rules.

What are your objections against having these comments in place?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I see. Disregard my comments in that case.

super();
const app = this;
app.controller(AccountController);
app.bind('repositories.account').toClass(accountRepository);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be nice to introduce app.repository API to take care of this.

app.repository(accountRepository);

The argument should be strongly typed to a base Repository interface, which is something that we cannot achieve via bind().toClass(), at least not now.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@raymondfeng thoughts on the above API to take care of this action?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can land and create an issue (feature) for the sugar API mentioned by @bajtos. This PR is huge already.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@b-admike Please link to the follow up issue here for posterity.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"main": "index.ts",
"dependencies": {
"@loopback/testlab": "^4.0.0-alpha.5",
"@types/mocha": "^2.2.41",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think both testlab and mocha should go to devDependencies, shouldn't it? Why do you actually need @types/mocha?

connect() {
let self = this;
return new Promise<void>(function(resolve, reject) {
self.connection.connect(function(err: any) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have you considered using https://www.npmjs.com/package/mysql-promise or https://www.npmjs.com/package/promise-mysql, instead of this manual way of adding promise support?

sqlStmt += "(" + vals.join(",") + ")" + ";";
debug("insert stmt ", sqlStmt);
return new Promise<Account>(function(resolve, reject) {
self.connection.query(sqlStmt, function(err: any, count: any) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have you considered using https://www.npmjs.com/package/@types/mysql to get better type information? (I don't know how it will work with promise-wrapped mysql packages though.)

options: Options
): Promise<EntityData> {
let self = this;
let sqlStmt = "INSERT INTO " + modelClass.name + " VALUES ";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you should escape modelCass.name in order to prevent SQL script injection. (Class names probably cannot be changed so much as to cause security problem, but I can imagine there may be valid class names that cannot be used in SQL without escaping.)

let sqlStmt = "UPDATE " + modelClass.name + " SET ";
var vals = [];
for (var prop in data) {
vals.push(prop + "=" + data[prop]);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SQL injection.

for (var prop in data) {
vals.push(prop + "=" + data[prop]);
}
sqlStmt += vals.join(",") + " WHERE " + "`id`=" + id + ";";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SQL injection - pass id as a parameter, let the driver to escape it.

options: Options
): Promise<boolean> {
let self = this;
let sqlStmt = "DELETE FROM " + modelClass.name + " WHERE `id`=" + id + ";";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SQL injection - pass id as a parameter, let the driver to escape it.

@@ -0,0 +1,73 @@
import { AccountController } from "../../controllers/AccountController";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We use single quotes for string delimiters in LoopBack code (and in Node.js code in general). This comment applies to all other JS/TS source files in this pull request too.

@@ -0,0 +1,72 @@
{
// See https://palantir.github.io/tslint/rules/
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think these comments were copied from loopback-next. I added this particular comment in loopback-next to make it easier for people that are not familiar with tslint to find a reference of all rules.

What are your objections against having these comments in place?

private connection: any;

constructor(config: Object) {
db.configure(config, require("mysql2"));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are we using the mysql2 driver?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is not what our module (loopback-connector-mysql) uses though. Should we not test this with the same driver that our module currently uses?


@api(def)
export class AccountController {
@inject('repositories.account') private repository: accountRepository;
Copy link
Collaborator

@superkhau superkhau Jul 6, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

move into constructor so that repository can be replaced by constructor injection

Copy link
Collaborator

@superkhau superkhau left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did only a part of the review for now, you can change it up and ping me again.

super();
const app = this;
app.controller(AccountController);
app.bind('repositories.account').toClass(accountRepository);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can land and create an issue (feature) for the sugar API mentioned by @bajtos. This PR is huge already.

"host":"localhost",
"user":"root",
"password": "dis-mysql",
"database": "test-db",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's make it simpler for future users

password: "password" // password
database: "loopback-next-example" // name of example

should be consistent across all examples

@@ -0,0 +1,201 @@
const debug = require("debug")("account-without-juggler");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const debug = require('debug')('loopback:repositories:account:datasources:connections:mysql');

Single quote everything in this file too

} from "loopback-next/packages/repository";

export class MySqlConn implements CrudConnector {
private connection: any;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

: Connection, let's get away from any

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't have that exported in Repository, but I was trying to make it of type IConnection from https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/mysql/index.d.ts#L39-#L83, but the return types for connect and disconnect don't align with ours (Promise<void>)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's not prefix with I. You can create your own type or interface: See https://github.com/strongloop/loopback-next/blob/master/packages/core/src/internal-types.ts. If we need to create a proxy to normalize the interfaces, so be it. cc @raymondfeng

private connection: any;

constructor(config: Object) {
db.configure(config, require("mysql2"));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

move the require up to the top of the file

return this.connection.ping();
}

public updateAll(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

public is default, remove for consistency

@kjdelisle kjdelisle dismissed their stale review July 6, 2017 20:59

My review isn't relevant due to age, don't let it hold this PR up.

@b-admike b-admike force-pushed the validate-repo-without-juggler branch from 1974c36 to bd999bf Compare July 6, 2017 21:37
@b-admike
Copy link
Member Author

b-admike commented Jul 6, 2017

@ssh24 @superkhau feedback applied, PTAL

@b-admike b-admike removed the request for review from raymondfeng July 6, 2017 21:39
in: 'query',
description: 'The id for the model instance to be deleted.',
required: true,
type:'object',
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit, indent off by one space here
and id -> ID or identifier

properties: {
count: {
type: 'number',
description: 'number of records deleted',
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The number

properties: {
count: {
type: 'number',
description: 'number of records updated',
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The number
and period at the end of your sentence

{
name: 'data',
in: 'body',
description: 'An object of model property name/value pairs',
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Period at the end for your description

{
name: 'id',
in: 'query',
description: 'The id of the model instance to be updated',
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Period at the end for your description.

200: {
schema: {
type: 'object',
description: 'update information',
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Full sentence description here pls.

200: {
schema: {
type: 'object',
description: 'delete information',
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

better desc here too

@api(def)
export class AccountController {
constructor(
@inject('repositories.account') private repository: accountRepository
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't type be AccountRepository?

import { Account } from './models/Account';


export class accountRepository extends CrudRepositoryImpl<Account, string> {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be AccountRepository

"check-type",
"check-typecast",
"check-preblock"
]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you separate out this linting stuff into another PR?

@b-admike b-admike force-pushed the validate-repo-without-juggler branch from eee0a2a to 4867443 Compare July 7, 2017 13:37
@b-admike
Copy link
Member Author

b-admike commented Jul 7, 2017

@superkhau PTAL

@b-admike b-admike force-pushed the validate-repo-without-juggler branch 2 times, most recently from 0dbd6b9 to 2496c42 Compare July 7, 2017 15:13
@b-admike b-admike requested a review from raymondfeng July 7, 2017 20:43
Copy link
Collaborator

@superkhau superkhau left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, we can refactor anything else that needs updating later.

super();
const app = this;
app.controller(AccountController);
app.bind('repositories.account').toClass(accountRepository);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@b-admike Please link to the follow up issue here for posterity.

"host":"localhost",
"user":"root",
"password": "password",
"database": "account-service",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use the name of the example to distinguish from other databases the user may have locally before.

const mysql = require('mysql');
const db = require('mysql-promise')();
import { CrudConnector } from 'loopback-next/packages/repository/src/crud-connector';
import { IError, IConnection } from '@types/mysql';
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do not use I for interfaces, should just be Error + Connection

this.settings = require("./mysql.json");
this.connector = new MySqlConn(this.settings);
}
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

single quotes in the whole file pls

settings: Object;

constructor() {
this.settings = require("./mysql.json");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

move to top of file


export class AccountRepository extends CrudRepositoryImpl<Account, string> {
constructor() {
const ds = new MySQLDs();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MySqlDs

export class Account extends Entity {
static definition = new ModelDefinition("Account", require("./account/model-definition").properties);
static modelName = "Account";
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto for single quotes

"required": true
}
}
};
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

single quotes and keys don't need quotes

function createAccountController() {
accCtrl = new AccountController();
accCtrl.repository = new accountRepository();
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No abbreviations with var names pls

@superkhau
Copy link
Collaborator

superkhau commented Jul 7, 2017

Please update the README with working instructions before landing too

@b-admike b-admike force-pushed the validate-repo-without-juggler branch from 2496c42 to 7b654da Compare July 10, 2017 04:21
@b-admike
Copy link
Member Author

@slnode test please

@b-admike b-admike merged commit 413e753 into master Jul 10, 2017
@b-admike b-admike deleted the validate-repo-without-juggler branch July 10, 2017 04:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants