@easy-cms/drizzle 0.4.0 → 0.5.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/index.d.ts CHANGED
@@ -75,6 +75,10 @@ interface ColumnModel {
75
75
  readonly path: readonly string[];
76
76
  readonly field: Field;
77
77
  readonly column: string;
78
+ /** For localized fields: the locale whose value this column holds. */
79
+ readonly locale?: string;
80
+ /** The default locale's column, used when a query names no locale. */
81
+ readonly defaultLocale?: boolean;
78
82
  }
79
83
  /** A child table holding array rows or hasMany values. */
80
84
  interface ChildModel {
package/dist/index.js CHANGED
@@ -22,15 +22,16 @@ function setAt(data, path, value) {
22
22
  }
23
23
  current[path[path.length - 1]] = value;
24
24
  }
25
+ var columnPath = (column) => column.locale === void 0 ? column.path : [...column.path, column.locale];
25
26
  function rowToData(model, row) {
26
27
  const data = {};
27
- for (const column of model.columns) setAt(data, column.path, row[column.column] ?? null);
28
+ for (const column of model.columns) setAt(data, columnPath(column), row[column.column] ?? null);
28
29
  for (const child of model.children) setAt(data, child.path, []);
29
30
  return data;
30
31
  }
31
32
  function dataToRow(model, data) {
32
33
  const row = {};
33
- for (const column of model.columns) row[column.column] = getAt(data, column.path) ?? null;
34
+ for (const column of model.columns) row[column.column] = getAt(data, columnPath(column)) ?? null;
34
35
  return row;
35
36
  }
36
37
  var CHUNK = 500;
@@ -413,13 +414,20 @@ function resolvePath(model, segments, drafts) {
413
414
  }
414
415
  const child = model.children.find((c) => c.path.join(".") === path.join("."));
415
416
  if (child) return { kind: "child", child, rest: segments.slice(i + 1) };
416
- if (i < segments.length - 1) {
417
+ const locale = field.localized ? segments[i + 1] : void 0;
418
+ const last = field.localized && locale !== void 0 ? i + 1 : i;
419
+ if (last < segments.length - 1) {
417
420
  throw new QueryError(`Cannot query inside "${shown}" (${field.type} field)`);
418
421
  }
419
422
  if (field.type === "json" || field.type === "richText") {
420
423
  throw new QueryError(`Cannot query "${shown}": ${field.type} fields are not queryable`);
421
424
  }
422
- const column = model.columns.find((c) => c.path.join(".") === path.join("."));
425
+ const same = (c) => c.path.join(".") === path.join(".");
426
+ const column = field.localized ? model.columns.find(
427
+ (c) => same(c) && (locale === void 0 ? c.defaultLocale : c.locale === locale)
428
+ ) : model.columns.find(same);
429
+ if (field.localized && !column)
430
+ throw new QueryError(`Unknown locale "${locale}" for "${shown}"`);
423
431
  if (!column) throw new QueryError(`Unknown field "${shown}"`);
424
432
  return { kind: "column", column: col2(model, column.column), field };
425
433
  }
@@ -583,7 +591,8 @@ function buildSchema(config, prefix, dialect) {
583
591
  prefix,
584
592
  tableNames: /* @__PURE__ */ new Set([migrationsTableName(prefix)]),
585
593
  // The hash covers the logical schema only, so it stays stable across releases for the same config.
586
- description: []
594
+ description: [],
595
+ localization: config.localization
587
596
  };
588
597
  const collections = /* @__PURE__ */ new Map();
589
598
  const tables = {};
@@ -715,12 +724,27 @@ function buildTable(builder, name, kind, fields, options) {
715
724
  described.push([base, "values", child.name]);
716
725
  } else {
717
726
  const kindOfColumn = columnKind(field);
718
- add(base, makeColumn(dialect, base, kindOfColumn), kindOfColumn);
719
- columnModels.push({ path: fieldPath, field, column: base });
720
- if (topLevel && kind === "root" && (field.unique || field.type === "slug")) {
721
- indexes.push({ column: base, unique: true });
722
- } else if (field.index || field.unique || field.type === "relationship" || field.type === "upload") {
723
- indexes.push({ column: base, unique: false });
727
+ const localization = field.localized ? builder.localization : null;
728
+ const targets = localization ? localization.locales.map((locale) => ({
729
+ column: locale === localization.defaultLocale ? base : `${base}__${snake(locale)}`,
730
+ locale
731
+ })) : [{ column: base, locale: void 0 }];
732
+ for (const { column, locale } of targets) {
733
+ add(column, makeColumn(dialect, column, kindOfColumn), kindOfColumn);
734
+ columnModels.push(
735
+ locale === void 0 ? { path: fieldPath, field, column } : {
736
+ path: fieldPath,
737
+ field,
738
+ column,
739
+ locale,
740
+ defaultLocale: locale === localization?.defaultLocale
741
+ }
742
+ );
743
+ if (topLevel && kind === "root" && (field.unique || field.type === "slug")) {
744
+ indexes.push({ column, unique: true });
745
+ } else if (field.index || field.unique || field.type === "relationship" || field.type === "upload") {
746
+ indexes.push({ column, unique: false });
747
+ }
724
748
  }
725
749
  }
726
750
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@easy-cms/drizzle",
3
- "version": "0.4.0",
3
+ "version": "0.5.0",
4
4
  "description": "Shared Drizzle ORM layer for Easy CMS database adapters",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -23,10 +23,10 @@
23
23
  "drizzle-orm": "0.45.3"
24
24
  },
25
25
  "peerDependencies": {
26
- "@easy-cms/core": "^0.4.0"
26
+ "@easy-cms/core": "^0.5.0"
27
27
  },
28
28
  "devDependencies": {
29
- "@easy-cms/core": "^0.4.0"
29
+ "@easy-cms/core": "^0.5.0"
30
30
  },
31
31
  "publishConfig": {
32
32
  "access": "public"