@verdant-web/store 5.0.2 → 5.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.
@@ -1,4 +1,4 @@
1
- import { StorageSchema, AuthorizationKey } from '@verdant-web/common';
1
+ import { AuthorizationKey, StorageSchema } from '@verdant-web/common';
2
2
  import { EntityStore } from '../entities/EntityStore.js';
3
3
  import { ObjectEntity } from '../index.js';
4
4
  /**
@@ -27,9 +27,20 @@ export declare class DocumentManager<Schema extends StorageSchema<any>> {
27
27
  deleteAllFromCollection: (collection: string, ids: string[], options?: {
28
28
  undoable?: boolean;
29
29
  }) => Promise<void>;
30
- clone: (collection: string, entity: ObjectEntity<any, any>, options?: {
30
+ /**
31
+ * Creates a clone of the given document entity, returning a new
32
+ * document entity in the given collection. The primary key of
33
+ * the original document will be regenerated automatically and
34
+ * access will be copied from the original. Both can be overridden
35
+ * explicitly via options.
36
+ */
37
+ clone: <TInit>(collection: string, entity: ObjectEntity<TInit, any>, options?: {
31
38
  undoable?: boolean;
32
39
  access?: AuthorizationKey;
33
40
  primaryKey?: string;
41
+ /**
42
+ * Modify the data before cloning
43
+ */
44
+ update?: (init: TInit) => TInit;
34
45
  }) => Promise<any>;
35
46
  }
@@ -1,4 +1,4 @@
1
- import { addFieldDefaults, constrainEntity, assert, createOid, isRootOid, } from '@verdant-web/common';
1
+ import { addFieldDefaults, assert, constrainEntity, createOid, isRootOid, } from '@verdant-web/common';
2
2
  /**
3
3
  * Exposes functionality for creating documents,
4
4
  * the only mutation which is available as an entry
@@ -53,6 +53,13 @@ export class DocumentManager {
53
53
  this.deleteAllFromCollection = async (collection, ids, options = {}) => {
54
54
  return this.entities.deleteAll(ids.map((primaryKey) => createOid(collection, primaryKey)), options);
55
55
  };
56
+ /**
57
+ * Creates a clone of the given document entity, returning a new
58
+ * document entity in the given collection. The primary key of
59
+ * the original document will be regenerated automatically and
60
+ * access will be copied from the original. Both can be overridden
61
+ * explicitly via options.
62
+ */
56
63
  this.clone = async (collection, entity, options = {}) => {
57
64
  if (!isRootOid(entity.uid)) {
58
65
  throw new Error('Cannot clone non-root documents');
@@ -70,7 +77,12 @@ export class DocumentManager {
70
77
  }
71
78
  snapshot[collectionSchema.primaryKey] = options.primaryKey;
72
79
  }
73
- return this.create(collection, snapshot, options);
80
+ // copy authorization of original unless user supplied override
81
+ if (entity.access && !options.access) {
82
+ options.access = entity.access;
83
+ }
84
+ const final = options.update ? options.update(snapshot) : snapshot;
85
+ return this.create(collection, final, options);
74
86
  };
75
87
  }
76
88
  }
@@ -1 +1 @@
1
- {"version":3,"file":"DocumentManager.js","sourceRoot":"","sources":["../../../src/entities/DocumentManager.ts"],"names":[],"mappings":"AAAA,OAAO,EACN,gBAAgB,EAChB,eAAe,EACf,MAAM,EACN,SAAS,EAKT,SAAS,GAET,MAAM,qBAAqB,CAAC;AAI7B;;;;GAIG;AACH,MAAM,OAAO,eAAe;IAC3B,YACS,MAAc,EACd,QAAqB;QADrB,WAAM,GAAN,MAAM,CAAQ;QACd,aAAQ,GAAR,QAAQ,CAAa;QAGtB,WAAM,GAAG,CAAC,UAAkB,EAAE,IAAS,EAAE,EAAE;YAClD,MAAM,cAAc,GAAG,IAAI,CAAC,MAAM,CAAC,WAAW,CAAC,UAAU,CAAC;iBACxD,UAGD,CAAC;YACF,MAAM,UAAU,GAAG,IAAI,CAAC,cAAc,CAAC,CAAC;YACxC,MAAM,CACL,UAAU,EACV,qCAAqC,cAAc,CAAC,QAAQ,EAAE,UAAU,IAAI,CAAC,SAAS,CACrF,IAAI,CACJ,GAAG,CACJ,CAAC;YACF,OAAO,SAAS,CAAC,UAAU,EAAE,UAAU,CAAC,CAAC;QAC1C,CAAC,CAAC;QAEM,gBAAW,GAAG,CAAC,cAAsB,EAAE,IAAS,EAAE,EAAE;YAC3D,MAAM,UAAU,GAAG,IAAI,CAAC,MAAM,CAAC,WAAW,CACzC,cAAc,CACa,CAAC;YAC7B,OAAO,gBAAgB,CAAC,UAA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AmE;YACnE,oCAAoC;YACpC,IAAI,CAAC,gBAAgB,CAAC,MAAM,CAAC,gBAAgB,CAAC,UAAU,CAAC,CAAC,OAAO,EAAE,CAAC;gBACnE,IAAI,CAAC,OAAO,CAAC,UAAU,EAAE,CAAC;oBACzB,MAAM,IAAI,KAAK,CACd,0CAA0C,UAAU,mHAAmH,CACvK,CAAC;gBACH,CAAC;gBACD,QAAQ,CAAC,gBAAgB,CAAC,UAAU,CAAC,GAAG,OAAO,CAAC,UAAU,CAAC;YAC5D,CAAC;YACD,+DAA+D;YAC/D,IAAI,MAAM,CAAC,MAAM,IAAI,CAAC,OAAO,CAAC,MAAM,EAAE,CAAC;gBACtC,OAAO,CAAC,MAAM,GAAG,MAAM,CAAC,MAAM,CAAC;YAChC,CAAC;YACD,MAAM,KAAK,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC,CAAC,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC;YACnE,OAAO,IAAI,CAAC,MAAM,CAAC,UAAU,EAAE,KAAK,EAAE,OAAO,CAAC,CAAC;QAChD,CAAC,CAAC;IA7IC,CAAC;CA8IJ"}
@@ -1,4 +1,4 @@
1
- import { ObjectIdentifier, StorageFieldSchema } from '@verdant-web/common';
1
+ import { AuthorizationKey, ObjectIdentifier, StorageFieldSchema } from '@verdant-web/common';
2
2
  import type { Entity } from './Entity.js';
3
3
  export type AccessibleEntityProperty<T> = T extends Array<any> ? number : T extends object ? keyof T : never;
4
4
  export type DataFromInit<Init> = Init extends {
@@ -78,7 +78,7 @@ export interface BaseEntity<Init, Value extends BaseEntityValue, Snapshot = Data
78
78
  /** If true, this Entity has authorization rules applied to it. */
79
79
  readonly isAuthorized: boolean;
80
80
  /** The authorization configuration string applied to this entity. */
81
- readonly access: string | undefined;
81
+ readonly access: AuthorizationKey | undefined;
82
82
  readonly invalid: boolean;
83
83
  /** The Verdant store namespace which contains this object */
84
84
  readonly namespace: string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@verdant-web/store",
3
- "version": "5.0.2",
3
+ "version": "5.1.0",
4
4
  "access": "public",
5
5
  "type": "module",
6
6
  "main": "dist/esm/index.js",
@@ -1,14 +1,14 @@
1
1
  import {
2
2
  addFieldDefaults,
3
- constrainEntity,
4
3
  assert,
4
+ AuthorizationKey,
5
+ constrainEntity,
5
6
  createOid,
7
+ isRootOid,
6
8
  SchemaCollection,
7
9
  StorageCollectionSchema,
8
10
  StorageDocument,
9
11
  StorageSchema,
10
- isRootOid,
11
- AuthorizationKey,
12
12
  } from '@verdant-web/common';
13
13
  import { EntityCreateOptions, EntityStore } from '../entities/EntityStore.js';
14
14
  import { ObjectEntity } from '../index.js';
@@ -119,20 +119,31 @@ export class DocumentManager<Schema extends StorageSchema<any>> {
119
119
  );
120
120
  };
121
121
 
122
- clone = async (
122
+ /**
123
+ * Creates a clone of the given document entity, returning a new
124
+ * document entity in the given collection. The primary key of
125
+ * the original document will be regenerated automatically and
126
+ * access will be copied from the original. Both can be overridden
127
+ * explicitly via options.
128
+ */
129
+ clone = async <TInit>(
123
130
  collection: string,
124
- entity: ObjectEntity<any, any>,
131
+ entity: ObjectEntity<TInit, any>,
125
132
  options: {
126
133
  undoable?: boolean;
127
134
  access?: AuthorizationKey;
128
135
  primaryKey?: string;
136
+ /**
137
+ * Modify the data before cloning
138
+ */
139
+ update?: (init: TInit) => TInit;
129
140
  } = {},
130
141
  ) => {
131
142
  if (!isRootOid(entity.uid)) {
132
143
  throw new Error('Cannot clone non-root documents');
133
144
  }
134
145
  // take the entity snapshot
135
- const snapshot = entity.getSnapshot();
146
+ const snapshot = entity.getSnapshot() as any;
136
147
  // remove the primary key
137
148
  const collectionSchema = this.schema.collections[collection];
138
149
  delete snapshot[collectionSchema.primaryKey];
@@ -146,6 +157,11 @@ export class DocumentManager<Schema extends StorageSchema<any>> {
146
157
  }
147
158
  snapshot[collectionSchema.primaryKey] = options.primaryKey;
148
159
  }
149
- return this.create(collection, snapshot, options);
160
+ // copy authorization of original unless user supplied override
161
+ if (entity.access && !options.access) {
162
+ options.access = entity.access;
163
+ }
164
+ const final = options.update ? options.update(snapshot) : snapshot;
165
+ return this.create(collection, final, options);
150
166
  };
151
167
  }
@@ -1,4 +1,8 @@
1
- import { ObjectIdentifier, StorageFieldSchema } from '@verdant-web/common';
1
+ import {
2
+ AuthorizationKey,
3
+ ObjectIdentifier,
4
+ StorageFieldSchema,
5
+ } from '@verdant-web/common';
2
6
  import type { Entity } from './Entity.js';
3
7
 
4
8
  export type AccessibleEntityProperty<T> =
@@ -112,7 +116,7 @@ export interface BaseEntity<
112
116
  /** If true, this Entity has authorization rules applied to it. */
113
117
  readonly isAuthorized: boolean;
114
118
  /** The authorization configuration string applied to this entity. */
115
- readonly access: string | undefined;
119
+ readonly access: AuthorizationKey | undefined;
116
120
  readonly invalid: boolean;
117
121
  /** The Verdant store namespace which contains this object */
118
122
  readonly namespace: string;