@xleddyl/nuxt-cms 0.1.57 → 0.1.58
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/dist/module.json +1 -1
- package/dist/runtime/seed.js +9 -2
- package/dist/runtime/server/plugins/migrate-libsql.js +2 -2
- package/dist/runtime/server/utils/libsql-migrations.d.ts +16 -0
- package/dist/runtime/server/utils/libsql-migrations.js +35 -0
- package/dist/runtime/server/utils/migrate.d.ts +1 -0
- package/dist/runtime/server/utils/migrate.js +7 -0
- package/package.json +1 -1
package/dist/module.json
CHANGED
package/dist/runtime/seed.js
CHANGED
|
@@ -3,6 +3,7 @@ import { dirname, isAbsolute, resolve } from "node:path";
|
|
|
3
3
|
import { migrationsDirFor } from "./shared/migrations-dir.js";
|
|
4
4
|
import { resolvePageRoutes, routePathsFromDir } from "./shared/page-routes.js";
|
|
5
5
|
import { customId, ID_LENGTH } from "./server/utils/custom-id.js";
|
|
6
|
+
import { applyLibsqlMigrations } from "./server/utils/libsql-migrations.js";
|
|
6
7
|
export { customId, ID_LENGTH };
|
|
7
8
|
export function resolveCmsPages(config, options = {}) {
|
|
8
9
|
const root = options.root ?? process.cwd();
|
|
@@ -27,7 +28,7 @@ export async function createCmsSeeder(opts = {}) {
|
|
|
27
28
|
const resolvedDbPath = isAbsolute(dbPath) ? dbPath : resolve(root, dbPath);
|
|
28
29
|
const migrationsFolder = opts.migrationsDir ?? migrationsDirFor(root, driver);
|
|
29
30
|
if (driver === "libsql") {
|
|
30
|
-
const {
|
|
31
|
+
const { readMigrationFiles } = await import("drizzle-orm/migrator");
|
|
31
32
|
const dbUrl = url || `file:${resolvedDbPath}`;
|
|
32
33
|
let db2;
|
|
33
34
|
if (dbUrl.startsWith("file:")) {
|
|
@@ -40,7 +41,13 @@ export async function createCmsSeeder(opts = {}) {
|
|
|
40
41
|
const { drizzle: drizzle2 } = await import("drizzle-orm/libsql/web");
|
|
41
42
|
db2 = drizzle2(createClient({ url: dbUrl, authToken: authToken || void 0 }));
|
|
42
43
|
}
|
|
43
|
-
|
|
44
|
+
const client = db2.$client;
|
|
45
|
+
return {
|
|
46
|
+
db: db2,
|
|
47
|
+
migrate: async () => {
|
|
48
|
+
await applyLibsqlMigrations(client, readMigrationFiles({ migrationsFolder }));
|
|
49
|
+
}
|
|
50
|
+
};
|
|
44
51
|
}
|
|
45
52
|
if (driver === "postgres") {
|
|
46
53
|
if (!url) throw new Error("[nuxt-cms] seed: Postgres needs a url (NUXT_CMS_DATABASE_URL)");
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { useDb } from "../utils/db-libsql.js";
|
|
2
|
-
import {
|
|
2
|
+
import { runLibsqlMigrations } from "../utils/migrate.js";
|
|
3
3
|
export default async () => {
|
|
4
|
-
await
|
|
4
|
+
await runLibsqlMigrations(useDb());
|
|
5
5
|
};
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export interface LibsqlMigration {
|
|
2
|
+
sql: string[];
|
|
3
|
+
folderMillis: number;
|
|
4
|
+
hash: string;
|
|
5
|
+
}
|
|
6
|
+
export interface LibsqlStatement {
|
|
7
|
+
sql: string;
|
|
8
|
+
args: (string | number)[];
|
|
9
|
+
}
|
|
10
|
+
export interface LibsqlMigrationClient {
|
|
11
|
+
execute: (statement: string) => Promise<{
|
|
12
|
+
rows: ArrayLike<unknown>[];
|
|
13
|
+
}>;
|
|
14
|
+
batch: (statements: LibsqlStatement[], mode: 'write') => Promise<unknown>;
|
|
15
|
+
}
|
|
16
|
+
export declare function applyLibsqlMigrations(client: LibsqlMigrationClient, migrations: LibsqlMigration[]): Promise<number>;
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
const MIGRATIONS_TABLE = "__drizzle_migrations";
|
|
2
|
+
async function lastAppliedMillis(client) {
|
|
3
|
+
const { rows } = await client.execute(
|
|
4
|
+
`SELECT id, hash, created_at FROM "${MIGRATIONS_TABLE}" ORDER BY created_at DESC LIMIT 1`
|
|
5
|
+
);
|
|
6
|
+
const createdAt = rows[0]?.[2];
|
|
7
|
+
return createdAt == null ? null : Number(createdAt);
|
|
8
|
+
}
|
|
9
|
+
export async function applyLibsqlMigrations(client, migrations) {
|
|
10
|
+
if (!migrations.length) return 0;
|
|
11
|
+
await client.execute(
|
|
12
|
+
`CREATE TABLE IF NOT EXISTS "${MIGRATIONS_TABLE}" (id SERIAL PRIMARY KEY, hash text NOT NULL, created_at numeric)`
|
|
13
|
+
);
|
|
14
|
+
const lastApplied = await lastAppliedMillis(client);
|
|
15
|
+
const pending = migrations.filter(
|
|
16
|
+
(migration) => lastApplied == null || lastApplied < migration.folderMillis
|
|
17
|
+
);
|
|
18
|
+
if (!pending.length) return 0;
|
|
19
|
+
const statements = pending.flatMap((migration) => [
|
|
20
|
+
...migration.sql.filter((statement) => statement.trim()).map((sql) => ({ sql, args: [] })),
|
|
21
|
+
{
|
|
22
|
+
sql: `INSERT INTO "${MIGRATIONS_TABLE}" ("hash", "created_at") VALUES (?, ?)`,
|
|
23
|
+
args: [migration.hash, migration.folderMillis]
|
|
24
|
+
}
|
|
25
|
+
]);
|
|
26
|
+
try {
|
|
27
|
+
await client.batch(statements, "write");
|
|
28
|
+
} catch (error) {
|
|
29
|
+
const latest = Math.max(...migrations.map((migration) => migration.folderMillis));
|
|
30
|
+
const appliedNow = await lastAppliedMillis(client).catch(() => null);
|
|
31
|
+
if (appliedNow != null && appliedNow >= latest) return 0;
|
|
32
|
+
throw error;
|
|
33
|
+
}
|
|
34
|
+
return pending.length;
|
|
35
|
+
}
|
|
@@ -5,4 +5,5 @@ export interface CmsMigration {
|
|
|
5
5
|
hash: string;
|
|
6
6
|
}
|
|
7
7
|
export declare function runCmsMigrations(db: unknown): Promise<void>;
|
|
8
|
+
export declare function runLibsqlMigrations(db: unknown): Promise<void>;
|
|
8
9
|
export declare function runD1Migrations(binding: unknown): Promise<void>;
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import cmsConfig from "#cms-config";
|
|
2
2
|
import { migrations as bundledMigrations } from "#cms-migrations";
|
|
3
3
|
import { useRuntimeConfig } from "#imports";
|
|
4
|
+
import { applyLibsqlMigrations } from "./libsql-migrations.js";
|
|
4
5
|
const MIGRATIONS_TABLE = "__drizzle_migrations";
|
|
5
6
|
async function readMigrations(migrationsDir) {
|
|
6
7
|
if (import.meta.dev) {
|
|
@@ -28,6 +29,12 @@ export async function runCmsMigrations(db) {
|
|
|
28
29
|
const { dialect, session } = db;
|
|
29
30
|
await dialect.migrate(migrations, session, {});
|
|
30
31
|
}
|
|
32
|
+
export async function runLibsqlMigrations(db) {
|
|
33
|
+
const migrations = await pendingMigrations();
|
|
34
|
+
if (!migrations.length) return;
|
|
35
|
+
const { $client } = db;
|
|
36
|
+
await applyLibsqlMigrations($client, migrations);
|
|
37
|
+
}
|
|
31
38
|
export async function runD1Migrations(binding) {
|
|
32
39
|
const migrations = await pendingMigrations();
|
|
33
40
|
if (!migrations.length) return;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@xleddyl/nuxt-cms",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.58",
|
|
4
4
|
"description": "Lightweight CMS that ships with your Nuxt app: runs on the Nitro server, content types defined in code, /cms admin panel, GraphQL API, SQLite or Postgres. No external CMS needed!",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "Edoardo Alberti (https://github.com/xleddyl)",
|