accel-record-core
Advanced tools
Comparing version 1.12.0 to 1.13.0
@@ -26,6 +26,10 @@ import { Models } from "../index.js"; | ||
// Updating fields other than the column field | ||
Object.keys(input).forEach((key) => { | ||
if (klass.attributeToColumn(key) == undefined) { | ||
proxy[key] = input[key]; | ||
Object.entries(input).forEach(([key, value]) => { | ||
const v = instance[key]; | ||
if (v instanceof Collection && Array.isArray(value)) { | ||
v.cache = value; | ||
} | ||
else if (klass.attributeToColumn(key) == undefined) { | ||
proxy[key] = value; | ||
} | ||
}); | ||
@@ -73,3 +77,4 @@ return proxy; | ||
if (typeof column === "string") { | ||
target[column] = value; | ||
const field = klass.findField(prop); | ||
target[column] = field ? field.cast(value) : value; | ||
return; | ||
@@ -76,0 +81,0 @@ } |
@@ -1,2 +0,2 @@ | ||
import { Fields } from "./fields"; | ||
import { Model } from "./index.js"; | ||
/** | ||
@@ -13,3 +13,3 @@ * Represents a class that assigns attributes to the corresponding fields of an object. | ||
*/ | ||
assignAttributes<T extends Fields>(this: T, attributes: Record<string, any>): void; | ||
assignAttributes<T extends Model>(this: T, attributes: Record<string, any>): void; | ||
} |
@@ -13,6 +13,6 @@ /** | ||
assignAttributes(attributes) { | ||
for (const key in attributes) { | ||
this[key] = attributes[key]; | ||
for (const [key, value] of Object.entries(attributes)) { | ||
this[key] = value; | ||
} | ||
} | ||
} |
@@ -25,4 +25,4 @@ declare const callbackMethods: readonly ["validation", "create", "save", "update", "destroy"]; | ||
callbacks: { | ||
before: Record<"create" | "update" | "save" | "destroy" | "validation", (() => any)[]>; | ||
after: Record<"create" | "update" | "save" | "destroy" | "validation", (() => any)[]>; | ||
before: Record<"create" | "save" | "update" | "destroy" | "validation", (() => any)[]>; | ||
after: Record<"create" | "save" | "update" | "destroy" | "validation", (() => any)[]>; | ||
}; | ||
@@ -29,0 +29,0 @@ /** |
@@ -9,2 +9,3 @@ import { Meta, Model } from "./index.js"; | ||
Persisted: Model; | ||
PrimaryKey: any; | ||
AssociationKey: string; | ||
@@ -11,0 +12,0 @@ JoinInput: any; |
import { createId as cuid } from "@paralleldrive/cuid2"; | ||
import { isBlank } from "../validation/validator/presence.js"; | ||
/** | ||
@@ -79,3 +80,3 @@ * Represents a field in a database table. | ||
case "Int": | ||
return Number(value); | ||
return castNumber(value); | ||
case "Bytes": | ||
@@ -86,4 +87,6 @@ case "String": | ||
return !!value; | ||
case "DateTime": | ||
return new Date(value); | ||
case "DateTime": { | ||
const date = new Date(value); | ||
return isInvalidDate(date) ? undefined : date; | ||
} | ||
case "JSON": | ||
@@ -96,1 +99,8 @@ return JSON.parse(value); | ||
} | ||
const castNumber = (value) => { | ||
if (isBlank(value)) | ||
return undefined; | ||
const num = Number(value); | ||
return Number.isFinite(num) ? num : undefined; | ||
}; | ||
const isInvalidDate = (date) => Number.isNaN(date.getTime()); |
@@ -167,3 +167,3 @@ import { type Model } from "./index.js"; | ||
*/ | ||
static find<T extends typeof Model>(this: T, id: number): Meta<T>["Persisted"]; | ||
static find<T extends typeof Model>(this: T, key: Meta<T>["PrimaryKey"]): Meta<T>["Persisted"]; | ||
/** | ||
@@ -170,0 +170,0 @@ * Finds a record by the specified parameters. |
@@ -178,4 +178,4 @@ import { Relation } from "./relation/index.js"; | ||
*/ | ||
static find(id) { | ||
return this.all().find(id); | ||
static find(key) { | ||
return this.all().find(key); | ||
} | ||
@@ -182,0 +182,0 @@ /** |
@@ -17,3 +17,3 @@ import { Model } from "../index.js"; | ||
*/ | ||
find<T, M extends ModelMeta>(this: Relation<T, M>, id: number): T; | ||
find<T, M extends ModelMeta>(this: Relation<T, M>, key: M["PrimaryKey"]): T; | ||
/** | ||
@@ -20,0 +20,0 @@ * Retrieves the first n elements. |
@@ -22,5 +22,8 @@ import { exec } from "../database.js"; | ||
*/ | ||
find(id) { | ||
const instance = isFinite(id) | ||
? this.setOption("wheres", [{ [this.model.primaryKeys[0]]: id }]).first() | ||
find(key) { | ||
const keys = [key].flat(); | ||
const valid = keys.every((key) => typeof key !== "number" || isFinite(key)); | ||
const where = this.model.primaryKeys.toHash((col, i) => [col, keys[i]]); | ||
const instance = valid | ||
? this.setOption("wheres", [where]).first() | ||
: undefined; | ||
@@ -27,0 +30,0 @@ if (!instance) { |
@@ -1,2 +0,2 @@ | ||
type ToHash<A extends ReadonlyArray<T>, T> = <K extends PropertyKey, V>(this: A, callback: (item: T) => [K, V]) => Record<K, V>; | ||
type ToHash<A extends ReadonlyArray<T>, T> = <K extends PropertyKey, V>(this: A, callback: (item: T, index: number) => [K, V]) => Record<K, V>; | ||
declare global { | ||
@@ -3,0 +3,0 @@ interface Array<T> { |
Array.prototype.toHash = function (callback) { | ||
const ret = {}; | ||
if (typeof this[Symbol.iterator] === "function") { | ||
for (const item of this) { | ||
const [key, value] = callback(item); | ||
ret[key] = value; | ||
} | ||
} | ||
this.forEach((item, index) => { | ||
const [key, value] = callback(item, index); | ||
ret[key] = value; | ||
}); | ||
return ret; | ||
}; | ||
export {}; |
{ | ||
"name": "accel-record-core", | ||
"version": "1.12.0", | ||
"version": "1.13.0", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "type": "module", |
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
266880
141
7218