qstd 0.2.27 → 0.2.28
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 +52 -0
- package/dist/server/aws/ddb/domain.d.ts +257 -0
- package/dist/server/aws/ddb/domain.d.ts.map +1 -0
- package/dist/server/aws/ddb/fns.d.ts +25 -0
- package/dist/server/aws/ddb/fns.d.ts.map +1 -0
- package/dist/server/aws/ddb/index.d.ts +4 -0
- package/dist/server/aws/ddb/index.d.ts.map +1 -0
- package/dist/server/aws/ddb/literals.d.ts +30 -0
- package/dist/server/aws/ddb/literals.d.ts.map +1 -0
- package/dist/server/aws/ddb/types.d.ts +216 -0
- package/dist/server/aws/ddb/types.d.ts.map +1 -0
- package/dist/server/index.cjs +601 -2
- package/dist/server/index.d.ts +1 -0
- package/dist/server/index.d.ts.map +1 -1
- package/dist/server/index.js +601 -3
- package/package.json +3 -1
package/dist/server/index.js
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import { format, formatDistanceToNow, formatISO, isSameYear, isSameMonth, isSameDay, addSeconds, addMinutes, addHours, addDays, addWeeks, addBusinessDays, addMonths, addYears } from 'date-fns';
|
|
2
2
|
import { promises } from 'fs';
|
|
3
|
+
import { DynamoDBDocumentClient, ScanCommand, QueryCommand, DeleteCommand, PutCommand, BatchGetCommand, TransactWriteCommand, BatchWriteCommand } from '@aws-sdk/lib-dynamodb';
|
|
4
|
+
import { DynamoDBClient, DeleteTableCommand, DescribeTableCommand, DynamoDBServiceException } from '@aws-sdk/client-dynamodb';
|
|
3
5
|
|
|
4
6
|
var __defProp = Object.defineProperty;
|
|
5
7
|
var __export = (target, all) => {
|
|
@@ -605,8 +607,8 @@ var readFile = async (filePath, encoding = "utf-8") => {
|
|
|
605
607
|
var writeBufferToFile = async (filepath, buffer) => {
|
|
606
608
|
if (!buffer) return;
|
|
607
609
|
const chunks = [];
|
|
608
|
-
for await (const
|
|
609
|
-
chunks.push(
|
|
610
|
+
for await (const chunk3 of buffer) {
|
|
611
|
+
chunks.push(chunk3);
|
|
610
612
|
}
|
|
611
613
|
await promises.writeFile(filepath, Buffer.concat(chunks));
|
|
612
614
|
};
|
|
@@ -614,4 +616,600 @@ var writeFile = async (filepath, content) => {
|
|
|
614
616
|
return promises.writeFile(filepath, content);
|
|
615
617
|
};
|
|
616
618
|
|
|
617
|
-
|
|
619
|
+
// src/server/aws/ddb/index.ts
|
|
620
|
+
var ddb_exports = {};
|
|
621
|
+
__export(ddb_exports, {
|
|
622
|
+
batchDelete: () => batchDelete,
|
|
623
|
+
batchGet: () => batchGet,
|
|
624
|
+
batchWrite: () => batchWrite,
|
|
625
|
+
create: () => create2,
|
|
626
|
+
deleteTable: () => deleteTable,
|
|
627
|
+
find: () => find,
|
|
628
|
+
lsi: () => lsi,
|
|
629
|
+
lsi2: () => lsi2,
|
|
630
|
+
lsiNormalized: () => lsiNormalized,
|
|
631
|
+
lsiPhash: () => lsiPhash,
|
|
632
|
+
lsiUsername: () => lsiUsername,
|
|
633
|
+
remove: () => remove,
|
|
634
|
+
save: () => save,
|
|
635
|
+
tableExists: () => tableExists
|
|
636
|
+
});
|
|
637
|
+
|
|
638
|
+
// src/server/aws/ddb/fns.ts
|
|
639
|
+
var validateFindProps = (props) => {
|
|
640
|
+
const isScan = "scan" in props && props.scan === true;
|
|
641
|
+
if (!props.tableName) {
|
|
642
|
+
throw new Error(`[ddb] "tableName" is required`);
|
|
643
|
+
}
|
|
644
|
+
if (props.limit && props.recursive) {
|
|
645
|
+
throw new Error(`[ddb] "limit" and "recursive" cannot be used together`);
|
|
646
|
+
}
|
|
647
|
+
if (!isScan) {
|
|
648
|
+
const queryProps = props;
|
|
649
|
+
if (!queryProps.pk) {
|
|
650
|
+
throw new Error(`[ddb] [find] "pk" is required for Query mode. Use scan: true to scan without pk.`);
|
|
651
|
+
}
|
|
652
|
+
if (queryProps.sk && "key" in queryProps.sk && queryProps.sk.key && queryProps.sk.key !== "sk" && !queryProps.indexName) {
|
|
653
|
+
throw new Error(
|
|
654
|
+
`[ddb] [find] you provided a custom sk but no indexName. If this is a mistake, change this error to a warn.`
|
|
655
|
+
);
|
|
656
|
+
}
|
|
657
|
+
}
|
|
658
|
+
};
|
|
659
|
+
var buildKeyConditionExpression = (pk, sk, names, values) => {
|
|
660
|
+
const pkKey = pk.key ?? "pk";
|
|
661
|
+
names["#pk"] = pkKey;
|
|
662
|
+
values[":pk"] = pk.value;
|
|
663
|
+
if (!sk) return "#pk = :pk";
|
|
664
|
+
const skName = sk.key ?? "sk";
|
|
665
|
+
names["#sk"] = skName;
|
|
666
|
+
if ("op" in sk && sk.op) {
|
|
667
|
+
switch (sk.op) {
|
|
668
|
+
case "=":
|
|
669
|
+
case ">=":
|
|
670
|
+
case ">":
|
|
671
|
+
case "<=":
|
|
672
|
+
case "<":
|
|
673
|
+
values[":sk"] = sk.value;
|
|
674
|
+
return `#pk = :pk AND #sk ${sk.op} :sk`;
|
|
675
|
+
case "begins_with":
|
|
676
|
+
values[":sk"] = sk.value;
|
|
677
|
+
return `#pk = :pk AND begins_with(#sk, :sk)`;
|
|
678
|
+
case "between": {
|
|
679
|
+
const [from, to] = sk.value;
|
|
680
|
+
values[":skFrom"] = from;
|
|
681
|
+
values[":skTo"] = to;
|
|
682
|
+
return `#pk = :pk AND #sk BETWEEN :skFrom AND :skTo`;
|
|
683
|
+
}
|
|
684
|
+
default:
|
|
685
|
+
throw new Error(`Unsupported SK op: ${JSON.stringify(sk, null, 2)}`);
|
|
686
|
+
}
|
|
687
|
+
}
|
|
688
|
+
if ("value" in sk && sk.value !== void 0) {
|
|
689
|
+
values[":sk"] = sk.value;
|
|
690
|
+
return `#pk = :pk AND #sk = :sk`;
|
|
691
|
+
}
|
|
692
|
+
if ("valueBeginsWith" in sk && sk.valueBeginsWith !== void 0) {
|
|
693
|
+
values[":sk"] = sk.valueBeginsWith;
|
|
694
|
+
return `#pk = :pk AND begins_with(#sk, :sk)`;
|
|
695
|
+
}
|
|
696
|
+
throw new Error(
|
|
697
|
+
`Invalid SK condition: expected 'op' or legacy 'value'/'valueBeginsWith'`
|
|
698
|
+
);
|
|
699
|
+
};
|
|
700
|
+
var buildFilterExpression = (filters, names, values) => {
|
|
701
|
+
if (!filters || filters.length === 0) return void 0;
|
|
702
|
+
const frags = [];
|
|
703
|
+
filters.forEach((f, i) => {
|
|
704
|
+
const nameToken = `#f${i}`;
|
|
705
|
+
names[nameToken] = f.key;
|
|
706
|
+
switch (f.op) {
|
|
707
|
+
case "attribute_exists":
|
|
708
|
+
frags.push(`attribute_exists(${nameToken})`);
|
|
709
|
+
break;
|
|
710
|
+
case "attribute_not_exists":
|
|
711
|
+
frags.push(`attribute_not_exists(${nameToken})`);
|
|
712
|
+
break;
|
|
713
|
+
case "between": {
|
|
714
|
+
const [lo, hi] = f.value;
|
|
715
|
+
const loPh = `:f${i}_lo`;
|
|
716
|
+
const hiPh = `:f${i}_hi`;
|
|
717
|
+
values[loPh] = lo;
|
|
718
|
+
values[hiPh] = hi;
|
|
719
|
+
frags.push(`${nameToken} BETWEEN ${loPh} AND ${hiPh}`);
|
|
720
|
+
break;
|
|
721
|
+
}
|
|
722
|
+
case "in": {
|
|
723
|
+
const arr = f.value;
|
|
724
|
+
if (!arr.length) {
|
|
725
|
+
throw new Error(
|
|
726
|
+
`'in' filter for ${f.key} requires at least one value`
|
|
727
|
+
);
|
|
728
|
+
}
|
|
729
|
+
const phs = arr.map((_, j) => `:f${i}_${j}`);
|
|
730
|
+
arr.forEach((v, j) => {
|
|
731
|
+
const ph = phs[j];
|
|
732
|
+
if (ph) values[ph] = v;
|
|
733
|
+
});
|
|
734
|
+
frags.push(`${nameToken} IN (${phs.join(", ")})`);
|
|
735
|
+
break;
|
|
736
|
+
}
|
|
737
|
+
case "contains":
|
|
738
|
+
case "begins_with": {
|
|
739
|
+
const ph = `:f${i}`;
|
|
740
|
+
values[ph] = f.value;
|
|
741
|
+
frags.push(`${f.op}(${nameToken}, ${ph})`);
|
|
742
|
+
break;
|
|
743
|
+
}
|
|
744
|
+
case "=":
|
|
745
|
+
case "<>":
|
|
746
|
+
case ">":
|
|
747
|
+
case ">=":
|
|
748
|
+
case "<":
|
|
749
|
+
case "<=": {
|
|
750
|
+
const ph = `:f${i}`;
|
|
751
|
+
values[ph] = f.value;
|
|
752
|
+
frags.push(`${nameToken} ${f.op} ${ph}`);
|
|
753
|
+
break;
|
|
754
|
+
}
|
|
755
|
+
}
|
|
756
|
+
});
|
|
757
|
+
return frags.join(" AND ");
|
|
758
|
+
};
|
|
759
|
+
var buildProjectionExpression = (projection, names) => {
|
|
760
|
+
const projectionNames = projection.map((key, idx) => {
|
|
761
|
+
const nameKey = `#proj${idx}`;
|
|
762
|
+
names[nameKey] = key;
|
|
763
|
+
return nameKey;
|
|
764
|
+
});
|
|
765
|
+
return projectionNames.join(", ");
|
|
766
|
+
};
|
|
767
|
+
var sleep3 = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
768
|
+
var backoffDelay = (attempt) => {
|
|
769
|
+
return Math.min(1e3 * Math.pow(2, attempt), 1e4);
|
|
770
|
+
};
|
|
771
|
+
var chunk2 = (arr, size) => {
|
|
772
|
+
return Array.from(
|
|
773
|
+
{ length: Math.ceil(arr.length / size) },
|
|
774
|
+
(_, i) => arr.slice(i * size, i * size + size)
|
|
775
|
+
);
|
|
776
|
+
};
|
|
777
|
+
|
|
778
|
+
// src/server/aws/ddb/domain.ts
|
|
779
|
+
var create2 = (props) => {
|
|
780
|
+
const tableName = props?.tableName;
|
|
781
|
+
const credentials = props?.credentials;
|
|
782
|
+
const client = DynamoDBDocumentClient.from(
|
|
783
|
+
new DynamoDBClient({
|
|
784
|
+
...credentials && { credentials },
|
|
785
|
+
region: "us-east-1"
|
|
786
|
+
}),
|
|
787
|
+
{
|
|
788
|
+
marshallOptions: {
|
|
789
|
+
// Whether to automatically convert empty strings, blobs, and sets to `null`.
|
|
790
|
+
convertEmptyValues: false,
|
|
791
|
+
// false, by default.
|
|
792
|
+
// Whether to remove undefined values while marshalling.
|
|
793
|
+
removeUndefinedValues: false,
|
|
794
|
+
// false, by default.
|
|
795
|
+
// Whether to convert typeof object to map attribute.
|
|
796
|
+
convertClassInstanceToMap: false
|
|
797
|
+
// false, by default.
|
|
798
|
+
},
|
|
799
|
+
unmarshallOptions: {
|
|
800
|
+
// Whether to return numbers as a string instead of converting them to native JavaScript numbers.
|
|
801
|
+
wrapNumbers: false
|
|
802
|
+
// false, by default.
|
|
803
|
+
}
|
|
804
|
+
}
|
|
805
|
+
);
|
|
806
|
+
return { client, tableName };
|
|
807
|
+
};
|
|
808
|
+
async function find(ddb, props) {
|
|
809
|
+
try {
|
|
810
|
+
const TableName = props.tableName ?? ddb.tableName;
|
|
811
|
+
const { filters, projection, debug } = props;
|
|
812
|
+
const isScan = "scan" in props && props.scan === true;
|
|
813
|
+
validateFindProps(props);
|
|
814
|
+
const names = {};
|
|
815
|
+
const values = {};
|
|
816
|
+
const KeyConditionExpression = isScan ? void 0 : buildKeyConditionExpression(
|
|
817
|
+
props.pk,
|
|
818
|
+
props.sk,
|
|
819
|
+
names,
|
|
820
|
+
values
|
|
821
|
+
);
|
|
822
|
+
const FilterExpression = filters ? buildFilterExpression(filters, names, values) : void 0;
|
|
823
|
+
if (debug) {
|
|
824
|
+
console.log(`[debug] [ddb] [find] input:`, {
|
|
825
|
+
isScan,
|
|
826
|
+
filters,
|
|
827
|
+
FilterExpression,
|
|
828
|
+
names,
|
|
829
|
+
values
|
|
830
|
+
});
|
|
831
|
+
}
|
|
832
|
+
const ProjectionExpression = projection ? buildProjectionExpression(projection, names) : void 0;
|
|
833
|
+
const all = [];
|
|
834
|
+
let startKey = props.startKey;
|
|
835
|
+
let pageCount = 0;
|
|
836
|
+
let totalItems = 0;
|
|
837
|
+
do {
|
|
838
|
+
if (props.maxPages && pageCount >= props.maxPages) break;
|
|
839
|
+
const command = isScan ? new ScanCommand({
|
|
840
|
+
TableName,
|
|
841
|
+
IndexName: props.indexName,
|
|
842
|
+
FilterExpression,
|
|
843
|
+
Limit: props.limit,
|
|
844
|
+
ProjectionExpression,
|
|
845
|
+
ExclusiveStartKey: startKey,
|
|
846
|
+
ConsistentRead: props.strong,
|
|
847
|
+
...Object.keys(names).length > 0 && {
|
|
848
|
+
ExpressionAttributeNames: names
|
|
849
|
+
},
|
|
850
|
+
...Object.keys(values).length > 0 && {
|
|
851
|
+
ExpressionAttributeValues: values
|
|
852
|
+
}
|
|
853
|
+
}) : new QueryCommand({
|
|
854
|
+
TableName,
|
|
855
|
+
IndexName: props.indexName,
|
|
856
|
+
KeyConditionExpression,
|
|
857
|
+
FilterExpression,
|
|
858
|
+
Limit: props.limit,
|
|
859
|
+
ProjectionExpression,
|
|
860
|
+
ExclusiveStartKey: startKey,
|
|
861
|
+
ConsistentRead: props.strong,
|
|
862
|
+
ExpressionAttributeNames: names,
|
|
863
|
+
ExpressionAttributeValues: values,
|
|
864
|
+
ScanIndexForward: props.sort === "desc" ? false : true
|
|
865
|
+
});
|
|
866
|
+
const result = await ddb.client.send(command);
|
|
867
|
+
const pageItems = result.Items ?? [];
|
|
868
|
+
all.push(...pageItems);
|
|
869
|
+
totalItems += pageItems.length;
|
|
870
|
+
pageCount++;
|
|
871
|
+
startKey = result.LastEvaluatedKey;
|
|
872
|
+
if (props.recursive) {
|
|
873
|
+
if (props.maxItems && totalItems >= props.maxItems) break;
|
|
874
|
+
if (!startKey) break;
|
|
875
|
+
if (typeof props.recursive === "function") {
|
|
876
|
+
const page = {
|
|
877
|
+
lastEvaluatedKey: result.LastEvaluatedKey,
|
|
878
|
+
scannedCount: result.ScannedCount ?? 0,
|
|
879
|
+
count: result.Count ?? 0,
|
|
880
|
+
items: pageItems
|
|
881
|
+
};
|
|
882
|
+
const shouldContinue = props.recursive(page, pageCount, totalItems);
|
|
883
|
+
if (!shouldContinue) break;
|
|
884
|
+
}
|
|
885
|
+
} else {
|
|
886
|
+
break;
|
|
887
|
+
}
|
|
888
|
+
} while (true);
|
|
889
|
+
const rawResponse = {
|
|
890
|
+
lastEvaluatedKey: startKey,
|
|
891
|
+
scannedCount: totalItems,
|
|
892
|
+
count: totalItems,
|
|
893
|
+
items: all
|
|
894
|
+
};
|
|
895
|
+
if (props.raw && props.first) {
|
|
896
|
+
return {
|
|
897
|
+
count: rawResponse.count,
|
|
898
|
+
item: rawResponse.items[0],
|
|
899
|
+
scannedCount: rawResponse.scannedCount
|
|
900
|
+
};
|
|
901
|
+
}
|
|
902
|
+
if (props.raw) return rawResponse;
|
|
903
|
+
if (props.first) return rawResponse.items[0];
|
|
904
|
+
return rawResponse.items;
|
|
905
|
+
} catch (error2) {
|
|
906
|
+
const err = error2;
|
|
907
|
+
console.log(`[error] [ddb] [find] failed with ${err.message}. Input:`);
|
|
908
|
+
console.dir(props, { depth: 100 });
|
|
909
|
+
throw err;
|
|
910
|
+
}
|
|
911
|
+
}
|
|
912
|
+
var remove = async (ddb, props) => {
|
|
913
|
+
const TableName = props.tableName ?? ddb.tableName;
|
|
914
|
+
const command = new DeleteCommand({ Key: props.key, TableName });
|
|
915
|
+
return ddb.client.send(command);
|
|
916
|
+
};
|
|
917
|
+
var save = async (ddb, props) => {
|
|
918
|
+
const TableName = props.tableName ?? ddb.tableName;
|
|
919
|
+
const command = new PutCommand({
|
|
920
|
+
Item: props.item,
|
|
921
|
+
TableName
|
|
922
|
+
});
|
|
923
|
+
return ddb.client.send(command);
|
|
924
|
+
};
|
|
925
|
+
async function batchGet(ddb, props) {
|
|
926
|
+
const { keys, maxRetries = 3, strong = false } = props;
|
|
927
|
+
const TableName = props.tableName ?? ddb.tableName;
|
|
928
|
+
const chunks = chunk2(keys, 100);
|
|
929
|
+
const allItems = [];
|
|
930
|
+
const requestedCount = keys.length;
|
|
931
|
+
let totalConsumedCapacity = 0;
|
|
932
|
+
for (const chunk3 of chunks) {
|
|
933
|
+
let attempt = 0;
|
|
934
|
+
let keysToFetch = chunk3;
|
|
935
|
+
while (keysToFetch.length > 0 && attempt <= maxRetries) {
|
|
936
|
+
if (attempt > 0) {
|
|
937
|
+
const delay = backoffDelay(attempt);
|
|
938
|
+
console.log(
|
|
939
|
+
`[info] [ddb] [batchGet]: Retrying ${keysToFetch.length} keys (attempt ${attempt}/${maxRetries}) after ${delay}ms`
|
|
940
|
+
);
|
|
941
|
+
await sleep3(delay);
|
|
942
|
+
}
|
|
943
|
+
try {
|
|
944
|
+
const command = new BatchGetCommand({
|
|
945
|
+
RequestItems: {
|
|
946
|
+
[TableName]: { Keys: keysToFetch, ConsistentRead: strong }
|
|
947
|
+
}
|
|
948
|
+
});
|
|
949
|
+
const result = await ddb.client.send(command);
|
|
950
|
+
const items = result.Responses?.[TableName] ?? [];
|
|
951
|
+
allItems.push(...items);
|
|
952
|
+
if (result.ConsumedCapacity) {
|
|
953
|
+
totalConsumedCapacity += result.ConsumedCapacity.reduce(
|
|
954
|
+
(sum, cap) => sum + (cap.CapacityUnits ?? 0),
|
|
955
|
+
0
|
|
956
|
+
);
|
|
957
|
+
}
|
|
958
|
+
const unprocessed = result.UnprocessedKeys?.[TableName]?.Keys;
|
|
959
|
+
if (unprocessed && unprocessed.length > 0) {
|
|
960
|
+
keysToFetch = unprocessed;
|
|
961
|
+
attempt++;
|
|
962
|
+
} else {
|
|
963
|
+
keysToFetch = [];
|
|
964
|
+
}
|
|
965
|
+
} catch (error2) {
|
|
966
|
+
console.log(
|
|
967
|
+
`[error] [ddb] [batchGet]: Error fetching chunk (attempt ${attempt}/${maxRetries}):`,
|
|
968
|
+
error2
|
|
969
|
+
);
|
|
970
|
+
if (attempt >= maxRetries) throw error2;
|
|
971
|
+
attempt++;
|
|
972
|
+
}
|
|
973
|
+
}
|
|
974
|
+
if (keysToFetch.length > 0) {
|
|
975
|
+
console.log(
|
|
976
|
+
`[error] [ddb] [batchGet]: Failed to fetch ${keysToFetch.length} keys after ${maxRetries} retries`
|
|
977
|
+
);
|
|
978
|
+
}
|
|
979
|
+
}
|
|
980
|
+
const missing = requestedCount - allItems.length;
|
|
981
|
+
return {
|
|
982
|
+
missing,
|
|
983
|
+
items: allItems,
|
|
984
|
+
count: allItems.length,
|
|
985
|
+
consumedCapacity: totalConsumedCapacity || void 0
|
|
986
|
+
};
|
|
987
|
+
}
|
|
988
|
+
async function batchWrite(ddb, props) {
|
|
989
|
+
const { maxRetries = 3 } = props;
|
|
990
|
+
const TableName = props.tableName ?? ddb.tableName;
|
|
991
|
+
const items = props.transform ? props.items.map(props.transform) : props.items;
|
|
992
|
+
const hasConditions = items.some((x) => x.cond);
|
|
993
|
+
const chunkSize = hasConditions ? 100 : 25;
|
|
994
|
+
const chunks = chunk2(items, chunkSize);
|
|
995
|
+
let processedCount = 0;
|
|
996
|
+
let failedCount = 0;
|
|
997
|
+
let totalConsumedCapacity = 0;
|
|
998
|
+
for (const chunk3 of chunks) {
|
|
999
|
+
let attempt = 0;
|
|
1000
|
+
let itemsToWrite = chunk3;
|
|
1001
|
+
while (itemsToWrite.length > 0 && attempt <= maxRetries) {
|
|
1002
|
+
if (attempt > 0) {
|
|
1003
|
+
const delay = backoffDelay(attempt);
|
|
1004
|
+
console.log(
|
|
1005
|
+
`[info] [ddb] [batchWrite]: Retrying ${itemsToWrite.length} items (attempt ${attempt}/${maxRetries}) after ${delay}ms`
|
|
1006
|
+
);
|
|
1007
|
+
await sleep3(delay);
|
|
1008
|
+
}
|
|
1009
|
+
try {
|
|
1010
|
+
if (hasConditions) {
|
|
1011
|
+
const transactItems = itemsToWrite.map((x) => ({
|
|
1012
|
+
Put: {
|
|
1013
|
+
TableName,
|
|
1014
|
+
Item: x.item,
|
|
1015
|
+
...x.cond && { ConditionExpression: x.cond }
|
|
1016
|
+
}
|
|
1017
|
+
}));
|
|
1018
|
+
const command = new TransactWriteCommand({
|
|
1019
|
+
TransactItems: transactItems
|
|
1020
|
+
});
|
|
1021
|
+
const result = await ddb.client.send(command);
|
|
1022
|
+
if (result.ConsumedCapacity) {
|
|
1023
|
+
totalConsumedCapacity += result.ConsumedCapacity.reduce(
|
|
1024
|
+
(sum, cap) => sum + (cap.CapacityUnits ?? 0),
|
|
1025
|
+
0
|
|
1026
|
+
);
|
|
1027
|
+
}
|
|
1028
|
+
processedCount += itemsToWrite.length;
|
|
1029
|
+
itemsToWrite = [];
|
|
1030
|
+
} else {
|
|
1031
|
+
const writeRequests = itemsToWrite.map((x) => ({
|
|
1032
|
+
PutRequest: { Item: x.item }
|
|
1033
|
+
}));
|
|
1034
|
+
const command = new BatchWriteCommand({
|
|
1035
|
+
RequestItems: { [TableName]: writeRequests }
|
|
1036
|
+
});
|
|
1037
|
+
const result = await ddb.client.send(command);
|
|
1038
|
+
if (result.ConsumedCapacity) {
|
|
1039
|
+
totalConsumedCapacity += result.ConsumedCapacity.reduce(
|
|
1040
|
+
(sum, cap) => sum + (cap.CapacityUnits ?? 0),
|
|
1041
|
+
0
|
|
1042
|
+
);
|
|
1043
|
+
}
|
|
1044
|
+
const unprocessed = result.UnprocessedItems?.[TableName];
|
|
1045
|
+
if (unprocessed && unprocessed.length > 0) {
|
|
1046
|
+
itemsToWrite = unprocessed.map((req) => ({
|
|
1047
|
+
item: req.PutRequest.Item
|
|
1048
|
+
}));
|
|
1049
|
+
processedCount += chunk3.length - itemsToWrite.length;
|
|
1050
|
+
attempt++;
|
|
1051
|
+
} else {
|
|
1052
|
+
processedCount += itemsToWrite.length;
|
|
1053
|
+
itemsToWrite = [];
|
|
1054
|
+
}
|
|
1055
|
+
}
|
|
1056
|
+
} catch (error2) {
|
|
1057
|
+
console.log(
|
|
1058
|
+
`[error] [ddb] [batchWrite]: Error writing chunk (attempt ${attempt}/${maxRetries}):`,
|
|
1059
|
+
error2
|
|
1060
|
+
);
|
|
1061
|
+
if (attempt >= maxRetries) {
|
|
1062
|
+
failedCount += itemsToWrite.length;
|
|
1063
|
+
itemsToWrite = [];
|
|
1064
|
+
} else {
|
|
1065
|
+
attempt++;
|
|
1066
|
+
}
|
|
1067
|
+
}
|
|
1068
|
+
}
|
|
1069
|
+
if (itemsToWrite.length > 0) {
|
|
1070
|
+
console.log(
|
|
1071
|
+
`[error] [ddb] [batchWrite]: Failed to write ${itemsToWrite.length} items after ${maxRetries} retries`
|
|
1072
|
+
);
|
|
1073
|
+
failedCount += itemsToWrite.length;
|
|
1074
|
+
}
|
|
1075
|
+
}
|
|
1076
|
+
return {
|
|
1077
|
+
failed: failedCount,
|
|
1078
|
+
processed: processedCount,
|
|
1079
|
+
consumedCapacity: totalConsumedCapacity || void 0
|
|
1080
|
+
};
|
|
1081
|
+
}
|
|
1082
|
+
async function batchDelete(ddb, props) {
|
|
1083
|
+
const { maxRetries = 3 } = props;
|
|
1084
|
+
const TableName = props.tableName ?? ddb.tableName;
|
|
1085
|
+
const keys = props.transform ? props.keys.map(props.transform) : props.keys;
|
|
1086
|
+
const hasConditions = keys.some((x) => x.cond);
|
|
1087
|
+
const chunkSize = hasConditions ? 100 : 25;
|
|
1088
|
+
const chunks = chunk2(keys, chunkSize);
|
|
1089
|
+
let processedCount = 0;
|
|
1090
|
+
let failedCount = 0;
|
|
1091
|
+
let totalConsumedCapacity = 0;
|
|
1092
|
+
for (const chunk3 of chunks) {
|
|
1093
|
+
let attempt = 0;
|
|
1094
|
+
let itemsToDelete = chunk3;
|
|
1095
|
+
while (itemsToDelete.length > 0 && attempt <= maxRetries) {
|
|
1096
|
+
if (attempt > 0) {
|
|
1097
|
+
const delay = backoffDelay(attempt);
|
|
1098
|
+
console.log(
|
|
1099
|
+
`[info] [ddb] [batchDelete]: Retrying ${itemsToDelete.length} items (attempt ${attempt}/${maxRetries}) after ${delay}ms`
|
|
1100
|
+
);
|
|
1101
|
+
await sleep3(delay);
|
|
1102
|
+
}
|
|
1103
|
+
try {
|
|
1104
|
+
if (hasConditions) {
|
|
1105
|
+
const transactItems = itemsToDelete.map((x) => ({
|
|
1106
|
+
Delete: {
|
|
1107
|
+
TableName,
|
|
1108
|
+
Key: x.key,
|
|
1109
|
+
...x.cond && { ConditionExpression: x.cond }
|
|
1110
|
+
}
|
|
1111
|
+
}));
|
|
1112
|
+
const command = new TransactWriteCommand({
|
|
1113
|
+
TransactItems: transactItems
|
|
1114
|
+
});
|
|
1115
|
+
const result = await ddb.client.send(command);
|
|
1116
|
+
if (result.ConsumedCapacity) {
|
|
1117
|
+
totalConsumedCapacity += result.ConsumedCapacity.reduce(
|
|
1118
|
+
(sum, cap) => sum + (cap.CapacityUnits ?? 0),
|
|
1119
|
+
0
|
|
1120
|
+
);
|
|
1121
|
+
}
|
|
1122
|
+
processedCount += itemsToDelete.length;
|
|
1123
|
+
itemsToDelete = [];
|
|
1124
|
+
} else {
|
|
1125
|
+
const writeRequests = itemsToDelete.map((x) => ({
|
|
1126
|
+
DeleteRequest: { Key: x.key }
|
|
1127
|
+
}));
|
|
1128
|
+
const command = new BatchWriteCommand({
|
|
1129
|
+
RequestItems: { [TableName]: writeRequests }
|
|
1130
|
+
});
|
|
1131
|
+
const result = await ddb.client.send(command);
|
|
1132
|
+
if (result.ConsumedCapacity) {
|
|
1133
|
+
totalConsumedCapacity += result.ConsumedCapacity.reduce(
|
|
1134
|
+
(sum, cap) => sum + (cap.CapacityUnits ?? 0),
|
|
1135
|
+
0
|
|
1136
|
+
);
|
|
1137
|
+
}
|
|
1138
|
+
const unprocessed = result.UnprocessedItems?.[TableName];
|
|
1139
|
+
if (unprocessed && unprocessed.length > 0) {
|
|
1140
|
+
itemsToDelete = unprocessed.map((req) => ({
|
|
1141
|
+
key: req.DeleteRequest.Key
|
|
1142
|
+
}));
|
|
1143
|
+
processedCount += chunk3.length - itemsToDelete.length;
|
|
1144
|
+
attempt++;
|
|
1145
|
+
} else {
|
|
1146
|
+
processedCount += itemsToDelete.length;
|
|
1147
|
+
itemsToDelete = [];
|
|
1148
|
+
}
|
|
1149
|
+
}
|
|
1150
|
+
} catch (error2) {
|
|
1151
|
+
console.log(
|
|
1152
|
+
`[error] [ddb] [batchDelete]: Error deleting chunk (attempt ${attempt}/${maxRetries}):`,
|
|
1153
|
+
error2
|
|
1154
|
+
);
|
|
1155
|
+
if (attempt >= maxRetries) {
|
|
1156
|
+
failedCount += itemsToDelete.length;
|
|
1157
|
+
itemsToDelete = [];
|
|
1158
|
+
} else {
|
|
1159
|
+
attempt++;
|
|
1160
|
+
}
|
|
1161
|
+
}
|
|
1162
|
+
}
|
|
1163
|
+
if (itemsToDelete.length > 0) {
|
|
1164
|
+
console.log(
|
|
1165
|
+
`[error] [ddb] [batchDelete]: Failed to delete ${itemsToDelete.length} items after ${maxRetries} retries`
|
|
1166
|
+
);
|
|
1167
|
+
failedCount += itemsToDelete.length;
|
|
1168
|
+
}
|
|
1169
|
+
}
|
|
1170
|
+
return {
|
|
1171
|
+
failed: failedCount,
|
|
1172
|
+
processed: processedCount,
|
|
1173
|
+
consumedCapacity: totalConsumedCapacity || void 0
|
|
1174
|
+
};
|
|
1175
|
+
}
|
|
1176
|
+
var deleteTable = (ddb, tableName) => {
|
|
1177
|
+
const TableName = tableName;
|
|
1178
|
+
const command = new DeleteTableCommand({ TableName });
|
|
1179
|
+
return ddb.client.send(command);
|
|
1180
|
+
};
|
|
1181
|
+
var tableExists = async (ddb, props) => {
|
|
1182
|
+
try {
|
|
1183
|
+
const TableName = props.tableName;
|
|
1184
|
+
const command = new DescribeTableCommand({ TableName });
|
|
1185
|
+
await ddb.client.send(command);
|
|
1186
|
+
return true;
|
|
1187
|
+
} catch (err) {
|
|
1188
|
+
if (err instanceof DynamoDBServiceException) {
|
|
1189
|
+
if (err.name === "ResourceNotFoundException" || err.$metadata.httpStatusCode === 400) {
|
|
1190
|
+
return false;
|
|
1191
|
+
}
|
|
1192
|
+
}
|
|
1193
|
+
throw err;
|
|
1194
|
+
}
|
|
1195
|
+
};
|
|
1196
|
+
|
|
1197
|
+
// src/server/aws/ddb/literals.ts
|
|
1198
|
+
var lsi = { name: "lsi", sk: "lsi" };
|
|
1199
|
+
var lsi2 = { name: "lsi2", sk: "lsi2" };
|
|
1200
|
+
var lsiUsername = {
|
|
1201
|
+
name: "username-lsi",
|
|
1202
|
+
sk: "username"
|
|
1203
|
+
};
|
|
1204
|
+
var lsiNormalized = {
|
|
1205
|
+
/** use in index_name */
|
|
1206
|
+
name: "normalized-lsi",
|
|
1207
|
+
sk: "normalized"
|
|
1208
|
+
};
|
|
1209
|
+
var lsiPhash = {
|
|
1210
|
+
/** use in index_name */
|
|
1211
|
+
name: "phash-lsi",
|
|
1212
|
+
sk: "phash"
|
|
1213
|
+
};
|
|
1214
|
+
|
|
1215
|
+
export { ddb_exports as DDB, dict_exports as Dict, file_exports as File, flow_exports as Flow, int_exports as Int, list_exports as List, log_exports as Log, money_exports as Money, random_exports as Random, str_exports as Str, time_exports as Time };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "qstd",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.28",
|
|
4
4
|
"description": "Standard Block component and utilities library with Panda CSS",
|
|
5
5
|
"author": "malin1",
|
|
6
6
|
"license": "MIT",
|
|
@@ -47,6 +47,8 @@
|
|
|
47
47
|
"react-dom": "^18.0.0 || ^19.0.0"
|
|
48
48
|
},
|
|
49
49
|
"dependencies": {
|
|
50
|
+
"@aws-sdk/client-dynamodb": "latest",
|
|
51
|
+
"@aws-sdk/lib-dynamodb": "latest",
|
|
50
52
|
"@floating-ui/react": "^0.27.12",
|
|
51
53
|
"@fortawesome/fontawesome-svg-core": "^7.0.0",
|
|
52
54
|
"@fortawesome/free-brands-svg-icons": "^7.0.0",
|