Skip to content
Closed
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 .github/workflows/dockerize.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ jobs:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
with:
node-version: 18
node-version: '16.20'
- name: ci
run: npm ci
- name: build
Expand Down
2 changes: 1 addition & 1 deletion .nvmrc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
18
18.16.1
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM node:18-alpine3.17 AS node
FROM node:18.16.1-alpine3.17 AS node
FROM docker:20.10.8-dind-alpine3.14

COPY --from=node /usr/lib /usr/lib
Expand Down
163 changes: 130 additions & 33 deletions imports/client.tsx

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions imports/minilinks-query.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { _serialize, _boolExpFields, serializeWhere, serializeQuery } from './client.js';
import { _serialize, _boolExpFields, serializeWhere, serializeQuery, useDeep } from './client.js';
import { BoolExpLink, ComparasionType, QueryLink } from './client_types.js';
import { MinilinkCollection, MinilinksGeneratorOptions, Link } from './minilinks.js';

Expand All @@ -12,7 +12,7 @@ export const minilinksQuery = <L extends Link<number>>(
): L[] => {
if (typeof(query) === 'number') return [ml.byId[query]];
else {
const q = serializeQuery(query);
const q = serializeQuery(query, 'links');
const result = minilinksQueryHandle<L>(q.where, ml);
return q.limit ? result.slice(q.offset || 0, (q.offset || 0) + (q.limit)) : result;
}
Expand All @@ -24,7 +24,7 @@ export const minilinksQueryIs = <L extends Link<number>>(
): boolean => {
if (typeof(query) === 'number') return link.id === query;
else {
const q = serializeQuery(query);
const q = serializeQuery(query, 'links');
return minilinksQueryLevel(
q,
link,
Expand Down
91 changes: 87 additions & 4 deletions imports/minilinks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,14 @@ export interface LinkHashFields {
[key: string|number]: any;
}

export interface Link<Ref extends number> extends LinkPlain<Ref>, LinkRelations<Link<Ref>>, LinkHashFields {}
export interface Link<Ref extends number> extends LinkPlain<Ref>, LinkRelations<Link<Ref>>, LinkHashFields {
_id?: number;
displayId: number;
}

export interface MinilinksResult<Link> {
virtual: { [id: number]: number };
virtualCounter: number;
links: Link[];
types: { [id: number]: Link[] };
byId: { [id: number]: Link };
Expand All @@ -65,10 +70,15 @@ export interface MinilinksResult<Link> {
errors?: MinilinkError[];
anomalies?: MinilinkError[];
}
update(linksArray: any[]): {
errors?: MinilinkError[];
anomalies?: MinilinkError[];
}
}

export class MinilinksLink<Ref extends number> {
ml?: MinilinkCollection<any, any>;
_id: Ref;
id: Ref;
type_id: Ref;
from_id?: Ref;
Expand Down Expand Up @@ -109,6 +119,9 @@ export class MinilinksLink<Ref extends number> {
get to(): MinilinksLink<Ref>[] {
return this.ml?.byId?.[this.to_id];
}
get displayId(): number {
return this._id || this.id;
}
value?: any;
string?: any;
number?: any;
Expand All @@ -124,7 +137,7 @@ export class MinilinksLink<Ref extends number> {
}
toPlain(): LinkPlain<Ref> {
return {
id: this.id,
id: this._id || this.id,
type_id: this.type_id,
from_id: this.from_id,
to_id: this.to_id,
Expand Down Expand Up @@ -195,6 +208,8 @@ export class MinilinkCollection<MGO extends MinilinksGeneratorOptions = typeof M
useMinilinksSubscription = useMinilinksSubscription;
useMinilinksHandle = useMinilinksHandle;

virtual: { [id: number]: number } = {};
virtualCounter = -1;
types: { [id: number]: L[] } = {};
byId: { [id: number]: L } = {};
byFrom: { [id: number]: L[] } = {};
Expand All @@ -220,8 +235,11 @@ export class MinilinkCollection<MGO extends MinilinksGeneratorOptions = typeof M
if (byId[linksArray[l][options.id]]) {
if (options.handler) options.handler(byId[linksArray[l][options.id]], this);
} else {
const isVirtual = linksArray[l].id < 0;
if (isVirtual) this.virtual[linksArray[l].id] = undefined;
const link = new this.options.Link({
ml: this,
_id: isVirtual ? undefined : linksArray[l].id,
_applies: [],
...linksArray[l],
});
Expand Down Expand Up @@ -407,8 +425,26 @@ export class MinilinkCollection<MGO extends MinilinksGeneratorOptions = typeof M
const toRemove = [];
const _byId: any = {};
for (let l = 0; l < linksArray.length; l++) {
const link = linksArray[l];
const old = byId[link.id];
let link = linksArray[l];

// find virtual
let old = byId[link.id];
const virtualIds = Object.keys(this.virtual);
const [virtual] = this.query({
id: { _in: virtualIds.map(i => Number(i)) },
...(link.type_id ? { type_id: link.type_id } : {}),
...(link.from_id ? { from_id: link.from_id } : {}),
...(link.to_id ? { to_id: link.to_id } : {}),
...(link.value ? { value: link.value } : {}),
});
if (virtual) {
if (old) throw new Error(`somehow we have oldLink.id ${old.id} and virtualLink.id ${virtual.id} virtualLink._id = ${virtual._id}`);
old = virtual;
this.virtual[virtual.id] = link.id;
virtual._id = link.id;
link = { ...link, _id: link.id, id: virtual.id };
}

if (!old) {
link._applies = [applyName];
this.emitter.emit('apply', old, link);
Expand Down Expand Up @@ -455,6 +491,53 @@ export class MinilinkCollection<MGO extends MinilinksGeneratorOptions = typeof M
this._updating = false;
return { errors: [...r1.errors, ...a1.errors, ...r2.errors, ...a2.errors], anomalies: [...r1.anomalies, ...a1.anomalies, ...r2.anomalies, ...a2.anomalies] };
}
update(linksArray: any[]): {
errors?: MinilinkError[];
anomalies?: MinilinkError[];
} {
log('update', linksArray, this);
const { byId, byFrom, byTo, byType, types, links, options } = this;
const toUpdate = [];
const beforeUpdate = {};
const _byId: any = {};
for (let l = 0; l < linksArray.length; l++) {
let link = linksArray[l];

// find virtual
let old = byId[link.id];
const virtualIds = Object.keys(this.virtual);
const [virtual] = this.query({
id: { _in: virtualIds.map(i => Number(i)) },
...(link.type_id ? { type_id: link.type_id } : {}),
...(link.from_id ? { from_id: link.from_id } : {}),
...(link.to_id ? { to_id: link.to_id } : {}),
...(link.value ? { value: link.value } : {}),
});
if (virtual) {
if (old) throw new Error(`somehow we have oldLink.id ${old.id} and virtualLink.id ${virtual.id} virtualLink._id = ${virtual._id}`);
old = virtual;
virtual._id = link.id;
link = { ...link, _id: link.id, id: virtual.id };
}

if (old) {
if (!options.equal(old, link)) {
toUpdate.push(link);
beforeUpdate[link.id] = old;
}
}
_byId[link.id] = link;
}
this._updating = true;
const r2 = this.remove(toUpdate.map(l => l[options.id]));
const a2 = this.add(toUpdate);
for (let i = 0; i < toUpdate.length; i++) {
const l = toUpdate[i];
this.emitter.emit('updated', beforeUpdate[l.id], byId[l.id]);
}
this._updating = false;
return { errors: [...r2.errors, ...a2.errors], anomalies: [...r2.anomalies, ...a2.anomalies] };
}
constructor(options?: MGO, memory?: any) {
const _options = options || MinilinksGeneratorOptionsDefault;
this.types = this.byType = memory?.types || {};
Expand Down
4 changes: 2 additions & 2 deletions imports/packager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ export class Packager<L extends Link<any>> {
let newId;
if (item.package) {
newId = await this.client.id(pckg?.dependencies?.[item?.package?.dependencyId]?.name, item.package.containValue, true);
if (!newId) pckg.errors.push(`dependency [${pckg?.dependencies?.[item?.package?.dependencyId]?.name} ${item.package.containValue}], not found`);
if (!newId) pckg.errors.push(`dependency [${pckg?.dependencies?.[item?.package?.dependencyId]?.name, item.package.containValue}], not founded`);
} else if (item.type) {
newId = ids[idsIndex++];
}
Expand Down Expand Up @@ -371,7 +371,7 @@ export class Packager<L extends Link<any>> {
const ids = await this.client.reserve(counter);
const { global, difference } = await this.globalizeIds(pckg, ids, sorted);
if (pckg.errors?.length) {
return { errors: pckg.errors };
return { errors };
}
await this.insertItems(pckg, global, counter, dependedLinks, errors, mutated);
if (errors.length) return { errors };
Expand Down
8 changes: 3 additions & 5 deletions migrations/1655979260869-sync-handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ const prepareFunction = /*javascript*/`
const selectValueTable = `\`SELECT * FROM \${table} WHERE link_id = \${linkId}\``;
const selectLinkByValue = `\`SELECT link_id as id FROM \${table} WHERE value = '\${value}'::\${table==='strings' ? 'text' : table==='objects' ? 'jsonb' : 'bigint'}\``;

const generateSelectWhereCode = /*javascript*/`(_where, shift = 0) => {
const generateSelectWhereCode = /*javascript*/`(_where) => {
const where = [];
let values = [];
let valueTable;
Expand Down Expand Up @@ -557,7 +557,7 @@ const deepFabric = /*javascript*/`(ownerId, hasura_session) => {
const findLinkIdByValue = ${findLinkIdByValueCode};
const fillValueByLinks = ${fillValueByLinksCode};
const isDeepEqual = ${isDeepEqualCode};
let generated = generateSelectWhere(_where, 1);
let generated = generateSelectWhere(_where);
const where = generated.where;
let links = [];
const valueTableString = generated.valueTable ? generated.valueTable === 'values' ? ' left join "strings" on "strings".link_id = "main".id left join "objects" on "objects".link_id = "main".id left join "numbers" on "numbers".link_id = "main".id' : ' left join "'.concat(generated.valueTable, '" on "',generated.valueTable,'".link_id = "main".id') : '';
Expand All @@ -569,7 +569,7 @@ const deepFabric = /*javascript*/`(ownerId, hasura_session) => {
if (options?.table === 'tree'){
const { id, link_id, parent_id, depth, root_id, position_id, tree_id } = _where;
const generateSelectWhere = ${generateSelectWhereCode};
let generated = generateSelectWhere(_where, 1);
let generated = generateSelectWhere(_where);
const where = generated.where;
let links = [];
if (where) links = plv8.execute(${selectTreeWithPermissions}, [ this.linkId, ...generated.values ]);
Expand Down Expand Up @@ -795,14 +795,12 @@ const triggerFunctionFabric = (handleOperationTypeId, valueTrigger) => /*javascr
`;

const deepClientFunction = /*javascript*/`

const checkInsertPermission = ${checkInsertPermissionCode};
const checkUpdatePermission = ${checkUpdatePermissionCode};
const checkDeleteLinkPermission = ${checkDeleteLinkPermissionCode};
const hasura_session = JSON.parse(plv8.execute("select current_setting('hasura.user', 't')")[0].current_setting);
const default_role = hasura_session['x-hasura-role'];
const default_user_id = hasura_session['x-hasura-user-id'];

const deep = (${deepFabric})(Number(clientlinkid), hasura_session);
const result = operation === 'id' || operation === 'update' || operation === 'objectSet' || operation === 'objectGet' ? deep[operation](...args) : operation === 'unsafe' ? deep[operation].plv8.execute(...args) : deep[operation](args, options);
if (hasura_session['x-hasura-role'] !== default_role || hasura_session['x-hasura-user-id'] !== default_user_id){
Expand Down
31 changes: 0 additions & 31 deletions migrations/1693460330175-sync-handlers-update.ts

This file was deleted.

2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 3 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,8 @@
"benchmark": "export DEEPLINKS_HASURA_PATH=localhost:8080; export DEEPLINKS_HASURA_SSL=0; export DEEPLINKS_HASURA_SECRET=myadminsecretkey; ts-node benchmarks/index.ts",
"migrate": "npm run package:build && npx migrate@latest up --matches '*.js'",
"unmigrate": "npm run package:build && npx migrate@latest down --matches '*.js'",
"migrate-s-h": "npm run package:build && npx migrate@latest up 1693460330175-sync-handlers-update --matches '*.js'",
"unmigrate-s-h": "npm run package:build && npx migrate@latest down 1693460330175-sync-handlers-update --matches '*.js'",
"remigrate-s-h": "npm run unmigrate-s-h && npm run migrate",
"migrate-s-h": "npm run package:build && npx migrate@latest up 1655979260869-sync-handlers --matches '*.js'",
"unmigrate-s-h": "npm run package:build && npx migrate@latest down 1655979260869-sync-handlers --matches '*.js'",
"start-engine-docker": "npm run start-engine-docker-core",
"start-engine-docker-core": "cd ./node_modules/@deep-foundation/hasura && npm run docker",
"stop-engine-docker": "cd ./node_modules/@deep-foundation/hasura/local && docker-compose --project-name deep down",
Expand Down Expand Up @@ -91,7 +90,7 @@
"react": "^18.0.0"
},
"engines": {
"node": "^18"
"node": "^18.16.1"
},
"devDependencies": {
"@testing-library/jest-dom": "^5.16.5",
Expand Down