Skip to content

Commit

Permalink
Merge pull request #169 from hemedani/main
Browse files Browse the repository at this point in the history
🏷️ export relation typesafety
  • Loading branch information
hemedani authored Jun 11, 2024
2 parents b046a38 + 88e245e commit 7ab83f7
Show file tree
Hide file tree
Showing 10 changed files with 46 additions and 35 deletions.
11 changes: 10 additions & 1 deletion src/models/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Struct } from "../npmDeps.ts";
import { ObjectId, Struct } from "../npmDeps.ts";

/**
* PureModel is interface of pure feature,
Expand Down Expand Up @@ -39,6 +39,15 @@ export interface IRelationsFileds {
[key: string]: TRelation;
}

export type TInsertRelations<T extends IRelationsFileds> = {
[mainKey in keyof T]?: {
_ids: ObjectId | ObjectId[];
relatedRelations?: {
[key in keyof T[mainKey]["relatedRelations"]]: boolean;
};
};
};

/**
* if schema has relation with other schema and in SQL that we keep foreign key.
* store in InRelation feature
Expand Down
11 changes: 1 addition & 10 deletions src/odm/insert/insertMany.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,13 @@ import {
} from "../../npmDeps.ts";
import { IRelationsFileds } from "../../mod.ts";
import { createProjection } from "../../models/createProjection.ts";
import { schemaFns, TSchemas } from "../../models/mod.ts";
import { schemaFns, TInsertRelations, TSchemas } from "../../models/mod.ts";
import { throwError } from "../../utils/mod.ts";
import { Projection } from "../aggregation/type.ts";
import { findOne } from "../find/findOne.ts";
import { find } from "../find/find.ts";
import { filterDocsByProjection } from "../utils/filterDocsByProjection.ts";

export type TInsertRelations<T extends IRelationsFileds> = {
[mainKey in keyof T]?: {
_ids: ObjectId | ObjectId[];
relatedRelations?: {
[key in keyof T[mainKey]["relatedRelations"]]: boolean;
};
};
};

export const insertMany = async <
TR extends IRelationsFileds,
PureFields extends Document,
Expand Down
11 changes: 1 addition & 10 deletions src/odm/insert/insertOne.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,11 @@ import {
} from "../../npmDeps.ts";
import { IRelationsFileds } from "../../mod.ts";
import { createProjection } from "../../models/createProjection.ts";
import { schemaFns, TSchemas } from "../../models/mod.ts";
import { schemaFns, TInsertRelations, TSchemas } from "../../models/mod.ts";
import { throwError } from "../../utils/mod.ts";
import { Projection } from "../aggregation/type.ts";
import { handleInsertOne } from "../utils/insert/mod.ts";

export type TInsertRelations<T extends IRelationsFileds> = {
[mainKey in keyof T]?: {
_ids: ObjectId | ObjectId[];
relatedRelations?: {
[key in keyof T[mainKey]["relatedRelations"]]: boolean;
};
};
};

export const insertOne = async <
TR extends IRelationsFileds,
PureFields extends Document,
Expand Down
9 changes: 7 additions & 2 deletions src/odm/newModel/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@ import {
optional,
UpdateFilter,
} from "../../mod.ts";
import { IPureFields, schemaFns, TSchemas } from "../../models/mod.ts";
import {
IPureFields,
schemaFns,
TInsertRelations,
TSchemas,
} from "../../models/mod.ts";
import {
BulkWriteOptions,
CountDocumentsOptions,
Expand All @@ -27,7 +32,7 @@ import { deleteOne } from "../delete/deleteOne.ts";
import { aggregation } from "../find/aggregation.ts";
import { find } from "../find/find.ts";
import { findOne } from "../find/findOne.ts";
import { insertOne, TInsertRelations } from "../insert/insertOne.ts";
import { insertOne } from "../insert/insertOne.ts";
import { addRelation } from "../relation/addRelation.ts";
import { insertMany } from "../insert/insertMany.ts";
import { removeRelation } from "../relation/removeRelation.ts";
Expand Down
8 changes: 6 additions & 2 deletions src/odm/relation/addRelation.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import { Db, Document, Filter } from "../../npmDeps.ts";
import { createProjection } from "../../models/createProjection.ts";
import { IRelationsFileds, schemaFns, TSchemas } from "../../models/mod.ts";
import {
IRelationsFileds,
schemaFns,
TInsertRelations,
TSchemas,
} from "../../models/mod.ts";
import { throwError } from "../../utils/throwError.ts";
import { Projection } from "../aggregation/type.ts";
import { TInsertRelations } from "../insert/insertOne.ts";
import { handleMultiRelation } from "../utils/insert/handleMultiRelation.ts";
import { handleSingleRelation } from "../utils/insert/handleSingleRelation.ts";
import { processRemoveRelatedRelations } from "../utils/processRemoveRelatedRelations.ts";
Expand Down
8 changes: 6 additions & 2 deletions src/odm/relation/removeRelation.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import { Db, Document, Filter, ObjectId } from "../../npmDeps.ts";
import { IRelationsFileds, schemaFns, TSchemas } from "../../models/mod.ts";
import {
IRelationsFileds,
schemaFns,
TInsertRelations,
TSchemas,
} from "../../models/mod.ts";
import { Projection } from "../aggregation/type.ts";
import { TInsertRelations } from "../insert/insertOne.ts";
import { throwError } from "../../utils/throwError.ts";
import { createProjection } from "../../models/createProjection.ts";
import { filterDocByProjection } from "../utils/filterDocByProjection.ts";
Expand Down
8 changes: 6 additions & 2 deletions src/odm/utils/insert/handleInsertOne.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { Db, IModel, IRelationsFileds } from "../../../mod.ts";
import { TInsertRelations } from "../../insert/insertOne.ts";
import {
Db,
IModel,
IRelationsFileds,
TInsertRelations,
} from "../../../mod.ts";
import { handleMultiRelation } from "./handleMultiRelation.ts";
import { handleSingleRelation } from "./handleSingleRelation.ts";

Expand Down
3 changes: 1 addition & 2 deletions src/odm/utils/insert/handleMultiRelation.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { IModel, IRelationsFileds } from "../../../mod.ts";
import { IModel, IRelationsFileds, TInsertRelations } from "../../../mod.ts";
import { Db, ObjectId } from "../../../npmDeps.ts";
import { throwError } from "../../../utils/mod.ts";
import { find } from "../../find/find.ts";
import { TInsertRelations } from "../../insert/insertOne.ts";
import { filterDocByProjection } from "../filterDocByProjection.ts";
import { filterDocsByProjection } from "../filterDocsByProjection.ts";
import { generateUpdateFilter } from "../generateUpdateFilter.ts";
Expand Down
3 changes: 1 addition & 2 deletions src/odm/utils/insert/handleSingleRelation.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { IModel, IRelationsFileds } from "../../../mod.ts";
import { IModel, IRelationsFileds, TInsertRelations } from "../../../mod.ts";
import { Db, ObjectId } from "../../../npmDeps.ts";
import { throwError } from "../../../utils/mod.ts";
import { findOne } from "../../find/findOne.ts";
import { TInsertRelations } from "../../insert/insertOne.ts";
import { filterDocByProjection } from "../filterDocByProjection.ts";
import { proccessRelatedRelation } from "./proccessRelatedRelation.ts";

Expand Down
9 changes: 7 additions & 2 deletions src/odm/utils/processRemoveRelatedRelations.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { Db, Document, UpdateFilter } from "../../npmDeps.ts";
import { IModel, IRelationsFileds, ObjectId, WithId } from "../../mod.ts";
import { TInsertRelations } from "../insert/insertOne.ts";
import {
IModel,
IRelationsFileds,
ObjectId,
TInsertRelations,
WithId,
} from "../../mod.ts";
import { generateRemoveRelatedRelationFilter } from "./generateRemoveRelationRelationFilter.ts";

export const processRemoveRelatedRelations = async <
Expand Down

0 comments on commit 7ab83f7

Please sign in to comment.