pinia-orm-edge 1.9.0-28582911.bbf7a93 → 1.9.0-28583486.807fa36

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/casts.d.cts CHANGED
@@ -1,4 +1,4 @@
1
- import { $ as CastAttribute, m as ModelFields } from './shared/pinia-orm.677cd0bd.cjs';
1
+ import { a0 as CastAttribute, m as ModelFields } from './shared/pinia-orm.b5d0a9df.cjs';
2
2
  import 'pinia';
3
3
  import '@pinia-orm/normalizr';
4
4
  import '@/composables';
package/dist/casts.d.mts CHANGED
@@ -1,4 +1,4 @@
1
- import { $ as CastAttribute, m as ModelFields } from './shared/pinia-orm.677cd0bd.mjs';
1
+ import { a0 as CastAttribute, m as ModelFields } from './shared/pinia-orm.b5d0a9df.mjs';
2
2
  import 'pinia';
3
3
  import '@pinia-orm/normalizr';
4
4
  import '@/composables';
package/dist/casts.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { $ as CastAttribute, m as ModelFields } from './shared/pinia-orm.677cd0bd.js';
1
+ import { a0 as CastAttribute, m as ModelFields } from './shared/pinia-orm.b5d0a9df.js';
2
2
  import 'pinia';
3
3
  import '@pinia-orm/normalizr';
4
4
  import '@/composables';
@@ -1,5 +1,5 @@
1
- import { a0 as TypeDefault, af as PropertyDecorator, ag as TypeOptions, ah as UidOptions, M as Model, s as PrimaryKey, z as deleteModes, $ as CastAttribute, ai as Mutator } from './shared/pinia-orm.677cd0bd.cjs';
2
- export { aj as NanoidOptions } from './shared/pinia-orm.677cd0bd.cjs';
1
+ import { a1 as TypeDefault, an as PropertyDecorator, ao as TypeOptions, ap as UidOptions, M as Model, s as PrimaryKey, H as deleteModes, a0 as CastAttribute, aj as Mutator } from './shared/pinia-orm.b5d0a9df.cjs';
2
+ export { aq as NanoidOptions } from './shared/pinia-orm.b5d0a9df.cjs';
3
3
  import 'pinia';
4
4
  import '@pinia-orm/normalizr';
5
5
  import '@/composables';
@@ -1,5 +1,5 @@
1
- import { a0 as TypeDefault, af as PropertyDecorator, ag as TypeOptions, ah as UidOptions, M as Model, s as PrimaryKey, z as deleteModes, $ as CastAttribute, ai as Mutator } from './shared/pinia-orm.677cd0bd.mjs';
2
- export { aj as NanoidOptions } from './shared/pinia-orm.677cd0bd.mjs';
1
+ import { a1 as TypeDefault, an as PropertyDecorator, ao as TypeOptions, ap as UidOptions, M as Model, s as PrimaryKey, H as deleteModes, a0 as CastAttribute, aj as Mutator } from './shared/pinia-orm.b5d0a9df.mjs';
2
+ export { aq as NanoidOptions } from './shared/pinia-orm.b5d0a9df.mjs';
3
3
  import 'pinia';
4
4
  import '@pinia-orm/normalizr';
5
5
  import '@/composables';
@@ -1,5 +1,5 @@
1
- import { a0 as TypeDefault, af as PropertyDecorator, ag as TypeOptions, ah as UidOptions, M as Model, s as PrimaryKey, z as deleteModes, $ as CastAttribute, ai as Mutator } from './shared/pinia-orm.677cd0bd.js';
2
- export { aj as NanoidOptions } from './shared/pinia-orm.677cd0bd.js';
1
+ import { a1 as TypeDefault, an as PropertyDecorator, ao as TypeOptions, ap as UidOptions, M as Model, s as PrimaryKey, H as deleteModes, a0 as CastAttribute, aj as Mutator } from './shared/pinia-orm.b5d0a9df.js';
2
+ export { aq as NanoidOptions } from './shared/pinia-orm.b5d0a9df.js';
3
3
  import 'pinia';
4
4
  import '@pinia-orm/normalizr';
5
5
  import '@/composables';
@@ -1,4 +1,4 @@
1
- import { M as Model, b as Collection } from './shared/pinia-orm.677cd0bd.cjs';
1
+ import { M as Model, b as Collection } from './shared/pinia-orm.b5d0a9df.cjs';
2
2
  import 'pinia';
3
3
  import '@pinia-orm/normalizr';
4
4
  import '@/composables';
@@ -24,7 +24,7 @@ declare function useCollect<M extends Model = Model>(models: Collection<M>): Use
24
24
  /**
25
25
  * Get the sum value of the specified filed.
26
26
  */
27
- declare function useSum(models: Collection, field: string): number;
27
+ declare function useSum(models: Collection<any>, field: string): number;
28
28
 
29
29
  /**
30
30
  * The useGroupBy method groups the collection's items by a given key.
@@ -34,7 +34,7 @@ declare function useGroupBy<T extends Record<string, any>>(models: T[], fields:
34
34
  /**
35
35
  * The keys method returns all of the collection's primary keys
36
36
  */
37
- declare function useKeys(models: Collection): string[];
37
+ declare function useKeys(models: Collection<any>): string[];
38
38
 
39
39
  /**
40
40
  * Get the min value of the specified filed.
@@ -1,4 +1,4 @@
1
- import { M as Model, b as Collection } from './shared/pinia-orm.677cd0bd.mjs';
1
+ import { M as Model, b as Collection } from './shared/pinia-orm.b5d0a9df.mjs';
2
2
  import 'pinia';
3
3
  import '@pinia-orm/normalizr';
4
4
  import '@/composables';
@@ -24,7 +24,7 @@ declare function useCollect<M extends Model = Model>(models: Collection<M>): Use
24
24
  /**
25
25
  * Get the sum value of the specified filed.
26
26
  */
27
- declare function useSum(models: Collection, field: string): number;
27
+ declare function useSum(models: Collection<any>, field: string): number;
28
28
 
29
29
  /**
30
30
  * The useGroupBy method groups the collection's items by a given key.
@@ -34,7 +34,7 @@ declare function useGroupBy<T extends Record<string, any>>(models: T[], fields:
34
34
  /**
35
35
  * The keys method returns all of the collection's primary keys
36
36
  */
37
- declare function useKeys(models: Collection): string[];
37
+ declare function useKeys(models: Collection<any>): string[];
38
38
 
39
39
  /**
40
40
  * Get the min value of the specified filed.
package/dist/helpers.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { M as Model, b as Collection } from './shared/pinia-orm.677cd0bd.js';
1
+ import { M as Model, b as Collection } from './shared/pinia-orm.b5d0a9df.js';
2
2
  import 'pinia';
3
3
  import '@pinia-orm/normalizr';
4
4
  import '@/composables';
@@ -24,7 +24,7 @@ declare function useCollect<M extends Model = Model>(models: Collection<M>): Use
24
24
  /**
25
25
  * Get the sum value of the specified filed.
26
26
  */
27
- declare function useSum(models: Collection, field: string): number;
27
+ declare function useSum(models: Collection<any>, field: string): number;
28
28
 
29
29
  /**
30
30
  * The useGroupBy method groups the collection's items by a given key.
@@ -34,7 +34,7 @@ declare function useGroupBy<T extends Record<string, any>>(models: T[], fields:
34
34
  /**
35
35
  * The keys method returns all of the collection's primary keys
36
36
  */
37
- declare function useKeys(models: Collection): string[];
37
+ declare function useKeys(models: Collection<any>): string[];
38
38
 
39
39
  /**
40
40
  * Get the min value of the specified filed.
package/dist/index.cjs CHANGED
@@ -380,7 +380,7 @@ class Schema {
380
380
  one(model, parent) {
381
381
  model = model || this.model;
382
382
  parent = parent || this.model;
383
- const entity = `${model.$entity()}${parent.$entity()}`;
383
+ const entity = `${model.$self().modelEntity()}${parent.$self().modelEntity()}`;
384
384
  if (this.schemas[entity]) {
385
385
  return this.schemas[entity];
386
386
  }
@@ -401,7 +401,7 @@ class Schema {
401
401
  */
402
402
  union(models, callback) {
403
403
  const schemas = models.reduce((schemas2, model) => {
404
- schemas2[model.$entity()] = this.one(model);
404
+ schemas2[model.$self().modelEntity()] = this.one(model);
405
405
  return schemas2;
406
406
  }, {});
407
407
  return new normalizr.schema.Union(schemas, callback);
@@ -410,7 +410,7 @@ class Schema {
410
410
  * Create a new normalizr entity.
411
411
  */
412
412
  newEntity(model, parent) {
413
- const entity = model.$entity();
413
+ const entity = model.$self().modelEntity();
414
414
  const idAttribute = this.idAttribute(model, parent);
415
415
  return new normalizr.schema.Entity(entity, {}, { idAttribute });
416
416
  }
@@ -448,7 +448,7 @@ class Schema {
448
448
  }
449
449
  }
450
450
  if (["BelongsTo", "HasOne", "MorphOne", "MorphTo"].includes(parent.$fields()[key]?.constructor.name ?? "") && Utils.isArray(parentRecord[key])) {
451
- Utils.throwError(['You are passing a list to "', `${parent.$entity()}.${key}`, `" which is a one to one Relation(${parent.$fields()[key]?.constructor.name}):`, JSON.stringify(parentRecord[key])]);
451
+ Utils.throwError(['You are passing a list to "', `${parent.$modelEntity()}.${key}`, `" which is a one to one Relation(${parent.$fields()[key]?.constructor.name}):`, JSON.stringify(parentRecord[key])]);
452
452
  }
453
453
  const id = model.$getIndexId(record);
454
454
  return id;
@@ -828,6 +828,8 @@ class Query {
828
828
  this.getNewHydrated = true;
829
829
  return this;
830
830
  }
831
+ // where(field: T, value?: WhereSecondaryClosure<M[T]> | M[T]): this;
832
+ // where<T extends WherePrimaryClosure<M> | keyof M>(field: T, value?: WhereSecondaryClosure<M[T]> | M[T]): this;
831
833
  /**
832
834
  * Add a basic where clause to the query.
833
835
  */
@@ -1030,11 +1032,14 @@ class Query {
1030
1032
  */
1031
1033
  getFieldWhereForRelations(relation, callback = () => {
1032
1034
  }, operator, count) {
1033
- const modelIdsByRelation = this.newQuery(this.model.$entity()).with(relation, callback).get(false).filter((model) => Utils.compareWithOperator(
1034
- Utils.isArray(model[relation]) ? model[relation].length : model[relation] === null ? 0 : 1,
1035
- typeof operator === "number" ? operator : count ?? 1,
1036
- typeof operator === "number" || count === void 0 ? ">=" : operator
1037
- )).map((model) => model.$getIndexId());
1035
+ const modelIdsByRelation = this.newQuery(this.model.$entity()).with(relation, callback).get(false).filter((model) => {
1036
+ const modelRelation = model[relation];
1037
+ return Utils.compareWithOperator(
1038
+ Utils.isArray(modelRelation) ? modelRelation.length : modelRelation === null ? 0 : 1,
1039
+ typeof operator === "number" ? operator : count ?? 1,
1040
+ typeof operator === "number" || count === void 0 ? ">=" : operator
1041
+ );
1042
+ }).map((model) => model.$getIndexId());
1038
1043
  return (model) => modelIdsByRelation.includes(model.$getIndexId());
1039
1044
  }
1040
1045
  /**
@@ -1177,9 +1182,6 @@ class Query {
1177
1182
  * Filter the given collection by the registered order conditions.
1178
1183
  */
1179
1184
  filterOrder(models) {
1180
- if (this.orders.length === 0) {
1181
- return models;
1182
- }
1183
1185
  const fields = this.orders.map((order) => order.field);
1184
1186
  const directions = this.orders.map((order) => order.direction);
1185
1187
  return Utils.orderBy(models, fields, directions);
@@ -1305,7 +1307,7 @@ class Query {
1305
1307
  save(records) {
1306
1308
  let processedData = this.newInterpreter().process(records);
1307
1309
  const modelTypes = this.model.$types();
1308
- const isChildEntity = this.model.$baseEntity() !== this.model.$entity();
1310
+ const isChildEntity = this.model.$baseEntity() !== this.model.$entity() || this.model.$baseNamespace() !== this.model.$namespace();
1309
1311
  if (Object.values(modelTypes).length > 0 || isChildEntity) {
1310
1312
  const modelTypesKeys = Object.keys(modelTypes);
1311
1313
  const recordsByTypes = {};
@@ -1319,10 +1321,10 @@ class Query {
1319
1321
  });
1320
1322
  for (const entry in recordsByTypes) {
1321
1323
  const typeModel = modelTypes[entry];
1322
- if (typeModel.entity === this.model.$entity()) {
1324
+ if (typeModel.modelEntity() === this.model.$modelEntity()) {
1323
1325
  processedData = this.newInterpreter().process(recordsByTypes[entry]);
1324
1326
  } else {
1325
- this.newQueryWithConstraints(typeModel.entity).save(recordsByTypes[entry]);
1327
+ this.newQueryWithConstraints(typeModel.modelEntity()).save(recordsByTypes[entry]);
1326
1328
  }
1327
1329
  }
1328
1330
  }
@@ -1978,7 +1980,7 @@ class Database {
1978
1980
  * Register the given model.
1979
1981
  */
1980
1982
  register(model) {
1981
- const entity = model.$entity();
1983
+ const entity = model.$self().modelEntity();
1982
1984
  if (!this.models[entity]) {
1983
1985
  this.models[entity] = model;
1984
1986
  this.registerRelatedModels(model);
@@ -2859,6 +2861,8 @@ class Model {
2859
2861
  * Create a new model instance.
2860
2862
  */
2861
2863
  constructor(attributes, options = { operation: "set" }) {
2864
+ // [s: keyof ModelFields]: any
2865
+ __publicField(this, "pivot");
2862
2866
  this.$boot();
2863
2867
  const fill = options.fill ?? true;
2864
2868
  fill && this.$fill(attributes, options);
@@ -2869,21 +2873,28 @@ class Model {
2869
2873
  static fields() {
2870
2874
  return {};
2871
2875
  }
2876
+ static usedNamespace() {
2877
+ return this.namespace ?? config.model.namespace;
2878
+ }
2879
+ static modelEntity() {
2880
+ return (this.usedNamespace() ? this.usedNamespace() + "/" : "") + this.entity;
2881
+ }
2872
2882
  /**
2873
2883
  * Build the schema by evaluating fields and registry.
2874
2884
  */
2875
2885
  static initializeSchema() {
2876
- this.schemas[this.entity] = {};
2877
- this.fieldsOnDelete[this.entity] = this.fieldsOnDelete[this.entity] ?? {};
2886
+ const entity = this.modelEntity();
2887
+ this.schemas[entity] = {};
2888
+ this.fieldsOnDelete[entity] = this.fieldsOnDelete[entity] ?? {};
2878
2889
  const registry = {
2879
2890
  ...this.fields(),
2880
- ...this.registries[this.entity]
2891
+ ...this.registries[entity]
2881
2892
  };
2882
2893
  for (const key in registry) {
2883
2894
  const attribute = registry[key];
2884
- this.schemas[this.entity][key] = typeof attribute === "function" ? attribute() : attribute;
2885
- if (this.fieldsOnDelete[this.entity][key]) {
2886
- this.schemas[this.entity][key] = this.schemas[this.entity][key].onDelete(this.fieldsOnDelete[this.entity][key]);
2895
+ this.schemas[entity][key] = typeof attribute === "function" ? attribute() : attribute;
2896
+ if (this.fieldsOnDelete[entity][key]) {
2897
+ this.schemas[entity][key] = this.schemas[entity][key].onDelete(this.fieldsOnDelete[entity][key]);
2887
2898
  }
2888
2899
  }
2889
2900
  }
@@ -2891,18 +2902,18 @@ class Model {
2891
2902
  * Set the attribute to the registry.
2892
2903
  */
2893
2904
  static setRegistry(key, attribute) {
2894
- if (!this.registries[this.entity]) {
2895
- this.registries[this.entity] = {};
2905
+ if (!this.registries[this.modelEntity()]) {
2906
+ this.registries[this.modelEntity()] = {};
2896
2907
  }
2897
- this.registries[this.entity][key] = attribute;
2908
+ this.registries[this.modelEntity()][key] = attribute;
2898
2909
  return this;
2899
2910
  }
2900
2911
  /**
2901
2912
  * Set delete behaviour for relation field
2902
2913
  */
2903
2914
  static setFieldDeleteMode(key, mode) {
2904
- this.fieldsOnDelete[this.entity] = this.fieldsOnDelete[this.entity] ?? {};
2905
- this.fieldsOnDelete[this.entity][key] = mode;
2915
+ this.fieldsOnDelete[this.modelEntity()] = this.fieldsOnDelete[this.modelEntity()] ?? {};
2916
+ this.fieldsOnDelete[this.modelEntity()][key] = mode;
2906
2917
  return this;
2907
2918
  }
2908
2919
  /**
@@ -3135,7 +3146,7 @@ class Model {
3135
3146
  * Get the namespace.
3136
3147
  */
3137
3148
  $namespace() {
3138
- return this.$self().namespace ?? config.model.namespace;
3149
+ return this.$self().usedNamespace();
3139
3150
  }
3140
3151
  /**
3141
3152
  * Get the store name.
@@ -3149,6 +3160,18 @@ class Model {
3149
3160
  $baseEntity() {
3150
3161
  return this.$self().baseEntity ?? this.$entity();
3151
3162
  }
3163
+ /**
3164
+ * Get the base namespace for this model.
3165
+ */
3166
+ $baseNamespace() {
3167
+ return this.$self().baseNamespace ?? this.$namespace();
3168
+ }
3169
+ /**
3170
+ * Get the model entity for this model.
3171
+ */
3172
+ $modelEntity() {
3173
+ return this.$self().modelEntity();
3174
+ }
3152
3175
  /**
3153
3176
  * Get the type key for this model.
3154
3177
  */
@@ -3177,7 +3200,7 @@ class Model {
3177
3200
  * Get the model fields for this model.
3178
3201
  */
3179
3202
  $fields() {
3180
- return this.$self().schemas[this.$entity()];
3203
+ return this.$self().schemas[this.$modelEntity()];
3181
3204
  }
3182
3205
  /**
3183
3206
  * Get the model hidden fields
@@ -3204,8 +3227,8 @@ class Model {
3204
3227
  * Bootstrap this model.
3205
3228
  */
3206
3229
  $boot() {
3207
- if (!this.$self().booted[this.$entity()]) {
3208
- this.$self().booted[this.$entity()] = true;
3230
+ if (!this.$self().booted[this.$modelEntity()]) {
3231
+ this.$self().booted[this.$modelEntity()] = true;
3209
3232
  this.$initializeSchema();
3210
3233
  }
3211
3234
  }
@@ -3522,6 +3545,10 @@ __publicField(Model, "entity");
3522
3545
  * The reference to the base entity name if the class extends a base entity.
3523
3546
  */
3524
3547
  __publicField(Model, "baseEntity");
3548
+ /**
3549
+ * The reference to the base namespace if the class extends a base with a different namespace.
3550
+ */
3551
+ __publicField(Model, "baseNamespace");
3525
3552
  /**
3526
3553
  * Define a namespace if you have multiple equal entity names.
3527
3554
  * Resulting in "{namespace}/{entity}"
package/dist/index.d.cts CHANGED
@@ -1,5 +1,5 @@
1
- import { R as Repository, C as Constructor, M as Model, Q as Query, D as DataStore, E as Elements, W as WeakCache, F as FilledInstallOptions } from './shared/pinia-orm.677cd0bd.cjs';
2
- export { A as AfterHook, x as Attribute, B as BeforeHook, J as BelongsTo, K as BelongsToMany, g as CacheConfigOptions, $ as CastAttribute, _ as Casts, b as Collection, i as CreatePiniaOrm, c as DataStoreState, k as Database, y as Dictionary, ad as EagerLoad, ae as EagerLoadConstraint, a as Element, a8 as Group, aa as GroupBy, ab as GroupByFields, G as GroupedCollection, L as HasMany, T as HasManyBy, U as HasManyThrough, O as HasOne, w as InheritanceTypes, h as InstallOptions, a2 as Interpreter, I as Item, v as MetaValues, f as ModelConfigOptions, m as ModelFields, t as ModelOptions, o as ModelRegistries, q as ModelRegistry, n as ModelSchemas, V as MorphMany, Z as MorphOne, X as MorphTo, Y as MorphToMany, N as NormalizedData, a7 as Order, a9 as OrderBy, ac as OrderDirection, d as PiniaOrmPlugin, P as PiniaOrmPluginContext, s as PrimaryKey, H as Relation, l as Schema, S as Schemas, a1 as Type, a0 as TypeDefault, a3 as Where, a6 as WhereGroup, a4 as WherePrimaryClosure, a5 as WhereSecondaryClosure, j as createORM, e as definePiniaOrmPlugin, z as deleteModes, p as plugins, r as registerPlugins, u as useDataStore } from './shared/pinia-orm.677cd0bd.cjs';
1
+ import { R as Repository, C as Constructor, M as Model, Q as Query, D as DataStore, E as Elements, W as WeakCache, F as FilledInstallOptions } from './shared/pinia-orm.b5d0a9df.cjs';
2
+ export { A as AfterHook, y as Attribute, B as BeforeHook, K as BelongsTo, L as BelongsToMany, am as CacheConfig, g as CacheConfigOptions, a0 as CastAttribute, $ as Casts, b as Collection, i as CreatePiniaOrm, c as DataStoreState, k as Database, z as Dictionary, ah as EagerLoad, ai as EagerLoadConstraint, a as Element, a6 as GetElementType, ac as Group, ae as GroupBy, af as GroupByFields, G as GroupedCollection, O as HasMany, U as HasManyBy, V as HasManyThrough, T as HasOne, w as InheritanceTypes, h as InstallOptions, a3 as Interpreter, I as Item, v as MetaValues, f as ModelConfigOptions, m as ModelFields, t as ModelOptions, o as ModelRegistries, q as ModelRegistry, n as ModelSchemas, X as MorphMany, _ as MorphOne, Y as MorphTo, Z as MorphToMany, aj as Mutator, ak as MutatorFunctions, al as Mutators, a5 as NonMethodKeys, N as NormalizedData, ab as Order, ad as OrderBy, ag as OrderDirection, d as PiniaOrmPlugin, P as PiniaOrmPluginContext, s as PrimaryKey, J as Relation, l as Schema, S as Schemas, a2 as Type, a1 as TypeDefault, a7 as UltimateKeys, a4 as Where, aa as WhereGroup, a8 as WherePrimaryClosure, a9 as WhereSecondaryClosure, x as WithKeys, j as createORM, e as definePiniaOrmPlugin, H as deleteModes, p as plugins, r as registerPlugins, u as useDataStore } from './shared/pinia-orm.b5d0a9df.cjs';
3
3
  import { Pinia } from 'pinia';
4
4
  import '@pinia-orm/normalizr';
5
5
  import '@/composables';
@@ -46,4 +46,4 @@ declare const config: FilledInstallOptions & {
46
46
  [key: string]: any;
47
47
  };
48
48
 
49
- export { CONFIG_DEFAULTS, DataStore, Elements, FilledInstallOptions, type MappedRepositories, Model, type ModelOrRepository, type ModelsOrRepositories, Query, Repository, config, mapRepos, useRepo, useStoreActions };
49
+ export { CONFIG_DEFAULTS, Constructor, DataStore, Elements, FilledInstallOptions, type MappedRepositories, Model, type ModelOrRepository, type ModelsOrRepositories, Query, Repository, config, mapRepos, useRepo, useStoreActions };
package/dist/index.d.mts CHANGED
@@ -1,5 +1,5 @@
1
- import { R as Repository, C as Constructor, M as Model, Q as Query, D as DataStore, E as Elements, W as WeakCache, F as FilledInstallOptions } from './shared/pinia-orm.677cd0bd.mjs';
2
- export { A as AfterHook, x as Attribute, B as BeforeHook, J as BelongsTo, K as BelongsToMany, g as CacheConfigOptions, $ as CastAttribute, _ as Casts, b as Collection, i as CreatePiniaOrm, c as DataStoreState, k as Database, y as Dictionary, ad as EagerLoad, ae as EagerLoadConstraint, a as Element, a8 as Group, aa as GroupBy, ab as GroupByFields, G as GroupedCollection, L as HasMany, T as HasManyBy, U as HasManyThrough, O as HasOne, w as InheritanceTypes, h as InstallOptions, a2 as Interpreter, I as Item, v as MetaValues, f as ModelConfigOptions, m as ModelFields, t as ModelOptions, o as ModelRegistries, q as ModelRegistry, n as ModelSchemas, V as MorphMany, Z as MorphOne, X as MorphTo, Y as MorphToMany, N as NormalizedData, a7 as Order, a9 as OrderBy, ac as OrderDirection, d as PiniaOrmPlugin, P as PiniaOrmPluginContext, s as PrimaryKey, H as Relation, l as Schema, S as Schemas, a1 as Type, a0 as TypeDefault, a3 as Where, a6 as WhereGroup, a4 as WherePrimaryClosure, a5 as WhereSecondaryClosure, j as createORM, e as definePiniaOrmPlugin, z as deleteModes, p as plugins, r as registerPlugins, u as useDataStore } from './shared/pinia-orm.677cd0bd.mjs';
1
+ import { R as Repository, C as Constructor, M as Model, Q as Query, D as DataStore, E as Elements, W as WeakCache, F as FilledInstallOptions } from './shared/pinia-orm.b5d0a9df.mjs';
2
+ export { A as AfterHook, y as Attribute, B as BeforeHook, K as BelongsTo, L as BelongsToMany, am as CacheConfig, g as CacheConfigOptions, a0 as CastAttribute, $ as Casts, b as Collection, i as CreatePiniaOrm, c as DataStoreState, k as Database, z as Dictionary, ah as EagerLoad, ai as EagerLoadConstraint, a as Element, a6 as GetElementType, ac as Group, ae as GroupBy, af as GroupByFields, G as GroupedCollection, O as HasMany, U as HasManyBy, V as HasManyThrough, T as HasOne, w as InheritanceTypes, h as InstallOptions, a3 as Interpreter, I as Item, v as MetaValues, f as ModelConfigOptions, m as ModelFields, t as ModelOptions, o as ModelRegistries, q as ModelRegistry, n as ModelSchemas, X as MorphMany, _ as MorphOne, Y as MorphTo, Z as MorphToMany, aj as Mutator, ak as MutatorFunctions, al as Mutators, a5 as NonMethodKeys, N as NormalizedData, ab as Order, ad as OrderBy, ag as OrderDirection, d as PiniaOrmPlugin, P as PiniaOrmPluginContext, s as PrimaryKey, J as Relation, l as Schema, S as Schemas, a2 as Type, a1 as TypeDefault, a7 as UltimateKeys, a4 as Where, aa as WhereGroup, a8 as WherePrimaryClosure, a9 as WhereSecondaryClosure, x as WithKeys, j as createORM, e as definePiniaOrmPlugin, H as deleteModes, p as plugins, r as registerPlugins, u as useDataStore } from './shared/pinia-orm.b5d0a9df.mjs';
3
3
  import { Pinia } from 'pinia';
4
4
  import '@pinia-orm/normalizr';
5
5
  import '@/composables';
@@ -46,4 +46,4 @@ declare const config: FilledInstallOptions & {
46
46
  [key: string]: any;
47
47
  };
48
48
 
49
- export { CONFIG_DEFAULTS, DataStore, Elements, FilledInstallOptions, type MappedRepositories, Model, type ModelOrRepository, type ModelsOrRepositories, Query, Repository, config, mapRepos, useRepo, useStoreActions };
49
+ export { CONFIG_DEFAULTS, Constructor, DataStore, Elements, FilledInstallOptions, type MappedRepositories, Model, type ModelOrRepository, type ModelsOrRepositories, Query, Repository, config, mapRepos, useRepo, useStoreActions };
package/dist/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
- import { R as Repository, C as Constructor, M as Model, Q as Query, D as DataStore, E as Elements, W as WeakCache, F as FilledInstallOptions } from './shared/pinia-orm.677cd0bd.js';
2
- export { A as AfterHook, x as Attribute, B as BeforeHook, J as BelongsTo, K as BelongsToMany, g as CacheConfigOptions, $ as CastAttribute, _ as Casts, b as Collection, i as CreatePiniaOrm, c as DataStoreState, k as Database, y as Dictionary, ad as EagerLoad, ae as EagerLoadConstraint, a as Element, a8 as Group, aa as GroupBy, ab as GroupByFields, G as GroupedCollection, L as HasMany, T as HasManyBy, U as HasManyThrough, O as HasOne, w as InheritanceTypes, h as InstallOptions, a2 as Interpreter, I as Item, v as MetaValues, f as ModelConfigOptions, m as ModelFields, t as ModelOptions, o as ModelRegistries, q as ModelRegistry, n as ModelSchemas, V as MorphMany, Z as MorphOne, X as MorphTo, Y as MorphToMany, N as NormalizedData, a7 as Order, a9 as OrderBy, ac as OrderDirection, d as PiniaOrmPlugin, P as PiniaOrmPluginContext, s as PrimaryKey, H as Relation, l as Schema, S as Schemas, a1 as Type, a0 as TypeDefault, a3 as Where, a6 as WhereGroup, a4 as WherePrimaryClosure, a5 as WhereSecondaryClosure, j as createORM, e as definePiniaOrmPlugin, z as deleteModes, p as plugins, r as registerPlugins, u as useDataStore } from './shared/pinia-orm.677cd0bd.js';
1
+ import { R as Repository, C as Constructor, M as Model, Q as Query, D as DataStore, E as Elements, W as WeakCache, F as FilledInstallOptions } from './shared/pinia-orm.b5d0a9df.js';
2
+ export { A as AfterHook, y as Attribute, B as BeforeHook, K as BelongsTo, L as BelongsToMany, am as CacheConfig, g as CacheConfigOptions, a0 as CastAttribute, $ as Casts, b as Collection, i as CreatePiniaOrm, c as DataStoreState, k as Database, z as Dictionary, ah as EagerLoad, ai as EagerLoadConstraint, a as Element, a6 as GetElementType, ac as Group, ae as GroupBy, af as GroupByFields, G as GroupedCollection, O as HasMany, U as HasManyBy, V as HasManyThrough, T as HasOne, w as InheritanceTypes, h as InstallOptions, a3 as Interpreter, I as Item, v as MetaValues, f as ModelConfigOptions, m as ModelFields, t as ModelOptions, o as ModelRegistries, q as ModelRegistry, n as ModelSchemas, X as MorphMany, _ as MorphOne, Y as MorphTo, Z as MorphToMany, aj as Mutator, ak as MutatorFunctions, al as Mutators, a5 as NonMethodKeys, N as NormalizedData, ab as Order, ad as OrderBy, ag as OrderDirection, d as PiniaOrmPlugin, P as PiniaOrmPluginContext, s as PrimaryKey, J as Relation, l as Schema, S as Schemas, a2 as Type, a1 as TypeDefault, a7 as UltimateKeys, a4 as Where, aa as WhereGroup, a8 as WherePrimaryClosure, a9 as WhereSecondaryClosure, x as WithKeys, j as createORM, e as definePiniaOrmPlugin, H as deleteModes, p as plugins, r as registerPlugins, u as useDataStore } from './shared/pinia-orm.b5d0a9df.js';
3
3
  import { Pinia } from 'pinia';
4
4
  import '@pinia-orm/normalizr';
5
5
  import '@/composables';
@@ -46,4 +46,4 @@ declare const config: FilledInstallOptions & {
46
46
  [key: string]: any;
47
47
  };
48
48
 
49
- export { CONFIG_DEFAULTS, DataStore, Elements, FilledInstallOptions, type MappedRepositories, Model, type ModelOrRepository, type ModelsOrRepositories, Query, Repository, config, mapRepos, useRepo, useStoreActions };
49
+ export { CONFIG_DEFAULTS, Constructor, DataStore, Elements, FilledInstallOptions, type MappedRepositories, Model, type ModelOrRepository, type ModelsOrRepositories, Query, Repository, config, mapRepos, useRepo, useStoreActions };
package/dist/index.mjs CHANGED
@@ -378,7 +378,7 @@ class Schema {
378
378
  one(model, parent) {
379
379
  model = model || this.model;
380
380
  parent = parent || this.model;
381
- const entity = `${model.$entity()}${parent.$entity()}`;
381
+ const entity = `${model.$self().modelEntity()}${parent.$self().modelEntity()}`;
382
382
  if (this.schemas[entity]) {
383
383
  return this.schemas[entity];
384
384
  }
@@ -399,7 +399,7 @@ class Schema {
399
399
  */
400
400
  union(models, callback) {
401
401
  const schemas = models.reduce((schemas2, model) => {
402
- schemas2[model.$entity()] = this.one(model);
402
+ schemas2[model.$self().modelEntity()] = this.one(model);
403
403
  return schemas2;
404
404
  }, {});
405
405
  return new schema.Union(schemas, callback);
@@ -408,7 +408,7 @@ class Schema {
408
408
  * Create a new normalizr entity.
409
409
  */
410
410
  newEntity(model, parent) {
411
- const entity = model.$entity();
411
+ const entity = model.$self().modelEntity();
412
412
  const idAttribute = this.idAttribute(model, parent);
413
413
  return new schema.Entity(entity, {}, { idAttribute });
414
414
  }
@@ -446,7 +446,7 @@ class Schema {
446
446
  }
447
447
  }
448
448
  if (["BelongsTo", "HasOne", "MorphOne", "MorphTo"].includes(parent.$fields()[key]?.constructor.name ?? "") && isArray(parentRecord[key])) {
449
- throwError(['You are passing a list to "', `${parent.$entity()}.${key}`, `" which is a one to one Relation(${parent.$fields()[key]?.constructor.name}):`, JSON.stringify(parentRecord[key])]);
449
+ throwError(['You are passing a list to "', `${parent.$modelEntity()}.${key}`, `" which is a one to one Relation(${parent.$fields()[key]?.constructor.name}):`, JSON.stringify(parentRecord[key])]);
450
450
  }
451
451
  const id = model.$getIndexId(record);
452
452
  return id;
@@ -826,6 +826,8 @@ class Query {
826
826
  this.getNewHydrated = true;
827
827
  return this;
828
828
  }
829
+ // where(field: T, value?: WhereSecondaryClosure<M[T]> | M[T]): this;
830
+ // where<T extends WherePrimaryClosure<M> | keyof M>(field: T, value?: WhereSecondaryClosure<M[T]> | M[T]): this;
829
831
  /**
830
832
  * Add a basic where clause to the query.
831
833
  */
@@ -1028,11 +1030,14 @@ class Query {
1028
1030
  */
1029
1031
  getFieldWhereForRelations(relation, callback = () => {
1030
1032
  }, operator, count) {
1031
- const modelIdsByRelation = this.newQuery(this.model.$entity()).with(relation, callback).get(false).filter((model) => compareWithOperator(
1032
- isArray(model[relation]) ? model[relation].length : model[relation] === null ? 0 : 1,
1033
- typeof operator === "number" ? operator : count ?? 1,
1034
- typeof operator === "number" || count === void 0 ? ">=" : operator
1035
- )).map((model) => model.$getIndexId());
1033
+ const modelIdsByRelation = this.newQuery(this.model.$entity()).with(relation, callback).get(false).filter((model) => {
1034
+ const modelRelation = model[relation];
1035
+ return compareWithOperator(
1036
+ isArray(modelRelation) ? modelRelation.length : modelRelation === null ? 0 : 1,
1037
+ typeof operator === "number" ? operator : count ?? 1,
1038
+ typeof operator === "number" || count === void 0 ? ">=" : operator
1039
+ );
1040
+ }).map((model) => model.$getIndexId());
1036
1041
  return (model) => modelIdsByRelation.includes(model.$getIndexId());
1037
1042
  }
1038
1043
  /**
@@ -1175,9 +1180,6 @@ class Query {
1175
1180
  * Filter the given collection by the registered order conditions.
1176
1181
  */
1177
1182
  filterOrder(models) {
1178
- if (this.orders.length === 0) {
1179
- return models;
1180
- }
1181
1183
  const fields = this.orders.map((order) => order.field);
1182
1184
  const directions = this.orders.map((order) => order.direction);
1183
1185
  return orderBy(models, fields, directions);
@@ -1303,7 +1305,7 @@ class Query {
1303
1305
  save(records) {
1304
1306
  let processedData = this.newInterpreter().process(records);
1305
1307
  const modelTypes = this.model.$types();
1306
- const isChildEntity = this.model.$baseEntity() !== this.model.$entity();
1308
+ const isChildEntity = this.model.$baseEntity() !== this.model.$entity() || this.model.$baseNamespace() !== this.model.$namespace();
1307
1309
  if (Object.values(modelTypes).length > 0 || isChildEntity) {
1308
1310
  const modelTypesKeys = Object.keys(modelTypes);
1309
1311
  const recordsByTypes = {};
@@ -1317,10 +1319,10 @@ class Query {
1317
1319
  });
1318
1320
  for (const entry in recordsByTypes) {
1319
1321
  const typeModel = modelTypes[entry];
1320
- if (typeModel.entity === this.model.$entity()) {
1322
+ if (typeModel.modelEntity() === this.model.$modelEntity()) {
1321
1323
  processedData = this.newInterpreter().process(recordsByTypes[entry]);
1322
1324
  } else {
1323
- this.newQueryWithConstraints(typeModel.entity).save(recordsByTypes[entry]);
1325
+ this.newQueryWithConstraints(typeModel.modelEntity()).save(recordsByTypes[entry]);
1324
1326
  }
1325
1327
  }
1326
1328
  }
@@ -1976,7 +1978,7 @@ class Database {
1976
1978
  * Register the given model.
1977
1979
  */
1978
1980
  register(model) {
1979
- const entity = model.$entity();
1981
+ const entity = model.$self().modelEntity();
1980
1982
  if (!this.models[entity]) {
1981
1983
  this.models[entity] = model;
1982
1984
  this.registerRelatedModels(model);
@@ -2857,6 +2859,8 @@ class Model {
2857
2859
  * Create a new model instance.
2858
2860
  */
2859
2861
  constructor(attributes, options = { operation: "set" }) {
2862
+ // [s: keyof ModelFields]: any
2863
+ __publicField(this, "pivot");
2860
2864
  this.$boot();
2861
2865
  const fill = options.fill ?? true;
2862
2866
  fill && this.$fill(attributes, options);
@@ -2867,21 +2871,28 @@ class Model {
2867
2871
  static fields() {
2868
2872
  return {};
2869
2873
  }
2874
+ static usedNamespace() {
2875
+ return this.namespace ?? config.model.namespace;
2876
+ }
2877
+ static modelEntity() {
2878
+ return (this.usedNamespace() ? this.usedNamespace() + "/" : "") + this.entity;
2879
+ }
2870
2880
  /**
2871
2881
  * Build the schema by evaluating fields and registry.
2872
2882
  */
2873
2883
  static initializeSchema() {
2874
- this.schemas[this.entity] = {};
2875
- this.fieldsOnDelete[this.entity] = this.fieldsOnDelete[this.entity] ?? {};
2884
+ const entity = this.modelEntity();
2885
+ this.schemas[entity] = {};
2886
+ this.fieldsOnDelete[entity] = this.fieldsOnDelete[entity] ?? {};
2876
2887
  const registry = {
2877
2888
  ...this.fields(),
2878
- ...this.registries[this.entity]
2889
+ ...this.registries[entity]
2879
2890
  };
2880
2891
  for (const key in registry) {
2881
2892
  const attribute = registry[key];
2882
- this.schemas[this.entity][key] = typeof attribute === "function" ? attribute() : attribute;
2883
- if (this.fieldsOnDelete[this.entity][key]) {
2884
- this.schemas[this.entity][key] = this.schemas[this.entity][key].onDelete(this.fieldsOnDelete[this.entity][key]);
2893
+ this.schemas[entity][key] = typeof attribute === "function" ? attribute() : attribute;
2894
+ if (this.fieldsOnDelete[entity][key]) {
2895
+ this.schemas[entity][key] = this.schemas[entity][key].onDelete(this.fieldsOnDelete[entity][key]);
2885
2896
  }
2886
2897
  }
2887
2898
  }
@@ -2889,18 +2900,18 @@ class Model {
2889
2900
  * Set the attribute to the registry.
2890
2901
  */
2891
2902
  static setRegistry(key, attribute) {
2892
- if (!this.registries[this.entity]) {
2893
- this.registries[this.entity] = {};
2903
+ if (!this.registries[this.modelEntity()]) {
2904
+ this.registries[this.modelEntity()] = {};
2894
2905
  }
2895
- this.registries[this.entity][key] = attribute;
2906
+ this.registries[this.modelEntity()][key] = attribute;
2896
2907
  return this;
2897
2908
  }
2898
2909
  /**
2899
2910
  * Set delete behaviour for relation field
2900
2911
  */
2901
2912
  static setFieldDeleteMode(key, mode) {
2902
- this.fieldsOnDelete[this.entity] = this.fieldsOnDelete[this.entity] ?? {};
2903
- this.fieldsOnDelete[this.entity][key] = mode;
2913
+ this.fieldsOnDelete[this.modelEntity()] = this.fieldsOnDelete[this.modelEntity()] ?? {};
2914
+ this.fieldsOnDelete[this.modelEntity()][key] = mode;
2904
2915
  return this;
2905
2916
  }
2906
2917
  /**
@@ -3133,7 +3144,7 @@ class Model {
3133
3144
  * Get the namespace.
3134
3145
  */
3135
3146
  $namespace() {
3136
- return this.$self().namespace ?? config.model.namespace;
3147
+ return this.$self().usedNamespace();
3137
3148
  }
3138
3149
  /**
3139
3150
  * Get the store name.
@@ -3147,6 +3158,18 @@ class Model {
3147
3158
  $baseEntity() {
3148
3159
  return this.$self().baseEntity ?? this.$entity();
3149
3160
  }
3161
+ /**
3162
+ * Get the base namespace for this model.
3163
+ */
3164
+ $baseNamespace() {
3165
+ return this.$self().baseNamespace ?? this.$namespace();
3166
+ }
3167
+ /**
3168
+ * Get the model entity for this model.
3169
+ */
3170
+ $modelEntity() {
3171
+ return this.$self().modelEntity();
3172
+ }
3150
3173
  /**
3151
3174
  * Get the type key for this model.
3152
3175
  */
@@ -3175,7 +3198,7 @@ class Model {
3175
3198
  * Get the model fields for this model.
3176
3199
  */
3177
3200
  $fields() {
3178
- return this.$self().schemas[this.$entity()];
3201
+ return this.$self().schemas[this.$modelEntity()];
3179
3202
  }
3180
3203
  /**
3181
3204
  * Get the model hidden fields
@@ -3202,8 +3225,8 @@ class Model {
3202
3225
  * Bootstrap this model.
3203
3226
  */
3204
3227
  $boot() {
3205
- if (!this.$self().booted[this.$entity()]) {
3206
- this.$self().booted[this.$entity()] = true;
3228
+ if (!this.$self().booted[this.$modelEntity()]) {
3229
+ this.$self().booted[this.$modelEntity()] = true;
3207
3230
  this.$initializeSchema();
3208
3231
  }
3209
3232
  }
@@ -3520,6 +3543,10 @@ __publicField(Model, "entity");
3520
3543
  * The reference to the base entity name if the class extends a base entity.
3521
3544
  */
3522
3545
  __publicField(Model, "baseEntity");
3546
+ /**
3547
+ * The reference to the base namespace if the class extends a base with a different namespace.
3548
+ */
3549
+ __publicField(Model, "baseNamespace");
3523
3550
  /**
3524
3551
  * Define a namespace if you have multiple equal entity names.
3525
3552
  * Resulting in "{namespace}/{entity}"
@@ -1,4 +1,4 @@
1
- import { $ as CastAttribute, aj as NanoidOptions, m as ModelFields, af as PropertyDecorator } from '../shared/pinia-orm.677cd0bd.cjs';
1
+ import { a0 as CastAttribute, aq as NanoidOptions, m as ModelFields, an as PropertyDecorator } from '../shared/pinia-orm.b5d0a9df.cjs';
2
2
  import 'pinia';
3
3
  import '@pinia-orm/normalizr';
4
4
  import '@/composables';