Skip to content

Commit

Permalink
remove allow_delete from sync
Browse files Browse the repository at this point in the history
  • Loading branch information
prostgles committed Dec 21, 2024
1 parent 1e834bb commit 44256e5
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 68 deletions.
6 changes: 3 additions & 3 deletions lib/DboBuilder/TableHandler/TableHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ export class TableHandler extends ViewHandler {
if (invalidParams.length)
throw "Invalid or dissallowed params found: " + invalidParams.join(", ");

const { synced_field, allow_delete }: SyncRule = table_rules.sync;
const { synced_field }: SyncRule = table_rules.sync;

if (!table_rules.sync.id_fields.length || !synced_field) {
const err = "INTERNAL ERROR: id_fields OR synced_field missing from publish";
Expand Down Expand Up @@ -212,13 +212,13 @@ export class TableHandler extends ViewHandler {
condition,
id_fields,
synced_field,
allow_delete,
// allow_delete,
socket,
table_rules,
filter: { ...filter },
params: { select },
})
.then((channelName) => ({ channelName, id_fields, synced_field }));
.then(({ channelName }) => ({ channelName, id_fields, synced_field }));
});
await this._log({
command: "sync",
Expand Down
1 change: 0 additions & 1 deletion lib/PubSubManager/PubSubManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ export type SyncParams = {
table_name: string;
table_rules?: TableRule;
synced_field: string;
allow_delete: boolean;
id_fields: string[];
batch_size: number;
filter: object;
Expand Down
33 changes: 17 additions & 16 deletions lib/PubSubManager/addSync.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { find, tryCatch } from "prostgles-types/dist/util";
import { find, tryCatchV2 } from "prostgles-types/dist/util";
import {
AddSyncParams,
BasicCallback,
Expand All @@ -11,17 +11,19 @@ import {
* Returns a sync channel
* A sync channel is unique per socket for each filter
*/
export async function addSync(this: PubSubManager, syncParams: AddSyncParams) {
export async function addSync(
this: PubSubManager,
syncParams: AddSyncParams
): Promise<{ channelName: string }> {
const sid = this.dboBuilder.prostgles.authHandler?.getSIDNoError({
socket: syncParams.socket,
});
const res = await tryCatch(async () => {
const res = await tryCatchV2(async () => {
const {
socket = null,
table_info = null,
table_rules,
synced_field = null,
allow_delete = false,
id_fields = [],
filter = {},
params,
Expand All @@ -32,22 +34,21 @@ export async function addSync(this: PubSubManager, syncParams: AddSyncParams) {
if (!socket || !table_info) throw "socket or table_info missing";

const { name: table_name } = table_info;
const channel_name = `${this.socketChannelPreffix}.${table_name}.${JSON.stringify(filter)}.sync`;
const channelName = `${this.socketChannelPreffix}.${table_name}.${JSON.stringify(filter)}.sync`;

if (!synced_field) throw "synced_field missing from table_rules";

this.upsertSocket(socket);

const upsertSync = () => {
const newSync = {
channel_name,
channel_name: channelName,
table_name,
filter,
condition: conditionParsed,
synced_field,
sid,
id_fields,
allow_delete,
table_rules,
throttle: Math.max(throttle || 0, table_rules.sync?.throttle || 0),
batch_size: table_rules.sync?.batch_size || DEFAULT_SYNC_BATCH_SIZE,
Expand All @@ -63,11 +64,11 @@ export async function addSync(this: PubSubManager, syncParams: AddSyncParams) {
};

/* Only a sync per socket per table per condition allowed */
const existing = find(this.syncs, { socket_id: socket.id, channel_name });
const existing = find(this.syncs, { socket_id: socket.id, channel_name: channelName });
if (!existing) {
this.syncs.push(newSync);

const unsyncChn = channel_name + "unsync";
const unsyncChn = channelName + "unsync";
socket.removeAllListeners(unsyncChn);
socket.once(unsyncChn, (_data: any, cb: BasicCallback) => {
this._log({
Expand All @@ -80,18 +81,18 @@ export async function addSync(this: PubSubManager, syncParams: AddSyncParams) {
connectedSocketIds: this.connectedSocketIds,
duration: -1,
});
socket.removeAllListeners(channel_name);
socket.removeAllListeners(channelName);
socket.removeAllListeners(unsyncChn);
this.syncs = this.syncs.filter((s) => {
const isMatch =
s.socket_id && s.socket_id === socket.id && s.channel_name === channel_name;
s.socket_id && s.socket_id === socket.id && s.channel_name === channelName;
return !isMatch;
});
cb(null, { res: "ok" });
});

socket.removeAllListeners(channel_name);
socket.on(channel_name, (data: any, cb: BasicCallback) => {
socket.removeAllListeners(channelName);
socket.on(channelName, (data: any, cb: BasicCallback) => {
if (!data) {
cb({ err: "Unexpected request. Need data or onSyncRequest" });
return;
Expand Down Expand Up @@ -130,7 +131,7 @@ export async function addSync(this: PubSubManager, syncParams: AddSyncParams) {

await this.addTrigger({ table_name, condition: conditionParsed }, undefined, socket);

return { result: channel_name };
return { channelName };
});

await this._log({
Expand All @@ -145,7 +146,7 @@ export async function addSync(this: PubSubManager, syncParams: AddSyncParams) {
sid,
});

if (res.error !== undefined) throw res.error;
if (res.hasError) throw res.error;

return res.result;
return res.data;
}
4 changes: 2 additions & 2 deletions lib/PublishParser/publishTypesAndUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ export const RULE_TO_METHODS = [
no_limits: null,
table_only: true,
allowed_params: {
allow_delete: 1,
// allow_delete: 1,
batch_size: 1,
id_fields: 1,
synced_field: 1,
Expand Down Expand Up @@ -379,7 +379,7 @@ export type SyncRule<Cols extends AnyObject = AnyObject> = {
/**
* EXPERIMENTAL. Disabled by default. If true then server will attempt to delete any records missing from client.
*/
allow_delete?: boolean;
// allow_delete?: boolean;

/**
* Throttle replication transmission in milliseconds. Defaults to 100
Expand Down
84 changes: 42 additions & 42 deletions lib/SyncReplication.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ export async function syncData(
table_name,
filter,
table_rules,
allow_delete = false,
params,
synced_field,
id_fields = [],
Expand Down Expand Up @@ -210,28 +209,28 @@ export async function syncData(
},
deleteData = async (deleted: AnyObject[]) => {
// console.log("deleteData deleteData deleteData " + deleted.length);
if (allow_delete) {
return Promise.all(
deleted.map(async (d) => {
const id_filter = pickKeys(d, id_fields);
try {
await (this.dbo[table_name] as TableHandler).delete(
id_filter,
undefined,
undefined,
table_rules
);
return 1;
} catch (e) {
console.error(e);
}
return 0;
})
);
} else {
console.warn("client tried to delete data without permission (allow_delete is false)");
}
return false;
// if (allow_delete) {
// return Promise.all(
// deleted.map(async (d) => {
// const id_filter = pickKeys(d, id_fields);
// try {
// await (this.dbo[table_name] as TableHandler).delete(
// id_filter,
// undefined,
// undefined,
// table_rules
// );
// return 1;
// } catch (e) {
// console.error(e);
// }
// return 0;
// })
// );
// } else {
// console.warn("client tried to delete data without permission (allow_delete is false)");
// }
// return false;
},
/**
* Upserts the given client data where synced_field is higher than on server
Expand Down Expand Up @@ -463,8 +462,8 @@ export async function syncData(
let inserted = 0,
updated = 0,
pushed = 0,
deleted = 0,
total = 0;
const deleted = 0;

// console.log("syncBatch", from_synced)

Expand All @@ -486,24 +485,25 @@ export async function syncData(
throw " d";
}

// console.log("allow_delete", table_rules.delete);
if (allow_delete && table_rules?.delete) {
const to_delete = serverData.filter((d) => {
return !clientData.find((c) => rowsIdsMatch(c, d));
});
await Promise.all(
to_delete.map((d) => {
deleted++;
return (this.dbo[table_name] as TableHandler).delete(
pickKeys(d, id_fields),
{},
undefined,
table_rules
);
})
);
serverData = await getServerData(min_synced, offset);
}
// TODO: Implement delete ensuring:
// 1. Delete is preformed only when clientData is fully synced (is kept in localStorage/IndexedDB OR is fully synced)
// if (allow_delete && table_rules?.delete) {
// const to_delete = serverData.filter((d) => {
// return !clientData.find((c) => rowsIdsMatch(c, d));
// });
// await Promise.all(
// to_delete.map((d) => {
// deleted++;
// return (this.dbo[table_name] as TableHandler).delete(
// pickKeys(d, id_fields),
// {},
// undefined,
// table_rules
// );
// })
// );
// serverData = await getServerData(min_synced, offset);
// }

const forClient = serverData.filter((s) => {
return !clientData.find(
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "prostgles-server",
"version": "4.2.189",
"version": "4.2.190",
"description": "",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
2 changes: 1 addition & 1 deletion tests/server/package-lock.json

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

0 comments on commit 44256e5

Please sign in to comment.