Skip to content

Commit

Permalink
Merge pull request #160 from hemedani/main
Browse files Browse the repository at this point in the history
✨ add createIndex to new model
  • Loading branch information
hemedani authored May 17, 2024
2 parents 85dc0d4 + 07de0fc commit e9fc37c
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 37 deletions.
68 changes: 32 additions & 36 deletions src/odm/mod.ts
Original file line number Diff line number Diff line change
@@ -1,48 +1,44 @@
import { Database } from "../deps.ts";
import {
IPureFields,
IRelationsFileds,
TRelation,
TSchemas,
} from "../models/mod.ts";
import { Db } from "../npmDeps.ts";
import { IPureFields, IRelationsFileds, TSchemas } from "../models/mod.ts";
import { schemaFns } from "../models/mod.ts";
import { assert, enums } from "../npmDeps.ts";
import { throwError } from "../utils/throwError.ts";
import { newModel } from "./newModel/mod.ts";
import { newModel, OptionType } from "./newModel/mod.ts";

export const odm = (schemasObj: TSchemas) => {
let mongoDb: Database;
let mongoDb: Db;

const setDb = (db: Database) => (mongoDb = db);
const setDb = (db: Db) => (mongoDb = db);

const getDbClient = () => mongoDb;
const getDbClient = () => mongoDb;

const getCollection = (collection: string) => {
const db = getDbClient();
const getSchemas = enums(schemaFns(schemasObj).getSchemasKeys());
assert(collection, getSchemas);
return db
? db.collection(collection)
: throwError("No database connection");
};
const getCollection = (collection: string) => {
const db = getDbClient();
const getSchemas = enums(schemaFns(schemasObj).getSchemasKeys());
assert(collection, getSchemas);
return db
? db.collection(collection)
: throwError("No database connection");
};

return {
setDb,
getCollection,
newModel: <PF extends IPureFields, TR extends IRelationsFileds>(
name: string,
pureFields: PF,
relations: TR,
options?: { excludes?: Partial<(keyof PF)>[] },
) => newModel<PF, TR>(
mongoDb,
schemasObj,
name,
pureFields,
relations,
options,
),
};
return {
setDb,
getCollection,
newModel: <PF extends IPureFields, TR extends IRelationsFileds>(
name: string,
pureFields: PF,
relations: TR,
options?: OptionType<PF>,
) =>
newModel<PF, TR>(
mongoDb,
schemasObj,
name,
pureFields,
relations,
options,
),
};
};

export * from "./utils/mod.ts";
19 changes: 18 additions & 1 deletion src/odm/newModel/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@ import { IPureFields, schemaFns, TSchemas } from "../../models/mod.ts";
import {
BulkWriteOptions,
CountDocumentsOptions,
CreateIndexesOptions,
Db,
DeleteOptions,
Document,
FindOneAndUpdateOptions,
IndexSpecification,
Infer,
InsertOneOptions,
OptionalUnlessRequiredId,
Expand All @@ -31,6 +33,14 @@ import { insertMany } from "../insert/insertMany.ts";
import { removeRelation } from "../relation/removeRelation.ts";
import { findOneAndUpdate } from "../update/findOneAndUpdate.ts";

export type OptionType<PF extends IPureFields> = {
createIndex?: {
indexSpec: IndexSpecification;
options?: CreateIndexesOptions;
};
excludes?: Partial<(keyof PF)>[];
};

export const newModel = <
PF extends IPureFields,
TR extends IRelationsFileds,
Expand All @@ -40,7 +50,7 @@ export const newModel = <
name: string,
pureFields: PF,
relations: TR,
options?: { excludes?: Partial<(keyof PF)>[] },
options?: OptionType<PF>,
) => {
type InferPureFieldsType = {
[key in keyof PF]?: Infer<PF[key]>;
Expand Down Expand Up @@ -89,6 +99,13 @@ export const newModel = <
options: (options as { excludes?: (string | number)[] }),
};

if (options && options.createIndex) {
db.collection(name).createIndex(
options.createIndex.indexSpec,
options.createIndex.options,
);
}

interface IFindModelInputs {
filters: Filter<Document>;
projection?: Projection;
Expand Down

0 comments on commit e9fc37c

Please sign in to comment.