@easy-cms/db-sqlite 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +21 -0
- package/dist/index.d.ts +20 -0
- package/dist/index.js +109 -0
- package/package.json +54 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Kanawoot K.
|
|
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,21 @@
|
|
|
1
|
+
# @easy-cms/db-sqlite
|
|
2
|
+
|
|
3
|
+
SQLite / libSQL (Turso) adapter for [Easy CMS](https://github.com/maritonx/easy-crm).
|
|
4
|
+
|
|
5
|
+
```ts
|
|
6
|
+
import { sqlite } from '@easy-cms/db-sqlite'
|
|
7
|
+
|
|
8
|
+
export default defineConfig({
|
|
9
|
+
db: sqlite({ url: 'file:./cms.db' }),
|
|
10
|
+
// ...
|
|
11
|
+
})
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
| Option | Default | |
|
|
15
|
+
|---|---|---|
|
|
16
|
+
| `url` | — | `file:./cms.db` (relative to the project root) or `libsql://…` |
|
|
17
|
+
| `authToken` | — | For Turso / remote libSQL |
|
|
18
|
+
| `tablePrefix` | `ecms_` | Prefix of every table Easy CMS creates |
|
|
19
|
+
| `migrationDir` | `easy-cms/migrations` | Where migration files are written |
|
|
20
|
+
|
|
21
|
+
In-memory databases (`:memory:`) are not supported because transactions open a second connection.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { DatabaseAdapter } from '@easy-cms/core';
|
|
2
|
+
import { DrizzleAdapterOptions, Dialect } from '@easy-cms/drizzle';
|
|
3
|
+
export { DEFAULT_MIGRATION_DIR, DEFAULT_TABLE_PREFIX } from '@easy-cms/drizzle';
|
|
4
|
+
|
|
5
|
+
interface SQLiteAdapterOptions extends DrizzleAdapterOptions {
|
|
6
|
+
/** `file:./cms.db` (relative to the project root) or a `libsql://` URL. */
|
|
7
|
+
readonly url: string;
|
|
8
|
+
/** Auth token for Turso / remote libSQL. */
|
|
9
|
+
readonly authToken?: string;
|
|
10
|
+
}
|
|
11
|
+
declare const sqliteDialect: Dialect;
|
|
12
|
+
/** SQLite / libSQL database adapter. */
|
|
13
|
+
declare function sqlite(options: SQLiteAdapterOptions): DatabaseAdapter;
|
|
14
|
+
/**
|
|
15
|
+
* libsql loads its prebuilt binary with a computed `require()`, which output
|
|
16
|
+
* tracing cannot follow. Returns the binaries installed for this machine.
|
|
17
|
+
*/
|
|
18
|
+
declare function nativeBinaries(): string[];
|
|
19
|
+
|
|
20
|
+
export { type SQLiteAdapterOptions, nativeBinaries, sqlite, sqliteDialect };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
// src/index.ts
|
|
2
|
+
import { readFileSync } from "fs";
|
|
3
|
+
import { createRequire } from "module";
|
|
4
|
+
import { dirname, isAbsolute, join, resolve } from "path";
|
|
5
|
+
import { fileURLToPath } from "url";
|
|
6
|
+
import {
|
|
7
|
+
connectDatabase
|
|
8
|
+
} from "@easy-cms/drizzle";
|
|
9
|
+
import { createClient } from "@libsql/client";
|
|
10
|
+
import { sql } from "drizzle-orm";
|
|
11
|
+
import { drizzle } from "drizzle-orm/libsql";
|
|
12
|
+
import { index, integer, real, sqliteTable, text, uniqueIndex } from "drizzle-orm/sqlite-core";
|
|
13
|
+
import { DEFAULT_MIGRATION_DIR, DEFAULT_TABLE_PREFIX } from "@easy-cms/drizzle";
|
|
14
|
+
var kit = () => import("drizzle-kit/api");
|
|
15
|
+
var sqliteDialect = {
|
|
16
|
+
name: "sqlite",
|
|
17
|
+
table: (name, columns, indexes) => sqliteTable(name, columns, indexes),
|
|
18
|
+
index: (name) => index(name),
|
|
19
|
+
uniqueIndex: (name) => uniqueIndex(name),
|
|
20
|
+
serial: (name) => integer(name).primaryKey({ autoIncrement: true }),
|
|
21
|
+
text: (name) => text(name),
|
|
22
|
+
integer: (name) => integer(name),
|
|
23
|
+
number: (name) => real(name),
|
|
24
|
+
boolean: (name) => integer(name, { mode: "boolean" }),
|
|
25
|
+
json: (name) => text(name, { mode: "json" }),
|
|
26
|
+
// LIKE is case-insensitive for ASCII in SQLite.
|
|
27
|
+
like: (column, pattern) => sql`${column} LIKE ${pattern} ESCAPE '\\'`,
|
|
28
|
+
param: () => "?",
|
|
29
|
+
migrationsTableSQL: (name) => `CREATE TABLE IF NOT EXISTS \`${name}\` (
|
|
30
|
+
\`id\` integer PRIMARY KEY AUTOINCREMENT NOT NULL,
|
|
31
|
+
\`name\` text NOT NULL UNIQUE,
|
|
32
|
+
\`hash\` text,
|
|
33
|
+
\`snapshot\` text,
|
|
34
|
+
\`applied_at\` text NOT NULL
|
|
35
|
+
)`,
|
|
36
|
+
snapshot: async (tables) => (await kit()).generateSQLiteDrizzleJson({ ...tables }),
|
|
37
|
+
migration: async (prev, cur) => (await kit()).generateSQLiteMigration(prev, cur)
|
|
38
|
+
};
|
|
39
|
+
function sqlite(options) {
|
|
40
|
+
let traceInclude;
|
|
41
|
+
return {
|
|
42
|
+
name: "sqlite",
|
|
43
|
+
init: async (args) => {
|
|
44
|
+
const client = createClient({
|
|
45
|
+
url: resolveUrl(options.url, args.cwd),
|
|
46
|
+
...options.authToken ? { authToken: options.authToken } : {}
|
|
47
|
+
});
|
|
48
|
+
if (options.url.startsWith("file:")) await client.execute("PRAGMA journal_mode = WAL");
|
|
49
|
+
const toStatement = (s) => ({ sql: s.sql, args: [...s.params ?? []] });
|
|
50
|
+
const connection = {
|
|
51
|
+
db: drizzle(client),
|
|
52
|
+
runner: {
|
|
53
|
+
async query(text2, params = []) {
|
|
54
|
+
const result = await client.execute({ sql: text2, args: [...params] });
|
|
55
|
+
return result.rows;
|
|
56
|
+
},
|
|
57
|
+
async transaction(statements) {
|
|
58
|
+
await client.batch(statements.map(toStatement), "write");
|
|
59
|
+
}
|
|
60
|
+
},
|
|
61
|
+
close: async () => client.close()
|
|
62
|
+
};
|
|
63
|
+
return connectDatabase(sqliteDialect, connection, args, options);
|
|
64
|
+
},
|
|
65
|
+
bundle: {
|
|
66
|
+
get traceInclude() {
|
|
67
|
+
traceInclude ??= nativeBinaries();
|
|
68
|
+
return traceInclude;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
function resolveUrl(url, cwd) {
|
|
74
|
+
if (url === ":memory:" || url === "file::memory:") {
|
|
75
|
+
throw new Error(
|
|
76
|
+
"@easy-cms/db-sqlite: in-memory databases are not supported; use a file URL such as file:./cms.db"
|
|
77
|
+
);
|
|
78
|
+
}
|
|
79
|
+
if (!url.startsWith("file:")) return url;
|
|
80
|
+
const path = url.slice("file:".length);
|
|
81
|
+
return isAbsolute(path) ? url : `file:${resolve(cwd, path)}`;
|
|
82
|
+
}
|
|
83
|
+
function nativeBinaries() {
|
|
84
|
+
try {
|
|
85
|
+
const client = fileURLToPath(import.meta.resolve("@libsql/client"));
|
|
86
|
+
const libsqlMain = createRequire(client).resolve("libsql");
|
|
87
|
+
const libsqlPkg = JSON.parse(
|
|
88
|
+
readFileSync(join(dirname(libsqlMain), "package.json"), "utf8")
|
|
89
|
+
);
|
|
90
|
+
const requireFromLibsql = createRequire(libsqlMain);
|
|
91
|
+
const found = [];
|
|
92
|
+
for (const name of Object.keys(libsqlPkg.optionalDependencies ?? {})) {
|
|
93
|
+
try {
|
|
94
|
+
found.push(requireFromLibsql.resolve(name));
|
|
95
|
+
} catch {
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
return found;
|
|
99
|
+
} catch {
|
|
100
|
+
return [];
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
export {
|
|
104
|
+
DEFAULT_MIGRATION_DIR,
|
|
105
|
+
DEFAULT_TABLE_PREFIX,
|
|
106
|
+
nativeBinaries,
|
|
107
|
+
sqlite,
|
|
108
|
+
sqliteDialect
|
|
109
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@easy-cms/db-sqlite",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "SQLite / libSQL database adapter for Easy CMS",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"sideEffects": false,
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"default": "./dist/index.js"
|
|
12
|
+
},
|
|
13
|
+
"./package.json": "./package.json"
|
|
14
|
+
},
|
|
15
|
+
"files": [
|
|
16
|
+
"dist"
|
|
17
|
+
],
|
|
18
|
+
"engines": {
|
|
19
|
+
"node": ">=22.12"
|
|
20
|
+
},
|
|
21
|
+
"dependencies": {
|
|
22
|
+
"@libsql/client": "^0.18.0",
|
|
23
|
+
"drizzle-kit": "0.31.11",
|
|
24
|
+
"drizzle-orm": "0.45.3",
|
|
25
|
+
"@easy-cms/drizzle": "^0.1.0"
|
|
26
|
+
},
|
|
27
|
+
"peerDependencies": {
|
|
28
|
+
"@easy-cms/core": "^0.1.0"
|
|
29
|
+
},
|
|
30
|
+
"devDependencies": {
|
|
31
|
+
"sharp": "^0.35.4",
|
|
32
|
+
"@easy-cms/core": "^0.1.0"
|
|
33
|
+
},
|
|
34
|
+
"publishConfig": {
|
|
35
|
+
"access": "public"
|
|
36
|
+
},
|
|
37
|
+
"repository": {
|
|
38
|
+
"type": "git",
|
|
39
|
+
"url": "git+https://github.com/maritonx/easy-crm.git",
|
|
40
|
+
"directory": "packages/db-sqlite"
|
|
41
|
+
},
|
|
42
|
+
"keywords": [
|
|
43
|
+
"easy-cms",
|
|
44
|
+
"sqlite",
|
|
45
|
+
"libsql",
|
|
46
|
+
"turso",
|
|
47
|
+
"drizzle"
|
|
48
|
+
],
|
|
49
|
+
"scripts": {
|
|
50
|
+
"build": "tsup",
|
|
51
|
+
"typecheck": "tsc --noEmit",
|
|
52
|
+
"test": "vitest run"
|
|
53
|
+
}
|
|
54
|
+
}
|