Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
ardalanamini committed Nov 16, 2018
1 parent 3991439 commit acf3024
Show file tree
Hide file tree
Showing 12 changed files with 95 additions and 2 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.4.0](https://github.com/foxifyjs/odin/releases/tag/v0.4.0) - *(2018-11-16)*

- :zap: Added `morphOne` relation
- :star2: Improved `toJsonSchema` functionality

## [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
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ User.schema = {
- [ ] `deleted`
- [ ] `restored`
- [ ] Relationships
- [x] `embedMany`
- [x] `hasMany`
- [x] `hasOne`
- [ ] `hasManyThrough`
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@foxify/odin",
"version": "0.3.1",
"version": "0.4.0",
"description": "Active Record Model",
"author": "Ardalan Amini <ardalanamini22@gmail.com> [https://github.com/ardalanamini]",
"contributors": [
Expand Down
21 changes: 21 additions & 0 deletions src/Model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,24 @@ class Model<T extends object = {}> extends Base<T>

const jsonSchema = jsonSchemaGenerator(this.schema);

if (this.timestamps) {
jsonSchema.properties[this.CREATED_AT] = {
type: "string",
};

jsonSchema.properties[this.UPDATED_AT] = {
type: "string",
};

jsonSchema.required.push(this.CREATED_AT);
}

if (this.softDelete) {
jsonSchema.properties[this.DELETED_AT] = {
type: "string",
};
}

for (const key of Object.getOwnPropertyNames(this.prototype)) {
if (key === "constructor") continue;

Expand All @@ -163,6 +181,9 @@ class Model<T extends object = {}> extends Base<T>

jsonSchema.properties[key] = {
type: "array",
items: {
type: "object",
},
};
}
}
Expand Down
13 changes: 13 additions & 0 deletions src/base/Relational.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,19 @@ interface Relational<T extends object = {}> extends Base<T> {
}

class Relational extends Base {
public embedMany<T extends Model = any>(
relation: string | typeof Model,
localKey?: string,
foreignKey?: string
): Relation<T> {
if (utils.string.isString(relation))
relation = this.constructor.models[relation] as typeof Model;

const EmbedMany: any = drivers[this.constructor.driver].Relation.EmbedMany;

return new EmbedMany(this, relation, localKey, foreignKey, this.embedMany);
}

public hasMany<T extends Model = any>(
relation: string | typeof Model,
localKey?: string,
Expand Down
6 changes: 6 additions & 0 deletions src/drivers/Driver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ namespace Driver {

export interface Join<T = any> extends Filter {
join(table: string, query?: Driver.JoinQuery<T>, as?: string): this;

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

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

export type JoinQuery<T = any> = (query: Join<T>) => Join<T>;
Expand Down
14 changes: 14 additions & 0 deletions src/drivers/MongoDB/Driver/Join.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,20 @@ class Join<T = any> extends Filter {

return this;
}

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

public whereIn(field: string, embeddedField: string): this;
public whereIn(field: string, values: any[]): this;
public whereIn(field: string, values: any) {
return super.whereIn(field, values);
}

public whereNotIn(field: string, embeddedField: string): this;
public whereNotIn(field: string, values: any[]): this;
public whereNotIn(field: string, values: any) {
return super.whereNotIn(field, values);
}
}

export default Join;
14 changes: 14 additions & 0 deletions src/drivers/MongoDB/Relation/EmbedMany.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import Query from "../../../base/Query";
import { EmbedMany as Base } from "../../Relation";

class EmbedMany<T extends object = {}> extends Base {
public load(query: Query<T>) {
return query.join(
this.relation,
q => q.whereIn(this.foreignKey, `${this.model.constructor.toString()}.${this.localKey}`),
this.as
);
}
}

export default EmbedMany;
1 change: 1 addition & 0 deletions src/drivers/MongoDB/Relation/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export { default as EmbedMany } from "./EmbedMany";
export { default as HasMany } from "./HasMany";
export { default as HasOne } from "./HasOne";
export { default as MorphMany } from "./MorphMany";
Expand Down
17 changes: 17 additions & 0 deletions src/drivers/Relation/EmbedMany.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import * as Model from "../../index";
import * as utils from "../../utils";
import HasMany from "../Relation/HasMany";

abstract class EmbedMany extends HasMany {
constructor(
model: Model,
relation: typeof Model,
localKey: string = `${utils.makeTableId(relation.toString())}s`,
foreignKey: string = "id",
caller: (...args: any[]) => any
) {
super(model, relation, localKey, foreignKey, caller);
}
}

export default EmbedMany;
1 change: 1 addition & 0 deletions src/drivers/Relation/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export { default as EmbedMany } from "./EmbedMany";
export { default as HasMany } from "./HasMany";
export { default as HasOne } from "./HasOne";
export { default as MorphMany } from "./MorphMany";
Expand Down

0 comments on commit acf3024

Please sign in to comment.