@oridune/epic-odm
Advanced tools
Comparing version 1.0.28 to 1.0.29
{ | ||
"name": "@oridune/epic-odm", | ||
"version": "1.0.28", | ||
"version": "1.0.29", | ||
"description": "Install 1 ODM and code once with any database driver.", | ||
@@ -5,0 +5,0 @@ "main": "./src/index.js", |
@@ -31,5 +31,6 @@ "use strict"; | ||
const exceptions_1 = require("../lib/exceptions"); | ||
const operators_1 = require("../lib/operators"); | ||
const utils_1 = require("../lib/utils"); | ||
// Project Resolver Function | ||
const normalizeProject = (project) => { | ||
const NormalizeProject = (project) => { | ||
project = utils_1.Utils.transposeObjectToDotNotation(project); | ||
@@ -43,3 +44,3 @@ if (Object.values(project).includes(1)) | ||
// Reference Stages Creator Function | ||
const createReferenceStages = (model, conditions, project) => { | ||
const CreateReferenceStages = (model, project) => { | ||
// Get Model Options | ||
@@ -62,3 +63,3 @@ const ModelOptions = (0, model_1.getModelOptions)(model); | ||
as: relation.name, | ||
pipeline: createReferenceStages(relation.reference(), conditions, ProjectItem), | ||
pipeline: CreateReferenceStages(relation.reference(), ProjectItem), | ||
}, | ||
@@ -80,2 +81,101 @@ }, | ||
}; | ||
const TransposeToNative = (objects) => { | ||
return objects.map((object) => { | ||
const Results = {}; | ||
const Transpose = (object, prefix) => { | ||
Object.keys(object).forEach((key) => { | ||
const Key = prefix ? `${prefix}.${key}` : key; | ||
const Target = object[key]; | ||
if (Target !== undefined) { | ||
if (typeof Target === "object" && | ||
Target !== null && | ||
!(Target instanceof Array) && | ||
!(Target instanceof require("mongodb").ObjectId) && | ||
!(Target instanceof operators_1.Operator) && | ||
!(Target instanceof RegExp)) | ||
Transpose(Target, Key); | ||
else if (Target instanceof operators_1.Operator) | ||
switch (Target.Type) { | ||
// Operators | ||
case "EQ": | ||
Results[Key] = { $eq: Target.Value }; | ||
break; | ||
case "NE": | ||
Results[Key] = { $ne: Target.Value }; | ||
break; | ||
case "GT": | ||
Results[Key] = { $gt: Target.Value }; | ||
break; | ||
case "ST": | ||
Results[Key] = { $lt: Target.Value }; | ||
break; | ||
case "GE": | ||
Results[Key] = { $gte: Target.Value }; | ||
break; | ||
case "SE": | ||
Results[Key] = { $lte: Target.Value }; | ||
break; | ||
case "NG": | ||
Results[Key] = { $not: { $gt: Target.Value } }; | ||
break; | ||
case "NS": | ||
Results[Key] = { $not: { $lt: Target.Value } }; | ||
break; | ||
case "RG": | ||
Results[Key] = { $regex: Target.Value }; | ||
break; | ||
case "IN": | ||
Results[Key] = { $in: Target.Value }; | ||
break; | ||
case "NI": | ||
Results[Key] = { $nin: Target.Value }; | ||
break; | ||
case "BT": | ||
Results[Key] = { | ||
$gte: Target.Value[0], | ||
$lte: Target.Value[1], | ||
}; | ||
break; | ||
case "NB": | ||
Results[Key] = { | ||
$not: { | ||
$gte: Target.Value[0], | ||
$lte: Target.Value[1], | ||
}, | ||
}; | ||
break; | ||
case "SetTo": | ||
Results["$set"] = { | ||
[Key]: Target.Value, | ||
}; | ||
break; | ||
case "ReplaceWith": | ||
Results["$replaceWith"] = { | ||
[Key]: Target.Value, | ||
}; | ||
break; | ||
case "IsArrayEmpty": | ||
Results[Key] = { | ||
$in: [null, []], | ||
}; | ||
break; | ||
case "NotArrayEmpty": | ||
Results[Key] = { | ||
$exists: true, | ||
$not: { $size: 0 }, | ||
}; | ||
break; | ||
default: | ||
Results[Key] = Target.Value; | ||
break; | ||
} | ||
else | ||
Results[Key] = Target; // Raw Injection | ||
} | ||
}); | ||
}; | ||
Transpose(object); | ||
return Results; | ||
}); | ||
}; | ||
class MongoDBDriver extends driver_1.DatabaseDriver { | ||
@@ -210,3 +310,3 @@ constructor(models, uri, Config = {}, Logs = false) { | ||
// Normalize Projection | ||
const Projection = normalizeProject(project.getFieldsList()); | ||
const Projection = NormalizeProject(project.getFieldsList()); | ||
// Create a Cursor | ||
@@ -233,3 +333,3 @@ const Cursor = Collection.aggregate([ | ||
$match: { | ||
$or: (condition instanceof Array ? condition : [condition]).map((condition) => [ | ||
$or: TransposeToNative((condition instanceof Array ? condition : [condition]).map((condition) => [ | ||
...Object.keys(ModelOptions.fields), | ||
@@ -239,8 +339,10 @@ ...Object.keys(ModelOptions.embeds), | ||
const Value = condition[field]; | ||
return Value === undefined ? obj : Object.assign(Object.assign({}, obj), { [field]: Value }); | ||
}, {})), | ||
return Value === undefined | ||
? obj | ||
: Object.assign(Object.assign({}, obj), { [field]: Value }); | ||
}, {}))), | ||
}, | ||
})), | ||
// Reference Stage | ||
...createReferenceStages(model, aggregate.where, project.getFieldsList()), | ||
...CreateReferenceStages(model, project.getFieldsList()), | ||
// Where Conditions Stage | ||
@@ -252,3 +354,5 @@ ...(aggregate.where instanceof Array | ||
: []).map((condition) => ({ | ||
$match: { $or: condition instanceof Array ? condition : [condition] }, | ||
$match: { | ||
$or: TransposeToNative(condition instanceof Array ? condition : [condition]), | ||
}, | ||
})), | ||
@@ -281,3 +385,5 @@ // Sorting Stage | ||
: []).map((condition) => ({ | ||
$match: { $or: condition instanceof Array ? condition : [condition] }, | ||
$match: { | ||
$or: TransposeToNative(condition instanceof Array ? condition : [condition]), | ||
}, | ||
})), | ||
@@ -303,3 +409,3 @@ ], { session: connection, allowDiskUse: true }); | ||
.collection(ModelOptions.getResolvedName()) | ||
.updateMany({ _id: { $in: targets.map((_) => _._id) } }, { $set: merge ? utils_1.Utils.transposeObjectToDotNotation(data) : data }, { session: connection }); | ||
.updateMany({ _id: { $in: targets.map((_) => _._id) } }, { $set: merge ? TransposeToNative([data])[0] : data }, { session: connection }); | ||
return { updatedCount: Results.modifiedCount }; | ||
@@ -306,0 +412,0 @@ } |
@@ -17,5 +17,6 @@ /** | ||
export * from "./lib/model/utils"; | ||
export * from "./lib/driver"; | ||
export * from "./lib/adapter"; | ||
export * from "./lib/database"; | ||
export * from "./lib/operators"; | ||
export * from "./lib/utils"; | ||
export * from "./drivers/mongodb"; | ||
export * from "./adapters/mongodb"; |
@@ -33,5 +33,6 @@ "use strict"; | ||
__exportStar(require("./lib/model/utils"), exports); | ||
__exportStar(require("./lib/driver"), exports); | ||
__exportStar(require("./lib/adapter"), exports); | ||
__exportStar(require("./lib/database"), exports); | ||
__exportStar(require("./lib/operators"), exports); | ||
__exportStar(require("./lib/utils"), exports); | ||
__exportStar(require("./drivers/mongodb"), exports); | ||
__exportStar(require("./adapters/mongodb"), exports); |
@@ -1,2 +0,2 @@ | ||
import { DatabaseDriver } from "./driver"; | ||
import { DatabaseAdapter } from "./adapter"; | ||
import { BaseModel } from "./model/base"; | ||
@@ -9,4 +9,4 @@ import { DatabaseCommand } from "./model/static"; | ||
} | ||
export declare class DatabaseSession<D extends DatabaseDriver<any, any>> { | ||
Driver: D; | ||
export declare class DatabaseSession<D extends DatabaseAdapter<any, any>> { | ||
Adapter: D; | ||
private Connection; | ||
@@ -21,3 +21,3 @@ private TransactionID; | ||
}): Promise<any[]>; | ||
constructor(Driver: D); | ||
constructor(Adapter: D); | ||
start(): Promise<this>; | ||
@@ -24,0 +24,0 @@ transaction<T extends any>(callback: () => T, rollback?: boolean): Promise<T>; |
@@ -9,4 +9,4 @@ "use strict"; | ||
class DatabaseSession { | ||
constructor(Driver) { | ||
this.Driver = Driver; | ||
constructor(Adapter) { | ||
this.Adapter = Adapter; | ||
this.TransactionID = null; | ||
@@ -97,7 +97,7 @@ } | ||
// Make a Connection if not exists | ||
if (!this.Driver.hasConnection()) | ||
await this.Driver.connect(); | ||
if (!this.Adapter.hasConnection()) | ||
await this.Adapter.connect(); | ||
// Get Connection | ||
if (!this.Connection) | ||
this.Connection = await this.Driver.getConnection(await this.Driver.getConnectionObject()); | ||
this.Connection = await this.Adapter.getConnection(await this.Adapter.getConnectionObject()); | ||
return this; | ||
@@ -119,3 +119,3 @@ } | ||
// Start a Transaction | ||
await this.Driver.startTransaction(this.Connection); | ||
await this.Adapter.startTransaction(this.Connection); | ||
// Own the Transaction | ||
@@ -129,4 +129,4 @@ Transaction.current = true; | ||
await (!rollback | ||
? this.Driver.commitTransaction(this.Connection) | ||
: this.Driver.rollbackTransaction(this.Connection)).catch((error) => console.log("Transaction Error:", error)); | ||
? this.Adapter.commitTransaction(this.Connection) | ||
: this.Adapter.rollbackTransaction(this.Connection)).catch((error) => console.log("Transaction Error:", error)); | ||
// Release Transaction | ||
@@ -143,3 +143,3 @@ this.TransactionID = null; | ||
// Rollback Transaction | ||
await this.Driver.rollbackTransaction(this.Connection); | ||
await this.Adapter.rollbackTransaction(this.Connection); | ||
console.log("Transaction Aborted!"); | ||
@@ -159,3 +159,3 @@ } | ||
case "create": | ||
return this.Driver._create(this.Connection, command, { | ||
return this.Adapter._create(this.Connection, command, { | ||
database: this, | ||
@@ -166,3 +166,3 @@ project: Project, | ||
case "createOne": | ||
return (await this.Driver._create(this.Connection, command, { | ||
return (await this.Adapter._create(this.Connection, command, { | ||
database: this, | ||
@@ -173,3 +173,3 @@ project: Project, | ||
case "find": | ||
return this.Driver._find(this.Connection, command, { | ||
return this.Adapter._find(this.Connection, command, { | ||
database: this, | ||
@@ -180,3 +180,3 @@ project: Project, | ||
case "findOne": | ||
return (await this.Driver._find(this.Connection, command, { | ||
return (await this.Adapter._find(this.Connection, command, { | ||
database: this, | ||
@@ -188,3 +188,3 @@ project: Project, | ||
case "save": | ||
return this.Driver._update(this.Connection, command, { | ||
return this.Adapter._update(this.Connection, command, { | ||
database: this, | ||
@@ -195,3 +195,3 @@ project: Project, | ||
case "delete": | ||
return this.Driver._delete(this.Connection, command, { | ||
return this.Adapter._delete(this.Connection, command, { | ||
database: this, | ||
@@ -206,3 +206,3 @@ project: Project, | ||
async end() { | ||
await this.Driver.endConnection(this.Connection); | ||
await this.Adapter.endConnection(this.Connection); | ||
return this; | ||
@@ -209,0 +209,0 @@ } |
import { BaseModel } from "./base"; | ||
import { Aggregation, ModelPayload, ModelToObject } from "./utils"; | ||
import { Aggregation, ModelPayload, Mutable } from "./utils"; | ||
import { GraphQLList, GraphQLObjectType } from "graphql"; | ||
@@ -11,3 +11,3 @@ export declare type DatabaseCommandType = "create" | "createOne" | "find" | "findOne" | "update" | "save" | "delete"; | ||
aggregate: Aggregation<M["prototype"]>; | ||
updates: ModelToObject<M["prototype"]>; | ||
updates: Mutable<M["prototype"]>; | ||
deletes: typeof BaseModel[]; | ||
@@ -24,5 +24,5 @@ } | ||
static findOne<M extends typeof BaseModel>(this: M, aggregate?: Aggregation<M["prototype"]> | BaseModel[] | BaseModel): DatabaseCommand<M, "findOne">; | ||
static update<M extends typeof BaseModel>(this: M, aggregate: BaseModel | BaseModel[] | Aggregation<M["prototype"]> | undefined, updates: ModelToObject<M["prototype"]>): DatabaseCommand<M, "update">; | ||
static update<M extends typeof BaseModel>(this: M, aggregate: BaseModel | BaseModel[] | Aggregation<M["prototype"]> | undefined, updates: Mutable<M["prototype"]>): DatabaseCommand<M, "update">; | ||
static delete<M extends typeof BaseModel>(this: M, aggregate?: Aggregation<M["prototype"]> | BaseModel[] | BaseModel, ...deletes: typeof BaseModel[]): DatabaseCommand<M, "delete">; | ||
static save<M extends typeof BaseModel>(this: M, items: M["prototype"] | M["prototype"][]): DatabaseCommand<M, "save">; | ||
} |
@@ -0,1 +1,2 @@ | ||
import { Operator, ValueType } from "../operators"; | ||
import { BaseModel } from "./base"; | ||
@@ -9,11 +10,14 @@ export declare type ModelPayload<M extends BaseModel> = { | ||
export declare type ModelReservedKeys = ""; | ||
export declare type ConditionValue = string | number | boolean | null | undefined; | ||
export declare type Conditions<T extends BaseModel> = ConditionsFrom<ModelToObject<T>>; | ||
export declare type ConditionsFrom<T extends ModelToObject<any>> = { | ||
[key in keyof T]: T[key] extends ConditionValue ? T[key] | Exclude<T[key], undefined>[] | object | DatabaseFunction<any, T[key]> : ConditionsFrom<T[key]>; | ||
[key in keyof T]?: T[key] extends ValueType ? T[key] | Exclude<T[key], undefined>[] | Operator<any, T[key]> : ConditionsFrom<T[key]>; | ||
}; | ||
export declare type Mutable<T extends BaseModel> = MutableFrom<ModelToObject<T>>; | ||
export declare type MutableFrom<T extends ModelToObject<any>> = { | ||
[key in keyof T]?: T[key] extends ValueType ? T[key] | Operator<any, T[key]> : MutableFrom<T[key]>; | ||
}; | ||
export declare type ModelToObject<T extends BaseModel> = { | ||
[key in Exclude<keyof T, ModelReservedKeys>]?: ModelValue<T[key]>; | ||
[key in Exclude<keyof T, ModelReservedKeys>]: ModelValue<T[key]>; | ||
}; | ||
export declare type ModelValue<T> = T extends Array<infer B> ? B extends BaseModel ? ModelToObject<B> : B extends ConditionValue ? B | object | DatabaseFunction<any, B> : ConditionValue | object | DatabaseFunction<any, any> : T extends BaseModel ? ModelToObject<T> : T extends ConditionValue ? T | object | DatabaseFunction<any, T> : ConditionValue | object | DatabaseFunction<any, any>; | ||
export declare type ModelValue<T> = T extends Array<infer B> ? B extends BaseModel ? ModelToObject<B> : ModelValue<B> : T extends BaseModel ? ModelToObject<T> : T extends ValueType ? T : ValueType; | ||
export interface Aggregation<M extends BaseModel> { | ||
@@ -38,6 +42,1 @@ search?: string; | ||
} | ||
export declare class DatabaseFunction<T, V> { | ||
Type: T; | ||
Arg: V[]; | ||
constructor(Type: T, Arg: V[]); | ||
} |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.DatabaseFunction = exports.ModelProjection = void 0; | ||
exports.ModelProjection = void 0; | ||
const model_1 = require("../decorators/model"); | ||
@@ -122,8 +122,1 @@ const utils_1 = require("../utils"); | ||
exports.ModelProjection = ModelProjection; | ||
class DatabaseFunction { | ||
constructor(Type, Arg) { | ||
this.Type = Type; | ||
this.Arg = Arg; | ||
} | ||
} | ||
exports.DatabaseFunction = DatabaseFunction; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
148072
42
3037