Skip to content

Commit

Permalink
Merge pull request #10 from koyopro/feature/column_meta
Browse files Browse the repository at this point in the history
Removal of `ModelMeta["OrderInput"]`
  • Loading branch information
koyopro authored Jun 7, 2024
2 parents dcc1f0b + 226e724 commit d52bda6
Show file tree
Hide file tree
Showing 11 changed files with 19 additions and 97 deletions.
2 changes: 0 additions & 2 deletions packages/accel-record-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ export type Meta<T> = ReturnType<typeof meta<T>>;

export declare function meta<T>(model: T): ModelMeta;

export type SortOrder = "asc" | "desc";

export type Filter<T> = {
in?: T[];
"<"?: T;
Expand Down
3 changes: 1 addition & 2 deletions packages/accel-record-core/src/meta.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Meta, Model, SortOrder } from "./index.js";
import { Meta, Model } from "./index.js";

export type { Meta };
export type Persisted<T> = Meta<T>["Persisted"];
Expand All @@ -12,5 +12,4 @@ export type ModelMeta = {
Column: Record<string, any>;
WhereInput: Record<string, any>;
CreateInput: Record<string, any>;
OrderInput: Record<string, SortOrder>;
};
4 changes: 2 additions & 2 deletions packages/accel-record-core/src/model/dirty.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export class Dirty {
*/
isAttributeChanged<T extends Model>(
this: T,
attribute: keyof Meta<T>["OrderInput"]
attribute: keyof Meta<T>["Column"]
): boolean {
return (
this[attribute as keyof T] !== this.originalValues[attribute as string]
Expand All @@ -42,7 +42,7 @@ export class Dirty {
*/
isChanged<T extends Model>(
this: T,
attribute?: keyof Meta<T>["OrderInput"]
attribute?: keyof Meta<T>["Column"]
): boolean {
if (attribute) {
return this.isAttributeChanged(attribute);
Expand Down
4 changes: 2 additions & 2 deletions packages/accel-record-core/src/model/import.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ type ImportOptions<T extends typeof Model> = {
* - If not provided or set to `true`, existing records will be updated.
* - If set to an array of keys, only the specified keys will be updated.
*/
onDuplicateKeyUpdate?: true | (keyof Meta<T>["OrderInput"])[];
onDuplicateKeyUpdate?: true | (keyof Meta<T>["Column"])[];

/**
* Specifies the conflict target for resolving conflicts when a duplicate key is encountered.
* - If not provided, the entire record will be considered as the conflict target.
* - If set to an array of keys, only the specified keys will be considered as the conflict target.
*/
conflictTarget?: (keyof Meta<T>["OrderInput"])[];
conflictTarget?: (keyof Meta<T>["Column"])[];
};

type ImportResult<T extends typeof Model> = {
Expand Down
6 changes: 3 additions & 3 deletions packages/accel-record-core/src/model/serialization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ type ToHashMethods<

export type ToHashResult<T, O extends ToHashOptions<T>> = {
[K in undefined extends O["only"]
? Exclude<keyof Meta<T>["OrderInput"], ToUnion<O["except"]>>
? Exclude<keyof Meta<T>["Column"], ToUnion<O["except"]>>
: ToUnion<O["only"]>]: T[Extract<K, keyof T>];
} & ToHashInclude<O, T> &
ToHashMethods<O, T>;
Expand All @@ -67,8 +67,8 @@ type NoArgMethods<T> = {
}[keyof T];

export type ToHashOptions<T> = {
only?: (keyof Meta<T>["OrderInput"])[];
except?: (keyof Meta<T>["OrderInput"])[];
only?: (keyof Meta<T>["Column"])[];
except?: (keyof Meta<T>["Column"])[];
methods?: NoArgMethods<T>[];
include?: Meta<T>["AssociationKey"] | ToHashIncludeOption<T>;
};
Expand Down
13 changes: 5 additions & 8 deletions packages/accel-record-core/src/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,7 @@ export class Query {
* @param columns - The columns to select.
* @returns A `Relation` object representing the query result with the selected columns.
*/
static select<
T extends typeof Model,
F extends (keyof Meta<T>["OrderInput"])[],
>(
static select<T extends typeof Model, F extends (keyof Meta<T>["Column"])[]>(
this: T,
...attributes: F
// @ts-ignore
Expand Down Expand Up @@ -209,7 +206,7 @@ export class Query {
*/
static order<T extends typeof Model>(
this: T,
attribute: keyof Meta<T>["OrderInput"],
attribute: keyof Meta<T>["Column"],
direction?: "asc" | "desc"
): Relation<InstanceType<T>, Meta<T>> {
return this.all().order(attribute, direction);
Expand Down Expand Up @@ -338,7 +335,7 @@ export class Query {
*/
static maximum<T extends typeof Model>(
this: T,
attribute: keyof Meta<T>["OrderInput"]
attribute: keyof Meta<T>["Column"]
) {
return this.all().maximum(attribute);
}
Expand All @@ -350,7 +347,7 @@ export class Query {
*/
static minimum<T extends typeof Model>(
this: T,
attribute: keyof Meta<T>["OrderInput"]
attribute: keyof Meta<T>["Column"]
) {
return this.all().minimum(attribute);
}
Expand All @@ -362,7 +359,7 @@ export class Query {
*/
static average<T extends typeof Model>(
this: T,
attribute: keyof Meta<T>["OrderInput"]
attribute: keyof Meta<T>["Column"]
) {
return this.all().average(attribute);
}
Expand Down
6 changes: 3 additions & 3 deletions packages/accel-record-core/src/relation/calculations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export class Calculations {
*/
minimum<T, M extends ModelMeta>(
this: Relation<T, M>,
attribute: keyof M["OrderInput"]
attribute: keyof M["Column"]
) {
const res = exec(
this.query().min(this.model.attributeToColumn(attribute as string))
Expand All @@ -43,7 +43,7 @@ export class Calculations {
*/
maximum<T, M extends ModelMeta>(
this: Relation<T, M>,
attribute: keyof M["OrderInput"]
attribute: keyof M["Column"]
) {
const res = exec(
this.query().max(this.model.attributeToColumn(attribute as string))
Expand All @@ -59,7 +59,7 @@ export class Calculations {
*/
average<T, M extends ModelMeta>(
this: Relation<T, M>,
attribute: keyof M["OrderInput"]
attribute: keyof M["Column"]
) {
const res = exec(
this.query().avg(this.model.attributeToColumn(attribute as string))
Expand Down
4 changes: 2 additions & 2 deletions packages/accel-record-core/src/relation/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ export class Query {
*/
order<T, M extends ModelMeta>(
this: Relation<T, M>,
attribute: keyof M["OrderInput"],
attribute: keyof M["Column"],
direction: "asc" | "desc" = "asc"
): Relation<T, M> {
const newOptions = JSON.parse(JSON.stringify(this.options));
Expand Down Expand Up @@ -164,7 +164,7 @@ export class Query {
select<
T,
M extends ModelMeta,
F extends (keyof M["OrderInput"])[],
F extends (keyof M["Column"])[],
// @ts-ignore
R extends { [K in F[number]]: M["Persisted"][K] },
>(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Validator } from "./index.js";

export type UniqunessOptions<T> =
| boolean
| { scope: (keyof Meta<T>["OrderInput"] & string)[] };
| { scope: (keyof Meta<T>["Column"] & string)[] };

export class UniquenessValidator<T extends Model> extends Validator<T> {
constructor(
Expand Down
8 changes: 0 additions & 8 deletions packages/prisma-generator-accel-record/src/generators/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,6 @@ export const generateTypes = (options: GeneratorOptions) => {
registerModel,
type Collection,
type Filter,
type SortOrder,
type StringFilter,
} from "accel-record";
Expand Down Expand Up @@ -161,12 +160,6 @@ declare module "accel-record" {
return ` & ({ ${f.name}: ${f.type} } | { ${foreignKeys} })`;
})
.join("");
const orderInputs =
model.fields
.filter(reject)
.filter((field) => field.relationName == undefined)
.map((field) => `\n ${field.name}?: SortOrder;`)
.join("") + "\n ";
data += `
declare module "./${model.fileName}" {
interface ${model.baseModel} {
Expand All @@ -187,7 +180,6 @@ type ${model.meta} = {
${columns}
}${associationColumns};
WhereInput: {${whereInputs(model)}};
OrderInput: {${orderInputs}};
};
registerModel(${model.persistedModel});
`;
Expand Down
64 changes: 0 additions & 64 deletions tests/models/_types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import {
registerModel,
type Collection,
type Filter,
type SortOrder,
type StringFilter,
} from "accel-record";

Expand Down Expand Up @@ -113,14 +112,6 @@ type UserMeta = {
createdAt?: Date | Date[] | Filter<Date> | null;
updatedAt?: Date | Date[] | Filter<Date> | null;
};
OrderInput: {
id?: SortOrder;
email?: SortOrder;
name?: SortOrder;
age?: SortOrder;
createdAt?: SortOrder;
updatedAt?: SortOrder;
};
};
registerModel(User);

Expand Down Expand Up @@ -159,10 +150,6 @@ type TeamMeta = {
id?: number | number[] | Filter<number> | null;
name?: string | string[] | StringFilter | null;
};
OrderInput: {
id?: SortOrder;
name?: SortOrder;
};
};
registerModel(Team);

Expand Down Expand Up @@ -210,12 +197,6 @@ type UserTeamMeta = {
assignedAt?: Date | Date[] | Filter<Date> | null;
assignedBy?: string | string[] | StringFilter | null;
};
OrderInput: {
userId?: SortOrder;
teamId?: SortOrder;
assignedAt?: SortOrder;
assignedBy?: SortOrder;
};
};
registerModel(UserTeam);

Expand Down Expand Up @@ -269,13 +250,6 @@ type PostMeta = {
author?: User | User[];
authorId?: number | number[] | Filter<number> | null;
};
OrderInput: {
id?: SortOrder;
title?: SortOrder;
content?: SortOrder;
published?: SortOrder;
authorId?: SortOrder;
};
};
registerModel(Post);

Expand Down Expand Up @@ -314,10 +288,6 @@ type PostTagMeta = {
id?: number | number[] | Filter<number> | null;
name?: string | string[] | StringFilter | null;
};
OrderInput: {
id?: SortOrder;
name?: SortOrder;
};
};
registerModel(PostTag);

Expand Down Expand Up @@ -364,13 +334,6 @@ type SettingMeta = {
threshold?: number | number[] | Filter<number> | null;
createdAt?: Date | Date[] | Filter<Date> | null;
};
OrderInput: {
settingId?: SortOrder;
userId?: SortOrder;
threshold?: SortOrder;
createdAt?: SortOrder;
data?: SortOrder;
};
};
registerModel(Setting);

Expand Down Expand Up @@ -430,16 +393,6 @@ type ProfileMeta = {
uuid?: string | string[] | StringFilter | null;
cuid?: string | string[] | StringFilter | null;
};
OrderInput: {
id?: SortOrder;
userId?: SortOrder;
bio?: SortOrder;
point?: SortOrder;
enabled?: SortOrder;
role?: SortOrder;
uuid?: SortOrder;
cuid?: SortOrder;
};
};
registerModel(Profile);

Expand Down Expand Up @@ -478,10 +431,6 @@ type CompanyMeta = {
id?: number | number[] | Filter<number> | null;
name?: string | string[] | StringFilter | null;
};
OrderInput: {
id?: SortOrder;
name?: SortOrder;
};
};
registerModel(Company);

Expand Down Expand Up @@ -522,11 +471,6 @@ type EmployeeMeta = {
companyId?: number | number[] | Filter<number> | null;
company?: Company | Company[];
};
OrderInput: {
id?: SortOrder;
name?: SortOrder;
companyId?: SortOrder;
};
};
registerModel(Employee);

Expand Down Expand Up @@ -580,13 +524,5 @@ type ValidateSampleMeta = {
count?: number | number[] | Filter<number> | null;
size?: string | string[] | StringFilter | null;
};
OrderInput: {
id?: SortOrder;
accepted?: SortOrder;
pattern?: SortOrder;
key?: SortOrder;
count?: SortOrder;
size?: SortOrder;
};
};
registerModel(ValidateSample);

0 comments on commit d52bda6

Please sign in to comment.