express-file-cluster 0.8.0 → 0.9.1

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.
Files changed (58) hide show
  1. package/README.md +1 -1
  2. package/dist/auth/index.cjs +3 -3
  3. package/dist/auth/index.d.cts +1 -1
  4. package/dist/auth/index.d.ts +1 -1
  5. package/dist/auth/index.js +2 -2
  6. package/dist/{chunk-ZDM5JHVK.cjs → chunk-2J3HHQKP.cjs} +3 -3
  7. package/dist/{chunk-ZDM5JHVK.cjs.map → chunk-2J3HHQKP.cjs.map} +1 -1
  8. package/dist/{chunk-BUBXO7WU.js → chunk-HXV543HZ.js} +10 -3
  9. package/dist/chunk-HXV543HZ.js.map +1 -0
  10. package/dist/{chunk-OSES7ZQ5.js → chunk-QKQZF573.js} +27 -5
  11. package/dist/chunk-QKQZF573.js.map +1 -0
  12. package/dist/{chunk-B2I7SYQ3.js → chunk-RRQ2RK5M.js} +371 -8
  13. package/dist/chunk-RRQ2RK5M.js.map +1 -0
  14. package/dist/{chunk-TRC4DUZB.js → chunk-RSZB7VAO.js} +2 -2
  15. package/dist/{chunk-FQFKHPLR.cjs → chunk-RTF7PHS2.cjs} +473 -110
  16. package/dist/chunk-RTF7PHS2.cjs.map +1 -0
  17. package/dist/{chunk-MGH4VDV3.cjs → chunk-VQSWSYK6.cjs} +10 -3
  18. package/dist/chunk-VQSWSYK6.cjs.map +1 -0
  19. package/dist/{chunk-75O3WQKR.cjs → chunk-X2US4RAM.cjs} +30 -8
  20. package/dist/chunk-X2US4RAM.cjs.map +1 -0
  21. package/dist/cli/db-runner.cjs +19 -19
  22. package/dist/cli/db-runner.js +3 -3
  23. package/dist/cli/index.cjs +364 -27
  24. package/dist/cli/index.cjs.map +1 -1
  25. package/dist/cli/index.js +344 -7
  26. package/dist/cli/index.js.map +1 -1
  27. package/dist/docs/api-reference/collection.md +155 -0
  28. package/dist/docs/api-reference/ignite.md +19 -0
  29. package/dist/docs/api-reference/table.md +3 -0
  30. package/dist/docs/cli/add.md +36 -7
  31. package/dist/docs/guides/authentication.md +50 -0
  32. package/dist/docs/guides/coming-from-mongoose.md +60 -0
  33. package/dist/docs/guides/database.md +2 -0
  34. package/dist/docs/guides/relations.md +147 -0
  35. package/dist/docs/guides/which-api.md +59 -0
  36. package/dist/docs/index.md +5 -1
  37. package/dist/docs/skills/SKILL.md +3 -3
  38. package/dist/docs/skills/api-surface.md +9 -0
  39. package/dist/docs/skills/what-not-to-invent.md +3 -2
  40. package/dist/index.cjs +21 -19
  41. package/dist/index.cjs.map +1 -1
  42. package/dist/index.d.cts +193 -3
  43. package/dist/index.d.ts +193 -3
  44. package/dist/index.js +8 -6
  45. package/dist/index.js.map +1 -1
  46. package/dist/registry/index.json +1 -1
  47. package/dist/tasks/index.d.cts +1 -1
  48. package/dist/tasks/index.d.ts +1 -1
  49. package/dist/{types-B1tJoI4g.d.cts → types-BY-ObnZH.d.cts} +47 -7
  50. package/dist/{types-B1tJoI4g.d.ts → types-BY-ObnZH.d.ts} +47 -7
  51. package/package.json +1 -1
  52. package/dist/chunk-75O3WQKR.cjs.map +0 -1
  53. package/dist/chunk-B2I7SYQ3.js.map +0 -1
  54. package/dist/chunk-BUBXO7WU.js.map +0 -1
  55. package/dist/chunk-FQFKHPLR.cjs.map +0 -1
  56. package/dist/chunk-MGH4VDV3.cjs.map +0 -1
  57. package/dist/chunk-OSES7ZQ5.js.map +0 -1
  58. /package/dist/{chunk-TRC4DUZB.js.map → chunk-RSZB7VAO.js.map} +0 -0
@@ -18,7 +18,7 @@ import {
18
18
  tableNameFor,
19
19
  timestampFields,
20
20
  validateAtPath
21
- } from "./chunk-OSES7ZQ5.js";
21
+ } from "./chunk-QKQZF573.js";
22
22
  import {
23
23
  countStatement,
24
24
  getOrm,
@@ -26,7 +26,7 @@ import {
26
26
  traceOps,
27
27
  untracked,
28
28
  whenQueryTracking
29
- } from "./chunk-BUBXO7WU.js";
29
+ } from "./chunk-HXV543HZ.js";
30
30
  import {
31
31
  getLogger
32
32
  } from "./chunk-277BSYVH.js";
@@ -55,6 +55,29 @@ function ctor(type, mg) {
55
55
  return Date;
56
56
  case "objectId":
57
57
  return mg.Schema.Types.ObjectId;
58
+ // defineCollection only:
59
+ case "decimal128":
60
+ return mg.Schema.Types.Decimal128;
61
+ case "long":
62
+ return BigInt;
63
+ // stored as a BSON Long (int64)
64
+ case "binary":
65
+ return Buffer;
66
+ case "geoPoint":
67
+ return new mg.Schema(
68
+ {
69
+ type: { type: String, enum: ["Point"], required: true },
70
+ coordinates: {
71
+ type: [Number],
72
+ required: true,
73
+ validate: {
74
+ validator: (c) => c.length === 2 && Math.abs(c[0]) <= 180 && Math.abs(c[1]) <= 90,
75
+ message: "must be [longitude, latitude]"
76
+ }
77
+ }
78
+ },
79
+ { _id: false }
80
+ );
58
81
  }
59
82
  return mg.Schema.Types.Mixed;
60
83
  }
@@ -199,7 +222,10 @@ function mongoModel(name, tree, options) {
199
222
  ...idx.expireAfterSeconds !== void 0 && {
200
223
  expireAfterSeconds: idx.expireAfterSeconds
201
224
  },
202
- ...idx.name !== void 0 && { name: idx.name }
225
+ ...idx.name !== void 0 && { name: idx.name },
226
+ ...idx.partialFilterExpression !== void 0 && {
227
+ partialFilterExpression: idx.partialFilterExpression
228
+ }
203
229
  });
204
230
  }
205
231
  const sequences = sequenceFields(name, tree);
@@ -444,9 +470,16 @@ function mongoModel(name, tree, options) {
444
470
  }
445
471
 
446
472
  // src/db/mongo.ts
447
- async function connectMongo(url) {
473
+ async function connectMongo(url, pool = {}) {
448
474
  const mg = await loadMongoose();
449
- await mg.connect(url);
475
+ await mg.connect(url, {
476
+ ...pool.max !== void 0 && { maxPoolSize: pool.max },
477
+ ...pool.idleTimeoutMs !== void 0 && { maxIdleTimeMS: pool.idleTimeoutMs },
478
+ ...pool.connectionTimeoutMs !== void 0 && {
479
+ serverSelectionTimeoutMS: pool.connectionTimeoutMs,
480
+ waitQueueTimeoutMS: pool.connectionTimeoutMs
481
+ }
482
+ });
450
483
  getLogger().info("MongoDB connected");
451
484
  return mg.connection;
452
485
  }
@@ -1708,10 +1741,16 @@ var TableRuntime = class {
1708
1741
  };
1709
1742
  after.push(async (owner) => {
1710
1743
  if (rel.kind === "one") {
1744
+ if (ops?.["delete"]) await T.deleteMany({ where: mine(owner) });
1711
1745
  if (mode === "update" && (ops?.["create"] || ops?.["connect"] || ops?.["disconnect"])) {
1712
- await unlink(owner);
1746
+ if (!T.tbl.columns.get(fk)?.node?.def.required) await unlink(owner);
1747
+ else if ((await T.findMany({ where: mine(owner), select: { id: true }, take: 1 })).length) {
1748
+ this.fail(
1749
+ key,
1750
+ `${rel.target}.${fk} is required, so the current ${rel.target} can't be unlinked \u2014 add delete: true to replace it`
1751
+ );
1752
+ }
1713
1753
  }
1714
- if (ops?.["delete"]) await T.deleteMany({ where: mine(owner) });
1715
1754
  if (ops?.["connect"] !== void 0) await link(owner, ops["connect"]);
1716
1755
  if (ops?.["create"] !== void 0) await createChild(owner, ops["create"]);
1717
1756
  return;
@@ -2651,6 +2690,329 @@ function defineTable(name, schema, options) {
2651
2690
  return createTable(name, schema, options ?? {});
2652
2691
  }
2653
2692
 
2693
+ // src/db/collection-model.ts
2694
+ function fromBson(value) {
2695
+ if (Array.isArray(value)) return value.map(fromBson);
2696
+ if (value == null || typeof value !== "object") return value;
2697
+ switch (value._bsontype) {
2698
+ case "Decimal128":
2699
+ return String(value);
2700
+ case "Long":
2701
+ return BigInt(String(value));
2702
+ case "Binary":
2703
+ return Buffer.from(value.buffer);
2704
+ case void 0:
2705
+ break;
2706
+ default:
2707
+ return value;
2708
+ }
2709
+ if (Object.getPrototypeOf(value) !== Object.prototype) return value;
2710
+ const out = {};
2711
+ for (const [k, v] of Object.entries(value)) out[k] = fromBson(v);
2712
+ return out;
2713
+ }
2714
+ function castLongs(value, node) {
2715
+ if (value == null) return value;
2716
+ if (node.kind === "scalar") {
2717
+ return node.type === "long" && typeof value === "number" ? BigInt(value) : value;
2718
+ }
2719
+ if (node.kind === "array" && Array.isArray(value))
2720
+ return value.map((v) => castLongs(v, node.item));
2721
+ if (node.kind === "object" && typeof value === "object") {
2722
+ castTree(value, node.fields);
2723
+ }
2724
+ return value;
2725
+ }
2726
+ function castTree(doc, tree) {
2727
+ for (const [key, node] of Object.entries(tree)) {
2728
+ if (key in doc) doc[key] = castLongs(doc[key], node);
2729
+ }
2730
+ return doc;
2731
+ }
2732
+ function hasLong(tree) {
2733
+ const walk = (n) => n.kind === "scalar" ? n.type === "long" : n.kind === "array" ? walk(n.item) : n.kind === "object" ? hasLong(n.fields) : false;
2734
+ return Object.values(tree).some(walk);
2735
+ }
2736
+ function writeResult(r) {
2737
+ return {
2738
+ matchedCount: r.matchedCount ?? 0,
2739
+ modifiedCount: r.modifiedCount ?? 0,
2740
+ upsertedCount: r.upsertedCount ?? 0,
2741
+ upsertedId: r.upsertedId == null ? null : String(r.upsertedId)
2742
+ };
2743
+ }
2744
+ function mapBulkOp(op) {
2745
+ const [kind, body] = Object.entries(op)[0] ?? [];
2746
+ if (!kind || body == null || typeof body !== "object") return op;
2747
+ const b = { ...body };
2748
+ if (b["filter"]) b["filter"] = mapFilter(b["filter"]);
2749
+ return { [kind]: b };
2750
+ }
2751
+ function createCollection(name, schema, options) {
2752
+ const tree = normalizeSchema(schema, "collection", name);
2753
+ const base = mongoModel(name, tree, options);
2754
+ const longs = hasLong(tree);
2755
+ const toRecord = (doc) => {
2756
+ if (doc == null) return null;
2757
+ const rec = normalise(fromBson(doc));
2758
+ return longs ? castTree(rec, tree) : rec;
2759
+ };
2760
+ const model = async () => {
2761
+ const engine = getDbEngine();
2762
+ if (engine !== "mongodb") {
2763
+ throw new Error(
2764
+ `[EFC] ${name} is a MongoDB collection (defineCollection) but the app is connected to PostgreSQL \u2014 check DATABASE_URL / config.database, or use defineModel for a model that runs on both`
2765
+ );
2766
+ }
2767
+ return await base.raw();
2768
+ };
2769
+ const session = () => {
2770
+ const tx = currentTx();
2771
+ return tx?.engine === "mongodb" ? tx.session : null;
2772
+ };
2773
+ const sess = () => {
2774
+ const s = session();
2775
+ return s ? { session: s } : {};
2776
+ };
2777
+ const guard = async (fn) => {
2778
+ const M = await model();
2779
+ try {
2780
+ return await fn(M);
2781
+ } catch (err) {
2782
+ return translateError(name, err);
2783
+ }
2784
+ };
2785
+ const findOpts = (q2, opts) => {
2786
+ if (opts?.projection) q2.select(opts.projection);
2787
+ if (opts?.sort) {
2788
+ const meta = Object.values(opts.sort).some((v) => typeof v === "object");
2789
+ q2.sort(meta ? opts.sort : mapSort(opts.sort));
2790
+ }
2791
+ if (opts?.skip !== void 0) q2.skip(opts.skip);
2792
+ if (opts?.limit !== void 0) q2.limit(opts.limit);
2793
+ if (opts?.populate) q2.populate(opts.populate);
2794
+ const collation = opts?.collation ?? options.collation;
2795
+ if (collation) q2.collation(collation);
2796
+ q2.session(session());
2797
+ return q2;
2798
+ };
2799
+ const isBadId = (err) => {
2800
+ const e = err;
2801
+ return e?.name === "CastError" && e.path === "_id";
2802
+ };
2803
+ const model$ = {
2804
+ name,
2805
+ find: (filter, opts) => guard(async (M) => {
2806
+ const docs = await findOpts(M.find(mapFilter(filter)), opts).lean();
2807
+ return docs.map(toRecord);
2808
+ }),
2809
+ findOne: (filter, opts) => guard(async (M) => toRecord(await findOpts(M.findOne(mapFilter(filter)), opts).lean())),
2810
+ findById: (id, opts) => guard(async (M) => {
2811
+ try {
2812
+ return toRecord(await findOpts(M.findById(id), opts).lean());
2813
+ } catch (err) {
2814
+ if (isBadId(err)) return null;
2815
+ throw err;
2816
+ }
2817
+ }),
2818
+ exists: (filter) => guard(async (M) => await M.exists(mapFilter(filter)).session(session()) !== null),
2819
+ countDocuments: (filter) => guard((M) => M.countDocuments(mapFilter(filter)).session(session()).exec()),
2820
+ estimatedDocumentCount: () => guard((M) => M.estimatedDocumentCount().exec()),
2821
+ distinct: (field, filter) => guard(async (M) => {
2822
+ const values = await M.distinct(field === "id" ? "_id" : field, mapFilter(filter)).session(
2823
+ session()
2824
+ );
2825
+ return values.map(
2826
+ (v) => v != null && v._bsontype === "ObjectId" ? String(v) : fromBson(v)
2827
+ );
2828
+ }),
2829
+ create: (data) => guard(async (M) => {
2830
+ const [doc] = await M.create([data], sess());
2831
+ return toRecord(doc.toObject({ flattenMaps: true }));
2832
+ }),
2833
+ insertMany: (data) => guard(async (M) => {
2834
+ if (data.length === 0) return [];
2835
+ const docs = await M.create(data, { ...sess(), ordered: true });
2836
+ return docs.map((d) => toRecord(d.toObject({ flattenMaps: true })));
2837
+ }),
2838
+ updateOne: (filter, update, opts) => guard(
2839
+ async (M) => writeResult(
2840
+ await M.updateOne(mapFilter(filter), update, {
2841
+ runValidators: true,
2842
+ upsert: opts?.upsert ?? false,
2843
+ ...sess()
2844
+ })
2845
+ )
2846
+ ),
2847
+ updateMany: (filter, update, opts) => guard(
2848
+ async (M) => writeResult(
2849
+ await M.updateMany(mapFilter(filter), update, {
2850
+ runValidators: true,
2851
+ upsert: opts?.upsert ?? false,
2852
+ ...sess()
2853
+ })
2854
+ )
2855
+ ),
2856
+ replaceOne: (filter, replacement, opts) => guard(
2857
+ async (M) => writeResult(
2858
+ await M.replaceOne(mapFilter(filter), replacement, {
2859
+ runValidators: true,
2860
+ upsert: opts?.upsert ?? false,
2861
+ ...sess()
2862
+ })
2863
+ )
2864
+ ),
2865
+ findOneAndUpdate: (filter, update, opts) => guard(
2866
+ async (M) => toRecord(
2867
+ await M.findOneAndUpdate(mapFilter(filter), update, {
2868
+ runValidators: true,
2869
+ upsert: opts?.upsert ?? false,
2870
+ returnDocument: opts?.returnDocument ?? "before",
2871
+ ...opts?.projection && { projection: opts.projection },
2872
+ ...opts?.sort && { sort: opts.sort },
2873
+ ...sess()
2874
+ }).lean()
2875
+ )
2876
+ ),
2877
+ findByIdAndUpdate: (id, update, opts) => guard(async (M) => {
2878
+ try {
2879
+ return toRecord(
2880
+ await M.findByIdAndUpdate(id, update, {
2881
+ runValidators: true,
2882
+ returnDocument: opts?.returnDocument ?? "before",
2883
+ ...sess()
2884
+ }).lean()
2885
+ );
2886
+ } catch (err) {
2887
+ if (isBadId(err)) return null;
2888
+ throw err;
2889
+ }
2890
+ }),
2891
+ findOneAndDelete: (filter, opts) => guard(
2892
+ async (M) => toRecord(
2893
+ await M.findOneAndDelete(mapFilter(filter), {
2894
+ ...opts?.sort && { sort: opts.sort },
2895
+ ...sess()
2896
+ }).lean()
2897
+ )
2898
+ ),
2899
+ findByIdAndDelete: (id) => guard(async (M) => {
2900
+ try {
2901
+ return toRecord(await M.findByIdAndDelete(id, sess()).lean());
2902
+ } catch (err) {
2903
+ if (isBadId(err)) return null;
2904
+ throw err;
2905
+ }
2906
+ }),
2907
+ deleteOne: (filter) => guard(async (M) => ({
2908
+ deletedCount: (await M.deleteOne(mapFilter(filter), sess())).deletedCount
2909
+ })),
2910
+ deleteMany: (filter) => guard(async (M) => ({
2911
+ deletedCount: (await M.deleteMany(mapFilter(filter), sess())).deletedCount
2912
+ })),
2913
+ aggregate: (pipeline) => guard(async (M) => {
2914
+ const agg = M.aggregate(pipeline);
2915
+ const s = session();
2916
+ if (s) agg.session(s);
2917
+ if (options.collation)
2918
+ agg.collation(options.collation);
2919
+ return (await agg).map(toRecord);
2920
+ }),
2921
+ bulkWrite: (ops, opts) => guard(async (M) => {
2922
+ if (ops.length === 0) {
2923
+ return {
2924
+ insertedCount: 0,
2925
+ matchedCount: 0,
2926
+ modifiedCount: 0,
2927
+ deletedCount: 0,
2928
+ upsertedCount: 0
2929
+ };
2930
+ }
2931
+ const r = await M.bulkWrite(
2932
+ ops.map(mapBulkOp),
2933
+ {
2934
+ ordered: opts?.ordered ?? true,
2935
+ runValidators: true,
2936
+ ...sess()
2937
+ }
2938
+ );
2939
+ return {
2940
+ insertedCount: r.insertedCount,
2941
+ matchedCount: r.matchedCount,
2942
+ modifiedCount: r.modifiedCount,
2943
+ deletedCount: r.deletedCount,
2944
+ upsertedCount: r.upsertedCount
2945
+ };
2946
+ }),
2947
+ watch: (pipeline = [], opts) => {
2948
+ let stream;
2949
+ let closed = false;
2950
+ const open = async () => {
2951
+ const M = await model();
2952
+ stream ??= M.watch(pipeline, {
2953
+ ...opts?.fullDocument && { fullDocument: opts.fullDocument }
2954
+ });
2955
+ return stream;
2956
+ };
2957
+ const close = async () => {
2958
+ closed = true;
2959
+ await stream?.close();
2960
+ };
2961
+ return {
2962
+ close,
2963
+ async *[Symbol.asyncIterator]() {
2964
+ const s = await open();
2965
+ try {
2966
+ while (!closed) {
2967
+ const change = await s.next().catch((err) => {
2968
+ if (closed) return null;
2969
+ throw err;
2970
+ });
2971
+ if (!change) return;
2972
+ const key = change["documentKey"];
2973
+ yield {
2974
+ operationType: String(change["operationType"]),
2975
+ id: key?._id == null ? void 0 : String(key._id),
2976
+ ...change["fullDocument"] != null && {
2977
+ fullDocument: toRecord(change["fullDocument"])
2978
+ },
2979
+ ...change["updateDescription"] != null && {
2980
+ updateDescription: fromBson(change["updateDescription"])
2981
+ },
2982
+ raw: change
2983
+ };
2984
+ }
2985
+ } finally {
2986
+ await close();
2987
+ }
2988
+ }
2989
+ };
2990
+ },
2991
+ raw: () => model(),
2992
+ sync: (opts) => model().then(() => base.sync(opts)),
2993
+ drop: () => model().then(() => base.drop())
2994
+ };
2995
+ traceOps(
2996
+ name,
2997
+ model$,
2998
+ Object.keys(model$).filter((op) => !["name", "raw", "sync", "drop", "watch"].includes(op))
2999
+ );
3000
+ registerModel({
3001
+ name,
3002
+ sync: model$.sync,
3003
+ plan: (opts) => model().then(() => base.plan(opts)),
3004
+ drop: model$.drop,
3005
+ findByIds: (ids) => model$.find({ _id: { $in: ids } }),
3006
+ pgTarget: () => {
3007
+ throw new Error(`[EFC] ${name} is a MongoDB collection (defineCollection)`);
3008
+ }
3009
+ });
3010
+ return model$;
3011
+ }
3012
+ function defineCollection(name, schema, options) {
3013
+ return createCollection(name, schema, options ?? {});
3014
+ }
3015
+
2654
3016
  // src/db/index.ts
2655
3017
  var _client = null;
2656
3018
  function setDbClient(client) {
@@ -2679,8 +3041,9 @@ export {
2679
3041
  defineModel,
2680
3042
  transaction,
2681
3043
  defineTable,
3044
+ defineCollection,
2682
3045
  setDbClient,
2683
3046
  getDbClient,
2684
3047
  db
2685
3048
  };
2686
- //# sourceMappingURL=chunk-B2I7SYQ3.js.map
3049
+ //# sourceMappingURL=chunk-RRQ2RK5M.js.map