express-file-cluster 0.9.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 (41) hide show
  1. package/dist/auth/index.d.cts +1 -1
  2. package/dist/auth/index.d.ts +1 -1
  3. package/dist/{chunk-2PBMLMO4.js → chunk-QKQZF573.js} +26 -4
  4. package/dist/chunk-QKQZF573.js.map +1 -0
  5. package/dist/{chunk-6QNLN6AI.js → chunk-RRQ2RK5M.js} +353 -3
  6. package/dist/chunk-RRQ2RK5M.js.map +1 -0
  7. package/dist/{chunk-RGTENTDN.cjs → chunk-RTF7PHS2.cjs} +432 -82
  8. package/dist/chunk-RTF7PHS2.cjs.map +1 -0
  9. package/dist/{chunk-66R7S7J3.cjs → chunk-X2US4RAM.cjs} +26 -4
  10. package/dist/chunk-X2US4RAM.cjs.map +1 -0
  11. package/dist/cli/db-runner.cjs +17 -17
  12. package/dist/cli/db-runner.js +2 -2
  13. package/dist/cli/index.cjs +363 -26
  14. package/dist/cli/index.cjs.map +1 -1
  15. package/dist/cli/index.js +343 -6
  16. package/dist/cli/index.js.map +1 -1
  17. package/dist/docs/api-reference/collection.md +155 -0
  18. package/dist/docs/cli/add.md +35 -6
  19. package/dist/docs/guides/authentication.md +50 -0
  20. package/dist/docs/guides/coming-from-mongoose.md +60 -0
  21. package/dist/docs/guides/which-api.md +21 -12
  22. package/dist/docs/index.md +3 -1
  23. package/dist/docs/skills/SKILL.md +2 -2
  24. package/dist/docs/skills/api-surface.md +8 -0
  25. package/dist/docs/skills/what-not-to-invent.md +3 -2
  26. package/dist/index.cjs +11 -9
  27. package/dist/index.cjs.map +1 -1
  28. package/dist/index.d.cts +193 -3
  29. package/dist/index.d.ts +193 -3
  30. package/dist/index.js +4 -2
  31. package/dist/index.js.map +1 -1
  32. package/dist/registry/index.json +1 -1
  33. package/dist/tasks/index.d.cts +1 -1
  34. package/dist/tasks/index.d.ts +1 -1
  35. package/dist/{types-gyvA7BtO.d.cts → types-BY-ObnZH.d.cts} +36 -7
  36. package/dist/{types-gyvA7BtO.d.ts → types-BY-ObnZH.d.ts} +36 -7
  37. package/package.json +1 -1
  38. package/dist/chunk-2PBMLMO4.js.map +0 -1
  39. package/dist/chunk-66R7S7J3.cjs.map +0 -1
  40. package/dist/chunk-6QNLN6AI.js.map +0 -1
  41. package/dist/chunk-RGTENTDN.cjs.map +0 -1
@@ -18,7 +18,7 @@ import {
18
18
  tableNameFor,
19
19
  timestampFields,
20
20
  validateAtPath
21
- } from "./chunk-2PBMLMO4.js";
21
+ } from "./chunk-QKQZF573.js";
22
22
  import {
23
23
  countStatement,
24
24
  getOrm,
@@ -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);
@@ -2664,6 +2690,329 @@ function defineTable(name, schema, options) {
2664
2690
  return createTable(name, schema, options ?? {});
2665
2691
  }
2666
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
+
2667
3016
  // src/db/index.ts
2668
3017
  var _client = null;
2669
3018
  function setDbClient(client) {
@@ -2692,8 +3041,9 @@ export {
2692
3041
  defineModel,
2693
3042
  transaction,
2694
3043
  defineTable,
3044
+ defineCollection,
2695
3045
  setDbClient,
2696
3046
  getDbClient,
2697
3047
  db
2698
3048
  };
2699
- //# sourceMappingURL=chunk-6QNLN6AI.js.map
3049
+ //# sourceMappingURL=chunk-RRQ2RK5M.js.map