{"action":"created","comment":{"url":"https://api.github.com/repos/interledger/five-bells-ledger/pulls/comments/65945053","id":65945053,"diff_hunk":"@@ -1,36 +1,121 @@\n 'use strict'\n \n-const assert = require('assert')\n const _ = require('lodash')\n-const Account = require('./account').Account\n+const assert = require('assert')\n const db = require('../../services/db')\n+const knex = require('../../lib/knex').knex\n+const config = require('../../services/config')\n+\n+const TABLE_NAME = 'L_ACCOUNTS'\n+\n+function convertFromPersistent (data) {\n+  data = _.cloneDeep(data)\n+  data = _.transform(data, (result, value, key) => {\n+    result[key.toLowerCase()] = value\n+  })\n+  delete data.account_id\n+  // some databases store booleans as 0 and 1, and knex does not convert\n+  data.is_disabled = Boolean(data.is_disabled)\n+  data.is_admin = Boolean(data.is_admin)\n+  // oracle has balance stored properly, but knex returns it with a small\n+  // rounding error, possibly a bug in knex; using toFixed as workaround\n+  data.balance = Number(Number(data.balance).toFixed(config.amount.scale))\n+  if (data.minimum_allowed_balance === null) {\n+    data.minimum_allowed_balance = Number.NEGATIVE_INFINITY\n+  } else if (data.minimum_allowed_balance) {\n+    data.minimum_allowed_balance = Number(data.minimum_allowed_balance)\n+  } else {\n+    data.minimum_allowed_balance = 0\n+  }\n+  delete data.created_at\n+  delete data.updated_at\n+  return data\n+}\n+\n+function convertToPersistent (data) {\n+  data = _.cloneDeep(data)\n+  if (data.balance) {\n+    data.balance = Number(Number(data.balance).toFixed(config.amount.scale))\n+  }\n+  if (data.minimum_allowed_balance) {\n+    if (data.minimum_allowed_balance === Number.NEGATIVE_INFINITY) {\n+      data.minimum_allowed_balance = null\n+    } else {\n+      data.minimum_allowed_balance = Number(data.minimum_allowed_balance)\n+    }\n+  }\n+  return _.transform(data, (result, value, key) => {\n+    result[key.toUpperCase()] = value\n+  })\n+}\n+\n+function getTransaction (options) {\n+  return !options ? knex : (!options.transaction ? knex : options.transaction)\n+}\n \n function * getAccounts (options) {\n-  const jsonAccounts = yield Account.findAll(options)\n-  return jsonAccounts.map(Account.fromDatabaseModel.bind(Account))\n+  return getTransaction(options)\n+    .from(TABLE_NAME).select()\n+    .then((results) => results.map(convertFromPersistent))\n }\n \n function * getConnectorAccounts (options) {\n-  const jsonAccounts = yield Account.findAll(options)\n-  return jsonAccounts.filter((account) => account.connector)\n-    .map(Account.fromDatabaseModel.bind(Account))\n+  return (yield getAccounts(options))\n+    .filter((account) => account.connector)\n+    .map(convertFromPersistent)\n+}\n+\n+function getAccountByKey (key, value, options) {\n+  return getTransaction(options)\n+    .from(TABLE_NAME).select().where(key, value)\n+    .then((results) => results.map(convertFromPersistent))\n+    .then((results) => {\n+      if (results.length === 1) {\n+        return results[0]\n+      } else if (results.length === 0) {\n+        return null\n+      } else {\n+        assert(false)\n+      }\n+    })\n+}\n+\n+function getAccount (name, options) {\n+  return getAccountByKey('NAME', name, options)\n+}\n+\n+function getAccountByFingerprint (fingerprint, options) {\n+  return getAccountByKey('FINGERPRINT', fingerprint, options)\n+}\n+\n+function * updateAccount (account, options) {\n+  return getTransaction(options)(TABLE_NAME)\n+    .update(convertToPersistent(account))\n+    .where('NAME', account.name)\n+}\n+\n+function * insertAccount (account, options) {\n+  return getTransaction(options)\n+    .insert(convertToPersistent(account))\n+    .into(TABLE_NAME)\n }\n \n-function * getAccount (name, options) {\n-  return yield Account.findByName(name, options)\n+function * insertAccounts (accounts, options) {","path":"src/models/db/accounts.js","position":108,"original_position":112,"commit_id":"2648083db9f872755adafd69a6e3795f0375a9c2","original_commit_id":"fd9a926f2f7e0f74c1bb3968115f5bd902ed4701","user":{"login":"MatthewPhinney","id":4961057,"avatar_url":"https://avatars.githubusercontent.com/u/4961057?v=3","gravatar_id":"","url":"https://api.github.com/users/MatthewPhinney","html_url":"https://github.com/MatthewPhinney","followers_url":"https://api.github.com/users/MatthewPhinney/followers","following_url":"https://api.github.com/users/MatthewPhinney/following{/other_user}","gists_url":"https://api.github.com/users/MatthewPhinney/gists{/gist_id}","starred_url":"https://api.github.com/users/MatthewPhinney/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/MatthewPhinney/subscriptions","organizations_url":"https://api.github.com/users/MatthewPhinney/orgs","repos_url":"https://api.github.com/users/MatthewPhinney/repos","events_url":"https://api.github.com/users/MatthewPhinney/events{/privacy}","received_events_url":"https://api.github.com/users/MatthewPhinney/received_events","type":"User","site_admin":false},"body":"That's a good point, but bear in mind that the `convertToExternal`, `convertFromPersistent`, etc. methods have the same name across multiple tables/models. If you want to be totally consistent, those should probably be renamed to `convertToAccountPersistent`, etc.","created_at":"2016-06-06T18:41:52Z","updated_at":"2016-06-06T18:41:52Z","html_url":"https://github.com/interledger/five-bells-ledger/pull/257#discussion_r65945053","pull_request_url":"https://api.github.com/repos/interledger/five-bells-ledger/pulls/257","_links":{"self":{"href":"https://api.github.com/repos/interledger/five-bells-ledger/pulls/comments/65945053"},"html":{"href":"https://github.com/interledger/five-bells-ledger/pull/257#discussion_r65945053"},"pull_request":{"href":"https://api.github.com/repos/interledger/five-bells-ledger/pulls/257"}}},"pull_request":{"url":"https://api.github.com/repos/interledger/five-bells-ledger/pulls/257","id":72580773,"html_url":"https://github.com/interledger/five-bells-ledger/pull/257","diff_url":"https://github.com/interledger/five-bells-ledger/pull/257.diff","patch_url":"https://github.com/interledger/five-bells-ledger/pull/257.patch","issue_url":"https://api.github.com/repos/interledger/five-bells-ledger/issues/257","number":257,"state":"open","locked":false,"title":"Remove Account model and rename database table and columns for accounts","user":{"login":"clark800","id":1238722,"avatar_url":"https://avatars.githubusercontent.com/u/1238722?v=3","gravatar_id":"","url":"https://api.github.com/users/clark800","html_url":"https://github.com/clark800","followers_url":"https://api.github.com/users/clark800/followers","following_url":"https://api.github.com/users/clark800/following{/other_user}","gists_url":"https://api.github.com/users/clark800/gists{/gist_id}","starred_url":"https://api.github.com/users/clark800/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/clark800/subscriptions","organizations_url":"https://api.github.com/users/clark800/orgs","repos_url":"https://api.github.com/users/clark800/repos","events_url":"https://api.github.com/users/clark800/events{/privacy}","received_events_url":"https://api.github.com/users/clark800/received_events","type":"User","site_admin":false},"body":"","created_at":"2016-06-03T22:52:22Z","updated_at":"2016-06-06T18:41:52Z","closed_at":null,"merged_at":null,"merge_commit_sha":"d529fd51cc5b1c844aa532bd3fde2b15ea2ac6b5","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/interledger/five-bells-ledger/pulls/257/commits","review_comments_url":"https://api.github.com/repos/interledger/five-bells-ledger/pulls/257/comments","review_comment_url":"https://api.github.com/repos/interledger/five-bells-ledger/pulls/comments{/number}","comments_url":"https://api.github.com/repos/interledger/five-bells-ledger/issues/257/comments","statuses_url":"https://api.github.com/repos/interledger/five-bells-ledger/statuses/2648083db9f872755adafd69a6e3795f0375a9c2","head":{"label":"clark800:accounts-rename","ref":"accounts-rename","sha":"2648083db9f872755adafd69a6e3795f0375a9c2","user":{"login":"clark800","id":1238722,"avatar_url":"https://avatars.githubusercontent.com/u/1238722?v=3","gravatar_id":"","url":"https://api.github.com/users/clark800","html_url":"https://github.com/clark800","followers_url":"https://api.github.com/users/clark800/followers","following_url":"https://api.github.com/users/clark800/following{/other_user}","gists_url":"https://api.github.com/users/clark800/gists{/gist_id}","starred_url":"https://api.github.com/users/clark800/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/clark800/subscriptions","organizations_url":"https://api.github.com/users/clark800/orgs","repos_url":"https://api.github.com/users/clark800/repos","events_url":"https://api.github.com/users/clark800/events{/privacy}","received_events_url":"https://api.github.com/users/clark800/received_events","type":"User","site_admin":false},"repo":{"id":50140432,"name":"five-bells-ledger","full_name":"clark800/five-bells-ledger","owner":{"login":"clark800","id":1238722,"avatar_url":"https://avatars.githubusercontent.com/u/1238722?v=3","gravatar_id":"","url":"https://api.github.com/users/clark800","html_url":"https://github.com/clark800","followers_url":"https://api.github.com/users/clark800/followers","following_url":"https://api.github.com/users/clark800/following{/other_user}","gists_url":"https://api.github.com/users/clark800/gists{/gist_id}","starred_url":"https://api.github.com/users/clark800/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/clark800/subscriptions","organizations_url":"https://api.github.com/users/clark800/orgs","repos_url":"https://api.github.com/users/clark800/repos","events_url":"https://api.github.com/users/clark800/events{/privacy}","received_events_url":"https://api.github.com/users/clark800/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/clark800/five-bells-ledger","description":"An implementation of the Five Bells ledger interface.","fork":true,"url":"https://api.github.com/repos/clark800/five-bells-ledger","forks_url":"https://api.github.com/repos/clark800/five-bells-ledger/forks","keys_url":"https://api.github.com/repos/clark800/five-bells-ledger/keys{/key_id}","collaborators_url":"https://api.github.com/repos/clark800/five-bells-ledger/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/clark800/five-bells-ledger/teams","hooks_url":"https://api.github.com/repos/clark800/five-bells-ledger/hooks","issue_events_url":"https://api.github.com/repos/clark800/five-bells-ledger/issues/events{/number}","events_url":"https://api.github.com/repos/clark800/five-bells-ledger/events","assignees_url":"https://api.github.com/repos/clark800/five-bells-ledger/assignees{/user}","branches_url":"https://api.github.com/repos/clark800/five-bells-ledger/branches{/branch}","tags_url":"https://api.github.com/repos/clark800/five-bells-ledger/tags","blobs_url":"https://api.github.com/repos/clark800/five-bells-ledger/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/clark800/five-bells-ledger/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/clark800/five-bells-ledger/git/refs{/sha}","trees_url":"https://api.github.com/repos/clark800/five-bells-ledger/git/trees{/sha}","statuses_url":"https://api.github.com/repos/clark800/five-bells-ledger/statuses/{sha}","languages_url":"https://api.github.com/repos/clark800/five-bells-ledger/languages","stargazers_url":"https://api.github.com/repos/clark800/five-bells-ledger/stargazers","contributors_url":"https://api.github.com/repos/clark800/five-bells-ledger/contributors","subscribers_url":"https://api.github.com/repos/clark800/five-bells-ledger/subscribers","subscription_url":"https://api.github.com/repos/clark800/five-bells-ledger/subscription","commits_url":"https://api.github.com/repos/clark800/five-bells-ledger/commits{/sha}","git_commits_url":"https://api.github.com/repos/clark800/five-bells-ledger/git/commits{/sha}","comments_url":"https://api.github.com/repos/clark800/five-bells-ledger/comments{/number}","issue_comment_url":"https://api.github.com/repos/clark800/five-bells-ledger/issues/comments{/number}","contents_url":"https://api.github.com/repos/clark800/five-bells-ledger/contents/{+path}","compare_url":"https://api.github.com/repos/clark800/five-bells-ledger/compare/{base}...{head}","merges_url":"https://api.github.com/repos/clark800/five-bells-ledger/merges","archive_url":"https://api.github.com/repos/clark800/five-bells-ledger/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/clark800/five-bells-ledger/downloads","issues_url":"https://api.github.com/repos/clark800/five-bells-ledger/issues{/number}","pulls_url":"https://api.github.com/repos/clark800/five-bells-ledger/pulls{/number}","milestones_url":"https://api.github.com/repos/clark800/five-bells-ledger/milestones{/number}","notifications_url":"https://api.github.com/repos/clark800/five-bells-ledger/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/clark800/five-bells-ledger/labels{/name}","releases_url":"https://api.github.com/repos/clark800/five-bells-ledger/releases{/id}","deployments_url":"https://api.github.com/repos/clark800/five-bells-ledger/deployments","created_at":"2016-01-21T22:15:51Z","updated_at":"2016-01-21T22:15:52Z","pushed_at":"2016-06-06T18:35:17Z","git_url":"git://github.com/clark800/five-bells-ledger.git","ssh_url":"git@github.com:clark800/five-bells-ledger.git","clone_url":"https://github.com/clark800/five-bells-ledger.git","svn_url":"https://github.com/clark800/five-bells-ledger","homepage":null,"size":1183,"stargazers_count":0,"watchers_count":0,"language":"JavaScript","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"interledger:master","ref":"master","sha":"bb3c60570f5d3030fa0df3ecd6e5971a6c6e33e9","user":{"login":"interledger","id":14062960,"avatar_url":"https://avatars.githubusercontent.com/u/14062960?v=3","gravatar_id":"","url":"https://api.github.com/users/interledger","html_url":"https://github.com/interledger","followers_url":"https://api.github.com/users/interledger/followers","following_url":"https://api.github.com/users/interledger/following{/other_user}","gists_url":"https://api.github.com/users/interledger/gists{/gist_id}","starred_url":"https://api.github.com/users/interledger/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/interledger/subscriptions","organizations_url":"https://api.github.com/users/interledger/orgs","repos_url":"https://api.github.com/users/interledger/repos","events_url":"https://api.github.com/users/interledger/events{/privacy}","received_events_url":"https://api.github.com/users/interledger/received_events","type":"Organization","site_admin":false},"repo":{"id":30608771,"name":"five-bells-ledger","full_name":"interledger/five-bells-ledger","owner":{"login":"interledger","id":14062960,"avatar_url":"https://avatars.githubusercontent.com/u/14062960?v=3","gravatar_id":"","url":"https://api.github.com/users/interledger","html_url":"https://github.com/interledger","followers_url":"https://api.github.com/users/interledger/followers","following_url":"https://api.github.com/users/interledger/following{/other_user}","gists_url":"https://api.github.com/users/interledger/gists{/gist_id}","starred_url":"https://api.github.com/users/interledger/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/interledger/subscriptions","organizations_url":"https://api.github.com/users/interledger/orgs","repos_url":"https://api.github.com/users/interledger/repos","events_url":"https://api.github.com/users/interledger/events{/privacy}","received_events_url":"https://api.github.com/users/interledger/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/interledger/five-bells-ledger","description":"An implementation of the Five Bells ledger interface.","fork":false,"url":"https://api.github.com/repos/interledger/five-bells-ledger","forks_url":"https://api.github.com/repos/interledger/five-bells-ledger/forks","keys_url":"https://api.github.com/repos/interledger/five-bells-ledger/keys{/key_id}","collaborators_url":"https://api.github.com/repos/interledger/five-bells-ledger/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/interledger/five-bells-ledger/teams","hooks_url":"https://api.github.com/repos/interledger/five-bells-ledger/hooks","issue_events_url":"https://api.github.com/repos/interledger/five-bells-ledger/issues/events{/number}","events_url":"https://api.github.com/repos/interledger/five-bells-ledger/events","assignees_url":"https://api.github.com/repos/interledger/five-bells-ledger/assignees{/user}","branches_url":"https://api.github.com/repos/interledger/five-bells-ledger/branches{/branch}","tags_url":"https://api.github.com/repos/interledger/five-bells-ledger/tags","blobs_url":"https://api.github.com/repos/interledger/five-bells-ledger/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/interledger/five-bells-ledger/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/interledger/five-bells-ledger/git/refs{/sha}","trees_url":"https://api.github.com/repos/interledger/five-bells-ledger/git/trees{/sha}","statuses_url":"https://api.github.com/repos/interledger/five-bells-ledger/statuses/{sha}","languages_url":"https://api.github.com/repos/interledger/five-bells-ledger/languages","stargazers_url":"https://api.github.com/repos/interledger/five-bells-ledger/stargazers","contributors_url":"https://api.github.com/repos/interledger/five-bells-ledger/contributors","subscribers_url":"https://api.github.com/repos/interledger/five-bells-ledger/subscribers","subscription_url":"https://api.github.com/repos/interledger/five-bells-ledger/subscription","commits_url":"https://api.github.com/repos/interledger/five-bells-ledger/commits{/sha}","git_commits_url":"https://api.github.com/repos/interledger/five-bells-ledger/git/commits{/sha}","comments_url":"https://api.github.com/repos/interledger/five-bells-ledger/comments{/number}","issue_comment_url":"https://api.github.com/repos/interledger/five-bells-ledger/issues/comments{/number}","contents_url":"https://api.github.com/repos/interledger/five-bells-ledger/contents/{+path}","compare_url":"https://api.github.com/repos/interledger/five-bells-ledger/compare/{base}...{head}","merges_url":"https://api.github.com/repos/interledger/five-bells-ledger/merges","archive_url":"https://api.github.com/repos/interledger/five-bells-ledger/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/interledger/five-bells-ledger/downloads","issues_url":"https://api.github.com/repos/interledger/five-bells-ledger/issues{/number}","pulls_url":"https://api.github.com/repos/interledger/five-bells-ledger/pulls{/number}","milestones_url":"https://api.github.com/repos/interledger/five-bells-ledger/milestones{/number}","notifications_url":"https://api.github.com/repos/interledger/five-bells-ledger/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/interledger/five-bells-ledger/labels{/name}","releases_url":"https://api.github.com/repos/interledger/five-bells-ledger/releases{/id}","deployments_url":"https://api.github.com/repos/interledger/five-bells-ledger/deployments","created_at":"2015-02-10T18:59:20Z","updated_at":"2016-04-29T19:27:04Z","pushed_at":"2016-06-06T18:35:18Z","git_url":"git://github.com/interledger/five-bells-ledger.git","ssh_url":"git@github.com:interledger/five-bells-ledger.git","clone_url":"https://github.com/interledger/five-bells-ledger.git","svn_url":"https://github.com/interledger/five-bells-ledger","homepage":null,"size":1462,"stargazers_count":12,"watchers_count":12,"language":"JavaScript","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":true,"forks_count":9,"mirror_url":null,"open_issues_count":9,"forks":9,"open_issues":9,"watchers":12,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/interledger/five-bells-ledger/pulls/257"},"html":{"href":"https://github.com/interledger/five-bells-ledger/pull/257"},"issue":{"href":"https://api.github.com/repos/interledger/five-bells-ledger/issues/257"},"comments":{"href":"https://api.github.com/repos/interledger/five-bells-ledger/issues/257/comments"},"review_comments":{"href":"https://api.github.com/repos/interledger/five-bells-ledger/pulls/257/comments"},"review_comment":{"href":"https://api.github.com/repos/interledger/five-bells-ledger/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/interledger/five-bells-ledger/pulls/257/commits"},"statuses":{"href":"https://api.github.com/repos/interledger/five-bells-ledger/statuses/2648083db9f872755adafd69a6e3795f0375a9c2"}}}}
{"actor":{"display_login":"MatthewPhinney"}}