|
| 1 | +// Issue #203 — the `insertPreserving` escape hatch (TS parity with the other 4 ports). |
| 2 | +// |
| 3 | +// For an entity that declares @autoSet fields, codegen emits an additional |
| 4 | +// `<Entity>InsertPreservingSchema` (the @autoSet columns validated VERBATIM, WITHOUT |
| 5 | +// the create-time `now()` transform) plus an `insertPreserving<Entity>(db, data)` |
| 6 | +// query — the import / restore / replication path that keeps the original timestamps. |
| 7 | +// Emitted ONLY when the entity has @autoSet fields; a plain entity is byte-identical. |
| 8 | + |
| 9 | +import { describe, test, expect } from "bun:test"; |
| 10 | +import type { MetaObject } from "@metaobjectsdev/metadata"; |
| 11 | +import { TypeId, TYPE_IDENTITY, |
| 12 | + FIELD_SUBTYPE_LONG, FIELD_SUBTYPE_STRING, FIELD_SUBTYPE_TIMESTAMP, |
| 13 | + IDENTITY_SUBTYPE_PRIMARY, OBJECT_SUBTYPE_ENTITY } from "@metaobjectsdev/metadata"; |
| 14 | +import { meta, metaRoot, metaObject, metaField } from "../_meta-build.js"; |
| 15 | +import { renderZodValidators, hasAutoSetFields } from "../../src/templates/zod-validators.js"; |
| 16 | +import { renderInsertPreservingFn } from "../../src/templates/queries.js"; |
| 17 | +import { renderQueriesFile } from "../../src/templates/queries-file.js"; |
| 18 | +import { makeRenderContext } from "../../src/render-context.js"; |
| 19 | +import { buildPkMap } from "../../src/pk-resolver.js"; |
| 20 | +import { buildRelationMap } from "../../src/relation-resolver.js"; |
| 21 | + |
| 22 | +/** Entity WITH @autoSet fields (createdAt onCreate + updatedAt onUpdate). */ |
| 23 | +function makeAutoSetEntity(): MetaObject { |
| 24 | + const entity = metaObject(OBJECT_SUBTYPE_ENTITY, "Event"); |
| 25 | + const id = metaField(FIELD_SUBTYPE_LONG, "id"); |
| 26 | + entity.addChild(id); |
| 27 | + const name = metaField(FIELD_SUBTYPE_STRING, "name"); |
| 28 | + name.setAttr("required", true); |
| 29 | + entity.addChild(name); |
| 30 | + const createdAt = metaField(FIELD_SUBTYPE_TIMESTAMP, "createdAt"); |
| 31 | + createdAt.setAttr("autoSet", "onCreate"); |
| 32 | + entity.addChild(createdAt); |
| 33 | + const updatedAt = metaField(FIELD_SUBTYPE_TIMESTAMP, "updatedAt"); |
| 34 | + updatedAt.setAttr("autoSet", "onUpdate"); |
| 35 | + entity.addChild(updatedAt); |
| 36 | + const primary = meta(new TypeId(TYPE_IDENTITY, IDENTITY_SUBTYPE_PRIMARY), "primary"); |
| 37 | + primary.setAttr("fields", ["id"]); |
| 38 | + primary.setAttr("generation", "increment"); |
| 39 | + entity.addChild(primary); |
| 40 | + return entity; |
| 41 | +} |
| 42 | + |
| 43 | +/** Plain entity, NO @autoSet field. */ |
| 44 | +function makePlainEntity(): MetaObject { |
| 45 | + const entity = metaObject(OBJECT_SUBTYPE_ENTITY, "Post"); |
| 46 | + const id = metaField(FIELD_SUBTYPE_LONG, "id"); |
| 47 | + entity.addChild(id); |
| 48 | + const title = metaField(FIELD_SUBTYPE_STRING, "title"); |
| 49 | + title.setAttr("required", true); |
| 50 | + entity.addChild(title); |
| 51 | + const primary = meta(new TypeId(TYPE_IDENTITY, IDENTITY_SUBTYPE_PRIMARY), "primary"); |
| 52 | + primary.setAttr("fields", ["id"]); |
| 53 | + primary.setAttr("generation", "increment"); |
| 54 | + entity.addChild(primary); |
| 55 | + return entity; |
| 56 | +} |
| 57 | + |
| 58 | +function makeCtx(obj: MetaObject) { |
| 59 | + const root = metaRoot(); |
| 60 | + root.addChild(obj); |
| 61 | + return makeRenderContext({ |
| 62 | + dialect: "sqlite", |
| 63 | + loadedRoot: root, |
| 64 | + outDir: "/x", |
| 65 | + dbImport: "~/server/db", |
| 66 | + pkMap: buildPkMap(root), |
| 67 | + relationMap: buildRelationMap(root), |
| 68 | + }); |
| 69 | +} |
| 70 | + |
| 71 | +describe("hasAutoSetFields", () => { |
| 72 | + test("true when an entity declares an @autoSet field", () => { |
| 73 | + expect(hasAutoSetFields(makeAutoSetEntity())).toBe(true); |
| 74 | + }); |
| 75 | + test("false for a plain entity", () => { |
| 76 | + expect(hasAutoSetFields(makePlainEntity())).toBe(false); |
| 77 | + }); |
| 78 | +}); |
| 79 | + |
| 80 | +describe("InsertPreservingSchema (emitted only for @autoSet entities)", () => { |
| 81 | + test("@autoSet entity emits <Entity>InsertPreservingSchema", () => { |
| 82 | + const out = renderZodValidators(makeAutoSetEntity()).toString(); |
| 83 | + expect(out).toContain("export const EventInsertPreservingSchema"); |
| 84 | + }); |
| 85 | + |
| 86 | + test("@autoSet columns are VERBATIM in the preserving schema — no now() transform", () => { |
| 87 | + const out = renderZodValidators(makeAutoSetEntity()).toString(); |
| 88 | + const preserving = out.split("export const EventInsertPreservingSchema")[1] ?? ""; |
| 89 | + // createdAt + updatedAt appear, but NOT with the create-time now() transform. |
| 90 | + expect(preserving).toContain("createdAt:"); |
| 91 | + expect(preserving).toContain("updatedAt:"); |
| 92 | + expect(preserving).not.toContain("new Date().toISOString()"); |
| 93 | + }); |
| 94 | + |
| 95 | + test("plain entity emits NO preserving schema", () => { |
| 96 | + const out = renderZodValidators(makePlainEntity()).toString(); |
| 97 | + expect(out).not.toContain("InsertPreservingSchema"); |
| 98 | + }); |
| 99 | +}); |
| 100 | + |
| 101 | +describe("renderInsertPreservingFn", () => { |
| 102 | + test("emits insertPreserving<Entity> parsing the preserving schema and inserting verbatim", () => { |
| 103 | + const entity = makeAutoSetEntity(); |
| 104 | + const out = renderInsertPreservingFn(entity, makeCtx(entity)).toString(); |
| 105 | + expect(out).toContain("insertPreservingEvent"); |
| 106 | + expect(out).toContain("EventInsertPreservingSchema.parse(data)"); |
| 107 | + expect(out).toContain(".insert("); |
| 108 | + expect(out).toContain(".returning()"); |
| 109 | + }); |
| 110 | +}); |
| 111 | + |
| 112 | +describe("renderQueriesFile insertPreserving gating", () => { |
| 113 | + test("@autoSet entity's queries file contains insertPreserving + imports the preserving schema", () => { |
| 114 | + const entity = makeAutoSetEntity(); |
| 115 | + const out = renderQueriesFile(entity, makeCtx(entity)); |
| 116 | + expect(out).toContain("insertPreservingEvent"); |
| 117 | + expect(out).toContain("EventInsertPreservingSchema"); |
| 118 | + }); |
| 119 | + |
| 120 | + test("plain entity's queries file has NO insertPreserving", () => { |
| 121 | + const entity = makePlainEntity(); |
| 122 | + const out = renderQueriesFile(entity, makeCtx(entity)); |
| 123 | + expect(out).not.toContain("insertPreserving"); |
| 124 | + expect(out).not.toContain("InsertPreservingSchema"); |
| 125 | + }); |
| 126 | +}); |
0 commit comments