@pgxsinkit/client 0.1.27 → 0.1.28

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pgxsinkit/client",
3
- "version": "0.1.27",
3
+ "version": "0.1.28",
4
4
  "files": [
5
5
  "dist",
6
6
  "src",
@@ -22,8 +22,8 @@
22
22
  "build": "bun --cwd ../.. run build:public-packages"
23
23
  },
24
24
  "dependencies": {
25
- "@pgxsinkit/contracts": "^0.1.27",
26
- "@pgxsinkit/sync-engine": "^0.1.27"
25
+ "@pgxsinkit/contracts": "^0.1.28",
26
+ "@pgxsinkit/sync-engine": "^0.1.28"
27
27
  },
28
28
  "peerDependencies": {
29
29
  "@electric-sql/pglite": "0.5.1",
package/src/mutation.ts CHANGED
@@ -1,3 +1,4 @@
1
+ import { is, SQL } from "drizzle-orm";
1
2
  import { getTableConfig, getViewConfig, type AnyPgTable } from "drizzle-orm/pg-core";
2
3
 
3
4
  /**
@@ -801,6 +802,40 @@ function filterContexts<TRegistry extends SyncTableRegistry>(
801
802
  return Object.values(contexts).filter((context): context is TableContext => context !== undefined);
802
803
  }
803
804
 
805
+ /**
806
+ * Materialise a column's default into a concrete JS value for the optimistic overlay
807
+ * row. The overlay INSERT writes an explicit value for every projected column (an
808
+ * omitted column becomes a literal `NULL`, which overrides any table-level DEFAULT), so
809
+ * a column declared `NOT NULL DEFAULT <x>` that the caller omits would violate the
810
+ * overlay's NOT NULL constraint unless we fill the default here — mirroring what the
811
+ * authoritative server applies on the base table.
812
+ *
813
+ * Only values we can produce client-side are materialised: a value-returning `defaultFn`
814
+ * (e.g. `$defaultFn`) or a literal `.default(value)`. SQL-expression defaults
815
+ * (`.default(sql\`…\`)`, `defaultRandom()`, `defaultNow()`) cannot be evaluated without
816
+ * the database, so they are left to the server (the column is then expected to be either
817
+ * caller-supplied or nullable in the optimistic row).
818
+ */
819
+ function materializeColumnDefault(column: TableContext["columns"][number]["column"]): {
820
+ ok: boolean;
821
+ value?: unknown;
822
+ } {
823
+ if (!column.hasDefault) {
824
+ return { ok: false };
825
+ }
826
+
827
+ if (typeof column.defaultFn === "function") {
828
+ const produced = column.defaultFn();
829
+ return is(produced, SQL) ? { ok: false } : { ok: true, value: produced };
830
+ }
831
+
832
+ if (column.default !== undefined && !is(column.default, SQL)) {
833
+ return { ok: true, value: column.default };
834
+ }
835
+
836
+ return { ok: false };
837
+ }
838
+
804
839
  function createOptimisticRecordFromContext<TCreate, TRecord>(context: TableContext, input: TCreate): TRecord {
805
840
  const record = {
806
841
  ...(isRecord(input) ? input : {}),
@@ -815,6 +850,17 @@ function createOptimisticRecordFromContext<TCreate, TRecord>(context: TableConte
815
850
  record.updatedAtUs = nowUs;
816
851
  }
817
852
 
853
+ for (const { propertyKey, column } of context.columns) {
854
+ if (record[propertyKey] !== undefined) {
855
+ continue;
856
+ }
857
+
858
+ const materialized = materializeColumnDefault(column);
859
+ if (materialized.ok) {
860
+ record[propertyKey] = materialized.value;
861
+ }
862
+ }
863
+
818
864
  return buildOptimisticRecord(context, record, {
819
865
  overlayKind: "pending_create",
820
866
  localUpdatedAtUs: nowUs,