Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -1 +1 @@
pnpm test
pnpm test:typedb-ignoreTodo
4 changes: 4 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

📝 following beta format X.Y.Z where Y = breaking change and Z = feature and fix. Later => FAIL.FEATURE.FIX

## 0.10.4(2024-05-01)

- Feat: Filtered mutations

## 0.10.3(2024-04-22)

- Refacto: Pre queries dependencies refacto
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@blitznocode/blitz-orm",
"version": "0.10.3",
"version": "0.10.4",
"main": "dist/index.cjs",
"module": "./dist/index.mjs",
"types": "dist/index.d.ts",
Expand Down Expand Up @@ -32,6 +32,7 @@
"test:typedb-ignoreTodo": "vitest run typedb -t \"^(?!.*TODO:).*\" ",
"test:typedb-mutation": "vitest run typedb/unit/mutations",
"test:typedb-query": "vitest run typedb/unit/queries",
"test:typedb-schema": "vitest run typedb/unit/schema",
"test:watch": "./tests/test.sh --watch",
"types": "tsc --noEmit",
"prepare": "husky"
Expand Down
1 change: 0 additions & 1 deletion src/adapters/surrealDB/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ const buildQuery = (thing: string, query: EnrichedBqlQuery) => {
};

const buildSurrealDbQuery: PipelineOperation<SurrealDbResponse> = async (req, res) => {
console.log('\n\nbuildSurrealDbQuery\n', JSON.stringify(req));
const { dbHandles, enrichedBqlQuery, schema } = req;

if (!enrichedBqlQuery) {
Expand Down
6 changes: 5 additions & 1 deletion src/define/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ export const bormDefine = async (config: BormConfig, schema: BormSchema, dbHandl
newUsedAttributes.forEach((attribute: Attribute) => {
attributes += `${attribute.dbPath} sub attribute,\n`;
// All conditions for BORM to TQL attribute types
if (attribute.contentType === 'TEXT' || attribute.contentType === 'ID') {
if (attribute.contentType === 'TEXT' || attribute.contentType === 'ID' || attribute.contentType === 'JSON') {
attributes += ' value string;\n';
} else if (attribute.contentType === 'EMAIL') {
attributes += ' value string,\n';
Expand All @@ -204,6 +204,10 @@ export const bormDefine = async (config: BormConfig, schema: BormSchema, dbHandl
attributes += ' value boolean;\n';
} else if (attribute.contentType === 'NUMBER') {
attributes += ' value long;\n';
} else {
throw new Error(
`Conversion of borm schema to TypeDB schema for data type "${attribute.contentType}" is not implemented`,
);
}
});

Expand Down
1 change: 0 additions & 1 deletion src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,6 @@ export const enrichSchema = (schema: BormSchema, dbHandles: DBHandles): Enriched
}),
) as EnrichedBormSchema;

// console.log('enrichedShema', JSON.stringify(enrichedSchema, null, 3));
return enrichedSchema;
};

Expand Down
26 changes: 8 additions & 18 deletions src/stateMachine/mutation/BQL/enrich.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,7 @@ const cleanStep = (node: BQLMutationBlock, field: string) => {
};

// eslint-disable-next-line @typescript-eslint/no-unused-vars
const dataFieldStep = (node: BQLMutationBlock, field: string) => {
//console.log(`${field}:node[field]`);
};
const dataFieldStep = (node: BQLMutationBlock, field: string) => {};

export const enrichBQLMutation = (
blocks: BQLMutationBlock | BQLMutationBlock[] | EnrichedBQLMutationBlock | EnrichedBQLMutationBlock[],
Expand All @@ -55,11 +53,11 @@ export const enrichBQLMutation = (
return;
}
if (isObject(value)) {
const paths = meta.nodePath?.split('.') || [];
if ('$root' in value) {
// This is hte $root object, we will split the real root if needed in this iteration
} else if (!('$thing' in value || '$entity' in value || '$relation' in value)) {
const toIgnore = ['$fields', '$dbNode'];
const paths = meta.nodePath?.split('.') || [];
const toIgnore = ['$fields', '$dbNode', '$filter'];
const lastPath = paths[paths.length - 1];
const secondToLastPath = paths[paths.length - 2];
if (key === '$root') {
Expand All @@ -72,15 +70,18 @@ export const enrichBQLMutation = (
}

const node = value as EnrichedBQLMutationBlock;
const isFilter = paths.includes('$filter');

Object.keys(node).forEach((field) => {
///1. Clean step
cleanStep(node, field);
if (field !== '$root' && isFilter) {
return;
}

if (field !== '$root' && (field.startsWith('$') || field.startsWith('%'))) {
return;
}

const fieldSchema =
field !== '$root' ? getFieldSchema(schema, node, field) : ({ fieldType: 'rootField' } as any);
if (!fieldSchema) {
Expand All @@ -100,17 +101,14 @@ export const enrichBQLMutation = (
if (['rootField', 'linkField', 'roleField'].includes(fieldSchema.fieldType)) {
///In the next steps we have (isArray(node[field]) ? node[field] : [node[field]]) multiple times, because it might mutate, can't replace by a var

//console.log('Before enrich', JSON.stringify(isDraft(node) ? current(node) : node, null, 2));
/// 3.2.1 replaces
//console.log('Before replace', JSON.stringify(isDraft(node) ? current(node) : node, null, 2));
if (['linkField', 'roleField'].includes(fieldSchema.fieldType)) {
if (node[field] === null) {
unlinkAll(node, field, fieldSchema);
} else {
replaceToObj(node, field, fieldSchema);
}
}
//console.log('After replace', JSON.stringify(isDraft(node) ? current(node) : node, null, 2));

//3.2.2 root $thing
if (fieldSchema.fieldType === 'rootField') {
Expand All @@ -119,13 +117,12 @@ export const enrichBQLMutation = (
}
const rootNode = node as unknown as { $root: BQLMutationBlock | BQLMutationBlock[] };
setRootMeta(rootNode, schema);
//console.log('After rootMeta', JSON.stringify(isDraft(node) ? current(node) : node, null, 2));
}

// 3.2.3 BQL pre-validations => All validations should happen on subNode, if else, leaves are skipped
const preValidate = isArray(node[field]) ? node[field] : [node[field]];

const cleanPath = meta.nodePath?.split('.').slice(1).join('.');
const cleanPath = paths.slice(1).join('.');
preValidate.forEach((subNode: BQLMutationBlock) => {
if (!subNode) {
return;
Expand Down Expand Up @@ -164,16 +161,11 @@ export const enrichBQLMutation = (

//3.2.5 splitIds()
splitMultipleIds(node, field, schema);
//console.log('After splitIds', JSON.stringify(isDraft(node) ? current(node) : node, null, 2));

//console.log('After enrichChildren', JSON.stringify(isDraft(node) ? current(node) : node, null, 2));

/// 3.2.6 Field computes
if (['rootField', 'linkField', 'roleField'].includes(fieldSchema.fieldType)) {
//console.log('toBeComputed', node, field);
computeFields(node, field, schema);
}
//console.log('After computeFields', JSON.stringify(isDraft(node) ? current(node) : node, null, 2));

// 3.2.7
//#region BQL validations
Expand Down Expand Up @@ -232,8 +224,6 @@ export const enrichBQLMutation = (
}
}),
);
//console.log('After enrich', result.$rootWrap.$root);
//console.log('After enrich', JSON.stringify(result.$rootWrap.$root, null, 2));

if (isArray(result.$rootWrap.$root)) {
return result.$rootWrap.$root as EnrichedBQLMutationBlock[];
Expand Down
2 changes: 0 additions & 2 deletions src/stateMachine/mutation/BQL/enrichSteps/computeFields.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ export const computeFields = (node: BQLMutationBlock, field: string, schema: Enr

// fill computed values
missingComputedFields.forEach((fieldPath) => {
//console.log('fieldPath', fieldPath);

const currentFieldDef = currentSchema.dataFields?.find((x) => x.path === fieldPath);
const currentLinkDef = currentSchema.linkFields?.find((x) => x.path === fieldPath);
// todo: multiple playedBy
Expand Down
17 changes: 12 additions & 5 deletions src/stateMachine/mutation/BQL/enrichSteps/preHookDependencies.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { isObject } from 'radash';
import type {
BQLResponse,
BormConfig,
DBHandles,
EnrichedBQLMutationBlock,
Expand All @@ -12,8 +13,8 @@ import type {
FilledBQLMutationBlock,
} from '../../../../types';
import { getThing } from '../../../../helpers';
import { queryPipeline } from '../../../../pipeline/pipeline';
import { DBNode } from '../../../../types/symbols';
import { runQueryMachine } from '../../../query/machine';

export const preHookDependencies = async (
blocks: EnrichedBQLMutationBlock | EnrichedBQLMutationBlock[],
Expand All @@ -23,13 +24,19 @@ export const preHookDependencies = async (
) => {
const mutations = Array.isArray(blocks) ? blocks : [blocks];
const transformationPreQueryReq = mutations.map((m) => mutationToQuery(m, true));
// @ts-expect-error todo
const transformationPreQueryRes = await queryPipeline(transformationPreQueryReq, config, schema, dbHandles);
return mutations.map((mut) => {
const res = await runQueryMachine(
// @ts-expect-error todo
transformationPreQueryReq,
schema,
config,
dbHandles,
);
const transformationPreQueryRes = res.bql.res as BQLResponse[];
return mutations.map((mut, i) => {
const thing = getThing(schema, mut.$thing);
return setDbNode({
mut: mut as Mutation,
node: transformationPreQueryRes as DbValue,
node: transformationPreQueryRes[i] as DbValue,
schema,
thing,
});
Expand Down
3 changes: 0 additions & 3 deletions src/stateMachine/mutation/BQL/enrichSteps/splitIds.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,12 @@ export const splitMultipleIds = (node: BQLMutationBlock, field: string, schema:
if (isObject(child) && '$id' in child && isArray(child['$id'])) {
const subNode = child as EnrichedBQLMutationBlock & { $id: string[] };
/*const childSchema =*/ getCurrentSchema(schema, subNode);
//console.log('childSchema', childSchema);
/// Depending on the DB this operation is required or not
if (!subNode.$bzId) {
throw new Error('[Internal Error] No bzId found');
}
// eslint-disable-next-line no-constant-condition
if (/*childSchema.dbContext.mutation?.splitArray$Ids*/ true) {
//console.log('subNode', subNode);
return subNode.$id.map(($id: string, i: number) => ({
...deepCurrent(subNode), //structured clone generates a weird bug with traverse, so not using it
$id: $id,
Expand All @@ -30,7 +28,6 @@ export const splitMultipleIds = (node: BQLMutationBlock, field: string, schema:
return child;
},
);
//console.log('transformedChildren', transformedChildren);
// if we splitted something, then reassign
if (transformedChildren.length > isArray(node[field]) ? node[field] : [node[field]].length) {
// eslint-disable-next-line no-param-reassign
Expand Down
2 changes: 0 additions & 2 deletions src/stateMachine/mutation/BQL/intermediary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,5 @@ export const addIntermediaryRelationsBQLMutation = (
}),
);

//console.log('after intermediaries', result.$root.$subRoot);
//console.log('After intermediaries', JSON.stringify(result.$root.$subRoot, null, 2));
return result.$root.$subRoot;
};
18 changes: 0 additions & 18 deletions src/stateMachine/mutation/BQL/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@ export const parseBQLMutation = async (
blocks: EnrichedBQLMutationBlock | EnrichedBQLMutationBlock[],
schema: EnrichedBormSchema,
) => {
//console.log('blocks.NEW', JSON.stringify(blocks, null, 2));
//console.log('blocks.NEW', isArray(blocks) ? blocks[0]?.spaces : blocks.spaces);

const listNodes = (blocks: EnrichedBQLMutationBlock | EnrichedBQLMutationBlock[]) => {
// todo: make immutable

Expand Down Expand Up @@ -165,8 +162,6 @@ export const parseBQLMutation = async (
/// split nodes with multiple ids // why? //no longer doing that
toNodes(dataObj);

// console.log('value', isDraft(value) ? current(value) : value);

// CASE 1: HAVE A PARENT THROUGH LINKFIELDS
const edgeSchema = value[EdgeSchema] as EnrichedLinkField;

Expand Down Expand Up @@ -290,8 +285,6 @@ export const parseBQLMutation = async (
return [k, v];
});

// console.log('rolesObjOnlyIds', rolesObjOnlyIds);

const objWithMetaDataOnly = oFilter(val, (k, _v) => {
// @ts-expect-error - TODO description
return k.startsWith('$') || k.startsWith('Symbol');
Expand Down Expand Up @@ -324,7 +317,6 @@ export const parseBQLMutation = async (
if (v.length > 1) {
throw new Error(`[Error] Role ${k} is not a MANY relation`);
} else {
//console.log('v', v, blocks);
return [k, v[0].$bzId || v[0]];
}
}
Expand All @@ -334,7 +326,6 @@ export const parseBQLMutation = async (
//@ts-expect-error - TODO
return [k, v.$bzId || v];
});
// console.log('rolesObjOnlyIdsGrouped', rolesObjOnlyIdsGrouped);

// todo: validations
/// 1) each ONE role has only ONE element // 2) no delete ops // 3) no arrayOps, because it's empty (or maybe yes and just consider it an add?) ...
Expand Down Expand Up @@ -409,17 +400,13 @@ export const parseBQLMutation = async (
}
}
};
// console.log('[blocks]', JSON.stringify(blocks, null, 3));
// console.log('[blocks]', blocks);

traverse(blocks, listOp);

return [nodes, edges];
};

const [parsedThings, parsedEdges] = listNodes(blocks);
//console.log('parsedThings', parsedThings);
//console.log('parsedEdges', parsedEdges);

/// some cases where we extract things, they must be ignored.
/// One of this cases is the situation where we have a thing that is linked somwhere and created, or updated.
Expand Down Expand Up @@ -516,9 +503,6 @@ export const parseBQLMutation = async (
return [...acc, curr];
}, [] as BQLMutationBlock[]);

//console.log('mergedThings', mergedThings);
//console.log('mergedEdges', mergedEdges);

/// VALIDATIONS

// VALIDATION: Check that every thing in the list that is an edge, has at least one player
Expand Down Expand Up @@ -552,8 +536,6 @@ export const parseBQLMutation = async (
);
}

//console.log('mergedThings', mergedThings);
//console.log('mergedEdges', mergedEdges);
return {
mergedThings,
mergedEdges,
Expand Down
Loading