@augment-vir/common 30.0.1 → 30.0.2
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/dist/augments/array/array-to-object.d.ts +10 -28
- package/dist/augments/array/array-to-object.js +56 -8
- package/dist/augments/prisma/base-prisma-types.d.ts +38 -0
- package/dist/augments/prisma/prisma-basic-model.d.ts +29 -0
- package/dist/augments/prisma/prisma-basic-model.js +1 -0
- package/dist/augments/prisma/prisma-full-model.d.ts +37 -0
- package/dist/augments/prisma/prisma-full-model.js +1 -0
- package/dist/augments/prisma/prisma-model-create.d.ts +159 -0
- package/dist/augments/prisma/prisma-model-create.js +58 -0
- package/dist/augments/prisma/prisma-model-name.d.ts +18 -0
- package/dist/augments/prisma/prisma-model-name.js +1 -0
- package/dist/index.d.ts +5 -1
- package/dist/index.js +5 -1
- package/package.json +7 -7
- package/dist/augments/prisma/prisma-models.d.ts +0 -143
- /package/dist/augments/prisma/{prisma-models.js → base-prisma-types.js} +0 -0
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { MaybePromise } from '@augment-vir/core';
|
|
1
2
|
/**
|
|
2
3
|
* Polyfill for `Object.groupBy`:
|
|
3
4
|
* https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object/groupBy
|
|
@@ -20,34 +21,15 @@
|
|
|
20
21
|
* ```
|
|
21
22
|
*/
|
|
22
23
|
export declare function groupArrayBy<ElementType, NewKey extends PropertyKey>(inputArray: ReadonlyArray<ElementType>, callback: (value: ElementType, index: number, originalArray: ReadonlyArray<ElementType>) => NewKey): Partial<Record<NewKey, ElementType[]>>;
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
*
|
|
28
|
-
* @category Array
|
|
29
|
-
* @category Object
|
|
30
|
-
* @category Package : @augment-vir/common
|
|
31
|
-
* @example
|
|
32
|
-
*
|
|
33
|
-
* ```ts
|
|
34
|
-
* import {arrayToObject} from '@augment-vir/common';
|
|
35
|
-
*
|
|
36
|
-
* const result = arrayToObject(
|
|
37
|
-
* [
|
|
38
|
-
* 'a',
|
|
39
|
-
* 'b',
|
|
40
|
-
* ],
|
|
41
|
-
* (value) => {
|
|
42
|
-
* return {key: `key-${value}`, value};
|
|
43
|
-
* },
|
|
44
|
-
* );
|
|
45
|
-
* // result is `{key-a: 'a', key-b: 'b'}`
|
|
46
|
-
* ```
|
|
47
|
-
*
|
|
48
|
-
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
49
|
-
*/
|
|
24
|
+
export declare function arrayToObject<ElementType, NewKey extends PropertyKey, NewValue>(inputArray: ReadonlyArray<ElementType>, callback: (value: ElementType, index: number, originalArray: ReadonlyArray<ElementType>) => Promise<{
|
|
25
|
+
key: NewKey;
|
|
26
|
+
value: NewValue;
|
|
27
|
+
} | undefined>): Promise<Partial<Record<NewKey, NewValue>>>;
|
|
50
28
|
export declare function arrayToObject<ElementType, NewKey extends PropertyKey, NewValue>(inputArray: ReadonlyArray<ElementType>, callback: (value: ElementType, index: number, originalArray: ReadonlyArray<ElementType>) => {
|
|
51
29
|
key: NewKey;
|
|
52
30
|
value: NewValue;
|
|
53
|
-
}): Partial<Record<NewKey, NewValue>>;
|
|
31
|
+
} | undefined): Partial<Record<NewKey, NewValue>>;
|
|
32
|
+
export declare function arrayToObject<ElementType, NewKey extends PropertyKey, NewValue>(inputArray: ReadonlyArray<ElementType>, callback: (value: ElementType, index: number, originalArray: ReadonlyArray<ElementType>) => MaybePromise<{
|
|
33
|
+
key: NewKey;
|
|
34
|
+
value: NewValue;
|
|
35
|
+
} | undefined>): MaybePromise<Partial<Record<NewKey, NewValue>>>;
|
|
@@ -1,5 +1,8 @@
|
|
|
1
|
+
import { check } from '@augment-vir/assert';
|
|
2
|
+
import { ensureError } from '@augment-vir/core';
|
|
1
3
|
import { getOrSet } from '../object/get-or-set.js';
|
|
2
4
|
import { typedObjectFromEntries } from '../object/object-entries.js';
|
|
5
|
+
import { filterMap } from './filter.js';
|
|
3
6
|
/**
|
|
4
7
|
* Polyfill for `Object.groupBy`:
|
|
5
8
|
* https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object/groupBy
|
|
@@ -32,7 +35,8 @@ export function groupArrayBy(inputArray, callback) {
|
|
|
32
35
|
/**
|
|
33
36
|
* Similar to {@link groupArrayBy} but maps array entries to a single key and requires `key` _and_
|
|
34
37
|
* `value` outputs from the callback. The resulting object does not have an array of elements
|
|
35
|
-
* (unless the original array itself contains arrays).
|
|
38
|
+
* (unless the original array itself contains arrays). Automatically handles the case where the
|
|
39
|
+
* callback returns a promise.
|
|
36
40
|
*
|
|
37
41
|
* @category Array
|
|
38
42
|
* @category Object
|
|
@@ -57,11 +61,55 @@ export function groupArrayBy(inputArray, callback) {
|
|
|
57
61
|
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
58
62
|
*/
|
|
59
63
|
export function arrayToObject(inputArray, callback) {
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
64
|
+
try {
|
|
65
|
+
let gotAPromise = false;
|
|
66
|
+
const mappedEntries = inputArray
|
|
67
|
+
.map((entry, index, originalArray) => {
|
|
68
|
+
const output = callback(entry, index, originalArray);
|
|
69
|
+
if (output instanceof Promise) {
|
|
70
|
+
gotAPromise = true;
|
|
71
|
+
return output;
|
|
72
|
+
}
|
|
73
|
+
else if (output) {
|
|
74
|
+
return [
|
|
75
|
+
output.key,
|
|
76
|
+
output.value,
|
|
77
|
+
];
|
|
78
|
+
}
|
|
79
|
+
else {
|
|
80
|
+
return undefined;
|
|
81
|
+
}
|
|
82
|
+
})
|
|
83
|
+
.filter(check.isTruthy);
|
|
84
|
+
if (gotAPromise) {
|
|
85
|
+
return new Promise(async (resolve, reject) => {
|
|
86
|
+
try {
|
|
87
|
+
const entries = filterMap(await Promise.all(mappedEntries), (entry) => {
|
|
88
|
+
if (!entry) {
|
|
89
|
+
return undefined;
|
|
90
|
+
}
|
|
91
|
+
else if (Array.isArray(entry)) {
|
|
92
|
+
return entry;
|
|
93
|
+
}
|
|
94
|
+
else {
|
|
95
|
+
return [
|
|
96
|
+
entry.key,
|
|
97
|
+
entry.value,
|
|
98
|
+
];
|
|
99
|
+
}
|
|
100
|
+
}, check.isTruthy);
|
|
101
|
+
resolve(typedObjectFromEntries(entries));
|
|
102
|
+
}
|
|
103
|
+
catch (error) {
|
|
104
|
+
reject(ensureError(error));
|
|
105
|
+
}
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
else {
|
|
109
|
+
return typedObjectFromEntries(mappedEntries);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
catch (error) {
|
|
113
|
+
throw ensureError(error);
|
|
114
|
+
}
|
|
67
115
|
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { AnyObject, type AnyFunction } from '@augment-vir/core';
|
|
2
|
+
/**
|
|
3
|
+
* A base type for Prisma model payloads because Prisma doesn't give us one. This currently only
|
|
4
|
+
* includes the properties that are used within this package.
|
|
5
|
+
*
|
|
6
|
+
* Note: this omits the `composites` property because I don't have any examples of what those
|
|
7
|
+
* actually are.
|
|
8
|
+
*
|
|
9
|
+
* @category Prisma : Common
|
|
10
|
+
* @category Package : @augment-vir/common
|
|
11
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
12
|
+
*/
|
|
13
|
+
export type BasePrismaPayload = {
|
|
14
|
+
name: string;
|
|
15
|
+
objects: Record<string, BasePrismaPayload | BasePrismaPayload[] | null>;
|
|
16
|
+
scalars: AnyObject;
|
|
17
|
+
};
|
|
18
|
+
/**
|
|
19
|
+
* Base Prisma client type that all `PrismaClient` instances should be able to match, with enough
|
|
20
|
+
* data that it'll omit random accidental objects.
|
|
21
|
+
*
|
|
22
|
+
* @category Prisma : Common
|
|
23
|
+
* @category Package : @augment-vir/common
|
|
24
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
25
|
+
*/
|
|
26
|
+
export type BasePrismaClient = {
|
|
27
|
+
$connect: AnyFunction;
|
|
28
|
+
$disconnect: AnyFunction;
|
|
29
|
+
$executeRaw: AnyFunction;
|
|
30
|
+
$executeRawUnsafe: AnyFunction;
|
|
31
|
+
$extends: AnyFunction;
|
|
32
|
+
$on: AnyFunction;
|
|
33
|
+
$queryRaw: AnyFunction;
|
|
34
|
+
$queryRawUnsafe: AnyFunction;
|
|
35
|
+
$transaction: AnyFunction;
|
|
36
|
+
} & {
|
|
37
|
+
[ModelName in string]: any;
|
|
38
|
+
};
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import type { BasePrismaClient } from './base-prisma-types.js';
|
|
2
|
+
import type { PrismaModelName } from './prisma-model-name.js';
|
|
3
|
+
/**
|
|
4
|
+
* A basic model entry with only its immediate properties.
|
|
5
|
+
*
|
|
6
|
+
* @category Prisma : Common
|
|
7
|
+
* @category Package : @augment-vir/common
|
|
8
|
+
* @example
|
|
9
|
+
*
|
|
10
|
+
* ```ts
|
|
11
|
+
* import type {PrismaClient} from '@prisma/client';
|
|
12
|
+
* import type {PrismaBasicModel} from '@augment-vir/common';
|
|
13
|
+
*
|
|
14
|
+
* function doThing(fullModel: PrismaBasicModel<PrismaClient, 'user'>) {}
|
|
15
|
+
* ```
|
|
16
|
+
*
|
|
17
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
18
|
+
*/
|
|
19
|
+
export type PrismaBasicModel<PrismaClient extends BasePrismaClient, Model extends PrismaModelName<PrismaClient>> = PrismaClient['model'][Model]['payload']['scalars'];
|
|
20
|
+
/**
|
|
21
|
+
* Basic model entries for all models in the database.
|
|
22
|
+
*
|
|
23
|
+
* @category Prisma : Common
|
|
24
|
+
* @category Package : @augment-vir/common
|
|
25
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
26
|
+
*/
|
|
27
|
+
export type PrismaAllBasicModels<PrismaClient extends BasePrismaClient> = Partial<{
|
|
28
|
+
[ModelName in PrismaModelName<PrismaClient>]: PrismaBasicModel<PrismaClient, ModelName>[];
|
|
29
|
+
}>;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { type BasePrismaClient, type BasePrismaPayload } from './base-prisma-types.js';
|
|
2
|
+
import type { PrismaModelName } from './prisma-model-name.js';
|
|
3
|
+
/**
|
|
4
|
+
* A full model entry with all relations from the given Prisma type map and model name.
|
|
5
|
+
*
|
|
6
|
+
* @category Prisma : Common
|
|
7
|
+
* @category Package : @augment-vir/common
|
|
8
|
+
* @example
|
|
9
|
+
*
|
|
10
|
+
* ```ts
|
|
11
|
+
* import type {Prisma} from '@prisma/client';
|
|
12
|
+
* import type {FullPrismaModel} from '@augment-vir/common';
|
|
13
|
+
*
|
|
14
|
+
* function doThing(fullModel: FullModel<Prisma.TypeMap, 'User'>) {}
|
|
15
|
+
* ```
|
|
16
|
+
*
|
|
17
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
18
|
+
*/
|
|
19
|
+
export type PrismaFullModel<PrismaClient extends BasePrismaClient, Model extends PrismaModelName<PrismaClient>> = ExpandPrismaTypeMapPayload<PrismaClient[Model][symbol]['types']['payload']>;
|
|
20
|
+
/**
|
|
21
|
+
* Expand a Prisma payload into its scalars and recursive relations.
|
|
22
|
+
*
|
|
23
|
+
* @category Prisma : Common : Util
|
|
24
|
+
* @category Package : @augment-vir/common
|
|
25
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
26
|
+
*/
|
|
27
|
+
export type ExpandPrismaTypeMapPayload<Payload extends BasePrismaPayload> = Payload['scalars'] & {
|
|
28
|
+
[Key in keyof Payload['objects']]: ExpandPotentialArrayPrismaTypeMapPayload<NonNullable<Payload['objects'][Key]>> | (null extends Payload['objects'][Key] ? null : never);
|
|
29
|
+
};
|
|
30
|
+
/**
|
|
31
|
+
* Expand a payload that might be inside of an array, keeping it inside of an array if it is.
|
|
32
|
+
*
|
|
33
|
+
* @category Prisma : Common : Util
|
|
34
|
+
* @category Package : @augment-vir/common
|
|
35
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
36
|
+
*/
|
|
37
|
+
export type ExpandPotentialArrayPrismaTypeMapPayload<PayloadWrapper extends BasePrismaPayload | BasePrismaPayload[]> = PayloadWrapper extends (infer ActualPayload extends BasePrismaPayload)[] ? ExpandPrismaTypeMapPayload<ActualPayload>[] : PayloadWrapper extends BasePrismaPayload ? ExpandPrismaTypeMapPayload<PayloadWrapper> : never;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
import type { BasePrismaClient } from './base-prisma-types.js';
|
|
2
|
+
import type { PrismaModelName } from './prisma-model-name.js';
|
|
3
|
+
/**
|
|
4
|
+
* Use this to define mock entries that _shouldn't_ be saved to the database so that we can easily
|
|
5
|
+
* write tests for missing data.
|
|
6
|
+
*
|
|
7
|
+
* @category Prisma : Common
|
|
8
|
+
* @category Package : @augment-vir/common
|
|
9
|
+
* @example
|
|
10
|
+
*
|
|
11
|
+
* ```ts
|
|
12
|
+
* import type {PrismaClient} from '@prisma/client';
|
|
13
|
+
* import {prismaModelCreateExclude, PrismaKeyedModelCreate} from '@augment-vir/common';
|
|
14
|
+
*
|
|
15
|
+
* export const mockUsers = {
|
|
16
|
+
* user1: {
|
|
17
|
+
* id: 1,
|
|
18
|
+
* authRole: 'admin',
|
|
19
|
+
* },
|
|
20
|
+
* missingUser: {
|
|
21
|
+
* id: -1,
|
|
22
|
+
* authRole: 'user',
|
|
23
|
+
* [prismaModelCreateExclude]: true,
|
|
24
|
+
* },
|
|
25
|
+
* } as const satisfies PrismaKeyedModelCreate<PrismaClient, 'User'>;
|
|
26
|
+
* ```
|
|
27
|
+
*
|
|
28
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
29
|
+
*/
|
|
30
|
+
export declare const prismaModelCreateExclude: unique symbol;
|
|
31
|
+
/**
|
|
32
|
+
* Use this to prevent the id property from being included when creating a mock record, allowing the
|
|
33
|
+
* database's internal auto-increment functionality to generate one. This is necessary when testing
|
|
34
|
+
* creation of new records because manually specified ids do not increment the auto incrementor.
|
|
35
|
+
*
|
|
36
|
+
* @category Prisma : Common
|
|
37
|
+
* @category Package : @augment-vir/common
|
|
38
|
+
* @example
|
|
39
|
+
*
|
|
40
|
+
* ```ts
|
|
41
|
+
* import type {PrismaClient} from '@prisma/client';
|
|
42
|
+
* import {prismaModelCreateOmitId, PrismaKeyedModelCreate} from '@augment-vir/common';
|
|
43
|
+
*
|
|
44
|
+
* export const mockUsers = {
|
|
45
|
+
* user1: {
|
|
46
|
+
* id: 1,
|
|
47
|
+
* authRole: 'admin',
|
|
48
|
+
* [prismaModelCreateOmitId]: true,
|
|
49
|
+
* },
|
|
50
|
+
* user2: {
|
|
51
|
+
* id: 2,
|
|
52
|
+
* authRole: 'admin',
|
|
53
|
+
* [prismaModelCreateOmitId]: true,
|
|
54
|
+
* },
|
|
55
|
+
* } as const satisfies PrismaKeyedModelCreate<PrismaClient, 'User'>;
|
|
56
|
+
* ```
|
|
57
|
+
*
|
|
58
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
59
|
+
*/
|
|
60
|
+
export declare const prismaModelCreateOmitId: unique symbol;
|
|
61
|
+
/**
|
|
62
|
+
* Extracts the creation data for a model from the given `PrismaClient` type.
|
|
63
|
+
*
|
|
64
|
+
* @category Prisma : Common
|
|
65
|
+
* @category Package : @augment-vir/common
|
|
66
|
+
* @example
|
|
67
|
+
*
|
|
68
|
+
* ```ts
|
|
69
|
+
* import type {PrismaClient} from '@prisma/client';
|
|
70
|
+
* import type {PrismaModelCreate} from '@augment-vir/common';
|
|
71
|
+
*
|
|
72
|
+
* function doThing(entry: PrismaModelCreate<PrismaClient, 'User'>) {}
|
|
73
|
+
* ```
|
|
74
|
+
*
|
|
75
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
76
|
+
*/
|
|
77
|
+
export type PrismaModelCreate<PrismaClient extends BasePrismaClient, Model extends PrismaModelName<PrismaClient>> = NonNullable<Parameters<PrismaClient[Model]['create']>[0]> extends {
|
|
78
|
+
data?: infer Data;
|
|
79
|
+
} ? NonNullable<Data> & Partial<{
|
|
80
|
+
[prismaModelCreateExclude]: true;
|
|
81
|
+
[prismaModelCreateOmitId]: true;
|
|
82
|
+
}> : `ERROR: failed to infer creation entry for model '${Model}'`;
|
|
83
|
+
/**
|
|
84
|
+
* A type for creating multiple Prisma create mocks that are named for future reference.
|
|
85
|
+
*
|
|
86
|
+
* @category Prisma : Common
|
|
87
|
+
* @category Package : @augment-vir/common
|
|
88
|
+
* @example
|
|
89
|
+
*
|
|
90
|
+
* ```ts
|
|
91
|
+
* import {PrismaKeyedModelCreate} from '@augment-vir/common';
|
|
92
|
+
* import type {PrismaClient} from '@prisma/client';
|
|
93
|
+
*
|
|
94
|
+
* const mockUsers = {
|
|
95
|
+
* mockUser1: {
|
|
96
|
+
* first_name: 'one',
|
|
97
|
+
* id: 123,
|
|
98
|
+
* // etc.
|
|
99
|
+
* },
|
|
100
|
+
* mockUser2: {
|
|
101
|
+
* first_name: 'two',
|
|
102
|
+
* id: 124,
|
|
103
|
+
* auth_role: 'user',
|
|
104
|
+
* // etc.
|
|
105
|
+
* },
|
|
106
|
+
* } as const satisfies PrismaKeyedModelCreate<PrismaClient, 'User'>;
|
|
107
|
+
* ```
|
|
108
|
+
*
|
|
109
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
110
|
+
*/
|
|
111
|
+
export type PrismaKeyedModelCreate<PrismaClient extends BasePrismaClient, Model extends PrismaModelName<PrismaClient>> = {
|
|
112
|
+
[EntryName in string]: PrismaModelCreate<PrismaClient, Model>;
|
|
113
|
+
};
|
|
114
|
+
/**
|
|
115
|
+
* Model create data stored by model name in either array or keyed form. Used in
|
|
116
|
+
* `prisma.client.addData()` from `@augment-vir/node`.
|
|
117
|
+
*
|
|
118
|
+
* @category Prisma : Common
|
|
119
|
+
* @category Package : @augment-vir/common
|
|
120
|
+
* @example
|
|
121
|
+
*
|
|
122
|
+
* ```ts
|
|
123
|
+
* import {PrismaKeyedModelCreate} from '@augment-vir/common';
|
|
124
|
+
* import type {PrismaClient} from '@prisma/client';
|
|
125
|
+
*
|
|
126
|
+
* const mockData: ModelCreateData<PrismaClient> = {
|
|
127
|
+
* user: {
|
|
128
|
+
* mockUser1: {
|
|
129
|
+
* first_name: 'one',
|
|
130
|
+
* id: 123,
|
|
131
|
+
* // etc.
|
|
132
|
+
* },
|
|
133
|
+
* mockUser2: {
|
|
134
|
+
* first_name: 'two',
|
|
135
|
+
* id: 124,
|
|
136
|
+
* auth_role: 'user',
|
|
137
|
+
* // etc.
|
|
138
|
+
* },
|
|
139
|
+
* },
|
|
140
|
+
* region: [
|
|
141
|
+
* {
|
|
142
|
+
* id: 1,
|
|
143
|
+
* name: 'North America',
|
|
144
|
+
* // etc.
|
|
145
|
+
* },
|
|
146
|
+
* {
|
|
147
|
+
* id: 2,
|
|
148
|
+
* name: 'Europe',
|
|
149
|
+
* // etc.
|
|
150
|
+
* },
|
|
151
|
+
* ],
|
|
152
|
+
* };
|
|
153
|
+
* ```
|
|
154
|
+
*
|
|
155
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
156
|
+
*/
|
|
157
|
+
export type PrismaAllModelsCreate<PrismaClient extends BasePrismaClient> = Readonly<Partial<{
|
|
158
|
+
[Model in PrismaModelName<PrismaClient>]: Readonly<PrismaKeyedModelCreate<PrismaClient, Model>> | ReadonlyArray<Readonly<PrismaModelCreate<PrismaClient, Model>>>;
|
|
159
|
+
}>>;
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Use this to define mock entries that _shouldn't_ be saved to the database so that we can easily
|
|
3
|
+
* write tests for missing data.
|
|
4
|
+
*
|
|
5
|
+
* @category Prisma : Common
|
|
6
|
+
* @category Package : @augment-vir/common
|
|
7
|
+
* @example
|
|
8
|
+
*
|
|
9
|
+
* ```ts
|
|
10
|
+
* import type {PrismaClient} from '@prisma/client';
|
|
11
|
+
* import {prismaModelCreateExclude, PrismaKeyedModelCreate} from '@augment-vir/common';
|
|
12
|
+
*
|
|
13
|
+
* export const mockUsers = {
|
|
14
|
+
* user1: {
|
|
15
|
+
* id: 1,
|
|
16
|
+
* authRole: 'admin',
|
|
17
|
+
* },
|
|
18
|
+
* missingUser: {
|
|
19
|
+
* id: -1,
|
|
20
|
+
* authRole: 'user',
|
|
21
|
+
* [prismaModelCreateExclude]: true,
|
|
22
|
+
* },
|
|
23
|
+
* } as const satisfies PrismaKeyedModelCreate<PrismaClient, 'User'>;
|
|
24
|
+
* ```
|
|
25
|
+
*
|
|
26
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
27
|
+
*/
|
|
28
|
+
export const prismaModelCreateExclude = Symbol('prisma-model-create-exclude');
|
|
29
|
+
/**
|
|
30
|
+
* Use this to prevent the id property from being included when creating a mock record, allowing the
|
|
31
|
+
* database's internal auto-increment functionality to generate one. This is necessary when testing
|
|
32
|
+
* creation of new records because manually specified ids do not increment the auto incrementor.
|
|
33
|
+
*
|
|
34
|
+
* @category Prisma : Common
|
|
35
|
+
* @category Package : @augment-vir/common
|
|
36
|
+
* @example
|
|
37
|
+
*
|
|
38
|
+
* ```ts
|
|
39
|
+
* import type {PrismaClient} from '@prisma/client';
|
|
40
|
+
* import {prismaModelCreateOmitId, PrismaKeyedModelCreate} from '@augment-vir/common';
|
|
41
|
+
*
|
|
42
|
+
* export const mockUsers = {
|
|
43
|
+
* user1: {
|
|
44
|
+
* id: 1,
|
|
45
|
+
* authRole: 'admin',
|
|
46
|
+
* [prismaModelCreateOmitId]: true,
|
|
47
|
+
* },
|
|
48
|
+
* user2: {
|
|
49
|
+
* id: 2,
|
|
50
|
+
* authRole: 'admin',
|
|
51
|
+
* [prismaModelCreateOmitId]: true,
|
|
52
|
+
* },
|
|
53
|
+
* } as const satisfies PrismaKeyedModelCreate<PrismaClient, 'User'>;
|
|
54
|
+
* ```
|
|
55
|
+
*
|
|
56
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
57
|
+
*/
|
|
58
|
+
export const prismaModelCreateOmitId = Symbol('prisma-model-create-exclude-id');
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { BasePrismaClient } from './base-prisma-types.js';
|
|
2
|
+
/**
|
|
3
|
+
* Extracts all model names from a generated `PrismaClient`.
|
|
4
|
+
*
|
|
5
|
+
* @category Prisma : Common
|
|
6
|
+
* @category Package : @augment-vir/common
|
|
7
|
+
* @example
|
|
8
|
+
*
|
|
9
|
+
* ```ts
|
|
10
|
+
* import type {PrismaClient} from '@prisma/client';
|
|
11
|
+
* import type {PrismaModelName} from '@augment-vir/common';
|
|
12
|
+
*
|
|
13
|
+
* function doThing(modelName: PrismaModelName<PrismaClient>) {}
|
|
14
|
+
* ```
|
|
15
|
+
*
|
|
16
|
+
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
17
|
+
*/
|
|
18
|
+
export type PrismaModelName<PrismaClient extends BasePrismaClient> = Exclude<keyof PrismaClient, `$${string}` | symbol>;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/dist/index.d.ts
CHANGED
|
@@ -51,7 +51,11 @@ export * from './augments/object/object-filter.js';
|
|
|
51
51
|
export * from './augments/object/object-keys.js';
|
|
52
52
|
export * from './augments/object/object-values.js';
|
|
53
53
|
export * from './augments/path/esm-path.js';
|
|
54
|
-
export * from './augments/prisma/prisma-
|
|
54
|
+
export * from './augments/prisma/base-prisma-types.js';
|
|
55
|
+
export * from './augments/prisma/prisma-basic-model.js';
|
|
56
|
+
export * from './augments/prisma/prisma-full-model.js';
|
|
57
|
+
export * from './augments/prisma/prisma-model-create.js';
|
|
58
|
+
export * from './augments/prisma/prisma-model-name.js';
|
|
55
59
|
export * from './augments/promise/timed-promise.js';
|
|
56
60
|
export * from './augments/random/random-boolean.js';
|
|
57
61
|
export * from './augments/random/random-integer.js';
|
package/dist/index.js
CHANGED
|
@@ -51,7 +51,11 @@ export * from './augments/object/object-filter.js';
|
|
|
51
51
|
export * from './augments/object/object-keys.js';
|
|
52
52
|
export * from './augments/object/object-values.js';
|
|
53
53
|
export * from './augments/path/esm-path.js';
|
|
54
|
-
export * from './augments/prisma/prisma-
|
|
54
|
+
export * from './augments/prisma/base-prisma-types.js';
|
|
55
|
+
export * from './augments/prisma/prisma-basic-model.js';
|
|
56
|
+
export * from './augments/prisma/prisma-full-model.js';
|
|
57
|
+
export * from './augments/prisma/prisma-model-create.js';
|
|
58
|
+
export * from './augments/prisma/prisma-model-name.js';
|
|
55
59
|
export * from './augments/promise/timed-promise.js';
|
|
56
60
|
export * from './augments/random/random-boolean.js';
|
|
57
61
|
export * from './augments/random/random-integer.js';
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@augment-vir/common",
|
|
3
|
-
"version": "30.0.
|
|
3
|
+
"version": "30.0.2",
|
|
4
4
|
"description": "A collection of augments, helpers types, functions, and classes for any JavaScript environment.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"augment",
|
|
@@ -39,20 +39,20 @@
|
|
|
39
39
|
"test:web": "virmator --no-deps test web"
|
|
40
40
|
},
|
|
41
41
|
"dependencies": {
|
|
42
|
-
"@augment-vir/assert": "^30.0.
|
|
43
|
-
"@augment-vir/core": "^30.0.
|
|
42
|
+
"@augment-vir/assert": "^30.0.2",
|
|
43
|
+
"@augment-vir/core": "^30.0.2",
|
|
44
44
|
"@date-vir/duration": "^6.0.0",
|
|
45
45
|
"ansi-styles": "^6.2.1",
|
|
46
46
|
"json5": "^2.2.3",
|
|
47
|
-
"type-fest": "^4.
|
|
47
|
+
"type-fest": "^4.26.1"
|
|
48
48
|
},
|
|
49
49
|
"devDependencies": {
|
|
50
50
|
"@web/dev-server-esbuild": "^1.0.2",
|
|
51
|
-
"@web/test-runner": "^0.
|
|
51
|
+
"@web/test-runner": "^0.19.0",
|
|
52
52
|
"@web/test-runner-commands": "^0.9.0",
|
|
53
53
|
"@web/test-runner-playwright": "^0.11.0",
|
|
54
|
-
"@web/test-runner-visual-regression": "^0.
|
|
55
|
-
"concurrently": "^
|
|
54
|
+
"@web/test-runner-visual-regression": "^0.10.0",
|
|
55
|
+
"concurrently": "^9.0.0",
|
|
56
56
|
"execute-in-browser": "^1.0.3",
|
|
57
57
|
"istanbul-smart-text-reporter": "^1.1.4"
|
|
58
58
|
},
|
|
@@ -1,143 +0,0 @@
|
|
|
1
|
-
import type { AnyFunction, AnyObject } from '@augment-vir/core';
|
|
2
|
-
/**
|
|
3
|
-
* Extracts all model names from a generated `Prisma` object.
|
|
4
|
-
*
|
|
5
|
-
* @category Prisma : Common
|
|
6
|
-
* @category Package : @augment-vir/common
|
|
7
|
-
* @example
|
|
8
|
-
*
|
|
9
|
-
* ```ts
|
|
10
|
-
* import type {Prisma} from '@prisma/client';
|
|
11
|
-
* import type {ModelNameFromPrismaTypeMap} from '@augment-vir/common';
|
|
12
|
-
*
|
|
13
|
-
* function doThing(modelName: ModelNameFromPrismaTypeMap<Prisma.TypeMap>) {}
|
|
14
|
-
* ```
|
|
15
|
-
*
|
|
16
|
-
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
17
|
-
*/
|
|
18
|
-
export type ModelNameFromPrismaTypeMap<PrismaTypeMap extends BasePrismaTypeMap> = PrismaTypeMap['meta']['modelProps'];
|
|
19
|
-
/**
|
|
20
|
-
* Extracts all model names from a generated `PrismaClient`.
|
|
21
|
-
*
|
|
22
|
-
* @category Prisma : Common
|
|
23
|
-
* @category Package : @augment-vir/common
|
|
24
|
-
* @example
|
|
25
|
-
*
|
|
26
|
-
* ```ts
|
|
27
|
-
* import type {PrismaClient} from '@prisma/client';
|
|
28
|
-
* import type {ModelNameFromPrismaClient} from '@augment-vir/common';
|
|
29
|
-
*
|
|
30
|
-
* function doThing(modelName: ModelNameFromPrismaClient<PrismaClient>) {}
|
|
31
|
-
* ```
|
|
32
|
-
*
|
|
33
|
-
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
34
|
-
*/
|
|
35
|
-
export type ModelNameFromPrismaClient<PrismaClient extends AnyObject> = keyof {
|
|
36
|
-
[Model in Extract<keyof PrismaClient, string> as PrismaClient[Model] extends {
|
|
37
|
-
findFirst: AnyFunction;
|
|
38
|
-
} ? Model : never]: boolean;
|
|
39
|
-
};
|
|
40
|
-
/**
|
|
41
|
-
* Extracts the creation data for a model from the given `PrismaClient` type.
|
|
42
|
-
*
|
|
43
|
-
* @category Prisma : Common
|
|
44
|
-
* @category Package : @augment-vir/common
|
|
45
|
-
* @example
|
|
46
|
-
*
|
|
47
|
-
* ```ts
|
|
48
|
-
* import type {PrismaClient} from '@prisma/client';
|
|
49
|
-
* import type {ModelCreationEntry} from '@augment-vir/common';
|
|
50
|
-
*
|
|
51
|
-
* function doThing(entry: ModelCreationEntry<PrismaClient, 'User'>) {}
|
|
52
|
-
* ```
|
|
53
|
-
*
|
|
54
|
-
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
55
|
-
*/
|
|
56
|
-
export type PrismaModelCreationEntry<PrismaClient extends AnyObject, Model extends ModelNameFromPrismaClient<PrismaClient>> = NonNullable<Parameters<PrismaClient[Model]['create']>[0]> extends {
|
|
57
|
-
data?: infer Data;
|
|
58
|
-
} ? NonNullable<Data> : `ERROR: failed to infer creation entry for model '${Model}'`;
|
|
59
|
-
/**
|
|
60
|
-
* A base type for `Prisma.TypeMap` because Prisma doesn't give us one. This currently only includes
|
|
61
|
-
* the properties that are used within `@augment-vir/common`.
|
|
62
|
-
*
|
|
63
|
-
* @category Prisma : Common
|
|
64
|
-
* @category Package : @augment-vir/common
|
|
65
|
-
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
66
|
-
*/
|
|
67
|
-
export type BasePrismaTypeMap = {
|
|
68
|
-
meta: {
|
|
69
|
-
modelProps: string;
|
|
70
|
-
};
|
|
71
|
-
model: Record<string, {
|
|
72
|
-
payload: BasePrismaPayload;
|
|
73
|
-
}>;
|
|
74
|
-
};
|
|
75
|
-
/**
|
|
76
|
-
* A base type for Prisma model payloads because Prisma doesn't give us one. This currently only
|
|
77
|
-
* includes the properties that are used within this package.
|
|
78
|
-
*
|
|
79
|
-
* Note: this omits the `composites` property because I don't have any examples of what those
|
|
80
|
-
* actually are.
|
|
81
|
-
*
|
|
82
|
-
* @category Prisma : Common
|
|
83
|
-
* @category Package : @augment-vir/common
|
|
84
|
-
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
85
|
-
*/
|
|
86
|
-
export type BasePrismaPayload = {
|
|
87
|
-
name: string;
|
|
88
|
-
objects: Record<string, BasePrismaPayload | BasePrismaPayload[] | null>;
|
|
89
|
-
scalars: AnyObject;
|
|
90
|
-
};
|
|
91
|
-
/**
|
|
92
|
-
* A full model entry with all relations from the given Prisma type map and model name.
|
|
93
|
-
*
|
|
94
|
-
* @category Prisma : Common
|
|
95
|
-
* @category Package : @augment-vir/common
|
|
96
|
-
* @example
|
|
97
|
-
*
|
|
98
|
-
* ```ts
|
|
99
|
-
* import type {Prisma} from '@prisma/client';
|
|
100
|
-
* import type {FullPrismaModel} from '@augment-vir/common';
|
|
101
|
-
*
|
|
102
|
-
* function doThing(fullModel: FullModel<Prisma.TypeMap, 'User'>) {}
|
|
103
|
-
* ```
|
|
104
|
-
*
|
|
105
|
-
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
106
|
-
*/
|
|
107
|
-
export type FullPrismaModel<PrismaTypeMap extends BasePrismaTypeMap, Model extends ModelNameFromPrismaTypeMap<PrismaTypeMap>> = ExpandPrismaTypeMapPayload<PrismaTypeMap['model'][Model]['payload']>;
|
|
108
|
-
/**
|
|
109
|
-
* A base model entry with only its immediate properties from the given Prisma type map and model
|
|
110
|
-
* name.
|
|
111
|
-
*
|
|
112
|
-
* @category Prisma : Common
|
|
113
|
-
* @category Package : @augment-vir/common
|
|
114
|
-
* @example
|
|
115
|
-
*
|
|
116
|
-
* ```ts
|
|
117
|
-
* import type {Prisma} from '@prisma/client';
|
|
118
|
-
* import type {BasePrismaModel} from '@augment-vir/common';
|
|
119
|
-
*
|
|
120
|
-
* function doThing(fullModel: BaseModel<Prisma.TypeMap, 'User'>) {}
|
|
121
|
-
* ```
|
|
122
|
-
*
|
|
123
|
-
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
124
|
-
*/
|
|
125
|
-
export type BasePrismaModel<PrismaTypeMap extends BasePrismaTypeMap, Model extends ModelNameFromPrismaTypeMap<PrismaTypeMap>> = PrismaTypeMap['model'][Model]['payload']['scalars'];
|
|
126
|
-
/**
|
|
127
|
-
* Expand a Prisma payload into its scalars and recursive relations.
|
|
128
|
-
*
|
|
129
|
-
* @category Prisma : Common : Util
|
|
130
|
-
* @category Package : @augment-vir/common
|
|
131
|
-
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
132
|
-
*/
|
|
133
|
-
export type ExpandPrismaTypeMapPayload<Payload extends BasePrismaPayload> = Payload['scalars'] & {
|
|
134
|
-
[Key in keyof Payload['objects']]: ExpandPotentialArrayPrismaTypeMapPayload<NonNullable<Payload['objects'][Key]>> | (null extends Payload['objects'][Key] ? null : never);
|
|
135
|
-
};
|
|
136
|
-
/**
|
|
137
|
-
* Expand a payload that might be inside of an array, keeping it inside of an array if it is.
|
|
138
|
-
*
|
|
139
|
-
* @category Prisma : Common : Util
|
|
140
|
-
* @category Package : @augment-vir/common
|
|
141
|
-
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
142
|
-
*/
|
|
143
|
-
export type ExpandPotentialArrayPrismaTypeMapPayload<PayloadWrapper extends BasePrismaPayload | BasePrismaPayload[]> = PayloadWrapper extends (infer ActualPayload extends BasePrismaPayload)[] ? ExpandPrismaTypeMapPayload<ActualPayload>[] : PayloadWrapper extends BasePrismaPayload ? ExpandPrismaTypeMapPayload<PayloadWrapper> : never;
|
|
File without changes
|