@stacksjs/database 0.70.87 → 0.70.90
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 +220 -0
- package/dist/class-seeder.js +116 -0
- package/dist/column.d.ts +17 -0
- package/dist/column.js +26 -0
- package/dist/custom/audits.js +57 -0
- package/dist/custom/errors.js +48 -0
- package/dist/custom/index.js +3 -0
- package/dist/custom/jobs.js +449 -0
- package/dist/database.js +178 -0
- package/dist/defaults.js +48 -0
- package/dist/driver-config.js +144 -0
- package/dist/drivers/defaults/index.js +2 -0
- package/dist/drivers/defaults/passwords.js +106 -0
- package/dist/drivers/defaults/traits.js +1125 -0
- package/dist/drivers/dynamodb.js +607 -0
- package/dist/drivers/helpers.js +206 -0
- package/dist/drivers/index.js +9 -0
- package/dist/drivers/mysql.js +322 -0
- package/dist/drivers/postgres.js +411 -0
- package/dist/drivers/sqlite.js +397 -0
- package/dist/factory.js +51 -0
- package/dist/fk-audit.js +181 -0
- package/dist/index.js +55 -1263
- package/dist/migration-lock.js +143 -0
- package/dist/migrations.js +528 -0
- package/dist/notification-tables.js +54 -0
- package/dist/query-logger.js +213 -0
- package/dist/query-parser.js +93 -0
- package/dist/rbac-tables.js +84 -0
- package/dist/safe-migrations.js +59 -0
- package/dist/schema.d.ts +4 -0
- package/dist/schema.js +10 -0
- package/dist/seed-scaffold.js +144 -0
- package/dist/seeder.js +363 -0
- package/dist/sql-helpers.js +24 -0
- package/dist/table.d.ts +7 -0
- package/dist/table.js +26 -0
- package/dist/tools/setup.d.ts +1 -0
- package/dist/tools/setup.js +6 -0
- package/dist/transaction-context.js +62 -0
- package/dist/types.js +23 -0
- package/dist/unique-audit.js +174 -0
- package/dist/utils.js +163 -0
- package/dist/uuid-columns.js +68 -0
- package/dist/validators.js +122 -0
- package/package.json +11 -11
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
import { env } from "@stacksjs/env";
|
|
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
|
+
postgres: {
|
|
28
|
+
name: "stacks",
|
|
29
|
+
host: "127.0.0.1",
|
|
30
|
+
port: 5432,
|
|
31
|
+
username: "postgres",
|
|
32
|
+
password: "",
|
|
33
|
+
prefix: "",
|
|
34
|
+
schema: "public"
|
|
35
|
+
},
|
|
36
|
+
browser: {}
|
|
37
|
+
};
|
|
38
|
+
export function getConnectionString(driver, config) {
|
|
39
|
+
switch (driver) {
|
|
40
|
+
case "sqlite": {
|
|
41
|
+
const sqliteConfig = config;
|
|
42
|
+
if (sqliteConfig.database === ":memory:")
|
|
43
|
+
return ":memory:";
|
|
44
|
+
return `sqlite://${sqliteConfig.database}`;
|
|
45
|
+
}
|
|
46
|
+
case "mysql": {
|
|
47
|
+
const mysqlConfig = config, { name, host = "127.0.0.1", port = 3306, username = "root", password = "" } = mysqlConfig;
|
|
48
|
+
return `mysql://${username}:${password}@${host}:${port}/${name}`;
|
|
49
|
+
}
|
|
50
|
+
case "singlestore": {
|
|
51
|
+
const ssConfig = config, { name, host = "127.0.0.1", port = 3306, username = "root", password = "" } = ssConfig;
|
|
52
|
+
return `mysql://${username}:${password}@${host}:${port}/${name}`;
|
|
53
|
+
}
|
|
54
|
+
case "postgres": {
|
|
55
|
+
const pgConfig = config, { name, host = "127.0.0.1", port = 5432, username = "postgres", password = "" } = pgConfig;
|
|
56
|
+
return `postgres://${username}:${password}@${host}:${port}/${name}`;
|
|
57
|
+
}
|
|
58
|
+
default:
|
|
59
|
+
throw Error(`Unsupported driver: ${driver}`);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
export function validateDriverConfig(driver, config) {
|
|
63
|
+
const errors = [];
|
|
64
|
+
switch (driver) {
|
|
65
|
+
case "sqlite": {
|
|
66
|
+
if (!config.database)
|
|
67
|
+
errors.push("SQLite requires a database path");
|
|
68
|
+
break;
|
|
69
|
+
}
|
|
70
|
+
case "mysql": {
|
|
71
|
+
if (!config.name)
|
|
72
|
+
errors.push("MySQL requires a database name");
|
|
73
|
+
break;
|
|
74
|
+
}
|
|
75
|
+
case "singlestore": {
|
|
76
|
+
if (!config.name)
|
|
77
|
+
errors.push("SingleStore requires a database name");
|
|
78
|
+
break;
|
|
79
|
+
}
|
|
80
|
+
case "postgres": {
|
|
81
|
+
if (!config.name)
|
|
82
|
+
errors.push("PostgreSQL requires a database name");
|
|
83
|
+
break;
|
|
84
|
+
}
|
|
85
|
+
default:
|
|
86
|
+
errors.push(`Unsupported driver: ${driver}`);
|
|
87
|
+
}
|
|
88
|
+
return {
|
|
89
|
+
valid: errors.length === 0,
|
|
90
|
+
errors
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
export function mergeWithDefaults(driver, config) {
|
|
94
|
+
return { ...driverDefaults[driver], ...config };
|
|
95
|
+
}
|
|
96
|
+
export function getConfigFromEnv(driver) {
|
|
97
|
+
switch (driver) {
|
|
98
|
+
case "sqlite":
|
|
99
|
+
return {
|
|
100
|
+
database: env.DB_DATABASE || "database/stacks.sqlite",
|
|
101
|
+
prefix: env.DB_PREFIX || ""
|
|
102
|
+
};
|
|
103
|
+
case "mysql":
|
|
104
|
+
return {
|
|
105
|
+
name: env.DB_DATABASE || "stacks",
|
|
106
|
+
host: env.DB_HOST || "127.0.0.1",
|
|
107
|
+
port: env.DB_PORT ?? 3306,
|
|
108
|
+
username: env.DB_USERNAME || "root",
|
|
109
|
+
password: env.DB_PASSWORD || "",
|
|
110
|
+
prefix: env.DB_PREFIX || ""
|
|
111
|
+
};
|
|
112
|
+
case "singlestore":
|
|
113
|
+
return {
|
|
114
|
+
name: env.DB_DATABASE || "stacks",
|
|
115
|
+
host: env.DB_HOST || "127.0.0.1",
|
|
116
|
+
port: env.DB_PORT ?? 3306,
|
|
117
|
+
username: env.DB_USERNAME || "root",
|
|
118
|
+
password: env.DB_PASSWORD || "",
|
|
119
|
+
prefix: env.DB_PREFIX || "",
|
|
120
|
+
ssl: env.DB_SSL === "true" || env.DB_SSL === "1"
|
|
121
|
+
};
|
|
122
|
+
case "postgres":
|
|
123
|
+
return {
|
|
124
|
+
name: env.DB_DATABASE || "stacks",
|
|
125
|
+
host: env.DB_HOST || "127.0.0.1",
|
|
126
|
+
port: env.DB_PORT ?? 5432,
|
|
127
|
+
username: env.DB_USERNAME || "postgres",
|
|
128
|
+
password: env.DB_PASSWORD || "",
|
|
129
|
+
prefix: env.DB_PREFIX || "",
|
|
130
|
+
schema: env.DB_SCHEMA || "public"
|
|
131
|
+
};
|
|
132
|
+
default:
|
|
133
|
+
throw Error(`Unsupported driver: ${driver}`);
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
export function detectDriver() {
|
|
137
|
+
if (env.DB_CONNECTION)
|
|
138
|
+
return env.DB_CONNECTION;
|
|
139
|
+
if (env.DATABASE_URL?.startsWith("postgres"))
|
|
140
|
+
return "postgres";
|
|
141
|
+
if (env.DATABASE_URL?.startsWith("mysql"))
|
|
142
|
+
return "mysql";
|
|
143
|
+
return "sqlite";
|
|
144
|
+
}
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import { log } from "@stacksjs/logging";
|
|
2
|
+
function italic(str) {
|
|
3
|
+
return `\x1B[3m${str}\x1B[23m`;
|
|
4
|
+
}
|
|
5
|
+
import { path } from "@stacksjs/path";
|
|
6
|
+
import { hasMigrationBeenCreated } from "../helpers";
|
|
7
|
+
export async function createPasswordResetsTable() {
|
|
8
|
+
if (await hasMigrationBeenCreated("password_resets"))
|
|
9
|
+
return;
|
|
10
|
+
let migrationContent = `import type { Database } from '@stacksjs/database'
|
|
11
|
+
`;
|
|
12
|
+
migrationContent += `import { sql } from '@stacksjs/database'
|
|
13
|
+
|
|
14
|
+
`;
|
|
15
|
+
migrationContent += `export async function up(db: Database<any>) {
|
|
16
|
+
`;
|
|
17
|
+
migrationContent += ` await db.schema
|
|
18
|
+
`;
|
|
19
|
+
migrationContent += ` .createTable('password_resets')
|
|
20
|
+
`;
|
|
21
|
+
migrationContent += ` .addColumn('email', 'varchar(255)', col => col.notNull())
|
|
22
|
+
`;
|
|
23
|
+
migrationContent += ` .addColumn('token', 'varchar(255)', col => col.notNull())
|
|
24
|
+
`;
|
|
25
|
+
migrationContent += ` .addColumn('created_at', 'timestamp', col => col.notNull().defaultTo(sql.raw('CURRENT_TIMESTAMP')))
|
|
26
|
+
`;
|
|
27
|
+
migrationContent += ` .execute()
|
|
28
|
+
|
|
29
|
+
`;
|
|
30
|
+
migrationContent += ` await db.schema
|
|
31
|
+
`;
|
|
32
|
+
migrationContent += ` .createIndex('password_resets_email_index')
|
|
33
|
+
`;
|
|
34
|
+
migrationContent += ` .on('password_resets')
|
|
35
|
+
`;
|
|
36
|
+
migrationContent += ` .column('email')
|
|
37
|
+
`;
|
|
38
|
+
migrationContent += ` .execute()
|
|
39
|
+
|
|
40
|
+
`;
|
|
41
|
+
migrationContent += ` await db.schema
|
|
42
|
+
`;
|
|
43
|
+
migrationContent += ` .createIndex('password_resets_token_index')
|
|
44
|
+
`;
|
|
45
|
+
migrationContent += ` .on('password_resets')
|
|
46
|
+
`;
|
|
47
|
+
migrationContent += ` .column('token')
|
|
48
|
+
`;
|
|
49
|
+
migrationContent += ` .execute()
|
|
50
|
+
`;
|
|
51
|
+
migrationContent += `}
|
|
52
|
+
`;
|
|
53
|
+
const migrationFileName = `${new Date().getTime().toString()}-create-password-resets-table.ts`, migrationFilePath = path.userMigrationsPath(migrationFileName);
|
|
54
|
+
await Bun.write(migrationFilePath, migrationContent);
|
|
55
|
+
log.success(`Created migration: ${italic(migrationFileName)}`);
|
|
56
|
+
}
|
|
57
|
+
export async function createPostgresPasswordResetsTable() {
|
|
58
|
+
if (await hasMigrationBeenCreated("password_resets"))
|
|
59
|
+
return;
|
|
60
|
+
let migrationContent = `import type { Database } from '@stacksjs/database'
|
|
61
|
+
`;
|
|
62
|
+
migrationContent += `import { sql } from '@stacksjs/database'
|
|
63
|
+
|
|
64
|
+
`;
|
|
65
|
+
migrationContent += `export async function up(db: Database<any>) {
|
|
66
|
+
`;
|
|
67
|
+
migrationContent += ` await db.schema
|
|
68
|
+
`;
|
|
69
|
+
migrationContent += ` .createTable('password_resets')
|
|
70
|
+
`;
|
|
71
|
+
migrationContent += ` .addColumn('email', 'varchar(255)', col => col.notNull())
|
|
72
|
+
`;
|
|
73
|
+
migrationContent += ` .addColumn('token', 'varchar(255)', col => col.notNull())
|
|
74
|
+
`;
|
|
75
|
+
migrationContent += ` .addColumn('created_at', 'timestamp', col => col.notNull().defaultTo(sql.raw('CURRENT_TIMESTAMP')))
|
|
76
|
+
`;
|
|
77
|
+
migrationContent += ` .execute()
|
|
78
|
+
|
|
79
|
+
`;
|
|
80
|
+
migrationContent += ` await db.schema
|
|
81
|
+
`;
|
|
82
|
+
migrationContent += ` .createIndex('password_resets_email_index')
|
|
83
|
+
`;
|
|
84
|
+
migrationContent += ` .on('password_resets')
|
|
85
|
+
`;
|
|
86
|
+
migrationContent += ` .column('email')
|
|
87
|
+
`;
|
|
88
|
+
migrationContent += ` .execute()
|
|
89
|
+
|
|
90
|
+
`;
|
|
91
|
+
migrationContent += ` await db.schema
|
|
92
|
+
`;
|
|
93
|
+
migrationContent += ` .createIndex('password_resets_token_index')
|
|
94
|
+
`;
|
|
95
|
+
migrationContent += ` .on('password_resets')
|
|
96
|
+
`;
|
|
97
|
+
migrationContent += ` .column('token')
|
|
98
|
+
`;
|
|
99
|
+
migrationContent += ` .execute()
|
|
100
|
+
`;
|
|
101
|
+
migrationContent += `}
|
|
102
|
+
`;
|
|
103
|
+
const migrationFileName = `${new Date().getTime().toString()}-create-password-resets-table.ts`, migrationFilePath = path.userMigrationsPath(migrationFileName);
|
|
104
|
+
await Bun.write(migrationFilePath, migrationContent);
|
|
105
|
+
log.success(`Created migration: ${italic(migrationFileName)}`);
|
|
106
|
+
}
|