akanjs 3.0.0-alpha.93 → 3.0.0-alpha.94

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.
@@ -39,6 +39,10 @@ const buildPlan = (fieldObj: FieldObject): DefaultPlan => {
39
39
  if (field.fieldType === "hidden" || field.fieldType === "secret") shared[key] = null;
40
40
  else if (field.default !== undefined && field.default !== null) {
41
41
  if (typeof field.default === "function") perCall.set(key, field.default as () => unknown);
42
+ else if (Array.isArray(field.default)) {
43
+ const items = field.default as unknown[];
44
+ perCall.set(key, () => [...items]);
45
+ }
42
46
  else shared[key] = field.default as object;
43
47
  } else if (field.isArray) perCall.set(key, () => []);
44
48
  else if (field.nullable) shared[key] = null;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "akanjs",
3
- "version": "3.0.0-alpha.93",
3
+ "version": "3.0.0-alpha.94",
4
4
  "sourceType": "module",
5
5
  "type": "module",
6
6
  "publishConfig": {
@@ -44,6 +44,8 @@ import {
44
44
  import { UpdateCompiler } from "./UpdateCompiler";
45
45
  import { decodeDateValue, encodeSqlValue, jsonStr } from "./values";
46
46
 
47
+ const freshDefault = (value: unknown) => (Array.isArray(value) ? [...(value as unknown[])] : value);
48
+
47
49
  export class SqlDocumentStore {
48
50
  readonly schema: DocumentSchema;
49
51
  readonly table: string;
@@ -331,7 +333,10 @@ export class SqlDocumentStore {
331
333
  const value = data[key];
332
334
  if (value === undefined) {
333
335
  if (props.default !== undefined && props.default !== null) {
334
- doc[key] = typeof props.default === "function" ? props.default(data) : props.default;
336
+ doc[key] = freshDefault(typeof props.default === "function" ? props.default(data) : props.default);
337
+ } else if (props.isClass && props.isScalar && !props.nullable) {
338
+
339
+ doc[key] = getDefault((props.modelRef as { [FIELD_META]: FieldMap })[FIELD_META] as never);
335
340
  } else if (!props.nullable && !["removedAt"].includes(key)) {
336
341
  if (["id", "createdAt", "updatedAt"].includes(key)) continue;
337
342
  throw new Error(`Missing required field: ${key}`);
@@ -597,9 +602,12 @@ export class SqlDocumentStore {
597
602
  if (value === undefined) {
598
603
  const def = props.default;
599
604
  if (def != null) {
600
- result[key] = typeof def === "function" ? (def as (data: unknown) => unknown)(payload) : def;
605
+ result[key] = freshDefault(typeof def === "function" ? (def as (data: unknown) => unknown)(payload) : def);
601
606
  } else if (props.nullable) {
602
607
  result[key] = null;
608
+ } else if (props.isClass && props.isScalar) {
609
+
610
+ result[key] = getDefault((props.modelRef as { [FIELD_META]: FieldMap })[FIELD_META] as never);
603
611
  } else {
604
612
  result[key] =
605
613
  ((props as Record<string, unknown>).modelRef as { [DEFAULT_VALUE]?: unknown })?.[DEFAULT_VALUE] ?? null;