Apply feedbacks

This commit is contained in:
Alexandre Bodin 2023-10-04 15:55:33 +02:00
parent c29b11186e
commit 290f9f1269
8 changed files with 37 additions and 32 deletions

View File

@ -1,4 +1,5 @@
import knex, { Knex } from 'knex';
import knex from 'knex';
import type { Knex } from 'knex';
import SqliteClient from 'knex/lib/dialects/sqlite3/index';
class LegacySqliteClient extends SqliteClient {

View File

@ -57,10 +57,11 @@ import { EntityManager, Repository, Entity } from './types';
export * from './types';
const isObj = (value: unknown): value is object => isObject(value) && !isNil(value);
const isRecord = (value: unknown): value is Record<string, unknown> =>
isObject(value) && !isNil(value);
const toId = (value: unknown | { id: unknown }): ID => {
if (isObj(value) && 'id' in value && isValidId(value.id)) {
if (isRecord(value) && 'id' in value && isValidId(value.id)) {
return value.id;
}
@ -75,7 +76,7 @@ const toIds = (value: unknown): ID[] => castArray(value || []).map(toId);
const isValidId = (value: unknown): value is ID => isString(value) || isInteger(value);
const isValidObjectId = (value: unknown): value is Entity =>
isObj(value) && 'id' in value && isValidId(value.id);
isRecord(value) && 'id' in value && isValidId(value.id);
const toIdArray = (
data: unknown
@ -125,7 +126,7 @@ const toAssocs = (data: Assocs) => {
isString(data) ||
isNumber(data) ||
isNull(data) ||
(isObj(data) && 'id' in data)
(isRecord(data) && 'id' in data)
) {
return {
set: isNull(data) ? data : toIdArray(data),
@ -297,7 +298,7 @@ export const createEntityManager = (db: Database): EntityManager => {
.insert(dataToInsert)
.execute<Array<ID | { id: ID }>>();
const id = isObj(res[0]) ? res[0].id : res[0];
const id = isRecord(res[0]) ? res[0].id : res[0];
const trx = await strapi.db.transaction();
try {
@ -463,7 +464,7 @@ export const createEntityManager = (db: Database): EntityManager => {
.insert(dataToInsert)
.execute<Array<ID | { id: ID }>>();
const id = isObj(res[0]) ? res[0].id : res[0];
const id = isRecord(res[0]) ? res[0].id : res[0];
const trx = await strapi.db.transaction();
try {

View File

@ -148,10 +148,10 @@ const sortConnectArray = (connectArr: Link[], initialArr: Link[] = [], strictSor
* another one that does not exist
* @return {*}
*/
const relationsOrderer = <T extends Record<string, ID | number | null>>(
initArr: T[],
idColumn: keyof T,
orderColumn: keyof T,
const relationsOrderer = <TRelation extends Record<string, ID | number | null>>(
initArr: TRelation[],
idColumn: keyof TRelation,
orderColumn: keyof TRelation,
strict?: boolean
) => {
const computedRelations: OrderedLink[] = castArray(initArr ?? []).map((r) => ({

View File

@ -12,8 +12,8 @@ type Context = {
uid: string;
};
type Input<T extends RelationalAttribute = RelationalAttribute> = {
attribute: T;
type Input<TRelationAttribute extends RelationalAttribute = RelationalAttribute> = {
attribute: TRelationAttribute;
attributeName: string;
results: Row[];
populateValue: {
@ -23,9 +23,10 @@ type Input<T extends RelationalAttribute = RelationalAttribute> = {
isCount: boolean;
};
type InputWithTarget<T extends RelationalAttribute = RelationalAttribute> = Input<T> & {
targetMeta: Meta;
};
type InputWithTarget<TRelationAttribute extends RelationalAttribute = RelationalAttribute> =
Input<TRelationAttribute> & {
targetMeta: Meta;
};
type MorphIdMap = Record<string, Record<ID, Row[]>>;

View File

@ -75,10 +75,10 @@ const toSingleRow = (meta: Meta, data: Rec = {}): Row => {
return data;
};
function toRow<T extends Rec | Rec[] | null>(
function toRow<TData extends Rec | Rec[] | null>(
meta: Meta,
data: T
): T extends null ? null : T extends Rec[] ? Row[] : Rec;
data: TData
): TData extends null ? null : TData extends Rec[] ? Row[] : Rec;
function toRow(meta: Meta, data: Rec | Rec[] | null): Row | Row[] | null {
if (_.isNil(data)) {
return data;

View File

@ -12,7 +12,7 @@ import { isKnexQuery } from '../../utils/knex';
import type { Ctx } from '../types';
import type { Attribute } from '../../types';
const isObj = (value: unknown): value is Record<string, unknown> => isPlainObject(value);
const isRecord = (value: unknown): value is Record<string, unknown> => isPlainObject(value);
const castValue = (value: unknown, attribute: Attribute | null) => {
if (!attribute) {
@ -33,7 +33,7 @@ const processSingleAttributeWhere = (
where: unknown,
operator = '$eq'
) => {
if (!isObj(where)) {
if (!isRecord(where)) {
if (isOperatorOfType('cast', operator)) {
return castValue(where, attribute);
}
@ -65,7 +65,7 @@ const processAttributeWhere = (attribute: Attribute | null, where: unknown, oper
};
const processNested = (where: unknown, ctx: WhereCtx) => {
if (!isObj(where)) {
if (!isRecord(where)) {
return where;
}
@ -83,7 +83,7 @@ function processWhere(
where: Record<string, unknown> | Record<string, unknown>[],
ctx: WhereCtx
): Record<string, unknown> | Record<string, unknown>[] {
if (!isArray(where) && !isObj(where)) {
if (!isArray(where) && !isRecord(where)) {
throw new Error('Where must be an array or an object');
}
@ -139,7 +139,7 @@ function processWhere(
uid: attribute.target,
});
if (!isObj(nestedWhere) || isOperatorOfType('where', keys(nestedWhere)[0])) {
if (!isRecord(nestedWhere) || isOperatorOfType('where', keys(nestedWhere)[0])) {
nestedWhere = { [qb.aliasColumn('id', subAlias)]: nestedWhere };
}
@ -351,7 +351,7 @@ const applyWhereToColumn = (
column: string,
columnWhere: Record<Operator, unknown> | Array<Record<Operator, unknown>>
) => {
if (!isObj(columnWhere)) {
if (!isRecord(columnWhere)) {
if (Array.isArray(columnWhere)) {
return qb.whereIn(column, columnWhere);
}
@ -378,7 +378,7 @@ type Where =
| Array<Where>;
const applyWhere = (qb: Knex.QueryBuilder, where: Where) => {
if (!isArray(where) && !isObj(where)) {
if (!isArray(where) && !isRecord(where)) {
throw new Error('Where must be an array or an object');
}

View File

@ -42,13 +42,15 @@ export interface QueryBuilder {
clone(): QueryBuilder;
select(args: string | Array<string | Knex.Raw>): QueryBuilder;
addSelect(args: string | string[]): QueryBuilder;
insert<T extends Record<string, unknown> | Record<string, unknown>[]>(data: T): QueryBuilder;
insert<TData extends Record<string, unknown> | Record<string, unknown>[]>(
data: TData
): QueryBuilder;
onConflict(args: any): QueryBuilder;
merge(args: any): QueryBuilder;
ignore(): QueryBuilder;
delete(): QueryBuilder;
ref(name: string): any;
update<T extends Record<string, unknown>>(data: T): QueryBuilder;
update<TData extends Record<string, unknown>>(data: TData): QueryBuilder;
increment(column: string, amount?: number): QueryBuilder;
decrement(column: string, amount?: number): QueryBuilder;
count(count?: string): QueryBuilder;

View File

@ -165,15 +165,15 @@ const onlineUpdate = async ({ strapi }: { strapi: Strapi }) => {
if (shouldContactRegistry) {
result.license = license ?? null;
const query = strapi.db?.queryBuilder('strapi::core-store').transacting(transaction);
const query = strapi.db!.queryBuilder('strapi::core-store').transacting(transaction);
if (!storedInfo) {
query?.insert({ key: 'ee_information', value: JSON.stringify(result) });
query.insert({ key: 'ee_information', value: JSON.stringify(result) });
} else {
query?.update({ value: JSON.stringify(result) }).where({ key: 'ee_information' });
query.update({ value: JSON.stringify(result) }).where({ key: 'ee_information' });
}
await query?.execute();
await query.execute();
}
await commit();