@m1212e/rumble 0.12.10 → 0.12.13

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/out/index.cjs CHANGED
@@ -22,35 +22,21 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
22
22
 
23
23
  //#endregion
24
24
  let node_fs_promises = require("node:fs/promises");
25
- node_fs_promises = __toESM(node_fs_promises);
26
25
  let node_path = require("node:path");
27
- node_path = __toESM(node_path);
28
26
  let graphql = require("graphql");
29
- graphql = __toESM(graphql);
30
27
  let es_toolkit = require("es-toolkit");
31
- es_toolkit = __toESM(es_toolkit);
32
28
  let wonka = require("wonka");
33
- wonka = __toESM(wonka);
34
29
  let __escape_tech_graphql_armor = require("@escape.tech/graphql-armor");
35
- __escape_tech_graphql_armor = __toESM(__escape_tech_graphql_armor);
36
30
  let __graphql_yoga_plugin_disable_introspection = require("@graphql-yoga/plugin-disable-introspection");
37
- __graphql_yoga_plugin_disable_introspection = __toESM(__graphql_yoga_plugin_disable_introspection);
38
31
  let graphql_yoga = require("graphql-yoga");
39
- graphql_yoga = __toESM(graphql_yoga);
40
32
  let sofa_api = require("sofa-api");
41
- sofa_api = __toESM(sofa_api);
42
33
  let drizzle_orm = require("drizzle-orm");
43
- drizzle_orm = __toESM(drizzle_orm);
44
34
  let drizzle_orm_casing = require("drizzle-orm/casing");
45
- drizzle_orm_casing = __toESM(drizzle_orm_casing);
46
35
  let drizzle_orm_pg_core = require("drizzle-orm/pg-core");
47
- drizzle_orm_pg_core = __toESM(drizzle_orm_pg_core);
48
36
  let pluralize = require("pluralize");
49
37
  pluralize = __toESM(pluralize);
50
38
  let drizzle_orm_mysql_core = require("drizzle-orm/mysql-core");
51
- drizzle_orm_mysql_core = __toESM(drizzle_orm_mysql_core);
52
39
  let drizzle_orm_sqlite_core = require("drizzle-orm/sqlite-core");
53
- drizzle_orm_sqlite_core = __toESM(drizzle_orm_sqlite_core);
54
40
  let __pothos_core = require("@pothos/core");
55
41
  __pothos_core = __toESM(__pothos_core);
56
42
  let __pothos_plugin_drizzle = require("@pothos/plugin-drizzle");
@@ -58,7 +44,6 @@ __pothos_plugin_drizzle = __toESM(__pothos_plugin_drizzle);
58
44
  let __pothos_plugin_smart_subscriptions = require("@pothos/plugin-smart-subscriptions");
59
45
  __pothos_plugin_smart_subscriptions = __toESM(__pothos_plugin_smart_subscriptions);
60
46
  let graphql_scalars = require("graphql-scalars");
61
- graphql_scalars = __toESM(graphql_scalars);
62
47
 
63
48
  //#region lib/client/generate/client.ts
64
49
  function generateClient({ apiUrl, rumbleImportPath, useExternalUrqlClient, availableSubscriptions }) {
@@ -499,8 +484,65 @@ const assertFirstEntryExists = (value) => {
499
484
 
500
485
  //#endregion
501
486
  //#region lib/helpers/mapNullFieldsToUndefined.ts
487
+ /**
488
+ * Helper to map null fields to undefined
489
+ * @param obj The object to map
490
+ * @returns The mapped object with all fields of 'null' transformed to 'undefined'
491
+ *
492
+ * This becomes useful for update mutations where you do not want to pass null (unset value in db)
493
+ * but undefined (do not touch value in db) in case of a value not beeing set in the args of said mutation
494
+ * @example
495
+ * ```ts
496
+ * updateUser: t.drizzleField({
497
+ type: User,
498
+ args: {
499
+ id: t.arg.string({ required: true }),
500
+ email: t.arg.string(),
501
+ lastName: t.arg.string(),
502
+ firstName: t.arg.string(),
503
+ },
504
+ resolve: async (query, root, args, ctx, info) => {
505
+ const mappedArgs = mapNullFieldsToUndefined(args);
506
+ const user = await db.transaction(async (tx) => {
507
+ const user = await tx
508
+ .update(schema.user)
509
+ .set({
510
+
511
+ // email: args.email ?? undefined,
512
+ // lastName: args.lastName ?? undefined,
513
+ // firstName: args.firstName ?? undefined,
514
+
515
+ // becomes this
516
+
517
+ email: mappedArgs.email,
518
+ lastName: mappedArgs.lastName,
519
+ firstName: mappedArgs.firstName,
520
+ })
521
+ .returning()
522
+ .then(assertFirstEntryExists);
523
+ return user;
524
+ });
525
+
526
+ pubsub.updated(user.id);
527
+
528
+ return db.query.user
529
+ .findFirst(
530
+ query(
531
+ ctx.abilities.user.filter('read').merge(
532
+ {
533
+ where: { id: user.id },
534
+ }
535
+ 1).query.single,
536
+ ),
537
+ )
538
+ .then(assertFindFirstExists);
539
+ },
540
+ }),
541
+ *
542
+ *
543
+ * ```
544
+ */
502
545
  function mapNullFieldsToUndefined(obj) {
503
- if (!obj) return;
504
546
  return Object.fromEntries(Object.entries(obj).map(([key, value]) => [key, value === null ? void 0 : value]));
505
547
  }
506
548
 
@@ -659,6 +701,8 @@ function tableHelper({ db, table }) {
659
701
  if (!foundRelation) throw new RumbleError(`Could not find schema for ${JSON.stringify(table)}`);
660
702
  const foundSchema = Object.values(db._.schema).find((schema) => schema.dbName === foundRelation.table[drizzleOriginalNameSymbol]);
661
703
  if (!foundSchema) throw new RumbleError(`Could not find schema for ${JSON.stringify(table)}`);
704
+ const fullSchema = db._.fullSchema?.[foundSchema.tsName];
705
+ if (!fullSchema) throw new RumbleError(`Could not find full schema for ${JSON.stringify(table)}`);
662
706
  return {
663
707
  columns: foundSchema.columns,
664
708
  primaryKey: foundSchema.primaryKey,
@@ -666,7 +710,8 @@ function tableHelper({ db, table }) {
666
710
  dbName: foundSchema.dbName,
667
711
  tsName: foundSchema.tsName,
668
712
  foundSchema,
669
- foundRelation
713
+ foundRelation,
714
+ fullSchema
670
715
  };
671
716
  }
672
717
 
@@ -1021,6 +1066,7 @@ const createWhereArgImplementer = ({ db, schemaBuilder, enumImplementer }) => {
1021
1066
  const clientCreatorImplementer = ({ builtSchema }) => {
1022
1067
  if (process.env.NODE_ENV !== "development") console.warn("Running rumble client generation in non development mode. Are you sure this is correct?");
1023
1068
  const clientCreator = async ({ apiUrl, outputPath, rumbleImportPath, useExternalUrqlClient }) => {
1069
+ if (process.env.NODE_ENV !== "development") console.warn("Running rumble client generation in non development mode. Are you sure this is correct?");
1024
1070
  await generateFromSchema({
1025
1071
  schema: builtSchema(),
1026
1072
  outputPath,
@@ -1045,6 +1091,46 @@ const createContextFunction = ({ context: makeUserContext, abilityBuilder }) =>
1045
1091
  };
1046
1092
  };
1047
1093
 
1094
+ //#endregion
1095
+ //#region lib/countQuery.ts
1096
+ const createCountQueryImplementer = ({ db, schemaBuilder, whereArgImplementer, makePubSubInstance }) => {
1097
+ return ({ table, listAction = "read", isAllowed }) => {
1098
+ const WhereArg = whereArgImplementer({ table });
1099
+ const { registerOnInstance } = makePubSubInstance({ table });
1100
+ const tableSchema = tableHelper({
1101
+ db,
1102
+ table
1103
+ });
1104
+ return schemaBuilder.queryFields((t) => {
1105
+ return { [`${pluralize.default.plural(table.toString())}Count`]: t.field({
1106
+ type: "Int",
1107
+ nullable: false,
1108
+ smartSubscription: true,
1109
+ description: `Count all ${pluralize.default.plural(table.toString())}`,
1110
+ subscribe: (subscriptions, _root, _args, _ctx, _info) => {
1111
+ registerOnInstance({
1112
+ instance: subscriptions,
1113
+ action: "created"
1114
+ });
1115
+ registerOnInstance({
1116
+ instance: subscriptions,
1117
+ action: "removed"
1118
+ });
1119
+ },
1120
+ args: { where: t.arg({
1121
+ type: WhereArg,
1122
+ required: false
1123
+ }) },
1124
+ resolve: async (root, args, ctx, info) => {
1125
+ if (isAllowed && !await isAllowed(ctx)) throw new RumbleErrorSafe("Not allowed to perform this action");
1126
+ Object.setPrototypeOf(args, Object.prototype);
1127
+ return db.select({ count: (0, drizzle_orm.count)() }).from(tableSchema.fullSchema).where(ctx.abilities[table].filter(listAction).merge(mapNullFieldsToUndefined(args)).sql.where).then(assertFirstEntryExists).then((r) => r.count);
1128
+ }
1129
+ }) };
1130
+ });
1131
+ };
1132
+ };
1133
+
1048
1134
  //#endregion
1049
1135
  //#region lib/helpers/sofaOpenAPIWebhookDocs.ts
1050
1136
  const sofaOpenAPIWebhookDocs = {
@@ -1792,6 +1878,12 @@ export const db = drizzle(
1792
1878
  orderArgImplementer: orderArg,
1793
1879
  makePubSubInstance
1794
1880
  });
1881
+ const countQuery = createCountQueryImplementer({
1882
+ ...rumbleInput,
1883
+ schemaBuilder,
1884
+ whereArgImplementer: whereArg,
1885
+ makePubSubInstance
1886
+ });
1795
1887
  const builtSchema = lazy(() => schemaBuilder.toSchema());
1796
1888
  const createYoga = (args) => {
1797
1889
  const enableApiDocs = args?.enableApiDocs ?? process?.env?.NODE_ENV === "development";
@@ -1825,7 +1917,8 @@ export const db = drizzle(
1825
1917
  clientCreator: clientCreatorImplementer({
1826
1918
  ...rumbleInput,
1827
1919
  builtSchema
1828
- })
1920
+ }),
1921
+ countQuery
1829
1922
  };
1830
1923
  };
1831
1924