@shivaedev/effect-prisma 0.6.3 → 0.7.0

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.
Files changed (138) hide show
  1. package/CHANGELOG.md +26 -1
  2. package/dist/bin/normalize-contract.js +1 -0
  3. package/dist/bin/normalize-contract.js.map +1 -1
  4. package/dist/database.d.ts +4 -4
  5. package/dist/database.d.ts.map +1 -1
  6. package/dist/database.js +12 -12
  7. package/dist/database.js.map +1 -1
  8. package/dist/error.d.ts +5 -5
  9. package/dist/error.d.ts.map +1 -1
  10. package/dist/error.js +5 -6
  11. package/dist/error.js.map +1 -1
  12. package/dist/index.d.ts +3 -3
  13. package/dist/internal/adapters/transaction-settlement.d.ts +10 -0
  14. package/dist/internal/adapters/transaction-settlement.d.ts.map +1 -0
  15. package/dist/internal/adapters/transaction-settlement.js +56 -0
  16. package/dist/internal/adapters/transaction-settlement.js.map +1 -0
  17. package/dist/internal/client-lifecycle.d.ts +2 -2
  18. package/dist/internal/client-lifecycle.d.ts.map +1 -1
  19. package/dist/internal/client-lifecycle.js.map +1 -1
  20. package/dist/internal/contract-normalization.d.ts +0 -4
  21. package/dist/internal/contract-normalization.d.ts.map +1 -1
  22. package/dist/internal/contract-normalization.js +5 -10
  23. package/dist/internal/contract-normalization.js.map +1 -1
  24. package/dist/internal/database-facade.d.ts +19 -0
  25. package/dist/internal/database-facade.d.ts.map +1 -0
  26. package/dist/internal/database-facade.js +79 -0
  27. package/dist/internal/database-facade.js.map +1 -0
  28. package/dist/internal/database-factory.d.ts +4 -48
  29. package/dist/internal/database-factory.d.ts.map +1 -1
  30. package/dist/internal/database-factory.js +11 -90
  31. package/dist/internal/database-factory.js.map +1 -1
  32. package/dist/internal/database-types.d.ts +33 -0
  33. package/dist/internal/database-types.d.ts.map +1 -0
  34. package/dist/internal/database-types.js +2 -0
  35. package/dist/internal/database-types.js.map +1 -0
  36. package/dist/internal/dynamic.d.ts +5 -0
  37. package/dist/internal/dynamic.d.ts.map +1 -0
  38. package/dist/internal/dynamic.js +12 -0
  39. package/dist/internal/dynamic.js.map +1 -0
  40. package/dist/internal/executor.d.ts +3 -7
  41. package/dist/internal/executor.d.ts.map +1 -1
  42. package/dist/internal/promise.d.ts +1 -1
  43. package/dist/internal/promise.d.ts.map +1 -1
  44. package/dist/internal/promise.js +2 -4
  45. package/dist/internal/promise.js.map +1 -1
  46. package/dist/internal/query-execution.d.ts +2 -2
  47. package/dist/internal/query-execution.d.ts.map +1 -1
  48. package/dist/internal/query-execution.js +2 -6
  49. package/dist/internal/query-execution.js.map +1 -1
  50. package/dist/internal/recipe.d.ts +3 -3
  51. package/dist/internal/recipe.d.ts.map +1 -1
  52. package/dist/internal/recipe.js +9 -20
  53. package/dist/internal/recipe.js.map +1 -1
  54. package/dist/internal/relation-plan.d.ts +1 -1
  55. package/dist/internal/relation-result.d.ts +4 -0
  56. package/dist/internal/relation-result.d.ts.map +1 -0
  57. package/dist/internal/relation-result.js +19 -0
  58. package/dist/internal/relation-result.js.map +1 -0
  59. package/dist/internal/relation-runtime.d.ts +3 -3
  60. package/dist/internal/relation-runtime.d.ts.map +1 -1
  61. package/dist/internal/relation-runtime.js +12 -37
  62. package/dist/internal/relation-runtime.js.map +1 -1
  63. package/dist/internal/relation-stream.d.ts +3 -3
  64. package/dist/internal/relation-stream.d.ts.map +1 -1
  65. package/dist/internal/relation-stream.js +20 -24
  66. package/dist/internal/relation-stream.js.map +1 -1
  67. package/dist/internal/sqlite-datetime.d.ts +1 -27
  68. package/dist/internal/sqlite-datetime.d.ts.map +1 -1
  69. package/dist/internal/sqlite-datetime.js +16 -35
  70. package/dist/internal/sqlite-datetime.js.map +1 -1
  71. package/dist/internal/sqlite-pragmas.d.ts +1 -13
  72. package/dist/internal/sqlite-pragmas.d.ts.map +1 -1
  73. package/dist/internal/sqlite-pragmas.js +2 -12
  74. package/dist/internal/sqlite-pragmas.js.map +1 -1
  75. package/dist/internal/testing.d.ts +1 -1
  76. package/dist/internal/testing.d.ts.map +1 -1
  77. package/dist/internal/testing.js +5 -2
  78. package/dist/internal/testing.js.map +1 -1
  79. package/dist/internal/transaction.d.ts +5 -5
  80. package/dist/internal/transaction.d.ts.map +1 -1
  81. package/dist/internal/transaction.js +5 -53
  82. package/dist/internal/transaction.js.map +1 -1
  83. package/dist/relation/include-metadata.d.ts +1 -1
  84. package/dist/relation/include-metadata.d.ts.map +1 -1
  85. package/dist/relation/include.d.ts +8 -5
  86. package/dist/relation/include.d.ts.map +1 -1
  87. package/dist/relation/prisma-methods.d.ts +17 -17
  88. package/dist/relation/prisma-methods.d.ts.map +1 -1
  89. package/dist/relation.d.ts +4 -4
  90. package/dist/relation.d.ts.map +1 -1
  91. package/dist/sqlite.d.ts +5 -10
  92. package/dist/sqlite.d.ts.map +1 -1
  93. package/dist/sqlite.js +14 -12
  94. package/dist/sqlite.js.map +1 -1
  95. package/dist/testing/transaction.d.ts +6 -7
  96. package/dist/testing/transaction.d.ts.map +1 -1
  97. package/dist/testing/transaction.js +2 -2
  98. package/dist/testing/transaction.js.map +1 -1
  99. package/dist/testing/types.d.ts +7 -7
  100. package/dist/testing/types.d.ts.map +1 -1
  101. package/dist/testing/vitest.d.ts +3 -3
  102. package/dist/testing/vitest.d.ts.map +1 -1
  103. package/dist/testing/vitest.js +2 -2
  104. package/dist/testing/vitest.js.map +1 -1
  105. package/dist/testing.d.ts +3 -3
  106. package/package.json +35 -37
  107. package/src/bin/normalize-contract.ts +2 -1
  108. package/src/database.ts +49 -67
  109. package/src/error.ts +13 -23
  110. package/src/index.ts +3 -3
  111. package/src/internal/adapters/transaction-settlement.ts +70 -0
  112. package/src/internal/client-lifecycle.ts +4 -11
  113. package/src/internal/contract-normalization.ts +6 -14
  114. package/src/internal/database-facade.ts +138 -0
  115. package/src/internal/database-factory.ts +34 -306
  116. package/src/internal/database-types.ts +65 -0
  117. package/src/internal/dynamic.ts +16 -0
  118. package/src/internal/executor.ts +4 -11
  119. package/src/internal/promise.ts +4 -12
  120. package/src/internal/query-execution.ts +4 -8
  121. package/src/internal/recipe.ts +24 -89
  122. package/src/internal/relation-plan.ts +1 -1
  123. package/src/internal/relation-result.ts +27 -0
  124. package/src/internal/relation-runtime.ts +45 -135
  125. package/src/internal/relation-stream.ts +24 -51
  126. package/src/internal/sqlite-datetime.ts +20 -54
  127. package/src/internal/sqlite-pragmas.ts +3 -16
  128. package/src/internal/testing.ts +13 -15
  129. package/src/internal/transaction.ts +16 -109
  130. package/src/relation/include-metadata.ts +11 -43
  131. package/src/relation/include.ts +46 -140
  132. package/src/relation/prisma-methods.ts +36 -122
  133. package/src/relation.ts +20 -48
  134. package/src/sqlite.ts +59 -85
  135. package/src/testing/transaction.ts +20 -33
  136. package/src/testing/types.ts +15 -34
  137. package/src/testing/vitest.ts +14 -28
  138. package/src/testing.ts +3 -3
@@ -9,57 +9,31 @@ import type {
9
9
  Collection as PrismaCollection,
10
10
  } from "@prisma-next/sql-orm-client";
11
11
  import type { Effect, Stream } from "effect";
12
- import type { PrismaError } from "../error.js";
13
- import type { CollectionResult, Relation, RelationQuery } from "../relation.js";
14
- import type { IncludeMethod } from "./include.js";
12
+ import type { PrismaError } from "../error.ts";
13
+ import type { CollectionResult, Relation, RelationQuery } from "../relation.ts";
14
+ import type { IncludeMethod } from "./include.ts";
15
15
 
16
- type AnyFunction = (...arguments_: ReadonlyArray<never>) => unknown;
16
+ type AnyFunction = (...arguments_: readonly never[]) => unknown;
17
17
  type AnyPostgresContract = PrismaContract<SqlStorage>;
18
18
  type FieldTuple<Row> = readonly [keyof Row & string, ...(keyof Row & string)[]];
19
19
  type Simplify<Value> = { [Key in keyof Value]: Value[Key] };
20
20
 
21
- type TerminalMethod<Method> = Method extends (
22
- ...arguments_: infer Arguments
23
- ) => PromiseLike<infer Value>
21
+ type TerminalMethod<Method> = Method extends (...arguments_: infer Arguments) => PromiseLike<infer Value>
24
22
  ? (...arguments_: Arguments) => Effect.Effect<Awaited<Value>, PrismaError>
25
23
  : never;
26
24
 
27
- type SelectMethod<
28
- Collection,
29
- Contract,
30
- Model extends string,
31
- DatabaseId,
32
- > = Contract extends AnyPostgresContract
33
- ? Collection extends PrismaCollection<
34
- Contract,
35
- Model,
36
- infer _Row,
37
- infer State
38
- >
25
+ type SelectMethod<Collection, Contract, Model extends string, DatabaseId> = Contract extends AnyPostgresContract
26
+ ? Collection extends PrismaCollection<Contract, Model, infer _Row, infer State>
39
27
  ? {
40
- select<Fields extends FieldTuple<DefaultModelRow<Contract, Model>>>(
28
+ select: <Fields extends FieldTuple<DefaultModelRow<Contract, Model>>>(
41
29
  ...fields: Fields
42
- ): Relation<
43
- PrismaCollection<
44
- Contract,
45
- Model,
46
- Pick<DefaultModelRow<Contract, Model>, Fields[number]>,
47
- State
48
- >,
49
- Contract,
50
- Model,
51
- DatabaseId
52
- >;
30
+ ) => Relation<PrismaCollection<Contract, Model, Pick<DefaultModelRow<Contract, Model>, Fields[number]>, State>, Contract, Model, DatabaseId>;
53
31
  }
54
32
  : Record<never, never>
55
33
  : Collection extends { select: infer Select extends AnyFunction }
56
34
  ? {
57
35
  select: Select extends (...arguments_: infer Arguments) => infer Result
58
- ? (
59
- ...arguments_: Arguments
60
- ) => Result extends object
61
- ? Relation<Result, Contract, Model, DatabaseId>
62
- : never
36
+ ? (...arguments_: Arguments) => Result extends object ? Relation<Result, Contract, Model, DatabaseId> : never
63
37
  : never;
64
38
  }
65
39
  : Record<never, never>;
@@ -70,122 +44,62 @@ type AggregateConfigure<Collection> = Collection extends {
70
44
  ? Exclude<Parameters<Method>[1], undefined>
71
45
  : never;
72
46
 
73
- type AggregateSuccess<
74
- Collection,
75
- Contract extends AnyPostgresContract,
76
- Model extends string,
77
- Spec extends AggregateSpec,
78
- > =
79
- Collection extends GroupedCollection<
80
- Contract,
81
- Model,
82
- infer Fields extends FieldTuple<DefaultModelRow<Contract, Model>>
83
- >
84
- ? Array<
85
- Simplify<
86
- Pick<DefaultModelRow<Contract, Model>, Fields[number]> &
87
- AggregateResult<Spec>
88
- >
89
- >
47
+ type AggregateSuccess<Collection, Contract extends AnyPostgresContract, Model extends string, Spec extends AggregateSpec> =
48
+ Collection extends GroupedCollection<Contract, Model, infer Fields extends FieldTuple<DefaultModelRow<Contract, Model>>>
49
+ ? Simplify<Pick<DefaultModelRow<Contract, Model>, Fields[number]> & AggregateResult<Spec>>[]
90
50
  : AggregateResult<Spec>;
91
51
 
92
- type AggregateMethod<
93
- Collection,
94
- Contract,
95
- Model extends string,
96
- > = Contract extends AnyPostgresContract
52
+ type AggregateMethod<Collection, Contract, Model extends string> = Contract extends AnyPostgresContract
97
53
  ? Collection extends { aggregate: AnyFunction }
98
54
  ? {
99
- aggregate<Spec extends AggregateSpec>(
55
+ aggregate: <Spec extends AggregateSpec>(
100
56
  make: (aggregate: AggregateBuilder<Contract, Model>) => Spec,
101
57
  configure?: AggregateConfigure<Collection>,
102
- ): Effect.Effect<
103
- AggregateSuccess<Collection, Contract, Model, Spec>,
104
- PrismaError
105
- >;
58
+ ) => Effect.Effect<AggregateSuccess<Collection, Contract, Model, Spec>, PrismaError>;
106
59
  }
107
60
  : Record<never, never>
108
61
  : Record<never, never>;
109
62
 
110
- type CollectionMethods<
111
- Collection,
112
- Contract,
113
- Model extends string,
114
- DatabaseId,
115
- > = Contract extends AnyPostgresContract
116
- ? Collection extends PrismaCollection<
117
- Contract,
118
- Model,
119
- infer _Row,
120
- infer State
121
- >
63
+ type CollectionMethods<Collection, Contract, Model extends string, DatabaseId> = Contract extends AnyPostgresContract
64
+ ? Collection extends PrismaCollection<Contract, Model, infer _Row, infer State>
122
65
  ? {
123
- groupBy<Fields extends FieldTuple<DefaultModelRow<Contract, Model>>>(
66
+ groupBy: <Fields extends FieldTuple<DefaultModelRow<Contract, Model>>>(
124
67
  ...fields: Fields
125
- ): Relation<
126
- GroupedCollection<Contract, Model, Fields>,
127
- Contract,
128
- Model,
129
- DatabaseId
130
- >;
131
- cursor(
132
- values: State extends { readonly hasOrderBy: true }
133
- ? Partial<
134
- Record<keyof DefaultModelRow<Contract, Model> & string, unknown>
135
- >
136
- : never,
137
- ): Relation<Collection, Contract, Model, DatabaseId>;
138
- distinct<Fields extends FieldTuple<DefaultModelRow<Contract, Model>>>(
68
+ ) => Relation<GroupedCollection<Contract, Model, Fields>, Contract, Model, DatabaseId>;
69
+ cursor: (
70
+ values: State extends { readonly hasOrderBy: true } ? Partial<Record<keyof DefaultModelRow<Contract, Model> & string, unknown>> : never,
71
+ ) => Relation<Collection, Contract, Model, DatabaseId>;
72
+ distinct: <Fields extends FieldTuple<DefaultModelRow<Contract, Model>>>(
139
73
  ...fields: Fields
140
- ): Relation<Collection, Contract, Model, DatabaseId>;
141
- distinctOn<Fields extends FieldTuple<DefaultModelRow<Contract, Model>>>(
142
- ...fields: State extends { readonly hasOrderBy: true }
143
- ? Fields
144
- : never
145
- ): Relation<Collection, Contract, Model, DatabaseId>;
74
+ ) => Relation<Collection, Contract, Model, DatabaseId>;
75
+ distinctOn: <Fields extends FieldTuple<DefaultModelRow<Contract, Model>>>(
76
+ ...fields: State extends { readonly hasOrderBy: true } ? Fields : never
77
+ ) => Relation<Collection, Contract, Model, DatabaseId>;
146
78
  } & (State extends { readonly hasWhere: true }
147
79
  ? {
148
- readonly [Key in
149
- | "delete"
150
- | "deleteAll"
151
- | "deleteCount"]: TerminalMethod<Collection[Key]>;
80
+ readonly [Key in "delete" | "deleteAll" | "deleteCount"]: TerminalMethod<Collection[Key]>;
152
81
  }
153
82
  : Record<never, never>)
154
83
  : Record<never, never>
155
84
  : Record<never, never>;
156
85
 
157
- type CollectionConveniences<
158
- Collection,
159
- Contract,
160
- Model extends string,
161
- DatabaseId,
162
- > = Contract extends AnyPostgresContract
163
- ? Collection extends PrismaCollection<
164
- Contract,
165
- Model,
166
- infer Row,
167
- infer _State
168
- >
86
+ type CollectionConveniences<Collection, Contract, Model extends string, DatabaseId> = Contract extends AnyPostgresContract
87
+ ? Collection extends PrismaCollection<Contract, Model, infer Row, infer _State>
169
88
  ? {
170
89
  readonly stream: Stream.Stream<Row, PrismaError>;
171
- count(): RelationQuery<number, Contract, Model, DatabaseId>;
172
- exists(): Effect.Effect<boolean, PrismaError>;
90
+ count: () => RelationQuery<number, Contract, Model, DatabaseId>;
91
+ exists: () => Effect.Effect<boolean, PrismaError>;
173
92
  }
174
93
  : Record<never, never>
175
94
  : CollectionResult<Collection> extends ReadonlyArray<infer Row>
176
95
  ? {
177
96
  readonly stream: Stream.Stream<Row, PrismaError>;
178
- count(): Effect.Effect<number, PrismaError>;
179
- exists(): Effect.Effect<boolean, PrismaError>;
97
+ count: () => Effect.Effect<number, PrismaError>;
98
+ exists: () => Effect.Effect<boolean, PrismaError>;
180
99
  }
181
100
  : Record<never, never>;
182
101
 
183
- export type PrismaRelationMethods<
184
- Collection,
185
- Contract,
186
- Model extends string,
187
- DatabaseId,
188
- > = SelectMethod<Collection, Contract, Model, DatabaseId> &
102
+ export type PrismaRelationMethods<Collection, Contract, Model extends string, DatabaseId> = SelectMethod<Collection, Contract, Model, DatabaseId> &
189
103
  IncludeMethod<Collection, Contract, Model, DatabaseId> &
190
104
  AggregateMethod<Collection, Contract, Model> &
191
105
  CollectionMethods<Collection, Contract, Model, DatabaseId> &
package/src/relation.ts CHANGED
@@ -1,18 +1,16 @@
1
1
  import type { Effect } from "effect";
2
- import type { PrismaError } from "./error.js";
3
- import type { PrismaRelationMethods } from "./relation/prisma-methods.js";
2
+ import type { PrismaError } from "./error.ts";
3
+ import type { PrismaRelationMethods } from "./relation/prisma-methods.ts";
4
4
 
5
- type AnyFunction = (...arguments_: ReadonlyArray<never>) => unknown;
5
+ type AnyFunction = (...arguments_: readonly never[]) => unknown;
6
6
 
7
7
  export type CollectionResult<Collection> = Collection extends {
8
- all(): infer Result;
8
+ all: () => infer Result;
9
9
  }
10
10
  ? Awaited<Result>
11
11
  : never;
12
12
 
13
- type NormalizeTerminal<Name, Result> = Name extends "first"
14
- ? import("effect").Option.Option<Exclude<Result, null>>
15
- : Result;
13
+ type NormalizeTerminal<Name, Result> = Name extends "first" ? import("effect").Option.Option<Exclude<Result, null>> : Result;
16
14
 
17
15
  type WrapReturn<Name, Result, Contract, Model extends string, DatabaseId> =
18
16
  Result extends PromiseLike<infer Value>
@@ -21,13 +19,7 @@ type WrapReturn<Name, Result, Contract, Model extends string, DatabaseId> =
21
19
  ? Relation<Result, Contract, Model, DatabaseId>
22
20
  : never;
23
21
 
24
- type WrapFunction<
25
- Name,
26
- Function_,
27
- Contract,
28
- Model extends string,
29
- DatabaseId,
30
- > = Function_ extends {
22
+ type WrapFunction<Name, Function_, Contract, Model extends string, DatabaseId> = Function_ extends {
31
23
  (...arguments_: infer Arguments1): infer Result1;
32
24
  (...arguments_: infer Arguments2): infer Result2;
33
25
  (...arguments_: infer Arguments3): infer Result3;
@@ -35,24 +27,12 @@ type WrapFunction<
35
27
  (...arguments_: infer Arguments5): infer Result5;
36
28
  (...arguments_: infer Arguments6): infer Result6;
37
29
  }
38
- ? ((
39
- ...arguments_: Arguments1
40
- ) => WrapReturn<Name, Result1, Contract, Model, DatabaseId>) &
41
- ((
42
- ...arguments_: Arguments2
43
- ) => WrapReturn<Name, Result2, Contract, Model, DatabaseId>) &
44
- ((
45
- ...arguments_: Arguments3
46
- ) => WrapReturn<Name, Result3, Contract, Model, DatabaseId>) &
47
- ((
48
- ...arguments_: Arguments4
49
- ) => WrapReturn<Name, Result4, Contract, Model, DatabaseId>) &
50
- ((
51
- ...arguments_: Arguments5
52
- ) => WrapReturn<Name, Result5, Contract, Model, DatabaseId>) &
53
- ((
54
- ...arguments_: Arguments6
55
- ) => WrapReturn<Name, Result6, Contract, Model, DatabaseId>)
30
+ ? ((...arguments_: Arguments1) => WrapReturn<Name, Result1, Contract, Model, DatabaseId>) &
31
+ ((...arguments_: Arguments2) => WrapReturn<Name, Result2, Contract, Model, DatabaseId>) &
32
+ ((...arguments_: Arguments3) => WrapReturn<Name, Result3, Contract, Model, DatabaseId>) &
33
+ ((...arguments_: Arguments4) => WrapReturn<Name, Result4, Contract, Model, DatabaseId>) &
34
+ ((...arguments_: Arguments5) => WrapReturn<Name, Result5, Contract, Model, DatabaseId>) &
35
+ ((...arguments_: Arguments6) => WrapReturn<Name, Result6, Contract, Model, DatabaseId>)
56
36
  : never;
57
37
 
58
38
  type FunctionKeys<Value> = {
@@ -79,20 +59,12 @@ type ExplicitPrismaMethod =
79
59
  | "variant";
80
60
 
81
61
  type RelationMethods<Collection, Contract, Model extends string, DatabaseId> = {
82
- readonly [Key in Exclude<
83
- FunctionKeys<Collection>,
84
- ExplicitPrismaMethod
85
- >]: WrapFunction<Key, Collection[Key], Contract, Model, DatabaseId>;
62
+ readonly [Key in Exclude<FunctionKeys<Collection>, ExplicitPrismaMethod>]: WrapFunction<Key, Collection[Key], Contract, Model, DatabaseId>;
86
63
  };
87
64
 
88
65
  declare const RelationQueryTypeId: unique symbol;
89
66
 
90
- export type RelationQuery<
91
- Value,
92
- Contract,
93
- Model extends string,
94
- DatabaseId,
95
- > = Effect.Effect<Value, PrismaError> & {
67
+ export type RelationQuery<Value, Contract, Model extends string, DatabaseId> = Effect.Effect<Value, PrismaError> & {
96
68
  readonly [RelationQueryTypeId]: {
97
69
  readonly contract: Contract;
98
70
  readonly database: (database: DatabaseId) => DatabaseId;
@@ -100,11 +72,11 @@ export type RelationQuery<
100
72
  };
101
73
  };
102
74
 
103
- export type Relation<
104
- Collection,
105
- Contract = undefined,
106
- Model extends string = string,
107
- DatabaseId = undefined,
108
- > = RelationQuery<CollectionResult<Collection>, Contract, Model, DatabaseId> &
75
+ export type Relation<Collection, Contract = undefined, Model extends string = string, DatabaseId = undefined> = RelationQuery<
76
+ CollectionResult<Collection>,
77
+ Contract,
78
+ Model,
79
+ DatabaseId
80
+ > &
109
81
  RelationMethods<Collection, Contract, Model, DatabaseId> &
110
82
  PrismaRelationMethods<Collection, Contract, Model, DatabaseId>;
package/src/sqlite.ts CHANGED
@@ -1,38 +1,20 @@
1
- import sqlite, {
2
- type SqliteClient,
3
- type SqliteOptionsBase,
4
- } from "@prisma-next/sqlite/runtime";
1
+ import sqlite, { type SqliteClient, type SqliteOptionsBase } from "@prisma-next/sqlite/runtime";
5
2
  import { type Layer, Semaphore } from "effect";
6
- import type { PrismaError } from "./error.js";
7
- import {
8
- acquireConnectedClient,
9
- assertAvailableModelNames,
10
- } from "./internal/client-lifecycle.js";
11
- import {
12
- type DatabaseIdentifier,
13
- type DatabaseIdentifierLiteral,
14
- type DatabaseServiceHolder,
15
- type DefaultModels,
16
- makeSqlDatabase,
17
- } from "./internal/database-factory.js";
18
- import type { AnySqlContract } from "./internal/executor.js";
19
- import { fromPrismaPromise } from "./internal/promise.js";
20
- import { decodeSqliteDatetimesAsUtc } from "./internal/sqlite-datetime.js";
21
- import {
22
- applySqlitePragmas,
23
- assertFileBackedPath,
24
- } from "./internal/sqlite-pragmas.js";
3
+ import type { PrismaError } from "./error.ts";
4
+ import { acquireConnectedClient, assertAvailableModelNames } from "./internal/client-lifecycle.ts";
5
+ import { contractModels } from "./internal/database-facade.ts";
6
+ import { makeSqlDatabase } from "./internal/database-factory.ts";
7
+ import type { DatabaseIdentifier, DatabaseIdentifierLiteral, DatabaseServiceHolder, DefaultModels } from "./internal/database-types.ts";
8
+ import type { AnySqlContract } from "./internal/executor.ts";
9
+ import { fromPrismaPromise } from "./internal/promise.ts";
10
+ import { decodeSqliteDatetimesAsUtc } from "./internal/sqlite-datetime.ts";
11
+ import { applySqlitePragmas, assertFileBackedPath } from "./internal/sqlite-pragmas.ts";
25
12
 
26
- export const defaultSqlitePragmas: ReadonlyArray<string> = ["journal_mode=WAL"];
13
+ export const defaultSqlitePragmas: readonly string[] = ["journal_mode=WAL"];
27
14
 
28
15
  export interface SqliteDatabaseLayerOptions extends SqliteOptionsBase {
29
- /** Path to the database file. In-memory databases are not supported. */
30
16
  readonly path: string;
31
- /**
32
- * Pragmas applied once when the Layer connects. Defaults to
33
- * {@link defaultSqlitePragmas}; pass `[]` to skip them.
34
- */
35
- readonly pragmas?: ReadonlyArray<string>;
17
+ readonly pragmas?: readonly string[];
36
18
  }
37
19
 
38
20
  type SqliteFactoryOptions<Contract extends AnySqlContract> =
@@ -45,15 +27,18 @@ type SqliteFactoryOptions<Contract extends AnySqlContract> =
45
27
  readonly contract?: never;
46
28
  };
47
29
 
48
- export interface SqliteDatabaseDefinition<
49
- Contract extends AnySqlContract,
50
- Identifier extends string,
51
- > extends DatabaseServiceHolder<Contract, Identifier> {
52
- readonly layer: (
53
- options: SqliteDatabaseLayerOptions,
54
- ) => Layer.Layer<DatabaseIdentifier<Contract, Identifier>, PrismaError>;
30
+ export interface SqliteDatabaseDefinition<Contract extends AnySqlContract, Identifier extends string>
31
+ extends DatabaseServiceHolder<Contract, Identifier> {
32
+ readonly layer: (options: SqliteDatabaseLayerOptions) => Layer.Layer<DatabaseIdentifier<Contract, Identifier>, PrismaError>;
55
33
  }
56
34
 
35
+ const clientOptions = ({ path, extensions, middleware, verifyMarker }: SqliteDatabaseLayerOptions) => ({
36
+ path,
37
+ ...(extensions === undefined ? {} : { extensions }),
38
+ ...(middleware === undefined ? {} : { middleware }),
39
+ ...(verifyMarker === undefined ? {} : { verifyMarker }),
40
+ });
41
+
57
42
  export const makeSqliteDatabase =
58
43
  <const Contract extends AnySqlContract>() =>
59
44
  <const Identifier extends string>(
@@ -62,55 +47,44 @@ export const makeSqliteDatabase =
62
47
  ): SqliteDatabaseDefinition<Contract, Identifier> => {
63
48
  type Models = DefaultModels<Contract>;
64
49
 
65
- return makeSqlDatabase<Contract, Identifier, SqliteDatabaseLayerOptions>(
66
- identifier,
67
- (layerOptions) => {
68
- assertFileBackedPath(layerOptions.path);
69
- applySqlitePragmas(
70
- layerOptions.path,
71
- layerOptions.pragmas ?? defaultSqlitePragmas,
72
- );
50
+ return makeSqlDatabase<Contract, Identifier, SqliteDatabaseLayerOptions>(identifier, (layerOptions) => {
51
+ assertFileBackedPath(layerOptions.path);
52
+ applySqlitePragmas(layerOptions.path, layerOptions.pragmas ?? defaultSqlitePragmas);
73
53
 
74
- const clientOptions = {
75
- path: layerOptions.path,
76
- extensions: layerOptions.extensions,
77
- middleware: layerOptions.middleware,
78
- verifyMarker: layerOptions.verifyMarker,
79
- };
80
- const client: SqliteClient<Contract> =
81
- options.contract === undefined
82
- ? sqlite<Contract>({
83
- ...clientOptions,
84
- contractJson: options.contractJson,
85
- })
86
- : sqlite<Contract>({
87
- ...clientOptions,
88
- contract: options.contract,
89
- });
54
+ const forwarded = clientOptions(layerOptions);
55
+ const client: SqliteClient<Contract> =
56
+ options.contract === undefined
57
+ ? sqlite<Contract>({
58
+ ...forwarded,
59
+ contractJson: options.contractJson,
60
+ })
61
+ : sqlite<Contract>({
62
+ ...forwarded,
63
+ contract: options.contract,
64
+ });
90
65
 
91
- decodeSqliteDatetimesAsUtc(client.context);
66
+ decodeSqliteDatetimesAsUtc(client.context);
92
67
 
93
- return fromPrismaPromise(() =>
94
- acquireConnectedClient(client, () => {
95
- // The SQLite client already exposes the unbound namespace.
96
- const models = client.orm as Models;
97
- const accessSemaphore = Semaphore.makeUnsafe(1);
98
- assertAvailableModelNames(Object.keys(models));
99
- return {
100
- client,
101
- identity: {},
102
- liveness: {
103
- closedCode: "RUNTIME.DATABASE_CLOSED",
104
- open: true,
105
- },
106
- mode: "root",
107
- models,
108
- querySemaphore: accessSemaphore,
109
- transactionIdentity: undefined,
110
- transactionSemaphore: accessSemaphore,
111
- };
112
- }),
113
- );
114
- },
115
- ) as SqliteDatabaseDefinition<Contract, Identifier>;
68
+ return fromPrismaPromise(() =>
69
+ acquireConnectedClient(client, () => {
70
+ // The SQLite client already exposes the unbound namespace.
71
+ const models = contractModels<Models>(client.orm);
72
+ const accessSemaphore = Semaphore.makeUnsafe(1);
73
+ assertAvailableModelNames(Object.keys(models));
74
+ return {
75
+ client,
76
+ identity: {},
77
+ liveness: {
78
+ closedCode: "RUNTIME.DATABASE_CLOSED",
79
+ open: true,
80
+ },
81
+ mode: "root",
82
+ models,
83
+ querySemaphore: accessSemaphore,
84
+ transactionIdentity: undefined,
85
+ transactionSemaphore: accessSemaphore,
86
+ };
87
+ }),
88
+ );
89
+ });
116
90
  };
@@ -1,38 +1,25 @@
1
1
  import type { Effect } from "effect";
2
- import type { PrismaError } from "../error.js";
3
- import { getDatabaseTesting } from "../internal/testing.js";
4
- import type { AnyDatabase } from "./types.js";
2
+ import type { PrismaError } from "../error.ts";
3
+ import { getDatabaseTesting } from "../internal/testing.ts";
4
+ import type { AnyDatabase } from "./types.ts";
5
5
 
6
- type WithTestTransaction = {
7
- <Database extends AnyDatabase>(
8
- database: Database,
9
- ): <A, E, R>(
10
- program: Effect.Effect<A, E, R> &
11
- (Effect.Services<Database> extends R ? unknown : never),
12
- ) => Effect.Effect<
13
- A,
14
- E | PrismaError,
15
- Effect.Services<Database> | Exclude<R, Effect.Services<Database>>
16
- >;
17
- <Database extends AnyDatabase, A, E, R>(
18
- database: Database,
19
- program: Effect.Effect<A, E, R> &
20
- (Effect.Services<Database> extends R ? unknown : never),
21
- ): Effect.Effect<
22
- A,
23
- E | PrismaError,
24
- Effect.Services<Database> | Exclude<R, Effect.Services<Database>>
25
- >;
26
- };
6
+ type TestProgram<Database extends AnyDatabase, A, E, R> = Effect.Effect<A, E, R> & (Effect.Services<Database> extends R ? unknown : never);
27
7
 
28
- export const withTestTransaction: WithTestTransaction = ((
29
- database: AnyDatabase,
30
- program?: Effect.Effect<unknown, unknown, unknown>,
31
- ) => {
32
- const run = (effect: Effect.Effect<unknown, unknown, unknown>) =>
33
- getDatabaseTesting<Effect.Services<typeof database>>(
34
- database,
35
- ).withTestTransaction(effect);
8
+ type TestTransaction<Database extends AnyDatabase, A, E, R> = Effect.Effect<
9
+ A,
10
+ E | PrismaError,
11
+ Effect.Services<Database> | Exclude<R, Effect.Services<Database>>
12
+ >;
13
+
14
+ export function withTestTransaction<Database extends AnyDatabase>(
15
+ database: Database,
16
+ ): <A, E, R>(program: TestProgram<Database, A, E, R>) => TestTransaction<Database, A, E, R>;
17
+ export function withTestTransaction<Database extends AnyDatabase, A, E, R>(
18
+ database: Database,
19
+ program: TestProgram<Database, A, E, R>,
20
+ ): TestTransaction<Database, A, E, R>;
21
+ export function withTestTransaction(database: AnyDatabase, program?: Effect.Effect<unknown, unknown, unknown>): unknown {
22
+ const run = (effect: Effect.Effect<unknown, unknown, unknown>) => getDatabaseTesting<unknown>(database).withTestTransaction(effect);
36
23
 
37
24
  return program === undefined ? run : run(program);
38
- }) as WithTestTransaction;
25
+ }
@@ -1,56 +1,37 @@
1
1
  import type { TestContext, Vitest } from "@effect/vitest";
2
- import type { EffectClock, EffectTestOptions } from "@shivaedev/effect-test";
3
2
  import type { Effect, Layer } from "effect";
4
- import type { AnyDatabase, DatabaseServiceOf } from "../database.js";
3
+ import type { EffectClock, EffectTestOptions } from "@shivaedev/effect-test";
4
+ import type { AnyDatabase, DatabaseServiceOf } from "../database.ts";
5
5
 
6
- export type { AnyDatabase } from "../database.js";
7
- export type DatabaseService<Database extends AnyDatabase> =
8
- DatabaseServiceOf<Database>;
6
+ export type { AnyDatabase } from "../database.ts";
7
+ export type DatabaseService<Database extends AnyDatabase> = DatabaseServiceOf<Database>;
9
8
 
10
- export type DatabaseTest<Database extends AnyDatabase, Provided> = <
11
- A,
12
- Eff extends Effect.Effect<unknown, unknown, Provided>,
13
- >(
9
+ export type DatabaseTest<Database extends AnyDatabase, Provided> = <A, Eff extends Effect.Effect<unknown, unknown, Provided>>(
14
10
  name: string,
15
- body: (
16
- database: DatabaseService<Database>,
17
- context: TestContext,
18
- ) => Generator<Eff, A, never>,
11
+ body: (database: DatabaseService<Database>, context: TestContext) => Generator<Eff, A, never>,
19
12
  options?: number | EffectTestOptions,
20
13
  ) => void;
21
14
 
22
- export interface DatabaseTester<Database extends AnyDatabase, Provided>
23
- extends DatabaseTest<Database, Provided> {
24
- readonly skip: DatabaseTest<Database, Provided>;
25
- readonly skipIf: (condition: unknown) => DatabaseTest<Database, Provided>;
26
- readonly runIf: (condition: unknown) => DatabaseTest<Database, Provided>;
27
- readonly only: DatabaseTest<Database, Provided>;
15
+ export interface DatabaseTester<Database extends AnyDatabase, Provided> extends DatabaseTest<Database, Provided> {
28
16
  readonly each: <Item>(
29
- cases: ReadonlyArray<Item>,
17
+ cases: readonly Item[],
30
18
  ) => <A, Eff extends Effect.Effect<unknown, unknown, Provided>>(
31
19
  name: string,
32
- body: (
33
- item: Item,
34
- database: DatabaseService<Database>,
35
- context: TestContext,
36
- ) => Generator<Eff, A, never>,
20
+ body: (item: Item, database: DatabaseService<Database>, context: TestContext) => Generator<Eff, A, never>,
37
21
  options?: number | EffectTestOptions,
38
22
  ) => void;
39
23
  readonly fails: DatabaseTest<Database, Provided>;
24
+ readonly only: DatabaseTest<Database, Provided>;
25
+ readonly runIf: (condition: unknown) => DatabaseTest<Database, Provided>;
26
+ readonly skip: DatabaseTest<Database, Provided>;
27
+ readonly skipIf: (condition: unknown) => DatabaseTest<Database, Provided>;
40
28
  }
41
29
 
42
- export type DatabaseIt<
43
- Database extends AnyDatabase,
44
- Provided,
45
- > = Vitest.Methods & {
30
+ export type DatabaseIt<Database extends AnyDatabase, Provided> = Vitest.Methods & {
46
31
  readonly effectDB: DatabaseTester<Database, Provided>;
47
32
  };
48
33
 
49
- export interface MakeDatabaseItOptions<
50
- Database extends AnyDatabase,
51
- Provided,
52
- LayerError,
53
- > {
34
+ export interface MakeDatabaseItOptions<Database extends AnyDatabase, Provided, LayerError> {
54
35
  readonly clock?: EffectClock;
55
36
  readonly database: Database;
56
37
  readonly layer: Layer.Layer<Provided, LayerError>;