@effect/sql-sqlite-bun 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 +144 -0
- package/dist/cjs/Client.js.map +1 -0
- package/dist/cjs/Migrator.js +111 -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 +55 -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 +109 -0
- package/dist/esm/Client.js.map +1 -0
- package/dist/esm/Migrator.js +63 -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 +48 -0
- package/src/Client.ts +185 -0
- package/src/Migrator.ts +89 -0
- package/src/index.ts +33 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023-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,144 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.makeCompiler = exports.make = exports.layer = exports.SqliteClient = void 0;
|
|
7
|
+
var Client = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/sql/Client"));
|
|
8
|
+
var _Error = /*#__PURE__*/require("@effect/sql/Error");
|
|
9
|
+
var Statement = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/sql/Statement"));
|
|
10
|
+
var _bunSqlite = /*#__PURE__*/require("bun:sqlite");
|
|
11
|
+
var Config = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("effect/Config"));
|
|
12
|
+
var Context = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("effect/Context"));
|
|
13
|
+
var Effect = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("effect/Effect"));
|
|
14
|
+
var _Function = /*#__PURE__*/require("effect/Function");
|
|
15
|
+
var Layer = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("effect/Layer"));
|
|
16
|
+
var Scope = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("effect/Scope"));
|
|
17
|
+
function _getRequireWildcardCache(e) {
|
|
18
|
+
if ("function" != typeof WeakMap) return null;
|
|
19
|
+
var r = new WeakMap(),
|
|
20
|
+
t = new WeakMap();
|
|
21
|
+
return (_getRequireWildcardCache = function (e) {
|
|
22
|
+
return e ? t : r;
|
|
23
|
+
})(e);
|
|
24
|
+
}
|
|
25
|
+
function _interopRequireWildcard(e, r) {
|
|
26
|
+
if (!r && e && e.__esModule) return e;
|
|
27
|
+
if (null === e || "object" != typeof e && "function" != typeof e) return {
|
|
28
|
+
default: e
|
|
29
|
+
};
|
|
30
|
+
var t = _getRequireWildcardCache(r);
|
|
31
|
+
if (t && t.has(e)) return t.get(e);
|
|
32
|
+
var n = {
|
|
33
|
+
__proto__: null
|
|
34
|
+
},
|
|
35
|
+
a = Object.defineProperty && Object.getOwnPropertyDescriptor;
|
|
36
|
+
for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) {
|
|
37
|
+
var i = a ? Object.getOwnPropertyDescriptor(e, u) : null;
|
|
38
|
+
i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u];
|
|
39
|
+
}
|
|
40
|
+
return n.default = e, t && t.set(e, n), n;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* @since 1.0.0
|
|
44
|
+
*/
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* @category tags
|
|
48
|
+
* @since 1.0.0
|
|
49
|
+
*/
|
|
50
|
+
const SqliteClient = exports.SqliteClient = /*#__PURE__*/Context.GenericTag("@effect/sql-sqlite-bun/SqliteClient");
|
|
51
|
+
/**
|
|
52
|
+
* @category constructor
|
|
53
|
+
* @since 1.0.0
|
|
54
|
+
*/
|
|
55
|
+
const make = options => Effect.gen(function* (_) {
|
|
56
|
+
const compiler = makeCompiler(options.transformQueryNames);
|
|
57
|
+
const transformRows = Client.defaultTransforms(options.transformResultNames).array;
|
|
58
|
+
const makeConnection = Effect.gen(function* (_) {
|
|
59
|
+
const db = new _bunSqlite.Database(options.filename, {
|
|
60
|
+
readonly: options.readonly,
|
|
61
|
+
readwrite: options.readwrite ?? true,
|
|
62
|
+
create: options.create ?? true
|
|
63
|
+
});
|
|
64
|
+
yield* _(Effect.addFinalizer(() => Effect.sync(() => db.close())));
|
|
65
|
+
if (options.disableWAL !== true) {
|
|
66
|
+
db.run("PRAGMA journal_mode = WAL;");
|
|
67
|
+
}
|
|
68
|
+
const run = (sql, params = []) => Effect.try({
|
|
69
|
+
try: () => db.query(sql).all(...params),
|
|
70
|
+
catch: error => new _Error.SqlError({
|
|
71
|
+
error
|
|
72
|
+
})
|
|
73
|
+
});
|
|
74
|
+
const runTransform = options.transformResultNames ? (sql, params) => Effect.map(run(sql, params), transformRows) : run;
|
|
75
|
+
const runValues = (sql, params = []) => Effect.try({
|
|
76
|
+
try: () => db.query(sql).values(...params),
|
|
77
|
+
catch: error => new _Error.SqlError({
|
|
78
|
+
error
|
|
79
|
+
})
|
|
80
|
+
});
|
|
81
|
+
return (0, _Function.identity)({
|
|
82
|
+
execute(sql, params) {
|
|
83
|
+
return runTransform(sql, params);
|
|
84
|
+
},
|
|
85
|
+
executeValues(sql, params) {
|
|
86
|
+
return runValues(sql, params);
|
|
87
|
+
},
|
|
88
|
+
executeWithoutTransform(sql, params) {
|
|
89
|
+
return run(sql, params);
|
|
90
|
+
},
|
|
91
|
+
executeRaw(sql, params) {
|
|
92
|
+
return runTransform(sql, params);
|
|
93
|
+
},
|
|
94
|
+
executeStream(_sql, _params) {
|
|
95
|
+
return Effect.dieMessage("executeStream not implemented");
|
|
96
|
+
},
|
|
97
|
+
export: Effect.try({
|
|
98
|
+
try: () => db.serialize(),
|
|
99
|
+
catch: error => new _Error.SqlError({
|
|
100
|
+
error
|
|
101
|
+
})
|
|
102
|
+
}),
|
|
103
|
+
loadExtension: path => Effect.try({
|
|
104
|
+
try: () => db.loadExtension(path),
|
|
105
|
+
catch: error => new _Error.SqlError({
|
|
106
|
+
error
|
|
107
|
+
})
|
|
108
|
+
})
|
|
109
|
+
});
|
|
110
|
+
});
|
|
111
|
+
const semaphore = yield* _(Effect.makeSemaphore(1));
|
|
112
|
+
const connection = yield* _(makeConnection);
|
|
113
|
+
const acquirer = semaphore.withPermits(1)(Effect.succeed(connection));
|
|
114
|
+
const transactionAcquirer = Effect.uninterruptibleMask(restore => Effect.as(Effect.zipRight(restore(semaphore.take(1)), Effect.tap(Effect.scope, scope => Scope.addFinalizer(scope, semaphore.release(1)))), connection));
|
|
115
|
+
return Object.assign(Client.make({
|
|
116
|
+
acquirer,
|
|
117
|
+
compiler,
|
|
118
|
+
transactionAcquirer
|
|
119
|
+
}), {
|
|
120
|
+
config: options,
|
|
121
|
+
export: Effect.flatMap(acquirer, _ => _.export),
|
|
122
|
+
loadExtension: path => Effect.flatMap(acquirer, _ => _.loadExtension(path))
|
|
123
|
+
});
|
|
124
|
+
});
|
|
125
|
+
/**
|
|
126
|
+
* @category layers
|
|
127
|
+
* @since 1.0.0
|
|
128
|
+
*/
|
|
129
|
+
exports.make = make;
|
|
130
|
+
const layer = config => Layer.scoped(SqliteClient, Effect.flatMap(Config.unwrap(config), make));
|
|
131
|
+
exports.layer = layer;
|
|
132
|
+
const escape = /*#__PURE__*/Statement.defaultEscape("\"");
|
|
133
|
+
/**
|
|
134
|
+
* @category compiler
|
|
135
|
+
* @since 1.0.0
|
|
136
|
+
*/
|
|
137
|
+
const makeCompiler = transform => Statement.makeCompiler({
|
|
138
|
+
placeholder: _ => `?`,
|
|
139
|
+
onIdentifier: transform ? _ => escape(transform(_)) : escape,
|
|
140
|
+
onRecordUpdate: () => ["", []],
|
|
141
|
+
onCustom: () => ["", []]
|
|
142
|
+
});
|
|
143
|
+
exports.makeCompiler = makeCompiler;
|
|
144
|
+
//# sourceMappingURL=Client.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Client.js","names":["Client","_interopRequireWildcard","require","_Error","Statement","_bunSqlite","Config","Context","Effect","_Function","Layer","Scope","_getRequireWildcardCache","e","WeakMap","r","t","__esModule","default","has","get","n","__proto__","a","Object","defineProperty","getOwnPropertyDescriptor","u","hasOwnProperty","call","i","set","SqliteClient","exports","GenericTag","make","options","gen","_","compiler","makeCompiler","transformQueryNames","transformRows","defaultTransforms","transformResultNames","array","makeConnection","db","Database","filename","readonly","readwrite","create","addFinalizer","sync","close","disableWAL","run","sql","params","try","query","all","catch","error","SqlError","runTransform","map","runValues","values","identity","execute","executeValues","executeWithoutTransform","executeRaw","executeStream","_sql","_params","dieMessage","export","serialize","loadExtension","path","semaphore","makeSemaphore","connection","acquirer","withPermits","succeed","transactionAcquirer","uninterruptibleMask","restore","as","zipRight","take","tap","scope","release","assign","config","flatMap","layer","scoped","unwrap","escape","defaultEscape","transform","placeholder","onIdentifier","onRecordUpdate","onCustom"],"sources":["../../src/Client.ts"],"sourcesContent":[null],"mappings":";;;;;;AAGA,IAAAA,MAAA,gBAAAC,uBAAA,eAAAC,OAAA;AAEA,IAAAC,MAAA,gBAAAD,OAAA;AACA,IAAAE,SAAA,gBAAAH,uBAAA,eAAAC,OAAA;AACA,IAAAG,UAAA,gBAAAH,OAAA;AACA,IAAAI,MAAA,gBAAAL,uBAAA,eAAAC,OAAA;AAEA,IAAAK,OAAA,gBAAAN,uBAAA,eAAAC,OAAA;AACA,IAAAM,MAAA,gBAAAP,uBAAA,eAAAC,OAAA;AACA,IAAAO,SAAA,gBAAAP,OAAA;AACA,IAAAQ,KAAA,gBAAAT,uBAAA,eAAAC,OAAA;AACA,IAAAS,KAAA,gBAAAV,uBAAA,eAAAC,OAAA;AAAqC,SAAAU,yBAAAC,CAAA;EAAA,yBAAAC,OAAA;EAAA,IAAAC,CAAA,OAAAD,OAAA;IAAAE,CAAA,OAAAF,OAAA;EAAA,QAAAF,wBAAA,YAAAA,CAAAC,CAAA;IAAA,OAAAA,CAAA,GAAAG,CAAA,GAAAD,CAAA;EAAA,GAAAF,CAAA;AAAA;AAAA,SAAAZ,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MAAM,CAACJ,MAAM,CAAC,EAAEhE,IAAI,CAAC,CAC5C;AAAAF,OAAA,CAAAoE,KAAA,GAAAA,KAAA;AAEH,MAAMG,MAAM,gBAAGpG,SAAS,CAACqG,aAAa,CAAC,IAAI,CAAC;AAE5C;;;;AAIO,MAAMjE,YAAY,GAAIkE,SAAiC,IAC5DtG,SAAS,CAACoC,YAAY,CAAC;EACrBmE,WAAW,EAAGrE,CAAC,IAAK,GAAG;EACvBsE,YAAY,EAAEF,SAAS,GAAIpE,CAAC,IAAKkE,MAAM,CAACE,SAAS,CAACpE,CAAC,CAAC,CAAC,GAAGkE,MAAM;EAC9DK,cAAc,EAAEA,CAAA,KAAM,CAAC,EAAE,EAAE,EAAE,CAAC;EAC9BC,QAAQ,EAAEA,CAAA,KAAM,CAAC,EAAE,EAAE,EAAE;CACxB,CAAC;AAAA7E,OAAA,CAAAO,YAAA,GAAAA,YAAA","ignoreList":[]}
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
var _exportNames = {
|
|
7
|
+
run: true,
|
|
8
|
+
makeLayer: true
|
|
9
|
+
};
|
|
10
|
+
exports.run = exports.makeLayer = void 0;
|
|
11
|
+
var Command = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/platform/Command"));
|
|
12
|
+
var _FileSystem = /*#__PURE__*/require("@effect/platform/FileSystem");
|
|
13
|
+
var _Path = /*#__PURE__*/require("@effect/platform/Path");
|
|
14
|
+
var Migrator = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/sql/Migrator"));
|
|
15
|
+
Object.keys(Migrator).forEach(function (key) {
|
|
16
|
+
if (key === "default" || key === "__esModule") return;
|
|
17
|
+
if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
|
|
18
|
+
if (key in exports && exports[key] === Migrator[key]) return;
|
|
19
|
+
Object.defineProperty(exports, key, {
|
|
20
|
+
enumerable: true,
|
|
21
|
+
get: function () {
|
|
22
|
+
return Migrator[key];
|
|
23
|
+
}
|
|
24
|
+
});
|
|
25
|
+
});
|
|
26
|
+
var Effect = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("effect/Effect"));
|
|
27
|
+
var Layer = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("effect/Layer"));
|
|
28
|
+
var Client = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("./Client.js"));
|
|
29
|
+
function _getRequireWildcardCache(e) {
|
|
30
|
+
if ("function" != typeof WeakMap) return null;
|
|
31
|
+
var r = new WeakMap(),
|
|
32
|
+
t = new WeakMap();
|
|
33
|
+
return (_getRequireWildcardCache = function (e) {
|
|
34
|
+
return e ? t : r;
|
|
35
|
+
})(e);
|
|
36
|
+
}
|
|
37
|
+
function _interopRequireWildcard(e, r) {
|
|
38
|
+
if (!r && e && e.__esModule) return e;
|
|
39
|
+
if (null === e || "object" != typeof e && "function" != typeof e) return {
|
|
40
|
+
default: e
|
|
41
|
+
};
|
|
42
|
+
var t = _getRequireWildcardCache(r);
|
|
43
|
+
if (t && t.has(e)) return t.get(e);
|
|
44
|
+
var n = {
|
|
45
|
+
__proto__: null
|
|
46
|
+
},
|
|
47
|
+
a = Object.defineProperty && Object.getOwnPropertyDescriptor;
|
|
48
|
+
for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) {
|
|
49
|
+
var i = a ? Object.getOwnPropertyDescriptor(e, u) : null;
|
|
50
|
+
i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u];
|
|
51
|
+
}
|
|
52
|
+
return n.default = e, t && t.set(e, n), n;
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* @since 1.0.0
|
|
56
|
+
*/
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* @since 1.0.0
|
|
60
|
+
*/
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* @category constructor
|
|
64
|
+
* @since 1.0.0
|
|
65
|
+
*/
|
|
66
|
+
const run = exports.run = /*#__PURE__*/Migrator.make({
|
|
67
|
+
getClient: Client.SqliteClient,
|
|
68
|
+
ensureTable(sql, table) {
|
|
69
|
+
return sql`
|
|
70
|
+
CREATE TABLE IF NOT EXISTS ${sql(table)} (
|
|
71
|
+
migration_id integer PRIMARY KEY NOT NULL,
|
|
72
|
+
created_at datetime NOT NULL DEFAULT current_timestamp,
|
|
73
|
+
name VARCHAR(255) NOT NULL
|
|
74
|
+
)
|
|
75
|
+
`;
|
|
76
|
+
},
|
|
77
|
+
dumpSchema(sql, path, table) {
|
|
78
|
+
const dump = args => Effect.gen(function* (_) {
|
|
79
|
+
const dump = yield* _(Command.make("sqlite3", sql.config.filename, ...args), Command.string);
|
|
80
|
+
return dump.replace(/^create table sqlite_sequence\(.*$/im, "").replace(/\n{2,}/gm, "\n\n").trim();
|
|
81
|
+
}).pipe(Effect.mapError(error => new Migrator.MigrationError({
|
|
82
|
+
reason: "failed",
|
|
83
|
+
message: error.message
|
|
84
|
+
})));
|
|
85
|
+
const dumpSchema = dump([".schema"]);
|
|
86
|
+
const dumpMigrations = dump(["--cmd", `.mode insert ${table}`, `select * from ${table}`]);
|
|
87
|
+
const dumpAll = Effect.map(Effect.all([dumpSchema, dumpMigrations], {
|
|
88
|
+
concurrency: 2
|
|
89
|
+
}), ([schema, migrations]) => schema + "\n\n" + migrations);
|
|
90
|
+
const dumpFile = file => Effect.gen(function* (_) {
|
|
91
|
+
const fs = yield* _(_FileSystem.FileSystem);
|
|
92
|
+
const path = yield* _(_Path.Path);
|
|
93
|
+
const dump = yield* _(dumpAll);
|
|
94
|
+
yield* _(fs.makeDirectory(path.dirname(file), {
|
|
95
|
+
recursive: true
|
|
96
|
+
}));
|
|
97
|
+
yield* _(fs.writeFileString(file, dump));
|
|
98
|
+
}).pipe(Effect.mapError(error => new Migrator.MigrationError({
|
|
99
|
+
reason: "failed",
|
|
100
|
+
message: error.message
|
|
101
|
+
})));
|
|
102
|
+
return dumpFile(path);
|
|
103
|
+
}
|
|
104
|
+
});
|
|
105
|
+
/**
|
|
106
|
+
* @category constructor
|
|
107
|
+
* @since 1.0.0
|
|
108
|
+
*/
|
|
109
|
+
const makeLayer = options => Layer.effectDiscard(run(options));
|
|
110
|
+
exports.makeLayer = makeLayer;
|
|
111
|
+
//# sourceMappingURL=Migrator.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Migrator.js","names":["Command","_interopRequireWildcard","require","_FileSystem","_Path","Migrator","Object","keys","forEach","key","prototype","hasOwnProperty","call","_exportNames","exports","defineProperty","enumerable","get","Effect","Layer","Client","_getRequireWildcardCache","e","WeakMap","r","t","__esModule","default","has","n","__proto__","a","getOwnPropertyDescriptor","u","i","set","run","make","getClient","SqliteClient","ensureTable","sql","table","dumpSchema","path","dump","args","gen","_","config","filename","string","replace","trim","pipe","mapError","error","MigrationError","reason","message","dumpMigrations","dumpAll","map","all","concurrency","schema","migrations","dumpFile","file","fs","FileSystem","Path","makeDirectory","dirname","recursive","writeFileString","makeLayer","options","effectDiscard"],"sources":["../../src/Migrator.ts"],"sourcesContent":[null],"mappings":";;;;;;;;;;AAGA,IAAAA,OAAA,gBAAAC,uBAAA,eAAAC,OAAA;AAEA,IAAAC,WAAA,gBAAAD,OAAA;AACA,IAAAE,KAAA,gBAAAF,OAAA;AAEA,IAAAG,QAAA,gBAAAJ,uBAAA,eAAAC,OAAA;AAQAI,MAAA,CAAAC,IAAA,CAAAF,QAAA,EAAAG,OAAA,WAAAC,GAAA;EAAA,IAAAA,GAAA,kBAAAA,GAAA;EAAA,IAAAH,MAAA,CAAAI,SAAA,CAAAC,cAAA,CAAAC,IAAA,CAAAC,YAAA,EAAAJ,GAAA;EAAA,IAAAA,GAAA,IAAAK,OAAA,IAAAA,OAAA,CAAAL,GAAA,MAAAJ,QAAA,CAAAI,GAAA;EAAAH,MAAA,CAAAS,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OAAA,CAAA8D,SAAA,GAAAA,SAAA","ignoreList":[]}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.statement = exports.schema = exports.resolver = exports.migrator = exports.error = exports.client = void 0;
|
|
7
|
+
var _client = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("./Client.js"));
|
|
8
|
+
exports.client = _client;
|
|
9
|
+
var _error = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/sql/Error"));
|
|
10
|
+
exports.error = _error;
|
|
11
|
+
var _migrator = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("./Migrator.js"));
|
|
12
|
+
exports.migrator = _migrator;
|
|
13
|
+
var _resolver = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/sql/Resolver"));
|
|
14
|
+
exports.resolver = _resolver;
|
|
15
|
+
var _schema = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/sql/Schema"));
|
|
16
|
+
exports.schema = _schema;
|
|
17
|
+
var _statement = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/sql/Statement"));
|
|
18
|
+
exports.statement = _statement;
|
|
19
|
+
function _getRequireWildcardCache(e) {
|
|
20
|
+
if ("function" != typeof WeakMap) return null;
|
|
21
|
+
var r = new WeakMap(),
|
|
22
|
+
t = new WeakMap();
|
|
23
|
+
return (_getRequireWildcardCache = function (e) {
|
|
24
|
+
return e ? t : r;
|
|
25
|
+
})(e);
|
|
26
|
+
}
|
|
27
|
+
function _interopRequireWildcard(e, r) {
|
|
28
|
+
if (!r && e && e.__esModule) return e;
|
|
29
|
+
if (null === e || "object" != typeof e && "function" != typeof e) return {
|
|
30
|
+
default: e
|
|
31
|
+
};
|
|
32
|
+
var t = _getRequireWildcardCache(r);
|
|
33
|
+
if (t && t.has(e)) return t.get(e);
|
|
34
|
+
var n = {
|
|
35
|
+
__proto__: null
|
|
36
|
+
},
|
|
37
|
+
a = Object.defineProperty && Object.getOwnPropertyDescriptor;
|
|
38
|
+
for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) {
|
|
39
|
+
var i = a ? Object.getOwnPropertyDescriptor(e, u) : null;
|
|
40
|
+
i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u];
|
|
41
|
+
}
|
|
42
|
+
return n.default = e, t && t.set(e, n), n;
|
|
43
|
+
}
|
|
44
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","names":[],"sources":["../../src/index.ts"],"sourcesContent":[null],"mappings":"","ignoreList":[]}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @since 1.0.0
|
|
3
|
+
*/
|
|
4
|
+
import * as Client from "@effect/sql/Client";
|
|
5
|
+
import { SqlError } from "@effect/sql/Error";
|
|
6
|
+
import * as Statement from "@effect/sql/Statement";
|
|
7
|
+
import * as Config from "effect/Config";
|
|
8
|
+
import type { ConfigError } from "effect/ConfigError";
|
|
9
|
+
import * as Context from "effect/Context";
|
|
10
|
+
import * as Effect from "effect/Effect";
|
|
11
|
+
import * as Layer from "effect/Layer";
|
|
12
|
+
import * as Scope from "effect/Scope";
|
|
13
|
+
/**
|
|
14
|
+
* @category models
|
|
15
|
+
* @since 1.0.0
|
|
16
|
+
*/
|
|
17
|
+
export interface SqliteClient extends Client.Client {
|
|
18
|
+
readonly config: SqliteClientConfig;
|
|
19
|
+
readonly export: Effect.Effect<Uint8Array, SqlError>;
|
|
20
|
+
readonly loadExtension: (path: string) => Effect.Effect<void, SqlError>;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* @category tags
|
|
24
|
+
* @since 1.0.0
|
|
25
|
+
*/
|
|
26
|
+
export declare const SqliteClient: Context.Tag<SqliteClient, SqliteClient>;
|
|
27
|
+
/**
|
|
28
|
+
* @category models
|
|
29
|
+
* @since 1.0.0
|
|
30
|
+
*/
|
|
31
|
+
export interface SqliteClientConfig {
|
|
32
|
+
readonly filename: string;
|
|
33
|
+
readonly readonly?: boolean | undefined;
|
|
34
|
+
readonly create?: boolean | undefined;
|
|
35
|
+
readonly readwrite?: boolean | undefined;
|
|
36
|
+
readonly disableWAL?: boolean | undefined;
|
|
37
|
+
readonly transformResultNames?: ((str: string) => string) | undefined;
|
|
38
|
+
readonly transformQueryNames?: ((str: string) => string) | undefined;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* @category constructor
|
|
42
|
+
* @since 1.0.0
|
|
43
|
+
*/
|
|
44
|
+
export declare const make: (options: SqliteClientConfig) => Effect.Effect<SqliteClient, never, Scope.Scope>;
|
|
45
|
+
/**
|
|
46
|
+
* @category layers
|
|
47
|
+
* @since 1.0.0
|
|
48
|
+
*/
|
|
49
|
+
export declare const layer: (config: Config.Config.Wrap<SqliteClientConfig>) => Layer.Layer<SqliteClient, ConfigError>;
|
|
50
|
+
/**
|
|
51
|
+
* @category compiler
|
|
52
|
+
* @since 1.0.0
|
|
53
|
+
*/
|
|
54
|
+
export declare const makeCompiler: (transform?: ((_: string) => string) | undefined) => Statement.Compiler;
|
|
55
|
+
//# sourceMappingURL=Client.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Client.d.ts","sourceRoot":"","sources":["../../src/Client.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,OAAO,KAAK,MAAM,MAAM,oBAAoB,CAAA;AAE5C,OAAO,EAAE,QAAQ,EAAE,MAAM,mBAAmB,CAAA;AAC5C,OAAO,KAAK,SAAS,MAAM,uBAAuB,CAAA;AAElD,OAAO,KAAK,MAAM,MAAM,eAAe,CAAA;AACvC,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,oBAAoB,CAAA;AACrD,OAAO,KAAK,OAAO,MAAM,gBAAgB,CAAA;AACzC,O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AAK,WACR,aAAa,CAAC,IAAI,CAAC,kBAAkB,CAAC,KAC7C,WAAW,CAAC,YAAY,EAAE,WAAW,CAIrC,CAAA;AAIH;;;GAGG;AACH,eAAO,MAAM,YAAY,oBAAoB,MAAM,KAAK,MAAM,oCAM1D,CAAA"}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { CommandExecutor } from "@effect/platform/CommandExecutor";
|
|
2
|
+
import { FileSystem } from "@effect/platform/FileSystem";
|
|
3
|
+
import { Path } from "@effect/platform/Path";
|
|
4
|
+
import type { SqlError } from "@effect/sql/Error";
|
|
5
|
+
import * as Migrator from "@effect/sql/Migrator";
|
|
6
|
+
import * as Effect from "effect/Effect";
|
|
7
|
+
import * as Layer from "effect/Layer";
|
|
8
|
+
import * as Client from "./Client.js";
|
|
9
|
+
/**
|
|
10
|
+
* @since 1.0.0
|
|
11
|
+
*/
|
|
12
|
+
export * from "@effect/sql/Migrator";
|
|
13
|
+
/**
|
|
14
|
+
* @category constructor
|
|
15
|
+
* @since 1.0.0
|
|
16
|
+
*/
|
|
17
|
+
export declare const run: <R>(options: Migrator.MigratorOptions<R>) => Effect.Effect<ReadonlyArray<readonly [id: number, name: string]>, SqlError | Migrator.MigrationError, Client.SqliteClient | R | FileSystem | Path | CommandExecutor>;
|
|
18
|
+
/**
|
|
19
|
+
* @category constructor
|
|
20
|
+
* @since 1.0.0
|
|
21
|
+
*/
|
|
22
|
+
export declare const makeLayer: (options: Migrator.MigratorOptions) => Layer.Layer<never, SqlError | Migrator.MigrationError, Client.SqliteClient | FileSystem | Path | CommandExecutor>;
|
|
23
|
+
//# sourceMappingURL=Migrator.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Migrator.d.ts","sourceRoot":"","sources":["../../src/Migrator.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,kCAAkC,CAAA;AACvE,OAAO,EAAE,UAAU,EAAE,MAAM,6BAA6B,CAAA;AACxD,OAAO,EAAE,IAAI,EAAE,MAAM,uBAAuB,CAAA;AAC5C,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,mBAAmB,CAAA;AACjD,OAAO,KAAK,QAAQ,MAAM,sBAAsB,CAAA;AAChD,OAAO,KAAK,MAAM,MAAM,eAAe,CAAA;AACvC,OAAO,KAAK,KAAK,MAAM,cAAc,CAAA;AACrC,OAAO,KAAK,MAAM,MAAM,aAAa,CAAA;AAErC;;GAEG;AACH,cAAc,sBAAsB,CAAA;AAEpC;;;GAGG;AACH,eAAO,MAAM,GAAG,EAAE,CAAC,CAAC,EAClB,OAAO,EAAE,QAAQ,CAAC,eAAe,CAAC,CAAC,CAAC,KACjC,MAAM,CAAC,MAAM,CAChB,aAAa,CAAC,SAAS,CAAC,EAAE,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,CAAC,CAAC,EAClD,QAAQ,GAAG,QAAQ,CAAC,cAAc,EAClC,MAAM,CAAC,YAAY,GAAG,CAAC,GAAG,UAAU,GAAG,IAAI,GAAG,eAAe,CAoD7D,CAAA;AAEF;;;GAGG;AACH,eAAO,MAAM,SAAS,YACX,SAAS,eAAe,KAChC,WAAW,CAAC,KAAK,EAAE,QAAQ,GAAG,SAAS,cAAc,EAAE,OAAO,YAAY,GAAG,UAAU,GAAG,IAAI,GAAG,eAAe,CAChF,CAAA"}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @since 1.0.0
|
|
3
|
+
*/
|
|
4
|
+
/**
|
|
5
|
+
* @since 1.0.0
|
|
6
|
+
*/
|
|
7
|
+
export * as client from "./Client.js";
|
|
8
|
+
/**
|
|
9
|
+
* @since 1.0.0
|
|
10
|
+
*/
|
|
11
|
+
export * as error from "@effect/sql/Error";
|
|
12
|
+
/**
|
|
13
|
+
* @since 1.0.0
|
|
14
|
+
*/
|
|
15
|
+
export * as migrator from "./Migrator.js";
|
|
16
|
+
/**
|
|
17
|
+
* @since 1.0.0
|
|
18
|
+
*/
|
|
19
|
+
export * as resolver from "@effect/sql/Resolver";
|
|
20
|
+
/**
|
|
21
|
+
* @since 1.0.0
|
|
22
|
+
*/
|
|
23
|
+
export * as schema from "@effect/sql/Schema";
|
|
24
|
+
/**
|
|
25
|
+
* @since 1.0.0
|
|
26
|
+
*/
|
|
27
|
+
export * as statement from "@effect/sql/Statement";
|
|
28
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH;;GAEG;AACH,OAAO,KAAK,MAAM,MAAM,aAAa,CAAA;AAErC;;GAEG;AACH,OAAO,KAAK,KAAK,MAAM,mBAAmB,CAAA;AAE1C;;GAEG;AACH,OAAO,KAAK,QAAQ,MAAM,eAAe,CAAA;AAEzC;;GAEG;AACH,OAAO,KAAK,QAAQ,MAAM,sBAAsB,CAAA;AAEhD;;GAEG;AACH,OAAO,KAAK,MAAM,MAAM,oBAAoB,CAAA;AAE5C;;GAEG;AACH,OAAO,KAAK,SAAS,MAAM,uBAAuB,CAAA"}
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @since 1.0.0
|
|
3
|
+
*/
|
|
4
|
+
import * as Client from "@effect/sql/Client";
|
|
5
|
+
import { SqlError } from "@effect/sql/Error";
|
|
6
|
+
import * as Statement from "@effect/sql/Statement";
|
|
7
|
+
import { Database } from "bun:sqlite";
|
|
8
|
+
import * as Config from "effect/Config";
|
|
9
|
+
import * as Context from "effect/Context";
|
|
10
|
+
import * as Effect from "effect/Effect";
|
|
11
|
+
import { identity } from "effect/Function";
|
|
12
|
+
import * as Layer from "effect/Layer";
|
|
13
|
+
import * as Scope from "effect/Scope";
|
|
14
|
+
/**
|
|
15
|
+
* @category tags
|
|
16
|
+
* @since 1.0.0
|
|
17
|
+
*/
|
|
18
|
+
export const SqliteClient = /*#__PURE__*/Context.GenericTag("@effect/sql-sqlite-bun/SqliteClient");
|
|
19
|
+
/**
|
|
20
|
+
* @category constructor
|
|
21
|
+
* @since 1.0.0
|
|
22
|
+
*/
|
|
23
|
+
export const make = options => Effect.gen(function* (_) {
|
|
24
|
+
const compiler = makeCompiler(options.transformQueryNames);
|
|
25
|
+
const transformRows = Client.defaultTransforms(options.transformResultNames).array;
|
|
26
|
+
const makeConnection = Effect.gen(function* (_) {
|
|
27
|
+
const db = new Database(options.filename, {
|
|
28
|
+
readonly: options.readonly,
|
|
29
|
+
readwrite: options.readwrite ?? true,
|
|
30
|
+
create: options.create ?? true
|
|
31
|
+
});
|
|
32
|
+
yield* _(Effect.addFinalizer(() => Effect.sync(() => db.close())));
|
|
33
|
+
if (options.disableWAL !== true) {
|
|
34
|
+
db.run("PRAGMA journal_mode = WAL;");
|
|
35
|
+
}
|
|
36
|
+
const run = (sql, params = []) => Effect.try({
|
|
37
|
+
try: () => db.query(sql).all(...params),
|
|
38
|
+
catch: error => new SqlError({
|
|
39
|
+
error
|
|
40
|
+
})
|
|
41
|
+
});
|
|
42
|
+
const runTransform = options.transformResultNames ? (sql, params) => Effect.map(run(sql, params), transformRows) : run;
|
|
43
|
+
const runValues = (sql, params = []) => Effect.try({
|
|
44
|
+
try: () => db.query(sql).values(...params),
|
|
45
|
+
catch: error => new SqlError({
|
|
46
|
+
error
|
|
47
|
+
})
|
|
48
|
+
});
|
|
49
|
+
return identity({
|
|
50
|
+
execute(sql, params) {
|
|
51
|
+
return runTransform(sql, params);
|
|
52
|
+
},
|
|
53
|
+
executeValues(sql, params) {
|
|
54
|
+
return runValues(sql, params);
|
|
55
|
+
},
|
|
56
|
+
executeWithoutTransform(sql, params) {
|
|
57
|
+
return run(sql, params);
|
|
58
|
+
},
|
|
59
|
+
executeRaw(sql, params) {
|
|
60
|
+
return runTransform(sql, params);
|
|
61
|
+
},
|
|
62
|
+
executeStream(_sql, _params) {
|
|
63
|
+
return Effect.dieMessage("executeStream not implemented");
|
|
64
|
+
},
|
|
65
|
+
export: Effect.try({
|
|
66
|
+
try: () => db.serialize(),
|
|
67
|
+
catch: error => new SqlError({
|
|
68
|
+
error
|
|
69
|
+
})
|
|
70
|
+
}),
|
|
71
|
+
loadExtension: path => Effect.try({
|
|
72
|
+
try: () => db.loadExtension(path),
|
|
73
|
+
catch: error => new SqlError({
|
|
74
|
+
error
|
|
75
|
+
})
|
|
76
|
+
})
|
|
77
|
+
});
|
|
78
|
+
});
|
|
79
|
+
const semaphore = yield* _(Effect.makeSemaphore(1));
|
|
80
|
+
const connection = yield* _(makeConnection);
|
|
81
|
+
const acquirer = semaphore.withPermits(1)(Effect.succeed(connection));
|
|
82
|
+
const transactionAcquirer = Effect.uninterruptibleMask(restore => Effect.as(Effect.zipRight(restore(semaphore.take(1)), Effect.tap(Effect.scope, scope => Scope.addFinalizer(scope, semaphore.release(1)))), connection));
|
|
83
|
+
return Object.assign(Client.make({
|
|
84
|
+
acquirer,
|
|
85
|
+
compiler,
|
|
86
|
+
transactionAcquirer
|
|
87
|
+
}), {
|
|
88
|
+
config: options,
|
|
89
|
+
export: Effect.flatMap(acquirer, _ => _.export),
|
|
90
|
+
loadExtension: path => Effect.flatMap(acquirer, _ => _.loadExtension(path))
|
|
91
|
+
});
|
|
92
|
+
});
|
|
93
|
+
/**
|
|
94
|
+
* @category layers
|
|
95
|
+
* @since 1.0.0
|
|
96
|
+
*/
|
|
97
|
+
export const layer = config => Layer.scoped(SqliteClient, Effect.flatMap(Config.unwrap(config), make));
|
|
98
|
+
const escape = /*#__PURE__*/Statement.defaultEscape("\"");
|
|
99
|
+
/**
|
|
100
|
+
* @category compiler
|
|
101
|
+
* @since 1.0.0
|
|
102
|
+
*/
|
|
103
|
+
export const makeCompiler = transform => Statement.makeCompiler({
|
|
104
|
+
placeholder: _ => `?`,
|
|
105
|
+
onIdentifier: transform ? _ => escape(transform(_)) : escape,
|
|
106
|
+
onRecordUpdate: () => ["", []],
|
|
107
|
+
onCustom: () => ["", []]
|
|
108
|
+
});
|
|
109
|
+
//# sourceMappingURL=Client.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Client.js","names":["Client","SqlError","Statement","Database","Config","Context","Effect","identity","Layer","Scope","SqliteClient","GenericTag","make","options","gen","_","compiler","makeCompiler","transformQueryNames","transformRows","defaultTransforms","transformResultNames","array","makeConnection","db","filename","readonly","readwrite","create","addFinalizer","sync","close","disableWAL","run","sql","params","try","query","all","catch","error","runTransform","map","runValues","values","execute","executeValues","executeWithoutTransform","executeRaw","executeStream","_sql","_params","dieMessage","export","serialize","loadExtension","path","semaphore","makeSemaphore","connection","acquirer","withPermits","succeed","transactionAcquirer","uninterruptibleMask","restore","as","zipRight","take","tap","scope","release","Object","assign","config","flatMap","layer","scoped","unwrap","escape","defaultEscape","transform","placeholder","onIdentifier","onRecordUpdate","onCustom"],"sources":["../../src/Client.ts"],"sourcesContent":[null],"mappings":"AAAA;;;AAGA,OAAO,KAAKA,MAAM,MAAM,oBAAoB;AAE5C,SAASC,QAAQ,QAAQ,mBAAmB;AAC5C,OAAO,KAAKC,SAAS,MAAM,uBAAuB;AAClD,SAASC,QAAQ,QAAQ,YAAY;AACrC,OAAO,KAAKC,MAAM,MAAM,eAAe;AAEvC,OAAO,KAAKC,OAAO,MAAM,gBAAgB;AACzC,OAAO,KAAKC,MAAM,MAAM,eAAe;AACvC,SAASC,QAAQ,QAAQ,iBAAiB;AAC1C,OAAO,KAAKC,KAAK,MAAM,cAAc;AACrC,OAAO,KAAKC,KAAK,MAAM,cAAc;AAYrC;;;;AAIA,OAAO,MAAMC,YAAY,gBAA4CL,OAAO,CAACM,UAAU,CACrF,qCAAqC,CACtC;AAsBD;;;;AAIA,OAAO,MAAMC,IAAI,GACfC,OAA2B,IAE3BP,MAAM,CAACQ,GAAG,CAAC,WAAUC,CAAC;EACpB,MAAMC,QAAQ,GAAGC,YAAY,CAACJ,OAAO,CAACK,mBAAmB,CAAC;EAC1D,MAAMC,aAAa,G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O,MAAM/D,YAAY,GAAIgE,SAAiC,IAC5D/E,SAAS,CAACe,YAAY,CAAC;EACrBiE,WAAW,EAAGnE,CAAC,IAAK,GAAG;EACvBoE,YAAY,EAAEF,SAAS,GAAIlE,CAAC,IAAKgE,MAAM,CAACE,SAAS,CAAClE,CAAC,CAAC,CAAC,GAAGgE,MAAM;EAC9DK,cAAc,EAAEA,CAAA,KAAM,CAAC,EAAE,EAAE,EAAE,CAAC;EAC9BC,QAAQ,EAAEA,CAAA,KAAM,CAAC,EAAE,EAAE,EAAE;CACxB,CAAC","ignoreList":[]}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @since 1.0.0
|
|
3
|
+
*/
|
|
4
|
+
import * as Command from "@effect/platform/Command";
|
|
5
|
+
import { FileSystem } from "@effect/platform/FileSystem";
|
|
6
|
+
import { Path } from "@effect/platform/Path";
|
|
7
|
+
import * as Migrator from "@effect/sql/Migrator";
|
|
8
|
+
import * as Effect from "effect/Effect";
|
|
9
|
+
import * as Layer from "effect/Layer";
|
|
10
|
+
import * as Client from "./Client.js";
|
|
11
|
+
/**
|
|
12
|
+
* @since 1.0.0
|
|
13
|
+
*/
|
|
14
|
+
export * from "@effect/sql/Migrator";
|
|
15
|
+
/**
|
|
16
|
+
* @category constructor
|
|
17
|
+
* @since 1.0.0
|
|
18
|
+
*/
|
|
19
|
+
export const run = /*#__PURE__*/Migrator.make({
|
|
20
|
+
getClient: Client.SqliteClient,
|
|
21
|
+
ensureTable(sql, table) {
|
|
22
|
+
return sql`
|
|
23
|
+
CREATE TABLE IF NOT EXISTS ${sql(table)} (
|
|
24
|
+
migration_id integer PRIMARY KEY NOT NULL,
|
|
25
|
+
created_at datetime NOT NULL DEFAULT current_timestamp,
|
|
26
|
+
name VARCHAR(255) NOT NULL
|
|
27
|
+
)
|
|
28
|
+
`;
|
|
29
|
+
},
|
|
30
|
+
dumpSchema(sql, path, table) {
|
|
31
|
+
const dump = args => Effect.gen(function* (_) {
|
|
32
|
+
const dump = yield* _(Command.make("sqlite3", sql.config.filename, ...args), Command.string);
|
|
33
|
+
return dump.replace(/^create table sqlite_sequence\(.*$/im, "").replace(/\n{2,}/gm, "\n\n").trim();
|
|
34
|
+
}).pipe(Effect.mapError(error => new Migrator.MigrationError({
|
|
35
|
+
reason: "failed",
|
|
36
|
+
message: error.message
|
|
37
|
+
})));
|
|
38
|
+
const dumpSchema = dump([".schema"]);
|
|
39
|
+
const dumpMigrations = dump(["--cmd", `.mode insert ${table}`, `select * from ${table}`]);
|
|
40
|
+
const dumpAll = Effect.map(Effect.all([dumpSchema, dumpMigrations], {
|
|
41
|
+
concurrency: 2
|
|
42
|
+
}), ([schema, migrations]) => schema + "\n\n" + migrations);
|
|
43
|
+
const dumpFile = file => Effect.gen(function* (_) {
|
|
44
|
+
const fs = yield* _(FileSystem);
|
|
45
|
+
const path = yield* _(Path);
|
|
46
|
+
const dump = yield* _(dumpAll);
|
|
47
|
+
yield* _(fs.makeDirectory(path.dirname(file), {
|
|
48
|
+
recursive: true
|
|
49
|
+
}));
|
|
50
|
+
yield* _(fs.writeFileString(file, dump));
|
|
51
|
+
}).pipe(Effect.mapError(error => new Migrator.MigrationError({
|
|
52
|
+
reason: "failed",
|
|
53
|
+
message: error.message
|
|
54
|
+
})));
|
|
55
|
+
return dumpFile(path);
|
|
56
|
+
}
|
|
57
|
+
});
|
|
58
|
+
/**
|
|
59
|
+
* @category constructor
|
|
60
|
+
* @since 1.0.0
|
|
61
|
+
*/
|
|
62
|
+
export const makeLayer = options => Layer.effectDiscard(run(options));
|
|
63
|
+
//# sourceMappingURL=Migrator.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Migrator.js","names":["Command","FileSystem","Path","Migrator","Effect","Layer","Client","run","make","getClient","SqliteClient","ensureTable","sql","table","dumpSchema","path","dump","args","gen","_","config","filename","string","replace","trim","pipe","mapError","error","MigrationError","reason","message","dumpMigrations","dumpAll","map","all","concurrency","schema","migrations","dumpFile","file","fs","makeDirectory","dirname","recursive","writeFileString","makeLayer","options","effectDiscard"],"sources":["../../src/Migrator.ts"],"sourcesContent":[null],"mappings":"AAAA;;;AAGA,OAAO,KAAKA,OAAO,MAAM,0BAA0B;AAEnD,SAASC,UAAU,QAAQ,6BAA6B;AACxD,SAASC,IAAI,QAAQ,uBAAuB;AAE5C,OAAO,KAAKC,QAAQ,MAAM,sBAAsB;AAChD,OAAO,KAAKC,MAAM,MAAM,eAAe;AACvC,OAAO,KAAKC,KAAK,MAAM,cAAc;AACrC,OAAO,KAAKC,MAAM,MAAM,aAAa;AAErC;;;AAGA,cAAc,sBAAsB;AAEpC;;;;AAIA,OAAO,MAAMC,GAAG,gBAMZJ,QAAQ,CAACK,IAAI,CAAC;EAChBC,SAAS,EAAEH,MAAM,CAACI,YAAY;EAC9BC,WAAWA,CAACC,GAAG,EAAEC,KAAK;IACpB,OAAOD,GAAG;mCACqBA,GAAG,CAACC,KAAK,CAAC;;;;;KAKxC;EACH,CAAC;EACDC,UAAUA,CAACF,GAAG,EAAEG,IAAI,EAAEF,KAAK;IACzB,MAAMG,IAAI,GAAIC,IAAmB,IAC/Bb,MAAM,CAACc,GAAG,CAAC,WAAUC,CAAC;MACpB,MAAMH,IAAI,GAAG,OAAOG,CAAC,CACnBnB,OAAO,CAACQ,IAAI,CAAC,SAAS,EAAEI,GAAG,CAACQ,MAAM,CAACC,QAAQ,EAAE,GAAGJ,IAAI,CAAC,EACrDjB,OAAO,CAACsB,MAAM,CACf;MACD,OAAON,IAAI,CAACO,OAAO,CAAC,sCAAsC,EAAE,EAAE,CAAC,CAC5DA,OAAO,CAAC,UAAU,EAAE,MAAM,CAAC,CAC3BC,IAAI,EAAE;IACX,CAAC,CAAC,CAACC,IAAI,CACLrB,MAAM,CAACsB,QAAQ,CAAEC,KAAK,IAAK,IAAIxB,QAAQ,CAACyB,cAAc,CAAC;MAAEC,MAAM,EAAE,QAAQ;MAAEC,OAAO,EAAEH,KAAK,CAACG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M8B,SAAS,GACpBC,OAAiC,IAEjCzC,KAAK,CAAC0C,aAAa,CAACxC,GAAG,CAACuC,OAAO,CAAC,CAAC","ignoreList":[]}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @since 1.0.0
|
|
3
|
+
*/
|
|
4
|
+
/**
|
|
5
|
+
* @since 1.0.0
|
|
6
|
+
*/
|
|
7
|
+
export * as client from "./Client.js";
|
|
8
|
+
/**
|
|
9
|
+
* @since 1.0.0
|
|
10
|
+
*/
|
|
11
|
+
export * as error from "@effect/sql/Error";
|
|
12
|
+
/**
|
|
13
|
+
* @since 1.0.0
|
|
14
|
+
*/
|
|
15
|
+
export * as migrator from "./Migrator.js";
|
|
16
|
+
/**
|
|
17
|
+
* @since 1.0.0
|
|
18
|
+
*/
|
|
19
|
+
export * as resolver from "@effect/sql/Resolver";
|
|
20
|
+
/**
|
|
21
|
+
* @since 1.0.0
|
|
22
|
+
*/
|
|
23
|
+
export * as schema from "@effect/sql/Schema";
|
|
24
|
+
/**
|
|
25
|
+
* @since 1.0.0
|
|
26
|
+
*/
|
|
27
|
+
export * as statement from "@effect/sql/Statement";
|
|
28
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","names":["client","error","migrator","resolver","schema","statement"],"sources":["../../src/index.ts"],"sourcesContent":[null],"mappings":"AAAA;;;AAIA;;;AAGA,OAAO,KAAKA,MAAM,MAAM,aAAa;AAErC;;;AAGA,OAAO,KAAKC,KAAK,MAAM,mBAAmB;AAE1C;;;AAGA,OAAO,KAAKC,QAAQ,MAAM,eAAe;AAEzC;;;AAGA,OAAO,KAAKC,QAAQ,MAAM,sBAAsB;AAEhD;;;AAGA,OAAO,KAAKC,MAAM,MAAM,oBAAoB;AAE5C;;;AAGA,OAAO,KAAKC,SAAS,MAAM,uBAAuB","ignoreList":[]}
|
package/package.json
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@effect/sql-sqlite-bun",
|
|
3
|
+
"version": "0.0.0-snapshot-189d4cae80e186661241002ad9d729628096520f",
|
|
4
|
+
"description": "A SQLite toolkit for Effect",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "https://github.com/effect-ts/effect.git",
|
|
9
|
+
"directory": "packages/sql-sqlite-bun"
|
|
10
|
+
},
|
|
11
|
+
"sideEffects": [],
|
|
12
|
+
"peerDependencies": {
|
|
13
|
+
"@effect/platform": "^0.0.0-snapshot-189d4cae80e186661241002ad9d729628096520f",
|
|
14
|
+
"@effect/sql": "^0.0.0-snapshot-189d4cae80e186661241002ad9d729628096520f",
|
|
15
|
+
"effect": "^0.0.0-snapshot-189d4cae80e186661241002ad9d729628096520f"
|
|
16
|
+
},
|
|
17
|
+
"main": "./dist/cjs/index.js",
|
|
18
|
+
"module": "./dist/esm/index.js",
|
|
19
|
+
"types": "./dist/dts/index.d.ts",
|
|
20
|
+
"exports": {
|
|
21
|
+
"./package.json": "./package.json",
|
|
22
|
+
".": {
|
|
23
|
+
"types": "./dist/dts/index.d.ts",
|
|
24
|
+
"import": "./dist/esm/index.js",
|
|
25
|
+
"default": "./dist/cjs/index.js"
|
|
26
|
+
},
|
|
27
|
+
"./Client": {
|
|
28
|
+
"types": "./dist/dts/Client.d.ts",
|
|
29
|
+
"import": "./dist/esm/Client.js",
|
|
30
|
+
"default": "./dist/cjs/Client.js"
|
|
31
|
+
},
|
|
32
|
+
"./Migrator": {
|
|
33
|
+
"types": "./dist/dts/Migrator.d.ts",
|
|
34
|
+
"import": "./dist/esm/Migrator.js",
|
|
35
|
+
"default": "./dist/cjs/Migrator.js"
|
|
36
|
+
}
|
|
37
|
+
},
|
|
38
|
+
"typesVersions": {
|
|
39
|
+
"*": {
|
|
40
|
+
"Client": [
|
|
41
|
+
"./dist/dts/Client.d.ts"
|
|
42
|
+
],
|
|
43
|
+
"Migrator": [
|
|
44
|
+
"./dist/dts/Migrator.d.ts"
|
|
45
|
+
]
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
}
|
package/src/Client.ts
ADDED
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @since 1.0.0
|
|
3
|
+
*/
|
|
4
|
+
import * as Client from "@effect/sql/Client"
|
|
5
|
+
import type { Connection } from "@effect/sql/Connection"
|
|
6
|
+
import { SqlError } from "@effect/sql/Error"
|
|
7
|
+
import * as Statement from "@effect/sql/Statement"
|
|
8
|
+
import { Database } from "bun:sqlite"
|
|
9
|
+
import * as Config from "effect/Config"
|
|
10
|
+
import type { ConfigError } from "effect/ConfigError"
|
|
11
|
+
import * as Context from "effect/Context"
|
|
12
|
+
import * as Effect from "effect/Effect"
|
|
13
|
+
import { identity } from "effect/Function"
|
|
14
|
+
import * as Layer from "effect/Layer"
|
|
15
|
+
import * as Scope from "effect/Scope"
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* @category models
|
|
19
|
+
* @since 1.0.0
|
|
20
|
+
*/
|
|
21
|
+
export interface SqliteClient extends Client.Client {
|
|
22
|
+
readonly config: SqliteClientConfig
|
|
23
|
+
readonly export: Effect.Effect<Uint8Array, SqlError>
|
|
24
|
+
readonly loadExtension: (path: string) => Effect.Effect<void, SqlError>
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* @category tags
|
|
29
|
+
* @since 1.0.0
|
|
30
|
+
*/
|
|
31
|
+
export const SqliteClient: Context.Tag<SqliteClient, SqliteClient> = Context.GenericTag(
|
|
32
|
+
"@effect/sql-sqlite-bun/SqliteClient"
|
|
33
|
+
)
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* @category models
|
|
37
|
+
* @since 1.0.0
|
|
38
|
+
*/
|
|
39
|
+
export interface SqliteClientConfig {
|
|
40
|
+
readonly filename: string
|
|
41
|
+
readonly readonly?: boolean | undefined
|
|
42
|
+
readonly create?: boolean | undefined
|
|
43
|
+
readonly readwrite?: boolean | undefined
|
|
44
|
+
readonly disableWAL?: boolean | undefined
|
|
45
|
+
|
|
46
|
+
readonly transformResultNames?: ((str: string) => string) | undefined
|
|
47
|
+
readonly transformQueryNames?: ((str: string) => string) | undefined
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
interface SqliteConnection extends Connection {
|
|
51
|
+
readonly export: Effect.Effect<Uint8Array, SqlError>
|
|
52
|
+
readonly loadExtension: (path: string) => Effect.Effect<void, SqlError>
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* @category constructor
|
|
57
|
+
* @since 1.0.0
|
|
58
|
+
*/
|
|
59
|
+
export const make = (
|
|
60
|
+
options: SqliteClientConfig
|
|
61
|
+
): Effect.Effect<SqliteClient, never, Scope.Scope> =>
|
|
62
|
+
Effect.gen(function*(_) {
|
|
63
|
+
const compiler = makeCompiler(options.transformQueryNames)
|
|
64
|
+
const transformRows = Client.defaultTransforms(
|
|
65
|
+
options.transformResultNames!
|
|
66
|
+
).array
|
|
67
|
+
|
|
68
|
+
const makeConnection = Effect.gen(function*(_) {
|
|
69
|
+
const db = new Database(options.filename, {
|
|
70
|
+
readonly: options.readonly,
|
|
71
|
+
readwrite: options.readwrite ?? true,
|
|
72
|
+
create: options.create ?? true
|
|
73
|
+
} as any)
|
|
74
|
+
yield* _(Effect.addFinalizer(() => Effect.sync(() => db.close())))
|
|
75
|
+
|
|
76
|
+
if (options.disableWAL !== true) {
|
|
77
|
+
db.run("PRAGMA journal_mode = WAL;")
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
const run = (
|
|
81
|
+
sql: string,
|
|
82
|
+
params: ReadonlyArray<Statement.Primitive> = []
|
|
83
|
+
) =>
|
|
84
|
+
Effect.try({
|
|
85
|
+
try: () => db.query(sql).all(...(params as any)) as Array<any>,
|
|
86
|
+
catch: (error) => new SqlError({ error })
|
|
87
|
+
})
|
|
88
|
+
|
|
89
|
+
const runTransform = options.transformResultNames
|
|
90
|
+
? (sql: string, params?: ReadonlyArray<Statement.Primitive>) => Effect.map(run(sql, params), transformRows)
|
|
91
|
+
: run
|
|
92
|
+
|
|
93
|
+
const runValues = (
|
|
94
|
+
sql: string,
|
|
95
|
+
params: ReadonlyArray<Statement.Primitive> = []
|
|
96
|
+
) =>
|
|
97
|
+
Effect.try({
|
|
98
|
+
try: () => db.query(sql).values(...(params as any)) as Array<any>,
|
|
99
|
+
catch: (error) => new SqlError({ error })
|
|
100
|
+
})
|
|
101
|
+
|
|
102
|
+
return identity<SqliteConnection>({
|
|
103
|
+
execute(sql, params) {
|
|
104
|
+
return runTransform(sql, params)
|
|
105
|
+
},
|
|
106
|
+
executeValues(sql, params) {
|
|
107
|
+
return runValues(sql, params)
|
|
108
|
+
},
|
|
109
|
+
executeWithoutTransform(sql, params) {
|
|
110
|
+
return run(sql, params)
|
|
111
|
+
},
|
|
112
|
+
executeRaw(sql, params) {
|
|
113
|
+
return runTransform(sql, params)
|
|
114
|
+
},
|
|
115
|
+
executeStream(_sql, _params) {
|
|
116
|
+
return Effect.dieMessage("executeStream not implemented")
|
|
117
|
+
},
|
|
118
|
+
export: Effect.try({
|
|
119
|
+
try: () => db.serialize(),
|
|
120
|
+
catch: (error) => new SqlError({ error })
|
|
121
|
+
}),
|
|
122
|
+
loadExtension: (path) =>
|
|
123
|
+
Effect.try({
|
|
124
|
+
try: () => db.loadExtension(path),
|
|
125
|
+
catch: (error) => new SqlError({ error })
|
|
126
|
+
})
|
|
127
|
+
})
|
|
128
|
+
})
|
|
129
|
+
|
|
130
|
+
const semaphore = yield* _(Effect.makeSemaphore(1))
|
|
131
|
+
const connection = yield* _(makeConnection)
|
|
132
|
+
|
|
133
|
+
const acquirer = semaphore.withPermits(1)(Effect.succeed(connection))
|
|
134
|
+
const transactionAcquirer = Effect.uninterruptibleMask((restore) =>
|
|
135
|
+
Effect.as(
|
|
136
|
+
Effect.zipRight(
|
|
137
|
+
restore(semaphore.take(1)),
|
|
138
|
+
Effect.tap(
|
|
139
|
+
Effect.scope,
|
|
140
|
+
(scope) => Scope.addFinalizer(scope, semaphore.release(1))
|
|
141
|
+
)
|
|
142
|
+
),
|
|
143
|
+
connection
|
|
144
|
+
)
|
|
145
|
+
)
|
|
146
|
+
|
|
147
|
+
return Object.assign(
|
|
148
|
+
Client.make({
|
|
149
|
+
acquirer,
|
|
150
|
+
compiler,
|
|
151
|
+
transactionAcquirer
|
|
152
|
+
}),
|
|
153
|
+
{
|
|
154
|
+
config: options,
|
|
155
|
+
export: Effect.flatMap(acquirer, (_) => _.export),
|
|
156
|
+
loadExtension: (path: string) => Effect.flatMap(acquirer, (_) => _.loadExtension(path))
|
|
157
|
+
}
|
|
158
|
+
)
|
|
159
|
+
})
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* @category layers
|
|
163
|
+
* @since 1.0.0
|
|
164
|
+
*/
|
|
165
|
+
export const layer = (
|
|
166
|
+
config: Config.Config.Wrap<SqliteClientConfig>
|
|
167
|
+
): Layer.Layer<SqliteClient, ConfigError> =>
|
|
168
|
+
Layer.scoped(
|
|
169
|
+
SqliteClient,
|
|
170
|
+
Effect.flatMap(Config.unwrap(config), make)
|
|
171
|
+
)
|
|
172
|
+
|
|
173
|
+
const escape = Statement.defaultEscape("\"")
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* @category compiler
|
|
177
|
+
* @since 1.0.0
|
|
178
|
+
*/
|
|
179
|
+
export const makeCompiler = (transform?: (_: string) => string) =>
|
|
180
|
+
Statement.makeCompiler({
|
|
181
|
+
placeholder: (_) => `?`,
|
|
182
|
+
onIdentifier: transform ? (_) => escape(transform(_)) : escape,
|
|
183
|
+
onRecordUpdate: () => ["", []],
|
|
184
|
+
onCustom: () => ["", []]
|
|
185
|
+
})
|
package/src/Migrator.ts
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
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 Client from "./Client.js"
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* @since 1.0.0
|
|
16
|
+
*/
|
|
17
|
+
export * from "@effect/sql/Migrator"
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* @category constructor
|
|
21
|
+
* @since 1.0.0
|
|
22
|
+
*/
|
|
23
|
+
export const run: <R>(
|
|
24
|
+
options: Migrator.MigratorOptions<R>
|
|
25
|
+
) => Effect.Effect<
|
|
26
|
+
ReadonlyArray<readonly [id: number, name: string]>,
|
|
27
|
+
SqlError | Migrator.MigrationError,
|
|
28
|
+
Client.SqliteClient | R | FileSystem | Path | CommandExecutor
|
|
29
|
+
> = Migrator.make({
|
|
30
|
+
getClient: Client.SqliteClient,
|
|
31
|
+
ensureTable(sql, table) {
|
|
32
|
+
return sql`
|
|
33
|
+
CREATE TABLE IF NOT EXISTS ${sql(table)} (
|
|
34
|
+
migration_id integer PRIMARY KEY NOT NULL,
|
|
35
|
+
created_at datetime NOT NULL DEFAULT current_timestamp,
|
|
36
|
+
name VARCHAR(255) NOT NULL
|
|
37
|
+
)
|
|
38
|
+
`
|
|
39
|
+
},
|
|
40
|
+
dumpSchema(sql, path, table) {
|
|
41
|
+
const dump = (args: Array<string>) =>
|
|
42
|
+
Effect.gen(function*(_) {
|
|
43
|
+
const dump = yield* _(
|
|
44
|
+
Command.make("sqlite3", sql.config.filename, ...args),
|
|
45
|
+
Command.string
|
|
46
|
+
)
|
|
47
|
+
return dump.replace(/^create table sqlite_sequence\(.*$/im, "")
|
|
48
|
+
.replace(/\n{2,}/gm, "\n\n")
|
|
49
|
+
.trim()
|
|
50
|
+
}).pipe(
|
|
51
|
+
Effect.mapError((error) => new Migrator.MigrationError({ reason: "failed", message: error.message }))
|
|
52
|
+
)
|
|
53
|
+
|
|
54
|
+
const dumpSchema = dump([".schema"])
|
|
55
|
+
|
|
56
|
+
const dumpMigrations = dump([
|
|
57
|
+
"--cmd",
|
|
58
|
+
`.mode insert ${table}`,
|
|
59
|
+
`select * from ${table}`
|
|
60
|
+
])
|
|
61
|
+
|
|
62
|
+
const dumpAll = Effect.map(
|
|
63
|
+
Effect.all([dumpSchema, dumpMigrations], { concurrency: 2 }),
|
|
64
|
+
([schema, migrations]) => schema + "\n\n" + migrations
|
|
65
|
+
)
|
|
66
|
+
|
|
67
|
+
const dumpFile = (file: string) =>
|
|
68
|
+
Effect.gen(function*(_) {
|
|
69
|
+
const fs = yield* _(FileSystem)
|
|
70
|
+
const path = yield* _(Path)
|
|
71
|
+
const dump = yield* _(dumpAll)
|
|
72
|
+
yield* _(fs.makeDirectory(path.dirname(file), { recursive: true }))
|
|
73
|
+
yield* _(fs.writeFileString(file, dump))
|
|
74
|
+
}).pipe(
|
|
75
|
+
Effect.mapError((error) => new Migrator.MigrationError({ reason: "failed", message: error.message }))
|
|
76
|
+
)
|
|
77
|
+
|
|
78
|
+
return dumpFile(path)
|
|
79
|
+
}
|
|
80
|
+
})
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* @category constructor
|
|
84
|
+
* @since 1.0.0
|
|
85
|
+
*/
|
|
86
|
+
export const makeLayer = (
|
|
87
|
+
options: Migrator.MigratorOptions
|
|
88
|
+
): Layer.Layer<never, SqlError | Migrator.MigrationError, Client.SqliteClient | FileSystem | Path | CommandExecutor> =>
|
|
89
|
+
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"
|