Skip to content

Commit

Permalink
fix delete for junction edges
Browse files Browse the repository at this point in the history
  • Loading branch information
tantaman committed Nov 3, 2022
1 parent a7dcd81 commit bcc97f3
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 18 deletions.
2 changes: 1 addition & 1 deletion examples/demo/src/domain.aphro
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ db: demo
User as Node {
1 id: ID<User>
2 email: string
}
}
4 changes: 2 additions & 2 deletions integration-tests/node/src/__tests__/count.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ beforeAll(async () => {
});

test('count', async () => {
await User.mutations.create(ctx, { name: 'Bill' }).save();
await User.create(ctx, { name: 'Bill' }).save();

let count = await User.queryAll(ctx).count().genxOnlyValue();
expect(count).toBe(1);

await commit(
ctx,
[1, 2, 3, 4].flatMap(i => User.mutations.create(ctx, { name: 'U' + i }).toChangesets()),
[1, 2, 3, 4].flatMap(i => User.create(ctx, { name: 'U' + i })),
);

count = await User.queryAll(ctx).count().genxOnlyValue();
Expand Down
4 changes: 2 additions & 2 deletions integration-tests/node/src/__tests__/ephemeral.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ test('Ephemeral nodes are not added to the cache on update', () => {

expect(cache.size).toBe(0);

appState = appState.mutations.openDeck({ openDeck: asId('b') }).save0();
appState = appState.update({ openDeckId: asId('b') }).save0();

expect(cache.size).toBe(0);
});
Expand All @@ -66,7 +66,7 @@ test('Changes to ephemeral nodes can be observed', () => {
notified = true;
});

appState.mutations.openDeck({ openDeck: asId('b') }).save();
appState.update({ openDeckId: asId('b') }).save();
jest.runAllTimers();
expect(notified).toBe(true);
});
4 changes: 2 additions & 2 deletions integration-tests/node/src/__tests__/reactivity.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ test('queryAll subscription', async () => {
disposer();

await liveResult
.latest![0].mutations.rename({
.latest![0].update({
name: 'mutated name',
})
.save();
Expand All @@ -81,7 +81,7 @@ test('reactivity via generator', async () => {
const result = await g.next().value;
expect(result.length).toBeGreaterThan(0);

await result[0].mutations.rename({ name: '---' }).save();
await result[0].update({ name: '---' }).save();

const newResult = await g.next().value;
expect(newResult.length).toBeGreaterThan(0);
Expand Down
2 changes: 1 addition & 1 deletion packages/codegen-ts/src/GenTypescriptModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ export default abstract class ${this.schema.name}Base

if (this.schema.type == 'standaloneEdge') {
ret.push(`get id(): SID_of<this> {
return (this.data.id1 + '-' + this.data.id2) as SID_of<this>;
return (this.data.id1 + '|' + this.data.id2) as SID_of<this>;
}`);
}

Expand Down
2 changes: 1 addition & 1 deletion packages/feature-gates/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@
* by disabling non essential features.
*/
export default {
NAMED_MUTATIONS: false,
NAMED_MUTATIONS: true,
};
7 changes: 6 additions & 1 deletion packages/model-runtime-ts/src/modelDataEncoding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@ export function decodeModelData<D>(data: any, spec: FieldsSpec): D {
// we _know_ that the input is never used by the caller.
encodedFields.forEach(([key, value]) => {
if (value.encoding === 'json') {
data[key] = JSON.parse(data[key]);
try {
data[key] = JSON.parse(data[key]);
} catch (e) {
console.error('Failed to parse data for key' + key, data[key]);
data[key] = undefined;
}
} else {
throw new Error(`Unsupported encoding ${value} on field ${key}`);
}
Expand Down
27 changes: 20 additions & 7 deletions packages/mutator-runtime-ts/src/sql/sqlWriter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ function buildUpsertSql<T extends {}>(nodes: IModel<T>[]) {
cols.map(c => sql.ident(c)),
', ',
)}) VALUES ${sql.join(rows, ', ')} ON CONFLICT (${sql.ident(
spec.type === 'node' ? spec.primaryKey : 'id1id2',
spec.type === 'node' ? spec.primaryKey : 'id1, id2',
)}) DO UPDATE SET ${sql.join(
cols.map(c => sql`${sql.ident(c)} = EXCLUDED.${sql.ident(c)}`),
', ',
Expand Down Expand Up @@ -69,12 +69,25 @@ export default {
const spec = first.spec;
const persist = spec.storage;

const query = sql`DELETE FROM ${sql.ident(persist.tablish)} WHERE ${sql.ident(
spec.type === 'node' ? spec.primaryKey : 'id1id2',
)} IN (${sql.join(
nodes.map(n => sql.value(n.id)),
', ',
)})`;
let query: SQLQuery | null;
if (spec.type === 'node') {
query = sql`DELETE FROM ${sql.ident(persist.tablish)} WHERE ${sql.ident(
spec.primaryKey,
)} IN (${sql.join(
nodes.map(n => sql.value(n.id)),
', ',
)})`;
} else {
query = sql.__dangerous__rawValue(
nodes
.map(n => {
const parts = n.id.split('|');
return `DELETE FROM "${persist.tablish}" WHERE "id1" = '${parts[0]}' AND "id2" = '${parts[1]}'`;
})
.join(';'),
);
console.log(nodes);
}

await (db as SQLResolvedDB).write(query);
},
Expand Down
4 changes: 3 additions & 1 deletion packages/query-runtime-ts/src/live/LiveResult.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,9 @@ export default class LiveResult<T> {
return observe<T[]>(change => {
// start the generator with an initial value
change(this.latest || []);
return this.subscribe(result => change(result));
return this.subscribe(result => {
return change(result);
});
});
}

Expand Down

0 comments on commit bcc97f3

Please sign in to comment.