@prisma-next/mongo-contract-ts 0.0.1
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/README.md +90 -0
- package/package.json +46 -0
- package/src/config-types.ts +13 -0
- package/src/contract-builder.ts +1403 -0
- package/src/exports/config-types.ts +2 -0
- package/src/exports/contract-builder.ts +19 -0
package/README.md
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
# @prisma-next/mongo-contract-ts
|
|
2
|
+
|
|
3
|
+
Mongo-specific TypeScript contract authoring surface for Prisma Next.
|
|
4
|
+
|
|
5
|
+
## Purpose
|
|
6
|
+
|
|
7
|
+
This package provides the Mongo `contract.ts` DSL:
|
|
8
|
+
|
|
9
|
+
- `defineContract(...)`
|
|
10
|
+
- `field`, `index`, `model`, `rel`, and `valueObject` helpers
|
|
11
|
+
- callback and object-literal authoring forms
|
|
12
|
+
|
|
13
|
+
It builds canonical `MongoContract` values and validates them through `@prisma-next/mongo-contract`.
|
|
14
|
+
|
|
15
|
+
## Current Slice
|
|
16
|
+
|
|
17
|
+
The current implementation supports:
|
|
18
|
+
|
|
19
|
+
- aggregate roots and collection-backed models
|
|
20
|
+
- typed reference relations via `rel.belongsTo`, `rel.hasOne`, and `rel.hasMany`
|
|
21
|
+
- owned models via `owner` plus parent `storageRelations`
|
|
22
|
+
- discriminator-based polymorphism via `base`, `discriminator`, and `variants`
|
|
23
|
+
- top-level value objects referenced from fields
|
|
24
|
+
- Mongo collection index authoring via model-local `indexes`
|
|
25
|
+
- Mongo collection option authoring via model-local `collectionOptions`
|
|
26
|
+
- base Mongo codec helpers such as `field.objectId()`, `field.string()`, `field.double()`, `field.int32()`, `field.bool()`, `field.date()`, and `field.vector()`
|
|
27
|
+
|
|
28
|
+
This first slice does not yet cover union or dict authoring, or Mongo validator authoring.
|
|
29
|
+
|
|
30
|
+
## Exports
|
|
31
|
+
|
|
32
|
+
- `./contract-builder`: Mongo DSL entrypoint
|
|
33
|
+
- `./config-types`: config helper types
|
|
34
|
+
|
|
35
|
+
## Usage
|
|
36
|
+
|
|
37
|
+
```typescript
|
|
38
|
+
import mongoFamily from '@prisma-next/family-mongo/pack';
|
|
39
|
+
import { defineContract, field, index, model, rel, valueObject } from '@prisma-next/mongo-contract-ts/contract-builder';
|
|
40
|
+
import mongoTarget from '@prisma-next/target-mongo/pack';
|
|
41
|
+
|
|
42
|
+
const Address = valueObject('Address', {
|
|
43
|
+
fields: {
|
|
44
|
+
street: field.string(),
|
|
45
|
+
zip: field.string().optional(),
|
|
46
|
+
},
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
const User = model('User', {
|
|
50
|
+
collection: 'users',
|
|
51
|
+
fields: {
|
|
52
|
+
_id: field.objectId(),
|
|
53
|
+
homeAddress: field.valueObject(Address).optional(),
|
|
54
|
+
},
|
|
55
|
+
indexes: [
|
|
56
|
+
index({ _id: 1 }, { unique: true }),
|
|
57
|
+
],
|
|
58
|
+
collectionOptions: {
|
|
59
|
+
collation: { locale: 'en', strength: 2 },
|
|
60
|
+
},
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
const Post = model('Post', {
|
|
64
|
+
collection: 'posts',
|
|
65
|
+
fields: {
|
|
66
|
+
_id: field.objectId(),
|
|
67
|
+
authorId: field.objectId(),
|
|
68
|
+
title: field.string(),
|
|
69
|
+
},
|
|
70
|
+
relations: {
|
|
71
|
+
author: rel.belongsTo(User, {
|
|
72
|
+
from: 'authorId',
|
|
73
|
+
to: User.ref('_id'),
|
|
74
|
+
}),
|
|
75
|
+
},
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
export const contract = defineContract({
|
|
79
|
+
family: mongoFamily,
|
|
80
|
+
target: mongoTarget,
|
|
81
|
+
valueObjects: { Address },
|
|
82
|
+
models: { User, Post },
|
|
83
|
+
});
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
## Notes
|
|
87
|
+
|
|
88
|
+
- Use `@prisma-next/family-mongo/pack` and `@prisma-next/target-mongo/pack` in authoring flows. They are pure pack refs and do not pull in control-plane runtime code.
|
|
89
|
+
- Runtime validation and row inference live in `@prisma-next/mongo-contract`.
|
|
90
|
+
- When you hoist reusable index or collection option objects, prefer `satisfies MongoIndexOptions` or `satisfies MongoCollectionOptions` from `@prisma-next/mongo-contract` so TypeScript validates the supported Mongo option surface.
|
package/package.json
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@prisma-next/mongo-contract-ts",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"sideEffects": false,
|
|
6
|
+
"description": "Mongo-specific TypeScript contract authoring surface for Prisma Next",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"build": "tsdown",
|
|
9
|
+
"test": "vitest run",
|
|
10
|
+
"test:coverage": "vitest run --coverage",
|
|
11
|
+
"typecheck": "tsc --project tsconfig.json --noEmit",
|
|
12
|
+
"lint": "biome check . --error-on-warnings",
|
|
13
|
+
"lint:fix": "biome check --write .",
|
|
14
|
+
"lint:fix:unsafe": "biome check --write --unsafe .",
|
|
15
|
+
"clean": "rm -rf dist dist-tsc dist-tsc-prod coverage .tmp-output"
|
|
16
|
+
},
|
|
17
|
+
"dependencies": {
|
|
18
|
+
"@prisma-next/config": "workspace:*",
|
|
19
|
+
"@prisma-next/contract": "workspace:*",
|
|
20
|
+
"@prisma-next/framework-components": "workspace:*",
|
|
21
|
+
"@prisma-next/mongo-contract": "workspace:*",
|
|
22
|
+
"@prisma-next/utils": "workspace:*"
|
|
23
|
+
},
|
|
24
|
+
"devDependencies": {
|
|
25
|
+
"@prisma-next/test-utils": "workspace:*",
|
|
26
|
+
"@prisma-next/tsconfig": "workspace:*",
|
|
27
|
+
"@prisma-next/tsdown": "workspace:*",
|
|
28
|
+
"tsdown": "catalog:",
|
|
29
|
+
"typescript": "catalog:",
|
|
30
|
+
"vitest": "catalog:"
|
|
31
|
+
},
|
|
32
|
+
"files": [
|
|
33
|
+
"dist",
|
|
34
|
+
"src"
|
|
35
|
+
],
|
|
36
|
+
"exports": {
|
|
37
|
+
"./config-types": "./dist/config-types.mjs",
|
|
38
|
+
"./contract-builder": "./dist/contract-builder.mjs",
|
|
39
|
+
"./package.json": "./package.json"
|
|
40
|
+
},
|
|
41
|
+
"repository": {
|
|
42
|
+
"type": "git",
|
|
43
|
+
"url": "https://github.com/prisma/prisma-next.git",
|
|
44
|
+
"directory": "packages/2-mongo-family/2-authoring/contract-ts"
|
|
45
|
+
}
|
|
46
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { ContractConfig } from '@prisma-next/config/config-types';
|
|
2
|
+
import type { Contract } from '@prisma-next/contract/types';
|
|
3
|
+
import { ifDefined } from '@prisma-next/utils/defined';
|
|
4
|
+
import { ok } from '@prisma-next/utils/result';
|
|
5
|
+
|
|
6
|
+
// This helper stays family-agnostic and intentionally accepts the base Contract shape even when
|
|
7
|
+
// re-exported from a Mongo-specific package.
|
|
8
|
+
export function typescriptContract(contract: Contract, output?: string): ContractConfig {
|
|
9
|
+
return {
|
|
10
|
+
source: async (_context) => ok(contract),
|
|
11
|
+
...ifDefined('output', output),
|
|
12
|
+
};
|
|
13
|
+
}
|