@foxify/odin
Advanced tools
Comparing version 0.1.0-beta.18 to 0.1.0-beta.19
@@ -34,4 +34,8 @@ /* | ||
declare class Base<T = any> { | ||
static readonly models: { | ||
[name: string]: (import("./utils").ClassInterface & typeof import("./Model").default & typeof import("./base/QueryBuilder").default & typeof import("./base/Relational").default & typeof import("./GraphQL/Model").default) | undefined; | ||
}; | ||
static isOdin: (arg: any) => arg is Odin<any>; | ||
static register: (...models: (import("./utils").ClassInterface & typeof import("./Model").default & typeof import("./base/QueryBuilder").default & typeof import("./base/Relational").default & typeof import("./GraphQL/Model").default)[]) => void; | ||
} | ||
export default Base; |
@@ -31,6 +31,15 @@ /* | ||
const e = require("."); | ||
const e = require("."), s = {}; | ||
class Base {} | ||
class Base { | ||
static get models() { | ||
return s; | ||
} | ||
} | ||
Base.isOdin = (s => s instanceof e), exports.default = Base; | ||
Base.isOdin = (s => s instanceof e), Base.register = ((...e) => { | ||
e.forEach(e => { | ||
if (s[e.name]) throw new Error(`Model "${e.name}" already exists`); | ||
s[e.name] = e; | ||
}); | ||
}), exports.default = Base; |
@@ -25,11 +25,11 @@ /* | ||
import * as Model from ".."; | ||
import Base from "../Base"; | ||
import Relation from "../drivers/Relation/Base"; | ||
import * as Model from "../index"; | ||
declare class Relational<T = any> extends Base<T> { | ||
hasMany(relation: typeof Model, localKey?: string, foreignKey?: string): Relation<T>; | ||
hasOne(relation: typeof Model, localKey?: string, foreignKey?: string): Relation<T>; | ||
morphMany(relation: typeof Model, localKey?: string, type?: string): Relation<T>; | ||
morphOne(relation: typeof Model, localKey?: string, type?: string): Relation<T>; | ||
declare class Relational extends Base { | ||
hasMany<T>(relation: string | typeof Model, localKey?: string, foreignKey?: string): Relation<T>; | ||
hasOne<T>(relation: string | typeof Model, localKey?: string, foreignKey?: string): Relation<T>; | ||
morphMany<T>(relation: string | typeof Model, localKey?: string, type?: string): Relation<T>; | ||
morphOne<T>(relation: string | typeof Model, localKey?: string, type?: string): Relation<T>; | ||
} | ||
export default Relational; |
@@ -31,17 +31,17 @@ /* | ||
const e = require("../Base"), r = require("../drivers"); | ||
const t = require("../Base"), r = require("../drivers"), s = require("../utils"); | ||
exports.default = class Relational extends e.default { | ||
hasMany(e, t, s) { | ||
return new (0, r[this.constructor.driver].Relation.HasMany)(this, e, t, s, this.hasMany); | ||
exports.default = class Relational extends t.default { | ||
hasMany(t, e, i) { | ||
return s.string.isString(t) && (t = this.constructor.models[t]), new (0, r[this.constructor.driver].Relation.HasMany)(this, t, e, i, this.hasMany); | ||
} | ||
hasOne(e, t, s) { | ||
return new (0, r[this.constructor.driver].Relation.HasOne)(this, e, t, s, this.hasOne); | ||
hasOne(t, e, i) { | ||
return s.string.isString(t) && (t = this.constructor.models[t]), new (0, r[this.constructor.driver].Relation.HasOne)(this, t, e, i, this.hasOne); | ||
} | ||
morphMany(e, t, s) { | ||
return new (0, r[this.constructor.driver].Relation.MorphMany)(this, e, t, void 0, s, this.morphMany); | ||
morphMany(t, e, i) { | ||
return s.string.isString(t) && (t = this.constructor.models[t]), new (0, r[this.constructor.driver].Relation.MorphMany)(this, t, e, void 0, i, this.morphMany); | ||
} | ||
morphOne(e, t, s) { | ||
return new (0, r[this.constructor.driver].Relation.MorphOne)(this, e, t, void 0, s, this.morphOne); | ||
morphOne(t, e, i) { | ||
return s.string.isString(t) && (t = this.constructor.models[t]), new (0, r[this.constructor.driver].Relation.MorphOne)(this, t, e, void 0, i, this.morphOne); | ||
} | ||
}; |
@@ -33,5 +33,5 @@ /* | ||
import * as Types from "./types"; | ||
interface Model<T = any> extends QueryBuilder<T>, Relational<T>, GraphQLInstance<T> { | ||
interface Model<T = any> extends QueryBuilder<T>, Relational, GraphQLInstance<T> { | ||
} | ||
declare class Model<T = any> extends Base<T> implements QueryBuilder<T>, Relational<T>, GraphQLInstance { | ||
declare class Model<T = any> extends Base<T> implements QueryBuilder<T>, Relational, GraphQLInstance { | ||
static DB: typeof DB; | ||
@@ -53,6 +53,2 @@ static GraphQL: GraphQL; | ||
static readonly driver: "MongoDB"; | ||
static readonly models: { | ||
[name: string]: Odin<any> | undefined; | ||
}; | ||
static register: (...models: Odin<any>[]) => void; | ||
static toString(): any; | ||
@@ -59,0 +55,0 @@ static validate<T = object>(document: T, updating?: boolean): { |
@@ -31,8 +31,8 @@ /* | ||
const e = require("./Base"), t = require("./connections"), s = require("./DB"), r = require("./GraphQL"), i = require("./types"), o = require("./types/Any"), a = require("./utils"), n = {}; | ||
const t = require("./Base"), e = require("./connections"), s = require("./DB"), r = require("./GraphQL"), i = require("./types"), o = require("./types/Any"), a = require("./utils"); | ||
// @utils.mixins(QueryBuilder, Relational, GraphQLInstance) | ||
class Model extends e.default { | ||
constructor(e = {}) { | ||
super(), this._isNew = !1, this.attributes = {}, e.id || (this._isNew = !0), this._setAttributes(e); | ||
class Model extends t.default { | ||
constructor(t = {}) { | ||
super(), this._isNew = !1, this.attributes = {}, t.id || (this._isNew = !0), this._setAttributes(t); | ||
} | ||
@@ -44,8 +44,8 @@ static get _table() { | ||
// TODO: id | ||
const e = Object.assign({ | ||
const t = Object.assign({ | ||
id: this.Types.ObjectId | ||
}, this.schema); | ||
return this.timestamps && (e[this.CREATED_AT] = this.Types.Date.default(() => new Date()), | ||
e[this.UPDATED_AT] = this.Types.Date), this.softDelete && (e[this.DELETED_AT] = this.Types.Date), | ||
e; | ||
return this.timestamps && (t[this.CREATED_AT] = this.Types.Date.default(() => new Date()), | ||
t[this.UPDATED_AT] = this.Types.Date), this.softDelete && (t[this.DELETED_AT] = this.Types.Date), | ||
t; | ||
} | ||
@@ -56,27 +56,24 @@ static get filename() { | ||
static get driver() { | ||
return t.getConnection(this.connection).driver; | ||
return e.getConnection(this.connection).driver; | ||
} | ||
static get models() { | ||
return n; | ||
} | ||
static toString() { | ||
return this._table; | ||
} | ||
static validate(e, t = !1) { | ||
const s = (e, r) => { | ||
static validate(t, e = !1) { | ||
const s = (t, r) => { | ||
const i = {}; | ||
let n = {}; | ||
for (const c in e) { | ||
const u = e[c]; | ||
let l = r[c]; | ||
for (const c in t) { | ||
const u = t[c]; | ||
let h = r[c]; | ||
if (u instanceof o.default) { | ||
// Type | ||
const e = u.validate(l, t); | ||
e.value && (i[c] = e.value), e.errors && (n[c] = e.errors); | ||
const t = u.validate(h, e); | ||
t.value && (i[c] = t.value), t.errors && (n[c] = t.errors); | ||
} else { | ||
// Object | ||
l || (l = {}); | ||
const e = s(u, l); | ||
if (e.errors) for (const t in e.errors) n[`${c}.${t}`] = e.errors[t]; | ||
a.object.size(e.value) > 0 && (i[c] = e.value); | ||
h || (h = {}); | ||
const t = s(u, h); | ||
if (t.errors) for (const e in t.errors) n[`${c}.${e}`] = t.errors[e]; | ||
a.object.size(t.value) > 0 && (i[c] = t.value); | ||
} | ||
@@ -88,25 +85,25 @@ } | ||
}; | ||
}, r = s(this._schema, e); | ||
if (r.errors && t && (a.object.forEach(r.errors, (e, t) => { | ||
1 === e.length && "Must be provided" === e[0] && delete r.errors[t]; | ||
}, r = s(this._schema, t); | ||
if (r.errors && e && (a.object.forEach(r.errors, (t, e) => { | ||
1 === t.length && "Must be provided" === t[0] && delete r.errors[e]; | ||
}), 0 === a.object.size(r.errors) && (r.errors = null)), r.errors) throw r.errors; | ||
const i = r.value; | ||
return t && this.timestamps && (i[this.UPDATED_AT] = new Date()), i; | ||
return e && this.timestamps && (i[this.UPDATED_AT] = new Date()), i; | ||
} | ||
_setAttributes(e) { | ||
const t = this.constructor._schema, s = [], r = []; | ||
for (const e in t) { | ||
const t = a.getGetterName(e), i = this[t] || (e => e); | ||
a.define(this, "get", e, () => i(this.attributes[e])), s.push(t); | ||
const o = a.getSetterName(e), n = this[o] || (e => e); | ||
a.define(this, "set", e, t => this.attributes[e] = n(t)), r.push(o); | ||
_setAttributes(t) { | ||
const e = this.constructor._schema, s = [], r = []; | ||
for (const t in e) { | ||
const e = a.getGetterName(t), i = this[e] || (t => t); | ||
a.define(this, "get", t, () => i(this.attributes[t])), s.push(e); | ||
const o = a.getSetterName(t), n = this[o] || (t => t); | ||
a.define(this, "set", t, e => this.attributes[t] = n(e)), r.push(o); | ||
} | ||
a.object.forEach(e, (e, t) => { | ||
-1 === r.indexOf(t) ? this.attributes[t] = e : this[t] = e; | ||
a.object.forEach(t, (t, e) => { | ||
-1 === r.indexOf(e) ? this.attributes[e] = t : this[e] = t; | ||
}), | ||
// virtual attributes | ||
Object.getOwnPropertyNames(this.constructor.prototype).forEach(e => { | ||
if (-1 !== s.indexOf(e)) return; | ||
const t = /^get([A-Z].*)Attribute$/.exec(e); | ||
t && a.define(this, "get", a.string.snakeCase(t[1]), this[e]); | ||
Object.getOwnPropertyNames(this.constructor.prototype).forEach(t => { | ||
if (-1 !== s.indexOf(t)) return; | ||
const e = /^get([A-Z].*)Attribute$/.exec(t); | ||
e && a.define(this, "get", a.string.snakeCase(e[1]), this[t]); | ||
}); | ||
@@ -118,4 +115,4 @@ } | ||
* @returns {*} | ||
*/ getAttribute(e) { | ||
return e.split(".").reduce((e, t) => e[t], this.attributes); | ||
*/ getAttribute(t) { | ||
return t.split(".").reduce((t, e) => t[e], this.attributes); | ||
} | ||
@@ -126,9 +123,9 @@ /** | ||
* @param {*} value | ||
*/ setAttribute(e, t) { | ||
a.object.set(this.attributes, e, t); | ||
*/ setAttribute(t, e) { | ||
a.object.set(this.attributes, t, e); | ||
} | ||
toJSON() { | ||
return a.object.mapValues(this.attributes, (e, t) => { | ||
const s = this[a.getGetterName(t)]; | ||
return s ? s(e) : e; | ||
return a.object.mapValues(this.attributes, (t, e) => { | ||
const s = this[a.getGetterName(e)]; | ||
return s ? s(t) : t; | ||
}); | ||
@@ -141,10 +138,5 @@ } | ||
Model.DB = s, Model.GraphQL = r.default, Model.Types = i, Model.connections = t.default, | ||
Model.DB = s, Model.GraphQL = r.default, Model.Types = i, Model.connections = e.default, | ||
Model.connection = "default", Model.schema = {}, Model.timestamps = !0, Model.softDelete = !1, | ||
Model.CREATED_AT = "created_at", Model.UPDATED_AT = "updated_at", Model.DELETED_AT = "deleted_at", | ||
Model.register = ((...e) => { | ||
e.forEach(e => { | ||
if (n[e.name]) throw new Error(`Model "${e.name}" already exists`); | ||
n[e.name] = e; | ||
}); | ||
}), exports.default = Model; | ||
exports.default = Model; |
{ | ||
"name": "@foxify/odin", | ||
"version": "0.1.0-beta.18", | ||
"version": "0.1.0-beta.19", | ||
"description": "Active Record Model", | ||
@@ -5,0 +5,0 @@ "author": "Ardalan Amini <ardalanamini22@gmail.com> [https://github.com/ardalanamini]", |
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
202462
4563