@effect/sql-sqlite-node 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 +173 -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 +56 -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 +133 -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 +51 -0
- package/src/Client.ts +218 -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,173 @@
|
|
|
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 _betterSqlite = /*#__PURE__*/_interopRequireDefault( /*#__PURE__*/require("better-sqlite3"));
|
|
11
|
+
var Cache = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("effect/Cache"));
|
|
12
|
+
var Config = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("effect/Config"));
|
|
13
|
+
var Context = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("effect/Context"));
|
|
14
|
+
var Duration = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("effect/Duration"));
|
|
15
|
+
var Effect = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("effect/Effect"));
|
|
16
|
+
var _Function = /*#__PURE__*/require("effect/Function");
|
|
17
|
+
var Layer = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("effect/Layer"));
|
|
18
|
+
var Scope = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("effect/Scope"));
|
|
19
|
+
function _interopRequireDefault(obj) {
|
|
20
|
+
return obj && obj.__esModule ? obj : {
|
|
21
|
+
default: obj
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
function _getRequireWildcardCache(e) {
|
|
25
|
+
if ("function" != typeof WeakMap) return null;
|
|
26
|
+
var r = new WeakMap(),
|
|
27
|
+
t = new WeakMap();
|
|
28
|
+
return (_getRequireWildcardCache = function (e) {
|
|
29
|
+
return e ? t : r;
|
|
30
|
+
})(e);
|
|
31
|
+
}
|
|
32
|
+
function _interopRequireWildcard(e, r) {
|
|
33
|
+
if (!r && e && e.__esModule) return e;
|
|
34
|
+
if (null === e || "object" != typeof e && "function" != typeof e) return {
|
|
35
|
+
default: e
|
|
36
|
+
};
|
|
37
|
+
var t = _getRequireWildcardCache(r);
|
|
38
|
+
if (t && t.has(e)) return t.get(e);
|
|
39
|
+
var n = {
|
|
40
|
+
__proto__: null
|
|
41
|
+
},
|
|
42
|
+
a = Object.defineProperty && Object.getOwnPropertyDescriptor;
|
|
43
|
+
for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) {
|
|
44
|
+
var i = a ? Object.getOwnPropertyDescriptor(e, u) : null;
|
|
45
|
+
i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u];
|
|
46
|
+
}
|
|
47
|
+
return n.default = e, t && t.set(e, n), n;
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* @since 1.0.0
|
|
51
|
+
*/
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* @category tags
|
|
55
|
+
* @since 1.0.0
|
|
56
|
+
*/
|
|
57
|
+
const SqliteClient = exports.SqliteClient = /*#__PURE__*/Context.GenericTag("@effect/sql-sqlite-node/SqliteClient");
|
|
58
|
+
/**
|
|
59
|
+
* @category constructor
|
|
60
|
+
* @since 1.0.0
|
|
61
|
+
*/
|
|
62
|
+
const make = options => Effect.gen(function* (_) {
|
|
63
|
+
const compiler = makeCompiler(options.transformQueryNames);
|
|
64
|
+
const transformRows = Client.defaultTransforms(options.transformResultNames).array;
|
|
65
|
+
const makeConnection = Effect.gen(function* (_) {
|
|
66
|
+
const db = new _betterSqlite.default(options.filename, {
|
|
67
|
+
readonly: options.readonly ?? false
|
|
68
|
+
});
|
|
69
|
+
yield* _(Effect.addFinalizer(() => Effect.sync(() => db.close())));
|
|
70
|
+
if (options.disableWAL !== true) {
|
|
71
|
+
db.pragma("journal_mode = WAL");
|
|
72
|
+
}
|
|
73
|
+
const prepareCache = yield* _(Cache.make({
|
|
74
|
+
capacity: options.prepareCacheSize ?? 200,
|
|
75
|
+
timeToLive: options.prepareCacheTTL ?? Duration.minutes(10),
|
|
76
|
+
lookup: sql => Effect.try({
|
|
77
|
+
try: () => db.prepare(sql),
|
|
78
|
+
catch: error => new _Error.SqlError({
|
|
79
|
+
error
|
|
80
|
+
})
|
|
81
|
+
})
|
|
82
|
+
}));
|
|
83
|
+
const run = (sql, params = []) => Effect.flatMap(prepareCache.get(sql), statement => Effect.try({
|
|
84
|
+
try: () => {
|
|
85
|
+
if (statement.reader) {
|
|
86
|
+
return statement.all(...params);
|
|
87
|
+
}
|
|
88
|
+
statement.run(...params);
|
|
89
|
+
return [];
|
|
90
|
+
},
|
|
91
|
+
catch: error => new _Error.SqlError({
|
|
92
|
+
error
|
|
93
|
+
})
|
|
94
|
+
}));
|
|
95
|
+
const runTransform = options.transformResultNames ? (sql, params) => Effect.map(run(sql, params), transformRows) : run;
|
|
96
|
+
const runValues = (sql, params) => Effect.acquireUseRelease(prepareCache.get(sql).pipe(Effect.map(_ => _.raw(true))), statement => Effect.try({
|
|
97
|
+
try: () => {
|
|
98
|
+
if (statement.reader) {
|
|
99
|
+
return statement.all(...params);
|
|
100
|
+
}
|
|
101
|
+
statement.run(...params);
|
|
102
|
+
return [];
|
|
103
|
+
},
|
|
104
|
+
catch: error => new _Error.SqlError({
|
|
105
|
+
error
|
|
106
|
+
})
|
|
107
|
+
}), statement => Effect.sync(() => statement.raw(false)));
|
|
108
|
+
return (0, _Function.identity)({
|
|
109
|
+
execute(sql, params) {
|
|
110
|
+
return runTransform(sql, params);
|
|
111
|
+
},
|
|
112
|
+
executeValues(sql, params) {
|
|
113
|
+
return runValues(sql, params);
|
|
114
|
+
},
|
|
115
|
+
executeWithoutTransform(sql, params) {
|
|
116
|
+
return run(sql, params);
|
|
117
|
+
},
|
|
118
|
+
executeRaw(sql, params) {
|
|
119
|
+
return runTransform(sql, params);
|
|
120
|
+
},
|
|
121
|
+
executeStream(_sql, _params) {
|
|
122
|
+
return Effect.dieMessage("executeStream not implemented");
|
|
123
|
+
},
|
|
124
|
+
export: Effect.try({
|
|
125
|
+
try: () => db.serialize(),
|
|
126
|
+
catch: error => new _Error.SqlError({
|
|
127
|
+
error
|
|
128
|
+
})
|
|
129
|
+
}),
|
|
130
|
+
loadExtension(path) {
|
|
131
|
+
return Effect.try({
|
|
132
|
+
try: () => db.loadExtension(path),
|
|
133
|
+
catch: error => new _Error.SqlError({
|
|
134
|
+
error
|
|
135
|
+
})
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
});
|
|
139
|
+
});
|
|
140
|
+
const semaphore = yield* _(Effect.makeSemaphore(1));
|
|
141
|
+
const connection = yield* _(makeConnection);
|
|
142
|
+
const acquirer = semaphore.withPermits(1)(Effect.succeed(connection));
|
|
143
|
+
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));
|
|
144
|
+
return Object.assign(Client.make({
|
|
145
|
+
acquirer,
|
|
146
|
+
compiler,
|
|
147
|
+
transactionAcquirer
|
|
148
|
+
}), {
|
|
149
|
+
config: options,
|
|
150
|
+
export: Effect.flatMap(acquirer, _ => _.export),
|
|
151
|
+
loadExtension: path => Effect.flatMap(acquirer, _ => _.loadExtension(path))
|
|
152
|
+
});
|
|
153
|
+
});
|
|
154
|
+
/**
|
|
155
|
+
* @category layers
|
|
156
|
+
* @since 1.0.0
|
|
157
|
+
*/
|
|
158
|
+
exports.make = make;
|
|
159
|
+
const layer = config => Layer.scoped(SqliteClient, Effect.flatMap(Config.unwrap(config), make));
|
|
160
|
+
exports.layer = layer;
|
|
161
|
+
const escape = /*#__PURE__*/Statement.defaultEscape("\"");
|
|
162
|
+
/**
|
|
163
|
+
* @category compiler
|
|
164
|
+
* @since 1.0.0
|
|
165
|
+
*/
|
|
166
|
+
const makeCompiler = transform => Statement.makeCompiler({
|
|
167
|
+
placeholder: _ => `?`,
|
|
168
|
+
onIdentifier: transform ? _ => escape(transform(_)) : escape,
|
|
169
|
+
onRecordUpdate: () => ["", []],
|
|
170
|
+
onCustom: () => ["", []]
|
|
171
|
+
});
|
|
172
|
+
exports.makeCompiler = makeCompiler;
|
|
173
|
+
//# sourceMappingURL=Client.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Client.js","names":["Client","_interopRequireWildcard","require","_Error","Statement","_betterSqlite","_interopRequireDefault","Cache","Config","Context","Duration","Effect","_Function","Layer","Scope","obj","__esModule","default","_getRequireWildcardCache","e","WeakMap","r","t","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","filename","readonly","addFinalizer","sync","close","disableWAL","pragma","prepareCache","capacity","prepareCacheSize","timeToLive","prepareCacheTTL","minutes","lookup","sql","try","prepare","catch","error","SqlError","run","params","flatMap","statement","reader","all","runTransform","map","runValues","acquireUseRelease","pipe","raw","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","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,aAAA,gBAAAC,sBAAA,eAAAJ,OAAA;AACA,IAAAK,KAAA,gBAAAN,uBAAA,eAAAC,OAAA;AACA,IAAAM,MAAA,gBAAAP,uBAAA,eAAAC,OAAA;AAEA,IAAAO,OAAA,gBAAAR,uBAAA,eAAAC,OAAA;AACA,IAAAQ,QAAA,gBAAAT,uBAAA,eAAAC,OAAA;AACA,IAAAS,MAAA,gBAAAV,uBAAA,eAAAC,OAAA;AACA,IAAAU,SAAA,gBAAAV,OAAA;AACA,IAAAW,KAAA,gBAAAZ,uBAAA,eAAAC,OAAA;AACA,IAAAY,KAAA,gBAAAb,uBAAA,eAAAC,OAAA;AAAqC,SAAAI,uBAAAS,GAAA;EAAA,OAAAA,GAAA,IAAAA,GAAA,CAAAC,UAAA,GAAAD,GAAA;IAAAE,OAAA,EAAAF;EAAA;AAAA;AAAA,SAAAG,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,SAAAlB,wBAAAkB,CAAA,EAAAE,CAAA;EAAA,KAAAA,CAAA,IAAAF,CAAA,IAAAA,CAAA,CA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AC,IAAK2E,MAAM,CAACE,SAAS,CAAC7E,CAAC,CAAC,CAAC,GAAG2E,MAAM;EAC9DK,cAAc,EAAEA,CAAA,KAAM,CAAC,EAAE,EAAE,EAAE,CAAC;EAC9BC,QAAQ,EAAEA,CAAA,KAAM,CAAC,EAAE,EAAE,EAAE;CACxB,CAAC;AAAAtF,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,cAAA,CAAAD,OAAA,EAAAL,GAAA;IAAAO,UAAA;IAAAC,GAAA,WAAAA,CAAA;MAAA,OAAAZ,QAAA,CAAAI,GAAA;IAAA;EAAA;AAAA;AAPA,IAAAS,MAAA,gBAAAjB,uBAAA,eAAAC,OAAA;AACA,IAAAiB,KAAA,gBAAAlB,uBAAA,eAAAC,OAAA;AACA,IAAAkB,MAAA,gBAAAnB,uBAAA,eAAAC,OAAA;AAAqC,SAAAmB,yBAAAC,CAAA;EAAA,yBAAAC,OAA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UAAU,CACvD;IAED,MAAMC,QAAQ,GAAIC,IAAY,IAC5BlD,MAAM,CAAC6B,GAAG,CAAC,WAAUC,CAAC;MACpB,MAAMqB,EAAE,GAAG,OAAOrB,CAAC,CAAC7C,WAAA,CAAAmE,UAAU,CAAC;MAC/B,MAAM1B,IAAI,GAAG,OAAOI,CAAC,CAAC5C,KAAA,CAAAmE,IAAI,CAAC;MAC3B,MAAM1B,IAAI,GAAG,OAAOG,CAAC,CAACa,OAAO,CAAC;MAC9B,OAAOb,CAAC,CAACqB,EAAE,CAACG,aAAa,CAAC5B,IAAI,CAAC6B,OAAO,CAACL,IAAI,CAAC,EAAE;QAAEM,SAAS,EAAE;MAAI,CAAE,CAAC,CAAC;MACnE,OAAO1B,CAAC,CAACqB,EAAE,CAACM,eAAe,CAACP,IAAI,EAAEvB,IAAI,CAAC,CAAC;IAC1C,CAAC,CAAC,CAACS,IAAI,CACLpC,MAAM,CAACqC,QAAQ,CAAEC,KAAK,IAAK,IAAInD,QAAQ,CAACoD,cAAc,CAAC;MAAEC,MAAM,EAAE,QAAQ;MAAEC,OAAO,EAAEH,KAAK,CAACG;IAAO,CAAE,CAAC,CAAC,CACtG;IAEH,OAAOQ,QAAQ,CAACvB,IAAI,CAAC;EACvB;CACD,CAAC;AAEF;;;;AAIO,MAAMgC,SAAS,GACpBC,OAAiC,IAEjC1D,KAAK,CAAC2D,aAAa,CAAC1C,GAAG,CAACyC,OAAO,CAAC,CAAC;AAAA/D,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,56 @@
|
|
|
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 Duration from "effect/Duration";
|
|
11
|
+
import * as Effect from "effect/Effect";
|
|
12
|
+
import * as Layer from "effect/Layer";
|
|
13
|
+
import * as Scope from "effect/Scope";
|
|
14
|
+
/**
|
|
15
|
+
* @category models
|
|
16
|
+
* @since 1.0.0
|
|
17
|
+
*/
|
|
18
|
+
export interface SqliteClient extends Client.Client {
|
|
19
|
+
readonly config: SqliteClientConfig;
|
|
20
|
+
readonly export: Effect.Effect<Uint8Array, SqlError>;
|
|
21
|
+
readonly loadExtension: (path: string) => Effect.Effect<void, SqlError>;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* @category tags
|
|
25
|
+
* @since 1.0.0
|
|
26
|
+
*/
|
|
27
|
+
export declare const SqliteClient: Context.Tag<SqliteClient, SqliteClient>;
|
|
28
|
+
/**
|
|
29
|
+
* @category models
|
|
30
|
+
* @since 1.0.0
|
|
31
|
+
*/
|
|
32
|
+
export interface SqliteClientConfig {
|
|
33
|
+
readonly filename: string;
|
|
34
|
+
readonly readonly?: boolean | undefined;
|
|
35
|
+
readonly prepareCacheSize?: number | undefined;
|
|
36
|
+
readonly prepareCacheTTL?: Duration.DurationInput | undefined;
|
|
37
|
+
readonly disableWAL?: boolean | undefined;
|
|
38
|
+
readonly transformResultNames?: ((str: string) => string) | undefined;
|
|
39
|
+
readonly transformQueryNames?: ((str: string) => string) | undefined;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* @category constructor
|
|
43
|
+
* @since 1.0.0
|
|
44
|
+
*/
|
|
45
|
+
export declare const make: (options: SqliteClientConfig) => Effect.Effect<SqliteClient, never, Scope.Scope>;
|
|
46
|
+
/**
|
|
47
|
+
* @category layers
|
|
48
|
+
* @since 1.0.0
|
|
49
|
+
*/
|
|
50
|
+
export declare const layer: (config: Config.Config.Wrap<SqliteClientConfig>) => Layer.Layer<SqliteClient, ConfigError>;
|
|
51
|
+
/**
|
|
52
|
+
* @category compiler
|
|
53
|
+
* @since 1.0.0
|
|
54
|
+
*/
|
|
55
|
+
export declare const makeCompiler: (transform?: ((_: string) => string) | undefined) => Statement.Compiler;
|
|
56
|
+
//# 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;AAGlD,OAAO,KAAK,MAAM,MAAM,eAAe,CAAA;AACvC,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,oBAAoB,CAAA;AACrD,OAAO,KAAK,OAAO,MAAM,gBAAgB,CAAA;AACzC,OAAO,KAAK,QAAQ,MAAM,iBAAiB,CAAA;AAC3C,OAAO,KAAK,MAAM,MAAM,eAAe,CAAA;AAEvC,OAAO,KAAK,KAAK,MAAM,cAAc,CAAA;AACrC,OAAO,KAAK,KAAK,MAAM,cAAc,CAAA;AAErC;;;GAGG;AACH,MAAM,WAAW,YAAa,SAAQ,MAAM,CAAC,MAAM;IACjD,QAAQ,CAAC,MAAM,EAAE,kBAAkB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|
|
|
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,133 @@
|
|
|
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 Sqlite from "better-sqlite3";
|
|
8
|
+
import * as Cache from "effect/Cache";
|
|
9
|
+
import * as Config from "effect/Config";
|
|
10
|
+
import * as Context from "effect/Context";
|
|
11
|
+
import * as Duration from "effect/Duration";
|
|
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
|
+
* @category tags
|
|
18
|
+
* @since 1.0.0
|
|
19
|
+
*/
|
|
20
|
+
export const SqliteClient = /*#__PURE__*/Context.GenericTag("@effect/sql-sqlite-node/SqliteClient");
|
|
21
|
+
/**
|
|
22
|
+
* @category constructor
|
|
23
|
+
* @since 1.0.0
|
|
24
|
+
*/
|
|
25
|
+
export const make = options => Effect.gen(function* (_) {
|
|
26
|
+
const compiler = makeCompiler(options.transformQueryNames);
|
|
27
|
+
const transformRows = Client.defaultTransforms(options.transformResultNames).array;
|
|
28
|
+
const makeConnection = Effect.gen(function* (_) {
|
|
29
|
+
const db = new Sqlite(options.filename, {
|
|
30
|
+
readonly: options.readonly ?? false
|
|
31
|
+
});
|
|
32
|
+
yield* _(Effect.addFinalizer(() => Effect.sync(() => db.close())));
|
|
33
|
+
if (options.disableWAL !== true) {
|
|
34
|
+
db.pragma("journal_mode = WAL");
|
|
35
|
+
}
|
|
36
|
+
const prepareCache = yield* _(Cache.make({
|
|
37
|
+
capacity: options.prepareCacheSize ?? 200,
|
|
38
|
+
timeToLive: options.prepareCacheTTL ?? Duration.minutes(10),
|
|
39
|
+
lookup: sql => Effect.try({
|
|
40
|
+
try: () => db.prepare(sql),
|
|
41
|
+
catch: error => new SqlError({
|
|
42
|
+
error
|
|
43
|
+
})
|
|
44
|
+
})
|
|
45
|
+
}));
|
|
46
|
+
const run = (sql, params = []) => Effect.flatMap(prepareCache.get(sql), statement => Effect.try({
|
|
47
|
+
try: () => {
|
|
48
|
+
if (statement.reader) {
|
|
49
|
+
return statement.all(...params);
|
|
50
|
+
}
|
|
51
|
+
statement.run(...params);
|
|
52
|
+
return [];
|
|
53
|
+
},
|
|
54
|
+
catch: error => new SqlError({
|
|
55
|
+
error
|
|
56
|
+
})
|
|
57
|
+
}));
|
|
58
|
+
const runTransform = options.transformResultNames ? (sql, params) => Effect.map(run(sql, params), transformRows) : run;
|
|
59
|
+
const runValues = (sql, params) => Effect.acquireUseRelease(prepareCache.get(sql).pipe(Effect.map(_ => _.raw(true))), statement => Effect.try({
|
|
60
|
+
try: () => {
|
|
61
|
+
if (statement.reader) {
|
|
62
|
+
return statement.all(...params);
|
|
63
|
+
}
|
|
64
|
+
statement.run(...params);
|
|
65
|
+
return [];
|
|
66
|
+
},
|
|
67
|
+
catch: error => new SqlError({
|
|
68
|
+
error
|
|
69
|
+
})
|
|
70
|
+
}), statement => Effect.sync(() => statement.raw(false)));
|
|
71
|
+
return identity({
|
|
72
|
+
execute(sql, params) {
|
|
73
|
+
return runTransform(sql, params);
|
|
74
|
+
},
|
|
75
|
+
executeValues(sql, params) {
|
|
76
|
+
return runValues(sql, params);
|
|
77
|
+
},
|
|
78
|
+
executeWithoutTransform(sql, params) {
|
|
79
|
+
return run(sql, params);
|
|
80
|
+
},
|
|
81
|
+
executeRaw(sql, params) {
|
|
82
|
+
return runTransform(sql, params);
|
|
83
|
+
},
|
|
84
|
+
executeStream(_sql, _params) {
|
|
85
|
+
return Effect.dieMessage("executeStream not implemented");
|
|
86
|
+
},
|
|
87
|
+
export: Effect.try({
|
|
88
|
+
try: () => db.serialize(),
|
|
89
|
+
catch: error => new SqlError({
|
|
90
|
+
error
|
|
91
|
+
})
|
|
92
|
+
}),
|
|
93
|
+
loadExtension(path) {
|
|
94
|
+
return Effect.try({
|
|
95
|
+
try: () => db.loadExtension(path),
|
|
96
|
+
catch: error => new SqlError({
|
|
97
|
+
error
|
|
98
|
+
})
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
});
|
|
102
|
+
});
|
|
103
|
+
const semaphore = yield* _(Effect.makeSemaphore(1));
|
|
104
|
+
const connection = yield* _(makeConnection);
|
|
105
|
+
const acquirer = semaphore.withPermits(1)(Effect.succeed(connection));
|
|
106
|
+
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));
|
|
107
|
+
return Object.assign(Client.make({
|
|
108
|
+
acquirer,
|
|
109
|
+
compiler,
|
|
110
|
+
transactionAcquirer
|
|
111
|
+
}), {
|
|
112
|
+
config: options,
|
|
113
|
+
export: Effect.flatMap(acquirer, _ => _.export),
|
|
114
|
+
loadExtension: path => Effect.flatMap(acquirer, _ => _.loadExtension(path))
|
|
115
|
+
});
|
|
116
|
+
});
|
|
117
|
+
/**
|
|
118
|
+
* @category layers
|
|
119
|
+
* @since 1.0.0
|
|
120
|
+
*/
|
|
121
|
+
export const layer = config => Layer.scoped(SqliteClient, Effect.flatMap(Config.unwrap(config), make));
|
|
122
|
+
const escape = /*#__PURE__*/Statement.defaultEscape("\"");
|
|
123
|
+
/**
|
|
124
|
+
* @category compiler
|
|
125
|
+
* @since 1.0.0
|
|
126
|
+
*/
|
|
127
|
+
export const makeCompiler = transform => Statement.makeCompiler({
|
|
128
|
+
placeholder: _ => `?`,
|
|
129
|
+
onIdentifier: transform ? _ => escape(transform(_)) : escape,
|
|
130
|
+
onRecordUpdate: () => ["", []],
|
|
131
|
+
onCustom: () => ["", []]
|
|
132
|
+
});
|
|
133
|
+
//# sourceMappingURL=Client.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Client.js","names":["Client","SqlError","Statement","Sqlite","Cache","Config","Context","Duration","Effect","identity","Layer","Scope","SqliteClient","GenericTag","make","options","gen","_","compiler","makeCompiler","transformQueryNames","transformRows","defaultTransforms","transformResultNames","array","makeConnection","db","filename","readonly","addFinalizer","sync","close","disableWAL","pragma","prepareCache","capacity","prepareCacheSize","timeToLive","prepareCacheTTL","minutes","lookup","sql","try","prepare","catch","error","run","params","flatMap","get","statement","reader","all","runTransform","map","runValues","acquireUseRelease","pipe","raw","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","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,OAAOC,MAAM,MA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,IAAI,CAAC;GACvF,CACF;AACH,CAAC,CAAC;AAEJ;;;;AAIA,OAAO,MAAMmB,KAAK,GAChBD,MAA8C,IAE9C9E,KAAK,CAACgF,MAAM,CACV9E,YAAY,EACZJ,MAAM,CAACwC,OAAO,CAAC3C,MAAM,CAACsF,MAAM,CAACH,MAAM,CAAC,EAAE1E,IAAI,CAAC,CAC5C;AAEH,MAAM8E,MAAM,gBAAG1F,SAAS,CAAC2F,aAAa,CAAC,IAAI,CAAC;AAE5C;;;;AAIA,OAAO,MAAM1E,YAAY,GAAI2E,SAAiC,IAC5D5F,SAAS,CAACiB,YAAY,CAAC;EACrB4E,WAAW,EAAG9E,CAAC,IAAK,GAAG;EACvB+E,YAAY,EAAEF,SAAS,GAAI7E,CAAC,IAAK2E,MAAM,CAACE,SAAS,CAAC7E,CAAC,CAAC,CAAC,GAAG2E,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,E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e,CAACL,IAAI,EAAEvB,IAAI,CAAC,CAAC;IAC1C,CAAC,CAAC,CAACS,IAAI,CACLrB,MAAM,CAACsB,QAAQ,CAAEC,KAAK,IAAK,IAAIxB,QAAQ,CAACyB,cAAc,CAAC;MAAEC,MAAM,EAAE,QAAQ;MAAEC,OAAO,EAAEH,KAAK,CAACG;IAAO,CAAE,CAAC,CAAC,CACtG;IAEH,OAAOQ,QAAQ,CAACvB,IAAI,CAAC;EACvB;CACD,CAAC;AAEF;;;;AAIA,OAAO,MAAM8B,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,51 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@effect/sql-sqlite-node",
|
|
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-node"
|
|
10
|
+
},
|
|
11
|
+
"sideEffects": [],
|
|
12
|
+
"dependencies": {
|
|
13
|
+
"better-sqlite3": "^9.5.0"
|
|
14
|
+
},
|
|
15
|
+
"peerDependencies": {
|
|
16
|
+
"@effect/platform": "^0.0.0-snapshot-189d4cae80e186661241002ad9d729628096520f",
|
|
17
|
+
"@effect/sql": "^0.0.0-snapshot-189d4cae80e186661241002ad9d729628096520f",
|
|
18
|
+
"effect": "^0.0.0-snapshot-189d4cae80e186661241002ad9d729628096520f"
|
|
19
|
+
},
|
|
20
|
+
"main": "./dist/cjs/index.js",
|
|
21
|
+
"module": "./dist/esm/index.js",
|
|
22
|
+
"types": "./dist/dts/index.d.ts",
|
|
23
|
+
"exports": {
|
|
24
|
+
"./package.json": "./package.json",
|
|
25
|
+
".": {
|
|
26
|
+
"types": "./dist/dts/index.d.ts",
|
|
27
|
+
"import": "./dist/esm/index.js",
|
|
28
|
+
"default": "./dist/cjs/index.js"
|
|
29
|
+
},
|
|
30
|
+
"./Client": {
|
|
31
|
+
"types": "./dist/dts/Client.d.ts",
|
|
32
|
+
"import": "./dist/esm/Client.js",
|
|
33
|
+
"default": "./dist/cjs/Client.js"
|
|
34
|
+
},
|
|
35
|
+
"./Migrator": {
|
|
36
|
+
"types": "./dist/dts/Migrator.d.ts",
|
|
37
|
+
"import": "./dist/esm/Migrator.js",
|
|
38
|
+
"default": "./dist/cjs/Migrator.js"
|
|
39
|
+
}
|
|
40
|
+
},
|
|
41
|
+
"typesVersions": {
|
|
42
|
+
"*": {
|
|
43
|
+
"Client": [
|
|
44
|
+
"./dist/dts/Client.d.ts"
|
|
45
|
+
],
|
|
46
|
+
"Migrator": [
|
|
47
|
+
"./dist/dts/Migrator.d.ts"
|
|
48
|
+
]
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
}
|
package/src/Client.ts
ADDED
|
@@ -0,0 +1,218 @@
|
|
|
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 Sqlite from "better-sqlite3"
|
|
9
|
+
import * as Cache from "effect/Cache"
|
|
10
|
+
import * as Config from "effect/Config"
|
|
11
|
+
import type { ConfigError } from "effect/ConfigError"
|
|
12
|
+
import * as Context from "effect/Context"
|
|
13
|
+
import * as Duration from "effect/Duration"
|
|
14
|
+
import * as Effect from "effect/Effect"
|
|
15
|
+
import { identity } from "effect/Function"
|
|
16
|
+
import * as Layer from "effect/Layer"
|
|
17
|
+
import * as Scope from "effect/Scope"
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* @category models
|
|
21
|
+
* @since 1.0.0
|
|
22
|
+
*/
|
|
23
|
+
export interface SqliteClient extends Client.Client {
|
|
24
|
+
readonly config: SqliteClientConfig
|
|
25
|
+
readonly export: Effect.Effect<Uint8Array, SqlError>
|
|
26
|
+
readonly loadExtension: (path: string) => Effect.Effect<void, SqlError>
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* @category tags
|
|
31
|
+
* @since 1.0.0
|
|
32
|
+
*/
|
|
33
|
+
export const SqliteClient: Context.Tag<SqliteClient, SqliteClient> = Context.GenericTag(
|
|
34
|
+
"@effect/sql-sqlite-node/SqliteClient"
|
|
35
|
+
)
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* @category models
|
|
39
|
+
* @since 1.0.0
|
|
40
|
+
*/
|
|
41
|
+
export interface SqliteClientConfig {
|
|
42
|
+
readonly filename: string
|
|
43
|
+
readonly readonly?: boolean | undefined
|
|
44
|
+
readonly prepareCacheSize?: number | undefined
|
|
45
|
+
readonly prepareCacheTTL?: Duration.DurationInput | undefined
|
|
46
|
+
readonly disableWAL?: boolean | undefined
|
|
47
|
+
|
|
48
|
+
readonly transformResultNames?: ((str: string) => string) | undefined
|
|
49
|
+
readonly transformQueryNames?: ((str: string) => string) | undefined
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
interface SqliteConnection extends Connection {
|
|
53
|
+
readonly export: Effect.Effect<Uint8Array, SqlError>
|
|
54
|
+
readonly loadExtension: (path: string) => Effect.Effect<void, SqlError>
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* @category constructor
|
|
59
|
+
* @since 1.0.0
|
|
60
|
+
*/
|
|
61
|
+
export const make = (
|
|
62
|
+
options: SqliteClientConfig
|
|
63
|
+
): Effect.Effect<SqliteClient, never, Scope.Scope> =>
|
|
64
|
+
Effect.gen(function*(_) {
|
|
65
|
+
const compiler = makeCompiler(options.transformQueryNames)
|
|
66
|
+
const transformRows = Client.defaultTransforms(
|
|
67
|
+
options.transformResultNames!
|
|
68
|
+
).array
|
|
69
|
+
|
|
70
|
+
const makeConnection = Effect.gen(function*(_) {
|
|
71
|
+
const db = new Sqlite(options.filename, {
|
|
72
|
+
readonly: options.readonly ?? false
|
|
73
|
+
})
|
|
74
|
+
yield* _(Effect.addFinalizer(() => Effect.sync(() => db.close())))
|
|
75
|
+
|
|
76
|
+
if (options.disableWAL !== true) {
|
|
77
|
+
db.pragma("journal_mode = WAL")
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
const prepareCache = yield* _(
|
|
81
|
+
Cache.make({
|
|
82
|
+
capacity: options.prepareCacheSize ?? 200,
|
|
83
|
+
timeToLive: options.prepareCacheTTL ?? Duration.minutes(10),
|
|
84
|
+
lookup: (sql: string) =>
|
|
85
|
+
Effect.try({
|
|
86
|
+
try: () => db.prepare(sql),
|
|
87
|
+
catch: (error) => new SqlError({ error })
|
|
88
|
+
})
|
|
89
|
+
})
|
|
90
|
+
)
|
|
91
|
+
|
|
92
|
+
const run = (
|
|
93
|
+
sql: string,
|
|
94
|
+
params: ReadonlyArray<Statement.Primitive> = []
|
|
95
|
+
) =>
|
|
96
|
+
Effect.flatMap(prepareCache.get(sql), (statement) =>
|
|
97
|
+
Effect.try({
|
|
98
|
+
try: () => {
|
|
99
|
+
if (statement.reader) {
|
|
100
|
+
return statement.all(...params) as ReadonlyArray<any>
|
|
101
|
+
}
|
|
102
|
+
statement.run(...params)
|
|
103
|
+
return []
|
|
104
|
+
},
|
|
105
|
+
catch: (error) => new SqlError({ error })
|
|
106
|
+
}))
|
|
107
|
+
|
|
108
|
+
const runTransform = options.transformResultNames
|
|
109
|
+
? (sql: string, params?: ReadonlyArray<Statement.Primitive>) => Effect.map(run(sql, params), transformRows)
|
|
110
|
+
: run
|
|
111
|
+
|
|
112
|
+
const runValues = (
|
|
113
|
+
sql: string,
|
|
114
|
+
params: ReadonlyArray<Statement.Primitive>
|
|
115
|
+
) =>
|
|
116
|
+
Effect.acquireUseRelease(
|
|
117
|
+
prepareCache.get(sql).pipe(Effect.map((_) => _.raw(true))),
|
|
118
|
+
(statement) =>
|
|
119
|
+
Effect.try({
|
|
120
|
+
try: () => {
|
|
121
|
+
if (statement.reader) {
|
|
122
|
+
return statement.all(...params) as ReadonlyArray<
|
|
123
|
+
ReadonlyArray<Statement.Primitive>
|
|
124
|
+
>
|
|
125
|
+
}
|
|
126
|
+
statement.run(...params)
|
|
127
|
+
return []
|
|
128
|
+
},
|
|
129
|
+
catch: (error) => new SqlError({ error })
|
|
130
|
+
}),
|
|
131
|
+
(statement) => Effect.sync(() => statement.raw(false))
|
|
132
|
+
)
|
|
133
|
+
|
|
134
|
+
return identity<SqliteConnection>({
|
|
135
|
+
execute(sql, params) {
|
|
136
|
+
return runTransform(sql, params)
|
|
137
|
+
},
|
|
138
|
+
executeValues(sql, params) {
|
|
139
|
+
return runValues(sql, params)
|
|
140
|
+
},
|
|
141
|
+
executeWithoutTransform(sql, params) {
|
|
142
|
+
return run(sql, params)
|
|
143
|
+
},
|
|
144
|
+
executeRaw(sql, params) {
|
|
145
|
+
return runTransform(sql, params)
|
|
146
|
+
},
|
|
147
|
+
executeStream(_sql, _params) {
|
|
148
|
+
return Effect.dieMessage("executeStream not implemented")
|
|
149
|
+
},
|
|
150
|
+
export: Effect.try({
|
|
151
|
+
try: () => db.serialize(),
|
|
152
|
+
catch: (error) => new SqlError({ error })
|
|
153
|
+
}),
|
|
154
|
+
loadExtension(path) {
|
|
155
|
+
return Effect.try({
|
|
156
|
+
try: () => db.loadExtension(path),
|
|
157
|
+
catch: (error) => new SqlError({ error })
|
|
158
|
+
})
|
|
159
|
+
}
|
|
160
|
+
})
|
|
161
|
+
})
|
|
162
|
+
|
|
163
|
+
const semaphore = yield* _(Effect.makeSemaphore(1))
|
|
164
|
+
const connection = yield* _(makeConnection)
|
|
165
|
+
|
|
166
|
+
const acquirer = semaphore.withPermits(1)(Effect.succeed(connection))
|
|
167
|
+
const transactionAcquirer = Effect.uninterruptibleMask((restore) =>
|
|
168
|
+
Effect.as(
|
|
169
|
+
Effect.zipRight(
|
|
170
|
+
restore(semaphore.take(1)),
|
|
171
|
+
Effect.tap(
|
|
172
|
+
Effect.scope,
|
|
173
|
+
(scope) => Scope.addFinalizer(scope, semaphore.release(1))
|
|
174
|
+
)
|
|
175
|
+
),
|
|
176
|
+
connection
|
|
177
|
+
)
|
|
178
|
+
)
|
|
179
|
+
|
|
180
|
+
return Object.assign(
|
|
181
|
+
Client.make({
|
|
182
|
+
acquirer,
|
|
183
|
+
compiler,
|
|
184
|
+
transactionAcquirer
|
|
185
|
+
}),
|
|
186
|
+
{
|
|
187
|
+
config: options,
|
|
188
|
+
export: Effect.flatMap(acquirer, (_) => _.export),
|
|
189
|
+
loadExtension: (path: string) => Effect.flatMap(acquirer, (_) => _.loadExtension(path))
|
|
190
|
+
}
|
|
191
|
+
)
|
|
192
|
+
})
|
|
193
|
+
|
|
194
|
+
/**
|
|
195
|
+
* @category layers
|
|
196
|
+
* @since 1.0.0
|
|
197
|
+
*/
|
|
198
|
+
export const layer = (
|
|
199
|
+
config: Config.Config.Wrap<SqliteClientConfig>
|
|
200
|
+
): Layer.Layer<SqliteClient, ConfigError> =>
|
|
201
|
+
Layer.scoped(
|
|
202
|
+
SqliteClient,
|
|
203
|
+
Effect.flatMap(Config.unwrap(config), make)
|
|
204
|
+
)
|
|
205
|
+
|
|
206
|
+
const escape = Statement.defaultEscape("\"")
|
|
207
|
+
|
|
208
|
+
/**
|
|
209
|
+
* @category compiler
|
|
210
|
+
* @since 1.0.0
|
|
211
|
+
*/
|
|
212
|
+
export const makeCompiler = (transform?: (_: string) => string) =>
|
|
213
|
+
Statement.makeCompiler({
|
|
214
|
+
placeholder: (_) => `?`,
|
|
215
|
+
onIdentifier: transform ? (_) => escape(transform(_)) : escape,
|
|
216
|
+
onRecordUpdate: () => ["", []],
|
|
217
|
+
onCustom: () => ["", []]
|
|
218
|
+
})
|
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"
|