MCPcopy
hub / github.com/Effect-TS/effect / make

Function make

packages/sql-d1/src/D1Client.ts:70–184  ·  view source on GitHub ↗
(
  options: D1ClientConfig
)

Source from the content-addressed store, hash-verified

68 * @since 1.0.0
69 */
70export const make = (
71 options: D1ClientConfig
72): Effect.Effect<D1Client, never, Scope.Scope | Reactivity.Reactivity> =>
73 Effect.gen(function*() {
74 const compiler = Statement.makeCompilerSqlite(options.transformQueryNames)
75 const transformRows = options.transformResultNames ?
76 Statement.defaultTransforms(options.transformResultNames).array :
77 undefined
78
79 const makeConnection = Effect.gen(function*() {
80 const db = options.db
81
82 const prepareCache = yield* Cache.make({
83 capacity: options.prepareCacheSize ?? 200,
84 timeToLive: options.prepareCacheTTL ?? Duration.minutes(10),
85 lookup: (sql: string) =>
86 Effect.try({
87 try: () => db.prepare(sql),
88 catch: (cause) => new SqlError({ cause, message: `Failed to prepare statement` })
89 })
90 })
91
92 const runStatement = (
93 statement: D1PreparedStatement,
94 params: ReadonlyArray<unknown> = []
95 ): Effect.Effect<ReadonlyArray<any>, SqlError, never> =>
96 Effect.tryPromise({
97 try: async () => {
98 const response = await statement.bind(...params).all()
99 if (response.error) {
100 throw response.error
101 }
102 return response.results || []
103 },
104 catch: (cause) => new SqlError({ cause, message: `Failed to execute statement` })
105 })
106
107 const runRaw = (
108 sql: string,
109 params: ReadonlyArray<unknown> = []
110 ) => runStatement(db.prepare(sql), params)
111
112 const runCached = (
113 sql: string,
114 params: ReadonlyArray<unknown> = []
115 ) => Effect.flatMap(prepareCache.get(sql), (s) => runStatement(s, params))
116
117 const runUncached = (
118 sql: string,
119 params: ReadonlyArray<unknown> = []
120 ) => runRaw(sql, params)
121
122 const runValues = (
123 sql: string,
124 params: ReadonlyArray<unknown>
125 ) =>
126 Effect.flatMap(
127 prepareCache.get(sql),

Callers 1

layerFunction · 0.70

Calls 4

identityFunction · 0.85
entriesMethod · 0.80
makeMethod · 0.65
dieMessageMethod · 0.65

Tested by

no test coverage detected