@effect/sql-pg 0.0.0-snapshot-189d4cae80e186661241002ad9d729628096520f
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/Client/package.json +6 -0
- package/LICENSE +21 -0
- package/Migrator/package.json +6 -0
- package/README.md +5 -0
- package/dist/cjs/Client.js +196 -0
- package/dist/cjs/Client.js.map +1 -0
- package/dist/cjs/Migrator.js +125 -0
- package/dist/cjs/Migrator.js.map +1 -0
- package/dist/cjs/index.js +44 -0
- package/dist/cjs/index.js.map +1 -0
- package/dist/dts/Client.d.ts +96 -0
- package/dist/dts/Client.d.ts.map +1 -0
- package/dist/dts/Migrator.d.ts +23 -0
- package/dist/dts/Migrator.d.ts.map +1 -0
- package/dist/dts/index.d.ts +28 -0
- package/dist/dts/index.d.ts.map +1 -0
- package/dist/esm/Client.js +156 -0
- package/dist/esm/Client.js.map +1 -0
- package/dist/esm/Migrator.js +77 -0
- package/dist/esm/Migrator.js.map +1 -0
- package/dist/esm/index.js +28 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/package.json +4 -0
- package/package.json +51 -0
- package/src/Client.ts +290 -0
- package/src/Migrator.ts +113 -0
- package/src/index.ts +33 -0
package/src/Migrator.ts
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @since 1.0.0
|
|
3
|
+
*/
|
|
4
|
+
import * as Command from "@effect/platform/Command"
|
|
5
|
+
import type { CommandExecutor } from "@effect/platform/CommandExecutor"
|
|
6
|
+
import { FileSystem } from "@effect/platform/FileSystem"
|
|
7
|
+
import { Path } from "@effect/platform/Path"
|
|
8
|
+
import type { SqlError } from "@effect/sql/Error"
|
|
9
|
+
import * as Migrator from "@effect/sql/Migrator"
|
|
10
|
+
import * as Effect from "effect/Effect"
|
|
11
|
+
import * as Layer from "effect/Layer"
|
|
12
|
+
import * as Secret from "effect/Secret"
|
|
13
|
+
import * as Client from "./Client.js"
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* @since 1.0.0
|
|
17
|
+
*/
|
|
18
|
+
export * from "@effect/sql/Migrator"
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* @category constructor
|
|
22
|
+
* @since 1.0.0
|
|
23
|
+
*/
|
|
24
|
+
export const run: <R>(
|
|
25
|
+
options: Migrator.MigratorOptions<R>
|
|
26
|
+
) => Effect.Effect<
|
|
27
|
+
ReadonlyArray<readonly [id: number, name: string]>,
|
|
28
|
+
SqlError | Migrator.MigrationError,
|
|
29
|
+
Client.PgClient | FileSystem | Path | CommandExecutor | R
|
|
30
|
+
> = Migrator.make({
|
|
31
|
+
getClient: Client.PgClient,
|
|
32
|
+
ensureTable(sql, table) {
|
|
33
|
+
return Effect.catchAll(
|
|
34
|
+
sql`select ${table}::regclass`,
|
|
35
|
+
() =>
|
|
36
|
+
sql`
|
|
37
|
+
CREATE TABLE ${sql(table)} (
|
|
38
|
+
migration_id integer primary key,
|
|
39
|
+
created_at timestamp with time zone not null default now(),
|
|
40
|
+
name text not null
|
|
41
|
+
)
|
|
42
|
+
`
|
|
43
|
+
)
|
|
44
|
+
},
|
|
45
|
+
lockTable(sql, table) {
|
|
46
|
+
return sql`
|
|
47
|
+
LOCK TABLE ${sql(table)} IN ACCESS EXCLUSIVE MODE
|
|
48
|
+
`
|
|
49
|
+
},
|
|
50
|
+
dumpSchema(sql, path, table) {
|
|
51
|
+
const pgDump = (args: Array<string>) =>
|
|
52
|
+
Effect.gen(function*(_) {
|
|
53
|
+
const dump = yield* _(
|
|
54
|
+
Command.make("pg_dump", ...args, "--no-owner", "--no-privileges"),
|
|
55
|
+
Command.env({
|
|
56
|
+
PATH: (globalThis as any).process?.env.PATH,
|
|
57
|
+
PGHOST: sql.config.host,
|
|
58
|
+
PGPORT: sql.config.port?.toString(),
|
|
59
|
+
PGUSER: sql.config.username,
|
|
60
|
+
PGPASSWORD: sql.config.password
|
|
61
|
+
? Secret.value(sql.config.password)
|
|
62
|
+
: undefined,
|
|
63
|
+
PGDATABASE: sql.config.database,
|
|
64
|
+
PGSSLMODE: sql.config.ssl ? "require" : "prefer"
|
|
65
|
+
}),
|
|
66
|
+
Command.string
|
|
67
|
+
)
|
|
68
|
+
|
|
69
|
+
return dump.replace(/^--.*$/gm, "")
|
|
70
|
+
.replace(/^SET .*$/gm, "")
|
|
71
|
+
.replace(/^SELECT pg_catalog\..*$/gm, "")
|
|
72
|
+
.replace(/\n{2,}/gm, "\n\n")
|
|
73
|
+
.trim()
|
|
74
|
+
}).pipe(
|
|
75
|
+
Effect.mapError((error) => new Migrator.MigrationError({ reason: "failed", message: error.message }))
|
|
76
|
+
)
|
|
77
|
+
|
|
78
|
+
const pgDumpSchema = pgDump(["--schema-only"])
|
|
79
|
+
|
|
80
|
+
const pgDumpMigrations = pgDump([
|
|
81
|
+
"--column-inserts",
|
|
82
|
+
"--data-only",
|
|
83
|
+
`--table=${table}`
|
|
84
|
+
])
|
|
85
|
+
|
|
86
|
+
const pgDumpAll = Effect.map(
|
|
87
|
+
Effect.all([pgDumpSchema, pgDumpMigrations], { concurrency: 2 }),
|
|
88
|
+
([schema, migrations]) => schema + "\n\n" + migrations
|
|
89
|
+
)
|
|
90
|
+
|
|
91
|
+
const pgDumpFile = (path: string) =>
|
|
92
|
+
Effect.gen(function*(_) {
|
|
93
|
+
const fs = yield* _(FileSystem)
|
|
94
|
+
const path_ = yield* _(Path)
|
|
95
|
+
const dump = yield* _(pgDumpAll)
|
|
96
|
+
yield* _(fs.makeDirectory(path_.dirname(path), { recursive: true }))
|
|
97
|
+
yield* _(fs.writeFileString(path, dump))
|
|
98
|
+
}).pipe(
|
|
99
|
+
Effect.mapError((error) => new Migrator.MigrationError({ reason: "failed", message: error.message }))
|
|
100
|
+
)
|
|
101
|
+
|
|
102
|
+
return pgDumpFile(path)
|
|
103
|
+
}
|
|
104
|
+
})
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* @category layers
|
|
108
|
+
* @since 1.0.0
|
|
109
|
+
*/
|
|
110
|
+
export const layer = (
|
|
111
|
+
options: Migrator.MigratorOptions
|
|
112
|
+
): Layer.Layer<never, SqlError | Migrator.MigrationError, Client.PgClient | FileSystem | Path | CommandExecutor> =>
|
|
113
|
+
Layer.effectDiscard(run(options))
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @since 1.0.0
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* @since 1.0.0
|
|
7
|
+
*/
|
|
8
|
+
export * as client from "./Client.js"
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* @since 1.0.0
|
|
12
|
+
*/
|
|
13
|
+
export * as error from "@effect/sql/Error"
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* @since 1.0.0
|
|
17
|
+
*/
|
|
18
|
+
export * as migrator from "./Migrator.js"
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* @since 1.0.0
|
|
22
|
+
*/
|
|
23
|
+
export * as resolver from "@effect/sql/Resolver"
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* @since 1.0.0
|
|
27
|
+
*/
|
|
28
|
+
export * as schema from "@effect/sql/Schema"
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* @since 1.0.0
|
|
32
|
+
*/
|
|
33
|
+
export * as statement from "@effect/sql/Statement"
|