alepha 0.13.8 → 0.14.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/dist/api/audits/index.d.ts +2 -1
- package/dist/api/audits/index.d.ts.map +1 -0
- package/dist/api/files/index.d.ts +2 -1
- package/dist/api/files/index.d.ts.map +1 -0
- package/dist/api/jobs/index.d.ts +158 -157
- package/dist/api/jobs/index.d.ts.map +1 -0
- package/dist/api/notifications/index.d.ts.map +1 -0
- package/dist/api/parameters/index.d.ts +4 -4
- package/dist/api/parameters/index.d.ts.map +1 -0
- package/dist/api/users/index.d.ts +132 -131
- package/dist/api/users/index.d.ts.map +1 -0
- package/dist/api/verifications/index.d.ts.map +1 -0
- package/dist/batch/index.d.ts.map +1 -0
- package/dist/bucket/index.d.ts.map +1 -0
- package/dist/cache/core/index.d.ts.map +1 -0
- package/dist/cache/redis/index.d.ts.map +1 -0
- package/dist/cli/index.d.ts +44 -32
- package/dist/cli/index.d.ts.map +1 -0
- package/dist/cli/index.js +380 -109
- package/dist/cli/index.js.map +1 -1
- package/dist/command/index.d.ts +11 -1
- package/dist/command/index.d.ts.map +1 -0
- package/dist/command/index.js +45 -6
- package/dist/command/index.js.map +1 -1
- package/dist/core/index.browser.js +1334 -1318
- package/dist/core/index.browser.js.map +1 -1
- package/dist/core/index.d.ts +75 -71
- package/dist/core/index.d.ts.map +1 -0
- package/dist/core/index.js +1337 -1321
- package/dist/core/index.js.map +1 -1
- package/dist/core/index.native.js +1337 -1321
- package/dist/core/index.native.js.map +1 -1
- package/dist/datetime/index.d.ts.map +1 -0
- package/dist/email/index.d.ts.map +1 -0
- package/dist/fake/index.d.ts.map +1 -0
- package/dist/file/index.d.ts.map +1 -0
- package/dist/lock/core/index.d.ts.map +1 -0
- package/dist/lock/redis/index.d.ts.map +1 -0
- package/dist/logger/index.d.ts +1 -0
- package/dist/logger/index.d.ts.map +1 -0
- package/dist/mcp/index.d.ts +820 -0
- package/dist/mcp/index.d.ts.map +1 -0
- package/dist/mcp/index.js +978 -0
- package/dist/mcp/index.js.map +1 -0
- package/dist/orm/index.d.ts +180 -107
- package/dist/orm/index.d.ts.map +1 -0
- package/dist/orm/index.js +260 -174
- package/dist/orm/index.js.map +1 -1
- package/dist/queue/core/index.d.ts +4 -4
- package/dist/queue/core/index.d.ts.map +1 -0
- package/dist/queue/redis/index.d.ts.map +1 -0
- package/dist/redis/index.d.ts.map +1 -0
- package/dist/retry/index.d.ts.map +1 -0
- package/dist/router/index.d.ts.map +1 -0
- package/dist/scheduler/index.d.ts.map +1 -0
- package/dist/security/index.d.ts.map +1 -0
- package/dist/server/auth/index.d.ts +155 -155
- package/dist/server/auth/index.d.ts.map +1 -0
- package/dist/server/cache/index.d.ts.map +1 -0
- package/dist/server/compress/index.d.ts.map +1 -0
- package/dist/server/cookies/index.d.ts.map +1 -0
- package/dist/server/core/index.d.ts.map +1 -0
- package/dist/server/cors/index.d.ts.map +1 -0
- package/dist/server/health/index.d.ts.map +1 -0
- package/dist/server/helmet/index.d.ts.map +1 -0
- package/dist/server/links/index.d.ts +33 -33
- package/dist/server/links/index.d.ts.map +1 -0
- package/dist/server/metrics/index.d.ts.map +1 -0
- package/dist/server/multipart/index.d.ts.map +1 -0
- package/dist/server/proxy/index.d.ts.map +1 -0
- package/dist/server/rate-limit/index.d.ts.map +1 -0
- package/dist/server/security/index.d.ts +9 -9
- package/dist/server/security/index.d.ts.map +1 -0
- package/dist/server/static/index.d.ts.map +1 -0
- package/dist/server/swagger/index.d.ts.map +1 -0
- package/dist/sms/index.d.ts.map +1 -0
- package/dist/thread/index.d.ts.map +1 -0
- package/dist/topic/core/index.d.ts.map +1 -0
- package/dist/topic/redis/index.d.ts.map +1 -0
- package/dist/vite/index.d.ts +10 -2
- package/dist/vite/index.d.ts.map +1 -0
- package/dist/vite/index.js +36 -14
- package/dist/vite/index.js.map +1 -1
- package/dist/websocket/index.d.ts.map +1 -0
- package/package.json +9 -4
- package/src/cli/apps/AlephaCli.ts +2 -0
- package/src/cli/apps/AlephaPackageBuilderCli.ts +12 -8
- package/src/cli/assets/mainTs.ts +9 -10
- package/src/cli/commands/ChangelogCommands.ts +389 -0
- package/src/cli/commands/DrizzleCommands.ts +204 -4
- package/src/cli/commands/ViteCommands.ts +26 -16
- package/src/cli/services/AlephaCliUtils.ts +23 -150
- package/src/command/providers/CliProvider.ts +76 -5
- package/src/core/providers/SchemaValidator.ts +23 -1
- package/src/mcp/errors/McpError.ts +72 -0
- package/src/mcp/helpers/jsonrpc.ts +163 -0
- package/src/mcp/index.ts +132 -0
- package/src/mcp/interfaces/McpTypes.ts +248 -0
- package/src/mcp/primitives/$prompt.ts +188 -0
- package/src/mcp/primitives/$resource.ts +171 -0
- package/src/mcp/primitives/$tool.ts +285 -0
- package/src/mcp/providers/McpServerProvider.ts +382 -0
- package/src/mcp/transports/SseMcpTransport.ts +172 -0
- package/src/mcp/transports/StdioMcpTransport.ts +126 -0
- package/src/orm/index.ts +12 -0
- package/src/orm/providers/drivers/CloudflareD1Provider.ts +164 -0
- package/src/orm/providers/drivers/NodeSqliteProvider.ts +3 -1
- package/src/vite/plugins/viteAlephaBuild.ts +8 -2
- package/src/vite/plugins/viteAlephaDev.ts +6 -2
- package/src/vite/tasks/buildServer.ts +1 -1
- package/src/vite/tasks/generateCloudflare.ts +43 -15
- package/src/vite/tasks/runAlepha.ts +1 -0
package/dist/orm/index.js
CHANGED
|
@@ -3,20 +3,20 @@ import { $atom, $context, $env, $hook, $inject, $module, $use, Alepha, AlephaErr
|
|
|
3
3
|
import { AlephaDateTime, DateTimeProvider } from "alepha/datetime";
|
|
4
4
|
import * as drizzle from "drizzle-orm";
|
|
5
5
|
import { and, arrayContained, arrayContains, arrayOverlaps, asc, between, desc, eq, getTableName, gt, gte, ilike, inArray, isNotNull, isNull, isSQLWrapper, like, lt, lte, ne, not, notBetween, notIlike, notInArray, notLike, or, sql, sql as sql$1 } from "drizzle-orm";
|
|
6
|
-
import * as pg$
|
|
6
|
+
import * as pg$1 from "drizzle-orm/pg-core";
|
|
7
7
|
import { alias, check, customType, foreignKey, index, pgEnum, pgSchema, pgTable, unique, uniqueIndex } from "drizzle-orm/pg-core";
|
|
8
8
|
import { mkdir, readFile, stat, writeFile } from "node:fs/promises";
|
|
9
9
|
import { $logger } from "alepha/logger";
|
|
10
10
|
import { isSQLWrapper as isSQLWrapper$1 } from "drizzle-orm/sql/sql";
|
|
11
|
+
import { randomUUID } from "node:crypto";
|
|
12
|
+
import * as pg$2 from "drizzle-orm/sqlite-core";
|
|
13
|
+
import { check as check$1, foreignKey as foreignKey$1, index as index$1, sqliteTable, unique as unique$1, uniqueIndex as uniqueIndex$1 } from "drizzle-orm/sqlite-core";
|
|
11
14
|
import { $lock } from "alepha/lock";
|
|
12
15
|
import { drizzle as drizzle$1 } from "drizzle-orm/postgres-js";
|
|
13
16
|
import { migrate } from "drizzle-orm/postgres-js/migrator";
|
|
14
17
|
import postgres from "postgres";
|
|
15
18
|
import { drizzle as drizzle$2 } from "drizzle-orm/sqlite-proxy";
|
|
16
19
|
import { migrate as migrate$1 } from "drizzle-orm/sqlite-proxy/migrator";
|
|
17
|
-
import { randomUUID } from "node:crypto";
|
|
18
|
-
import * as pg$1 from "drizzle-orm/sqlite-core";
|
|
19
|
-
import { check as check$1, foreignKey as foreignKey$1, index as index$1, sqliteTable, unique as unique$1, uniqueIndex as uniqueIndex$1 } from "drizzle-orm/sqlite-core";
|
|
20
20
|
import { migrate as migrate$2 } from "drizzle-orm/pglite/migrator";
|
|
21
21
|
import { $retry } from "alepha/retry";
|
|
22
22
|
|
|
@@ -1690,22 +1690,6 @@ const devMigrationsSchema = t.object({
|
|
|
1690
1690
|
created_at: t.string()
|
|
1691
1691
|
});
|
|
1692
1692
|
|
|
1693
|
-
//#endregion
|
|
1694
|
-
//#region ../../src/orm/errors/DbMigrationError.ts
|
|
1695
|
-
var DbMigrationError = class extends DbError {
|
|
1696
|
-
name = "DbMigrationError";
|
|
1697
|
-
constructor(cause) {
|
|
1698
|
-
super("Failed to migrate database", cause);
|
|
1699
|
-
}
|
|
1700
|
-
};
|
|
1701
|
-
|
|
1702
|
-
//#endregion
|
|
1703
|
-
//#region ../../src/orm/types/byte.ts
|
|
1704
|
-
/**
|
|
1705
|
-
* Postgres bytea type.
|
|
1706
|
-
*/
|
|
1707
|
-
const byte = customType({ dataType: () => "bytea" });
|
|
1708
|
-
|
|
1709
1693
|
//#endregion
|
|
1710
1694
|
//#region ../../src/orm/services/ModelBuilder.ts
|
|
1711
1695
|
/**
|
|
@@ -1798,6 +1782,226 @@ var ModelBuilder = class {
|
|
|
1798
1782
|
}
|
|
1799
1783
|
};
|
|
1800
1784
|
|
|
1785
|
+
//#endregion
|
|
1786
|
+
//#region ../../src/orm/services/SqliteModelBuilder.ts
|
|
1787
|
+
var SqliteModelBuilder = class extends ModelBuilder {
|
|
1788
|
+
buildTable(entity, options) {
|
|
1789
|
+
const tableName = entity.name;
|
|
1790
|
+
if (options.tables.has(tableName)) return;
|
|
1791
|
+
const table = sqliteTable(tableName, this.schemaToSqliteColumns(tableName, entity.schema, options.enums, options.tables), this.getTableConfig(entity, options.tables));
|
|
1792
|
+
options.tables.set(tableName, table);
|
|
1793
|
+
}
|
|
1794
|
+
buildSequence(sequence, options) {
|
|
1795
|
+
throw new AlephaError("SQLite does not support sequences");
|
|
1796
|
+
}
|
|
1797
|
+
/**
|
|
1798
|
+
* Get SQLite-specific config builder for the table.
|
|
1799
|
+
*/
|
|
1800
|
+
getTableConfig(entity, tables) {
|
|
1801
|
+
const sqliteBuilders = {
|
|
1802
|
+
index: index$1,
|
|
1803
|
+
uniqueIndex: uniqueIndex$1,
|
|
1804
|
+
unique: unique$1,
|
|
1805
|
+
check: check$1,
|
|
1806
|
+
foreignKey: foreignKey$1
|
|
1807
|
+
};
|
|
1808
|
+
const tableResolver = (entityName) => {
|
|
1809
|
+
return tables.get(entityName);
|
|
1810
|
+
};
|
|
1811
|
+
return this.buildTableConfig(entity, sqliteBuilders, tableResolver, (config, self) => {
|
|
1812
|
+
const customConfigs = config(self);
|
|
1813
|
+
return Array.isArray(customConfigs) ? customConfigs : [];
|
|
1814
|
+
});
|
|
1815
|
+
}
|
|
1816
|
+
schemaToSqliteColumns = (tableName, schema$1, enums, tables) => {
|
|
1817
|
+
return Object.entries(schema$1.properties).reduce((columns, [key, value]) => {
|
|
1818
|
+
let col = this.mapFieldToSqliteColumn(tableName, key, value, enums);
|
|
1819
|
+
if ("default" in value && value.default != null) col = col.default(value.default);
|
|
1820
|
+
if (PG_PRIMARY_KEY in value) col = col.primaryKey();
|
|
1821
|
+
if (PG_REF in value) {
|
|
1822
|
+
const config = value[PG_REF];
|
|
1823
|
+
col = col.references(() => {
|
|
1824
|
+
const ref = config.ref();
|
|
1825
|
+
const table = tables.get(ref.entity.name);
|
|
1826
|
+
if (!table) throw new AlephaError(`Referenced table ${ref.entity.name} not found for ${tableName}.${key}`);
|
|
1827
|
+
const target = table[ref.name];
|
|
1828
|
+
if (!target) throw new AlephaError(`Referenced column ${ref.name} not found in table ${ref.entity.name} for ${tableName}.${key}`);
|
|
1829
|
+
return target;
|
|
1830
|
+
}, config.actions);
|
|
1831
|
+
}
|
|
1832
|
+
if (schema$1.required?.includes(key)) col = col.notNull();
|
|
1833
|
+
return {
|
|
1834
|
+
...columns,
|
|
1835
|
+
[key]: col
|
|
1836
|
+
};
|
|
1837
|
+
}, {});
|
|
1838
|
+
};
|
|
1839
|
+
mapFieldToSqliteColumn = (tableName, fieldName, value, enums) => {
|
|
1840
|
+
const key = this.toColumnName(fieldName);
|
|
1841
|
+
if ("anyOf" in value && Array.isArray(value.anyOf) && value.anyOf.length === 2 && value.anyOf.some((it) => t.schema.isNull(it))) value = value.anyOf.find((it) => !t.schema.isNull(it));
|
|
1842
|
+
if (t.schema.isInteger(value)) {
|
|
1843
|
+
if (PG_SERIAL in value || PG_IDENTITY in value) return pg$2.integer(key, { mode: "number" }).primaryKey({ autoIncrement: true });
|
|
1844
|
+
return pg$2.integer(key);
|
|
1845
|
+
}
|
|
1846
|
+
if (t.schema.isBigInt(value)) {
|
|
1847
|
+
if (PG_PRIMARY_KEY in value || PG_IDENTITY in value) return pg$2.integer(key, { mode: "number" }).primaryKey({ autoIncrement: true });
|
|
1848
|
+
return pg$2.integer(key, { mode: "number" });
|
|
1849
|
+
}
|
|
1850
|
+
if (t.schema.isNumber(value)) {
|
|
1851
|
+
if (PG_IDENTITY in value) return pg$2.integer(key, { mode: "number" }).primaryKey({ autoIncrement: true });
|
|
1852
|
+
return pg$2.numeric(key);
|
|
1853
|
+
}
|
|
1854
|
+
if (t.schema.isString(value)) return this.mapStringToSqliteColumn(key, value);
|
|
1855
|
+
if (t.schema.isBoolean(value)) return this.sqliteBool(key, value);
|
|
1856
|
+
if (t.schema.isObject(value)) return this.sqliteJson(key, value);
|
|
1857
|
+
if (t.schema.isRecord(value)) return this.sqliteJson(key, value);
|
|
1858
|
+
if (t.schema.isAny(value)) return this.sqliteJson(key, value);
|
|
1859
|
+
if (t.schema.isArray(value)) {
|
|
1860
|
+
if (t.schema.isObject(value.items)) return this.sqliteJson(key, value);
|
|
1861
|
+
if (t.schema.isRecord(value.items)) return this.sqliteJson(key, value);
|
|
1862
|
+
if (t.schema.isAny(value.items)) return this.sqliteJson(key, value);
|
|
1863
|
+
if (t.schema.isString(value.items)) return this.sqliteJson(key, value);
|
|
1864
|
+
if (t.schema.isInteger(value.items)) return this.sqliteJson(key, value);
|
|
1865
|
+
if (t.schema.isNumber(value.items)) return this.sqliteJson(key, value);
|
|
1866
|
+
if (t.schema.isBoolean(value.items)) return this.sqliteJson(key, value);
|
|
1867
|
+
}
|
|
1868
|
+
if (t.schema.isUnsafe(value) && "type" in value && value.type === "string") return this.mapStringToSqliteColumn(key, value);
|
|
1869
|
+
throw new Error(`Unsupported schema for field '${tableName}.${fieldName}' (schema: ${JSON.stringify(value)})`);
|
|
1870
|
+
};
|
|
1871
|
+
mapStringToSqliteColumn = (key, value) => {
|
|
1872
|
+
if (value.format === "uuid") {
|
|
1873
|
+
if (PG_PRIMARY_KEY in value) return pg$2.text(key).primaryKey().$defaultFn(() => randomUUID());
|
|
1874
|
+
return pg$2.text(key);
|
|
1875
|
+
}
|
|
1876
|
+
if (value.format === "byte") return this.sqliteJson(key, value);
|
|
1877
|
+
if (value.format === "date-time") {
|
|
1878
|
+
if (PG_CREATED_AT in value) return this.sqliteDateTime(key, {}).default(sql$1`(unixepoch('subsec') * 1000)`);
|
|
1879
|
+
if (PG_UPDATED_AT in value) return this.sqliteDateTime(key, {}).default(sql$1`(unixepoch('subsec') * 1000)`);
|
|
1880
|
+
return this.sqliteDateTime(key, {});
|
|
1881
|
+
}
|
|
1882
|
+
if (value.format === "date") return this.sqliteDate(key, {});
|
|
1883
|
+
return pg$2.text(key);
|
|
1884
|
+
};
|
|
1885
|
+
sqliteJson = (name, document) => pg$2.customType({
|
|
1886
|
+
dataType: () => "text",
|
|
1887
|
+
toDriver: (value) => JSON.stringify(value),
|
|
1888
|
+
fromDriver: (value) => {
|
|
1889
|
+
return value && typeof value === "string" ? JSON.parse(value) : value;
|
|
1890
|
+
}
|
|
1891
|
+
})(name, { document }).$type();
|
|
1892
|
+
sqliteDateTime = pg$2.customType({
|
|
1893
|
+
dataType: () => "integer",
|
|
1894
|
+
toDriver: (value) => new Date(value).getTime(),
|
|
1895
|
+
fromDriver: (value) => {
|
|
1896
|
+
return new Date(value).toISOString();
|
|
1897
|
+
}
|
|
1898
|
+
});
|
|
1899
|
+
sqliteBool = pg$2.customType({
|
|
1900
|
+
dataType: () => "integer",
|
|
1901
|
+
toDriver: (value) => value ? 1 : 0,
|
|
1902
|
+
fromDriver: (value) => value === 1
|
|
1903
|
+
});
|
|
1904
|
+
sqliteDate = pg$2.customType({
|
|
1905
|
+
dataType: () => "integer",
|
|
1906
|
+
toDriver: (value) => new Date(value).getTime(),
|
|
1907
|
+
fromDriver: (value) => {
|
|
1908
|
+
return new Date(value).toISOString().split("T")[0];
|
|
1909
|
+
}
|
|
1910
|
+
});
|
|
1911
|
+
};
|
|
1912
|
+
|
|
1913
|
+
//#endregion
|
|
1914
|
+
//#region ../../src/orm/providers/drivers/CloudflareD1Provider.ts
|
|
1915
|
+
/**
|
|
1916
|
+
* Cloudflare D1 SQLite provider using Drizzle ORM.
|
|
1917
|
+
*
|
|
1918
|
+
* This provider requires a D1 binding to be set via `cloudflareD1Options` before starting.
|
|
1919
|
+
* The binding is typically obtained from the Cloudflare Workers environment.
|
|
1920
|
+
*
|
|
1921
|
+
* @example
|
|
1922
|
+
* ```ts
|
|
1923
|
+
* // In your Cloudflare Worker
|
|
1924
|
+
* alepha.set(cloudflareD1Options, { binding: env.DB });
|
|
1925
|
+
* ```
|
|
1926
|
+
*/
|
|
1927
|
+
var CloudflareD1Provider = class extends DatabaseProvider {
|
|
1928
|
+
kit = $inject(DrizzleKitProvider);
|
|
1929
|
+
log = $logger();
|
|
1930
|
+
builder = $inject(SqliteModelBuilder);
|
|
1931
|
+
env = $env(t.object({ DATABASE_URL: t.string({ description: "Expect to be 'cloudflare-d1://name:id'" }) }));
|
|
1932
|
+
d1;
|
|
1933
|
+
drizzleDb;
|
|
1934
|
+
get name() {
|
|
1935
|
+
return "d1";
|
|
1936
|
+
}
|
|
1937
|
+
dialect = "sqlite";
|
|
1938
|
+
get url() {
|
|
1939
|
+
return this.env.DATABASE_URL;
|
|
1940
|
+
}
|
|
1941
|
+
get db() {
|
|
1942
|
+
if (!this.drizzleDb) throw new AlephaError("D1 database not initialized");
|
|
1943
|
+
return this.drizzleDb;
|
|
1944
|
+
}
|
|
1945
|
+
async execute(query) {
|
|
1946
|
+
const { rows } = await this.db.run(query);
|
|
1947
|
+
return rows;
|
|
1948
|
+
}
|
|
1949
|
+
onStart = $hook({
|
|
1950
|
+
on: "start",
|
|
1951
|
+
handler: async () => {
|
|
1952
|
+
const [bindingName] = this.env.DATABASE_URL.replace("cloudflare-d1://", "").split(":");
|
|
1953
|
+
const cloudflareEnv = this.alepha.store.get("cloudflare.env");
|
|
1954
|
+
if (!cloudflareEnv) throw new AlephaError("Cloudflare Workers environment not found in Alepha store under 'cloudflare.env'.");
|
|
1955
|
+
const binding = cloudflareEnv[bindingName];
|
|
1956
|
+
if (!binding) throw new AlephaError(`D1 binding '${bindingName}' not found in Cloudflare Workers environment.`);
|
|
1957
|
+
this.d1 = binding;
|
|
1958
|
+
const { drizzle: drizzle$3 } = await import("drizzle-orm/d1");
|
|
1959
|
+
this.drizzleDb = drizzle$3(this.d1);
|
|
1960
|
+
await this.migrateDatabase();
|
|
1961
|
+
this.log.info("Using Cloudflare D1 database");
|
|
1962
|
+
}
|
|
1963
|
+
});
|
|
1964
|
+
async executeMigrations(migrationsFolder) {
|
|
1965
|
+
const { migrate: migrate$3 } = await import("drizzle-orm/d1/migrator");
|
|
1966
|
+
await migrate$3(this.db, { migrationsFolder });
|
|
1967
|
+
}
|
|
1968
|
+
/**
|
|
1969
|
+
* Override development migration to skip sync (not supported on D1).
|
|
1970
|
+
* D1 requires proper migrations to be applied.
|
|
1971
|
+
*/
|
|
1972
|
+
async runDevelopmentMigration(migrationsFolder) {
|
|
1973
|
+
await this.executeMigrations(migrationsFolder);
|
|
1974
|
+
}
|
|
1975
|
+
/**
|
|
1976
|
+
* Override test migration to run migrations instead of sync.
|
|
1977
|
+
* D1 doesn't support schema synchronization.
|
|
1978
|
+
*/
|
|
1979
|
+
async runTestMigration() {
|
|
1980
|
+
const migrationsFolder = this.getMigrationsFolder();
|
|
1981
|
+
try {
|
|
1982
|
+
await this.executeMigrations(migrationsFolder);
|
|
1983
|
+
} catch {
|
|
1984
|
+
this.log.warn("D1 migrations failed in test environment - ensure migrations exist");
|
|
1985
|
+
}
|
|
1986
|
+
}
|
|
1987
|
+
};
|
|
1988
|
+
|
|
1989
|
+
//#endregion
|
|
1990
|
+
//#region ../../src/orm/errors/DbMigrationError.ts
|
|
1991
|
+
var DbMigrationError = class extends DbError {
|
|
1992
|
+
name = "DbMigrationError";
|
|
1993
|
+
constructor(cause) {
|
|
1994
|
+
super("Failed to migrate database", cause);
|
|
1995
|
+
}
|
|
1996
|
+
};
|
|
1997
|
+
|
|
1998
|
+
//#endregion
|
|
1999
|
+
//#region ../../src/orm/types/byte.ts
|
|
2000
|
+
/**
|
|
2001
|
+
* Postgres bytea type.
|
|
2002
|
+
*/
|
|
2003
|
+
const byte = customType({ dataType: () => "bytea" });
|
|
2004
|
+
|
|
1801
2005
|
//#endregion
|
|
1802
2006
|
//#region ../../src/orm/services/PostgresModelBuilder.ts
|
|
1803
2007
|
var PostgresModelBuilder = class extends ModelBuilder {
|
|
@@ -1869,43 +2073,43 @@ var PostgresModelBuilder = class extends ModelBuilder {
|
|
|
1869
2073
|
const key = this.toColumnName(fieldName);
|
|
1870
2074
|
if ("anyOf" in value && Array.isArray(value.anyOf) && value.anyOf.length === 2 && value.anyOf.some((it) => t.schema.isNull(it))) value = value.anyOf.find((it) => !t.schema.isNull(it));
|
|
1871
2075
|
if (t.schema.isInteger(value)) {
|
|
1872
|
-
if (PG_SERIAL in value) return pg$
|
|
2076
|
+
if (PG_SERIAL in value) return pg$1.serial(key);
|
|
1873
2077
|
if (PG_IDENTITY in value) {
|
|
1874
2078
|
const options = value[PG_IDENTITY];
|
|
1875
|
-
if (options.mode === "byDefault") return pg$
|
|
1876
|
-
return pg$
|
|
2079
|
+
if (options.mode === "byDefault") return pg$1.integer().generatedByDefaultAsIdentity(options);
|
|
2080
|
+
return pg$1.integer().generatedAlwaysAsIdentity(options);
|
|
1877
2081
|
}
|
|
1878
|
-
return pg$
|
|
2082
|
+
return pg$1.integer(key);
|
|
1879
2083
|
}
|
|
1880
2084
|
if (t.schema.isBigInt(value)) {
|
|
1881
2085
|
if (PG_IDENTITY in value) {
|
|
1882
2086
|
const options = value[PG_IDENTITY];
|
|
1883
|
-
if (options.mode === "byDefault") return pg$
|
|
1884
|
-
return pg$
|
|
2087
|
+
if (options.mode === "byDefault") return pg$1.bigint({ mode: "bigint" }).generatedByDefaultAsIdentity(options);
|
|
2088
|
+
return pg$1.bigint({ mode: "bigint" }).generatedAlwaysAsIdentity(options);
|
|
1885
2089
|
}
|
|
1886
2090
|
}
|
|
1887
2091
|
if (t.schema.isNumber(value)) {
|
|
1888
2092
|
if (PG_IDENTITY in value) {
|
|
1889
2093
|
const options = value[PG_IDENTITY];
|
|
1890
|
-
if (options.mode === "byDefault") return pg$
|
|
1891
|
-
return pg$
|
|
2094
|
+
if (options.mode === "byDefault") return pg$1.bigint({ mode: "number" }).generatedByDefaultAsIdentity(options);
|
|
2095
|
+
return pg$1.bigint({ mode: "number" }).generatedAlwaysAsIdentity(options);
|
|
1892
2096
|
}
|
|
1893
|
-
if (value.format === "int64") return pg$
|
|
1894
|
-
return pg$
|
|
2097
|
+
if (value.format === "int64") return pg$1.bigint(key, { mode: "number" });
|
|
2098
|
+
return pg$1.numeric(key);
|
|
1895
2099
|
}
|
|
1896
2100
|
if (t.schema.isString(value)) return this.mapStringToColumn(key, value);
|
|
1897
|
-
if (t.schema.isBoolean(value)) return pg$
|
|
2101
|
+
if (t.schema.isBoolean(value)) return pg$1.boolean(key);
|
|
1898
2102
|
if (t.schema.isObject(value)) return schema(key, value);
|
|
1899
2103
|
if (t.schema.isRecord(value)) return schema(key, value);
|
|
1900
2104
|
const isTypeEnum = (value$1) => t.schema.isUnsafe(value$1) && "type" in value$1 && value$1.type === "string" && "enum" in value$1 && Array.isArray(value$1.enum);
|
|
1901
2105
|
if (t.schema.isArray(value)) {
|
|
1902
2106
|
if (t.schema.isObject(value.items)) return schema(key, value);
|
|
1903
2107
|
if (t.schema.isRecord(value.items)) return schema(key, value);
|
|
1904
|
-
if (t.schema.isString(value.items)) return pg$
|
|
1905
|
-
if (t.schema.isInteger(value.items)) return pg$
|
|
1906
|
-
if (t.schema.isNumber(value.items)) return pg$
|
|
1907
|
-
if (t.schema.isBoolean(value.items)) return pg$
|
|
1908
|
-
if (isTypeEnum(value.items)) return pg$
|
|
2108
|
+
if (t.schema.isString(value.items)) return pg$1.text(key).array();
|
|
2109
|
+
if (t.schema.isInteger(value.items)) return pg$1.integer(key).array();
|
|
2110
|
+
if (t.schema.isNumber(value.items)) return pg$1.numeric(key).array();
|
|
2111
|
+
if (t.schema.isBoolean(value.items)) return pg$1.boolean(key).array();
|
|
2112
|
+
if (isTypeEnum(value.items)) return pg$1.text(key).array();
|
|
1909
2113
|
}
|
|
1910
2114
|
if (isTypeEnum(value)) {
|
|
1911
2115
|
if (!value.enum.every((it) => typeof it === "string")) throw new AlephaError(`Enum for ${fieldName} must be an array of strings, got ${JSON.stringify(value.enum)}`);
|
|
@@ -1932,27 +2136,27 @@ var PostgresModelBuilder = class extends ModelBuilder {
|
|
|
1932
2136
|
mapStringToColumn = (key, value) => {
|
|
1933
2137
|
if ("format" in value) {
|
|
1934
2138
|
if (value.format === "uuid") {
|
|
1935
|
-
if (PG_PRIMARY_KEY in value) return pg$
|
|
1936
|
-
return pg$
|
|
2139
|
+
if (PG_PRIMARY_KEY in value) return pg$1.uuid(key).defaultRandom();
|
|
2140
|
+
return pg$1.uuid(key);
|
|
1937
2141
|
}
|
|
1938
2142
|
if (value.format === "byte") return byte(key);
|
|
1939
2143
|
if (value.format === "date-time") {
|
|
1940
|
-
if (PG_CREATED_AT in value) return pg$
|
|
2144
|
+
if (PG_CREATED_AT in value) return pg$1.timestamp(key, {
|
|
1941
2145
|
mode: "string",
|
|
1942
2146
|
withTimezone: true
|
|
1943
2147
|
}).defaultNow();
|
|
1944
|
-
if (PG_UPDATED_AT in value) return pg$
|
|
2148
|
+
if (PG_UPDATED_AT in value) return pg$1.timestamp(key, {
|
|
1945
2149
|
mode: "string",
|
|
1946
2150
|
withTimezone: true
|
|
1947
2151
|
}).defaultNow();
|
|
1948
|
-
return pg$
|
|
2152
|
+
return pg$1.timestamp(key, {
|
|
1949
2153
|
mode: "string",
|
|
1950
2154
|
withTimezone: true
|
|
1951
2155
|
});
|
|
1952
2156
|
}
|
|
1953
|
-
if (value.format === "date") return pg$
|
|
2157
|
+
if (value.format === "date") return pg$1.date(key, { mode: "string" });
|
|
1954
2158
|
}
|
|
1955
|
-
return pg$
|
|
2159
|
+
return pg$1.text(key);
|
|
1956
2160
|
};
|
|
1957
2161
|
};
|
|
1958
2162
|
|
|
@@ -2091,134 +2295,6 @@ var NodePostgresProvider = class NodePostgresProvider extends DatabaseProvider {
|
|
|
2091
2295
|
}
|
|
2092
2296
|
};
|
|
2093
2297
|
|
|
2094
|
-
//#endregion
|
|
2095
|
-
//#region ../../src/orm/services/SqliteModelBuilder.ts
|
|
2096
|
-
var SqliteModelBuilder = class extends ModelBuilder {
|
|
2097
|
-
buildTable(entity, options) {
|
|
2098
|
-
const tableName = entity.name;
|
|
2099
|
-
if (options.tables.has(tableName)) return;
|
|
2100
|
-
const table = sqliteTable(tableName, this.schemaToSqliteColumns(tableName, entity.schema, options.enums, options.tables), this.getTableConfig(entity, options.tables));
|
|
2101
|
-
options.tables.set(tableName, table);
|
|
2102
|
-
}
|
|
2103
|
-
buildSequence(sequence, options) {
|
|
2104
|
-
throw new AlephaError("SQLite does not support sequences");
|
|
2105
|
-
}
|
|
2106
|
-
/**
|
|
2107
|
-
* Get SQLite-specific config builder for the table.
|
|
2108
|
-
*/
|
|
2109
|
-
getTableConfig(entity, tables) {
|
|
2110
|
-
const sqliteBuilders = {
|
|
2111
|
-
index: index$1,
|
|
2112
|
-
uniqueIndex: uniqueIndex$1,
|
|
2113
|
-
unique: unique$1,
|
|
2114
|
-
check: check$1,
|
|
2115
|
-
foreignKey: foreignKey$1
|
|
2116
|
-
};
|
|
2117
|
-
const tableResolver = (entityName) => {
|
|
2118
|
-
return tables.get(entityName);
|
|
2119
|
-
};
|
|
2120
|
-
return this.buildTableConfig(entity, sqliteBuilders, tableResolver, (config, self) => {
|
|
2121
|
-
const customConfigs = config(self);
|
|
2122
|
-
return Array.isArray(customConfigs) ? customConfigs : [];
|
|
2123
|
-
});
|
|
2124
|
-
}
|
|
2125
|
-
schemaToSqliteColumns = (tableName, schema$1, enums, tables) => {
|
|
2126
|
-
return Object.entries(schema$1.properties).reduce((columns, [key, value]) => {
|
|
2127
|
-
let col = this.mapFieldToSqliteColumn(tableName, key, value, enums);
|
|
2128
|
-
if ("default" in value && value.default != null) col = col.default(value.default);
|
|
2129
|
-
if (PG_PRIMARY_KEY in value) col = col.primaryKey();
|
|
2130
|
-
if (PG_REF in value) {
|
|
2131
|
-
const config = value[PG_REF];
|
|
2132
|
-
col = col.references(() => {
|
|
2133
|
-
const ref = config.ref();
|
|
2134
|
-
const table = tables.get(ref.entity.name);
|
|
2135
|
-
if (!table) throw new AlephaError(`Referenced table ${ref.entity.name} not found for ${tableName}.${key}`);
|
|
2136
|
-
const target = table[ref.name];
|
|
2137
|
-
if (!target) throw new AlephaError(`Referenced column ${ref.name} not found in table ${ref.entity.name} for ${tableName}.${key}`);
|
|
2138
|
-
return target;
|
|
2139
|
-
}, config.actions);
|
|
2140
|
-
}
|
|
2141
|
-
if (schema$1.required?.includes(key)) col = col.notNull();
|
|
2142
|
-
return {
|
|
2143
|
-
...columns,
|
|
2144
|
-
[key]: col
|
|
2145
|
-
};
|
|
2146
|
-
}, {});
|
|
2147
|
-
};
|
|
2148
|
-
mapFieldToSqliteColumn = (tableName, fieldName, value, enums) => {
|
|
2149
|
-
const key = this.toColumnName(fieldName);
|
|
2150
|
-
if ("anyOf" in value && Array.isArray(value.anyOf) && value.anyOf.length === 2 && value.anyOf.some((it) => t.schema.isNull(it))) value = value.anyOf.find((it) => !t.schema.isNull(it));
|
|
2151
|
-
if (t.schema.isInteger(value)) {
|
|
2152
|
-
if (PG_SERIAL in value || PG_IDENTITY in value) return pg$1.integer(key, { mode: "number" }).primaryKey({ autoIncrement: true });
|
|
2153
|
-
return pg$1.integer(key);
|
|
2154
|
-
}
|
|
2155
|
-
if (t.schema.isBigInt(value)) {
|
|
2156
|
-
if (PG_PRIMARY_KEY in value || PG_IDENTITY in value) return pg$1.integer(key, { mode: "number" }).primaryKey({ autoIncrement: true });
|
|
2157
|
-
return pg$1.integer(key, { mode: "number" });
|
|
2158
|
-
}
|
|
2159
|
-
if (t.schema.isNumber(value)) {
|
|
2160
|
-
if (PG_IDENTITY in value) return pg$1.integer(key, { mode: "number" }).primaryKey({ autoIncrement: true });
|
|
2161
|
-
return pg$1.numeric(key);
|
|
2162
|
-
}
|
|
2163
|
-
if (t.schema.isString(value)) return this.mapStringToSqliteColumn(key, value);
|
|
2164
|
-
if (t.schema.isBoolean(value)) return this.sqliteBool(key, value);
|
|
2165
|
-
if (t.schema.isObject(value)) return this.sqliteJson(key, value);
|
|
2166
|
-
if (t.schema.isRecord(value)) return this.sqliteJson(key, value);
|
|
2167
|
-
if (t.schema.isAny(value)) return this.sqliteJson(key, value);
|
|
2168
|
-
if (t.schema.isArray(value)) {
|
|
2169
|
-
if (t.schema.isObject(value.items)) return this.sqliteJson(key, value);
|
|
2170
|
-
if (t.schema.isRecord(value.items)) return this.sqliteJson(key, value);
|
|
2171
|
-
if (t.schema.isAny(value.items)) return this.sqliteJson(key, value);
|
|
2172
|
-
if (t.schema.isString(value.items)) return this.sqliteJson(key, value);
|
|
2173
|
-
if (t.schema.isInteger(value.items)) return this.sqliteJson(key, value);
|
|
2174
|
-
if (t.schema.isNumber(value.items)) return this.sqliteJson(key, value);
|
|
2175
|
-
if (t.schema.isBoolean(value.items)) return this.sqliteJson(key, value);
|
|
2176
|
-
}
|
|
2177
|
-
if (t.schema.isUnsafe(value) && "type" in value && value.type === "string") return this.mapStringToSqliteColumn(key, value);
|
|
2178
|
-
throw new Error(`Unsupported schema for field '${tableName}.${fieldName}' (schema: ${JSON.stringify(value)})`);
|
|
2179
|
-
};
|
|
2180
|
-
mapStringToSqliteColumn = (key, value) => {
|
|
2181
|
-
if (value.format === "uuid") {
|
|
2182
|
-
if (PG_PRIMARY_KEY in value) return pg$1.text(key).primaryKey().$defaultFn(() => randomUUID());
|
|
2183
|
-
return pg$1.text(key);
|
|
2184
|
-
}
|
|
2185
|
-
if (value.format === "byte") return this.sqliteJson(key, value);
|
|
2186
|
-
if (value.format === "date-time") {
|
|
2187
|
-
if (PG_CREATED_AT in value) return this.sqliteDateTime(key, {}).default(sql$1`(unixepoch('subsec') * 1000)`);
|
|
2188
|
-
if (PG_UPDATED_AT in value) return this.sqliteDateTime(key, {}).default(sql$1`(unixepoch('subsec') * 1000)`);
|
|
2189
|
-
return this.sqliteDateTime(key, {});
|
|
2190
|
-
}
|
|
2191
|
-
if (value.format === "date") return this.sqliteDate(key, {});
|
|
2192
|
-
return pg$1.text(key);
|
|
2193
|
-
};
|
|
2194
|
-
sqliteJson = (name, document) => pg$1.customType({
|
|
2195
|
-
dataType: () => "text",
|
|
2196
|
-
toDriver: (value) => JSON.stringify(value),
|
|
2197
|
-
fromDriver: (value) => {
|
|
2198
|
-
return value && typeof value === "string" ? JSON.parse(value) : value;
|
|
2199
|
-
}
|
|
2200
|
-
})(name, { document }).$type();
|
|
2201
|
-
sqliteDateTime = pg$1.customType({
|
|
2202
|
-
dataType: () => "integer",
|
|
2203
|
-
toDriver: (value) => new Date(value).getTime(),
|
|
2204
|
-
fromDriver: (value) => {
|
|
2205
|
-
return new Date(value).toISOString();
|
|
2206
|
-
}
|
|
2207
|
-
});
|
|
2208
|
-
sqliteBool = pg$1.customType({
|
|
2209
|
-
dataType: () => "integer",
|
|
2210
|
-
toDriver: (value) => value ? 1 : 0,
|
|
2211
|
-
fromDriver: (value) => value === 1
|
|
2212
|
-
});
|
|
2213
|
-
sqliteDate = pg$1.customType({
|
|
2214
|
-
dataType: () => "integer",
|
|
2215
|
-
toDriver: (value) => new Date(value).getTime(),
|
|
2216
|
-
fromDriver: (value) => {
|
|
2217
|
-
return new Date(value).toISOString().split("T")[0];
|
|
2218
|
-
}
|
|
2219
|
-
});
|
|
2220
|
-
};
|
|
2221
|
-
|
|
2222
2298
|
//#endregion
|
|
2223
2299
|
//#region ../../src/orm/providers/drivers/NodeSqliteProvider.ts
|
|
2224
2300
|
const envSchema$1 = t.object({ DATABASE_URL: t.optional(t.text()) });
|
|
@@ -2289,7 +2365,7 @@ var NodeSqliteProvider = class extends DatabaseProvider {
|
|
|
2289
2365
|
on: "start",
|
|
2290
2366
|
handler: async () => {
|
|
2291
2367
|
const { DatabaseSync } = await import("node:sqlite");
|
|
2292
|
-
const filepath = this.url.replace("sqlite://", "");
|
|
2368
|
+
const filepath = this.url.replace("sqlite://", "").replace("sqlite:", "");
|
|
2293
2369
|
if (filepath !== ":memory:" && filepath !== "") {
|
|
2294
2370
|
const dirname = filepath.split("/").slice(0, -1).join("/");
|
|
2295
2371
|
if (dirname) await mkdir(dirname, { recursive: true }).catch(() => null);
|
|
@@ -2861,6 +2937,7 @@ const AlephaPostgres = $module({
|
|
|
2861
2937
|
NodePostgresProvider,
|
|
2862
2938
|
PglitePostgresProvider,
|
|
2863
2939
|
NodeSqliteProvider,
|
|
2940
|
+
CloudflareD1Provider,
|
|
2864
2941
|
SqliteModelBuilder,
|
|
2865
2942
|
PostgresModelBuilder,
|
|
2866
2943
|
DrizzleKitProvider,
|
|
@@ -2879,7 +2956,16 @@ const AlephaPostgres = $module({
|
|
|
2879
2956
|
const isPostgres = url?.startsWith("postgres:");
|
|
2880
2957
|
const isSqlite = url?.startsWith("sqlite:");
|
|
2881
2958
|
const isMemory = url?.includes(":memory:");
|
|
2882
|
-
|
|
2959
|
+
const isFile = !!url && !isPostgres && !isMemory;
|
|
2960
|
+
if (url?.startsWith("cloudflare-d1:")) {
|
|
2961
|
+
alepha.with({
|
|
2962
|
+
optional: true,
|
|
2963
|
+
provide: DatabaseProvider,
|
|
2964
|
+
use: CloudflareD1Provider
|
|
2965
|
+
});
|
|
2966
|
+
return;
|
|
2967
|
+
}
|
|
2968
|
+
if (hasPGlite && (isMemory || isFile || !url) && !isSqlite) {
|
|
2883
2969
|
alepha.with({
|
|
2884
2970
|
optional: true,
|
|
2885
2971
|
provide: DatabaseProvider,
|
|
@@ -2904,5 +2990,5 @@ const AlephaPostgres = $module({
|
|
|
2904
2990
|
});
|
|
2905
2991
|
|
|
2906
2992
|
//#endregion
|
|
2907
|
-
export { $entity, $repository, $sequence, $transaction, AlephaPostgres, DatabaseProvider, DbConflictError, DbEntityNotFoundError, DbError, DbMigrationError, DbVersionMismatchError, DrizzleKitProvider, EntityPrimitive, NodePostgresProvider, NodeSqliteProvider, PG_CREATED_AT, PG_DEFAULT, PG_DELETED_AT, PG_ENUM, PG_IDENTITY, PG_PRIMARY_KEY, PG_REF, PG_SERIAL, PG_UPDATED_AT, PG_VERSION, PostgresTypeProvider, Repository, RepositoryProvider, SequencePrimitive, buildQueryString, drizzle, getAttrFields, insertSchema, legacyIdSchema, nodeSqliteOptions, pageQuerySchema, pageSchema, parseQueryString, pg, pgAttr, schema, sql, updateSchema };
|
|
2993
|
+
export { $entity, $repository, $sequence, $transaction, AlephaPostgres, CloudflareD1Provider, DatabaseProvider, DbConflictError, DbEntityNotFoundError, DbError, DbMigrationError, DbVersionMismatchError, DrizzleKitProvider, EntityPrimitive, NodePostgresProvider, NodeSqliteProvider, PG_CREATED_AT, PG_DEFAULT, PG_DELETED_AT, PG_ENUM, PG_IDENTITY, PG_PRIMARY_KEY, PG_REF, PG_SERIAL, PG_UPDATED_AT, PG_VERSION, PostgresTypeProvider, Repository, RepositoryProvider, SequencePrimitive, buildQueryString, drizzle, getAttrFields, insertSchema, legacyIdSchema, nodeSqliteOptions, pageQuerySchema, pageSchema, parseQueryString, pg, pgAttr, schema, sql, updateSchema };
|
|
2908
2994
|
//# sourceMappingURL=index.js.map
|