Skip to content

Commit

Permalink
Update Version
Browse files Browse the repository at this point in the history
  • Loading branch information
ardalanamini committed Nov 16, 2018
1 parent 4c9ad03 commit 3d679e6
Show file tree
Hide file tree
Showing 7 changed files with 140 additions and 61 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@

---

## [v0.3.0](https://github.com/foxifyjs/odin/releases/tag/v0.3.0) - *(2018-11-16)*

- :zap: Added `toJsonSchema` static method to Model so you can easily use it with `Foxify` router schema option
- :star2: Improved throwing validation error for `Foxify` usage

## [v0.2.0](https://github.com/foxifyjs/odin/releases/tag/v0.2.0) - *(2018-11-14)*

- :zap: Added more advanced filtering ability to queries and joins
Expand Down
12 changes: 4 additions & 8 deletions demo/Bill.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
const Model = require("../dist/index");

const {
Types
} = Model;
const Model = require("../dist");

class Bill extends Model {
user() {
return this.hasOne(Model.models.User);
return this.hasOne("User");
}
}

Bill.schema = {
user_id: Types.ObjectId.required,
bill: Types.Number.positive.required,
user_id: Bill.Types.Id.required,
bill: Bill.Types.Number.positive.required,
};

Model.register(Bill);
Expand Down
16 changes: 6 additions & 10 deletions demo/User.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,18 @@
const Model = require("../dist/index");

const {
Types
} = Model;
const Model = require("../dist");

class User extends Model {
bills() {
return this.hasMany(Model.models.Bill);
return this.hasMany("Bill");
}
}

User.schema = {
name: {
first: Types.String.min(3).required,
last: Types.String.min(3),
first: User.Types.String.min(3).required,
last: User.Types.String.min(3),
},
username: Types.String.token.required,
email: Types.String.email.required,
username: User.Types.String.token.required,
email: User.Types.String.email.required,
};

Model.register(User);
Expand Down
5 changes: 3 additions & 2 deletions demo/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ const env = require("dotenv").load({
}).parsed;

const Model = require("../dist/index");
const User = require("./User");
const Bill = require("./Bill");

const time = () => new Date().getTime();

Expand All @@ -21,6 +19,9 @@ Model.connections({
}
})

const User = require("./User");
const Bill = require("./Bill");

const end = time();

console.log(
Expand Down
91 changes: 54 additions & 37 deletions package-lock.json

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

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@foxify/odin",
"version": "0.2.2",
"version": "0.3.0",
"description": "Active Record Model",
"author": "Ardalan Amini <[email protected]> [https://github.com/ardalanamini]",
"contributors": [
Expand Down Expand Up @@ -43,7 +43,7 @@
"@types/graphql": "^0.13.4",
"@types/graphql-iso-date": "^3.3.1",
"@types/mongodb": "^3.1.14",
"@types/node": "^10.12.7",
"@types/node": "^10.12.9",
"async": "^2.6.1",
"caller-id": "^0.1.0",
"deasync": "^0.1.14",
Expand All @@ -67,7 +67,7 @@
"rimraf": "^2.6.2",
"ts-jest": "^23.10.4",
"tslint": "^5.11.0",
"tslint-config-airbnb": "^5.11.0",
"tslint-config-airbnb": "^5.11.1",
"typescript": "^3.1.6",
"uglify-es": "^3.3.9"
},
Expand Down
66 changes: 65 additions & 1 deletion src/Model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ import GraphQL from "./GraphQL";
import GraphQLInstance from "./GraphQL/Model";
import * as Types from "./types";
import TypeAny from "./types/Any";
import TypeArray from "./types/Array";
import TypeDate from "./types/Date";
import TypeObjectId from "./types/ObjectId";
import * as utils from "./utils";

const EVENTS = ["created"];
Expand Down Expand Up @@ -79,6 +82,60 @@ class Model<T extends object = {}> extends Base<T>
return this._table;
}

public static toJsonSchema() {
const jsonSchemaGenerator = (schema: Odin.Schema) => {
const value: { [key: string]: any } = {};
const required: string[] = [];

for (const key in schema) {
const type = schema[key];

if (type instanceof TypeAny) {
// Type

let schemaType: string = (type as any)._type.toLowerCase();

if (
type instanceof TypeObjectId
|| type instanceof TypeDate
) schemaType = "string";

value[key] = {
type: schemaType,
};

if (type instanceof TypeArray) value[key].items = {
type: (type.ofType as any)._type.toLowerCase(),
};

if ((type as any)._required) required.push(key);

continue;
}

// Object

const generated = jsonSchemaGenerator(type);

if (utils.object.size(generated) === 1) continue;

value[key] = {
type: "object",
properties: generated,
};

if (generated.required.length) required.push(key);
}

return {
...value,
required,
};
};

return jsonSchemaGenerator(this.schema);
}

public static validate<T = object>(document: T, updating: boolean = false) {
const validator = (schema: Odin.Schema, doc: T) => {
const value: { [key: string]: any } = {};
Expand Down Expand Up @@ -128,7 +185,14 @@ class Model<T extends object = {}> extends Base<T>
if (utils.object.size(validation.errors) === 0) validation.errors = null;
}

if (validation.errors) throw validation.errors;
if (validation.errors) {
const error = new Error("Unprocessable Entity") as any;

error.errors = validation.errors;
error.code = 422;

throw error;
}

const value = validation.value;

Expand Down

0 comments on commit 3d679e6

Please sign in to comment.