@effect/sql-pglite 4.0.0-beta.57
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/LICENSE +21 -0
- package/README.md +7 -0
- package/dist/PgliteClient.d.ts +134 -0
- package/dist/PgliteClient.d.ts.map +1 -0
- package/dist/PgliteClient.js +243 -0
- package/dist/PgliteClient.js.map +1 -0
- package/dist/PgliteMigrator.d.ts +23 -0
- package/dist/PgliteMigrator.d.ts.map +1 -0
- package/dist/PgliteMigrator.js +17 -0
- package/dist/PgliteMigrator.js.map +1 -0
- package/dist/index.d.ts +12 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +13 -0
- package/dist/index.js.map +1 -0
- package/package.json +67 -0
- package/src/PgliteClient.ts +440 -0
- package/src/PgliteMigrator.ts +33 -0
- package/src/index.ts +15 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024-present The Contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @since 1.0.0
|
|
3
|
+
*/
|
|
4
|
+
import { type PGliteInterface, type PGliteOptions } from "@electric-sql/pglite";
|
|
5
|
+
import * as Config from "effect/Config";
|
|
6
|
+
import * as Context from "effect/Context";
|
|
7
|
+
import * as Effect from "effect/Effect";
|
|
8
|
+
import * as Layer from "effect/Layer";
|
|
9
|
+
import * as Scope from "effect/Scope";
|
|
10
|
+
import * as Stream from "effect/Stream";
|
|
11
|
+
import * as Reactivity from "effect/unstable/reactivity/Reactivity";
|
|
12
|
+
import * as Client from "effect/unstable/sql/SqlClient";
|
|
13
|
+
import { SqlError } from "effect/unstable/sql/SqlError";
|
|
14
|
+
import type { Custom, Fragment } from "effect/unstable/sql/Statement";
|
|
15
|
+
import * as Statement from "effect/unstable/sql/Statement";
|
|
16
|
+
/**
|
|
17
|
+
* @category type ids
|
|
18
|
+
* @since 1.0.0
|
|
19
|
+
*/
|
|
20
|
+
export declare const TypeId: TypeId;
|
|
21
|
+
/**
|
|
22
|
+
* @category type ids
|
|
23
|
+
* @since 1.0.0
|
|
24
|
+
*/
|
|
25
|
+
export type TypeId = "~@effect/sql-pglite/PgliteClient";
|
|
26
|
+
/**
|
|
27
|
+
* @category models
|
|
28
|
+
* @since 1.0.0
|
|
29
|
+
*/
|
|
30
|
+
export interface PgliteClient extends Client.SqlClient {
|
|
31
|
+
readonly [TypeId]: TypeId;
|
|
32
|
+
readonly config: PgliteClientConfig;
|
|
33
|
+
readonly pglite: PGliteInterface;
|
|
34
|
+
readonly json: (_: unknown) => Fragment;
|
|
35
|
+
readonly listen: (channel: string) => Stream.Stream<string, SqlError>;
|
|
36
|
+
readonly notify: (channel: string, payload: string) => Effect.Effect<void, SqlError>;
|
|
37
|
+
readonly dumpDataDir: (compression?: "none" | "gzip" | "auto") => Effect.Effect<File | Blob, SqlError>;
|
|
38
|
+
readonly refreshArrayTypes: Effect.Effect<void, SqlError>;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* @category tags
|
|
42
|
+
* @since 1.0.0
|
|
43
|
+
*/
|
|
44
|
+
export declare const PgliteClient: Context.Service<PgliteClient, PgliteClient>;
|
|
45
|
+
/**
|
|
46
|
+
* @category models
|
|
47
|
+
* @since 1.0.0
|
|
48
|
+
*/
|
|
49
|
+
export type PgliteClientConfig = PgliteClientConfig.Create | PgliteClientConfig.Live;
|
|
50
|
+
/**
|
|
51
|
+
* @category models
|
|
52
|
+
* @since 1.0.0
|
|
53
|
+
*/
|
|
54
|
+
export declare namespace PgliteClientConfig {
|
|
55
|
+
/**
|
|
56
|
+
* @category models
|
|
57
|
+
* @since 1.0.0
|
|
58
|
+
*/
|
|
59
|
+
interface Base {
|
|
60
|
+
readonly spanAttributes?: Record<string, unknown> | undefined;
|
|
61
|
+
readonly transformResultNames?: ((str: string) => string) | undefined;
|
|
62
|
+
readonly transformQueryNames?: ((str: string) => string) | undefined;
|
|
63
|
+
readonly transformJson?: boolean | undefined;
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* @category models
|
|
67
|
+
* @since 1.0.0
|
|
68
|
+
*/
|
|
69
|
+
interface Create extends Base, PGliteOptions {
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* @category models
|
|
73
|
+
* @since 1.0.0
|
|
74
|
+
*/
|
|
75
|
+
interface Live extends Base {
|
|
76
|
+
readonly liveClient: PGliteInterface;
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* @category models
|
|
80
|
+
* @since 1.0.0
|
|
81
|
+
*/
|
|
82
|
+
interface ConfigBase extends Base {
|
|
83
|
+
readonly dataDir?: string | undefined;
|
|
84
|
+
readonly username?: string | undefined;
|
|
85
|
+
readonly database?: string | undefined;
|
|
86
|
+
readonly relaxedDurability?: boolean | undefined;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* @category constructor
|
|
91
|
+
* @since 1.0.0
|
|
92
|
+
*/
|
|
93
|
+
export declare const make: (options?: PgliteClientConfig) => Effect.Effect<PgliteClient, SqlError, Scope.Scope | Reactivity.Reactivity>;
|
|
94
|
+
/**
|
|
95
|
+
* @category constructor
|
|
96
|
+
* @since 1.0.0
|
|
97
|
+
*/
|
|
98
|
+
export declare const fromClient: (options: PgliteClientConfig.Base & {
|
|
99
|
+
readonly liveClient: PGliteInterface;
|
|
100
|
+
}) => Effect.Effect<PgliteClient, SqlError, Scope.Scope | Reactivity.Reactivity>;
|
|
101
|
+
/**
|
|
102
|
+
* @category layers
|
|
103
|
+
* @since 1.0.0
|
|
104
|
+
*/
|
|
105
|
+
export declare const layerFrom: <E, R>(acquire: Effect.Effect<PgliteClient, E, R>) => Layer.Layer<PgliteClient | Client.SqlClient, E, Exclude<R, Scope.Scope | Reactivity.Reactivity>>;
|
|
106
|
+
/**
|
|
107
|
+
* @category layers
|
|
108
|
+
* @since 1.0.0
|
|
109
|
+
*/
|
|
110
|
+
export declare const layerConfig: (config: Config.Wrap<PgliteClientConfig.ConfigBase>) => Layer.Layer<PgliteClient | Client.SqlClient, Config.ConfigError | SqlError>;
|
|
111
|
+
/**
|
|
112
|
+
* @category layers
|
|
113
|
+
* @since 1.0.0
|
|
114
|
+
*/
|
|
115
|
+
export declare const layer: (config?: PgliteClientConfig | undefined) => Layer.Layer<PgliteClient | Client.SqlClient, SqlError>;
|
|
116
|
+
/**
|
|
117
|
+
* @category constructor
|
|
118
|
+
* @since 1.0.0
|
|
119
|
+
*/
|
|
120
|
+
export declare const makeCompiler: (transform?: (_: string) => string, transformJson?: boolean) => Statement.Compiler;
|
|
121
|
+
/**
|
|
122
|
+
* @category custom types
|
|
123
|
+
* @since 1.0.0
|
|
124
|
+
*/
|
|
125
|
+
export type PgCustom = PgJson;
|
|
126
|
+
/**
|
|
127
|
+
* @category custom types
|
|
128
|
+
* @since 1.0.0
|
|
129
|
+
*/
|
|
130
|
+
interface PgJson extends Custom<"PgJson", unknown> {
|
|
131
|
+
}
|
|
132
|
+
declare const PgJson: (paramA: unknown, paramB: void, paramC: void) => PgJson;
|
|
133
|
+
export {};
|
|
134
|
+
//# sourceMappingURL=PgliteClient.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"PgliteClient.d.ts","sourceRoot":"","sources":["../src/PgliteClient.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,OAAO,EAAU,KAAK,eAAe,EAAE,KAAK,aAAa,EAAE,MAAM,sBAAsB,CAAA;AACvF,OAAO,KAAK,MAAM,MAAM,eAAe,CAAA;AACvC,OAAO,KAAK,OAAO,MAAM,gBAAgB,CAAA;AACzC,OAAO,KAAK,MAAM,MAAM,eAAe,CAAA;AAEvC,OAAO,KAAK,KAAK,MAAM,cAAc,CAAA;AAErC,OAAO,KAAK,KAAK,MAAM,cAAc,CAAA;AAErC,OAAO,KAAK,MAAM,MAAM,eAAe,CAAA;AACvC,OAAO,KAAK,UAAU,MAAM,uCAAuC,CAAA;AACnE,OAAO,KAAK,MAAM,MAAM,+BAA+B,CAAA;AAEvD,OAAO,EAQL,QAAQ,EAIT,MAAM,8BAA8B,CAAA;AACrC,OAAO,KAAK,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,+BAA+B,CAAA;AACrE,OAAO,KAAK,SAAS,MAAM,+BAA+B,CAAA;AAE1D;;;GAGG;AACH,eAAO,MAAM,MAAM,EAAE,MAA2C,CAAA;AAEhE;;;GAGG;AACH,MAAM,MAAM,MAAM,GAAG,kCAAkC,CAAA;AAEvD;;;GAGG;AACH,MAAM,WAAW,YAAa,SAAQ,MAAM,CAAC,SAAS;IACpD,QA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GAAG,UAAU,CAAC,UAAU,CAAC,CAMlD,CAAA;AAEhD;;;GAGG;AACH,eAAO,MAAM,WAAW,EAAE,CACxB,MAAM,EAAE,MAAM,CAAC,IAAI,CAAC,kBAAkB,CAAC,UAAU,CAAC,KAC/C,KAAK,CAAC,KAAK,CAAC,YAAY,GAAG,MAAM,CAAC,SAAS,EAAE,MAAM,CAAC,WAAW,GAAG,QAAQ,CAM3E,CAAA;AAEJ;;;GAGG;AACH,eAAO,MAAM,KAAK,GAChB,SAAS,kBAAkB,GAAG,SAAS,KACtC,KAAK,CAAC,KAAK,CAAC,YAAY,GAAG,MAAM,CAAC,SAAS,EAAE,QAAQ,CAA4B,CAAA;AAEpF;;;GAGG;AACH,eAAO,MAAM,YAAY,GACvB,YAAY,CAAC,CAAC,EAAE,MAAM,KAAK,MAAM,EACjC,uBAAoB,KACnB,SAAS,CAAC,QAsCZ,CAAA;AAKD;;;GAGG;AACH,MAAM,MAAM,QAAQ,GAAG,MAAM,CAAA;AAE7B;;;GAGG;AACH,UAAU,MAAO,SAAQ,MAAM,CAAC,QAAQ,EAAE,OAAO,CAAC;CAAG;AACrD,QAAA,MAAM,MAAM,yDAAqC,CAAA"}
|
|
@@ -0,0 +1,243 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @since 1.0.0
|
|
3
|
+
*/
|
|
4
|
+
import { PGlite } from "@electric-sql/pglite";
|
|
5
|
+
import * as Config from "effect/Config";
|
|
6
|
+
import * as Context from "effect/Context";
|
|
7
|
+
import * as Effect from "effect/Effect";
|
|
8
|
+
import * as Fiber from "effect/Fiber";
|
|
9
|
+
import * as Layer from "effect/Layer";
|
|
10
|
+
import * as Queue from "effect/Queue";
|
|
11
|
+
import * as Scope from "effect/Scope";
|
|
12
|
+
import * as Semaphore from "effect/Semaphore";
|
|
13
|
+
import * as Stream from "effect/Stream";
|
|
14
|
+
import * as Reactivity from "effect/unstable/reactivity/Reactivity";
|
|
15
|
+
import * as Client from "effect/unstable/sql/SqlClient";
|
|
16
|
+
import { AuthenticationError, AuthorizationError, ConnectionError, ConstraintError, DeadlockError, LockTimeoutError, SerializationError, SqlError, SqlSyntaxError, StatementTimeoutError, UnknownError } from "effect/unstable/sql/SqlError";
|
|
17
|
+
import * as Statement from "effect/unstable/sql/Statement";
|
|
18
|
+
/**
|
|
19
|
+
* @category type ids
|
|
20
|
+
* @since 1.0.0
|
|
21
|
+
*/
|
|
22
|
+
export const TypeId = "~@effect/sql-pglite/PgliteClient";
|
|
23
|
+
/**
|
|
24
|
+
* @category tags
|
|
25
|
+
* @since 1.0.0
|
|
26
|
+
*/
|
|
27
|
+
export const PgliteClient = /*#__PURE__*/Context.Service("@effect/sql-pglite/PgliteClient");
|
|
28
|
+
/**
|
|
29
|
+
* @category constructor
|
|
30
|
+
* @since 1.0.0
|
|
31
|
+
*/
|
|
32
|
+
export const make = (options = {}) => Effect.gen(function* () {
|
|
33
|
+
const pglite = "liveClient" in options ? options.liveClient : yield* Effect.acquireRelease(Effect.tryPromise({
|
|
34
|
+
try: async () => {
|
|
35
|
+
const pg = new PGlite(options);
|
|
36
|
+
await pg.waitReady;
|
|
37
|
+
return pg;
|
|
38
|
+
},
|
|
39
|
+
catch: cause => new SqlError({
|
|
40
|
+
reason: classifyError(cause, "PgliteClient: Failed to connect", "connect")
|
|
41
|
+
})
|
|
42
|
+
}), pg => Effect.promise(() => pg.close()).pipe(Effect.timeoutOption(1000)), {
|
|
43
|
+
interruptible: true
|
|
44
|
+
});
|
|
45
|
+
return yield* fromClient({
|
|
46
|
+
...options,
|
|
47
|
+
liveClient: pglite
|
|
48
|
+
});
|
|
49
|
+
});
|
|
50
|
+
/**
|
|
51
|
+
* @category constructor
|
|
52
|
+
* @since 1.0.0
|
|
53
|
+
*/
|
|
54
|
+
export const fromClient = options => Effect.gen(function* () {
|
|
55
|
+
const pglite = options.liveClient;
|
|
56
|
+
const compiler = makeCompiler(options.transformQueryNames, options.transformJson);
|
|
57
|
+
const transformRows = options.transformResultNames ? Statement.defaultTransforms(options.transformResultNames, options.transformJson).array : undefined;
|
|
58
|
+
const spanAttributes = [...(options.spanAttributes ? Object.entries(options.spanAttributes) : []), [ATTR_DB_SYSTEM_NAME, "postgresql"]];
|
|
59
|
+
const connection = new PgliteConnection(pglite);
|
|
60
|
+
const semaphore = Semaphore.makeUnsafe(1);
|
|
61
|
+
const acquirer = semaphore.withPermits(1)(Effect.succeed(connection));
|
|
62
|
+
const transactionAcquirer = Effect.uninterruptibleMask(restore => {
|
|
63
|
+
const fiber = Fiber.getCurrent();
|
|
64
|
+
const scope = Context.getUnsafe(fiber.context, Scope.Scope);
|
|
65
|
+
return Effect.as(Effect.tap(restore(semaphore.take(1)), () => Scope.addFinalizer(scope, semaphore.release(1))), connection);
|
|
66
|
+
});
|
|
67
|
+
const config = options;
|
|
68
|
+
const client = yield* Client.make({
|
|
69
|
+
acquirer,
|
|
70
|
+
compiler,
|
|
71
|
+
transactionAcquirer,
|
|
72
|
+
spanAttributes,
|
|
73
|
+
transformRows
|
|
74
|
+
});
|
|
75
|
+
return Object.assign(client, {
|
|
76
|
+
[TypeId]: TypeId,
|
|
77
|
+
config,
|
|
78
|
+
pglite,
|
|
79
|
+
json: _ => Statement.fragment([PgJson(_)]),
|
|
80
|
+
listen: channel => Stream.callback(queue => Effect.acquireRelease(Effect.tryPromise({
|
|
81
|
+
try: () => pglite.listen(channel, payload => {
|
|
82
|
+
Queue.offerUnsafe(queue, payload);
|
|
83
|
+
}),
|
|
84
|
+
catch: cause => new SqlError({
|
|
85
|
+
reason: classifyError(cause, "Failed to listen", "listen")
|
|
86
|
+
})
|
|
87
|
+
}), unlisten => Effect.promise(() => unlisten()), {
|
|
88
|
+
interruptible: true
|
|
89
|
+
})),
|
|
90
|
+
notify: (channel, payload) => Effect.tryPromise({
|
|
91
|
+
try: () => pglite.exec(`NOTIFY ${escape(channel)}, ${escapeLiteral(payload)}`),
|
|
92
|
+
catch: cause => new SqlError({
|
|
93
|
+
reason: classifyError(cause, "Failed to notify", "notify")
|
|
94
|
+
})
|
|
95
|
+
}).pipe(semaphore.withPermit, Effect.asVoid),
|
|
96
|
+
dumpDataDir: compression => semaphore.withPermit(Effect.tryPromise({
|
|
97
|
+
try: () => pglite.dumpDataDir(compression),
|
|
98
|
+
catch: cause => new SqlError({
|
|
99
|
+
reason: classifyError(cause, "Failed to dump data dir", "dumpDataDir")
|
|
100
|
+
})
|
|
101
|
+
})),
|
|
102
|
+
refreshArrayTypes: semaphore.withPermit(Effect.tryPromise({
|
|
103
|
+
try: () => pglite.refreshArrayTypes(),
|
|
104
|
+
catch: cause => new SqlError({
|
|
105
|
+
reason: classifyError(cause, "Failed to refresh array types", "refreshArrayTypes")
|
|
106
|
+
})
|
|
107
|
+
}))
|
|
108
|
+
});
|
|
109
|
+
});
|
|
110
|
+
class PgliteConnection {
|
|
111
|
+
pglite;
|
|
112
|
+
constructor(pglite) {
|
|
113
|
+
this.pglite = pglite;
|
|
114
|
+
}
|
|
115
|
+
run(method, sql, params) {
|
|
116
|
+
return Effect.map(Effect.tryPromise({
|
|
117
|
+
try: () => this.pglite.query(sql, params),
|
|
118
|
+
catch: cause => new SqlError({
|
|
119
|
+
reason: classifyError(cause, "Failed to execute statement", method)
|
|
120
|
+
})
|
|
121
|
+
}), result => result.rows);
|
|
122
|
+
}
|
|
123
|
+
execute(sql, params, transformRows) {
|
|
124
|
+
return transformRows ? Effect.map(this.run("execute", sql, params), transformRows) : this.run("execute", sql, params);
|
|
125
|
+
}
|
|
126
|
+
executeRaw(sql, params) {
|
|
127
|
+
return Effect.tryPromise({
|
|
128
|
+
try: () => this.pglite.query(sql, params),
|
|
129
|
+
catch: cause => new SqlError({
|
|
130
|
+
reason: classifyError(cause, "Failed to execute statement", "executeRaw")
|
|
131
|
+
})
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
executeValues(sql, params) {
|
|
135
|
+
return Effect.map(Effect.tryPromise({
|
|
136
|
+
try: () => this.pglite.query(sql, params, {
|
|
137
|
+
rowMode: "array"
|
|
138
|
+
}),
|
|
139
|
+
catch: cause => new SqlError({
|
|
140
|
+
reason: classifyError(cause, "Failed to execute statement", "executeValues")
|
|
141
|
+
})
|
|
142
|
+
}), result => result.rows);
|
|
143
|
+
}
|
|
144
|
+
executeUnprepared(sql, params, transformRows) {
|
|
145
|
+
return transformRows ? Effect.map(this.run("executeUnprepared", sql, params), transformRows) : this.run("executeUnprepared", sql, params);
|
|
146
|
+
}
|
|
147
|
+
executeStream(sql, params, transformRows) {
|
|
148
|
+
const eff = transformRows ? Effect.map(this.run("executeStream", sql, params), transformRows) : this.run("executeStream", sql, params);
|
|
149
|
+
return Stream.fromArrayEffect(eff);
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
/**
|
|
153
|
+
* @category layers
|
|
154
|
+
* @since 1.0.0
|
|
155
|
+
*/
|
|
156
|
+
export const layerFrom = acquire => Layer.effectContext(Effect.map(acquire, client => Context.make(PgliteClient, client).pipe(Context.add(Client.SqlClient, client)))).pipe(Layer.provide(Reactivity.layer));
|
|
157
|
+
/**
|
|
158
|
+
* @category layers
|
|
159
|
+
* @since 1.0.0
|
|
160
|
+
*/
|
|
161
|
+
export const layerConfig = config => layerFrom(Effect.flatMap(Config.unwrap(config).asEffect(), resolved => make(resolved)));
|
|
162
|
+
/**
|
|
163
|
+
* @category layers
|
|
164
|
+
* @since 1.0.0
|
|
165
|
+
*/
|
|
166
|
+
export const layer = config => layerFrom(make(config));
|
|
167
|
+
/**
|
|
168
|
+
* @category constructor
|
|
169
|
+
* @since 1.0.0
|
|
170
|
+
*/
|
|
171
|
+
export const makeCompiler = (transform, transformJson = true) => {
|
|
172
|
+
const transformValue = transformJson && transform ? Statement.defaultTransforms(transform).value : undefined;
|
|
173
|
+
return Statement.makeCompiler({
|
|
174
|
+
dialect: "pg",
|
|
175
|
+
placeholder(_) {
|
|
176
|
+
return `$${_}`;
|
|
177
|
+
},
|
|
178
|
+
onIdentifier: transform ? function (value, withoutTransform) {
|
|
179
|
+
return withoutTransform ? escape(value) : escape(transform(value));
|
|
180
|
+
} : escape,
|
|
181
|
+
onRecordUpdate(placeholders, valueAlias, valueColumns, values, returning) {
|
|
182
|
+
return [`(values ${placeholders}) AS ${valueAlias}${valueColumns}${returning ? ` RETURNING ${returning[0]}` : ""}`, returning ? values.flat().concat(returning[1]) : values.flat()];
|
|
183
|
+
},
|
|
184
|
+
onCustom(type, placeholder, withoutTransform) {
|
|
185
|
+
switch (type.kind) {
|
|
186
|
+
case "PgJson":
|
|
187
|
+
{
|
|
188
|
+
return [placeholder(undefined), [withoutTransform || transformValue === undefined ? type.paramA : transformValue(type.paramA)]];
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
});
|
|
193
|
+
};
|
|
194
|
+
const escape = /*#__PURE__*/Statement.defaultEscape("\"");
|
|
195
|
+
const escapeLiteral = value => `'${value.replace(/'/g, "''")}'`;
|
|
196
|
+
const PgJson = /*#__PURE__*/Statement.custom("PgJson");
|
|
197
|
+
const ATTR_DB_SYSTEM_NAME = "db.system.name";
|
|
198
|
+
const pgCodeFromCause = cause => {
|
|
199
|
+
if (typeof cause !== "object" || cause === null || !("code" in cause)) {
|
|
200
|
+
return undefined;
|
|
201
|
+
}
|
|
202
|
+
const code = cause.code;
|
|
203
|
+
return typeof code === "string" ? code : undefined;
|
|
204
|
+
};
|
|
205
|
+
const classifyError = (cause, message, operation) => {
|
|
206
|
+
const props = {
|
|
207
|
+
cause,
|
|
208
|
+
message,
|
|
209
|
+
operation
|
|
210
|
+
};
|
|
211
|
+
const code = pgCodeFromCause(cause);
|
|
212
|
+
if (code !== undefined) {
|
|
213
|
+
if (code.startsWith("08")) {
|
|
214
|
+
return new ConnectionError(props);
|
|
215
|
+
}
|
|
216
|
+
if (code.startsWith("28")) {
|
|
217
|
+
return new AuthenticationError(props);
|
|
218
|
+
}
|
|
219
|
+
if (code === "42501") {
|
|
220
|
+
return new AuthorizationError(props);
|
|
221
|
+
}
|
|
222
|
+
if (code.startsWith("42")) {
|
|
223
|
+
return new SqlSyntaxError(props);
|
|
224
|
+
}
|
|
225
|
+
if (code.startsWith("23")) {
|
|
226
|
+
return new ConstraintError(props);
|
|
227
|
+
}
|
|
228
|
+
if (code === "40P01") {
|
|
229
|
+
return new DeadlockError(props);
|
|
230
|
+
}
|
|
231
|
+
if (code === "40001") {
|
|
232
|
+
return new SerializationError(props);
|
|
233
|
+
}
|
|
234
|
+
if (code === "55P03") {
|
|
235
|
+
return new LockTimeoutError(props);
|
|
236
|
+
}
|
|
237
|
+
if (code === "57014") {
|
|
238
|
+
return new StatementTimeoutError(props);
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
return new UnknownError(props);
|
|
242
|
+
};
|
|
243
|
+
//# sourceMappingURL=PgliteClient.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"PgliteClient.js","names":["PGlite","Config","Context","Effect","Fiber","Layer","Queue","Scope","Semaphore","Stream","Reactivity","Client","AuthenticationError","AuthorizationError","ConnectionError","ConstraintError","DeadlockError","LockTimeoutError","SerializationError","SqlError","SqlSyntaxError","StatementTimeoutError","UnknownError","Statement","TypeId","PgliteClient","Service","make","options","gen","pglite","liveClient","acquireRelease","tryPromise","try","pg","waitReady","catch","cause","reason","classifyError","promise","close","pipe","timeoutOption","interruptible","fromClient","compiler","makeCompiler","transformQueryNames","transformJson","transformRows","transformResultNames","defaultTransforms","array","undefined","spanAttributes","Object","entries","ATTR_DB_SYSTEM_NAME","connection","PgliteConnection","semaphore","makeUnsafe","acquirer","withPermits","succeed","transactionAcquirer","uninterruptibleMask","restore","fiber","getCurrent","scope","getUnsafe","context","as","tap","take","addFinalizer","release","config","client","assign","json","_","fragment","PgJson","listen","channel","callback","queue","payload","offerUnsafe","unlisten","notify","exec","escape","escapeLiteral","withPermit","asVoid","dumpDataDir","compression","refreshArrayTypes","constructor","run","method","sql","params","map","query","result","rows","execute","executeRaw","executeValues","rowMode","executeUnprepared","executeStream","eff","fromArrayEffect","layerFrom","acquire","effectContext","add","SqlClient","provide","layer","layerConfig","flatMap","unwrap","asEffect","resolved","transform","transformValue","value","dialect","placeholder","onIdentifier","withoutTransform","onRecordUpdate","placeholders","valueAlias","valueColumns","values","returning","flat","concat","onCustom","type","kind","paramA","defaultEscape","replace","custom","pgCodeFromCause","code","message","operation","props","startsWith"],"sources":["../src/PgliteClient.ts"],"sourcesContent":[null],"mappings":"AAAA;;;AAGA,SAASA,MAAM,QAAkD,sBAAsB;AACvF,OAAO,KAAKC,MAAM,MAAM,eAAe;AACvC,OAAO,KAAKC,OAAO,MAAM,gBAAgB;AACzC,OAAO,KAAKC,MAAM,MAAM,eAAe;AACvC,OAAO,KAAKC,KAAK,MAAM,cAAc;AACrC,OAAO,KAAKC,KAAK,MAAM,cAAc;AACrC,OAAO,KAAKC,KAAK,MAAM,cAAc;AACrC,OAAO,KAAKC,KAAK,MAAM,cAAc;AACrC,OAAO,KAAKC,SAAS,MAAM,kBAAkB;AAC7C,OAAO,KAAKC,MAAM,MAAM,eAAe;AACvC,OAAO,KAAKC,UAAU,MAAM,uCAAuC;AACnE,OAAO,KAAKC,MAAM,MAAM,+BAA+B;AAEvD,SACEC,mBAAmB,EACnBC,kBAAkB,EAClBC,eAAe,EACfC,eAAe,EACfC,aAAa,EACbC,gBAAgB,EAChBC,kBAAkB,EAClBC,QAAQ,EACRC,cAAc,EACdC,qBAAqB,EACrBC,YAAY,QACP,8BAA8B;AAErC,OAAO,KAAKC,SAAS,MAAM,+BAA+B;AAE1D;;;;AAIA,OAAO,MAAMC,MAAM,GAAW,kCAAkC;AAuBhE;;;;AAIA,OAAO,MAAMC,YAAY,gBAAGvB,OAAO,CAACwB,OAAO,CAAe,iCAAiC,CAAC;AAkD5F;;;;AAIA,OAAO,MAAMC,IAAI,GAAGA,CAClBC,OAAA,GAA8B,EAAE,KAEhCzB,MAAM,CAAC0B,GAAG,CAAC,aAAS;EAClB,MAAMC,MAAM,GAAG,YAAY,IAAIF,OAA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ACA,OAAO,IAAIxI,YAAY,CAACwI,KAAK,CAAC;AAChC,CAAC","ignoreList":[]}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @since 1.0.0
|
|
3
|
+
*/
|
|
4
|
+
import type * as Effect from "effect/Effect";
|
|
5
|
+
import * as Layer from "effect/Layer";
|
|
6
|
+
import * as Migrator from "effect/unstable/sql/Migrator";
|
|
7
|
+
import type * as Client from "effect/unstable/sql/SqlClient";
|
|
8
|
+
import type { SqlError } from "effect/unstable/sql/SqlError";
|
|
9
|
+
/**
|
|
10
|
+
* @since 1.0.0
|
|
11
|
+
*/
|
|
12
|
+
export * from "effect/unstable/sql/Migrator";
|
|
13
|
+
/**
|
|
14
|
+
* @category constructor
|
|
15
|
+
* @since 1.0.0
|
|
16
|
+
*/
|
|
17
|
+
export declare const run: <R2 = never>(options: Migrator.MigratorOptions<R2>) => Effect.Effect<ReadonlyArray<readonly [id: number, name: string]>, Migrator.MigrationError | SqlError, Client.SqlClient | R2>;
|
|
18
|
+
/**
|
|
19
|
+
* @category constructor
|
|
20
|
+
* @since 1.0.0
|
|
21
|
+
*/
|
|
22
|
+
export declare const layer: <R>(options: Migrator.MigratorOptions<R>) => Layer.Layer<never, Migrator.MigrationError | SqlError, Client.SqlClient | R>;
|
|
23
|
+
//# sourceMappingURL=PgliteMigrator.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"PgliteMigrator.d.ts","sourceRoot":"","sources":["../src/PgliteMigrator.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,OAAO,KAAK,KAAK,MAAM,MAAM,eAAe,CAAA;AAC5C,OAAO,KAAK,KAAK,MAAM,cAAc,CAAA;AACrC,OAAO,KAAK,QAAQ,MAAM,8BAA8B,CAAA;AACxD,OAAO,KAAK,KAAK,MAAM,MAAM,+BAA+B,CAAA;AAC5D,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,8BAA8B,CAAA;AAE5D;;GAEG;AACH,cAAc,8BAA8B,CAAA;AAE5C;;;GAGG;AACH,eAAO,MAAM,GAAG,EAAE,CAAC,EAAE,GAAG,KAAK,EAC3B,OAAO,EAAE,QAAQ,CAAC,eAAe,CAAC,EAAE,CAAC,KAClC,MAAM,CAAC,MAAM,CAChB,aAAa,CAAC,SAAS,CAAC,EAAE,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,CAAC,CAAC,EAClD,QAAQ,CAAC,cAAc,GAAG,QAAQ,EAClC,MAAM,CAAC,SAAS,GAAG,EAAE,CACF,CAAA;AAErB;;;GAGG;AACH,eAAO,MAAM,KAAK,GAAI,CAAC,EACrB,SAAS,QAAQ,CAAC,eAAe,CAAC,CAAC,CAAC,KACnC,KAAK,CAAC,KAAK,CAAC,KAAK,EAAE,QAAQ,CAAC,cAAc,GAAG,QAAQ,EAAE,MAAM,CAAC,SAAS,GAAG,CAAC,CAAsC,CAAA"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import * as Layer from "effect/Layer";
|
|
2
|
+
import * as Migrator from "effect/unstable/sql/Migrator";
|
|
3
|
+
/**
|
|
4
|
+
* @since 1.0.0
|
|
5
|
+
*/
|
|
6
|
+
export * from "effect/unstable/sql/Migrator";
|
|
7
|
+
/**
|
|
8
|
+
* @category constructor
|
|
9
|
+
* @since 1.0.0
|
|
10
|
+
*/
|
|
11
|
+
export const run = /*#__PURE__*/Migrator.make({});
|
|
12
|
+
/**
|
|
13
|
+
* @category constructor
|
|
14
|
+
* @since 1.0.0
|
|
15
|
+
*/
|
|
16
|
+
export const layer = options => Layer.effectDiscard(run(options));
|
|
17
|
+
//# sourceMappingURL=PgliteMigrator.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"PgliteMigrator.js","names":["Layer","Migrator","run","make","layer","options","effectDiscard"],"sources":["../src/PgliteMigrator.ts"],"sourcesContent":[null],"mappings":"AAIA,OAAO,KAAKA,KAAK,MAAM,cAAc;AACrC,OAAO,KAAKC,QAAQ,MAAM,8BAA8B;AAIxD;;;AAGA,cAAc,8BAA8B;AAE5C;;;;AAIA,OAAO,MAAMC,GAAG,gBAMZD,QAAQ,CAACE,IAAI,CAAC,EAAE,CAAC;AAErB;;;;AAIA,OAAO,MAAMC,KAAK,GAChBC,OAAoC,IAC6CL,KAAK,CAACM,aAAa,CAACJ,GAAG,CAACG,OAAO,CAAC,CAAC","ignoreList":[]}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AAIH;;GAEG;AACH,OAAO,KAAK,YAAY,MAAM,mBAAmB,CAAA;AAEjD;;GAEG;AACH,OAAO,KAAK,cAAc,MAAM,qBAAqB,CAAA"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @since 1.0.0
|
|
3
|
+
*/
|
|
4
|
+
// @barrel: Auto-generated exports. Do not edit manually.
|
|
5
|
+
/**
|
|
6
|
+
* @since 1.0.0
|
|
7
|
+
*/
|
|
8
|
+
export * as PgliteClient from "./PgliteClient.js";
|
|
9
|
+
/**
|
|
10
|
+
* @since 1.0.0
|
|
11
|
+
*/
|
|
12
|
+
export * as PgliteMigrator from "./PgliteMigrator.js";
|
|
13
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","names":["PgliteClient","PgliteMigrator"],"sources":["../src/index.ts"],"sourcesContent":[null],"mappings":"AAAA;;;AAIA;AAEA;;;AAGA,OAAO,KAAKA,YAAY,MAAM,mBAAmB;AAEjD;;;AAGA,OAAO,KAAKC,cAAc,MAAM,qBAAqB","ignoreList":[]}
|
package/package.json
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@effect/sql-pglite",
|
|
3
|
+
"version": "4.0.0-beta.57",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"description": "A PGlite toolkit for Effect",
|
|
7
|
+
"homepage": "https://effect.website",
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "https://github.com/Effect-TS/effect-smol.git",
|
|
11
|
+
"directory": "packages/sql/pglite"
|
|
12
|
+
},
|
|
13
|
+
"bugs": {
|
|
14
|
+
"url": "https://github.com/Effect-TS/effect-smol/issues"
|
|
15
|
+
},
|
|
16
|
+
"tags": [
|
|
17
|
+
"typescript",
|
|
18
|
+
"sql",
|
|
19
|
+
"pglite",
|
|
20
|
+
"postgres",
|
|
21
|
+
"database"
|
|
22
|
+
],
|
|
23
|
+
"keywords": [
|
|
24
|
+
"typescript",
|
|
25
|
+
"sql",
|
|
26
|
+
"pglite",
|
|
27
|
+
"postgres",
|
|
28
|
+
"database"
|
|
29
|
+
],
|
|
30
|
+
"sideEffects": [],
|
|
31
|
+
"exports": {
|
|
32
|
+
"./package.json": "./package.json",
|
|
33
|
+
".": "./dist/index.js",
|
|
34
|
+
"./*": "./dist/*.js",
|
|
35
|
+
"./internal/*": null,
|
|
36
|
+
"./*/index": null
|
|
37
|
+
},
|
|
38
|
+
"files": [
|
|
39
|
+
"src/**/*.ts",
|
|
40
|
+
"dist/**/*.js",
|
|
41
|
+
"dist/**/*.js.map",
|
|
42
|
+
"dist/**/*.d.ts",
|
|
43
|
+
"dist/**/*.d.ts.map"
|
|
44
|
+
],
|
|
45
|
+
"publishConfig": {
|
|
46
|
+
"access": "public",
|
|
47
|
+
"provenance": true
|
|
48
|
+
},
|
|
49
|
+
"devDependencies": {
|
|
50
|
+
"effect": "^4.0.0-beta.57"
|
|
51
|
+
},
|
|
52
|
+
"peerDependencies": {
|
|
53
|
+
"effect": "^4.0.0-beta.57"
|
|
54
|
+
},
|
|
55
|
+
"dependencies": {
|
|
56
|
+
"@electric-sql/pglite": "^0.4.4"
|
|
57
|
+
},
|
|
58
|
+
"scripts": {
|
|
59
|
+
"codegen": "effect-utils codegen",
|
|
60
|
+
"build": "tsc -b tsconfig.json && pnpm babel",
|
|
61
|
+
"build:tsgo": "tsgo -b tsconfig.json && pnpm babel",
|
|
62
|
+
"babel": "babel dist --plugins annotate-pure-calls --out-dir dist --source-maps",
|
|
63
|
+
"check": "tsc -b tsconfig.json",
|
|
64
|
+
"test": "vitest",
|
|
65
|
+
"coverage": "vitest --coverage"
|
|
66
|
+
}
|
|
67
|
+
}
|
|
@@ -0,0 +1,440 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @since 1.0.0
|
|
3
|
+
*/
|
|
4
|
+
import { PGlite, type PGliteInterface, type PGliteOptions } from "@electric-sql/pglite"
|
|
5
|
+
import * as Config from "effect/Config"
|
|
6
|
+
import * as Context from "effect/Context"
|
|
7
|
+
import * as Effect from "effect/Effect"
|
|
8
|
+
import * as Fiber from "effect/Fiber"
|
|
9
|
+
import * as Layer from "effect/Layer"
|
|
10
|
+
import * as Queue from "effect/Queue"
|
|
11
|
+
import * as Scope from "effect/Scope"
|
|
12
|
+
import * as Semaphore from "effect/Semaphore"
|
|
13
|
+
import * as Stream from "effect/Stream"
|
|
14
|
+
import * as Reactivity from "effect/unstable/reactivity/Reactivity"
|
|
15
|
+
import * as Client from "effect/unstable/sql/SqlClient"
|
|
16
|
+
import type { Connection } from "effect/unstable/sql/SqlConnection"
|
|
17
|
+
import {
|
|
18
|
+
AuthenticationError,
|
|
19
|
+
AuthorizationError,
|
|
20
|
+
ConnectionError,
|
|
21
|
+
ConstraintError,
|
|
22
|
+
DeadlockError,
|
|
23
|
+
LockTimeoutError,
|
|
24
|
+
SerializationError,
|
|
25
|
+
SqlError,
|
|
26
|
+
SqlSyntaxError,
|
|
27
|
+
StatementTimeoutError,
|
|
28
|
+
UnknownError
|
|
29
|
+
} from "effect/unstable/sql/SqlError"
|
|
30
|
+
import type { Custom, Fragment } from "effect/unstable/sql/Statement"
|
|
31
|
+
import * as Statement from "effect/unstable/sql/Statement"
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* @category type ids
|
|
35
|
+
* @since 1.0.0
|
|
36
|
+
*/
|
|
37
|
+
export const TypeId: TypeId = "~@effect/sql-pglite/PgliteClient"
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* @category type ids
|
|
41
|
+
* @since 1.0.0
|
|
42
|
+
*/
|
|
43
|
+
export type TypeId = "~@effect/sql-pglite/PgliteClient"
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* @category models
|
|
47
|
+
* @since 1.0.0
|
|
48
|
+
*/
|
|
49
|
+
export interface PgliteClient extends Client.SqlClient {
|
|
50
|
+
readonly [TypeId]: TypeId
|
|
51
|
+
readonly config: PgliteClientConfig
|
|
52
|
+
readonly pglite: PGliteInterface
|
|
53
|
+
readonly json: (_: unknown) => Fragment
|
|
54
|
+
readonly listen: (channel: string) => Stream.Stream<string, SqlError>
|
|
55
|
+
readonly notify: (channel: string, payload: string) => Effect.Effect<void, SqlError>
|
|
56
|
+
readonly dumpDataDir: (compression?: "none" | "gzip" | "auto") => Effect.Effect<File | Blob, SqlError>
|
|
57
|
+
readonly refreshArrayTypes: Effect.Effect<void, SqlError>
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* @category tags
|
|
62
|
+
* @since 1.0.0
|
|
63
|
+
*/
|
|
64
|
+
export const PgliteClient = Context.Service<PgliteClient>("@effect/sql-pglite/PgliteClient")
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* @category models
|
|
68
|
+
* @since 1.0.0
|
|
69
|
+
*/
|
|
70
|
+
export type PgliteClientConfig = PgliteClientConfig.Create | PgliteClientConfig.Live
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* @category models
|
|
74
|
+
* @since 1.0.0
|
|
75
|
+
*/
|
|
76
|
+
export declare namespace PgliteClientConfig {
|
|
77
|
+
/**
|
|
78
|
+
* @category models
|
|
79
|
+
* @since 1.0.0
|
|
80
|
+
*/
|
|
81
|
+
export interface Base {
|
|
82
|
+
readonly spanAttributes?: Record<string, unknown> | undefined
|
|
83
|
+
readonly transformResultNames?: ((str: string) => string) | undefined
|
|
84
|
+
readonly transformQueryNames?: ((str: string) => string) | undefined
|
|
85
|
+
readonly transformJson?: boolean | undefined
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* @category models
|
|
90
|
+
* @since 1.0.0
|
|
91
|
+
*/
|
|
92
|
+
export interface Create extends Base, PGliteOptions {}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* @category models
|
|
96
|
+
* @since 1.0.0
|
|
97
|
+
*/
|
|
98
|
+
export interface Live extends Base {
|
|
99
|
+
readonly liveClient: PGliteInterface
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* @category models
|
|
104
|
+
* @since 1.0.0
|
|
105
|
+
*/
|
|
106
|
+
export interface ConfigBase extends Base {
|
|
107
|
+
readonly dataDir?: string | undefined
|
|
108
|
+
readonly username?: string | undefined
|
|
109
|
+
readonly database?: string | undefined
|
|
110
|
+
readonly relaxedDurability?: boolean | undefined
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* @category constructor
|
|
116
|
+
* @since 1.0.0
|
|
117
|
+
*/
|
|
118
|
+
export const make = (
|
|
119
|
+
options: PgliteClientConfig = {}
|
|
120
|
+
): Effect.Effect<PgliteClient, SqlError, Scope.Scope | Reactivity.Reactivity> =>
|
|
121
|
+
Effect.gen(function*() {
|
|
122
|
+
const pglite = "liveClient" in options
|
|
123
|
+
? options.liveClient
|
|
124
|
+
: yield* Effect.acquireRelease(
|
|
125
|
+
Effect.tryPromise({
|
|
126
|
+
try: async () => {
|
|
127
|
+
const pg = new PGlite(options)
|
|
128
|
+
await pg.waitReady
|
|
129
|
+
return pg
|
|
130
|
+
},
|
|
131
|
+
catch: (cause) => new SqlError({ reason: classifyError(cause, "PgliteClient: Failed to connect", "connect") })
|
|
132
|
+
}),
|
|
133
|
+
(pg) => Effect.promise(() => pg.close()).pipe(Effect.timeoutOption(1000)),
|
|
134
|
+
{ interruptible: true }
|
|
135
|
+
)
|
|
136
|
+
|
|
137
|
+
return yield* fromClient({ ...options, liveClient: pglite })
|
|
138
|
+
})
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* @category constructor
|
|
142
|
+
* @since 1.0.0
|
|
143
|
+
*/
|
|
144
|
+
export const fromClient = (
|
|
145
|
+
options:
|
|
146
|
+
& PgliteClientConfig.Base
|
|
147
|
+
& {
|
|
148
|
+
readonly liveClient: PGliteInterface
|
|
149
|
+
}
|
|
150
|
+
): Effect.Effect<PgliteClient, SqlError, Scope.Scope | Reactivity.Reactivity> =>
|
|
151
|
+
Effect.gen(function*() {
|
|
152
|
+
const pglite = options.liveClient
|
|
153
|
+
const compiler = makeCompiler(options.transformQueryNames, options.transformJson)
|
|
154
|
+
const transformRows = options.transformResultNames
|
|
155
|
+
? Statement.defaultTransforms(options.transformResultNames, options.transformJson).array
|
|
156
|
+
: undefined
|
|
157
|
+
|
|
158
|
+
const spanAttributes: Array<[string, unknown]> = [
|
|
159
|
+
...(options.spanAttributes ? Object.entries(options.spanAttributes) : []),
|
|
160
|
+
[ATTR_DB_SYSTEM_NAME, "postgresql"]
|
|
161
|
+
]
|
|
162
|
+
|
|
163
|
+
const connection = new PgliteConnection(pglite)
|
|
164
|
+
const semaphore = Semaphore.makeUnsafe(1)
|
|
165
|
+
const acquirer = semaphore.withPermits(1)(Effect.succeed(connection))
|
|
166
|
+
const transactionAcquirer = Effect.uninterruptibleMask((restore) => {
|
|
167
|
+
const fiber = Fiber.getCurrent()!
|
|
168
|
+
const scope = Context.getUnsafe(fiber.context, Scope.Scope)
|
|
169
|
+
return Effect.as(
|
|
170
|
+
Effect.tap(
|
|
171
|
+
restore(semaphore.take(1)),
|
|
172
|
+
() => Scope.addFinalizer(scope, semaphore.release(1))
|
|
173
|
+
),
|
|
174
|
+
connection
|
|
175
|
+
)
|
|
176
|
+
})
|
|
177
|
+
|
|
178
|
+
const config = options as PgliteClientConfig
|
|
179
|
+
const client = yield* Client.make({
|
|
180
|
+
acquirer,
|
|
181
|
+
compiler,
|
|
182
|
+
transactionAcquirer,
|
|
183
|
+
spanAttributes,
|
|
184
|
+
transformRows
|
|
185
|
+
})
|
|
186
|
+
|
|
187
|
+
return Object.assign(
|
|
188
|
+
client,
|
|
189
|
+
{
|
|
190
|
+
[TypeId]: TypeId as TypeId,
|
|
191
|
+
config,
|
|
192
|
+
pglite,
|
|
193
|
+
json: (_: unknown) => Statement.fragment([PgJson(_)]),
|
|
194
|
+
listen: (channel: string) =>
|
|
195
|
+
Stream.callback<string, SqlError>((queue) =>
|
|
196
|
+
Effect.acquireRelease(
|
|
197
|
+
Effect.tryPromise({
|
|
198
|
+
try: () =>
|
|
199
|
+
pglite.listen(channel, (payload) => {
|
|
200
|
+
Queue.offerUnsafe(queue, payload)
|
|
201
|
+
}),
|
|
202
|
+
catch: (cause) => new SqlError({ reason: classifyError(cause, "Failed to listen", "listen") })
|
|
203
|
+
}),
|
|
204
|
+
(unlisten) => Effect.promise(() => unlisten()),
|
|
205
|
+
{ interruptible: true }
|
|
206
|
+
)
|
|
207
|
+
),
|
|
208
|
+
notify: (channel: string, payload: string) =>
|
|
209
|
+
Effect.tryPromise({
|
|
210
|
+
try: () => pglite.exec(`NOTIFY ${escape(channel)}, ${escapeLiteral(payload)}`),
|
|
211
|
+
catch: (cause) => new SqlError({ reason: classifyError(cause, "Failed to notify", "notify") })
|
|
212
|
+
}).pipe(
|
|
213
|
+
semaphore.withPermit,
|
|
214
|
+
Effect.asVoid
|
|
215
|
+
),
|
|
216
|
+
dumpDataDir: (compression?: "none" | "gzip" | "auto") =>
|
|
217
|
+
semaphore.withPermit(
|
|
218
|
+
Effect.tryPromise({
|
|
219
|
+
try: () => pglite.dumpDataDir(compression),
|
|
220
|
+
catch: (cause) => new SqlError({ reason: classifyError(cause, "Failed to dump data dir", "dumpDataDir") })
|
|
221
|
+
})
|
|
222
|
+
),
|
|
223
|
+
refreshArrayTypes: semaphore.withPermit(
|
|
224
|
+
Effect.tryPromise({
|
|
225
|
+
try: () => pglite.refreshArrayTypes(),
|
|
226
|
+
catch: (cause) =>
|
|
227
|
+
new SqlError({ reason: classifyError(cause, "Failed to refresh array types", "refreshArrayTypes") })
|
|
228
|
+
})
|
|
229
|
+
)
|
|
230
|
+
}
|
|
231
|
+
)
|
|
232
|
+
})
|
|
233
|
+
|
|
234
|
+
class PgliteConnection implements Connection {
|
|
235
|
+
readonly pglite: PGliteInterface
|
|
236
|
+
constructor(pglite: PGliteInterface) {
|
|
237
|
+
this.pglite = pglite
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
private run(method: string, sql: string, params: ReadonlyArray<unknown>) {
|
|
241
|
+
return Effect.map(
|
|
242
|
+
Effect.tryPromise({
|
|
243
|
+
try: () => this.pglite.query<any>(sql, params as Array<any>),
|
|
244
|
+
catch: (cause) => new SqlError({ reason: classifyError(cause, "Failed to execute statement", method) })
|
|
245
|
+
}),
|
|
246
|
+
(result) => result.rows
|
|
247
|
+
)
|
|
248
|
+
}
|
|
249
|
+
execute(
|
|
250
|
+
sql: string,
|
|
251
|
+
params: ReadonlyArray<unknown>,
|
|
252
|
+
transformRows: (<A extends object>(row: ReadonlyArray<A>) => ReadonlyArray<A>) | undefined
|
|
253
|
+
) {
|
|
254
|
+
return transformRows
|
|
255
|
+
? Effect.map(this.run("execute", sql, params), transformRows)
|
|
256
|
+
: this.run("execute", sql, params)
|
|
257
|
+
}
|
|
258
|
+
executeRaw(sql: string, params: ReadonlyArray<unknown>) {
|
|
259
|
+
return Effect.tryPromise({
|
|
260
|
+
try: () => this.pglite.query<any>(sql, params as Array<any>),
|
|
261
|
+
catch: (cause) => new SqlError({ reason: classifyError(cause, "Failed to execute statement", "executeRaw") })
|
|
262
|
+
})
|
|
263
|
+
}
|
|
264
|
+
executeValues(sql: string, params: ReadonlyArray<unknown>) {
|
|
265
|
+
return Effect.map(
|
|
266
|
+
Effect.tryPromise({
|
|
267
|
+
try: () => this.pglite.query<any>(sql, params as Array<any>, { rowMode: "array" }),
|
|
268
|
+
catch: (cause) => new SqlError({ reason: classifyError(cause, "Failed to execute statement", "executeValues") })
|
|
269
|
+
}),
|
|
270
|
+
(result) => result.rows as ReadonlyArray<ReadonlyArray<any>>
|
|
271
|
+
)
|
|
272
|
+
}
|
|
273
|
+
executeUnprepared(
|
|
274
|
+
sql: string,
|
|
275
|
+
params: ReadonlyArray<unknown>,
|
|
276
|
+
transformRows: (<A extends object>(row: ReadonlyArray<A>) => ReadonlyArray<A>) | undefined
|
|
277
|
+
) {
|
|
278
|
+
return transformRows
|
|
279
|
+
? Effect.map(this.run("executeUnprepared", sql, params), transformRows)
|
|
280
|
+
: this.run("executeUnprepared", sql, params)
|
|
281
|
+
}
|
|
282
|
+
executeStream(
|
|
283
|
+
sql: string,
|
|
284
|
+
params: ReadonlyArray<unknown>,
|
|
285
|
+
transformRows: (<A extends object>(row: ReadonlyArray<A>) => ReadonlyArray<A>) | undefined
|
|
286
|
+
) {
|
|
287
|
+
const eff = transformRows
|
|
288
|
+
? Effect.map(this.run("executeStream", sql, params), transformRows)
|
|
289
|
+
: this.run("executeStream", sql, params)
|
|
290
|
+
return Stream.fromArrayEffect(eff)
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
/**
|
|
295
|
+
* @category layers
|
|
296
|
+
* @since 1.0.0
|
|
297
|
+
*/
|
|
298
|
+
export const layerFrom = <E, R>(
|
|
299
|
+
acquire: Effect.Effect<PgliteClient, E, R>
|
|
300
|
+
): Layer.Layer<PgliteClient | Client.SqlClient, E, Exclude<R, Scope.Scope | Reactivity.Reactivity>> =>
|
|
301
|
+
Layer.effectContext(
|
|
302
|
+
Effect.map(acquire, (client) =>
|
|
303
|
+
Context.make(PgliteClient, client).pipe(
|
|
304
|
+
Context.add(Client.SqlClient, client)
|
|
305
|
+
))
|
|
306
|
+
).pipe(Layer.provide(Reactivity.layer)) as any
|
|
307
|
+
|
|
308
|
+
/**
|
|
309
|
+
* @category layers
|
|
310
|
+
* @since 1.0.0
|
|
311
|
+
*/
|
|
312
|
+
export const layerConfig: (
|
|
313
|
+
config: Config.Wrap<PgliteClientConfig.ConfigBase>
|
|
314
|
+
) => Layer.Layer<PgliteClient | Client.SqlClient, Config.ConfigError | SqlError> = (
|
|
315
|
+
config: Config.Wrap<PgliteClientConfig.ConfigBase>
|
|
316
|
+
): Layer.Layer<PgliteClient | Client.SqlClient, Config.ConfigError | SqlError> =>
|
|
317
|
+
layerFrom(Effect.flatMap(
|
|
318
|
+
Config.unwrap(config).asEffect(),
|
|
319
|
+
(resolved) => make(resolved as PgliteClientConfig)
|
|
320
|
+
))
|
|
321
|
+
|
|
322
|
+
/**
|
|
323
|
+
* @category layers
|
|
324
|
+
* @since 1.0.0
|
|
325
|
+
*/
|
|
326
|
+
export const layer = (
|
|
327
|
+
config?: PgliteClientConfig | undefined
|
|
328
|
+
): Layer.Layer<PgliteClient | Client.SqlClient, SqlError> => layerFrom(make(config))
|
|
329
|
+
|
|
330
|
+
/**
|
|
331
|
+
* @category constructor
|
|
332
|
+
* @since 1.0.0
|
|
333
|
+
*/
|
|
334
|
+
export const makeCompiler = (
|
|
335
|
+
transform?: (_: string) => string,
|
|
336
|
+
transformJson = true
|
|
337
|
+
): Statement.Compiler => {
|
|
338
|
+
const transformValue = transformJson && transform
|
|
339
|
+
? Statement.defaultTransforms(transform).value
|
|
340
|
+
: undefined
|
|
341
|
+
|
|
342
|
+
return Statement.makeCompiler<PgCustom>({
|
|
343
|
+
dialect: "pg",
|
|
344
|
+
placeholder(_) {
|
|
345
|
+
return `$${_}`
|
|
346
|
+
},
|
|
347
|
+
onIdentifier: transform ?
|
|
348
|
+
function(value, withoutTransform) {
|
|
349
|
+
return withoutTransform ? escape(value) : escape(transform(value))
|
|
350
|
+
} :
|
|
351
|
+
escape,
|
|
352
|
+
onRecordUpdate(placeholders, valueAlias, valueColumns, values, returning) {
|
|
353
|
+
return [
|
|
354
|
+
`(values ${placeholders}) AS ${valueAlias}${valueColumns}${returning ? ` RETURNING ${returning[0]}` : ""}`,
|
|
355
|
+
returning ?
|
|
356
|
+
values.flat().concat(returning[1]) :
|
|
357
|
+
values.flat()
|
|
358
|
+
]
|
|
359
|
+
},
|
|
360
|
+
onCustom(type, placeholder, withoutTransform) {
|
|
361
|
+
switch (type.kind) {
|
|
362
|
+
case "PgJson": {
|
|
363
|
+
return [
|
|
364
|
+
placeholder(undefined),
|
|
365
|
+
[
|
|
366
|
+
withoutTransform || transformValue === undefined
|
|
367
|
+
? type.paramA
|
|
368
|
+
: transformValue(type.paramA)
|
|
369
|
+
]
|
|
370
|
+
]
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
})
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
const escape = Statement.defaultEscape("\"")
|
|
378
|
+
const escapeLiteral = (value: string) => `'${value.replace(/'/g, "''")}'`
|
|
379
|
+
|
|
380
|
+
/**
|
|
381
|
+
* @category custom types
|
|
382
|
+
* @since 1.0.0
|
|
383
|
+
*/
|
|
384
|
+
export type PgCustom = PgJson
|
|
385
|
+
|
|
386
|
+
/**
|
|
387
|
+
* @category custom types
|
|
388
|
+
* @since 1.0.0
|
|
389
|
+
*/
|
|
390
|
+
interface PgJson extends Custom<"PgJson", unknown> {}
|
|
391
|
+
const PgJson = Statement.custom<PgJson>("PgJson")
|
|
392
|
+
|
|
393
|
+
const ATTR_DB_SYSTEM_NAME = "db.system.name"
|
|
394
|
+
|
|
395
|
+
const pgCodeFromCause = (cause: unknown): string | undefined => {
|
|
396
|
+
if (typeof cause !== "object" || cause === null || !("code" in cause)) {
|
|
397
|
+
return undefined
|
|
398
|
+
}
|
|
399
|
+
const code = cause.code
|
|
400
|
+
return typeof code === "string" ? code : undefined
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
const classifyError = (
|
|
404
|
+
cause: unknown,
|
|
405
|
+
message: string,
|
|
406
|
+
operation: string
|
|
407
|
+
) => {
|
|
408
|
+
const props = { cause, message, operation }
|
|
409
|
+
const code = pgCodeFromCause(cause)
|
|
410
|
+
if (code !== undefined) {
|
|
411
|
+
if (code.startsWith("08")) {
|
|
412
|
+
return new ConnectionError(props)
|
|
413
|
+
}
|
|
414
|
+
if (code.startsWith("28")) {
|
|
415
|
+
return new AuthenticationError(props)
|
|
416
|
+
}
|
|
417
|
+
if (code === "42501") {
|
|
418
|
+
return new AuthorizationError(props)
|
|
419
|
+
}
|
|
420
|
+
if (code.startsWith("42")) {
|
|
421
|
+
return new SqlSyntaxError(props)
|
|
422
|
+
}
|
|
423
|
+
if (code.startsWith("23")) {
|
|
424
|
+
return new ConstraintError(props)
|
|
425
|
+
}
|
|
426
|
+
if (code === "40P01") {
|
|
427
|
+
return new DeadlockError(props)
|
|
428
|
+
}
|
|
429
|
+
if (code === "40001") {
|
|
430
|
+
return new SerializationError(props)
|
|
431
|
+
}
|
|
432
|
+
if (code === "55P03") {
|
|
433
|
+
return new LockTimeoutError(props)
|
|
434
|
+
}
|
|
435
|
+
if (code === "57014") {
|
|
436
|
+
return new StatementTimeoutError(props)
|
|
437
|
+
}
|
|
438
|
+
}
|
|
439
|
+
return new UnknownError(props)
|
|
440
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @since 1.0.0
|
|
3
|
+
*/
|
|
4
|
+
import type * as Effect from "effect/Effect"
|
|
5
|
+
import * as Layer from "effect/Layer"
|
|
6
|
+
import * as Migrator from "effect/unstable/sql/Migrator"
|
|
7
|
+
import type * as Client from "effect/unstable/sql/SqlClient"
|
|
8
|
+
import type { SqlError } from "effect/unstable/sql/SqlError"
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* @since 1.0.0
|
|
12
|
+
*/
|
|
13
|
+
export * from "effect/unstable/sql/Migrator"
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* @category constructor
|
|
17
|
+
* @since 1.0.0
|
|
18
|
+
*/
|
|
19
|
+
export const run: <R2 = never>(
|
|
20
|
+
options: Migrator.MigratorOptions<R2>
|
|
21
|
+
) => Effect.Effect<
|
|
22
|
+
ReadonlyArray<readonly [id: number, name: string]>,
|
|
23
|
+
Migrator.MigrationError | SqlError,
|
|
24
|
+
Client.SqlClient | R2
|
|
25
|
+
> = Migrator.make({})
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* @category constructor
|
|
29
|
+
* @since 1.0.0
|
|
30
|
+
*/
|
|
31
|
+
export const layer = <R>(
|
|
32
|
+
options: Migrator.MigratorOptions<R>
|
|
33
|
+
): Layer.Layer<never, Migrator.MigrationError | SqlError, Client.SqlClient | R> => Layer.effectDiscard(run(options))
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @since 1.0.0
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
// @barrel: Auto-generated exports. Do not edit manually.
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* @since 1.0.0
|
|
9
|
+
*/
|
|
10
|
+
export * as PgliteClient from "./PgliteClient.ts"
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* @since 1.0.0
|
|
14
|
+
*/
|
|
15
|
+
export * as PgliteMigrator from "./PgliteMigrator.ts"
|