Skip to content

Commit

Permalink
Update Version
Browse files Browse the repository at this point in the history
  • Loading branch information
ardalanamini committed Nov 14, 2018
1 parent cbb56cb commit 9506b09
Show file tree
Hide file tree
Showing 22 changed files with 540 additions and 307 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@

---

## [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
- :zap: Added `numeral` to `String` schema type
- :zap: Added `enum` to `String` schema type

## [v0.1.0](https://github.com/foxifyjs/odin/releases/tag/v0.1.0) - *(2018-10-20)*

- :zap: Added model hook `created`
Expand Down
43 changes: 25 additions & 18 deletions package-lock.json

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

8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@foxify/odin",
"version": "0.1.3",
"version": "0.2.0",
"description": "Active Record Model",
"author": "Ardalan Amini <ardalanamini22@gmail.com> [https://github.com/ardalanamini]",
"contributors": [
Expand Down Expand Up @@ -43,10 +43,10 @@
"@types/graphql": "^0.13.4",
"@types/graphql-iso-date": "^3.3.1",
"@types/mongodb": "^3.1.14",
"@types/node": "^10.12.3",
"@types/node": "^10.12.7",
"async": "^2.6.1",
"caller-id": "^0.1.0",
"deasync": "^0.1.13",
"deasync": "^0.1.14",
"graphql": "^0.13.2",
"graphql-iso-date": "^3.6.1",
"mongodb": "^3.1.9",
Expand All @@ -63,7 +63,7 @@
"dotenv": "^6.1.0",
"fs-readdir-recursive": "^1.1.0",
"jest": "^23.6.0",
"mongodb-memory-server": "^2.7.0",
"mongodb-memory-server": "^2.7.2",
"rimraf": "^2.6.2",
"ts-jest": "^23.10.4",
"tslint": "^5.11.0",
Expand Down
2 changes: 2 additions & 0 deletions src/DB.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ class DB<T = any, D extends Driver<T> = any, A = undefined> {

/******************************* Where Clauses ******************************/

public where(query: Driver.FilterQuery): this;
public where(field: string, value: any): this;
public where(field: string, operator: Driver.Operator, value: any): this;
public where() {
Expand All @@ -66,6 +67,7 @@ class DB<T = any, D extends Driver<T> = any, A = undefined> {
return this;
}

public orWhere(query: Driver.FilterQuery): this;
public orWhere(field: string, value: any): this;
public orWhere(field: string, operator: Driver.Operator, value: any): this;
public orWhere() {
Expand Down
8 changes: 5 additions & 3 deletions src/Model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ class Model<T extends object = {}> extends Base<T>
implements QueryBuilder<T>, Relational, GraphQLInstance {
public static DB = DB;
public static GraphQL = GraphQL;
public static Types = Types;
public static connections = connections;

public static connection: Odin.Connection = "default";
Expand All @@ -44,9 +43,8 @@ class Model<T extends object = {}> extends Base<T>
}

private static get _schema() {
// TODO: id
const schema: Odin.Schema = {
id: this.Types.ObjectId,
id: this.Types.Id,
...this.schema,
};

Expand All @@ -61,6 +59,10 @@ class Model<T extends object = {}> extends Base<T>
return schema;
}

public static get Types() {
return new Types(this.driver);
}

public static get driver() {
return getConnection(this.connection).driver;
}
Expand Down
5 changes: 3 additions & 2 deletions src/base/QueryBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class QueryBuilder<T extends object = {}> extends Base<T> {
throw new TypeError("Expected at least one 'relation', got none");

return this.query(relations.map((name) => {
let query = (this.prototype as any)[name] as any;
let query = (this.prototype as any)[name] as Relation;

if (!utils.function.isFunction(query))
throw new Error(`Relation '${name}' does not exist on '${this.name}' Model`);
Expand All @@ -53,10 +53,11 @@ class QueryBuilder<T extends object = {}> extends Base<T> {

/******************************* Where Clauses ******************************/

public static where<T extends object>(query: Driver.FilterQuery): Query<T>;
public static where<T extends object>(field: string, value: any): Query<T>;
public static where<T extends object>(
field: string, operator: Driver.Operator, value: any): Query<T>;
public static where(field: string, operator: Driver.Operator | any, value?: any) {
public static where(field: any, operator?: Driver.Operator | any, value?: any) {
return this.query().where(field, operator, value);
}

Expand Down
49 changes: 37 additions & 12 deletions src/drivers/Driver.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,53 @@
import * as mongodb from "mongodb";
import * as connections from "../connections";

module Driver {
namespace Driver {
export type Callback<T = any> = (error: Error, result: T) => void;

export type Operator = "<" | "<=" | "=" | "<>" | ">=" | ">";

export type Order = "asc" | "desc";

export type Id = number | mongodb.ObjectId;
export type Id = number | string;

export interface GroupQueryObject<T = any> {
having: (field: string, operator: Operator | any, value?: any) => GroupQueryObject<T>;
export interface Filter {
where(query: FilterQuery): this;
where(field: string, value: any): this;
where(field: string, operator: Driver.Operator, value: any): this;

orWhere(query: FilterQuery): this;
orWhere(field: string, value: any): this;
orWhere(field: string, operator: Driver.Operator, value: any): this;

whereLike(field: string, value: any): this;

whereNotLike(field: string, value: any): this;

whereIn(field: string, values: any[]): this;

whereNotIn(field: string, values: any[]): this;

whereBetween(field: string, start: any, end: any): this;

whereNotBetween(field: string, start: any, end: any): this;

whereNull(field: string): this;

whereNotNull(field: string): this;
}

export type GroupQuery<T = any> = (query: GroupQueryObject<T>) => void;
export type FilterQuery = (query: Filter) => Filter;

export interface JoinQueryObject<T = any> {
on: (field: string, operator: Operator | any, value?: any) => JoinQueryObject<T>;
export interface Join<T = any> extends Filter {
join(table: string, query?: Driver.JoinQuery<T>, as?: string): this;
}

export type JoinQuery<T = any> = (query: JoinQueryObject<T>) => void;
export type JoinQuery<T = any> = (query: Join<T>) => Join<T>;

export interface GroupQueryObject<T = any> {
having: (field: string, operator: Operator | any, value?: any) => GroupQueryObject<T>;
}

export type WhereQuery<T = any> = (query: Driver<T>) => Driver<T>;
export type GroupQuery<T = any> = (query: GroupQueryObject<T>) => void;

export type Mapper<T = any> = (item: T, index: number, items: T[]) => any;
}
Expand All @@ -44,11 +69,11 @@ abstract class Driver<T = any> {

/******************************* Where Clauses ******************************/

// abstract where(query: Driver.WhereQuery): this;
public abstract where(query: Driver.FilterQuery): this;
public abstract where(field: string, value: any): this;
public abstract where(field: string, operator: Driver.Operator, value: any): this;

// abstract orWhere(query: Driver.WhereQuery): this;
public abstract orWhere(query: Driver.FilterQuery): this;
public abstract orWhere(field: string, value: any): this;
public abstract orWhere(field: string, operator: Driver.Operator, value: any): this;

Expand Down
Loading

0 comments on commit 9506b09

Please sign in to comment.