Skip to content

Commit

Permalink
fixup type error on genTypescriptImpl return, unpack imports for co…
Browse files Browse the repository at this point in the history
…mplex fields
  • Loading branch information
tantaman committed Aug 19, 2022
1 parent 76fde85 commit 3233b09
Show file tree
Hide file tree
Showing 23 changed files with 113 additions and 86 deletions.
18 changes: 2 additions & 16 deletions TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,9 @@
- Absurd SQL debug extension (e.g., inspect tables)

# API Problems
- mutations... defined mutations exist as methods on the model?
- just basic `update`, `create` and `delete` to not require mutations defined on schema
- `update` / etc. can have `cs` variants that return a cs rather than immediately saving
- `cs` and `optimistic` variants so you don't have to deal with a returned tuple
- Enable sets (not just maps or arrays)
- Simplify context and bootstrapping
- Another pass at simplifying mutations
- readonly enumerations of arrays...
- Using mutations within mutations and gathering all changesets together while still allowing chaining
- Reading already queried edges
- Sync if it is fulfilled? Still async but have a query cache?
- Device ids in context?
Expand All @@ -28,13 +24,8 @@
- Sub to the whole thing I suppose.
- All model props are getters so you could technically understand what props a function against that model uses.
- Maybe we go `solidjs` first?
- Auto-build of schemas?
- Better working of partially generated files and auto-completed imports..
- Use `infer` keyword more to reduce number of generics required
- See the commit that added this line
- Access to model in mutator for updates
- Default to always applying an order by id if no ordering exists -- stable results.
- map, union, intersect, filter, etc. on QueryBase
- generated ids are not being converted to hex representations and ints on storage?
- Support edge queries?? RN we just traverse edges rather than allowing returning of edge data (if it exists).
- typename and package scoped id suffix for caching? E.g., if someone wants auto-incr (not globally unique) primary keys
Expand Down Expand Up @@ -67,13 +58,8 @@
- Ordering of properties on reads and writes (values statement write the right cols, returned rows map to right json keys)
- Migration error messages
- @databases, postgres
- Auto-create IDs for create mutations
- Mutations in mutations
- Circular reference support in mutations
- Codegen and imports ... and maybe using traits to get rid of manual sections / partially generated files
- "unable to find manual section for ..."
- Instrumentation tasks
- Allow index constraints!
- Allow docstrings for fields and node and edges!
- Test for cache population on read & write (createFrom was not doing so)
- - id of imports for mutation args
Expand Down
5 changes: 3 additions & 2 deletions examples/chinook/src/domain/generated/Album.sqlite.sql
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
-- SIGNED-SOURCE: <60fbd93fad0677addad3a8436c2882fa>
-- SIGNED-SOURCE: <fbee880c3559bd965d736d99338c6ec4>
-- STATEMENT
CREATE TABLE
"album" ("id", "title", "artistId", PRIMARY KEY ("id"))
"album" ("id", "title", "artistId", PRIMARY KEY ("id"));
5 changes: 3 additions & 2 deletions examples/chinook/src/domain/generated/Artist.sqlite.sql
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
-- SIGNED-SOURCE: <16e599a6f1358a9f51ffb025232f84b3>
-- SIGNED-SOURCE: <aab7e42b60b9ceefb3f111f3652fca88>
-- STATEMENT
CREATE TABLE
"artist" ("id", "name", PRIMARY KEY ("id"))
"artist" ("id", "name", PRIMARY KEY ("id"));
5 changes: 3 additions & 2 deletions examples/chinook/src/domain/generated/Customer.sqlite.sql
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
-- SIGNED-SOURCE: <c49c968b9ca146e3b878461a4105ac3b>
-- SIGNED-SOURCE: <ccd91ab8fe97f4a9aa8473100bd401c7>
-- STATEMENT
CREATE TABLE
"customer" (
"id",
Expand All @@ -15,4 +16,4 @@ CREATE TABLE
"email",
"supportRepId",
PRIMARY KEY ("id")
)
);
5 changes: 3 additions & 2 deletions examples/chinook/src/domain/generated/Employee.sqlite.sql
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
-- SIGNED-SOURCE: <3d927e1b406f7d1872b68f5d55ca37f0>
-- SIGNED-SOURCE: <535495a449a77496759a5e3cb925a61a>
-- STATEMENT
CREATE TABLE
"employee" (
"id",
Expand All @@ -17,4 +18,4 @@ CREATE TABLE
"fax",
"email",
PRIMARY KEY ("id")
)
);
5 changes: 3 additions & 2 deletions examples/chinook/src/domain/generated/Genre.sqlite.sql
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
-- SIGNED-SOURCE: <fe5516da3fa9d92f47d414a3cf484b80>
-- SIGNED-SOURCE: <6659dc39c2f0fd6b8ced999a310befbf>
-- STATEMENT
CREATE TABLE
"genre" ("id", "name", PRIMARY KEY ("id"))
"genre" ("id", "name", PRIMARY KEY ("id"));
8 changes: 7 additions & 1 deletion examples/chinook/src/domain/generated/GenreBase.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SIGNED-SOURCE: <3ab2103daa82e72019f061ab53eccc09>
// SIGNED-SOURCE: <f516bbf163594ea60f33e79aa65bbec4>
/**
* AUTO-GENERATED FILE
* Do not modify. Update your schema and re-generate for changes.
Expand All @@ -19,8 +19,10 @@ import { Context } from "@aphro/runtime-ts";
import TrackQuery from "./TrackQuery.js";
import Track from "../Track.js";
import GenreMutations from "./GenreMutations.js";
import { InstancedMutations } from "./GenreMutations.js";

declare type Muts = typeof GenreMutations;
declare type IMuts = InstancedMutations;

export type Data = {
id: SID_of<Genre>;
Expand All @@ -35,6 +37,10 @@ export default abstract class GenreBase extends Node<Data> {
return GenreMutations;
}

get mutations(): IMuts {
return new InstancedMutations(this as any);
}

get id(): SID_of<this> {
return this.data.id as unknown as SID_of<this>;
}
Expand Down
25 changes: 16 additions & 9 deletions examples/chinook/src/domain/generated/GenreMutations.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SIGNED-SOURCE: <134bfff0df3204f7f03f17ae3e7037a4>
// SIGNED-SOURCE: <6e0dfbf4ea2be93c6e076f35814d269f>
/**
* AUTO-GENERATED FILE
* Do not modify. Update your schema and re-generate for changes.
Expand All @@ -7,7 +7,7 @@ import * as impls from "../GenreMutationsImpl.js";
import { ICreateOrUpdateBuilder } from "@aphro/runtime-ts";
import { Context } from "@aphro/runtime-ts";
import { MutationsBase } from "@aphro/runtime-ts";
import type Genre from "../Genre.js";
import Genre from "../Genre.js";
import { default as spec } from "./GenreSpec.js";
import { Data } from "./GenreBase.js";
import { UpdateMutationBuilder } from "@aphro/runtime-ts";
Expand Down Expand Up @@ -41,17 +41,24 @@ class Mutations extends MutationsBase<Genre, Data> {
}
}

export default {
const staticMutations = {
create(ctx: Context, args: CreateArgs): Mutations {
return new Mutations(ctx, new CreateMutationBuilder(ctx, spec)).create(
args
);
},
rename(model: Genre, args: RenameArgs): Mutations {
};

export default staticMutations;

export class InstancedMutations {
constructor(private model: Genre) {}

rename(args: RenameArgs): Mutations {
return new Mutations(
model.ctx,
new UpdateMutationBuilder(model.ctx, spec, model),
model
this.model.ctx,
new UpdateMutationBuilder(this.model.ctx, spec, this.model),
this.model
).rename(args);
},
};
}
}
5 changes: 3 additions & 2 deletions examples/chinook/src/domain/generated/Invoice.sqlite.sql
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
-- SIGNED-SOURCE: <65d968502500c818963a29568a300105>
-- SIGNED-SOURCE: <c6cef64d4e54ec0e0bf1f3da02481b91>
-- STATEMENT
CREATE TABLE
"invoice" (
"id",
Expand All @@ -11,4 +12,4 @@ CREATE TABLE
"billingPostalCode",
"total",
PRIMARY KEY ("id")
)
);
5 changes: 3 additions & 2 deletions examples/chinook/src/domain/generated/InvoiceLine.sqlite.sql
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
-- SIGNED-SOURCE: <51d087c1f4d8970aa0d8c7d860db2884>
-- SIGNED-SOURCE: <02482512fc99368115f9f924b4eba544>
-- STATEMENT
CREATE TABLE
"invoiceline" (
"id",
Expand All @@ -7,4 +8,4 @@ CREATE TABLE
"unitPrice",
"quantity",
PRIMARY KEY ("id")
)
);
5 changes: 3 additions & 2 deletions examples/chinook/src/domain/generated/MediaType.sqlite.sql
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
-- SIGNED-SOURCE: <af91931d4051a1f5616662538f497d4d>
-- SIGNED-SOURCE: <64370081725ed7c957d4f20cd1be4dfa>
-- STATEMENT
CREATE TABLE
"mediatype" ("id", "name", PRIMARY KEY ("id"))
"mediatype" ("id", "name", PRIMARY KEY ("id"));
5 changes: 3 additions & 2 deletions examples/chinook/src/domain/generated/Playlist.sqlite.sql
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
-- SIGNED-SOURCE: <e8f99ad6f852290ea4e6cb0674251066>
-- SIGNED-SOURCE: <7613eb8a38668e66459092953ed5448e>
-- STATEMENT
CREATE TABLE
"playlist" ("id", "name", PRIMARY KEY ("id"))
"playlist" ("id", "name", PRIMARY KEY ("id"));
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
-- SIGNED-SOURCE: <e317f99d0f185c1ff00585127a1f2f66>
-- SIGNED-SOURCE: <f9ce94b73f711a60ce7e5780e4ce9c1e>
-- STATEMENT
CREATE TABLE
"playlisttrack" ("id1", "id2")
"playlisttrack" ("id1", "id2");
5 changes: 3 additions & 2 deletions examples/chinook/src/domain/generated/Track.sqlite.sql
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
-- SIGNED-SOURCE: <ff878f51149877fa4be507fb7a20276e>
-- SIGNED-SOURCE: <56a5fdb7cb3182a7603af79842cf5563>
-- STATEMENT
CREATE TABLE
"track" (
"id",
Expand All @@ -11,4 +12,4 @@ CREATE TABLE
"bytes",
"unitPrice",
PRIMARY KEY ("id")
)
);
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ ${this.getCode([])}
m.verb !== 'create' ? `model: ${this.schema.name}, ` : ''
}mutator: Omit<IMutationBuilder<${
this.schema.name
}, Data>, 'toChangeset'>, ${destructured}: ${casedName}Args): void | Changeset<any>[] {
}, Data>, 'toChangeset'>, ${destructured}: ${casedName}Args): void | Changeset<any, any>[] {
// Use the provided mutator to make your desired changes.
// e.g., mutator.set({name: "Foo" });
// You do not need to return anything from this method. The mutator will track your changes.
Expand Down
57 changes: 34 additions & 23 deletions packages/codegen-ts/src/GenTypescriptModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ import {
ID,
Import,
SchemaNode,
RemoveNameField,
Field,
FieldDeclaration,
TypeAtom,
} from '@aphro/schema-api';
import { nodeFn, edgeFn, tsImport, fieldFn } from '@aphro/schema';
import * as path from 'path';
Expand Down Expand Up @@ -175,7 +179,7 @@ export default abstract class ${this.schema.name}Base
tsImport('{Context}', null, '@aphro/runtime-ts'),
...(this.schema.type === 'node' ? this.schema.extensions.module?.imports.values() || [] : []),
...this.getEdgeImports(),
...this.getIdFieldImports(),
// ...this.getIdFieldImports(),
...this.getNestedTypeImports(),
...this.getMutationsImports(),
].filter(i => i.name !== this.schema.name + 'Base');
Expand All @@ -192,35 +196,42 @@ export default abstract class ${this.schema.name}Base
];
}

private getIdFieldImports(): Import[] {
const idFields = Object.values(this.schema.fields)
.flatMap(f => f.type)
.filter((f): f is ID => typeof f !== 'string' && f.type === 'id' && f.of !== 'any');

return idFields.map(f => tsImport(f.of, null, '../' + f.of + '.js'));
}

private getNestedTypeImports(): Import[] {
const typeFields = Object.values(this.schema.fields)
.flatMap(f =>
f.type.map(t => {
if (typeof t === 'string') {
return t;
}

// TODO: technically we need to recurse into array and map structures to pull imports
if (t.type === 'array' || t.type === 'map') {
if (typeof t.values === 'string') {
return t.values;
}
}
}),
)
.flatMap(f => this.getFieldTypeImports(f.type))
.filter((f): f is string => f != null);

return typeFields.map(f => tsImport(f, null, '../' + f + '.js'));
}

private getFieldTypeImports = (type: TypeAtom[]): (string | undefined)[] => {
return type.flatMap(t => {
if (typeof t === 'string') {
return t;
}

if (t.type === 'intersection' || t.type === 'union') {
return;
}

return this.getFieldImports(t);
});
};

private getFieldImports = (field: RemoveNameField<Field>): string | undefined => {
if (field.type === 'array' || field.type === 'map') {
if (typeof field.values === 'string') {
return field.values;
} else {
return this.getFieldImports(field.values);
}
}

if (field.type === 'id' && field.of != 'any') {
return field.of;
}
};

private getEdgeImports(): Import[] {
if (this.schema.type === 'standaloneEdge') {
return [];
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SIGNED-SOURCE: <8d0ee6ccbb98a66399de7ed8fca50878>
// SIGNED-SOURCE: <2521c499931acb9b7393ff94504d228c>
/**
* AUTO-GENERATED FILE
* Do not modify. Update your schema and re-generate for changes.
Expand All @@ -15,8 +15,8 @@ import { Node } from "@aphro/runtime-ts";
import { NodeSpecWithCreate } from "@aphro/runtime-ts";
import { SID_of } from "@aphro/runtime-ts";
import { Context } from "@aphro/runtime-ts";
import Deck from "../Deck.js";
import Identity from "../Identity.js";
import Deck from "../Deck.js";
import Component from "../Component.js";
import AppStateMutations from "./AppStateMutations.js";
import { InstancedMutations } from "./AppStateMutations.js";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
-- SIGNED-SOURCE: <27a6ff0588d4a73fa4b29f8d85a86796>
-- SIGNED-SOURCE: <449399a4163603a3f090d30400cdde91>
-- STATEMENT
CREATE TABLE
"component" (
"id"
Expand All @@ -14,4 +15,4 @@ CREATE TABLE
/* n=4 */
,
PRIMARY KEY ("id")
)
);
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
-- SIGNED-SOURCE: <d35e9509aaccaea4b5de42513ab35b5b>
-- SIGNED-SOURCE: <f95eb040bf46ca1ff546cec50c107860>
-- STATEMENT
CREATE TABLE
"deck" (
"id"
Expand All @@ -20,4 +21,4 @@ CREATE TABLE
/* n=6 */
,
PRIMARY KEY ("id")
)
);
Loading

0 comments on commit 3233b09

Please sign in to comment.