@avelonjs/orm 0.1.0 → 0.3.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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/model.ts +17 -14
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@avelonjs/orm",
3
- "version": "0.1.0",
3
+ "version": "0.3.0",
4
4
  "private": false,
5
5
  "description": "Model layer and QueryIR builder for Avelon applications.",
6
6
  "license": "MIT",
package/src/model.ts CHANGED
@@ -16,6 +16,7 @@ import {
16
16
  type QueryIR,
17
17
  type QueryResult,
18
18
  type RelationLoad,
19
+ type WardAction,
19
20
  type WardInput,
20
21
  } from '@avelonjs/core'
21
22
 
@@ -278,10 +279,7 @@ export class QueryBuilder<TModel extends Model = Model> {
278
279
  ...(this.#limit !== undefined && mode !== 'count' ? { limit: this.#limit } : {}),
279
280
  ...(this.#offset !== undefined && mode !== 'count' ? { offset: this.#offset } : {}),
280
281
  }
281
- if (this.#unwarded) return this.#ward === undefined ? base : injectWard(base, this.#ward)
282
- const resolved = this.#resolvedWard()
283
- if (resolved === undefined && this.#ward === undefined) return base
284
- return injectWard(base, this.#ward ?? resolved ?? true)
282
+ return this.#warded(base, 'read')
285
283
  }
286
284
 
287
285
  /** Executes a select and hydrates models. */
@@ -322,9 +320,9 @@ export class QueryBuilder<TModel extends Model = Model> {
322
320
  }
323
321
  }
324
322
 
325
- /** Inserts one or more rows. */
323
+ /** Inserts one or more rows under the model's `insert` ward. */
326
324
  async insert(values: Row | Row[], returning?: string[] | '*'): Promise<QueryResult<Row>> {
327
- return this.#driver.execute<Row>({
325
+ const base: QueryIR = {
328
326
  table: tableOf(this.#model),
329
327
  mode: 'insert',
330
328
  select: [],
@@ -333,7 +331,8 @@ export class QueryBuilder<TModel extends Model = Model> {
333
331
  order: [],
334
332
  values,
335
333
  returning,
336
- })
334
+ }
335
+ return this.#driver.execute<Row>(this.#warded(base, 'insert'))
337
336
  }
338
337
 
339
338
  /** Updates matching rows. */
@@ -360,15 +359,19 @@ export class QueryBuilder<TModel extends Model = Model> {
360
359
  })
361
360
  }
362
361
 
363
- #resolvedWard(): WardInput | undefined {
362
+ #warded(base: QueryIR, action: WardAction): QueryIR {
363
+ if (this.#unwarded) return this.#ward === undefined ? base : injectWard(base, this.#ward)
364
+ const resolved = this.#resolvedWard(action)
365
+ if (resolved === undefined && this.#ward === undefined) return base
366
+ return injectWard(base, this.#ward ?? resolved ?? true)
367
+ }
368
+
369
+ #resolvedWard(action: WardAction): WardInput | undefined {
370
+ const actor = this.#actor === undefined ? Gate.actor() : this.#actor
364
371
  try {
365
- return resolveWard(
366
- this.#model,
367
- 'read',
368
- this.#actor === undefined ? Gate.actor() : this.#actor,
369
- )
372
+ return resolveWard(this.#model, action, actor)
370
373
  } catch {
371
- return this.#model.ward?.(this.#actor === undefined ? Gate.actor() : this.#actor)
374
+ return this.#model.ward?.(actor)
372
375
  }
373
376
  }
374
377