@foxify/odin
Advanced tools
Comparing version 0.1.0-beta.8 to 0.1.0-beta.9
@@ -15,2 +15,7 @@ # Changelog | ||
## [v0.1.0-beta.9](https://github.com/foxifyjs/odin/releases/tag/v0.1.0-beta.9) - *(2018-08-21)* | ||
- :star2: `UPDATED_AT` and `DELETED_AT` is applied | ||
- :beetle: `GraphQL` restore bug fix | ||
## [v0.1.0-beta.8](https://github.com/foxifyjs/odin/releases/tag/v0.1.0-beta.8) - *(2018-08-21)* | ||
@@ -17,0 +22,0 @@ |
@@ -30,3 +30,7 @@ /* | ||
interface Query<T = any> extends DB<T> { | ||
/******************************* With Trashed *******************************/ | ||
withTrashed(): this; | ||
/*********************************** Joins **********************************/ | ||
join(table: string | ModelConstructor, query?: Driver.JoinQuery<T>, as?: string): this; | ||
/*********************************** Read ***********************************/ | ||
get(): Promise<Array<Model<T>>>; | ||
@@ -36,11 +40,30 @@ get(callback: Driver.Callback<Array<Model<T>>>): void; | ||
first(callback: Driver.Callback<Model<T>>): void; | ||
/********************************** Inserts *********************************/ | ||
insert(items: T[]): Promise<number>; | ||
insert(items: T[], callback: Driver.Callback<number>): void; | ||
/********************************* Restoring ********************************/ | ||
restore(): Promise<number>; | ||
restore(callback: Driver.Callback<number>): void; | ||
} | ||
declare class Query<T = any> extends DB<T> { | ||
protected readonly _model: ModelConstructor; | ||
protected _withTrashed: boolean; | ||
constructor(model: ModelConstructor, table: string, relations?: Relation[]); | ||
private _apply_trashed_options; | ||
/*********************************** Read ***********************************/ | ||
exists(callback?: Driver.Callback<boolean>): any; | ||
count(callback?: Driver.Callback<number>): any; | ||
value(field: string, callback?: Driver.Callback<any>): any; | ||
pluck(field: string, callback?: Driver.Callback<any>): any; | ||
max(field: string, callback?: Driver.Callback<any>): any; | ||
min(field: string, callback?: Driver.Callback<any>): any; | ||
avg(field: string, callback?: Driver.Callback<any>): any; | ||
insertGetId(item: T, callback?: Driver.Callback<Driver.Id>): any; | ||
/********************************** Updates *********************************/ | ||
update(update: T, callback?: Driver.Callback<number>): any; | ||
increment(field: string, count?: number | Driver.Callback<number>, callback?: Driver.Callback<number>): any; | ||
decrement(field: string, count?: number | Driver.Callback<number>, callback?: Driver.Callback<number>): any; | ||
/********************************** Deletes *********************************/ | ||
delete(callback?: Driver.Callback<number>): any; | ||
} | ||
export default Query; |
@@ -27,9 +27,9 @@ /* | ||
var t = this && this.__awaiter || function(t, e, r, i) { | ||
return new (r || (r = Promise))(function(n, s) { | ||
var t = this && this.__awaiter || function(t, e, r, s) { | ||
return new (r || (r = Promise))(function(i, n) { | ||
function fulfilled(t) { | ||
try { | ||
step(i.next(t)); | ||
step(s.next(t)); | ||
} catch (t) { | ||
s(t); | ||
n(t); | ||
} | ||
@@ -39,13 +39,13 @@ } | ||
try { | ||
step(i.throw(t)); | ||
step(s.throw(t)); | ||
} catch (t) { | ||
s(t); | ||
n(t); | ||
} | ||
} | ||
function step(t) { | ||
t.done ? n(t.value) : new r(function(e) { | ||
t.done ? i(t.value) : new r(function(e) { | ||
e(t.value); | ||
}).then(fulfilled, rejected); | ||
} | ||
step((i = i.apply(t, e || [])).next()); | ||
step((s = s.apply(t, e || [])).next()); | ||
}); | ||
@@ -58,10 +58,16 @@ }; | ||
const e = require("async"), r = require("../DB"), i = require("../utils"); | ||
const e = require("async"), r = require("../DB"), s = require("../utils"); | ||
exports.default = class Query extends r { | ||
constructor(t, e, r = []) { | ||
super(t.connection), this.table(e), this._model = t, r.forEach(t => t.load(this)); | ||
super(t.connection), this._withTrashed = !1, this.table(e), this._model = t, r.forEach(t => t.load(this)); | ||
} | ||
_apply_trashed_options() { | ||
return this._withTrashed || this.whereNull(this._model.DELETED_AT), this; | ||
} | ||
withTrashed() { | ||
return this._withTrashed = !0, this; | ||
} | ||
join(t, e, r) { | ||
if (!i.string.isString(t)) { | ||
if (!s.string.isString(t)) { | ||
const e = t; | ||
@@ -72,15 +78,21 @@ t = e.toString(), r || (r = t), this.map(t => (t[r] = t[r].map(t => new e(t)), t)); | ||
} | ||
exists(t) { | ||
return this._apply_trashed_options(), super.exists(t); | ||
} | ||
count(t) { | ||
return this._apply_trashed_options(), super.count(t); | ||
} | ||
get(r) { | ||
const i = t => super[t]; | ||
const s = t => super[t]; | ||
return t(this, void 0, void 0, function*() { | ||
const t = (t, e) => e(void 0, new this._model(t)); | ||
if (r) return i("get").call(this, (i, n) => { | ||
if (i) return r(i, n); | ||
e.map(n, t, r); | ||
if (this._apply_trashed_options(), r) return s("get").call(this, (s, i) => { | ||
if (s) return r(s, i); | ||
e.map(i, t, r); | ||
}); | ||
let n = []; | ||
return e.map(yield i("get").call(this), t, (t, e) => { | ||
let i = []; | ||
return e.map(yield s("get").call(this), t, (t, e) => { | ||
if (t) throw t; | ||
n = e; | ||
}), n; | ||
i = e; | ||
}), i; | ||
}); | ||
@@ -91,11 +103,27 @@ } | ||
return t(this, void 0, void 0, function*() { | ||
if (e) return r("first").call(this, (t, r) => e(t, r && new this._model(r))); | ||
const t = yield r("first").call(this); | ||
return t && new this._model(t); | ||
const t = this._model; | ||
if (this._apply_trashed_options(), e) return r("first").call(this, (r, s) => e(r, s && new t(s))); | ||
const s = yield r("first").call(this); | ||
return s && new t(s); | ||
}); | ||
} | ||
value(t, e) { | ||
return this._apply_trashed_options(), super.value(t, e); | ||
} | ||
pluck(t, e) { | ||
return this._apply_trashed_options(), super.pluck(t, e); | ||
} | ||
max(t, e) { | ||
return this._apply_trashed_options(), super.max(t, e); | ||
} | ||
min(t, e) { | ||
return this._apply_trashed_options(), super.min(t, e); | ||
} | ||
avg(t, e) { | ||
return this._apply_trashed_options(), super.avg(t, e); | ||
} | ||
insert(t, r) { | ||
const i = !Array.isArray(t) && new Error(`Expected 'items' to be an array, '${typeof t}' given`), n = (t, e) => { | ||
const s = this._model, i = !Array.isArray(t) && new Error(`Expected 'items' to be an array, '${typeof t}' given`), n = (t, e) => { | ||
try { | ||
e(void 0, this._model.validate(t)); | ||
e(void 0, s.validate(t)); | ||
} catch (t) { | ||
@@ -116,4 +144,5 @@ e(t); | ||
insertGetId(t, e) { | ||
const r = this._model; | ||
try { | ||
t = this._model.validate(t); | ||
t = r.validate(t); | ||
} catch (t) { | ||
@@ -132,4 +161,18 @@ if (e) return e(t, void 0); | ||
} | ||
return super.update(t, e); | ||
return this._apply_trashed_options(), super.update(t, e); | ||
} | ||
increment(t, e, r) { | ||
return this._apply_trashed_options(), super.increment(t, e, r); | ||
} | ||
decrement(t, e, r) { | ||
return this._apply_trashed_options(), super.decrement(t, e, r); | ||
} | ||
delete(t) { | ||
return this._apply_trashed_options(), super.delete(t); | ||
} | ||
restore(t) { | ||
return super.update({ | ||
[this._model.DELETED_AT]: null | ||
}, t); | ||
} | ||
}; |
@@ -30,2 +30,4 @@ /* | ||
interface QueryBuilder<T = any> { | ||
/******************************* With Trashed *******************************/ | ||
withTrashed(): Query<T>; | ||
/****************************** With Relations ******************************/ | ||
@@ -76,5 +78,2 @@ with(...relations: string[]): Query<T>; | ||
create(item: T, callback: Driver.Callback<Model<T>>): void; | ||
/********************************** Updates *********************************/ | ||
save(): Promise<Model<T>>; | ||
save(callback: Driver.Callback<Model<T>>): void; | ||
/********************************** Deletes *********************************/ | ||
@@ -85,4 +84,8 @@ destroy(ids: Driver.Id | Driver.Id[]): Promise<number>; | ||
export interface QueryInstance<T = any> { | ||
/********************************** Updates *********************************/ | ||
save(): Promise<Model<T>>; | ||
save(callback: Driver.Callback<Model<T>>): void; | ||
/********************************* Restoring ********************************/ | ||
restore(): Promise<boolean>; | ||
restore(callback: Driver.Callback<boolean>): void; | ||
} | ||
@@ -92,5 +95,9 @@ declare class QueryBuilder { | ||
static _table: string; | ||
static softDelete: boolean; | ||
static DELETED_AT: string; | ||
private _isNew; | ||
attributes: ModelConstructor.Document; | ||
static query(relations?: Relation[]): Query<any>; | ||
/******************************* With Trashed *******************************/ | ||
static withTrashed(): Query<any>; | ||
/****************************** With Relations ******************************/ | ||
@@ -130,5 +137,9 @@ static with(...relations: string[]): Query<any>; | ||
static create(item: any, callback?: Driver.Callback<any>): Promise<any>; | ||
/********************************** Updates *********************************/ | ||
save(callback?: Driver.Callback<any>): Promise<any>; | ||
/********************************** Deletes *********************************/ | ||
static destroy(ids: Driver.Id | Driver.Id[]): any; | ||
static destroy(ids: Driver.Id | Driver.Id[], callback?: Driver.Callback<number>): any; | ||
/********************************* Restoring ********************************/ | ||
restore(callback?: Driver.Callback<boolean>): Promise<any>; | ||
} | ||
export default QueryBuilder; |
@@ -62,2 +62,5 @@ /* | ||
} | ||
static withTrashed() { | ||
return this.query().withTrashed(); | ||
} | ||
static with(...t) { | ||
@@ -169,5 +172,19 @@ if (0 === t.length) throw new TypeError("Expected at least one 'relation', got none"); | ||
} | ||
static destroy(t) { | ||
return Array.isArray(t) ? this.query().whereIn("id", t).delete() : this.query().where("id", t).delete(); | ||
static destroy(t, e) { | ||
let r = this.query(); | ||
return r = Array.isArray(t) ? r.whereIn("id", t) : r.where("id", t), this.softDelete ? r.update({ | ||
[this.DELETED_AT]: new Date() | ||
}, e) : r.delete(e); | ||
} | ||
restore(e) { | ||
return t(this, void 0, void 0, function*() { | ||
const t = this.attributes.id; | ||
if (this._isNew || !t) return !1; | ||
const r = this.constructor.where("id", t); | ||
return e ? r.restore((t, r) => { | ||
if (t) return e(t, r); | ||
e(t, !!r); | ||
}) : !!(yield r.restore()); | ||
}); | ||
} | ||
}; |
@@ -40,2 +40,5 @@ /* | ||
private static softDelete; | ||
private static CREATED_AT; | ||
private static UPDATED_AT; | ||
private static DELETED_AT; | ||
static DB: ModelConstructor.DB; | ||
@@ -42,0 +45,0 @@ static connection: ModelConstructor.Connection; |
@@ -27,9 +27,9 @@ /* | ||
var e = this && this.__awaiter || function(e, t, r, n) { | ||
return new (r || (r = Promise))(function(s, i) { | ||
var e = this && this.__awaiter || function(e, t, r, i) { | ||
return new (r || (r = Promise))(function(s, n) { | ||
function fulfilled(e) { | ||
try { | ||
step(n.next(e)); | ||
step(i.next(e)); | ||
} catch (e) { | ||
i(e); | ||
n(e); | ||
} | ||
@@ -39,5 +39,5 @@ } | ||
try { | ||
step(n.throw(e)); | ||
step(i.throw(e)); | ||
} catch (e) { | ||
i(e); | ||
n(e); | ||
} | ||
@@ -50,3 +50,3 @@ } | ||
} | ||
step((n = n.apply(e, t || [])).next()); | ||
step((i = i.apply(e, t || [])).next()); | ||
}); | ||
@@ -59,9 +59,9 @@ }; | ||
const t = require("graphql"), r = require("../types/Any"), n = require("../utils"), s = (e, n) => { | ||
const i = {}, a = {}; | ||
for (const o in n) { | ||
const u = n[o]; | ||
if (u instanceof r.default) { | ||
const t = u.toGraphQL(e, o); | ||
i[o] = { | ||
const t = require("graphql"), r = require("../types/Any"), i = require("../utils"), s = (e, i) => { | ||
const n = {}, a = {}; | ||
for (const o in i) { | ||
const c = i[o]; | ||
if (c instanceof r.default) { | ||
const t = c.toGraphQL(e, o); | ||
n[o] = { | ||
type: t.field | ||
@@ -73,7 +73,7 @@ }, a[o] = { | ||
} | ||
const c = s(e, u); | ||
i[o] = { | ||
const u = s(e, c); | ||
n[o] = { | ||
type: new t.GraphQLObjectType({ | ||
name: `${e}_${o}`, | ||
fields: c.fields | ||
fields: u.fields | ||
}) | ||
@@ -83,3 +83,3 @@ }, a[o] = { | ||
name: `${e}_${o}_input`, | ||
fields: c.args | ||
fields: u.args | ||
}) | ||
@@ -89,12 +89,12 @@ }; | ||
return { | ||
fields: i, | ||
fields: n, | ||
args: a | ||
}; | ||
}, i = e => e.selectionSet.selections.reduce((e, t) => (e.push(t.name.value), e), []), a = (e, s) => { | ||
const i = e => `${e}_ASC`, a = e => `${e}_DESC`, o = (e, t) => { | ||
}, n = e => e.selectionSet.selections.reduce((e, t) => (e.push(t.name.value), e), []), a = (e, s) => { | ||
const n = e => `${e}_ASC`, a = e => `${e}_DESC`, o = (e, t) => { | ||
const s = {}; | ||
for (const u in e) { | ||
const c = e[u], p = n.array.compact([ t, u ]).join("."); | ||
if (c instanceof r.default) { | ||
const e = i(p); | ||
for (const c in e) { | ||
const u = e[c], p = i.array.compact([ t, c ]).join("."); | ||
if (u instanceof r.default) { | ||
const e = n(p); | ||
s[e] = { | ||
@@ -107,3 +107,3 @@ value: e | ||
}; | ||
} else n.object.forEach(o(e, p), (e, t) => s[t] = e); | ||
} else i.object.forEach(o(e, p), (e, t) => s[t] = e); | ||
} | ||
@@ -120,7 +120,3 @@ return s; | ||
static toGraphQL() { | ||
const r = this.name, u = this._table, c = n.string.pluralize(u, 1), p = s(r, this.schema), l = Object.assign({ | ||
id: { | ||
type: t.GraphQLID | ||
} | ||
}, p.args), d = new t.GraphQLObjectType({ | ||
const r = this.name, c = this._table, u = i.string.pluralize(c, 1), p = s(r, this._schema), d = p.args, l = new t.GraphQLObjectType({ | ||
name: r, | ||
@@ -133,13 +129,13 @@ fields: Object.assign({ | ||
}), h = () => this.DB.connection(this.connection).table(this._table), y = { | ||
[c]: { | ||
type: d, | ||
args: l, | ||
[u]: { | ||
type: l, | ||
args: d, | ||
resolve: (t, r, s, a) => e(this, void 0, void 0, function*() { | ||
const e = i(a.fieldNodes[0]); | ||
return yield n.object.reduce(r, (e, t, r) => e.where(r, t), h()).first(e); | ||
const e = n(a.fieldNodes[0]); | ||
return yield i.object.reduce(r, (e, t, r) => e.where(r, t), h()).first(e); | ||
}) | ||
}, | ||
[u]: { | ||
type: new t.GraphQLList(d), | ||
args: Object.assign({}, l, { | ||
[c]: { | ||
type: new t.GraphQLList(l), | ||
args: Object.assign({}, d, { | ||
skip: { | ||
@@ -156,5 +152,5 @@ type: t.GraphQLInt | ||
resolve: (t, r, s, a) => e(this, void 0, void 0, function*() { | ||
const e = i(a.fieldNodes[0]); | ||
const e = n(a.fieldNodes[0]); | ||
let t = h(); | ||
return this.timestamps && (t = t.orderBy("created_at", "desc")), yield n.object.reduce(r, (e, t, r) => { | ||
return this.timestamps && (t = t.orderBy("created_at", "desc")), yield i.object.reduce(r, (e, t, r) => { | ||
switch (r) { | ||
@@ -178,6 +174,6 @@ case "skip": | ||
name: `${r}Input`, | ||
fields: p.args | ||
fields: i.object.omit(d, [ "id", this.CREATED_AT, this.UPDATED_AT, this.DELETED_AT ]) | ||
}), v = { | ||
[`create_${c}`]: { | ||
type: d, | ||
[`create_${u}`]: { | ||
type: l, | ||
args: { | ||
@@ -188,7 +184,7 @@ data: { | ||
}, | ||
resolve: (t, r, n, s) => e(this, void 0, void 0, function*() { | ||
resolve: (t, r, i, s) => e(this, void 0, void 0, function*() { | ||
return o(yield this.create(r.data)); | ||
}) | ||
}, | ||
[`insert_${u}`]: { | ||
[`insert_${c}`]: { | ||
type: t.GraphQLInt, | ||
@@ -200,11 +196,11 @@ args: { | ||
}, | ||
resolve: (t, r, n, s) => e(this, void 0, void 0, function*() { | ||
resolve: (t, r, i, s) => e(this, void 0, void 0, function*() { | ||
return yield this.insert(r.data); | ||
}) | ||
}, | ||
[`update_${u}`]: { | ||
[`update_${c}`]: { | ||
type: t.GraphQLInt, | ||
args: { | ||
query: { | ||
type: new t.GraphQLNonNull(f) | ||
type: d | ||
}, | ||
@@ -215,29 +211,27 @@ data: { | ||
}, | ||
resolve: (t, r, s, i) => e(this, void 0, void 0, function*() { | ||
return yield n.object.reduce(r.query, (e, t, r) => e.where(r, t), this).update(r.data); | ||
resolve: (t, r, s, n) => e(this, void 0, void 0, function*() { | ||
return yield i.object.reduce(r.query, (e, t, r) => e.where(r, t), this).update(r.data); | ||
}) | ||
}, | ||
[`delete_${u}`]: { | ||
[`delete_${c}`]: { | ||
type: t.GraphQLInt, | ||
args: { | ||
query: { | ||
type: new t.GraphQLNonNull(f) | ||
type: d | ||
} | ||
}, | ||
resolve: (t, r, s, i) => e(this, void 0, void 0, function*() { | ||
return yield n.object.reduce(r.query, (e, t, r) => e.where(r, t), this).delete(); | ||
resolve: (t, r, s, n) => e(this, void 0, void 0, function*() { | ||
return yield i.object.reduce(r.query, (e, t, r) => e.where(r, t), this).delete(); | ||
}) | ||
} | ||
}; | ||
return this.softDelete && (v[`restore_${u}`] = { | ||
return this.softDelete && (v[`restore_${c}`] = { | ||
type: t.GraphQLInt, | ||
args: { | ||
query: { | ||
type: new t.GraphQLNonNull(f) | ||
type: d | ||
} | ||
}, | ||
resolve: (t, r, s, i) => e(this, void 0, void 0, function*() { | ||
return yield n.object.reduce(r.query, (e, t, r) => e.where(r, t), this).update({ | ||
deleted_at: null | ||
}); | ||
resolve: (t, r, s, n) => e(this, void 0, void 0, function*() { | ||
return yield i.object.reduce(r.query, (e, t, r) => e.where(r, t), this.withTrashed()).restore(); | ||
}) | ||
@@ -244,0 +238,0 @@ }), { |
@@ -65,2 +65,6 @@ /* | ||
timestamps?: boolean; | ||
softDelete: boolean; | ||
CREATED_AT: string; | ||
UPDATED_AT: string; | ||
DELETED_AT: string; | ||
readonly driver: TDriver; | ||
@@ -67,0 +71,0 @@ readonly filename: string; |
@@ -27,6 +27,6 @@ /* | ||
var e = this && this.__decorate || function(e, t, r, s) { | ||
var i, o = arguments.length, a = o < 3 ? t : null === s ? s = Object.getOwnPropertyDescriptor(t, r) : s; | ||
if ("object" == typeof Reflect && "function" == typeof Reflect.decorate) a = Reflect.decorate(e, t, r, s); else for (var n = e.length - 1; n >= 0; n--) (i = e[n]) && (a = (o < 3 ? i(a) : o > 3 ? i(t, r, a) : i(t, r)) || a); | ||
return o > 3 && a && Object.defineProperty(t, r, a), a; | ||
var t = this && this.__decorate || function(t, e, s, r) { | ||
var i, o = arguments.length, a = o < 3 ? e : null === r ? r = Object.getOwnPropertyDescriptor(e, s) : r; | ||
if ("object" == typeof Reflect && "function" == typeof Reflect.decorate) a = Reflect.decorate(t, e, s, r); else for (var n = t.length - 1; n >= 0; n--) (i = t[n]) && (a = (o < 3 ? i(a) : o > 3 ? i(e, s, a) : i(e, s)) || a); | ||
return o > 3 && a && Object.defineProperty(e, s, a), a; | ||
}; | ||
@@ -38,7 +38,7 @@ | ||
const t = require("./DB"); | ||
const e = require("./DB"); | ||
exports.DB = t; | ||
exports.DB = e; | ||
const r = require("./base/QueryBuilder"), s = require("./base/Relational"), i = require("./connections"); | ||
const s = require("./base/QueryBuilder"), r = require("./base/Relational"), i = require("./connections"); | ||
@@ -58,4 +58,4 @@ exports.connections = i.default; | ||
let l = class Model { | ||
constructor(e = {}) { | ||
this._isNew = !1, this.attributes = {}, e.id || (this._isNew = !0), this._setAttributes(e); | ||
constructor(t = {}) { | ||
this._isNew = !1, this.attributes = {}, t.id || (this._isNew = !0), this._setAttributes(t); | ||
} | ||
@@ -66,8 +66,8 @@ static get _table() { | ||
static get _schema() { | ||
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; | ||
} | ||
@@ -83,17 +83,17 @@ static get filename() { | ||
} | ||
static validate(e, t = !1) { | ||
const r = (e, t) => { | ||
const s = {}; | ||
static validate(t, e = !1) { | ||
const s = (t, e) => { | ||
const r = {}; | ||
let i = {}; | ||
for (const o in e) { | ||
const n = e[o]; | ||
let c = t[o]; | ||
for (const o in t) { | ||
const n = t[o]; | ||
let c = e[o]; | ||
if (n instanceof a.default) { | ||
const e = n.validate(c); | ||
e.value && (s[o] = e.value), e.errors && (i[o] = e.errors); | ||
const t = n.validate(c); | ||
t.value && (r[o] = t.value), t.errors && (i[o] = t.errors); | ||
} else { | ||
c || (c = {}); | ||
const e = r(n, c); | ||
if (e.errors) for (const t in e.errors) i[`${o}.${t}`] = e.errors[t]; | ||
u.object.size(e.value) > 0 && (s[o] = e.value); | ||
const t = s(n, c); | ||
if (t.errors) for (const e in t.errors) i[`${o}.${e}`] = t.errors[e]; | ||
u.object.size(t.value) > 0 && (r[o] = t.value); | ||
} | ||
@@ -103,36 +103,37 @@ } | ||
errors: i, | ||
value: s | ||
value: r | ||
}; | ||
}, s = r(this._schema, e); | ||
if (s.errors && t && (u.object.forEach(s.errors, (e, t) => { | ||
1 === e.length && "Must be provided" === e.first() && delete s.errors[t]; | ||
}), 0 === u.object.size(s.errors) && (s.errors = null)), s.errors) throw s.errors; | ||
return s.value; | ||
}, r = s(this._schema, t); | ||
if (r.errors && e && (u.object.forEach(r.errors, (t, e) => { | ||
1 === t.length && "Must be provided" === t.first() && delete r.errors[e]; | ||
}), 0 === u.object.size(r.errors) && (r.errors = null)), r.errors) throw r.errors; | ||
const i = r.value; | ||
return e && this.timestamps && (i[this.UPDATED_AT] = new Date()), i; | ||
} | ||
_setAttributes(e) { | ||
const t = this.constructor._schema, r = [], s = []; | ||
for (const e in t) { | ||
const t = u.getGetterName(e), i = this[t] || (e => e); | ||
u.define(this, "get", e, () => i(this.attributes[e])), r.push(t); | ||
const o = u.getSetterName(e), a = this[o] || (e => e); | ||
u.define(this, "set", e, t => this.attributes[e] = a(t)), s.push(o); | ||
_setAttributes(t) { | ||
const e = this.constructor._schema, s = [], r = []; | ||
for (const t in e) { | ||
const e = u.getGetterName(t), i = this[e] || (t => t); | ||
u.define(this, "get", t, () => i(this.attributes[t])), s.push(e); | ||
const o = u.getSetterName(t), a = this[o] || (t => t); | ||
u.define(this, "set", t, e => this.attributes[t] = a(e)), r.push(o); | ||
} | ||
u.object.forEach(e, (e, t) => { | ||
-1 === s.indexOf(t) ? this.attributes[t] = e : this[t] = e; | ||
}), Object.getOwnPropertyNames(this.constructor.prototype).forEach(e => { | ||
if (-1 !== r.indexOf(e)) return; | ||
const t = /^get([A-Z].*)Attribute$/.exec(e); | ||
t && u.define(this, "get", u.string.snakeCase(t[1]), this[e]); | ||
u.object.forEach(t, (t, e) => { | ||
-1 === r.indexOf(e) ? this.attributes[e] = t : this[e] = t; | ||
}), Object.getOwnPropertyNames(this.constructor.prototype).forEach(t => { | ||
if (-1 !== s.indexOf(t)) return; | ||
const e = /^get([A-Z].*)Attribute$/.exec(t); | ||
e && u.define(this, "get", u.string.snakeCase(e[1]), this[t]); | ||
}); | ||
} | ||
getAttribute(e) { | ||
return e.split(".").reduce((e, t) => e[t], this.attributes); | ||
getAttribute(t) { | ||
return t.split(".").reduce((t, e) => t[e], this.attributes); | ||
} | ||
setAttribute(e, t) { | ||
u.object.set(this.attributes, e, t); | ||
setAttribute(t, e) { | ||
u.object.set(this.attributes, t, e); | ||
} | ||
toJSON() { | ||
return u.object.mapValues(this.attributes, (e, t) => { | ||
const r = this[u.getGetterName(t)]; | ||
return r ? r(e) : e; | ||
return u.object.mapValues(this.attributes, (t, e) => { | ||
const s = this[u.getGetterName(e)]; | ||
return s ? s(t) : t; | ||
}); | ||
@@ -145,5 +146,5 @@ } | ||
l.DB = t, l.GraphQL = c.default, l.Types = o, l.connections = i.default, l.connection = "default", | ||
l.DB = e, l.GraphQL = c.default, l.Types = o, l.connections = i.default, l.connection = "default", | ||
l.schema = {}, l.timestamps = !0, l.softDelete = !1, l.CREATED_AT = "created_at", | ||
l.UPDATED_AT = "updated_at", l.DELETED_AT = "deleted_at", l = e([ u.mixins(r.default, s.default, n.default) ], l), | ||
l.UPDATED_AT = "updated_at", l.DELETED_AT = "deleted_at", l = t([ u.mixins(s.default, r.default, n.default) ], l), | ||
exports.Model = l; | ||
@@ -150,0 +151,0 @@ |
{ | ||
"name": "@foxify/odin", | ||
"version": "0.1.0-beta.8", | ||
"version": "0.1.0-beta.9", | ||
"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
191952
4355