@prisma-next/mongo-emitter 0.11.0-dev.67 → 0.11.0-dev.69

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.
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.mts","names":[],"sources":["../../src/index.ts"],"mappings":";;;;cAyEa,aAAA;;0BAGa,QAAA,EAAQ,IAAA,EAAQ,iBAAA;8BAuCZ,QAAA;gCAsGE,QAAA,EAAQ,mBAAA;+CAMK,KAAA,EAAS,aAAA;;;;+CAuCT,YAAA;AAAA"}
1
+ {"version":3,"file":"index.d.mts","names":[],"sources":["../../src/index.ts"],"mappings":";;;;cAyEa,aAAA;;0BAGa,QAAA,EAAQ,IAAA,EAAQ,iBAAA;8BA6CZ,QAAA;gCAyGE,QAAA,EAAQ,mBAAA;+CAMK,KAAA,EAAS,aAAA;;;;+CAuCT,YAAA;AAAA"}
@@ -38,14 +38,20 @@ const mongoEmission = {
38
38
  id: "mongo",
39
39
  validateTypes(contract, _ctx) {
40
40
  const typeIdRegex = /^([^/]+)\/([^@]+)@(\d+)$/;
41
- for (const [modelName, model] of Object.entries(contract.models)) for (const [fieldName, field] of Object.entries(model.fields)) {
42
- const fieldType = field.type;
43
- if (!fieldType) continue;
44
- const scalarTypes = fieldType.kind === "scalar" ? [fieldType] : fieldType.kind === "union" && fieldType.members ? fieldType.members.filter((m) => m.kind === "scalar") : [];
45
- for (const scalarType of scalarTypes) {
46
- const { codecId } = scalarType;
47
- if (!codecId) throw new Error(`Field "${fieldName}" on model "${modelName}" is missing codecId`);
48
- if (!codecId.match(typeIdRegex)?.[1]) throw new Error(`Field "${fieldName}" on model "${modelName}" has invalid codec ID format "${codecId}". Expected format: ns/name@version`);
41
+ for (const [namespaceId, domainNs] of Object.entries(contract.domain.namespaces)) {
42
+ const models = domainNs.models;
43
+ for (const [modelName, model] of Object.entries(models)) {
44
+ const qualifiedName = `${namespaceId}:${modelName}`;
45
+ for (const [fieldName, field] of Object.entries(model.fields)) {
46
+ const fieldType = field.type;
47
+ if (!fieldType) continue;
48
+ const scalarTypes = fieldType.kind === "scalar" ? [fieldType] : fieldType.kind === "union" && fieldType.members ? fieldType.members.filter((m) => m.kind === "scalar") : [];
49
+ for (const scalarType of scalarTypes) {
50
+ const { codecId } = scalarType;
51
+ if (!codecId) throw new Error(`Field "${fieldName}" on model "${qualifiedName}" is missing codecId`);
52
+ if (!codecId.match(typeIdRegex)?.[1]) throw new Error(`Field "${fieldName}" on model "${qualifiedName}" has invalid codec ID format "${codecId}". Expected format: ns/name@version`);
53
+ }
54
+ }
49
55
  }
50
56
  }
51
57
  },
@@ -56,35 +62,38 @@ const mongoEmission = {
56
62
  assertUniqueMongoCollectionNames(storage);
57
63
  const collectionNames = /* @__PURE__ */ new Set();
58
64
  for (const ns of Object.values(storage.namespaces)) for (const c of Object.keys(ns.collections)) collectionNames.add(c);
59
- const models = contract.models;
60
- if (!models || Object.keys(models).length === 0) return;
61
- for (const [modelName, model] of Object.entries(models)) {
62
- if (!model.fields || typeof model.fields !== "object") throw new Error(`Model "${modelName}" is missing required field "fields"`);
63
- if (!model.relations || typeof model.relations !== "object") throw new Error(`Model "${modelName}" is missing required field "relations" (must be an object)`);
64
- if (!model.storage || typeof model.storage !== "object") throw new Error(`Model "${modelName}" is missing required field "storage" (must be an object)`);
65
- const collectionValue = model.storage["collection"];
66
- const collection = typeof collectionValue === "string" ? collectionValue : void 0;
67
- if (model.owner) {
68
- if (collection) throw new Error(`Owned model "${modelName}" must not have storage.collection (embedded models are stored within their owner)`);
69
- if (!models[model.owner]) throw new Error(`Model "${modelName}" declares owner "${model.owner}" which does not exist in models`);
70
- } else if (collection) {
71
- if (!collectionNames.has(collection)) throw new Error(`Model "${modelName}" references collection "${collection}" which is not in storage.namespaces[..].collections`);
72
- }
73
- if (model.base) {
74
- const baseModel = models[model.base.model];
75
- if (!baseModel) throw new Error(`Model "${modelName}" declares base "${model.base.model}" which does not exist in models`);
76
- const variantCollection = collection;
77
- const baseCollection = baseModel.storage["collection"];
78
- if (variantCollection !== baseCollection) throw new Error(`Variant "${modelName}" must share its base's collection ("${baseCollection ?? "(none)"}"), but has "${variantCollection ?? "(none)"}"`);
79
- }
80
- const storageRelations = model.storage["relations"];
81
- if (storageRelations) {
82
- for (const relName of Object.keys(storageRelations)) if (!model.relations[relName]) throw new Error(`Model "${modelName}" has storage.relations.${relName} but no matching domain-level relation`);
83
- }
84
- for (const [relName, rel] of Object.entries(model.relations)) {
85
- const targetModelName = rel["to"]?.model;
86
- if (targetModelName) {
87
- if (models[targetModelName]?.owner === modelName && !storageRelations?.[relName]) throw new Error(`Model "${modelName}" has embed relation "${relName}" to owned model "${targetModelName}" but no matching storage.relations entry`);
65
+ for (const [namespaceId, domainNs] of Object.entries(contract.domain.namespaces)) {
66
+ const models = domainNs.models;
67
+ if (Object.keys(models).length === 0) continue;
68
+ for (const [modelName, model] of Object.entries(models)) {
69
+ const qualifiedName = `${namespaceId}:${modelName}`;
70
+ if (!model.fields || typeof model.fields !== "object") throw new Error(`Model "${qualifiedName}" is missing required field "fields"`);
71
+ if (!model.relations || typeof model.relations !== "object") throw new Error(`Model "${qualifiedName}" is missing required field "relations" (must be an object)`);
72
+ if (!model.storage || typeof model.storage !== "object") throw new Error(`Model "${qualifiedName}" is missing required field "storage" (must be an object)`);
73
+ const collectionValue = model.storage["collection"];
74
+ const collection = typeof collectionValue === "string" ? collectionValue : void 0;
75
+ if (model.owner) {
76
+ if (collection) throw new Error(`Owned model "${qualifiedName}" must not have storage.collection (embedded models are stored within their owner)`);
77
+ if (!models[model.owner]) throw new Error(`Model "${qualifiedName}" declares owner "${model.owner}" which does not exist in models`);
78
+ } else if (collection) {
79
+ if (!collectionNames.has(collection)) throw new Error(`Model "${qualifiedName}" references collection "${collection}" which is not in storage.namespaces[..].collections`);
80
+ }
81
+ if (model.base) {
82
+ const baseModel = models[model.base.model];
83
+ if (!baseModel) throw new Error(`Model "${qualifiedName}" declares base "${model.base.namespace}:${model.base.model}" which does not exist in models`);
84
+ const variantCollection = collection;
85
+ const baseCollection = baseModel.storage["collection"];
86
+ if (variantCollection !== baseCollection) throw new Error(`Variant "${qualifiedName}" must share its base's collection ("${baseCollection ?? "(none)"}"), but has "${variantCollection ?? "(none)"}"`);
87
+ }
88
+ const storageRelations = model.storage["relations"];
89
+ if (storageRelations) {
90
+ for (const relName of Object.keys(storageRelations)) if (!model.relations[relName]) throw new Error(`Model "${qualifiedName}" has storage.relations.${relName} but no matching domain-level relation`);
91
+ }
92
+ for (const [relName, rel] of Object.entries(model.relations)) {
93
+ const targetModelName = rel["to"]?.model;
94
+ if (targetModelName) {
95
+ if (models[targetModelName]?.owner === modelName && !storageRelations?.[relName]) throw new Error(`Model "${qualifiedName}" has embed relation "${relName}" to owned model "${targetModelName}" but no matching storage.relations entry`);
96
+ }
88
97
  }
89
98
  }
90
99
  }
@@ -1 +1 @@
1
- {"version":3,"file":"index.mjs","names":["relObj"],"sources":["../../src/index.ts"],"sourcesContent":["import type { Contract, ContractModel } from '@prisma-next/contract/types';\nimport { serializeObjectKey, serializeValue } from '@prisma-next/emitter/domain-type-generation';\nimport type { ValidationContext } from '@prisma-next/framework-components/emission';\nimport type { Namespace } from '@prisma-next/framework-components/ir';\nimport type { MongoCollection, MongoStorage } from '@prisma-next/mongo-contract';\n\nconst MONGO_NAMESPACE_KIND_FALLBACK = 'mongo-namespace' as const;\n\nfunction mongoNamespaceSerializedKind(ns: Namespace): string {\n const kind = (ns as { kind?: unknown }).kind;\n if (typeof kind === 'string') {\n return `readonly kind: ${serializeValue(kind)}`;\n }\n return `readonly kind: '${MONGO_NAMESPACE_KIND_FALLBACK}'`;\n}\n\nfunction assertUniqueMongoCollectionNames(storage: MongoStorage): void {\n const seen = new Map<string, string>();\n for (const [namespaceId, ns] of Object.entries(storage.namespaces)) {\n for (const coll of Object.keys(ns.collections)) {\n const existing = seen.get(coll);\n if (existing !== undefined && existing !== namespaceId) {\n throw new Error(\n `Duplicate collection name \"${coll}\" in namespaces \"${existing}\" and \"${namespaceId}\"`,\n );\n }\n seen.set(coll, namespaceId);\n }\n }\n}\n\nfunction generateMongoCollectionEntryType(coll: MongoCollection): string {\n const entries = Object.entries(coll).filter(([key, v]) => v !== undefined && key !== 'kind');\n if (entries.length === 0) {\n return 'MongoCollection';\n }\n return serializeValue(coll);\n}\n\nfunction generateMongoNamespaceCollectionsType(\n collections: Readonly<Record<string, MongoCollection>>,\n): string {\n const entries: string[] = [];\n for (const [collName, coll] of Object.entries(collections).sort(([a], [b]) =>\n a.localeCompare(b),\n )) {\n entries.push(\n `readonly ${serializeObjectKey(collName)}: ${generateMongoCollectionEntryType(coll)}`,\n );\n }\n if (entries.length === 0) {\n return 'Record<string, never>';\n }\n return `{ ${entries.join('; ')} }`;\n}\n\nfunction generateMongoNamespacesType(namespaces: MongoStorage['namespaces']): string {\n const sorted = Object.entries(namespaces ?? {}).sort(([a], [b]) => a.localeCompare(b));\n if (sorted.length === 0) {\n return 'Record<string, never>';\n }\n const parts: string[] = [];\n for (const [name, ns] of sorted) {\n const collectionsType = generateMongoNamespaceCollectionsType(\n ns.collections as Readonly<Record<string, MongoCollection>>,\n );\n parts.push(\n `readonly ${serializeObjectKey(name)}: { readonly id: ${serializeValue(ns.id)}; ${mongoNamespaceSerializedKind(ns)}; readonly collections: ${collectionsType} }`,\n );\n }\n return `{ ${parts.join('; ')} }`;\n}\n\nexport const mongoEmission = {\n id: 'mongo',\n\n validateTypes(contract: Contract, _ctx: ValidationContext): void {\n const typeIdRegex = /^([^/]+)\\/([^@]+)@(\\d+)$/;\n\n for (const [modelName, model] of Object.entries(contract.models)) {\n for (const [fieldName, field] of Object.entries(model.fields)) {\n const fieldType = (\n field as {\n type?: {\n kind: string;\n codecId?: string;\n members?: ReadonlyArray<{ kind: string; codecId?: string }>;\n };\n }\n ).type;\n if (!fieldType) continue;\n\n const scalarTypes: Array<{ codecId?: string }> =\n fieldType.kind === 'scalar'\n ? [fieldType]\n : fieldType.kind === 'union' && fieldType.members\n ? fieldType.members.filter((m) => m.kind === 'scalar')\n : [];\n\n for (const scalarType of scalarTypes) {\n const { codecId } = scalarType;\n if (!codecId) {\n throw new Error(`Field \"${fieldName}\" on model \"${modelName}\" is missing codecId`);\n }\n const match = codecId.match(typeIdRegex);\n if (!match?.[1]) {\n throw new Error(\n `Field \"${fieldName}\" on model \"${modelName}\" has invalid codec ID format \"${codecId}\". Expected format: ns/name@version`,\n );\n }\n }\n }\n }\n },\n\n validateStructure(contract: Contract): void {\n if (contract.targetFamily !== 'mongo') {\n throw new Error(`Expected targetFamily \"mongo\", got \"${contract.targetFamily}\"`);\n }\n\n const storage = contract.storage as MongoStorage | undefined;\n if (!storage?.namespaces || typeof storage.namespaces !== 'object') {\n throw new Error('Mongo contract must have storage.namespaces');\n }\n\n assertUniqueMongoCollectionNames(storage);\n\n const collectionNames = new Set<string>();\n for (const ns of Object.values(storage.namespaces)) {\n for (const c of Object.keys(ns.collections)) {\n collectionNames.add(c);\n }\n }\n\n const models = contract.models;\n if (!models || Object.keys(models).length === 0) return;\n\n for (const [modelName, model] of Object.entries(models)) {\n if (!model.fields || typeof model.fields !== 'object') {\n throw new Error(`Model \"${modelName}\" is missing required field \"fields\"`);\n }\n if (!model.relations || typeof model.relations !== 'object') {\n throw new Error(\n `Model \"${modelName}\" is missing required field \"relations\" (must be an object)`,\n );\n }\n if (!model.storage || typeof model.storage !== 'object') {\n throw new Error(\n `Model \"${modelName}\" is missing required field \"storage\" (must be an object)`,\n );\n }\n\n const collectionValue = model.storage['collection'];\n const collection = typeof collectionValue === 'string' ? collectionValue : undefined;\n\n if (model.owner) {\n if (collection) {\n throw new Error(\n `Owned model \"${modelName}\" must not have storage.collection (embedded models are stored within their owner)`,\n );\n }\n if (!models[model.owner]) {\n throw new Error(\n `Model \"${modelName}\" declares owner \"${model.owner}\" which does not exist in models`,\n );\n }\n } else if (collection) {\n if (!collectionNames.has(collection)) {\n throw new Error(\n `Model \"${modelName}\" references collection \"${collection}\" which is not in storage.namespaces[..].collections`,\n );\n }\n }\n\n if (model.base) {\n const baseModel = models[model.base.model];\n if (!baseModel) {\n throw new Error(\n `Model \"${modelName}\" declares base \"${model.base.model}\" which does not exist in models`,\n );\n }\n const variantCollection = collection;\n const baseCollection = baseModel.storage['collection'] as string | undefined;\n if (variantCollection !== baseCollection) {\n throw new Error(\n `Variant \"${modelName}\" must share its base's collection (\"${baseCollection ?? '(none)'}\"), but has \"${variantCollection ?? '(none)'}\"`,\n );\n }\n }\n\n const storageRelations = model.storage['relations'] as Record<string, unknown> | undefined;\n if (storageRelations) {\n for (const relName of Object.keys(storageRelations)) {\n if (!model.relations[relName]) {\n throw new Error(\n `Model \"${modelName}\" has storage.relations.${relName} but no matching domain-level relation`,\n );\n }\n }\n }\n\n for (const [relName, rel] of Object.entries(model.relations)) {\n const relObj = rel as Record<string, unknown>;\n const targetRef = relObj['to'] as { readonly model?: string } | undefined;\n const targetModelName = targetRef?.model;\n if (targetModelName) {\n const targetModel = models[targetModelName];\n if (targetModel?.owner === modelName && !storageRelations?.[relName]) {\n throw new Error(\n `Model \"${modelName}\" has embed relation \"${relName}\" to owned model \"${targetModelName}\" but no matching storage.relations entry`,\n );\n }\n }\n }\n }\n },\n\n generateStorageType(contract: Contract, storageHashTypeName: string): string {\n const storage = contract.storage as MongoStorage;\n const namespacesType = generateMongoNamespacesType(storage.namespaces);\n return `{ readonly namespaces: ${namespacesType}; readonly storageHash: ${storageHashTypeName} }`;\n },\n\n generateModelStorageType(_modelName: string, model: ContractModel): string {\n const parts: string[] = [];\n const collection = model.storage['collection'] as string | undefined;\n if (collection) {\n parts.push(`readonly collection: ${serializeValue(collection)}`);\n }\n\n const storageRelations = model.storage['relations'] as\n | Record<string, Record<string, unknown>>\n | undefined;\n if (storageRelations && Object.keys(storageRelations).length > 0) {\n const relEntries: string[] = [];\n for (const [relName, relVal] of Object.entries(storageRelations)) {\n relEntries.push(`readonly ${serializeObjectKey(relName)}: ${serializeValue(relVal)}`);\n }\n parts.push(`readonly relations: { ${relEntries.join('; ')} }`);\n }\n\n return parts.length > 0 ? `{ ${parts.join('; ')} }` : 'Record<string, never>';\n },\n\n getFamilyImports(): string[] {\n return [\n 'import type {',\n ' MongoCollection,',\n ' MongoContractWithTypeMaps,',\n ' MongoTypeMaps,',\n \"} from '@prisma-next/mongo-contract';\",\n ];\n },\n\n getFamilyTypeAliases(): string {\n return '';\n },\n\n getTypeMapsExpression(): string {\n return 'MongoTypeMaps<CodecTypes, FieldOutputTypes, FieldInputTypes>';\n },\n\n getContractWrapper(contractBaseName: string, typeMapsName: string): string {\n return `export type Contract = MongoContractWithTypeMaps<${contractBaseName}, ${typeMapsName}>;`;\n },\n};\n"],"mappings":";;AAMA,MAAM,gCAAgC;AAEtC,SAAS,6BAA6B,IAAuB;CAC3D,MAAM,OAAQ,GAA0B;CACxC,IAAI,OAAO,SAAS,UAClB,OAAO,kBAAkB,eAAe,IAAI;CAE9C,OAAO,mBAAmB,8BAA8B;AAC1D;AAEA,SAAS,iCAAiC,SAA6B;CACrE,MAAM,uBAAO,IAAI,IAAoB;CACrC,KAAK,MAAM,CAAC,aAAa,OAAO,OAAO,QAAQ,QAAQ,UAAU,GAC/D,KAAK,MAAM,QAAQ,OAAO,KAAK,GAAG,WAAW,GAAG;EAC9C,MAAM,WAAW,KAAK,IAAI,IAAI;EAC9B,IAAI,aAAa,KAAA,KAAa,aAAa,aACzC,MAAM,IAAI,MACR,8BAA8B,KAAK,mBAAmB,SAAS,SAAS,YAAY,EACtF;EAEF,KAAK,IAAI,MAAM,WAAW;CAC5B;AAEJ;AAEA,SAAS,iCAAiC,MAA+B;CAEvE,IADgB,OAAO,QAAQ,IAAI,EAAE,QAAQ,CAAC,KAAK,OAAO,MAAM,KAAA,KAAa,QAAQ,MAC3E,EAAE,WAAW,GACrB,OAAO;CAET,OAAO,eAAe,IAAI;AAC5B;AAEA,SAAS,sCACP,aACQ;CACR,MAAM,UAAoB,CAAC;CAC3B,KAAK,MAAM,CAAC,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K,MAAM,CAAC,SAAS,WAAW,OAAO,QAAQ,gBAAgB,GAC7D,WAAW,KAAK,YAAY,mBAAmB,OAAO,EAAE,IAAI,eAAe,MAAM,GAAG;GAEtF,MAAM,KAAK,yBAAyB,WAAW,KAAK,IAAI,EAAE,GAAG;EAC/D;EAEA,OAAO,MAAM,SAAS,IAAI,KAAK,MAAM,KAAK,IAAI,EAAE,MAAM;CACxD;CAEA,mBAA6B;EAC3B,OAAO;GACL;GACA;GACA;GACA;GACA;EACF;CACF;CAEA,uBAA+B;EAC7B,OAAO;CACT;CAEA,wBAAgC;EAC9B,OAAO;CACT;CAEA,mBAAmB,kBAA0B,cAA8B;EACzE,OAAO,oDAAoD,iBAAiB,IAAI,aAAa;CAC/F;AACF"}
1
+ {"version":3,"file":"index.mjs","names":["relObj"],"sources":["../../src/index.ts"],"sourcesContent":["import type { Contract, ContractModel } from '@prisma-next/contract/types';\nimport { serializeObjectKey, serializeValue } from '@prisma-next/emitter/domain-type-generation';\nimport type { ValidationContext } from '@prisma-next/framework-components/emission';\nimport type { Namespace } from '@prisma-next/framework-components/ir';\nimport type { MongoCollection, MongoStorage } from '@prisma-next/mongo-contract';\n\nconst MONGO_NAMESPACE_KIND_FALLBACK = 'mongo-namespace' as const;\n\nfunction mongoNamespaceSerializedKind(ns: Namespace): string {\n const kind = (ns as { kind?: unknown }).kind;\n if (typeof kind === 'string') {\n return `readonly kind: ${serializeValue(kind)}`;\n }\n return `readonly kind: '${MONGO_NAMESPACE_KIND_FALLBACK}'`;\n}\n\nfunction assertUniqueMongoCollectionNames(storage: MongoStorage): void {\n const seen = new Map<string, string>();\n for (const [namespaceId, ns] of Object.entries(storage.namespaces)) {\n for (const coll of Object.keys(ns.collections)) {\n const existing = seen.get(coll);\n if (existing !== undefined && existing !== namespaceId) {\n throw new Error(\n `Duplicate collection name \"${coll}\" in namespaces \"${existing}\" and \"${namespaceId}\"`,\n );\n }\n seen.set(coll, namespaceId);\n }\n }\n}\n\nfunction generateMongoCollectionEntryType(coll: MongoCollection): string {\n const entries = Object.entries(coll).filter(([key, v]) => v !== undefined && key !== 'kind');\n if (entries.length === 0) {\n return 'MongoCollection';\n }\n return serializeValue(coll);\n}\n\nfunction generateMongoNamespaceCollectionsType(\n collections: Readonly<Record<string, MongoCollection>>,\n): string {\n const entries: string[] = [];\n for (const [collName, coll] of Object.entries(collections).sort(([a], [b]) =>\n a.localeCompare(b),\n )) {\n entries.push(\n `readonly ${serializeObjectKey(collName)}: ${generateMongoCollectionEntryType(coll)}`,\n );\n }\n if (entries.length === 0) {\n return 'Record<string, never>';\n }\n return `{ ${entries.join('; ')} }`;\n}\n\nfunction generateMongoNamespacesType(namespaces: MongoStorage['namespaces']): string {\n const sorted = Object.entries(namespaces ?? {}).sort(([a], [b]) => a.localeCompare(b));\n if (sorted.length === 0) {\n return 'Record<string, never>';\n }\n const parts: string[] = [];\n for (const [name, ns] of sorted) {\n const collectionsType = generateMongoNamespaceCollectionsType(\n ns.collections as Readonly<Record<string, MongoCollection>>,\n );\n parts.push(\n `readonly ${serializeObjectKey(name)}: { readonly id: ${serializeValue(ns.id)}; ${mongoNamespaceSerializedKind(ns)}; readonly collections: ${collectionsType} }`,\n );\n }\n return `{ ${parts.join('; ')} }`;\n}\n\nexport const mongoEmission = {\n id: 'mongo',\n\n validateTypes(contract: Contract, _ctx: ValidationContext): void {\n const typeIdRegex = /^([^/]+)\\/([^@]+)@(\\d+)$/;\n\n for (const [namespaceId, domainNs] of Object.entries(contract.domain.namespaces)) {\n const models = domainNs.models as Record<string, ContractModel>;\n for (const [modelName, model] of Object.entries(models)) {\n const qualifiedName = `${namespaceId}:${modelName}`;\n for (const [fieldName, field] of Object.entries(model.fields)) {\n const fieldType = (\n field as {\n type?: {\n kind: string;\n codecId?: string;\n members?: ReadonlyArray<{ kind: string; codecId?: string }>;\n };\n }\n ).type;\n if (!fieldType) continue;\n\n const scalarTypes: Array<{ codecId?: string }> =\n fieldType.kind === 'scalar'\n ? [fieldType]\n : fieldType.kind === 'union' && fieldType.members\n ? fieldType.members.filter((m) => m.kind === 'scalar')\n : [];\n\n for (const scalarType of scalarTypes) {\n const { codecId } = scalarType;\n if (!codecId) {\n throw new Error(\n `Field \"${fieldName}\" on model \"${qualifiedName}\" is missing codecId`,\n );\n }\n const match = codecId.match(typeIdRegex);\n if (!match?.[1]) {\n throw new Error(\n `Field \"${fieldName}\" on model \"${qualifiedName}\" has invalid codec ID format \"${codecId}\". Expected format: ns/name@version`,\n );\n }\n }\n }\n }\n }\n },\n\n validateStructure(contract: Contract): void {\n if (contract.targetFamily !== 'mongo') {\n throw new Error(`Expected targetFamily \"mongo\", got \"${contract.targetFamily}\"`);\n }\n\n const storage = contract.storage as MongoStorage | undefined;\n if (!storage?.namespaces || typeof storage.namespaces !== 'object') {\n throw new Error('Mongo contract must have storage.namespaces');\n }\n\n assertUniqueMongoCollectionNames(storage);\n\n const collectionNames = new Set<string>();\n for (const ns of Object.values(storage.namespaces)) {\n for (const c of Object.keys(ns.collections)) {\n collectionNames.add(c);\n }\n }\n\n for (const [namespaceId, domainNs] of Object.entries(contract.domain.namespaces)) {\n const models = domainNs.models as Record<string, ContractModel>;\n if (Object.keys(models).length === 0) continue;\n\n for (const [modelName, model] of Object.entries(models)) {\n const qualifiedName = `${namespaceId}:${modelName}`;\n if (!model.fields || typeof model.fields !== 'object') {\n throw new Error(`Model \"${qualifiedName}\" is missing required field \"fields\"`);\n }\n if (!model.relations || typeof model.relations !== 'object') {\n throw new Error(\n `Model \"${qualifiedName}\" is missing required field \"relations\" (must be an object)`,\n );\n }\n if (!model.storage || typeof model.storage !== 'object') {\n throw new Error(\n `Model \"${qualifiedName}\" is missing required field \"storage\" (must be an object)`,\n );\n }\n\n const collectionValue = model.storage['collection'];\n const collection = typeof collectionValue === 'string' ? collectionValue : undefined;\n\n if (model.owner) {\n if (collection) {\n throw new Error(\n `Owned model \"${qualifiedName}\" must not have storage.collection (embedded models are stored within their owner)`,\n );\n }\n if (!models[model.owner]) {\n throw new Error(\n `Model \"${qualifiedName}\" declares owner \"${model.owner}\" which does not exist in models`,\n );\n }\n } else if (collection) {\n if (!collectionNames.has(collection)) {\n throw new Error(\n `Model \"${qualifiedName}\" references collection \"${collection}\" which is not in storage.namespaces[..].collections`,\n );\n }\n }\n\n if (model.base) {\n const baseModel = models[model.base.model];\n if (!baseModel) {\n throw new Error(\n `Model \"${qualifiedName}\" declares base \"${model.base.namespace}:${model.base.model}\" which does not exist in models`,\n );\n }\n const variantCollection = collection;\n const baseCollection = baseModel.storage['collection'] as string | undefined;\n if (variantCollection !== baseCollection) {\n throw new Error(\n `Variant \"${qualifiedName}\" must share its base's collection (\"${baseCollection ?? '(none)'}\"), but has \"${variantCollection ?? '(none)'}\"`,\n );\n }\n }\n\n const storageRelations = model.storage['relations'] as Record<string, unknown> | undefined;\n if (storageRelations) {\n for (const relName of Object.keys(storageRelations)) {\n if (!model.relations[relName]) {\n throw new Error(\n `Model \"${qualifiedName}\" has storage.relations.${relName} but no matching domain-level relation`,\n );\n }\n }\n }\n\n for (const [relName, rel] of Object.entries(model.relations)) {\n const relObj = rel as Record<string, unknown>;\n const targetRef = relObj['to'] as { readonly model?: string } | undefined;\n const targetModelName = targetRef?.model;\n if (targetModelName) {\n const targetModel = models[targetModelName];\n if (targetModel?.owner === modelName && !storageRelations?.[relName]) {\n throw new Error(\n `Model \"${qualifiedName}\" has embed relation \"${relName}\" to owned model \"${targetModelName}\" but no matching storage.relations entry`,\n );\n }\n }\n }\n }\n }\n },\n\n generateStorageType(contract: Contract, storageHashTypeName: string): string {\n const storage = contract.storage as MongoStorage;\n const namespacesType = generateMongoNamespacesType(storage.namespaces);\n return `{ readonly namespaces: ${namespacesType}; readonly storageHash: ${storageHashTypeName} }`;\n },\n\n generateModelStorageType(_modelName: string, model: ContractModel): string {\n const parts: string[] = [];\n const collection = model.storage['collection'] as string | undefined;\n if (collection) {\n parts.push(`readonly collection: ${serializeValue(collection)}`);\n }\n\n const storageRelations = model.storage['relations'] as\n | Record<string, Record<string, unknown>>\n | undefined;\n if (storageRelations && Object.keys(storageRelations).length > 0) {\n const relEntries: string[] = [];\n for (const [relName, relVal] of Object.entries(storageRelations)) {\n relEntries.push(`readonly ${serializeObjectKey(relName)}: ${serializeValue(relVal)}`);\n }\n parts.push(`readonly relations: { ${relEntries.join('; ')} }`);\n }\n\n return parts.length > 0 ? `{ ${parts.join('; ')} }` : 'Record<string, never>';\n },\n\n getFamilyImports(): string[] {\n return [\n 'import type {',\n ' MongoCollection,',\n ' MongoContractWithTypeMaps,',\n ' MongoTypeMaps,',\n \"} from '@prisma-next/mongo-contract';\",\n ];\n },\n\n getFamilyTypeAliases(): string {\n return '';\n },\n\n getTypeMapsExpression(): string {\n return 'MongoTypeMaps<CodecTypes, FieldOutputTypes, FieldInputTypes>';\n },\n\n getContractWrapper(contractBaseName: string, typeMapsName: string): string {\n return `export type Contract = MongoContractWithTypeMaps<${contractBaseName}, ${typeMapsName}>;`;\n },\n};\n"],"mappings":";;AAMA,MAAM,gCAAgC;AAEtC,SAAS,6BAA6B,IAAuB;CAC3D,MAAM,OAAQ,GAA0B;CACxC,IAAI,OAAO,SAAS,UAClB,OAAO,kBAAkB,eAAe,IAAI;CAE9C,OAAO,mBAAmB,8BAA8B;AAC1D;AAEA,SAAS,iCAAiC,SAA6B;CACrE,MAAM,uBAAO,IAAI,IAAoB;CACrC,KAAK,MAAM,CAAC,aAAa,OAAO,OAAO,QAAQ,QAAQ,UAAU,GAC/D,KAAK,MAAM,QAAQ,OAAO,KAAK,GAAG,WAAW,GAAG;EAC9C,MAAM,WAAW,KAAK,IAAI,IAAI;EAC9B,IAAI,aAAa,KAAA,KAAa,aAAa,aACzC,MAAM,IAAI,MACR,8BAA8B,KAAK,mBAAmB,SAAS,SAAS,YAAY,EACtF;EAEF,KAAK,IAAI,MAAM,WAAW;CAC5B;AAEJ;AAEA,SAAS,iCAAiC,MAA+B;CAEvE,IADgB,OAAO,QAAQ,IAAI,EAAE,QAAQ,CAAC,K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,gBAAgB,0CAC9F;KAAA;IAGN;GACF;EACF;CACF;CAEA,oBAAoB,UAAoB,qBAAqC;EAC3E,MAAM,UAAU,SAAS;EAEzB,OAAO,0BADgB,4BAA4B,QAAQ,UACb,EAAE,0BAA0B,oBAAoB;CAChG;CAEA,yBAAyB,YAAoB,OAA8B;EACzE,MAAM,QAAkB,CAAC;EACzB,MAAM,aAAa,MAAM,QAAQ;EACjC,IAAI,YACF,MAAM,KAAK,wBAAwB,eAAe,UAAU,GAAG;EAGjE,MAAM,mBAAmB,MAAM,QAAQ;EAGvC,IAAI,oBAAoB,OAAO,KAAK,gBAAgB,EAAE,SAAS,GAAG;GAChE,MAAM,aAAuB,CAAC;GAC9B,KAAK,MAAM,CAAC,SAAS,WAAW,OAAO,QAAQ,gBAAgB,GAC7D,WAAW,KAAK,YAAY,mBAAmB,OAAO,EAAE,IAAI,eAAe,MAAM,GAAG;GAEtF,MAAM,KAAK,yBAAyB,WAAW,KAAK,IAAI,EAAE,GAAG;EAC/D;EAEA,OAAO,MAAM,SAAS,IAAI,KAAK,MAAM,KAAK,IAAI,EAAE,MAAM;CACxD;CAEA,mBAA6B;EAC3B,OAAO;GACL;GACA;GACA;GACA;GACA;EACF;CACF;CAEA,uBAA+B;EAC7B,OAAO;CACT;CAEA,wBAAgC;EAC9B,OAAO;CACT;CAEA,mBAAmB,kBAA0B,cAA8B;EACzE,OAAO,oDAAoD,iBAAiB,IAAI,aAAa;CAC/F;AACF"}
package/package.json CHANGED
@@ -1,20 +1,20 @@
1
1
  {
2
2
  "name": "@prisma-next/mongo-emitter",
3
- "version": "0.11.0-dev.67",
3
+ "version": "0.11.0-dev.69",
4
4
  "license": "Apache-2.0",
5
5
  "type": "module",
6
6
  "sideEffects": false,
7
7
  "description": "Mongo emitter hook for Prisma Next",
8
8
  "dependencies": {
9
- "@prisma-next/contract": "0.11.0-dev.67",
10
- "@prisma-next/emitter": "0.11.0-dev.67",
11
- "@prisma-next/framework-components": "0.11.0-dev.67",
12
- "@prisma-next/mongo-contract": "0.11.0-dev.67"
9
+ "@prisma-next/contract": "0.11.0-dev.69",
10
+ "@prisma-next/emitter": "0.11.0-dev.69",
11
+ "@prisma-next/framework-components": "0.11.0-dev.69",
12
+ "@prisma-next/mongo-contract": "0.11.0-dev.69"
13
13
  },
14
14
  "devDependencies": {
15
- "@prisma-next/test-utils": "0.11.0-dev.67",
16
- "@prisma-next/tsconfig": "0.11.0-dev.67",
17
- "@prisma-next/tsdown": "0.11.0-dev.67",
15
+ "@prisma-next/test-utils": "0.11.0-dev.69",
16
+ "@prisma-next/tsconfig": "0.11.0-dev.69",
17
+ "@prisma-next/tsdown": "0.11.0-dev.69",
18
18
  "tsdown": "0.22.0",
19
19
  "typescript": "5.9.3",
20
20
  "vitest": "4.1.6"
package/src/index.ts CHANGED
@@ -77,36 +77,42 @@ export const mongoEmission = {
77
77
  validateTypes(contract: Contract, _ctx: ValidationContext): void {
78
78
  const typeIdRegex = /^([^/]+)\/([^@]+)@(\d+)$/;
79
79
 
80
- for (const [modelName, model] of Object.entries(contract.models)) {
81
- for (const [fieldName, field] of Object.entries(model.fields)) {
82
- const fieldType = (
83
- field as {
84
- type?: {
85
- kind: string;
86
- codecId?: string;
87
- members?: ReadonlyArray<{ kind: string; codecId?: string }>;
88
- };
89
- }
90
- ).type;
91
- if (!fieldType) continue;
80
+ for (const [namespaceId, domainNs] of Object.entries(contract.domain.namespaces)) {
81
+ const models = domainNs.models as Record<string, ContractModel>;
82
+ for (const [modelName, model] of Object.entries(models)) {
83
+ const qualifiedName = `${namespaceId}:${modelName}`;
84
+ for (const [fieldName, field] of Object.entries(model.fields)) {
85
+ const fieldType = (
86
+ field as {
87
+ type?: {
88
+ kind: string;
89
+ codecId?: string;
90
+ members?: ReadonlyArray<{ kind: string; codecId?: string }>;
91
+ };
92
+ }
93
+ ).type;
94
+ if (!fieldType) continue;
92
95
 
93
- const scalarTypes: Array<{ codecId?: string }> =
94
- fieldType.kind === 'scalar'
95
- ? [fieldType]
96
- : fieldType.kind === 'union' && fieldType.members
97
- ? fieldType.members.filter((m) => m.kind === 'scalar')
98
- : [];
96
+ const scalarTypes: Array<{ codecId?: string }> =
97
+ fieldType.kind === 'scalar'
98
+ ? [fieldType]
99
+ : fieldType.kind === 'union' && fieldType.members
100
+ ? fieldType.members.filter((m) => m.kind === 'scalar')
101
+ : [];
99
102
 
100
- for (const scalarType of scalarTypes) {
101
- const { codecId } = scalarType;
102
- if (!codecId) {
103
- throw new Error(`Field "${fieldName}" on model "${modelName}" is missing codecId`);
104
- }
105
- const match = codecId.match(typeIdRegex);
106
- if (!match?.[1]) {
107
- throw new Error(
108
- `Field "${fieldName}" on model "${modelName}" has invalid codec ID format "${codecId}". Expected format: ns/name@version`,
109
- );
103
+ for (const scalarType of scalarTypes) {
104
+ const { codecId } = scalarType;
105
+ if (!codecId) {
106
+ throw new Error(
107
+ `Field "${fieldName}" on model "${qualifiedName}" is missing codecId`,
108
+ );
109
+ }
110
+ const match = codecId.match(typeIdRegex);
111
+ if (!match?.[1]) {
112
+ throw new Error(
113
+ `Field "${fieldName}" on model "${qualifiedName}" has invalid codec ID format "${codecId}". Expected format: ns/name@version`,
114
+ );
115
+ }
110
116
  }
111
117
  }
112
118
  }
@@ -132,84 +138,87 @@ export const mongoEmission = {
132
138
  }
133
139
  }
134
140
 
135
- const models = contract.models;
136
- if (!models || Object.keys(models).length === 0) return;
137
-
138
- for (const [modelName, model] of Object.entries(models)) {
139
- if (!model.fields || typeof model.fields !== 'object') {
140
- throw new Error(`Model "${modelName}" is missing required field "fields"`);
141
- }
142
- if (!model.relations || typeof model.relations !== 'object') {
143
- throw new Error(
144
- `Model "${modelName}" is missing required field "relations" (must be an object)`,
145
- );
146
- }
147
- if (!model.storage || typeof model.storage !== 'object') {
148
- throw new Error(
149
- `Model "${modelName}" is missing required field "storage" (must be an object)`,
150
- );
151
- }
152
-
153
- const collectionValue = model.storage['collection'];
154
- const collection = typeof collectionValue === 'string' ? collectionValue : undefined;
141
+ for (const [namespaceId, domainNs] of Object.entries(contract.domain.namespaces)) {
142
+ const models = domainNs.models as Record<string, ContractModel>;
143
+ if (Object.keys(models).length === 0) continue;
155
144
 
156
- if (model.owner) {
157
- if (collection) {
158
- throw new Error(
159
- `Owned model "${modelName}" must not have storage.collection (embedded models are stored within their owner)`,
160
- );
145
+ for (const [modelName, model] of Object.entries(models)) {
146
+ const qualifiedName = `${namespaceId}:${modelName}`;
147
+ if (!model.fields || typeof model.fields !== 'object') {
148
+ throw new Error(`Model "${qualifiedName}" is missing required field "fields"`);
161
149
  }
162
- if (!models[model.owner]) {
150
+ if (!model.relations || typeof model.relations !== 'object') {
163
151
  throw new Error(
164
- `Model "${modelName}" declares owner "${model.owner}" which does not exist in models`,
152
+ `Model "${qualifiedName}" is missing required field "relations" (must be an object)`,
165
153
  );
166
154
  }
167
- } else if (collection) {
168
- if (!collectionNames.has(collection)) {
155
+ if (!model.storage || typeof model.storage !== 'object') {
169
156
  throw new Error(
170
- `Model "${modelName}" references collection "${collection}" which is not in storage.namespaces[..].collections`,
157
+ `Model "${qualifiedName}" is missing required field "storage" (must be an object)`,
171
158
  );
172
159
  }
173
- }
174
160
 
175
- if (model.base) {
176
- const baseModel = models[model.base.model];
177
- if (!baseModel) {
178
- throw new Error(
179
- `Model "${modelName}" declares base "${model.base.model}" which does not exist in models`,
180
- );
181
- }
182
- const variantCollection = collection;
183
- const baseCollection = baseModel.storage['collection'] as string | undefined;
184
- if (variantCollection !== baseCollection) {
185
- throw new Error(
186
- `Variant "${modelName}" must share its base's collection ("${baseCollection ?? '(none)'}"), but has "${variantCollection ?? '(none)'}"`,
187
- );
188
- }
189
- }
161
+ const collectionValue = model.storage['collection'];
162
+ const collection = typeof collectionValue === 'string' ? collectionValue : undefined;
190
163
 
191
- const storageRelations = model.storage['relations'] as Record<string, unknown> | undefined;
192
- if (storageRelations) {
193
- for (const relName of Object.keys(storageRelations)) {
194
- if (!model.relations[relName]) {
164
+ if (model.owner) {
165
+ if (collection) {
166
+ throw new Error(
167
+ `Owned model "${qualifiedName}" must not have storage.collection (embedded models are stored within their owner)`,
168
+ );
169
+ }
170
+ if (!models[model.owner]) {
195
171
  throw new Error(
196
- `Model "${modelName}" has storage.relations.${relName} but no matching domain-level relation`,
172
+ `Model "${qualifiedName}" declares owner "${model.owner}" which does not exist in models`,
173
+ );
174
+ }
175
+ } else if (collection) {
176
+ if (!collectionNames.has(collection)) {
177
+ throw new Error(
178
+ `Model "${qualifiedName}" references collection "${collection}" which is not in storage.namespaces[..].collections`,
197
179
  );
198
180
  }
199
181
  }
200
- }
201
182
 
202
- for (const [relName, rel] of Object.entries(model.relations)) {
203
- const relObj = rel as Record<string, unknown>;
204
- const targetRef = relObj['to'] as { readonly model?: string } | undefined;
205
- const targetModelName = targetRef?.model;
206
- if (targetModelName) {
207
- const targetModel = models[targetModelName];
208
- if (targetModel?.owner === modelName && !storageRelations?.[relName]) {
183
+ if (model.base) {
184
+ const baseModel = models[model.base.model];
185
+ if (!baseModel) {
209
186
  throw new Error(
210
- `Model "${modelName}" has embed relation "${relName}" to owned model "${targetModelName}" but no matching storage.relations entry`,
187
+ `Model "${qualifiedName}" declares base "${model.base.namespace}:${model.base.model}" which does not exist in models`,
211
188
  );
212
189
  }
190
+ const variantCollection = collection;
191
+ const baseCollection = baseModel.storage['collection'] as string | undefined;
192
+ if (variantCollection !== baseCollection) {
193
+ throw new Error(
194
+ `Variant "${qualifiedName}" must share its base's collection ("${baseCollection ?? '(none)'}"), but has "${variantCollection ?? '(none)'}"`,
195
+ );
196
+ }
197
+ }
198
+
199
+ const storageRelations = model.storage['relations'] as Record<string, unknown> | undefined;
200
+ if (storageRelations) {
201
+ for (const relName of Object.keys(storageRelations)) {
202
+ if (!model.relations[relName]) {
203
+ throw new Error(
204
+ `Model "${qualifiedName}" has storage.relations.${relName} but no matching domain-level relation`,
205
+ );
206
+ }
207
+ }
208
+ }
209
+
210
+ for (const [relName, rel] of Object.entries(model.relations)) {
211
+ const relObj = rel as Record<string, unknown>;
212
+ const targetRef = relObj['to'] as { readonly model?: string } | undefined;
213
+ const targetModelName = targetRef?.model;
214
+ if (targetModelName) {
215
+ const targetModel = models[targetModelName];
216
+ if (targetModel?.owner === modelName && !storageRelations?.[relName]) {
217
+ throw new Error(
218
+ `Model "${qualifiedName}" has embed relation "${relName}" to owned model "${targetModelName}" but no matching storage.relations entry`,
219
+ );
220
+ }
221
+ }
213
222
  }
214
223
  }
215
224
  }