create-joist-app 1.0.5 → 1.0.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "create-joist-app",
3
- "version": "1.0.5",
3
+ "version": "1.0.7",
4
4
  "description": "Create a new Joist ORM project with a single command",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -1,14 +1,23 @@
1
- import { EntityManager, newPgConnectionConfig } from "joist-orm";
1
+ import type { EntityManager } from "joist-orm";
2
+ import { newPgConnectionConfig } from "joist-orm";
2
3
  import { PostgresDriver } from "joist-orm/pg";
3
4
  import { Pool } from "pg";
4
5
 
5
- export interface Context {
6
+ /** The app-wide global context, created once on boot & shared across all requests. */
7
+ export interface AppContext {
6
8
  pool: Pool;
9
+ driver: PostgresDriver;
10
+ }
11
+
12
+ /** The request-specific context, created once per request. */
13
+ export interface RequestContext extends AppContext {
7
14
  em: EntityManager;
8
15
  }
9
16
 
10
- export function newContext(): Context {
17
+ export type Context = RequestContext;
18
+
19
+ export function newAppContext(): AppContext {
11
20
  const pool = new Pool(newPgConnectionConfig());
12
- const em = new EntityManager({}, new PostgresDriver(pool));
13
- return { em, pool };
21
+ const driver = new PostgresDriver(pool);
22
+ return { pool, driver };
14
23
  }
@@ -4,11 +4,8 @@ import { newAuthor, newBook } from "./factories";
4
4
  describe("Author", () => {
5
5
  it("can create an author", async () => {
6
6
  const em = newEm();
7
- const author = newAuthor(em, { firstName: "John", lastName: "Doe" });
7
+ newAuthor(em, { firstName: "John", lastName: "Doe" });
8
8
  await em.flush();
9
-
10
- const loaded = await em.load(author.constructor, author.id);
11
- expect(loaded).toMatchEntity({ firstName: "John", lastName: "Doe" });
12
9
  });
13
10
 
14
11
  it("has a full name", async () => {
@@ -1,6 +1,6 @@
1
- import { EntityManager, FactoryOpts, newTestInstance } from "joist-orm";
1
+ import { type DeepNew, EntityManager, FactoryOpts, newTestInstance } from "joist-orm";
2
2
  import { Author } from "../Author";
3
3
 
4
- export function newAuthor(em: EntityManager, opts?: FactoryOpts<Author>): Author {
5
- return newTestInstance(em, Author, opts);
4
+ export function newAuthor(em: EntityManager, opts?: FactoryOpts<Author>): DeepNew<Author> {
5
+ return newTestInstance(em, Author, opts) as DeepNew<Author>;
6
6
  }
@@ -1,6 +1,6 @@
1
- import { EntityManager, FactoryOpts, newTestInstance } from "joist-orm";
1
+ import { type DeepNew, EntityManager, FactoryOpts, newTestInstance } from "joist-orm";
2
2
  import { Book } from "../Book";
3
3
 
4
- export function newBook(em: EntityManager, opts?: FactoryOpts<Book>): Book {
5
- return newTestInstance(em, Book, opts);
4
+ export function newBook(em: EntityManager, opts?: FactoryOpts<Book>): DeepNew<Book> {
5
+ return newTestInstance(em, Book, opts) as DeepNew<Book>;
6
6
  }
@@ -8,7 +8,7 @@ describe("authorResolvers", () => {
8
8
  // Given a Author
9
9
  const a = newAuthor(em);
10
10
  // Then we can query it
11
- const result = await runFields(ctx, a, ["firstName", "lastName", "createdAt", "updatedAt"]);
11
+ const result = await runFields(ctx, a, ["firstName", "lastName"]);
12
12
  expect(result).toMatchEntity({});
13
13
  });
14
14
  });
@@ -8,7 +8,7 @@ describe("bookResolvers", () => {
8
8
  // Given a Book
9
9
  const b = newBook(em);
10
10
  // Then we can query it
11
- const result = await runFields(ctx, b, ["title", "createdAt", "updatedAt"]);
11
+ const result = await runFields(ctx, b, ["title"]);
12
12
  expect(result).toMatchEntity({});
13
13
  });
14
14
  });
@@ -1,15 +1,9 @@
1
- import { authorResolvers } from "./authorResolvers";
2
- import { bookResolvers } from "./bookResolvers";
1
+ import { enumResolvers } from "src/resolvers/enumResolvers";
2
+ import { mutationResolvers } from "src/resolvers/mutations";
3
+ import { objectResolvers } from "src/resolvers/objects";
3
4
 
4
5
  export const resolvers = {
5
- Query: {
6
- ...authorResolvers.Query,
7
- ...bookResolvers.Query,
8
- },
9
- Mutation: {
10
- ...authorResolvers.Mutation,
11
- ...bookResolvers.Mutation,
12
- },
13
- Author: authorResolvers.Author,
14
- Book: bookResolvers.Book,
6
+ ...enumResolvers,
7
+ ...objectResolvers,
8
+ Mutation: mutationResolvers,
15
9
  };
@@ -1,13 +1,10 @@
1
1
  import { ApolloServer } from "@apollo/server";
2
2
  import { startStandaloneServer } from "@apollo/server/standalone";
3
- import { loadSchemaSync } from "@graphql-tools/load";
4
3
  import { GraphQLFileLoader } from "@graphql-tools/graphql-file-loader";
5
- import { EntityManager } from "joist-orm";
6
- import { newPgConnectionConfig } from "joist-orm/pg";
7
- import knex from "knex";
4
+ import { loadSchemaSync } from "@graphql-tools/load";
8
5
  import path from "path";
9
- import { Context } from "./context";
10
- import { entities } from "./entities";
6
+ import { Context, newAppContext } from "./context";
7
+ import { EntityManager } from "./entities";
11
8
  import { resolvers } from "./resolvers";
12
9
 
13
10
  const typeDefs = loadSchemaSync(path.join(__dirname, "./**/*.graphql"), {
@@ -15,22 +12,17 @@ const typeDefs = loadSchemaSync(path.join(__dirname, "./**/*.graphql"), {
15
12
  });
16
13
 
17
14
  async function main() {
18
- const config = newPgConnectionConfig();
19
- const db = knex({ client: "pg", connection: config });
20
-
21
- const server = new ApolloServer<Context>({
22
- typeDefs,
23
- resolvers,
24
- });
25
-
15
+ const appContext = newAppContext();
16
+ const server = new ApolloServer<Context>({ typeDefs, resolvers });
26
17
  const { url } = await startStandaloneServer(server, {
27
18
  listen: { port: parseInt(process.env.PORT || "4000") },
28
19
  context: async () => {
29
- const em = new EntityManager({ entities, driver: db }, {});
30
- return { em };
20
+ const ctx = { ...appContext, em: null as any } satisfies Context;
21
+ const em = new EntityManager(ctx, appContext.driver);
22
+ Object.assign(ctx, { em });
23
+ return ctx;
31
24
  },
32
25
  });
33
-
34
26
  console.log(`🚀 Server ready at ${url}`);
35
27
  }
36
28
 
@@ -25,7 +25,7 @@ afterAll(async () => {
25
25
 
26
26
  export function newEm(): EntityManager {
27
27
  const driver = new PostgresDriver(pool);
28
- const ctx = { pool, em: null as any } satisfies Context;
28
+ const ctx = { pool, driver, em: null as any } satisfies Context;
29
29
  const em = new EntityManager(ctx, driver);
30
30
  Object.assign(ctx, { em });
31
31
  return em;