@elysiumjs/core 0.2.0 → 0.3.1
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/README.md +1 -1
- package/package.json +2 -2
- package/src/model.ts +24 -0
- package/src/redis.ts +2 -2
- package/src/repository.ts +11 -20
package/README.md
CHANGED
|
@@ -109,4 +109,4 @@ Feel free to open issues for any bugs or questions you may have. Pull requests a
|
|
|
109
109
|
|
|
110
110
|
## License
|
|
111
111
|
|
|
112
|
-
This project is licensed under the Apache License, Version 2.0. See the [LICENSE](LICENSE) file for more information.
|
|
112
|
+
This project is licensed under the Apache License, Version 2.0. See the [LICENSE](../../LICENSE) file for more information.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@elysiumjs/core",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.1",
|
|
4
4
|
"license": "Apache-2.0",
|
|
5
5
|
"description": "A super-charged web framework for Bun, built on top of ElysiaJS.",
|
|
6
6
|
"private": false,
|
|
@@ -41,7 +41,7 @@
|
|
|
41
41
|
"@types/bun": "latest"
|
|
42
42
|
},
|
|
43
43
|
"peerDependencies": {
|
|
44
|
-
"typescript": "^5"
|
|
44
|
+
"typescript": "^5.8.3"
|
|
45
45
|
},
|
|
46
46
|
"trustedDependencies": [
|
|
47
47
|
"es5-ext"
|
package/src/model.ts
CHANGED
|
@@ -119,6 +119,12 @@ export type ModelClass<
|
|
|
119
119
|
*/
|
|
120
120
|
readonly $inferUpdate: Partial<TTable['$inferSelect']>;
|
|
121
121
|
|
|
122
|
+
/**
|
|
123
|
+
* The drizzle's table schema wrapped by this model.
|
|
124
|
+
* Will be automatically wrapped by the `Tenancy.withTenant()` function when the current tenant is not `public`.
|
|
125
|
+
*/
|
|
126
|
+
readonly table: TTable;
|
|
127
|
+
|
|
122
128
|
/**
|
|
123
129
|
* The name of the table wrapped by the model.
|
|
124
130
|
*/
|
|
@@ -189,6 +195,24 @@ export const Model = <TColumnsMap extends Record<string, PgColumnBuilderBase>>(
|
|
|
189
195
|
*/
|
|
190
196
|
public static readonly $inferUpdate: Partial<typeof table.$inferInsert> = table.$inferInsert;
|
|
191
197
|
|
|
198
|
+
/**
|
|
199
|
+
* The drizzle's table schema wrapped by this model.
|
|
200
|
+
*/
|
|
201
|
+
public static get table(): ReturnType<typeof pgTable<string, TColumnsMap>> {
|
|
202
|
+
// If we are not inside an Application context, we can't use the tenancy system
|
|
203
|
+
if (!Application.instance || !Application.context.getStore) {
|
|
204
|
+
return table;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
if (this.supportTenancy) {
|
|
208
|
+
const tenant = Tenancy.getCurrentTenant() ?? 'public';
|
|
209
|
+
// @ts-expect-error typeof this strangely doesn't match the ModelClass type
|
|
210
|
+
return Tenancy.withTenant(tenant, this);
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
return table;
|
|
214
|
+
}
|
|
215
|
+
|
|
192
216
|
/**
|
|
193
217
|
* The name of the table wrapped by the model.
|
|
194
218
|
*/
|
package/src/redis.ts
CHANGED
|
@@ -26,7 +26,7 @@ import { Service } from './service';
|
|
|
26
26
|
* Properties used to create a new redis connection.
|
|
27
27
|
* @author Axel Nana <axel.nana@workbud.com>
|
|
28
28
|
*/
|
|
29
|
-
export type RedisConnectionProps = Bun.RedisOptions;
|
|
29
|
+
export type RedisConnectionProps = Bun.RedisOptions & { url: string };
|
|
30
30
|
|
|
31
31
|
/**
|
|
32
32
|
* Keyv adapter for redis. This adapter is based on the Bun's Redis client.
|
|
@@ -361,7 +361,7 @@ export namespace Redis {
|
|
|
361
361
|
* @param config The configuration for the redis connection.
|
|
362
362
|
* @returns The newly created and registered redis connection.
|
|
363
363
|
*/
|
|
364
|
-
export const registerConnection = (name: string, config:
|
|
364
|
+
export const registerConnection = (name: string, config: RedisConnectionProps) => {
|
|
365
365
|
if (Service.exists(getConnectionName(name))) {
|
|
366
366
|
// TODO: Use logger service here
|
|
367
367
|
console.error(`A connection with the name ${name} has already been registered.`);
|
package/src/repository.ts
CHANGED
|
@@ -24,7 +24,6 @@ import { pgTable } from 'drizzle-orm/pg-core';
|
|
|
24
24
|
|
|
25
25
|
import { Application } from './app';
|
|
26
26
|
import { Database } from './database';
|
|
27
|
-
import { Tenancy } from './model';
|
|
28
27
|
|
|
29
28
|
/**
|
|
30
29
|
* Database's primary column type.
|
|
@@ -178,24 +177,12 @@ export const Repository = <
|
|
|
178
177
|
return db ?? Database.getConnection(connection);
|
|
179
178
|
}
|
|
180
179
|
|
|
181
|
-
/**
|
|
182
|
-
* The drizzle's table schema wrapped by this repository.
|
|
183
|
-
*/
|
|
184
|
-
public static get table(): ReturnType<typeof pgTable<string, TColumnsMap>> {
|
|
185
|
-
if (model.supportTenancy) {
|
|
186
|
-
const tenant = Tenancy.getCurrentTenant() ?? 'public';
|
|
187
|
-
return Tenancy.withTenant(tenant, model);
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
return table;
|
|
191
|
-
}
|
|
192
|
-
|
|
193
180
|
/**
|
|
194
181
|
* Retrieves all the records in the database.
|
|
195
182
|
* @returns All the records in the database.
|
|
196
183
|
*/
|
|
197
184
|
public async all(): Promise<TSelect[]> {
|
|
198
|
-
return await this.db.select().from(
|
|
185
|
+
return await this.db.select().from(model.table);
|
|
199
186
|
}
|
|
200
187
|
|
|
201
188
|
/**
|
|
@@ -204,7 +191,7 @@ export const Repository = <
|
|
|
204
191
|
* @returns The record with the given id.
|
|
205
192
|
*/
|
|
206
193
|
public async find(id: TId): Promise<TSelect | null> {
|
|
207
|
-
const [row] = await this.db.select().from(
|
|
194
|
+
const [row] = await this.db.select().from(model.table).where(eq(model.table.id, id));
|
|
208
195
|
return (row ?? null) as TSelect | null;
|
|
209
196
|
}
|
|
210
197
|
|
|
@@ -214,7 +201,7 @@ export const Repository = <
|
|
|
214
201
|
* @returns The inserted record.
|
|
215
202
|
*/
|
|
216
203
|
public async insert(data: TInsert): Promise<TSelect> {
|
|
217
|
-
const [row] = await this.db.insert(
|
|
204
|
+
const [row] = await this.db.insert(model.table).values(data).returning();
|
|
218
205
|
return row as TSelect;
|
|
219
206
|
}
|
|
220
207
|
|
|
@@ -225,7 +212,11 @@ export const Repository = <
|
|
|
225
212
|
* @returns The updated record.
|
|
226
213
|
*/
|
|
227
214
|
public async update(id: TId, data: TUpdate): Promise<TSelect> {
|
|
228
|
-
const [row] = await this.db
|
|
215
|
+
const [row] = await this.db
|
|
216
|
+
.update(model.table)
|
|
217
|
+
.set(data)
|
|
218
|
+
.where(eq(model.table.id, id))
|
|
219
|
+
.returning();
|
|
229
220
|
return row as TSelect;
|
|
230
221
|
}
|
|
231
222
|
|
|
@@ -235,7 +226,7 @@ export const Repository = <
|
|
|
235
226
|
* @returns The updated records.
|
|
236
227
|
*/
|
|
237
228
|
public async updateAll(data: TUpdate): Promise<TSelect[]> {
|
|
238
|
-
return await this.db.update(
|
|
229
|
+
return await this.db.update(model.table).set(data).returning();
|
|
239
230
|
}
|
|
240
231
|
|
|
241
232
|
/**
|
|
@@ -244,7 +235,7 @@ export const Repository = <
|
|
|
244
235
|
* @returns The deleted record.
|
|
245
236
|
*/
|
|
246
237
|
public async delete(id: TId): Promise<TSelect> {
|
|
247
|
-
const [row] = await this.db.delete(
|
|
238
|
+
const [row] = await this.db.delete(model.table).where(eq(model.table.id, id)).returning();
|
|
248
239
|
return row as TSelect;
|
|
249
240
|
}
|
|
250
241
|
|
|
@@ -253,7 +244,7 @@ export const Repository = <
|
|
|
253
244
|
* @returns All the records in the database.
|
|
254
245
|
*/
|
|
255
246
|
public async deleteAll(): Promise<TSelect[]> {
|
|
256
|
-
return await this.db.delete(
|
|
247
|
+
return await this.db.delete(model.table).returning();
|
|
257
248
|
}
|
|
258
249
|
}
|
|
259
250
|
|