Skip to content

Commit 5d34206

Browse files
Joonmo YangJoonmo Yang
Joonmo Yang
authored and
Joonmo Yang
committed
Use objection.js for ORM
1 parent c637db8 commit 5d34206

File tree

9 files changed

+549
-9
lines changed

9 files changed

+549
-9
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ node_modules/
44
/public/stylesheets/*.css
55
/public/stylesheets/*.css.map
66
.DS_Store
7+
/config/local*.js

config/default.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
const path = require("path");
2+
3+
module.exports = {
4+
knex: {
5+
client: "pg",
6+
connection: {
7+
host: "localhost",
8+
port: 5432,
9+
database: "codechain-keystore",
10+
user: "codechain"
11+
},
12+
migrations: {
13+
directory: path.resolve(__dirname, "..", "migrations"),
14+
tableName: "knex"
15+
},
16+
seeds: {
17+
directory: path.resolve(__dirname, "..", "seeds")
18+
}
19+
},
20+
};

knexfile.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = require("config").get("knex");

migrations/00000000000000_keys.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
const table = "keys";
2+
3+
exports.up = async (knex) => {
4+
const hasTable = await knex.schema.hasTable(table);
5+
if (!hasTable) {
6+
return knex.schema.createTable(table, (t) => {
7+
t.string("type").notNullable();
8+
t.string("address").notNullable();
9+
t.integer("version").notNullable();
10+
t.string("kdf").notNullable();
11+
t.json("kdfparams");
12+
t.string("mac").notNullable();
13+
t.string("cipher").notNullable();
14+
t.json("cipherparams");
15+
t.string("ciphertext").notNullable();
16+
t.json("meta");
17+
18+
t.primary(["type", "address"]);
19+
});
20+
}
21+
else {
22+
// TODO: Add detailed error message
23+
throw Error();
24+
}
25+
};
26+
27+
exports.down = async (knex) => {
28+
const hasTable = await knex.schema.hasTable(table);
29+
if (hasTable) {
30+
return knex.schema.dropTable(table);
31+
}
32+
else {
33+
// TODO: Add detailed error message
34+
throw Error();
35+
}
36+
};

package.json

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,23 @@
77
"start": "ts-node ./bin/www.ts",
88
"test": "yarn lint && jest --env node",
99
"lint": "tslint -p . && prettier '**/*.ts' -l",
10-
"fmt": "tslint -p . --fix && prettier '**/*ts' --write"
10+
"fmt": "tslint -p . --fix && prettier '**/*ts' --write",
11+
"migrate": "knex migrate:latest --knexfile ./knexfile.js",
12+
"rollback": "knex migrate:rollback --knexfile ./knexfile.js",
13+
"seed": "knex seed:run --knexfile ./knexfile.js"
1114
},
1215
"dependencies": {
16+
"@types/config": "^0.0.34",
1317
"codechain-keystore": "~0.4.0",
14-
"config": "^2.0.1",
18+
"config": "^3.1.0",
1519
"debug": "~2.6.9",
1620
"express": "~4.16.0",
1721
"http-errors": "~1.6.2",
22+
"knex": "^0.16.5",
1823
"morgan": "~1.9.1",
1924
"morgan-body": "^2.4.5",
25+
"objection": "^1.6.8",
26+
"pg": "^7.10.0",
2027
"request": "^2.88.0",
2128
"request-promise": "^4.2.2"
2229
},

seeds/keys.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
const table = "keys";
2+
3+
exports.seed = async (knex) => {
4+
await knex(table).del();
5+
return knex(table).insert([]);
6+
};

src/app.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1+
import * as config from "config";
12
import * as express from "express";
23
import * as createError from "http-errors";
4+
import * as knex from "knex";
35
import * as logger from "morgan";
6+
import { Model } from "objection";
47
import * as path from "path";
58
const morganBody = require("morgan-body");
69

@@ -9,6 +12,7 @@ import { createRouter as createApiRouter } from "./routes/api";
912
import { createRouter as createPingRouter } from "./routes/ping";
1013

1114
export async function createApp(): Promise<[express.Application, Context]> {
15+
Model.knex(knex(config.get("knex")));
1216
const app = express();
1317

1418
// view engine setup

src/models/key.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { Model, snakeCaseMappers } from "objection";
2+
3+
export default class KeyModel extends Model {
4+
public static columnNameMappers = snakeCaseMappers();
5+
6+
public static tableName = "keys";
7+
public static idColumn = ["type", "address"];
8+
public readonly type!: "asset" | "platform";
9+
public readonly address!: string;
10+
public readonly version!: number;
11+
12+
public readonly kdf!: string;
13+
public readonly kdfparams?: any;
14+
public readonly mac!: string;
15+
16+
public readonly cipher!: string;
17+
public readonly cipherparams?: any;
18+
public readonly ciphertext!: string;
19+
20+
public readonly meta!: any;
21+
}

0 commit comments

Comments
 (0)