@dyrected/core 2.0.0 → 2.4.0

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/dist/server.cjs CHANGED
@@ -154,7 +154,14 @@ var DefaultsService = class {
154
154
  static apply(fields, data = {}) {
155
155
  const result = { ...data || {} };
156
156
  fields.forEach((field) => {
157
- const value = result[field.name];
157
+ let value = result[field.name];
158
+ if ((value === void 0 || value === null) && field.renameTo) {
159
+ const legacyValue = result[field.renameTo];
160
+ if (legacyValue !== void 0 && legacyValue !== null) {
161
+ value = legacyValue;
162
+ result[field.name] = legacyValue;
163
+ }
164
+ }
158
165
  if (value === void 0 || value === null) {
159
166
  if (field.defaultValue !== void 0) {
160
167
  result[field.name] = field.defaultValue;
@@ -233,6 +240,19 @@ var CollectionController = class {
233
240
  sort,
234
241
  where
235
242
  });
243
+ if (result.total === 0 && this.collection.initialData && !where && page === 1) {
244
+ console.log(`[dyrected/core] Auto-seeding collection "${this.collection.slug}" from config.initialData`);
245
+ for (const data of this.collection.initialData) {
246
+ await db.create({ collection: this.collection.slug, data });
247
+ }
248
+ result = await db.find({
249
+ collection: this.collection.slug,
250
+ limit,
251
+ page,
252
+ sort,
253
+ where
254
+ });
255
+ }
236
256
  result.docs = result.docs.map((doc) => DefaultsService.apply(this.collection.fields, doc));
237
257
  if (depth > 0) {
238
258
  const populationService = new PopulationService(db, config.collections);
@@ -385,6 +405,54 @@ var CollectionController = class {
385
405
  }
386
406
  return c.json({ message: "Deleted" });
387
407
  }
408
+ async deleteMany(c) {
409
+ const config = c.get("config");
410
+ const db = config.db;
411
+ if (!db) return c.json({ message: "Database not configured" }, 500);
412
+ const user = c.get("user");
413
+ let ids = [];
414
+ try {
415
+ const body = await c.req.json().catch(() => null);
416
+ if (body?.ids && Array.isArray(body.ids)) {
417
+ ids = body.ids;
418
+ }
419
+ } catch {
420
+ }
421
+ if (!ids.length) {
422
+ const raw = c.req.queries("ids") ?? c.req.queries("ids[]") ?? [];
423
+ ids = raw.filter(Boolean);
424
+ }
425
+ if (!ids.length) return c.json({ message: "No IDs provided" }, 400);
426
+ const deleted = [];
427
+ const failed = [];
428
+ for (const id of ids) {
429
+ try {
430
+ let before = null;
431
+ if (this.collection.audit) {
432
+ before = await db.findOne({ collection: this.collection.slug, id });
433
+ }
434
+ await db.delete({ collection: this.collection.slug, id });
435
+ deleted.push(id);
436
+ if (this.collection.audit) {
437
+ AuditService.log(db, {
438
+ operation: "delete",
439
+ collection: this.collection.slug,
440
+ documentId: id,
441
+ user: user ? { id: user.sub, collection: user.collection, email: user.email } : void 0,
442
+ before,
443
+ after: null
444
+ });
445
+ }
446
+ } catch (err) {
447
+ failed.push({ id, error: err?.message ?? "Unknown error" });
448
+ }
449
+ }
450
+ return c.json({
451
+ message: `Deleted ${deleted.length} document(s)`,
452
+ deleted,
453
+ ...failed.length ? { failed } : {}
454
+ });
455
+ }
388
456
  async seed(c) {
389
457
  const config = c.get("config");
390
458
  const db = config.db;
@@ -419,7 +487,13 @@ var GlobalController = class {
419
487
  const db = config.db;
420
488
  if (!db) return c.json({ message: "Database not configured" }, 500);
421
489
  const depth = c.req.query("depth") !== void 0 ? Number(c.req.query("depth")) : 1;
422
- const data = await db.getGlobal({ slug: this.global.slug });
490
+ let data = await db.getGlobal({ slug: this.global.slug });
491
+ const isEmpty = !data || Object.keys(data).length === 0;
492
+ if (isEmpty && this.global.initialData) {
493
+ console.log(`[dyrected/core] Auto-seeding global "${this.global.slug}" from config.initialData`);
494
+ await db.updateGlobal({ slug: this.global.slug, data: this.global.initialData });
495
+ data = this.global.initialData;
496
+ }
423
497
  const dataWithDefaults = DefaultsService.apply(this.global.fields, data);
424
498
  if (depth > 0 && dataWithDefaults) {
425
499
  const populationService = new PopulationService(db, config.collections);
@@ -1634,6 +1708,7 @@ function registerRoutes(app, config) {
1634
1708
  app.get(path, accessGate(collection, "read"), (c) => controller.find(c));
1635
1709
  app.post(path, accessGate(collection, "create"), (c) => controller.create(c));
1636
1710
  app.post(`${path}/media`, accessGate(collection, "create"), (c) => controller.create(c));
1711
+ app.delete(`${path}/delete-many`, accessGate(collection, "delete"), (c) => controller.deleteMany(c));
1637
1712
  app.get(`${path}/:id`, accessGate(collection, "read"), (c) => controller.findOne(c));
1638
1713
  app.patch(`${path}/:id`, accessGate(collection, "update"), (c) => controller.update(c));
1639
1714
  app.delete(`${path}/:id`, accessGate(collection, "delete"), (c) => controller.delete(c));
@@ -1684,8 +1759,10 @@ function registerRoutes(app, config) {
1684
1759
  if (id) {
1685
1760
  if (method === "GET") return controller.findOne(c);
1686
1761
  if (method === "PATCH") return controller.update(c);
1762
+ if (method === "DELETE" && id === "delete-many") return controller.deleteMany(c);
1687
1763
  if (method === "DELETE") return controller.delete(c);
1688
1764
  if (method === "POST" && id === "media") return controller.create(c);
1765
+ if (method === "POST" && id === "seed") return controller.seed(c);
1689
1766
  } else {
1690
1767
  if (method === "GET") return controller.find(c);
1691
1768
  if (method === "POST") return controller.create(c);
package/dist/server.d.cts CHANGED
@@ -1,28 +1,10 @@
1
- import * as hono_types from 'hono/types';
1
+ import { c as DyrectedContext, D as DyrectedConfig, C as CollectionConfig, G as GlobalConfig } from './app-KApbf4XL.cjs';
2
+ export { g as createDyrectedApp } from './app-KApbf4XL.cjs';
2
3
  import * as hono from 'hono';
3
4
  import { Hono, Context } from 'hono';
4
- import { D as DyrectedConfig, C as CollectionConfig, G as GlobalConfig } from './index-RylhgOwj.cjs';
5
5
  import * as hono_utils_http_status from 'hono/utils/http-status';
6
6
  import * as hono_utils_types from 'hono/utils/types';
7
-
8
- interface DyrectedContext {
9
- Variables: {
10
- config: DyrectedConfig;
11
- siteId?: string;
12
- workspaceId?: string;
13
- /** Decoded JWT payload set by requireAuth() or optionalAuth() middleware. */
14
- user?: {
15
- sub: string;
16
- email: string;
17
- collection: string;
18
- [key: string]: any;
19
- };
20
- };
21
- }
22
- /**
23
- * Create the main Dyrected Hono application.
24
- */
25
- declare function createDyrectedApp(rawConfig: DyrectedConfig): Promise<Hono<DyrectedContext, hono_types.BlankSchema, "/">>;
7
+ import 'hono/types';
26
8
 
27
9
  /**
28
10
  * Register dynamic routes based on the provided configuration.
@@ -161,6 +143,18 @@ declare class CollectionController {
161
143
  delete(c: Context<DyrectedContext>): Promise<Response & hono.TypedResponse<{
162
144
  message: string;
163
145
  }, hono_utils_http_status.ContentfulStatusCode, "json">>;
146
+ deleteMany(c: Context<DyrectedContext>): Promise<(Response & hono.TypedResponse<{
147
+ message: string;
148
+ }, 500, "json">) | (Response & hono.TypedResponse<{
149
+ message: string;
150
+ }, 400, "json">) | (Response & hono.TypedResponse<{
151
+ failed?: {
152
+ id: string;
153
+ error: string;
154
+ }[] | undefined;
155
+ message: string;
156
+ deleted: string[];
157
+ }, hono_utils_http_status.ContentfulStatusCode, "json">)>;
164
158
  seed(c: Context<DyrectedContext>): Promise<Response & hono.TypedResponse<{
165
159
  message: string;
166
160
  }, hono_utils_http_status.ContentfulStatusCode, "json">>;
@@ -240,4 +234,4 @@ declare class PreviewController {
240
234
  }, 401, "json">)>;
241
235
  }
242
236
 
243
- export { AuthController, CollectionController, type DyrectedContext, GlobalController, MediaController, PreviewController, createDyrectedApp, registerRoutes };
237
+ export { AuthController, CollectionController, DyrectedContext, GlobalController, MediaController, PreviewController, registerRoutes };
package/dist/server.d.ts CHANGED
@@ -1,28 +1,10 @@
1
- import * as hono_types from 'hono/types';
1
+ import { c as DyrectedContext, D as DyrectedConfig, C as CollectionConfig, G as GlobalConfig } from './app-KApbf4XL.js';
2
+ export { g as createDyrectedApp } from './app-KApbf4XL.js';
2
3
  import * as hono from 'hono';
3
4
  import { Hono, Context } from 'hono';
4
- import { D as DyrectedConfig, C as CollectionConfig, G as GlobalConfig } from './index-RylhgOwj.js';
5
5
  import * as hono_utils_http_status from 'hono/utils/http-status';
6
6
  import * as hono_utils_types from 'hono/utils/types';
7
-
8
- interface DyrectedContext {
9
- Variables: {
10
- config: DyrectedConfig;
11
- siteId?: string;
12
- workspaceId?: string;
13
- /** Decoded JWT payload set by requireAuth() or optionalAuth() middleware. */
14
- user?: {
15
- sub: string;
16
- email: string;
17
- collection: string;
18
- [key: string]: any;
19
- };
20
- };
21
- }
22
- /**
23
- * Create the main Dyrected Hono application.
24
- */
25
- declare function createDyrectedApp(rawConfig: DyrectedConfig): Promise<Hono<DyrectedContext, hono_types.BlankSchema, "/">>;
7
+ import 'hono/types';
26
8
 
27
9
  /**
28
10
  * Register dynamic routes based on the provided configuration.
@@ -161,6 +143,18 @@ declare class CollectionController {
161
143
  delete(c: Context<DyrectedContext>): Promise<Response & hono.TypedResponse<{
162
144
  message: string;
163
145
  }, hono_utils_http_status.ContentfulStatusCode, "json">>;
146
+ deleteMany(c: Context<DyrectedContext>): Promise<(Response & hono.TypedResponse<{
147
+ message: string;
148
+ }, 500, "json">) | (Response & hono.TypedResponse<{
149
+ message: string;
150
+ }, 400, "json">) | (Response & hono.TypedResponse<{
151
+ failed?: {
152
+ id: string;
153
+ error: string;
154
+ }[] | undefined;
155
+ message: string;
156
+ deleted: string[];
157
+ }, hono_utils_http_status.ContentfulStatusCode, "json">)>;
164
158
  seed(c: Context<DyrectedContext>): Promise<Response & hono.TypedResponse<{
165
159
  message: string;
166
160
  }, hono_utils_http_status.ContentfulStatusCode, "json">>;
@@ -240,4 +234,4 @@ declare class PreviewController {
240
234
  }, 401, "json">)>;
241
235
  }
242
236
 
243
- export { AuthController, CollectionController, type DyrectedContext, GlobalController, MediaController, PreviewController, createDyrectedApp, registerRoutes };
237
+ export { AuthController, CollectionController, DyrectedContext, GlobalController, MediaController, PreviewController, registerRoutes };