@veap/database 0.0.0-stage → 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 +176 -2
- package/dist/casts.d.ts +9 -0
- package/dist/casts.js +78 -0
- package/dist/connection.d.ts +49 -0
- package/dist/connection.js +189 -0
- package/dist/errors.d.ts +7 -0
- package/dist/errors.js +17 -0
- package/dist/events.d.ts +5 -0
- package/dist/events.js +7 -0
- package/dist/factory.d.ts +68 -0
- package/dist/factory.js +161 -0
- package/dist/index.d.ts +17 -0
- package/dist/index.js +17 -0
- package/dist/logger.d.ts +11 -0
- package/dist/logger.js +21 -0
- package/dist/migration-provider.d.ts +5 -0
- package/dist/migration-provider.js +15 -0
- package/dist/migrations/generator.d.ts +4 -0
- package/dist/migrations/generator.js +58 -0
- package/dist/migrations/index.d.ts +3 -0
- package/dist/migrations/index.js +3 -0
- package/dist/migrations/runner.d.ts +12 -0
- package/dist/migrations/runner.js +178 -0
- package/dist/migrations/schema-builder.d.ts +84 -0
- package/dist/migrations/schema-builder.js +409 -0
- package/dist/model.d.ts +379 -0
- package/dist/model.js +899 -0
- package/dist/provider.d.ts +12 -0
- package/dist/provider.js +58 -0
- package/dist/query-builder.d.ts +197 -0
- package/dist/query-builder.js +715 -0
- package/dist/relations/belongs-to-many.d.ts +15 -0
- package/dist/relations/belongs-to-many.js +80 -0
- package/dist/relations/belongs-to.d.ts +8 -0
- package/dist/relations/belongs-to.js +37 -0
- package/dist/relations/eager-loader.d.ts +5 -0
- package/dist/relations/eager-loader.js +66 -0
- package/dist/relations/has-many.d.ts +9 -0
- package/dist/relations/has-many.js +48 -0
- package/dist/relations/has-one.d.ts +8 -0
- package/dist/relations/has-one.js +37 -0
- package/dist/relations/index.d.ts +11 -0
- package/dist/relations/index.js +11 -0
- package/dist/relations/morph-many.d.ts +14 -0
- package/dist/relations/morph-many.js +65 -0
- package/dist/relations/morph-map.d.ts +32 -0
- package/dist/relations/morph-map.js +73 -0
- package/dist/relations/morph-one.d.ts +14 -0
- package/dist/relations/morph-one.js +65 -0
- package/dist/relations/morph-to-many.d.ts +44 -0
- package/dist/relations/morph-to-many.js +215 -0
- package/dist/relations/morph-to.d.ts +19 -0
- package/dist/relations/morph-to.js +90 -0
- package/dist/relations/relation.d.ts +22 -0
- package/dist/relations/relation.js +13 -0
- package/dist/scopes.d.ts +16 -0
- package/dist/scopes.js +1 -0
- package/dist/seeder.d.ts +25 -0
- package/dist/seeder.js +49 -0
- package/dist/types.d.ts +31 -0
- package/dist/types.js +1 -0
- package/dist/utils.d.ts +9 -0
- package/dist/utils.js +20 -0
- package/package.json +50 -4
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { ServiceProvider } from "@veap/kernel";
|
|
2
|
+
import type { DatabaseConfig } from "./types";
|
|
3
|
+
export interface DatabaseProviderConfig {
|
|
4
|
+
databaseUrl?: string;
|
|
5
|
+
config?: DatabaseConfig;
|
|
6
|
+
}
|
|
7
|
+
export declare class DatabaseServiceProvider extends ServiceProvider {
|
|
8
|
+
private config?;
|
|
9
|
+
constructor(container: any, config?: DatabaseProviderConfig);
|
|
10
|
+
register(): void;
|
|
11
|
+
boot(): Promise<void>;
|
|
12
|
+
}
|
package/dist/provider.js
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { ConfigService, DATABASE, ServiceProvider } from "@veap/kernel";
|
|
2
|
+
import knex from "knex";
|
|
3
|
+
import { initDatabase, isSqliteDatabase, resolvePostgresSslConfig, resolveSqliteFilename, setKnex, } from "./connection";
|
|
4
|
+
import { debug } from "./logger";
|
|
5
|
+
export class DatabaseServiceProvider extends ServiceProvider {
|
|
6
|
+
config;
|
|
7
|
+
constructor(container, config) {
|
|
8
|
+
super(container);
|
|
9
|
+
this.config = config;
|
|
10
|
+
}
|
|
11
|
+
register() {
|
|
12
|
+
// Port token registration
|
|
13
|
+
}
|
|
14
|
+
async boot() {
|
|
15
|
+
const configService = await this.container.resolve(ConfigService);
|
|
16
|
+
const databaseUrl = this.config?.databaseUrl ||
|
|
17
|
+
configService.get("DATABASE_URL") ||
|
|
18
|
+
process.env.DATABASE_URL ||
|
|
19
|
+
process.env.DATABASE_FILE;
|
|
20
|
+
const isProd = configService.get("NODE_ENV") === "production" ||
|
|
21
|
+
process.env.NODE_ENV === "production";
|
|
22
|
+
if (this.config?.config) {
|
|
23
|
+
const instance = initDatabase(this.config.config);
|
|
24
|
+
this.container.register({ token: DATABASE, useValue: instance });
|
|
25
|
+
this.container.register({ token: "Knex", useValue: instance });
|
|
26
|
+
return;
|
|
27
|
+
}
|
|
28
|
+
const resolvedUrl = databaseUrl || "dev.sqlite";
|
|
29
|
+
const isSqlite = isSqliteDatabase(resolvedUrl);
|
|
30
|
+
const client = isSqlite ? "better-sqlite3" : "pg";
|
|
31
|
+
debug("veap:database", `Auto-initializing Knex from DATABASE_URL with client: ${client}`);
|
|
32
|
+
const ssl = resolvePostgresSslConfig({
|
|
33
|
+
databaseUrl: resolvedUrl,
|
|
34
|
+
isProd,
|
|
35
|
+
rejectUnauthorizedEnv: configService.get("DATABASE_SSL_REJECT_UNAUTHORIZED"),
|
|
36
|
+
caCert: configService.get("DATABASE_SSL_CA"),
|
|
37
|
+
});
|
|
38
|
+
const instance = knex({
|
|
39
|
+
client,
|
|
40
|
+
connection: isSqlite
|
|
41
|
+
? { filename: resolveSqliteFilename(resolvedUrl) }
|
|
42
|
+
: {
|
|
43
|
+
connectionString: resolvedUrl,
|
|
44
|
+
ssl,
|
|
45
|
+
},
|
|
46
|
+
useNullAsDefault: true,
|
|
47
|
+
});
|
|
48
|
+
setKnex(instance);
|
|
49
|
+
this.container.register({
|
|
50
|
+
token: DATABASE,
|
|
51
|
+
useValue: instance,
|
|
52
|
+
});
|
|
53
|
+
this.container.register({
|
|
54
|
+
token: "Knex",
|
|
55
|
+
useValue: instance,
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
}
|
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
import type { Knex } from "knex";
|
|
2
|
+
import type { Model } from "./model";
|
|
3
|
+
export declare class ModelQueryBuilder<M extends Model = any> {
|
|
4
|
+
modelClass: any;
|
|
5
|
+
[key: string]: any;
|
|
6
|
+
protected knexBuilder: Knex.QueryBuilder;
|
|
7
|
+
protected eagerRelations: string[];
|
|
8
|
+
protected _withTrashed: boolean;
|
|
9
|
+
protected _onlyTrashed: boolean;
|
|
10
|
+
protected _softDeleteScopeApplied: boolean;
|
|
11
|
+
protected _scopesApplied: boolean;
|
|
12
|
+
protected _ignoredGlobalScopes: Set<string>;
|
|
13
|
+
protected _ignoreAllGlobalScopes: boolean;
|
|
14
|
+
constructor(modelClass: any, // typeof Model
|
|
15
|
+
customKnexBuilder?: Knex.QueryBuilder);
|
|
16
|
+
/**
|
|
17
|
+
* Explicitly applies a local scope method by name.
|
|
18
|
+
*/
|
|
19
|
+
scope(name: string, ...args: any[]): this;
|
|
20
|
+
/**
|
|
21
|
+
* Excludes a specific global scope from this query.
|
|
22
|
+
*/
|
|
23
|
+
withoutGlobalScope(scope: string): this;
|
|
24
|
+
/**
|
|
25
|
+
* Excludes multiple or all global scopes from this query.
|
|
26
|
+
*/
|
|
27
|
+
withoutGlobalScopes(scopes?: string[]): this;
|
|
28
|
+
/**
|
|
29
|
+
* Includes soft-deleted models in query results.
|
|
30
|
+
*/
|
|
31
|
+
withTrashed(): this;
|
|
32
|
+
/**
|
|
33
|
+
* Restricts query to only soft-deleted models.
|
|
34
|
+
*/
|
|
35
|
+
onlyTrashed(): this;
|
|
36
|
+
/**
|
|
37
|
+
* Applies both user-defined global scopes and soft-delete scope.
|
|
38
|
+
*/
|
|
39
|
+
protected applyGlobalScopes(): void;
|
|
40
|
+
/**
|
|
41
|
+
* Applies the soft delete filter to the underlying query if enabled.
|
|
42
|
+
*/
|
|
43
|
+
protected applySoftDeleteScope(): void;
|
|
44
|
+
/**
|
|
45
|
+
* Returns the underlying Knex query builder.
|
|
46
|
+
*/
|
|
47
|
+
toKnex(): Knex.QueryBuilder;
|
|
48
|
+
/**
|
|
49
|
+
* Basic WHERE clause. Automatically maps camelCase to snake_case columns.
|
|
50
|
+
*/
|
|
51
|
+
where(column: any, operator?: any, value?: any): this;
|
|
52
|
+
orWhere(column: any, operator?: any, value?: any): this;
|
|
53
|
+
whereIn(column: string, values: any[]): this;
|
|
54
|
+
whereNotIn(column: string, values: any[]): this;
|
|
55
|
+
whereNull(column: string): this;
|
|
56
|
+
whereNotNull(column: string): this;
|
|
57
|
+
whereBetween(column: string, range: [any, any]): this;
|
|
58
|
+
whereLike(column: string, pattern: string): this;
|
|
59
|
+
whereILike(column: string, pattern: string): this;
|
|
60
|
+
select(...columns: any[]): this;
|
|
61
|
+
orderBy(column: string, direction?: "asc" | "desc"): this;
|
|
62
|
+
latest(column?: string): this;
|
|
63
|
+
oldest(column?: string): this;
|
|
64
|
+
limit(count: number): this;
|
|
65
|
+
take(count: number): this;
|
|
66
|
+
offset(count: number): this;
|
|
67
|
+
skip(count: number): this;
|
|
68
|
+
join(table: string, first: string, operatorOrSecond: string, second?: string): this;
|
|
69
|
+
leftJoin(table: string, first: string, operatorOrSecond: string, second?: string): this;
|
|
70
|
+
/**
|
|
71
|
+
* Eager load relations (e.g. .with('author', 'posts.comments'))
|
|
72
|
+
*/
|
|
73
|
+
with(...relations: (string | string[])[]): this;
|
|
74
|
+
/**
|
|
75
|
+
* Helper to retrieve a relation instance from a temporary model instance.
|
|
76
|
+
*/
|
|
77
|
+
protected getRelationInstance(relationName: string): any;
|
|
78
|
+
/**
|
|
79
|
+
* Builds an EXISTS or COUNT subquery for a relation.
|
|
80
|
+
*/
|
|
81
|
+
protected buildRelationSubquery(sub: Knex.QueryBuilder, relationName: string, callback?: (builder: ModelQueryBuilder) => void, isCount?: boolean): void;
|
|
82
|
+
/**
|
|
83
|
+
* Adds a relationship count condition to the query with where clause.
|
|
84
|
+
*/
|
|
85
|
+
has(relation: string, operator?: string, count?: number, boolean?: "and" | "or", callback?: (query: ModelQueryBuilder) => void): this;
|
|
86
|
+
/**
|
|
87
|
+
* Adds an OR relationship count condition to the query.
|
|
88
|
+
*/
|
|
89
|
+
orHas(relation: string, operator?: string, count?: number, callback?: (query: ModelQueryBuilder) => void): this;
|
|
90
|
+
/**
|
|
91
|
+
* Adds a relationship existence condition with an optional callback constraint.
|
|
92
|
+
*/
|
|
93
|
+
whereHas(relation: string, callback?: (query: ModelQueryBuilder) => void): this;
|
|
94
|
+
/**
|
|
95
|
+
* Adds an OR relationship existence condition with an optional callback constraint.
|
|
96
|
+
*/
|
|
97
|
+
orWhereHas(relation: string, callback?: (query: ModelQueryBuilder) => void): this;
|
|
98
|
+
/**
|
|
99
|
+
* Adds a relationship absence condition with an optional callback constraint.
|
|
100
|
+
*/
|
|
101
|
+
whereDoesntHave(relation: string, callback?: (query: ModelQueryBuilder) => void): this;
|
|
102
|
+
/**
|
|
103
|
+
* Adds an OR relationship absence condition with an optional callback constraint.
|
|
104
|
+
*/
|
|
105
|
+
orWhereDoesntHave(relation: string, callback?: (query: ModelQueryBuilder) => void): this;
|
|
106
|
+
/**
|
|
107
|
+
* Alias for whereDoesntHave.
|
|
108
|
+
*/
|
|
109
|
+
doesntHave(relation: string): this;
|
|
110
|
+
/**
|
|
111
|
+
* Alias for orWhereDoesntHave.
|
|
112
|
+
*/
|
|
113
|
+
orDoesntHave(relation: string): this;
|
|
114
|
+
/**
|
|
115
|
+
* Adds subquery counts for related models as `{relation}_count`.
|
|
116
|
+
*/
|
|
117
|
+
withCount(...relations: (string | string[] | Record<string, ((q: ModelQueryBuilder) => void) | undefined>)[]): this;
|
|
118
|
+
/**
|
|
119
|
+
* Executes query and returns hydrated Model instances.
|
|
120
|
+
*/
|
|
121
|
+
get(): Promise<M[]>;
|
|
122
|
+
/**
|
|
123
|
+
* Returns the first matched Model instance or null.
|
|
124
|
+
*/
|
|
125
|
+
first(): Promise<M | null>;
|
|
126
|
+
/**
|
|
127
|
+
* Returns the first matched Model or throws an error.
|
|
128
|
+
*/
|
|
129
|
+
firstOrFail(): Promise<M>;
|
|
130
|
+
/**
|
|
131
|
+
* Get the first record matching the attributes or instantiate it.
|
|
132
|
+
*/
|
|
133
|
+
firstOrNew(attributes: Record<string, any>, values?: Record<string, any>): Promise<M>;
|
|
134
|
+
/**
|
|
135
|
+
* Get the first record matching the attributes or create it.
|
|
136
|
+
*/
|
|
137
|
+
firstOrCreate(attributes: Record<string, any>, values?: Record<string, any>): Promise<M>;
|
|
138
|
+
/**
|
|
139
|
+
* Create or update a record matching the attributes, and fill it with values.
|
|
140
|
+
*/
|
|
141
|
+
updateOrCreate(attributes: Record<string, any>, values?: Record<string, any>): Promise<M>;
|
|
142
|
+
/**
|
|
143
|
+
* Finds a record by its primary key.
|
|
144
|
+
*/
|
|
145
|
+
find(id: any): Promise<M | null>;
|
|
146
|
+
/**
|
|
147
|
+
* Finds a record by primary key or throws an error.
|
|
148
|
+
*/
|
|
149
|
+
findOrFail(id: any): Promise<M>;
|
|
150
|
+
/**
|
|
151
|
+
* Counts rows matching criteria.
|
|
152
|
+
*/
|
|
153
|
+
count(column?: string): Promise<number>;
|
|
154
|
+
/**
|
|
155
|
+
* Checks if any records exist matching criteria.
|
|
156
|
+
*/
|
|
157
|
+
exists(): Promise<boolean>;
|
|
158
|
+
/**
|
|
159
|
+
* Plucks a single column's values into an array.
|
|
160
|
+
*/
|
|
161
|
+
pluck<K extends string>(column: K): Promise<any[]>;
|
|
162
|
+
/**
|
|
163
|
+
* Paginates results.
|
|
164
|
+
*/
|
|
165
|
+
paginate(page?: number, perPage?: number): Promise<{
|
|
166
|
+
data: M[];
|
|
167
|
+
total: number;
|
|
168
|
+
page: number;
|
|
169
|
+
perPage: number;
|
|
170
|
+
lastPage: number;
|
|
171
|
+
}>;
|
|
172
|
+
/**
|
|
173
|
+
* Bulk updates matching records.
|
|
174
|
+
*/
|
|
175
|
+
update(attributes: Record<string, any>): Promise<number>;
|
|
176
|
+
/**
|
|
177
|
+
* Bulk deletes matching records (soft-deletes if softDeletes is enabled).
|
|
178
|
+
*/
|
|
179
|
+
delete(): Promise<number>;
|
|
180
|
+
/**
|
|
181
|
+
* Bulk force-deletes matching records (hard delete regardless of softDeletes setting).
|
|
182
|
+
*/
|
|
183
|
+
forceDelete(): Promise<number>;
|
|
184
|
+
/**
|
|
185
|
+
* Bulk restores soft-deleted matching records.
|
|
186
|
+
*/
|
|
187
|
+
restore(): Promise<number>;
|
|
188
|
+
/**
|
|
189
|
+
* Increments column value.
|
|
190
|
+
*/
|
|
191
|
+
increment(column: string, amount?: number): Promise<number>;
|
|
192
|
+
/**
|
|
193
|
+
* Decrements column value.
|
|
194
|
+
*/
|
|
195
|
+
decrement(column: string, amount?: number): Promise<number>;
|
|
196
|
+
toSQL(): Knex.Sql;
|
|
197
|
+
}
|