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
@@ -0,0 +1,155 @@
1
+ # defineCollection — API Reference
2
+
3
+ `defineCollection` is the MongoDB-native model API. It uses mongoose's vocabulary (`find` with projections, `findOneAndUpdate`, `bulkWrite`, aggregation pipelines, change streams) and adds BSON types. It runs on the same foundation as `defineModel`: schema format, inferred types, validation, auto-sync, `transaction()`, `efc db generate`. Underneath, it is a mongoose model.
4
+
5
+ Building on PostgreSQL? Use [`defineTable`](./table.md). Need one model for both engines? Use [`defineModel`](./db.md). See [Which model API?](../guides/which-api.md).
6
+
7
+ ```ts
8
+ import { defineCollection, type InferRecord } from 'express-file-cluster';
9
+
10
+ export const Place = defineCollection('Place', {
11
+ name: { type: 'string', required: true },
12
+ location: { type: 'geoPoint', index: '2dsphere' },
13
+ price: { type: 'decimal128' }, // '19.99', exact
14
+ visits: { type: 'long', default: 0 }, // bigint
15
+ tags: { type: 'array', of: 'string', default: [] },
16
+ owner: { type: 'objectId', ref: 'User' },
17
+ reviews: [{ rating: { type: 'number', required: true, min: 1, max: 5 }, text: { type: 'string' } }],
18
+ }, { indexes: [{ fields: { name: 'text' } }] });
19
+
20
+ export type Place = InferRecord<typeof Place>;
21
+
22
+ const near = await Place.find(
23
+ { location: { $near: { $geometry: { type: 'Point', coordinates: [85.32, 27.71] }, $maxDistance: 2000 } } },
24
+ { projection: { name: 1 }, limit: 10 },
25
+ );
26
+ await Place.findOneAndUpdate({ name }, { $inc: { visits: 1 } }, { upsert: true, returnDocument: 'after' });
27
+ ```
28
+
29
+ Using a `defineCollection` model on a PostgreSQL connection fails at its first query, with an `[EFC]` error naming the model.
30
+
31
+ ---
32
+
33
+ ## Field types
34
+
35
+ Every `defineModel` field type and option works (`string`, `number`, `boolean`, `date`, `objectId`, embedded objects and arrays, `required`, `default`, `enum`, `min`/`max`, `match`, `validate`, `ref`, `sequence`, `renamedFrom`). On top of them:
36
+
37
+ | Type | Stored as | In records |
38
+ |---|---|---|
39
+ | `decimal128` | `Decimal128` | `string` (exact, `'19.99'`); writes take strings or numbers |
40
+ | `long` | `Long` (int64) | `bigint`, whatever the size; writes take bigints or numbers |
41
+ | `geoPoint` | GeoJSON `{ type: 'Point', coordinates: [lng, lat] }` | `GeoPoint`; longitude first, both checked for range |
42
+ | `binary` | `Binary` | `Buffer` |
43
+
44
+ The PostgreSQL types (`integer`, `bigint`, `decimal`, `uuid`, `json`) and `defineTable` options are refused at definition, with an `[EFC]` error naming the field.
45
+
46
+ `index` takes `true`, or a special index: `'2dsphere'` (geo), `'text'` or `'hashed'`. In `options.indexes`, field values can also be `'2dsphere'` and `'hashed'`, and an index can be partial:
47
+
48
+ ```ts
49
+ { indexes: [
50
+ { fields: { name: 'text', description: 'text' } },
51
+ { fields: { email: 1 }, unique: true, partialFilterExpression: { deletedAt: { $exists: false } } },
52
+ { fields: { expiresAt: 1 }, expireAfterSeconds: 0 }, // TTL
53
+ ] }
54
+ ```
55
+
56
+ ## Options
57
+
58
+ `ModelOptions` (`timestamps`, `indexes`, `collection`) plus:
59
+
60
+ | Option | |
61
+ |---|---|
62
+ | `collation` | default collation for queries and aggregations, e.g. `{ locale: 'en', strength: 2 }` (case-insensitive) |
63
+
64
+ ---
65
+
66
+ ## Reading
67
+
68
+ ```ts
69
+ find(filter?, options?): Promise<Record[]>
70
+ findOne(filter?, options?): Promise<Record | null>
71
+ findById(id, options?): Promise<Record | null> // malformed id → null
72
+ exists(filter): Promise<boolean>
73
+ countDocuments(filter?): Promise<number>
74
+ estimatedDocumentCount(): Promise<number> // from metadata: fast, no filter
75
+ distinct(field, filter?): Promise<value[]> // arrays are unwound, as in MongoDB
76
+ ```
77
+
78
+ | `options` | |
79
+ |---|---|
80
+ | `projection` | `{ name: 1 }` (only these, plus `id`) or `{ secret: 0 }` (all but these) |
81
+ | `sort` | `{ createdAt: -1, name: 'asc' }`, or `{ score: { $meta: 'textScore' } }` |
82
+ | `skip`, `limit` | |
83
+ | `populate` | `ref` fields to replace with the referenced documents |
84
+ | `collation` | overrides the model's |
85
+
86
+ Filters are MongoDB query documents: every query operator works (`$near`, `$geoWithin`, `$text`, `$elemMatch`, `$regex`, `$expr`, …), as do dotted paths. `id` is accepted for `_id`. Field values are type-checked against the record; dotted paths and operators are not.
87
+
88
+ ## Writing
89
+
90
+ ```ts
91
+ create(data): Promise<Record>
92
+ insertMany(data[]): Promise<Record[]>
93
+ updateOne(filter, update, { upsert? }): Promise<WriteResult>
94
+ updateMany(filter, update, { upsert? }): Promise<WriteResult>
95
+ replaceOne(filter, replacement, { upsert? }): Promise<WriteResult>
96
+ findOneAndUpdate(filter, update, { upsert?, returnDocument?, projection?, sort? }): Promise<Record | null>
97
+ findByIdAndUpdate(id, update, { returnDocument? }): Promise<Record | null>
98
+ findOneAndDelete(filter, { sort? }): Promise<Record | null>
99
+ findByIdAndDelete(id): Promise<Record | null>
100
+ deleteOne(filter): Promise<{ deletedCount }>
101
+ deleteMany(filter?): Promise<{ deletedCount }>
102
+ bulkWrite(ops, { ordered? }): Promise<BulkResult>
103
+ ```
104
+
105
+ - `WriteResult` is `{ matchedCount, modifiedCount, upsertedCount, upsertedId }`.
106
+ - As in mongoose, `findOneAndUpdate` returns the document **before** the update unless you pass `returnDocument: 'after'`.
107
+ - Updates take plain fields (shorthand for `$set`) or any update operator (`$set $unset $inc $mul $min $max $push $pull $addToSet $pop $rename $setOnInsert $currentDate`).
108
+ - **Writes are validated**: creates against the whole schema, updates on the fields they change (`runValidators`). Failures throw `ModelValidationError` (400 with `issues`). A duplicate on a unique index is an `HttpError` 409.
109
+ - `bulkWrite` takes `insertOne`, `updateOne`, `updateMany`, `replaceOne`, `deleteOne` and `deleteMany` operations, and returns `{ insertedCount, matchedCount, modifiedCount, deletedCount, upsertedCount }`.
110
+
111
+ ```ts
112
+ await Place.bulkWrite([
113
+ { insertOne: { document: { name: 'Patan' } } },
114
+ { updateOne: { filter: { name: 'Boudha' }, update: { $addToSet: { tags: 'stupa' } }, upsert: true } },
115
+ { deleteMany: { filter: { visits: { $lt: 1 } } } },
116
+ ]);
117
+ ```
118
+
119
+ ## Aggregation
120
+
121
+ ```ts
122
+ const ratings = await Place.aggregate<{ _id: string; avg: number }>([
123
+ { $unwind: '$reviews' },
124
+ { $group: { _id: '$name', avg: { $avg: '$reviews.rating' } } },
125
+ { $sort: { avg: -1 } },
126
+ ]);
127
+ ```
128
+
129
+ The pipeline is passed to MongoDB unchanged; type the output with the generic. Results get the same conversions as records: decimals become strings, longs bigints, `_id` gains a string `id`. `$lookup` joins other collections by collection name.
130
+
131
+ ## Change streams
132
+
133
+ ```ts
134
+ const stream = Place.watch([{ $match: { operationType: 'insert' } }]);
135
+ for await (const change of stream) {
136
+ console.log(change.operationType, change.id, change.fullDocument?.name);
137
+ if (done) break; // or: await stream.close()
138
+ }
139
+ ```
140
+
141
+ Each change has `operationType`, `id` (the document's), `fullDocument` (inserts and replaces; updates with `{ fullDocument: 'updateLookup' }`), `updateDescription` and `raw` (the driver's event). Change streams need a replica set.
142
+
143
+ ## Transactions
144
+
145
+ Calls inside `transaction(async () => { … })` join it, as with `defineModel`. MongoDB transactions need a replica set.
146
+
147
+ ## Sync and migrations
148
+
149
+ The same as `defineModel` on MongoDB. On first use the collection and its indexes are created (`autoSync`). `efc db sync [--dry-run]` reconciles indexes, and `efc db generate` writes migrations for index changes and `renamedFrom` fields.
150
+
151
+ ## Also
152
+
153
+ - `raw()`: the mongoose `Model`, for anything not covered here.
154
+ - `sync({ dryRun? })`, `drop()`.
155
+ - Types: `InferRecord<typeof Place>`, `InferCreate<typeof Place>`, `CollectionFilter<R>`, `CollectionUpdate<R>`.
@@ -31,6 +31,7 @@ interface EFCConfig {
31
31
  // Database
32
32
  database?: 'mongodb' | 'postgresql';
33
33
  databaseUrl?: string;
34
+ databasePool?: { max?: number; idleTimeoutMs?: number; connectionTimeoutMs?: number };
34
35
 
35
36
  // Auth
36
37
  authStrategy?: 'http-only' | 'localStorage';
@@ -150,6 +151,24 @@ Connection string for the database. `ignite()` never reads `DATABASE_URL` from t
150
151
 
151
152
  ---
152
153
 
154
+ ### `databasePool`
155
+
156
+ Connection pool settings. Each worker process has its own pool, so the database sees up to `max × workers` connections. Keep that under the server's limit (PostgreSQL's `max_connections` defaults to 100).
157
+
158
+ ```ts
159
+ databasePool: { max: 5, idleTimeoutMs: 30_000, connectionTimeoutMs: 5_000 },
160
+ ```
161
+
162
+ | Option | PostgreSQL (`pg` Pool) | MongoDB (mongoose) |
163
+ |---|---|---|
164
+ | `max` | `max` — default 10 | `maxPoolSize` — default 100 |
165
+ | `idleTimeoutMs` | `idleTimeoutMillis` — default 10000 | `maxIdleTimeMS` — default: never |
166
+ | `connectionTimeoutMs` | `connectionTimeoutMillis` — default: no limit | `serverSelectionTimeoutMS` (default 30000) and `waitQueueTimeoutMS` (default: no limit) |
167
+
168
+ Unset options keep the driver's default. `efc db` commands use the defaults.
169
+
170
+ ---
171
+
153
172
  ### `authStrategy`
154
173
 
155
174
  How JWTs are delivered to clients:
@@ -403,6 +403,9 @@ await User.update({
403
403
  - `connect`, `disconnect`, `set` and `delete` take unique wheres (or ids); a list or a single one.
404
404
  A `connect` that matches nothing is a **400** on the field.
405
405
  - Children created through a back-relation get their foreign key from the parent — leave it out.
406
+ - On `update`, a `hasOne` `create` / `connect` / `disconnect` first unlinks the current child (its
407
+ foreign key is set to `null`). If that key is `required` and a child exists, it's a **400**:
408
+ add `delete: true` (`profile: { delete: true, create: { … } }`) to replace it.
406
409
  - The whole call runs in **one transaction**: anything failing rolls it all back.
407
410
  - `createMany` / `updateMany` write rows only; they refuse nested writes, as in Prisma.
408
411
  - `upsert` with nested writes reads first, then creates or updates, in the transaction (without
@@ -34,6 +34,7 @@ efc diff # compare installed features with the regi
34
34
  | `--overwrite` | Replace files you've changed with the registry version |
35
35
  | `--no-install` | Don't run `npm install` / `pnpm install` / … afterwards |
36
36
  | `--no-prompt` | Don't ask the items' questions — write placeholders to `.env` instead |
37
+ | `--entity <name>` | Scaffold a custom authenticatable entity (model + namespaced auth routes) |
37
38
 
38
39
  ---
39
40
 
@@ -66,13 +67,41 @@ efc diff # compare installed features with the regi
66
67
 
67
68
  The full route list is in [Generated Portals](../guides/generated-portals.md); OAuth setup is in [Sign in with Google & Apple](../guides/oauth.md).
68
69
 
69
- A few things hold for every item:
70
+ ---
71
+
72
+ ## Custom auth entities
73
+
74
+ While `efc add auth` scaffolds the standard **User** and **Admin** principals, real-world apps (marketplaces, food delivery, logistics) often need custom entities like `Merchant`, `Driver`, `Seller`, or `DeliveryPartner`.
75
+
76
+ EFC provides a first-class shortcut to scaffold an authenticatable entity with its own model and namespaced routes:
77
+
78
+ ```bash
79
+ # Any of these syntaxes work:
80
+ efc add auth --entity merchant
81
+ efc add auth:merchant
82
+ efc add auth new merchant
83
+
84
+ # Multiple entities at once:
85
+ efc add auth:merchant auth:delivery-partner
86
+ ```
87
+
88
+ ### What gets generated
89
+
90
+ For `efc add auth:merchant`:
91
+
92
+ | File | Purpose |
93
+ |---|---|
94
+ | `src/model/Merchant.ts` | Model with `name`, `email`, `password`, `role: 'merchant'`, `isActive`, tokens |
95
+ | `src/api/auth/merchant/register.ts` | `POST /auth/merchant/register` |
96
+ | `src/api/auth/merchant/login.ts` | `POST /auth/merchant/login` (issues token with `role: 'merchant'`) |
97
+ | `src/api/auth/merchant/logout.ts` | `POST /auth/merchant/logout` |
98
+ | `src/api/auth/merchant/me.ts` | `GET /auth/merchant/me` (guarded by `requireAuth('merchant')`) |
99
+
100
+ - **No existing routes modified:** Existing `/auth/login` and `/auth/register` remain untouched.
101
+ - **Model registered:** On PostgreSQL with `defineTable`, registers the model in `src/model/index.ts`.
102
+ - **Packages ensured:** Adds `bcrypt` (and `@types/bcrypt` in TypeScript) to `package.json` and runs package manager install if missing.
103
+ - **Tracked in efc.json:** Recorded as `auth:<slug>` in `efc.json`.
70
104
 
71
- - **Dependencies come along.** `efc add admin-users` also installs `auth` and `admin` if they're missing.
72
- - **Routes that send email** (`auth`, `forgot-password`, `email-verification`) enqueue the `SendEmail` task. The base app has a stub that logs the message. `efc add email-client` swaps in real SMTP. These items need a task backend (`tasks` in `efc.config`); the base app always has one.
73
- - **Models shared by two items** (`Plan`, `SupportTicket`, `Notification`) are identical in both. The second item skips the file that's already there.
74
- - **On PostgreSQL with `defineTable`**, references are real foreign keys, and `efc add` registers new models in `src/model/index.ts` (the export plus the `Tables` entry). Models declare no back-relations. Add `relations: { … }` to a model when you want `include`. See [defineTable](../api-reference/table.md).
75
- - **Tables and collections** are created on first use (`autoSync`). With `autoSync: false`, run `efc db sync` or write a migration.
76
105
 
77
106
  ---
78
107
 
@@ -111,7 +140,7 @@ Wrong answers are asked again. A question is only asked for a variable the app d
111
140
 
112
141
  ```json
113
142
  {
114
- "registry": "0.8.0",
143
+ "registry": "0.9.0",
115
144
  "language": "typescript",
116
145
  "database": "postgresql",
117
146
  "modelApi": "defineTable",
@@ -187,6 +187,55 @@ and pass it through as `cookieDomain: process.env.COOKIE_DOMAIN` in `efc.config.
187
187
 
188
188
  ---
189
189
 
190
+ ## Custom auth entities
191
+
192
+ EFC ships with **User** and **Admin** as built-in principals (`efc add auth`). Many apps need additional authenticatable entities — a food delivery app might have `Merchant` and `DeliveryPartner`, a marketplace might have `Seller` and `Buyer`.
193
+
194
+ Use `efc add auth:` to scaffold a new entity with its own model and namespaced auth routes:
195
+
196
+ ```bash
197
+ # Either syntax works:
198
+ efc add auth:merchant
199
+ efc add auth --entity merchant
200
+
201
+ # Multiple at once:
202
+ efc add auth:merchant auth:driver
203
+ ```
204
+
205
+ ### What gets generated
206
+
207
+ For `efc add auth:merchant`:
208
+
209
+ | File | Purpose |
210
+ |---|---|
211
+ | `src/model/Merchant.ts` | Model with name, email, password, role, isActive, tokens |
212
+ | `src/api/auth/merchant/register.ts` | `POST /auth/merchant/register` |
213
+ | `src/api/auth/merchant/login.ts` | `POST /auth/merchant/login` |
214
+ | `src/api/auth/merchant/logout.ts` | `POST /auth/merchant/logout` |
215
+ | `src/api/auth/merchant/me.ts` | `GET /auth/merchant/me` |
216
+
217
+ The generated code uses the same auth primitives (`issueToken`, `requireAuth`, `revokeSession`) as the built-in User/Admin. Each entity's JWT carries `role: 'merchant'` (or whatever the entity name is, lowercased).
218
+
219
+ ### Protecting routes by entity
220
+
221
+ ```ts
222
+ import { requireAuth } from 'express-file-cluster/auth';
223
+
224
+ // Only merchants can access this route:
225
+ export const middlewares = [requireAuth('merchant')];
226
+
227
+ // Merchants OR admins:
228
+ export const middlewares = [requireAuth('merchant', 'admin')];
229
+ ```
230
+
231
+ ### Customising the generated code
232
+
233
+ The files belong to you — they're copied into your `src/` just like `efc add auth`. Add fields to the model, change the registration flow, add email verification — it's your code.
234
+
235
+ > **Note:** The existing `/auth/login` and `/auth/register` routes are **not modified**. Each custom entity gets its own namespace (`/auth/<entity>/...`), so nothing breaks.
236
+
237
+ ---
238
+
190
239
  ## Environment variables
191
240
 
192
241
  | Variable | Wired into (via `efc.config.ts`) | Description |
@@ -196,3 +245,4 @@ and pass it through as `cookieDomain: process.env.COOKIE_DOMAIN` in `efc.config.
196
245
  | `COOKIE_DOMAIN` | `cookieDomain` | Cookie domain for `http-only` strategy |
197
246
 
198
247
  `ignite()` doesn't read any of these from `process.env` itself — see the full [Environment Variables reference](./environment-variables.md) for how the scaffolded `efc.config.ts` wires them.
248
+
@@ -0,0 +1,60 @@
1
+ # Coming from mongoose
2
+
3
+ `defineCollection` keeps mongoose's query language, with the schema as a plain object, types inferred from it, and validation errors that become HTTP responses. Underneath it is still mongoose, so your data and indexes stay as they are.
4
+
5
+ ---
6
+
7
+ ## 1. Schemas
8
+
9
+ ```ts
10
+ // mongoose
11
+ const PlaceSchema = new Schema({
12
+ name: { type: String, required: true },
13
+ price: Schema.Types.Decimal128,
14
+ location: { type: { type: String, enum: ['Point'] }, coordinates: [Number] },
15
+ owner: { type: Schema.Types.ObjectId, ref: 'User' },
16
+ }, { timestamps: true });
17
+ PlaceSchema.index({ location: '2dsphere' });
18
+ export const Place = model('Place', PlaceSchema);
19
+
20
+ // EFC
21
+ export const Place = defineCollection('Place', {
22
+ name: { type: 'string', required: true },
23
+ price: { type: 'decimal128' },
24
+ location: { type: 'geoPoint', index: '2dsphere' },
25
+ owner: { type: 'objectId', ref: 'User' },
26
+ });
27
+ export type Place = InferRecord<typeof Place>; // no interface to keep in step
28
+ ```
29
+
30
+ | mongoose | EFC |
31
+ |---|---|
32
+ | `String`, `Number`, `Boolean`, `Date`, `ObjectId` | `'string'`, `'number'`, `'boolean'`, `'date'`, `'objectId'` |
33
+ | `Decimal128`, `BigInt`, `Buffer` | `'decimal128'` (string), `'long'` (bigint), `'binary'` (Buffer) |
34
+ | GeoJSON point sub-schema | `'geoPoint'` |
35
+ | `[String]`, `[SubSchema]` | `{ type: 'array', of: 'string' }`, `[{ … }]` |
36
+ | `required`, `default`, `enum`, `min`, `max`, `minlength`, `match`, `validate` | same names (`minLength`, `maxLength`) |
37
+ | `timestamps: true` | the default |
38
+ | `schema.index({...})` | `index:` on the field, or `options.indexes` |
39
+
40
+ ## 2. Queries
41
+
42
+ | mongoose | EFC |
43
+ |---|---|
44
+ | `Model.find(filter).select('name').sort('-createdAt').limit(10).lean()` | `Place.find(filter, { projection: { name: 1 }, sort: { createdAt: -1 }, limit: 10 })` |
45
+ | `.populate('owner')` | `{ populate: 'owner' }` |
46
+ | `findOne`, `findById`, `countDocuments`, `distinct`, `exists` | same names |
47
+ | `create`, `insertMany`, `updateOne`, `updateMany`, `replaceOne`, `deleteOne`, `deleteMany` | same names and result counts |
48
+ | `findOneAndUpdate(f, u, { new: true, upsert: true })` | `findOneAndUpdate(f, u, { returnDocument: 'after', upsert: true })` |
49
+ | `aggregate([...])`, `bulkWrite([...])`, `watch([...])` | same |
50
+ | `session` passed to every call | `transaction(async () => { … })` and calls inside join it |
51
+
52
+ Records are plain objects (like `lean()`) with a string `id`. Decimals come back as strings and longs as bigints.
53
+
54
+ ## 3. What you gain
55
+
56
+ - Validation errors become a **400** with `issues`, and duplicate keys a **409**, with no error-handling code.
57
+ - Collections and indexes are created on first use (`autoSync`), and `efc db generate` writes index migrations.
58
+ - Need one model for MongoDB and PostgreSQL? Use [`defineModel`](../api-reference/db.md) instead. See [Which model API?](./which-api.md).
59
+
60
+ Full reference: [`defineCollection`](../api-reference/collection.md).
@@ -33,6 +33,8 @@ const config: EFCConfig = {
33
33
  };
34
34
  ```
35
35
 
36
+ Not sure whether to use `defineModel` or the PostgreSQL-native `defineTable`? See [Which model API?](./which-api.md).
37
+
36
38
  Each worker process connects once during Pre-Flight, before routes mount — no cold connection on the first request.
37
39
 
38
40
  ---
@@ -0,0 +1,147 @@
1
+ # Relations
2
+
3
+ This guide builds a small blog schema with `defineTable` (PostgreSQL): users, profiles, posts, comments and tags. It covers each relation kind and how to read, filter and write through it. For every option, see the [`defineTable` reference](../api-reference/table.md#foreign-keys).
4
+
5
+ ---
6
+
7
+ ## The schema
8
+
9
+ ```ts
10
+ // src/model/index.ts
11
+ import { defineTable } from 'express-file-cluster';
12
+
13
+ export const User = defineTable('User', {
14
+ email: { type: 'string', required: true, unique: true },
15
+ role: { type: 'string', enum: ['user', 'admin'], default: 'user' },
16
+ }, {
17
+ relations: {
18
+ posts: { hasMany: 'Post', foreignKey: 'author' }, // the other side of Post.author
19
+ profile: { hasOne: 'Profile', foreignKey: 'user' }, // Profile.user is unique
20
+ },
21
+ });
22
+
23
+ export const Profile = defineTable('Profile', {
24
+ user: { type: 'uuid', ref: 'User', required: true, unique: true, onDelete: 'cascade' },
25
+ bio: { type: 'string' },
26
+ });
27
+
28
+ export const Tag = defineTable('Tag', {
29
+ label: { type: 'string', required: true, unique: true },
30
+ }, {
31
+ relations: { posts: { hasMany: 'Post', foreignKey: 'tags' } },
32
+ });
33
+
34
+ export const Post = defineTable('Post', {
35
+ title: { type: 'string', required: true },
36
+ published: { type: 'boolean', default: false },
37
+ author: { type: 'uuid', ref: 'User', required: true, onDelete: 'cascade' }, // belongs to
38
+ tags: { type: 'array', of: 'uuid', ref: 'Tag' }, // many-to-many
39
+ }, {
40
+ relations: { comments: { hasMany: 'Comment', foreignKey: 'post' } },
41
+ });
42
+
43
+ export const Comment = defineTable('Comment', {
44
+ post: { type: 'uuid', ref: 'Post', required: true, onDelete: 'cascade' },
45
+ body: { type: 'string', required: true },
46
+ approved: { type: 'boolean', default: false },
47
+ });
48
+
49
+ // typed includes: register the models once
50
+ declare module 'express-file-cluster' {
51
+ interface Tables {
52
+ User: typeof User; Profile: typeof Profile; Tag: typeof Tag; Post: typeof Post; Comment: typeof Comment;
53
+ }
54
+ }
55
+ ```
56
+
57
+ What each piece creates on the first sync:
58
+
59
+ | Declared | In the database |
60
+ |---|---|
61
+ | `author: { type: 'uuid', ref: 'User' }` | an `author` column, a foreign key to `users(id)`, an index |
62
+ | `tags: { type: 'array', of: 'uuid', ref: 'Tag' }` | a join table `posts_tags` (`owner_id`, `ref_id`, `position`), cascading both ways |
63
+ | `relations: { posts: { hasMany … } }` | nothing: it reads `Post.author` from the other side |
64
+ | `relations: { profile: { hasOne … } }` | nothing, but `Profile.user` must be `unique` |
65
+
66
+ Records hold ids, not objects: `post.author` is a string and `post.tags` is a `string[]`. `include` swaps them for records when you ask.
67
+
68
+ ## Reading with `include`
69
+
70
+ ```ts
71
+ const post = await Post.findUnique({
72
+ where: { id },
73
+ include: {
74
+ author: { select: { email: true } },
75
+ tags: true,
76
+ comments: { where: { approved: true }, orderBy: { createdAt: 'desc' }, take: 10 },
77
+ },
78
+ });
79
+ // { id, title, …, author: { email }, tags: [{ id, label, … }], comments: [{ … }] }
80
+
81
+ const users = await User.findMany({
82
+ include: { profile: true, posts: { where: { published: true }, include: { tags: true } } },
83
+ });
84
+ ```
85
+
86
+ However deep the `include` goes, the call runs **one SQL statement**. Turn on `logging.queries` to see the statement count per call.
87
+
88
+ ## Filtering by relation
89
+
90
+ ```ts
91
+ await User.findMany({ where: { posts: { some: { published: true } } } }); // has a published post
92
+ await User.findMany({ where: { posts: { none: {} } } }); // no posts at all
93
+ await Post.findMany({ where: { author: { is: { role: 'admin' } } } }); // by an admin
94
+ await Post.findMany({ where: { tags: { every: { label: { startsWith: 'pub-' } } } } });
95
+ await Post.findMany({ where: { comments: { some: { approved: false } } } }); // needs moderation
96
+ await Post.findMany({ where: { tags: { has: tagId } } }); // by id, no join
97
+ ```
98
+
99
+ To-many relations take `some` / `every` / `none`. To-one relations take `is` / `isNot` (or `null`). They nest, and work anywhere `where` does: `count`, `updateMany`, `deleteMany`, and inside `include`.
100
+
101
+ ## Writing through relations
102
+
103
+ ```ts
104
+ // create a post, connect its author, create or connect tags, add a first comment: one transaction
105
+ const post = await Post.create({
106
+ data: {
107
+ title: 'Hello',
108
+ author: { connect: { email: 'ann@example.com' } },
109
+ tags: { connectOrCreate: [{ where: { label: 'news' }, create: { label: 'news' } }] },
110
+ comments: { create: [{ body: 'First!' }] },
111
+ },
112
+ include: { tags: true, comments: true },
113
+ });
114
+
115
+ await Post.update({ where: { id: post.id }, data: { tags: { push: launchTagId } } });
116
+ await Post.update({ where: { id: post.id }, data: { tags: { set: [] } } });
117
+
118
+ await User.update({
119
+ where: { id: userId },
120
+ data: {
121
+ profile: { create: { bio: 'Writes about Postgres' } }, // has one already? add delete: true
122
+ posts: { deleteMany: { published: false } }, // drop their drafts
123
+ },
124
+ });
125
+ ```
126
+
127
+ A `connect` that matches nothing is a **400** on that field, and nothing is written. A `hasOne` `create` or `connect` unlinks the current profile, if there is one. `Profile.user` is required, so here it can't be unlinked: write `profile: { delete: true, create: { … } }` to replace it. `createMany` and `updateMany` don't take nested writes.
128
+
129
+ ## Deleting
130
+
131
+ `onDelete` on the ref field decides what happens to the records pointing at a deleted one:
132
+
133
+ ```ts
134
+ await User.delete({ where: { id } }); // cascades: profile, posts, and (through posts) comments go too
135
+ ```
136
+
137
+ With the default `onDelete: 'restrict'` the same delete would be a **409** while posts still point at the user. `'setNull'` clears the field instead, so it can't be used on `required` fields. Join-table links are removed with either side.
138
+
139
+ ## On MongoDB, or with `defineModel`
140
+
141
+ `defineModel` keeps relations portable: a `ref` is a plain id field on both engines, with no foreign key, and `populate` resolves it in a batched second query:
142
+
143
+ ```ts
144
+ await Post.find({ published: true }, { populate: 'author' });
145
+ ```
146
+
147
+ There is no `include`, relation filtering or nested writes there. For those, use `defineTable` on PostgreSQL. See [Which model API?](./which-api.md).
@@ -0,0 +1,59 @@
1
+ # Which model API?
2
+
3
+ EFC has three ways to define a model. They share one foundation: the schema format, inferred types, validation, auto-sync, `transaction()`, undeclared-field rules and errors. They differ in which engines they run on and in their query vocabulary.
4
+
5
+ | | `defineModel` | `defineTable` | `defineCollection` |
6
+ |---|---|---|---|
7
+ | Engine | MongoDB or PostgreSQL (set by `DATABASE_URL`) | PostgreSQL only | MongoDB only |
8
+ | Queries | portable subset: `find(filter, { sort, limit, populate })`, `$set`, `$inc` | Prisma-style: `findMany({ where, include, orderBy, take, cursor })`, `findUnique`, `upsert`, `groupBy`, `aggregate` | mongoose-style: every query operator, `find(filter, { projection })`, `findOneAndUpdate`, `bulkWrite`, `aggregate(pipeline)`, `watch` |
9
+ | Relations | `ref` + `populate` (a plain column on PostgreSQL) | real foreign keys, many-to-many join tables, `include` in one SQL statement, relation filters, nested writes | `ref` + `populate`, `$lookup`, embedded documents |
10
+ | Field types | the portable set (`string`, `number`, `boolean`, `date`, `objectId`, objects, arrays) | + `integer`, `bigint`, `decimal`, `uuid`, `json`, `varchar`, native arrays and enums, `serial` ids | + `decimal128`, `long`, `geoPoint`, `binary`; `2dsphere`, `text`, `hashed` and partial indexes |
11
+ | Migrations | auto-sync, `efc db generate` | auto-sync, `efc db generate`, `efc db pull`, `efc import-prisma` | auto-sync, `efc db generate` |
12
+ | Wrong engine | — | fails at first use with an `[EFC]` error naming the model | the same |
13
+
14
+ ## Pick one
15
+
16
+ - **Building on PostgreSQL:** use `defineTable`. You get foreign keys, joins and SQL types. `create-efc-app` generates `defineTable` models for PostgreSQL apps.
17
+ - **Building on MongoDB:** use `defineCollection`. You get the full query language, geo and text search, BSON types, `bulkWrite` and change streams. It is mongoose underneath (`raw()` hands you the mongoose model).
18
+ - **The app may switch engines, or ships to both:** use `defineModel` and stay inside the shared filter and update subset (`$eq $ne $gt $gte $lt $lte $in $nin $exists $regex $and $or`; `$set $unset $inc $push $pull`).
19
+ - **Coming from Prisma:** use `defineTable`. `efc import-prisma` writes the models for you. See [Coming from Prisma](./coming-from-prisma.md).
20
+
21
+ ## The same query, three ways
22
+
23
+ ```ts
24
+ // defineModel
25
+ const posts = await Post.find(
26
+ { status: 'published', views: { $gte: 100 } },
27
+ { sort: { createdAt: -1 }, limit: 20, populate: 'author' },
28
+ );
29
+ await Post.update(id, { $inc: { views: 1 } });
30
+
31
+ // defineTable
32
+ const posts = await Post.findMany({
33
+ where: { status: 'published', views: { gte: 100 } },
34
+ orderBy: { createdAt: 'desc' },
35
+ take: 20,
36
+ include: { author: true },
37
+ });
38
+ await Post.update({ where: { id }, data: { views: { increment: 1 } } });
39
+
40
+ // defineCollection
41
+ const posts = await Post.find(
42
+ { status: 'published', views: { $gte: 100 } },
43
+ { sort: { createdAt: -1 }, limit: 20, populate: 'author', projection: { body: 0 } },
44
+ );
45
+ await Post.findByIdAndUpdate(id, { $inc: { views: 1 } });
46
+ ```
47
+
48
+ ## Moving between them
49
+
50
+ On MongoDB, `defineModel` and `defineCollection` over the same collection read and write the same documents. Only the method names and results differ (`updateMany` returns `{ modifiedCount, … }` instead of a number, for example).
51
+
52
+ On PostgreSQL, a `defineModel` and a `defineTable` over the same table read and write the same records, so you can move one model at a time. Two things change on the way:
53
+
54
+ - A `ref` becomes a real foreign key. The field's `type` must match the target's id (`uuid` for the default id, `integer` for `id: 'serial'`). Rows pointing at missing records don't block the deploy: the key is added `NOT VALID`, and validation is skipped with a warning naming the count.
55
+ - An array of refs (a `jsonb` column under `defineModel`) moves into a join table on the first sync. The old column is kept but no longer read, so don't keep a `defineModel` writing that field afterwards.
56
+
57
+ Run `efc db sync --dry-run` before you switch to see exactly what will change.
58
+
59
+ See also: [Database guide](./database.md) · [Relations](./relations.md) · [`defineModel` API](../api-reference/db.md) · [`defineTable` API](../api-reference/table.md) · [`defineCollection` API](../api-reference/collection.md)
@@ -2,7 +2,7 @@
2
2
 
3
3
  **express-file-cluster (EFC)** is an opinionated backend framework built on Express. It removes routing ceremony, saturates every CPU core automatically, and ships a production-grade background-task subsystem — all from a single `ignite()` call.
4
4
 
5
- > **Status: v0.8.0 — Beta.** File-based routing (incl. catch-all and optional segments), request validation, clustering, auth, structured logging, MongoDB and PostgreSQL models with types inferred from the schema, PostgreSQL-native `defineTable` (Prisma-style queries and relations: foreign keys, many-to-many, `include`, relation filters, nested writes), query logging, BullMQ and pg-boss tasks with cron and dead-letter tooling, migrations, and the `efc add` feature registry (auth, admin, billing… copied into your app as source you own) are all implemented.
5
+ > **Status: v0.9.0 — Beta.** File-based routing (incl. catch-all and optional segments), request validation, clustering, auth, structured logging, MongoDB and PostgreSQL models with types inferred from the schema, PostgreSQL-native `defineTable` (Prisma-style queries and relations: foreign keys, many-to-many, `include`, relation filters, nested writes), query logging, BullMQ and pg-boss tasks with cron and dead-letter tooling, migrations (written for you from model changes: `efc db generate`, plus `efc db pull` and `efc import-prisma`), and the `efc add` feature registry (auth, admin, billing… copied into your app as source you own) are all implemented.
6
6
 
7
7
  ---
8
8
 
@@ -37,9 +37,13 @@
37
37
  | [Authentication](./guides/authentication.md) | JWT strategies, login/logout, `requireAuth` |
38
38
  | [RBAC](./guides/rbac.md) | Role-based route protection via the `requireAuth('role')` shorthand |
39
39
  | [Database](./guides/database.md) | `defineModel`, queries, transactions, MongoDB vs PostgreSQL |
40
+ | [Which model API?](./guides/which-api.md) | `defineModel` vs `defineTable` vs `defineCollection`, and moving between them |
41
+ | [Relations](./guides/relations.md) | Foreign keys, many-to-many, `include`, relation filters, nested writes |
40
42
  | [defineTable](./api-reference/table.md) | PostgreSQL-native models: Prisma-style queries, SQL types, aggregation |
43
+ | [defineCollection](./api-reference/collection.md) | MongoDB-native models: mongoose-style queries, BSON types, geo, bulk writes, change streams |
41
44
  | [Migrations & Seeds](./guides/migrations.md) | `efc db generate / migrate / rollback / pull / seed / studio`, drift checks |
42
45
  | [Coming from Prisma](./guides/coming-from-prisma.md) | `efc import-prisma`, schema and query mapping |
46
+ | [Coming from mongoose](./guides/coming-from-mongoose.md) | `defineCollection` for mongoose users: schema and query mapping |
43
47
  | [Validation](./guides/validation.md) | Route `schema` exports with zod, valibot, arktype |
44
48
  | [Logging](./guides/logging.md) | pino, request IDs, `getLogger()` |
45
49
  | [Caching & Sessions](./guides/caching.md) | `cache`, `cacheResponse`, revoking JWT sessions |
@@ -37,8 +37,8 @@ Load **only** the module the work points to. Each is self-contained.
37
37
  docs/ ← full documentation
38
38
  ```
39
39
 
40
- ## Status (v0.8.0 — Beta, Phase 2 complete + defineTable with relations + `efc add` registry)
40
+ ## Status (v0.9.0 — Beta, Phase 2 complete + defineTable with relations + generated migrations + `efc add` registry)
41
41
 
42
- Implemented: file-based router (incl. `[[id]]`, `[...slug]`, `[[...slug]]`), request validation (`export const schema`, Standard Schema), multi-core clustering with graceful shutdown, http-only + localStorage auth, structured logging (pino, request IDs), `defineModel` on MongoDB (mongoose) **and** PostgreSQL (Drizzle) with validation, query options, bulk ops, transactions, `sync()`/`drop()`, PostgreSQL-native `defineTable` (Prisma-style queries, foreign keys, many-to-many, `include`, relation filters, nested writes), query logging (`logging.queries`), BullMQ **and** pg-boss task backends with cron schedules and dead-letter CLI, worker_threads for CPU-bound tasks, migrations/seeds/studio (`efc db`) incl. migrations generated from model diffs (`efc db generate`), `efc db pull` and `efc import-prisma`, full CLI.
42
+ Implemented: file-based router (incl. `[[id]]`, `[...slug]`, `[[...slug]]`), request validation (`export const schema`, Standard Schema), multi-core clustering with graceful shutdown, http-only + localStorage auth, structured logging (pino, request IDs), `defineModel` on MongoDB (mongoose) **and** PostgreSQL (Drizzle) with validation, query options, bulk ops, transactions, `sync()`/`drop()`, PostgreSQL-native `defineTable` (Prisma-style queries, foreign keys, many-to-many, `include`, relation filters, nested writes), MongoDB-native `defineCollection` (mongoose vocabulary, BSON types, geo/text, `bulkWrite`, change streams), query logging (`logging.queries`), BullMQ **and** pg-boss task backends with cron schedules and dead-letter CLI, worker_threads for CPU-bound tasks, migrations/seeds/studio (`efc db`) incl. migrations generated from model diffs (`efc db generate`), `efc db pull` and `efc import-prisma`, full CLI.
43
43
 
44
- **Not yet implemented (Phase 3+):** WebSockets, plugins, OpenAPI generation, OpenTelemetry, `efc lint`, `efc deploy`, `defineCollection`.
44
+ **Not yet implemented (Phase 3+):** WebSockets, plugins, OpenAPI generation, OpenTelemetry, `efc lint`, `efc deploy`.