@jaypie/dynamodb 0.0.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/LICENSE.txt +21 -0
- package/README.md +15 -0
- package/dist/cjs/client.d.ts +27 -0
- package/dist/cjs/constants.d.ts +9 -0
- package/dist/cjs/entities.d.ts +71 -0
- package/dist/cjs/index.cjs +483 -0
- package/dist/cjs/index.cjs.map +1 -0
- package/dist/cjs/index.d.ts +7 -0
- package/dist/cjs/keyBuilders.d.ts +59 -0
- package/dist/cjs/queries.d.ts +48 -0
- package/dist/cjs/types.d.ts +142 -0
- package/dist/esm/client.d.ts +27 -0
- package/dist/esm/constants.d.ts +9 -0
- package/dist/esm/entities.d.ts +71 -0
- package/dist/esm/index.d.ts +7 -0
- package/dist/esm/index.js +450 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/keyBuilders.d.ts +59 -0
- package/dist/esm/queries.d.ts +48 -0
- package/dist/esm/types.d.ts +142 -0
- package/package.json +52 -0
package/LICENSE.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Finlayson Studio, LLC
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
# Jaypie DynamoDB
|
|
2
|
+
|
|
3
|
+
DynamoDB single-table storage utilities for Jaypie applications.
|
|
4
|
+
|
|
5
|
+
See [Jaypie](https://github.com/finlaysonstudio/jaypie) for usage.
|
|
6
|
+
|
|
7
|
+
## Changelog
|
|
8
|
+
|
|
9
|
+
| Date | Version | Summary |
|
|
10
|
+
| ---------- | ------- | -------------- |
|
|
11
|
+
| 1/07/2026 | 0.1.0 | Initial commit |
|
|
12
|
+
|
|
13
|
+
## License
|
|
14
|
+
|
|
15
|
+
[MIT License](./LICENSE.txt). Published by Finlayson Studio
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { DynamoDBDocumentClient } from "@aws-sdk/lib-dynamodb";
|
|
2
|
+
import type { DynamoClientConfig } from "./types.js";
|
|
3
|
+
/**
|
|
4
|
+
* Initialize the DynamoDB client
|
|
5
|
+
* Must be called once at application startup before using query functions
|
|
6
|
+
*
|
|
7
|
+
* @param config - Client configuration
|
|
8
|
+
*/
|
|
9
|
+
export declare function initClient(config: DynamoClientConfig): void;
|
|
10
|
+
/**
|
|
11
|
+
* Get the initialized DynamoDB Document Client
|
|
12
|
+
* @throws ConfigurationError if client has not been initialized
|
|
13
|
+
*/
|
|
14
|
+
export declare function getDocClient(): DynamoDBDocumentClient;
|
|
15
|
+
/**
|
|
16
|
+
* Get the configured table name
|
|
17
|
+
* @throws ConfigurationError if client has not been initialized
|
|
18
|
+
*/
|
|
19
|
+
export declare function getTableName(): string;
|
|
20
|
+
/**
|
|
21
|
+
* Check if the client has been initialized
|
|
22
|
+
*/
|
|
23
|
+
export declare function isInitialized(): boolean;
|
|
24
|
+
/**
|
|
25
|
+
* Reset the client state (primarily for testing)
|
|
26
|
+
*/
|
|
27
|
+
export declare function resetClient(): void;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export declare const APEX = "@";
|
|
2
|
+
export declare const SEPARATOR = "#";
|
|
3
|
+
export declare const INDEX_ALIAS = "indexAlias";
|
|
4
|
+
export declare const INDEX_CLASS = "indexClass";
|
|
5
|
+
export declare const INDEX_OU = "indexOu";
|
|
6
|
+
export declare const INDEX_TYPE = "indexType";
|
|
7
|
+
export declare const INDEX_XID = "indexXid";
|
|
8
|
+
export declare const ARCHIVED_SUFFIX = "#archived";
|
|
9
|
+
export declare const DELETED_SUFFIX = "#deleted";
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import type { FabricEntity } from "./types.js";
|
|
2
|
+
/**
|
|
3
|
+
* Parameters for getEntity
|
|
4
|
+
*/
|
|
5
|
+
export interface GetEntityParams {
|
|
6
|
+
/** Entity ID (sort key) */
|
|
7
|
+
id: string;
|
|
8
|
+
/** Entity model (partition key) */
|
|
9
|
+
model: string;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Parameters for putEntity
|
|
13
|
+
*/
|
|
14
|
+
export interface PutEntityParams<T extends FabricEntity> {
|
|
15
|
+
/** The entity to save */
|
|
16
|
+
entity: T;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Parameters for updateEntity
|
|
20
|
+
*/
|
|
21
|
+
export interface UpdateEntityParams<T extends FabricEntity> {
|
|
22
|
+
/** The entity with updated fields */
|
|
23
|
+
entity: T;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Parameters for deleteEntity (soft delete)
|
|
27
|
+
*/
|
|
28
|
+
export interface DeleteEntityParams {
|
|
29
|
+
/** Entity ID (sort key) */
|
|
30
|
+
id: string;
|
|
31
|
+
/** Entity model (partition key) */
|
|
32
|
+
model: string;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Parameters for archiveEntity
|
|
36
|
+
*/
|
|
37
|
+
export interface ArchiveEntityParams {
|
|
38
|
+
/** Entity ID (sort key) */
|
|
39
|
+
id: string;
|
|
40
|
+
/** Entity model (partition key) */
|
|
41
|
+
model: string;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Get a single entity by primary key
|
|
45
|
+
*/
|
|
46
|
+
export declare function getEntity<T extends FabricEntity = FabricEntity>(params: GetEntityParams): Promise<T | null>;
|
|
47
|
+
/**
|
|
48
|
+
* Put (create or replace) an entity
|
|
49
|
+
* Auto-populates GSI index keys via indexEntity
|
|
50
|
+
*/
|
|
51
|
+
export declare function putEntity<T extends FabricEntity>(params: PutEntityParams<T>): Promise<T>;
|
|
52
|
+
/**
|
|
53
|
+
* Update an existing entity
|
|
54
|
+
* Auto-populates GSI index keys and sets updatedAt
|
|
55
|
+
*/
|
|
56
|
+
export declare function updateEntity<T extends FabricEntity>(params: UpdateEntityParams<T>): Promise<T>;
|
|
57
|
+
/**
|
|
58
|
+
* Soft delete an entity by setting deletedAt timestamp
|
|
59
|
+
* Re-indexes with appropriate suffix based on archived/deleted state
|
|
60
|
+
*/
|
|
61
|
+
export declare function deleteEntity(params: DeleteEntityParams): Promise<boolean>;
|
|
62
|
+
/**
|
|
63
|
+
* Archive an entity by setting archivedAt timestamp
|
|
64
|
+
* Re-indexes with appropriate suffix based on archived/deleted state
|
|
65
|
+
*/
|
|
66
|
+
export declare function archiveEntity(params: ArchiveEntityParams): Promise<boolean>;
|
|
67
|
+
/**
|
|
68
|
+
* Hard delete an entity (permanently removes from table)
|
|
69
|
+
* Use with caution - prefer deleteEntity for soft delete
|
|
70
|
+
*/
|
|
71
|
+
export declare function destroyEntity(params: DeleteEntityParams): Promise<boolean>;
|
|
@@ -0,0 +1,483 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var clientDynamodb = require('@aws-sdk/client-dynamodb');
|
|
4
|
+
var libDynamodb = require('@aws-sdk/lib-dynamodb');
|
|
5
|
+
var errors = require('@jaypie/errors');
|
|
6
|
+
|
|
7
|
+
const DEFAULT_REGION = "us-east-1";
|
|
8
|
+
const LOCAL_CREDENTIALS = {
|
|
9
|
+
accessKeyId: "local",
|
|
10
|
+
secretAccessKey: "local",
|
|
11
|
+
};
|
|
12
|
+
// Module-level state
|
|
13
|
+
let docClient = null;
|
|
14
|
+
let tableName = null;
|
|
15
|
+
/**
|
|
16
|
+
* Check if endpoint indicates local development mode
|
|
17
|
+
*/
|
|
18
|
+
function isLocalEndpoint(endpoint) {
|
|
19
|
+
if (!endpoint)
|
|
20
|
+
return false;
|
|
21
|
+
return endpoint.includes("127.0.0.1") || endpoint.includes("localhost");
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Initialize the DynamoDB client
|
|
25
|
+
* Must be called once at application startup before using query functions
|
|
26
|
+
*
|
|
27
|
+
* @param config - Client configuration
|
|
28
|
+
*/
|
|
29
|
+
function initClient(config) {
|
|
30
|
+
const { endpoint, region = DEFAULT_REGION } = config;
|
|
31
|
+
// Auto-detect local mode and use dummy credentials
|
|
32
|
+
const credentials = config.credentials ??
|
|
33
|
+
(isLocalEndpoint(endpoint) ? LOCAL_CREDENTIALS : undefined);
|
|
34
|
+
const dynamoClient = new clientDynamodb.DynamoDBClient({
|
|
35
|
+
...(credentials && { credentials }),
|
|
36
|
+
...(endpoint && { endpoint }),
|
|
37
|
+
region,
|
|
38
|
+
});
|
|
39
|
+
docClient = libDynamodb.DynamoDBDocumentClient.from(dynamoClient, {
|
|
40
|
+
marshallOptions: {
|
|
41
|
+
removeUndefinedValues: true,
|
|
42
|
+
},
|
|
43
|
+
});
|
|
44
|
+
tableName = config.tableName;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Get the initialized DynamoDB Document Client
|
|
48
|
+
* @throws ConfigurationError if client has not been initialized
|
|
49
|
+
*/
|
|
50
|
+
function getDocClient() {
|
|
51
|
+
if (!docClient) {
|
|
52
|
+
throw new errors.ConfigurationError("DynamoDB client not initialized. Call initClient() first.");
|
|
53
|
+
}
|
|
54
|
+
return docClient;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Get the configured table name
|
|
58
|
+
* @throws ConfigurationError if client has not been initialized
|
|
59
|
+
*/
|
|
60
|
+
function getTableName() {
|
|
61
|
+
if (!tableName) {
|
|
62
|
+
throw new errors.ConfigurationError("DynamoDB client not initialized. Call initClient() first.");
|
|
63
|
+
}
|
|
64
|
+
return tableName;
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Check if the client has been initialized
|
|
68
|
+
*/
|
|
69
|
+
function isInitialized() {
|
|
70
|
+
return docClient !== null && tableName !== null;
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Reset the client state (primarily for testing)
|
|
74
|
+
*/
|
|
75
|
+
function resetClient() {
|
|
76
|
+
docClient = null;
|
|
77
|
+
tableName = null;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
// Primary markers
|
|
81
|
+
const APEX = "@"; // Root-level marker (DynamoDB prohibits empty strings)
|
|
82
|
+
const SEPARATOR = "#"; // Composite key separator
|
|
83
|
+
// GSI names
|
|
84
|
+
const INDEX_ALIAS = "indexAlias";
|
|
85
|
+
const INDEX_CLASS = "indexClass";
|
|
86
|
+
const INDEX_OU = "indexOu";
|
|
87
|
+
const INDEX_TYPE = "indexType";
|
|
88
|
+
const INDEX_XID = "indexXid";
|
|
89
|
+
// Index suffixes for soft state
|
|
90
|
+
const ARCHIVED_SUFFIX = "#archived";
|
|
91
|
+
const DELETED_SUFFIX = "#deleted";
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Build the indexOu key for hierarchical queries
|
|
95
|
+
* @param ou - The organizational unit (APEX or "{parent.model}#{parent.id}")
|
|
96
|
+
* @param model - The entity model name
|
|
97
|
+
* @returns Composite key: "{ou}#{model}"
|
|
98
|
+
*/
|
|
99
|
+
function buildIndexOu(ou, model) {
|
|
100
|
+
return `${ou}${SEPARATOR}${model}`;
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Build the indexAlias key for human-friendly lookups
|
|
104
|
+
* @param ou - The organizational unit
|
|
105
|
+
* @param model - The entity model name
|
|
106
|
+
* @param alias - The human-friendly alias
|
|
107
|
+
* @returns Composite key: "{ou}#{model}#{alias}"
|
|
108
|
+
*/
|
|
109
|
+
function buildIndexAlias(ou, model, alias) {
|
|
110
|
+
return `${ou}${SEPARATOR}${model}${SEPARATOR}${alias}`;
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* Build the indexClass key for category filtering
|
|
114
|
+
* @param ou - The organizational unit
|
|
115
|
+
* @param model - The entity model name
|
|
116
|
+
* @param recordClass - The category classification
|
|
117
|
+
* @returns Composite key: "{ou}#{model}#{class}"
|
|
118
|
+
*/
|
|
119
|
+
function buildIndexClass(ou, model, recordClass) {
|
|
120
|
+
return `${ou}${SEPARATOR}${model}${SEPARATOR}${recordClass}`;
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* Build the indexType key for type filtering
|
|
124
|
+
* @param ou - The organizational unit
|
|
125
|
+
* @param model - The entity model name
|
|
126
|
+
* @param type - The type classification
|
|
127
|
+
* @returns Composite key: "{ou}#{model}#{type}"
|
|
128
|
+
*/
|
|
129
|
+
function buildIndexType(ou, model, type) {
|
|
130
|
+
return `${ou}${SEPARATOR}${model}${SEPARATOR}${type}`;
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* Build the indexXid key for external ID lookups
|
|
134
|
+
* @param ou - The organizational unit
|
|
135
|
+
* @param model - The entity model name
|
|
136
|
+
* @param xid - The external ID
|
|
137
|
+
* @returns Composite key: "{ou}#{model}#{xid}"
|
|
138
|
+
*/
|
|
139
|
+
function buildIndexXid(ou, model, xid) {
|
|
140
|
+
return `${ou}${SEPARATOR}${model}${SEPARATOR}${xid}`;
|
|
141
|
+
}
|
|
142
|
+
/**
|
|
143
|
+
* Calculate the organizational unit from a parent reference
|
|
144
|
+
* @param parent - Optional parent entity reference
|
|
145
|
+
* @returns APEX ("@") if no parent, otherwise "{parent.model}#{parent.id}"
|
|
146
|
+
*/
|
|
147
|
+
function calculateOu(parent) {
|
|
148
|
+
if (!parent) {
|
|
149
|
+
return APEX;
|
|
150
|
+
}
|
|
151
|
+
return `${parent.model}${SEPARATOR}${parent.id}`;
|
|
152
|
+
}
|
|
153
|
+
/**
|
|
154
|
+
* Auto-populate GSI index keys on an entity
|
|
155
|
+
* - indexOu is always populated from ou + model
|
|
156
|
+
* - indexAlias is populated only when alias is present
|
|
157
|
+
* - indexClass is populated only when class is present
|
|
158
|
+
* - indexType is populated only when type is present
|
|
159
|
+
* - indexXid is populated only when xid is present
|
|
160
|
+
*
|
|
161
|
+
* @param entity - The entity to populate index keys for
|
|
162
|
+
* @param suffix - Optional suffix to append to all index keys (e.g., "#deleted", "#archived")
|
|
163
|
+
* @returns The entity with populated index keys
|
|
164
|
+
*/
|
|
165
|
+
function indexEntity(entity, suffix = "") {
|
|
166
|
+
const result = { ...entity };
|
|
167
|
+
// indexOu is always set (from ou + model)
|
|
168
|
+
result.indexOu = buildIndexOu(entity.ou, entity.model) + suffix;
|
|
169
|
+
// Optional indexes - only set when the source field is present
|
|
170
|
+
if (entity.alias !== undefined) {
|
|
171
|
+
result.indexAlias =
|
|
172
|
+
buildIndexAlias(entity.ou, entity.model, entity.alias) + suffix;
|
|
173
|
+
}
|
|
174
|
+
if (entity.class !== undefined) {
|
|
175
|
+
result.indexClass =
|
|
176
|
+
buildIndexClass(entity.ou, entity.model, entity.class) + suffix;
|
|
177
|
+
}
|
|
178
|
+
if (entity.type !== undefined) {
|
|
179
|
+
result.indexType =
|
|
180
|
+
buildIndexType(entity.ou, entity.model, entity.type) + suffix;
|
|
181
|
+
}
|
|
182
|
+
if (entity.xid !== undefined) {
|
|
183
|
+
result.indexXid =
|
|
184
|
+
buildIndexXid(entity.ou, entity.model, entity.xid) + suffix;
|
|
185
|
+
}
|
|
186
|
+
return result;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* Get a single entity by primary key
|
|
191
|
+
*/
|
|
192
|
+
async function getEntity(params) {
|
|
193
|
+
const { id, model } = params;
|
|
194
|
+
const docClient = getDocClient();
|
|
195
|
+
const tableName = getTableName();
|
|
196
|
+
const command = new libDynamodb.GetCommand({
|
|
197
|
+
Key: { id, model },
|
|
198
|
+
TableName: tableName,
|
|
199
|
+
});
|
|
200
|
+
const response = await docClient.send(command);
|
|
201
|
+
return response.Item ?? null;
|
|
202
|
+
}
|
|
203
|
+
/**
|
|
204
|
+
* Put (create or replace) an entity
|
|
205
|
+
* Auto-populates GSI index keys via indexEntity
|
|
206
|
+
*/
|
|
207
|
+
async function putEntity(params) {
|
|
208
|
+
const { entity } = params;
|
|
209
|
+
const docClient = getDocClient();
|
|
210
|
+
const tableName = getTableName();
|
|
211
|
+
// Auto-populate index keys
|
|
212
|
+
const indexedEntity = indexEntity(entity);
|
|
213
|
+
const command = new libDynamodb.PutCommand({
|
|
214
|
+
Item: indexedEntity,
|
|
215
|
+
TableName: tableName,
|
|
216
|
+
});
|
|
217
|
+
await docClient.send(command);
|
|
218
|
+
return indexedEntity;
|
|
219
|
+
}
|
|
220
|
+
/**
|
|
221
|
+
* Update an existing entity
|
|
222
|
+
* Auto-populates GSI index keys and sets updatedAt
|
|
223
|
+
*/
|
|
224
|
+
async function updateEntity(params) {
|
|
225
|
+
const { entity } = params;
|
|
226
|
+
const docClient = getDocClient();
|
|
227
|
+
const tableName = getTableName();
|
|
228
|
+
// Update timestamp and re-index
|
|
229
|
+
const updatedEntity = indexEntity({
|
|
230
|
+
...entity,
|
|
231
|
+
updatedAt: new Date().toISOString(),
|
|
232
|
+
});
|
|
233
|
+
const command = new libDynamodb.PutCommand({
|
|
234
|
+
Item: updatedEntity,
|
|
235
|
+
TableName: tableName,
|
|
236
|
+
});
|
|
237
|
+
await docClient.send(command);
|
|
238
|
+
return updatedEntity;
|
|
239
|
+
}
|
|
240
|
+
/**
|
|
241
|
+
* Calculate suffix based on entity's archived/deleted state
|
|
242
|
+
*/
|
|
243
|
+
function calculateEntitySuffix(entity) {
|
|
244
|
+
const hasArchived = Boolean(entity.archivedAt);
|
|
245
|
+
const hasDeleted = Boolean(entity.deletedAt);
|
|
246
|
+
if (hasArchived && hasDeleted) {
|
|
247
|
+
return ARCHIVED_SUFFIX + DELETED_SUFFIX;
|
|
248
|
+
}
|
|
249
|
+
if (hasArchived) {
|
|
250
|
+
return ARCHIVED_SUFFIX;
|
|
251
|
+
}
|
|
252
|
+
if (hasDeleted) {
|
|
253
|
+
return DELETED_SUFFIX;
|
|
254
|
+
}
|
|
255
|
+
return "";
|
|
256
|
+
}
|
|
257
|
+
/**
|
|
258
|
+
* Soft delete an entity by setting deletedAt timestamp
|
|
259
|
+
* Re-indexes with appropriate suffix based on archived/deleted state
|
|
260
|
+
*/
|
|
261
|
+
async function deleteEntity(params) {
|
|
262
|
+
const { id, model } = params;
|
|
263
|
+
const docClient = getDocClient();
|
|
264
|
+
const tableName = getTableName();
|
|
265
|
+
// Fetch the current entity
|
|
266
|
+
const existing = await getEntity({ id, model });
|
|
267
|
+
if (!existing) {
|
|
268
|
+
return false;
|
|
269
|
+
}
|
|
270
|
+
const now = new Date().toISOString();
|
|
271
|
+
// Build updated entity with deletedAt
|
|
272
|
+
const updatedEntity = {
|
|
273
|
+
...existing,
|
|
274
|
+
deletedAt: now,
|
|
275
|
+
updatedAt: now,
|
|
276
|
+
};
|
|
277
|
+
// Calculate suffix based on combined state (may already be archived)
|
|
278
|
+
const suffix = calculateEntitySuffix(updatedEntity);
|
|
279
|
+
const deletedEntity = indexEntity(updatedEntity, suffix);
|
|
280
|
+
const command = new libDynamodb.PutCommand({
|
|
281
|
+
Item: deletedEntity,
|
|
282
|
+
TableName: tableName,
|
|
283
|
+
});
|
|
284
|
+
await docClient.send(command);
|
|
285
|
+
return true;
|
|
286
|
+
}
|
|
287
|
+
/**
|
|
288
|
+
* Archive an entity by setting archivedAt timestamp
|
|
289
|
+
* Re-indexes with appropriate suffix based on archived/deleted state
|
|
290
|
+
*/
|
|
291
|
+
async function archiveEntity(params) {
|
|
292
|
+
const { id, model } = params;
|
|
293
|
+
const docClient = getDocClient();
|
|
294
|
+
const tableName = getTableName();
|
|
295
|
+
// Fetch the current entity
|
|
296
|
+
const existing = await getEntity({ id, model });
|
|
297
|
+
if (!existing) {
|
|
298
|
+
return false;
|
|
299
|
+
}
|
|
300
|
+
const now = new Date().toISOString();
|
|
301
|
+
// Build updated entity with archivedAt
|
|
302
|
+
const updatedEntity = {
|
|
303
|
+
...existing,
|
|
304
|
+
archivedAt: now,
|
|
305
|
+
updatedAt: now,
|
|
306
|
+
};
|
|
307
|
+
// Calculate suffix based on combined state (may already be deleted)
|
|
308
|
+
const suffix = calculateEntitySuffix(updatedEntity);
|
|
309
|
+
const archivedEntity = indexEntity(updatedEntity, suffix);
|
|
310
|
+
const command = new libDynamodb.PutCommand({
|
|
311
|
+
Item: archivedEntity,
|
|
312
|
+
TableName: tableName,
|
|
313
|
+
});
|
|
314
|
+
await docClient.send(command);
|
|
315
|
+
return true;
|
|
316
|
+
}
|
|
317
|
+
/**
|
|
318
|
+
* Hard delete an entity (permanently removes from table)
|
|
319
|
+
* Use with caution - prefer deleteEntity for soft delete
|
|
320
|
+
*/
|
|
321
|
+
async function destroyEntity(params) {
|
|
322
|
+
const { id, model } = params;
|
|
323
|
+
const docClient = getDocClient();
|
|
324
|
+
const tableName = getTableName();
|
|
325
|
+
const command = new libDynamodb.DeleteCommand({
|
|
326
|
+
Key: { id, model },
|
|
327
|
+
TableName: tableName,
|
|
328
|
+
});
|
|
329
|
+
await docClient.send(command);
|
|
330
|
+
return true;
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
/**
|
|
334
|
+
* Calculate the suffix based on archived/deleted flags
|
|
335
|
+
* When both are true, returns combined suffix (archived first, alphabetically)
|
|
336
|
+
*/
|
|
337
|
+
function calculateSuffix({ archived, deleted, }) {
|
|
338
|
+
if (archived && deleted) {
|
|
339
|
+
return ARCHIVED_SUFFIX + DELETED_SUFFIX;
|
|
340
|
+
}
|
|
341
|
+
if (archived) {
|
|
342
|
+
return ARCHIVED_SUFFIX;
|
|
343
|
+
}
|
|
344
|
+
if (deleted) {
|
|
345
|
+
return DELETED_SUFFIX;
|
|
346
|
+
}
|
|
347
|
+
return "";
|
|
348
|
+
}
|
|
349
|
+
/**
|
|
350
|
+
* Execute a GSI query with common options
|
|
351
|
+
*/
|
|
352
|
+
async function executeQuery(indexName, keyValue, options = {}) {
|
|
353
|
+
const { ascending = false, limit, startKey } = options;
|
|
354
|
+
const docClient = getDocClient();
|
|
355
|
+
const tableName = getTableName();
|
|
356
|
+
const command = new libDynamodb.QueryCommand({
|
|
357
|
+
ExclusiveStartKey: startKey,
|
|
358
|
+
ExpressionAttributeNames: {
|
|
359
|
+
"#pk": indexName,
|
|
360
|
+
},
|
|
361
|
+
ExpressionAttributeValues: {
|
|
362
|
+
":pkValue": keyValue,
|
|
363
|
+
},
|
|
364
|
+
IndexName: indexName,
|
|
365
|
+
KeyConditionExpression: "#pk = :pkValue",
|
|
366
|
+
...(limit && { Limit: limit }),
|
|
367
|
+
ScanIndexForward: ascending,
|
|
368
|
+
TableName: tableName,
|
|
369
|
+
});
|
|
370
|
+
const response = await docClient.send(command);
|
|
371
|
+
return {
|
|
372
|
+
items: (response.Items ?? []),
|
|
373
|
+
lastEvaluatedKey: response.LastEvaluatedKey,
|
|
374
|
+
};
|
|
375
|
+
}
|
|
376
|
+
/**
|
|
377
|
+
* Query entities by organizational unit (parent hierarchy)
|
|
378
|
+
* Uses indexOu GSI
|
|
379
|
+
*
|
|
380
|
+
* @param params.archived - Query archived entities instead of active ones
|
|
381
|
+
* @param params.deleted - Query deleted entities instead of active ones
|
|
382
|
+
* @throws ConfigurationError if both archived and deleted are true
|
|
383
|
+
*/
|
|
384
|
+
async function queryByOu(params) {
|
|
385
|
+
const { archived, deleted, model, ou, ...options } = params;
|
|
386
|
+
const suffix = calculateSuffix({ archived, deleted });
|
|
387
|
+
const keyValue = buildIndexOu(ou, model) + suffix;
|
|
388
|
+
return executeQuery(INDEX_OU, keyValue, options);
|
|
389
|
+
}
|
|
390
|
+
/**
|
|
391
|
+
* Query a single entity by human-friendly alias
|
|
392
|
+
* Uses indexAlias GSI
|
|
393
|
+
*
|
|
394
|
+
* @param params.archived - Query archived entities instead of active ones
|
|
395
|
+
* @param params.deleted - Query deleted entities instead of active ones
|
|
396
|
+
* @throws ConfigurationError if both archived and deleted are true
|
|
397
|
+
* @returns The matching entity or null if not found
|
|
398
|
+
*/
|
|
399
|
+
async function queryByAlias(params) {
|
|
400
|
+
const { alias, archived, deleted, model, ou } = params;
|
|
401
|
+
const suffix = calculateSuffix({ archived, deleted });
|
|
402
|
+
const keyValue = buildIndexAlias(ou, model, alias) + suffix;
|
|
403
|
+
const result = await executeQuery(INDEX_ALIAS, keyValue, { limit: 1 });
|
|
404
|
+
return result.items[0] ?? null;
|
|
405
|
+
}
|
|
406
|
+
/**
|
|
407
|
+
* Query entities by category classification
|
|
408
|
+
* Uses indexClass GSI
|
|
409
|
+
*
|
|
410
|
+
* @param params.archived - Query archived entities instead of active ones
|
|
411
|
+
* @param params.deleted - Query deleted entities instead of active ones
|
|
412
|
+
* @throws ConfigurationError if both archived and deleted are true
|
|
413
|
+
*/
|
|
414
|
+
async function queryByClass(params) {
|
|
415
|
+
const { archived, deleted, model, ou, recordClass, ...options } = params;
|
|
416
|
+
const suffix = calculateSuffix({ archived, deleted });
|
|
417
|
+
const keyValue = buildIndexClass(ou, model, recordClass) + suffix;
|
|
418
|
+
return executeQuery(INDEX_CLASS, keyValue, options);
|
|
419
|
+
}
|
|
420
|
+
/**
|
|
421
|
+
* Query entities by type classification
|
|
422
|
+
* Uses indexType GSI
|
|
423
|
+
*
|
|
424
|
+
* @param params.archived - Query archived entities instead of active ones
|
|
425
|
+
* @param params.deleted - Query deleted entities instead of active ones
|
|
426
|
+
* @throws ConfigurationError if both archived and deleted are true
|
|
427
|
+
*/
|
|
428
|
+
async function queryByType(params) {
|
|
429
|
+
const { archived, deleted, model, ou, type, ...options } = params;
|
|
430
|
+
const suffix = calculateSuffix({ archived, deleted });
|
|
431
|
+
const keyValue = buildIndexType(ou, model, type) + suffix;
|
|
432
|
+
return executeQuery(INDEX_TYPE, keyValue, options);
|
|
433
|
+
}
|
|
434
|
+
/**
|
|
435
|
+
* Query a single entity by external ID
|
|
436
|
+
* Uses indexXid GSI
|
|
437
|
+
*
|
|
438
|
+
* @param params.archived - Query archived entities instead of active ones
|
|
439
|
+
* @param params.deleted - Query deleted entities instead of active ones
|
|
440
|
+
* @throws ConfigurationError if both archived and deleted are true
|
|
441
|
+
* @returns The matching entity or null if not found
|
|
442
|
+
*/
|
|
443
|
+
async function queryByXid(params) {
|
|
444
|
+
const { archived, deleted, model, ou, xid } = params;
|
|
445
|
+
const suffix = calculateSuffix({ archived, deleted });
|
|
446
|
+
const keyValue = buildIndexXid(ou, model, xid) + suffix;
|
|
447
|
+
const result = await executeQuery(INDEX_XID, keyValue, { limit: 1 });
|
|
448
|
+
return result.items[0] ?? null;
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
exports.APEX = APEX;
|
|
452
|
+
exports.ARCHIVED_SUFFIX = ARCHIVED_SUFFIX;
|
|
453
|
+
exports.DELETED_SUFFIX = DELETED_SUFFIX;
|
|
454
|
+
exports.INDEX_ALIAS = INDEX_ALIAS;
|
|
455
|
+
exports.INDEX_CLASS = INDEX_CLASS;
|
|
456
|
+
exports.INDEX_OU = INDEX_OU;
|
|
457
|
+
exports.INDEX_TYPE = INDEX_TYPE;
|
|
458
|
+
exports.INDEX_XID = INDEX_XID;
|
|
459
|
+
exports.SEPARATOR = SEPARATOR;
|
|
460
|
+
exports.archiveEntity = archiveEntity;
|
|
461
|
+
exports.buildIndexAlias = buildIndexAlias;
|
|
462
|
+
exports.buildIndexClass = buildIndexClass;
|
|
463
|
+
exports.buildIndexOu = buildIndexOu;
|
|
464
|
+
exports.buildIndexType = buildIndexType;
|
|
465
|
+
exports.buildIndexXid = buildIndexXid;
|
|
466
|
+
exports.calculateOu = calculateOu;
|
|
467
|
+
exports.deleteEntity = deleteEntity;
|
|
468
|
+
exports.destroyEntity = destroyEntity;
|
|
469
|
+
exports.getDocClient = getDocClient;
|
|
470
|
+
exports.getEntity = getEntity;
|
|
471
|
+
exports.getTableName = getTableName;
|
|
472
|
+
exports.indexEntity = indexEntity;
|
|
473
|
+
exports.initClient = initClient;
|
|
474
|
+
exports.isInitialized = isInitialized;
|
|
475
|
+
exports.putEntity = putEntity;
|
|
476
|
+
exports.queryByAlias = queryByAlias;
|
|
477
|
+
exports.queryByClass = queryByClass;
|
|
478
|
+
exports.queryByOu = queryByOu;
|
|
479
|
+
exports.queryByType = queryByType;
|
|
480
|
+
exports.queryByXid = queryByXid;
|
|
481
|
+
exports.resetClient = resetClient;
|
|
482
|
+
exports.updateEntity = updateEntity;
|
|
483
|
+
//# sourceMappingURL=index.cjs.map
|