@stacksjs/database 0.70.258 → 0.70.260
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/auth-tables.js +18 -137
- package/dist/column.js +1 -26
- package/dist/custom/audits.js +20 -54
- package/dist/custom/errors.js +16 -46
- package/dist/custom/index.js +1 -3
- package/dist/custom/jobs.js +13 -137
- package/dist/database.js +1 -181
- package/dist/datetime-columns.js +2 -79
- package/dist/ddl-constraints.js +7 -111
- package/dist/defaults.js +1 -48
- package/dist/dialect.js +1 -79
- package/dist/driver-config.js +1 -172
- package/dist/drivers/defaults/index.js +1 -1
- package/dist/drivers/defaults/traits.js +1 -29
- package/dist/drivers/dynamodb.js +1 -607
- package/dist/drivers/helpers.js +1 -206
- package/dist/drivers/index.js +1 -9
- package/dist/drivers/mysql.js +58 -299
- package/dist/drivers/postgres.js +78 -368
- package/dist/drivers/sqlite.js +61 -379
- package/dist/ensure-database.js +1 -145
- package/dist/fk-audit.js +3 -187
- package/dist/index.js +1 -64
- package/dist/managed-columns.js +1 -59
- package/dist/migration-dialect.js +4 -107
- package/dist/migration-ledger.js +1 -382
- package/dist/migration-lock.js +1 -143
- package/dist/migrations.js +15 -1118
- package/dist/model-sources.js +1 -76
- package/dist/notification-tables.js +4 -49
- package/dist/query-logger.js +2 -241
- package/dist/query-parser.js +1 -93
- package/dist/rbac-tables.js +6 -61
- package/dist/relation-columns.js +1 -66
- package/dist/replicas.js +1 -74
- package/dist/safe-migrations.js +2 -52
- package/dist/schema.js +1 -10
- package/dist/seeder.js +1 -457
- package/dist/sql-helpers.js +1 -50
- package/dist/table.js +1 -26
- package/dist/tools/setup.js +1 -6
- package/dist/trait-tables.js +8 -153
- package/dist/transaction-context.js +1 -62
- package/dist/types.js +1 -98
- package/dist/unique-audit.js +3 -155
- package/dist/utils.js +1 -285
- package/dist/uuid-columns.js +1 -68
- package/dist/validators.js +1 -122
- package/dist/vschema.js +2 -121
- package/package.json +20 -13
package/dist/driver-config.js
CHANGED
|
@@ -1,172 +1 @@
|
|
|
1
|
-
import {
|
|
2
|
-
export const driverDefaults = {
|
|
3
|
-
sqlite: {
|
|
4
|
-
database: "database/stacks.sqlite",
|
|
5
|
-
prefix: ""
|
|
6
|
-
},
|
|
7
|
-
mysql: {
|
|
8
|
-
name: "stacks",
|
|
9
|
-
host: "127.0.0.1",
|
|
10
|
-
port: 3306,
|
|
11
|
-
username: "root",
|
|
12
|
-
password: "",
|
|
13
|
-
prefix: "",
|
|
14
|
-
charset: "utf8mb4",
|
|
15
|
-
collation: "utf8mb4_unicode_ci"
|
|
16
|
-
},
|
|
17
|
-
singlestore: {
|
|
18
|
-
name: "stacks",
|
|
19
|
-
host: "127.0.0.1",
|
|
20
|
-
port: 3306,
|
|
21
|
-
username: "root",
|
|
22
|
-
password: "",
|
|
23
|
-
prefix: "",
|
|
24
|
-
charset: "utf8mb4",
|
|
25
|
-
ssl: !1
|
|
26
|
-
},
|
|
27
|
-
vitess: {
|
|
28
|
-
name: "stacks",
|
|
29
|
-
host: "127.0.0.1",
|
|
30
|
-
port: 15306,
|
|
31
|
-
username: "root",
|
|
32
|
-
password: "",
|
|
33
|
-
prefix: "",
|
|
34
|
-
ssl: !1
|
|
35
|
-
},
|
|
36
|
-
postgres: {
|
|
37
|
-
name: "stacks",
|
|
38
|
-
host: "127.0.0.1",
|
|
39
|
-
port: 5432,
|
|
40
|
-
username: "postgres",
|
|
41
|
-
password: "",
|
|
42
|
-
prefix: "",
|
|
43
|
-
schema: "public"
|
|
44
|
-
},
|
|
45
|
-
browser: {}
|
|
46
|
-
};
|
|
47
|
-
export function getConnectionString(driver, config) {
|
|
48
|
-
switch (driver) {
|
|
49
|
-
case "sqlite": {
|
|
50
|
-
const sqliteConfig = config;
|
|
51
|
-
if (sqliteConfig.database === ":memory:")
|
|
52
|
-
return ":memory:";
|
|
53
|
-
return `sqlite://${sqliteConfig.database}`;
|
|
54
|
-
}
|
|
55
|
-
case "mysql": {
|
|
56
|
-
const mysqlConfig = config, { name, host = "127.0.0.1", port = 3306, username = "root", password = "" } = mysqlConfig;
|
|
57
|
-
return `mysql://${username}:${password}@${host}:${port}/${name}`;
|
|
58
|
-
}
|
|
59
|
-
case "singlestore": {
|
|
60
|
-
const ssConfig = config, { name, host = "127.0.0.1", port = 3306, username = "root", password = "" } = ssConfig;
|
|
61
|
-
return `mysql://${username}:${password}@${host}:${port}/${name}`;
|
|
62
|
-
}
|
|
63
|
-
case "vitess": {
|
|
64
|
-
const vtConfig = config, { name, host = "127.0.0.1", port = 15306, username = "root", password = "", tabletType } = vtConfig, keyspace = tabletType ? `${name}@${tabletType}` : name;
|
|
65
|
-
return `mysql://${username}:${password}@${host}:${port}/${keyspace}`;
|
|
66
|
-
}
|
|
67
|
-
case "postgres": {
|
|
68
|
-
const pgConfig = config, { name, host = "127.0.0.1", port = 5432, username = "postgres", password = "" } = pgConfig;
|
|
69
|
-
return `postgres://${username}:${password}@${host}:${port}/${name}`;
|
|
70
|
-
}
|
|
71
|
-
default:
|
|
72
|
-
throw Error(`Unsupported driver: ${driver}`);
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
export function validateDriverConfig(driver, config) {
|
|
76
|
-
const errors = [];
|
|
77
|
-
switch (driver) {
|
|
78
|
-
case "sqlite": {
|
|
79
|
-
if (!config.database)
|
|
80
|
-
errors.push("SQLite requires a database path");
|
|
81
|
-
break;
|
|
82
|
-
}
|
|
83
|
-
case "mysql": {
|
|
84
|
-
if (!config.name)
|
|
85
|
-
errors.push("MySQL requires a database name");
|
|
86
|
-
break;
|
|
87
|
-
}
|
|
88
|
-
case "singlestore": {
|
|
89
|
-
if (!config.name)
|
|
90
|
-
errors.push("SingleStore requires a database name");
|
|
91
|
-
break;
|
|
92
|
-
}
|
|
93
|
-
case "vitess": {
|
|
94
|
-
if (!config.name)
|
|
95
|
-
errors.push("Vitess requires a keyspace name");
|
|
96
|
-
break;
|
|
97
|
-
}
|
|
98
|
-
case "postgres": {
|
|
99
|
-
if (!config.name)
|
|
100
|
-
errors.push("PostgreSQL requires a database name");
|
|
101
|
-
break;
|
|
102
|
-
}
|
|
103
|
-
default:
|
|
104
|
-
errors.push(`Unsupported driver: ${driver}`);
|
|
105
|
-
}
|
|
106
|
-
return {
|
|
107
|
-
valid: errors.length === 0,
|
|
108
|
-
errors
|
|
109
|
-
};
|
|
110
|
-
}
|
|
111
|
-
export function mergeWithDefaults(driver, config) {
|
|
112
|
-
return { ...driverDefaults[driver], ...config };
|
|
113
|
-
}
|
|
114
|
-
export function getConfigFromEnv(driver) {
|
|
115
|
-
switch (driver) {
|
|
116
|
-
case "sqlite":
|
|
117
|
-
return {
|
|
118
|
-
database: env.DB_DATABASE || "database/stacks.sqlite",
|
|
119
|
-
prefix: env.DB_PREFIX || ""
|
|
120
|
-
};
|
|
121
|
-
case "mysql":
|
|
122
|
-
return {
|
|
123
|
-
name: env.DB_DATABASE || "stacks",
|
|
124
|
-
host: env.DB_HOST || "127.0.0.1",
|
|
125
|
-
port: env.DB_PORT ?? 3306,
|
|
126
|
-
username: env.DB_USERNAME || "root",
|
|
127
|
-
password: env.DB_PASSWORD || "",
|
|
128
|
-
prefix: env.DB_PREFIX || ""
|
|
129
|
-
};
|
|
130
|
-
case "singlestore":
|
|
131
|
-
return {
|
|
132
|
-
name: env.DB_DATABASE || "stacks",
|
|
133
|
-
host: env.DB_HOST || "127.0.0.1",
|
|
134
|
-
port: env.DB_PORT ?? 3306,
|
|
135
|
-
username: env.DB_USERNAME || "root",
|
|
136
|
-
password: env.DB_PASSWORD || "",
|
|
137
|
-
prefix: env.DB_PREFIX || "",
|
|
138
|
-
ssl: env.DB_SSL === "true" || env.DB_SSL === "1"
|
|
139
|
-
};
|
|
140
|
-
case "vitess":
|
|
141
|
-
return {
|
|
142
|
-
name: env.DB_DATABASE || "stacks",
|
|
143
|
-
host: env.DB_HOST || "127.0.0.1",
|
|
144
|
-
port: env.DB_PORT ?? 15306,
|
|
145
|
-
username: env.DB_USERNAME || "root",
|
|
146
|
-
password: env.DB_PASSWORD || "",
|
|
147
|
-
prefix: env.DB_PREFIX || "",
|
|
148
|
-
ssl: env.DB_SSL === "true" || env.DB_SSL === "1"
|
|
149
|
-
};
|
|
150
|
-
case "postgres":
|
|
151
|
-
return {
|
|
152
|
-
name: env.DB_DATABASE || "stacks",
|
|
153
|
-
host: env.DB_HOST || "127.0.0.1",
|
|
154
|
-
port: env.DB_PORT ?? 5432,
|
|
155
|
-
username: env.DB_USERNAME || "postgres",
|
|
156
|
-
password: env.DB_PASSWORD || "",
|
|
157
|
-
prefix: env.DB_PREFIX || "",
|
|
158
|
-
schema: env.DB_SCHEMA || "public"
|
|
159
|
-
};
|
|
160
|
-
default:
|
|
161
|
-
throw Error(`Unsupported driver: ${driver}`);
|
|
162
|
-
}
|
|
163
|
-
}
|
|
164
|
-
export function detectDriver() {
|
|
165
|
-
if (env.DB_CONNECTION)
|
|
166
|
-
return env.DB_CONNECTION;
|
|
167
|
-
if (env.DATABASE_URL?.startsWith("postgres"))
|
|
168
|
-
return "postgres";
|
|
169
|
-
if (env.DATABASE_URL?.startsWith("mysql"))
|
|
170
|
-
return "mysql";
|
|
171
|
-
return "sqlite";
|
|
172
|
-
}
|
|
1
|
+
import{env}from"@stacksjs/env";export const driverDefaults={sqlite:{database:"database/stacks.sqlite",prefix:""},mysql:{name:"stacks",host:"127.0.0.1",port:3306,username:"root",password:"",prefix:"",charset:"utf8mb4",collation:"utf8mb4_unicode_ci"},singlestore:{name:"stacks",host:"127.0.0.1",port:3306,username:"root",password:"",prefix:"",charset:"utf8mb4",ssl:!1},vitess:{name:"stacks",host:"127.0.0.1",port:15306,username:"root",password:"",prefix:"",ssl:!1},postgres:{name:"stacks",host:"127.0.0.1",port:5432,username:"postgres",password:"",prefix:"",schema:"public"},browser:{}};export function getConnectionString(driver,config){switch(driver){case"sqlite":{const sqliteConfig=config;if(sqliteConfig.database===":memory:")return":memory:";return`sqlite://${sqliteConfig.database}`}case"mysql":{const mysqlConfig=config,{name,host="127.0.0.1",port=3306,username="root",password=""}=mysqlConfig;return`mysql://${username}:${password}@${host}:${port}/${name}`}case"singlestore":{const ssConfig=config,{name,host="127.0.0.1",port=3306,username="root",password=""}=ssConfig;return`mysql://${username}:${password}@${host}:${port}/${name}`}case"vitess":{const vtConfig=config,{name,host="127.0.0.1",port=15306,username="root",password="",tabletType}=vtConfig,keyspace=tabletType?`${name}@${tabletType}`:name;return`mysql://${username}:${password}@${host}:${port}/${keyspace}`}case"postgres":{const pgConfig=config,{name,host="127.0.0.1",port=5432,username="postgres",password=""}=pgConfig;return`postgres://${username}:${password}@${host}:${port}/${name}`}default:throw Error(`Unsupported driver: ${driver}`)}}export function validateDriverConfig(driver,config){const errors=[];switch(driver){case"sqlite":{if(!config.database)errors.push("SQLite requires a database path");break}case"mysql":{if(!config.name)errors.push("MySQL requires a database name");break}case"singlestore":{if(!config.name)errors.push("SingleStore requires a database name");break}case"vitess":{if(!config.name)errors.push("Vitess requires a keyspace name");break}case"postgres":{if(!config.name)errors.push("PostgreSQL requires a database name");break}default:errors.push(`Unsupported driver: ${driver}`)}return{valid:errors.length===0,errors}}export function mergeWithDefaults(driver,config){return{...driverDefaults[driver],...config}}export function getConfigFromEnv(driver){switch(driver){case"sqlite":return{database:env.DB_DATABASE||"database/stacks.sqlite",prefix:env.DB_PREFIX||""};case"mysql":return{name:env.DB_DATABASE||"stacks",host:env.DB_HOST||"127.0.0.1",port:env.DB_PORT??3306,username:env.DB_USERNAME||"root",password:env.DB_PASSWORD||"",prefix:env.DB_PREFIX||""};case"singlestore":return{name:env.DB_DATABASE||"stacks",host:env.DB_HOST||"127.0.0.1",port:env.DB_PORT??3306,username:env.DB_USERNAME||"root",password:env.DB_PASSWORD||"",prefix:env.DB_PREFIX||"",ssl:env.DB_SSL==="true"||env.DB_SSL==="1"};case"vitess":return{name:env.DB_DATABASE||"stacks",host:env.DB_HOST||"127.0.0.1",port:env.DB_PORT??15306,username:env.DB_USERNAME||"root",password:env.DB_PASSWORD||"",prefix:env.DB_PREFIX||"",ssl:env.DB_SSL==="true"||env.DB_SSL==="1"};case"postgres":return{name:env.DB_DATABASE||"stacks",host:env.DB_HOST||"127.0.0.1",port:env.DB_PORT??5432,username:env.DB_USERNAME||"postgres",password:env.DB_PASSWORD||"",prefix:env.DB_PREFIX||"",schema:env.DB_SCHEMA||"public"};default:throw Error(`Unsupported driver: ${driver}`)}}export function detectDriver(){if(env.DB_CONNECTION)return env.DB_CONNECTION;if(env.DATABASE_URL?.startsWith("postgres"))return"postgres";if(env.DATABASE_URL?.startsWith("mysql"))return"mysql";return"sqlite"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export
|
|
1
|
+
export*from"./traits";
|
|
@@ -1,29 +1 @@
|
|
|
1
|
-
import { db }
|
|
2
|
-
import { dialectCapabilities } from "../../dialect";
|
|
3
|
-
import { traitTableNames } from "../../trait-tables";
|
|
4
|
-
import { env as envVars } from "@stacksjs/env";
|
|
5
|
-
function getDbDriver() {
|
|
6
|
-
return process.env.DB_CONNECTION || envVars.DB_CONNECTION || "sqlite";
|
|
7
|
-
}
|
|
8
|
-
export function commonTableNames() {
|
|
9
|
-
return [
|
|
10
|
-
...traitTableNames(),
|
|
11
|
-
"passkeys",
|
|
12
|
-
"password_resets",
|
|
13
|
-
"query_logs",
|
|
14
|
-
"tags",
|
|
15
|
-
"comments",
|
|
16
|
-
"categories_models",
|
|
17
|
-
"activities"
|
|
18
|
-
];
|
|
19
|
-
}
|
|
20
|
-
export async function dropCommonTables() {
|
|
21
|
-
const { identifierQuote: q, wire } = dialectCapabilities(getDbDriver()), cascade = wire === "postgres" ? " CASCADE" : "";
|
|
22
|
-
for (const table of commonTableNames())
|
|
23
|
-
await db.unsafe(`DROP TABLE IF EXISTS ${q}${table}${q}${cascade}`).execute();
|
|
24
|
-
}
|
|
25
|
-
export async function dropMigrationTables() {
|
|
26
|
-
const { identifierQuote: q, wire } = dialectCapabilities(getDbDriver()), cascade = wire === "postgres" ? " CASCADE" : "";
|
|
27
|
-
for (const table of ["migrations", "migration_locks"])
|
|
28
|
-
await db.unsafe(`DROP TABLE IF EXISTS ${q}${table}${q}${cascade}`).execute();
|
|
29
|
-
}
|
|
1
|
+
import{db}from"../../utils";import{dialectCapabilities}from"../../dialect";import{traitTableNames}from"../../trait-tables";import{env as envVars}from"@stacksjs/env";function getDbDriver(){return process.env.DB_CONNECTION||envVars.DB_CONNECTION||"sqlite"}export function commonTableNames(){return[...traitTableNames(),"passkeys","password_resets","query_logs","tags","comments","categories_models","activities"]}export async function dropCommonTables(){const{identifierQuote:q,wire}=dialectCapabilities(getDbDriver()),cascade=wire==="postgres"?" CASCADE":"";for(const table of commonTableNames())await db.unsafe(`DROP TABLE IF EXISTS ${q}${table}${q}${cascade}`).execute()}export async function dropMigrationTables(){const{identifierQuote:q,wire}=dialectCapabilities(getDbDriver()),cascade=wire==="postgres"?" CASCADE":"";for(const table of["migrations","migration_locks"])await db.unsafe(`DROP TABLE IF EXISTS ${q}${table}${q}${cascade}`).execute()}
|