@oridune/epic-odm
Advanced tools
Comparing version 1.0.4 to 1.0.5
{ | ||
"name": "@oridune/epic-odm", | ||
"version": "1.0.4", | ||
"version": "1.0.5", | ||
"description": "Install 1 ODM and code once with any database driver.", | ||
@@ -33,2 +33,3 @@ "main": "./src/index.js", | ||
"@types/node": "^17.0.21", | ||
"graphql": "^16.5.0", | ||
"jest": "^28.1.0", | ||
@@ -35,0 +36,0 @@ "mongodb": "^4.4.1", |
@@ -6,2 +6,4 @@ import { BaseModel } from "../model/base"; | ||
name: string; | ||
/** Description of the Field */ | ||
description?: string; | ||
/** Custom Field Type */ | ||
@@ -8,0 +10,0 @@ type: FieldDataTypes; |
@@ -13,2 +13,3 @@ import { BaseModel } from "../model/base"; | ||
name: string; | ||
description?: string; | ||
indexes: Record<string, IndexInterface<M>> | (() => Record<string, IndexInterface<M>>); | ||
@@ -15,0 +16,0 @@ onInit?: () => any; |
import { BaseModel } from "./base"; | ||
import { Aggregation, ModelPayload, ModelToObject } from "./utils"; | ||
import { GraphQLObjectType } from "graphql"; | ||
export declare type DatabaseCommandType = "create" | "createOne" | "find" | "findOne" | "update" | "save" | "delete"; | ||
@@ -14,3 +15,5 @@ export interface DatabaseCommand<M extends typeof BaseModel, T extends DatabaseCommandType> { | ||
export declare class BaseModelStatic { | ||
static graphQLObject?: GraphQLObjectType; | ||
static new<M extends typeof BaseModel>(this: M, data: ModelPayload<M["prototype"]>): M["prototype"]; | ||
static toGraphQLObject<M extends typeof BaseModel>(this: M): any; | ||
static create<M extends typeof BaseModel>(this: M, payload: ModelPayload<M["prototype"]>[]): DatabaseCommand<M, "create">; | ||
@@ -17,0 +20,0 @@ static createOne<M extends typeof BaseModel>(this: M, payload: ModelPayload<M["prototype"]>): DatabaseCommand<M, "createOne">; |
@@ -5,2 +5,3 @@ "use strict"; | ||
const base_1 = require("./base"); | ||
const model_1 = require("../decorators/model"); | ||
class BaseModelStatic { | ||
@@ -15,2 +16,47 @@ static new(data) { | ||
} | ||
static toGraphQLObject() { | ||
// Return from memory if already created. | ||
if (this.graphQLObject instanceof require("graphql").GraphQLObjectType) | ||
return this.graphQLObject; | ||
// Get Model Options | ||
const ModelOptions = model_1.getModelOptions(this); | ||
// Create Object Type From Model | ||
return (this.graphQLObject = new (require("graphql").GraphQLObjectType)({ | ||
name: ModelOptions.name, | ||
description: ModelOptions.description, | ||
fields: () => { | ||
// Empty Fields | ||
const Fields = {}; | ||
// Create Fields | ||
for (const Field of Object.values(ModelOptions.fields)) | ||
Fields[Field.options.name] = { | ||
type: Field.dataType === Number | ||
? require("graphql").GraphQLFloat | ||
: Field.dataType === Boolean | ||
? require("graphql").GraphQLBoolean | ||
: Field.dataType === Object || Field.dataType === Array | ||
? new (require("graphql").GraphQLScalarType)({ | ||
name: "Object", | ||
description: "Unknown object type.", | ||
}) | ||
: require("graphql").GraphQLString, | ||
}; | ||
// Embed to Fields | ||
for (const Embed of Object.values(ModelOptions.embeds)) | ||
Fields[Embed.options.name] = { type: Embed.model.toGraphQLObject() }; | ||
// Relation to Fields | ||
for (const Relation of Object.values(ModelOptions.relations)) | ||
Fields[Relation.name] = { | ||
type: ["OneToOne", "ManyToOne"].includes(Relation.type) | ||
? Relation.reference() === this | ||
? this.graphQLObject | ||
: Relation.reference().toGraphQLObject() | ||
: Relation.reference() === this | ||
? new (require("graphql").GraphQLList)(this.graphQLObject) | ||
: new (require("graphql").GraphQLList)(Relation.reference().toGraphQLObject()), | ||
}; | ||
return Fields; | ||
}, | ||
})); | ||
} | ||
static create(payload) { | ||
@@ -17,0 +63,0 @@ return { |
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
90620
1845
11