tsynamo
Advanced tools
Comparing version 0.0.6 to 0.0.7
@@ -1,31 +0,3 @@ | ||
import { QueryCommand, GetCommand, PutCommand, DynamoDBDocumentClient } from '@aws-sdk/lib-dynamodb'; | ||
import { QueryCommand, GetCommand, PutCommand, DeleteCommand, DynamoDBDocumentClient } from '@aws-sdk/lib-dynamodb'; | ||
type AttributesNode = { | ||
readonly kind: "AttributesNode"; | ||
readonly attributes: string[]; | ||
}; | ||
type ConsistentReadNode = { | ||
readonly kind: "ConsistentReadNode"; | ||
readonly enabled: boolean; | ||
}; | ||
type KeysNode = { | ||
readonly kind: "KeysNode"; | ||
readonly keys: Record<string, unknown>; | ||
}; | ||
type TableNode = { | ||
readonly kind: "TableNode"; | ||
readonly table: string; | ||
}; | ||
type GetNode = { | ||
readonly kind: "GetNode"; | ||
readonly table: TableNode; | ||
readonly keys?: KeysNode; | ||
readonly consistentRead?: ConsistentReadNode; | ||
readonly attributes?: AttributesNode; | ||
}; | ||
type AttributeExistsFunctionExpression = { | ||
@@ -73,7 +45,2 @@ readonly kind: "AttributeExistsFunctionExpression"; | ||
type ExpressionNode = { | ||
readonly kind: "ExpressionNode"; | ||
readonly expressions: ExpressionJoinTypeNode[]; | ||
}; | ||
type ExpressionNotExpression = { | ||
@@ -91,2 +58,54 @@ readonly kind: "ExpressionNotExpression"; | ||
type ExpressionNode = { | ||
readonly kind: "ExpressionNode"; | ||
readonly expressions: ExpressionJoinTypeNode[]; | ||
}; | ||
type KeysNode = { | ||
readonly kind: "KeysNode"; | ||
readonly keys: Record<string, unknown>; | ||
}; | ||
type ReturnValuesOptions = "NONE" | "ALL_OLD" | "UPDATED_OLD" | "ALL_NEW" | "UPDATED_NEW"; | ||
type ReturnValuesNode = { | ||
readonly kind: "ReturnValuesNode"; | ||
readonly option: ReturnValuesOptions; | ||
}; | ||
type ReturnOldValuesNode = { | ||
readonly kind: "ReturnValuesNode"; | ||
readonly option: Extract<ReturnValuesOptions, "NONE" | "ALL_OLD">; | ||
}; | ||
type TableNode = { | ||
readonly kind: "TableNode"; | ||
readonly table: string; | ||
}; | ||
type DeleteNode = { | ||
readonly kind: "DeleteNode"; | ||
readonly table: TableNode; | ||
readonly conditionExpression: ExpressionNode; | ||
readonly returnValues?: ReturnValuesNode; | ||
readonly returnValuesOnConditionCheckFailure?: ReturnOldValuesNode; | ||
readonly keys?: KeysNode; | ||
}; | ||
type AttributesNode = { | ||
readonly kind: "AttributesNode"; | ||
readonly attributes: string[]; | ||
}; | ||
type ConsistentReadNode = { | ||
readonly kind: "ConsistentReadNode"; | ||
readonly enabled: boolean; | ||
}; | ||
type GetNode = { | ||
readonly kind: "GetNode"; | ||
readonly table: TableNode; | ||
readonly keys?: KeysNode; | ||
readonly consistentRead?: ConsistentReadNode; | ||
readonly attributes?: AttributesNode; | ||
}; | ||
type KeyConditionComparatorExpression = { | ||
@@ -130,8 +149,2 @@ readonly kind: "KeyConditionComparatorExpression"; | ||
type ReturnValuesOptions = "NONE" | "ALL_OLD" | "UPDATED_OLD" | "ALL_NEW" | "UPDATED_NEW"; | ||
type ReturnValuesNode = { | ||
readonly kind: "ReturnValuesNode"; | ||
readonly option: ReturnValuesOptions; | ||
}; | ||
type PutNode = { | ||
@@ -149,5 +162,7 @@ readonly kind: "PutNode"; | ||
compile(rootNode: PutNode): PutCommand; | ||
compile(rootNode: DeleteNode): DeleteCommand; | ||
compileGetNode(getNode: GetNode): GetCommand; | ||
compileQueryNode(queryNode: QueryNode): QueryCommand; | ||
compilePutNode(putNode: PutNode): PutCommand; | ||
compileDeleteNode(deleteNode: DeleteNode): DeleteCommand; | ||
compileAttributeNamesNode(node?: AttributesNode): { | ||
@@ -258,22 +273,2 @@ ProjectionExpression: string | undefined; | ||
interface GetQueryBuilderInterface<DDB, Table extends keyof DDB, O> { | ||
keys<Keys extends PickPk<DDB[Table]> & PickSkRequired<DDB[Table]>>(pk: Keys): GetQueryBuilderInterface<DDB, Table, O>; | ||
consistentRead(enabled: boolean): GetQueryBuilderInterface<DDB, Table, O>; | ||
attributes<A extends readonly ObjectFullPaths<DDB[Table]>[] & string[]>(attributes: A): GetQueryBuilderInterface<DDB, Table, SelectAttributes<DDB[Table], A>>; | ||
execute(): Promise<ExecuteOutput<O> | undefined>; | ||
} | ||
declare class GetQueryBuilder<DDB, Table extends keyof DDB, O extends DDB[Table]> implements GetQueryBuilderInterface<DDB, Table, O> { | ||
#private; | ||
constructor(props: GetQueryBuilderProps); | ||
keys<Keys extends PickPk<DDB[Table]> & PickSkRequired<DDB[Table]>>(keys: Keys): GetQueryBuilder<DDB, Table, O>; | ||
consistentRead(enabled: boolean): GetQueryBuilderInterface<DDB, Table, O>; | ||
attributes<A extends readonly ObjectFullPaths<DDB[Table]>[] & string[]>(attributes: A): GetQueryBuilderInterface<DDB, Table, SelectAttributes<DDB[Table], A>>; | ||
execute: () => Promise<ExecuteOutput<O> | undefined>; | ||
} | ||
interface GetQueryBuilderProps { | ||
readonly node: GetNode; | ||
readonly ddbClient: DynamoDBDocumentClient; | ||
readonly queryCompiler: QueryCompiler; | ||
} | ||
interface ExpressionBuilderInterface<DDB, Table extends keyof DDB, O, AllowKeys = false> { | ||
@@ -328,3 +323,63 @@ expression<Key extends ObjectKeyPaths<AllowKeys extends true ? DDB[Table] : PickNonKeys<DDB[Table]>>>(...args: ComparatorExprArg<DDB, Table, Key>): ExpressionBuilderInterface<DDB, Table, O, AllowKeys>; | ||
]; | ||
type ExprArgs<DDB, Table extends keyof DDB, O, Key, AllowKeysInExpression = true> = ComparatorExprArg<DDB, Table, Key> | AttributeFuncExprArg<Key> | AttributeBeginsWithExprArg<Key> | AttributeContainsExprArg<DDB, Table, Key> | AttributeBetweenExprArg<DDB, Table, Key> | BuilderExprArg<DDB, Table, O, AllowKeysInExpression> | NotExprArg<DDB, Table, O, AllowKeysInExpression>; | ||
interface DeleteItemQueryBuilderInterface<DDB, Table extends keyof DDB, O> { | ||
conditionExpression<Key extends ObjectKeyPaths<DDB[Table]>>(...args: ComparatorExprArg<DDB, Table, Key>): DeleteItemQueryBuilderInterface<DDB, Table, O>; | ||
conditionExpression<Key extends ObjectKeyPaths<DDB[Table]>>(...args: AttributeFuncExprArg<Key>): DeleteItemQueryBuilderInterface<DDB, Table, O>; | ||
conditionExpression<Key extends ObjectKeyPaths<DDB[Table]>>(...args: AttributeBeginsWithExprArg<Key>): DeleteItemQueryBuilderInterface<DDB, Table, O>; | ||
conditionExpression<Key extends ObjectKeyPaths<DDB[Table]>>(...args: AttributeContainsExprArg<DDB, Table, Key>): DeleteItemQueryBuilderInterface<DDB, Table, O>; | ||
conditionExpression<Key extends ObjectKeyPaths<DDB[Table]>>(...args: AttributeBetweenExprArg<DDB, Table, Key>): DeleteItemQueryBuilderInterface<DDB, Table, O>; | ||
conditionExpression<Key extends ObjectKeyPaths<DDB[Table]>>(...args: NotExprArg<DDB, Table, Key>): DeleteItemQueryBuilderInterface<DDB, Table, O>; | ||
conditionExpression<Key extends ObjectKeyPaths<DDB[Table]>>(...args: BuilderExprArg<DDB, Table, Key>): DeleteItemQueryBuilderInterface<DDB, Table, O>; | ||
orConditionExpression<Key extends ObjectKeyPaths<DDB[Table]>>(...args: ComparatorExprArg<DDB, Table, Key>): DeleteItemQueryBuilderInterface<DDB, Table, O>; | ||
orConditionExpression<Key extends ObjectKeyPaths<DDB[Table]>>(...args: AttributeFuncExprArg<Key>): DeleteItemQueryBuilderInterface<DDB, Table, O>; | ||
orConditionExpression<Key extends ObjectKeyPaths<DDB[Table]>>(...args: AttributeBeginsWithExprArg<Key>): DeleteItemQueryBuilderInterface<DDB, Table, O>; | ||
orConditionExpression<Key extends ObjectKeyPaths<DDB[Table]>>(...args: AttributeContainsExprArg<DDB, Table, Key>): DeleteItemQueryBuilderInterface<DDB, Table, O>; | ||
orConditionExpression<Key extends ObjectKeyPaths<DDB[Table]>>(...args: AttributeBetweenExprArg<DDB, Table, Key>): DeleteItemQueryBuilderInterface<DDB, Table, O>; | ||
orConditionExpression<Key extends ObjectKeyPaths<DDB[Table]>>(...args: NotExprArg<DDB, Table, Key>): DeleteItemQueryBuilderInterface<DDB, Table, O>; | ||
orConditionExpression<Key extends ObjectKeyPaths<DDB[Table]>>(...args: BuilderExprArg<DDB, Table, Key>): DeleteItemQueryBuilderInterface<DDB, Table, O>; | ||
returnValues(option: Extract<ReturnValuesOptions, "NONE" | "ALL_OLD">): DeleteItemQueryBuilderInterface<DDB, Table, O>; | ||
returnValuesOnConditionCheckFailure(option: Extract<ReturnValuesOptions, "NONE" | "ALL_OLD">): DeleteItemQueryBuilderInterface<DDB, Table, O>; | ||
keys<Keys extends PickPk<DDB[Table]> & PickSkRequired<DDB[Table]>>(pk: Keys): DeleteItemQueryBuilderInterface<DDB, Table, O>; | ||
execute(): Promise<ExecuteOutput<O>[] | undefined>; | ||
} | ||
/** | ||
* @todo support ReturnValuesOnConditionCheckFailure | ||
*/ | ||
declare class DeleteItemQueryBuilder<DDB, Table extends keyof DDB, O extends DDB[Table]> implements DeleteItemQueryBuilderInterface<DDB, Table, O> { | ||
#private; | ||
constructor(props: DeleteItemQueryBuilderProps); | ||
conditionExpression<Key extends ObjectKeyPaths<DDB[Table]>>(...args: ExprArgs<DDB, Table, O, Key>): DeleteItemQueryBuilderInterface<DDB, Table, O>; | ||
orConditionExpression<Key extends ObjectKeyPaths<DDB[Table]>>(...args: ExprArgs<DDB, Table, O, Key>): DeleteItemQueryBuilderInterface<DDB, Table, O>; | ||
returnValues(option: Extract<ReturnValuesOptions, "NONE" | "ALL_OLD">): DeleteItemQueryBuilderInterface<DDB, Table, O>; | ||
returnValuesOnConditionCheckFailure(option: Extract<ReturnValuesOptions, "NONE" | "ALL_OLD">): DeleteItemQueryBuilderInterface<DDB, Table, O>; | ||
keys<Keys extends PickPk<DDB[Table]> & PickSkRequired<DDB[Table]>>(keys: Keys): DeleteItemQueryBuilder<DDB, Table, O>; | ||
execute: () => Promise<ExecuteOutput<O>[] | undefined>; | ||
} | ||
interface DeleteItemQueryBuilderProps { | ||
readonly node: DeleteNode; | ||
readonly ddbClient: DynamoDBDocumentClient; | ||
readonly queryCompiler: QueryCompiler; | ||
} | ||
interface GetQueryBuilderInterface<DDB, Table extends keyof DDB, O> { | ||
keys<Keys extends PickPk<DDB[Table]> & PickSkRequired<DDB[Table]>>(pk: Keys): GetQueryBuilderInterface<DDB, Table, O>; | ||
consistentRead(enabled: boolean): GetQueryBuilderInterface<DDB, Table, O>; | ||
attributes<A extends readonly ObjectFullPaths<DDB[Table]>[] & string[]>(attributes: A): GetQueryBuilderInterface<DDB, Table, SelectAttributes<DDB[Table], A>>; | ||
execute(): Promise<ExecuteOutput<O> | undefined>; | ||
} | ||
declare class GetQueryBuilder<DDB, Table extends keyof DDB, O extends DDB[Table]> implements GetQueryBuilderInterface<DDB, Table, O> { | ||
#private; | ||
constructor(props: GetQueryBuilderProps); | ||
keys<Keys extends PickPk<DDB[Table]> & PickSkRequired<DDB[Table]>>(keys: Keys): GetQueryBuilder<DDB, Table, O>; | ||
consistentRead(enabled: boolean): GetQueryBuilderInterface<DDB, Table, O>; | ||
attributes<A extends readonly ObjectFullPaths<DDB[Table]>[] & string[]>(attributes: A): GetQueryBuilderInterface<DDB, Table, SelectAttributes<DDB[Table], A>>; | ||
execute: () => Promise<ExecuteOutput<O> | undefined>; | ||
} | ||
interface GetQueryBuilderProps { | ||
readonly node: GetNode; | ||
readonly ddbClient: DynamoDBDocumentClient; | ||
readonly queryCompiler: QueryCompiler; | ||
} | ||
interface PutItemQueryBuilderInterface<DDB, Table extends keyof DDB, O> { | ||
@@ -388,3 +443,3 @@ conditionExpression<Key extends ObjectKeyPaths<DDB[Table]>>(...args: ComparatorExprArg<DDB, Table, Key>): PutItemQueryBuilderInterface<DDB, Table, O>; | ||
*/ | ||
getItemFrom<Table extends keyof DDB & string>(table: Table): GetQueryBuilder<DDB, Table, DDB[Table]>; | ||
getItem<Table extends keyof DDB & string>(table: Table): GetQueryBuilder<DDB, Table, DDB[Table]>; | ||
/** | ||
@@ -404,2 +459,9 @@ * | ||
putItem<Table extends keyof DDB & string>(table: Table): PutItemQueryBuilderInterface<DDB, Table, DDB[Table]>; | ||
/** | ||
* | ||
* @param table Table to perform the delete item command to | ||
* | ||
* @see https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/client/dynamodb/command/DeleteItemCommand/ | ||
*/ | ||
deleteItem<Table extends keyof DDB & string>(table: Table): DeleteItemQueryBuilder<DDB, Table, DDB[Table]>; | ||
} | ||
@@ -406,0 +468,0 @@ interface QueryCreatorProps { |
@@ -5,6 +5,6 @@ 'use strict'; | ||
var D=(o,e)=>{Object.defineProperties(o.prototype,{then:{enumerable:!1,value:()=>{throw new Error(e)}}});};var p=class o{#e;constructor(e){this.#e=e;}keys(e){return new o({...this.#e,node:{...this.#e.node,keys:{kind:"KeysNode",keys:e}}})}consistentRead(e){return new o({...this.#e,node:{...this.#e.node,consistentRead:{kind:"ConsistentReadNode",enabled:e}}})}attributes(e){return new o({...this.#e,node:{...this.#e.node,attributes:{kind:"AttributesNode",attributes:e}}})}execute=async()=>{let e=this.#e.queryCompiler.compile(this.#e.node);return (await this.#e.ddbClient.send(e)).Item??void 0}};D(p,"Don't await GetQueryBuilder instances directly. To execute the query you need to call the `execute` method");var d=class o{#e;constructor(e){this.#e=e;}_expression(e,...t){if(t[1]==="begins_with"){let[n,r,s]=t;return new o({...this.#e,node:{...this.#e.node,expressions:this.#e.node.expressions.concat({kind:"ExpressionJoinTypeNode",expr:{kind:"BeginsWithFunctionExpression",key:n,substr:s},joinType:e})}})}else if(t[1]==="contains"){let[n,r,s]=t;return new o({...this.#e,node:{...this.#e.node,expressions:this.#e.node.expressions.concat({kind:"ExpressionJoinTypeNode",expr:{kind:"ContainsFunctionExpression",key:n,value:s},joinType:e})}})}else if(t[1]==="attribute_exists"||t[1]==="attribute_not_exists"){let[n,r]=t,s;return r==="attribute_exists"?s={kind:"AttributeExistsFunctionExpression",key:n}:s={kind:"AttributeNotExistsFunctionExpression",key:n},new o({...this.#e,node:{...this.#e.node,expressions:this.#e.node.expressions.concat({kind:"ExpressionJoinTypeNode",expr:s,joinType:e})}})}else if(t[1]==="BETWEEN"){let[n,r,s,i]=t;return new o({...this.#e,node:{...this.#e.node,expressions:this.#e.node.expressions.concat({kind:"ExpressionJoinTypeNode",expr:{kind:"BetweenConditionExpression",key:n,left:s,right:i},joinType:e})}})}else if(typeof t[0]!="function"&&t[0]!=="NOT"&&typeof t[1]!="function"&&t[1]!==void 0&&t[2]!==void 0){let[n,r,s]=t;return new o({...this.#e,node:{...this.#e.node,expressions:this.#e.node.expressions.concat({kind:"ExpressionJoinTypeNode",joinType:e,expr:{kind:"ExpressionComparatorExpressions",key:n,operation:r,value:s}})}})}else if(typeof t[0]=="function"||typeof t[1]=="function"){let n;if(typeof t[0]=="function"?n=t[0]:typeof t[1]=="function"&&(n=t[1]),!n)throw new Error("Could not find builder");let r=new o({...this.#e,node:{expressions:[],kind:"ExpressionNode"}}),i=n(r)._getNode(),a={kind:"ExpressionJoinTypeNode",expr:i,joinType:e};return t[0]==="NOT"&&(a={...a,expr:{kind:"ExpressionNotExpression",expr:i}}),new o({...this.#e,node:{...this.#e.node,expressions:this.#e.node.expressions.concat(a)}})}throw new Error("Invalid arguments given to expression builder")}expression(...e){return this._expression("AND",...e)}orExpression(...e){return this._expression("OR",...e)}_getNode(){return this.#e.node}};var y=class o{#e;constructor(e){this.#e=e;}conditionExpression(...e){let n=new d({node:{...this.#e.node.conditionExpression}}).expression(...e)._getNode();return new o({...this.#e,node:{...this.#e.node,conditionExpression:n}})}orConditionExpression(...e){let n=new d({node:{...this.#e.node.conditionExpression}}).orExpression(...e)._getNode();return new o({...this.#e,node:{...this.#e.node,conditionExpression:n}})}item(e){return new o({...this.#e,node:{...this.#e.node,item:{kind:"ItemNode",item:e}}})}returnValues(e){return new o({...this.#e,node:{...this.#e.node,returnValues:{kind:"ReturnValuesNode",option:e}}})}execute=async()=>{let e=this.#e.queryCompiler.compile(this.#e.node);return (await this.#e.ddbClient.send(e)).Attributes}};D(y,"Don't await PutQueryBuilder instances directly. To execute the query you need to call the `execute` method");var b=class o{#e;constructor(e){this.#e=e;}keyCondition(...e){if(e[1]==="begins_with"){let[t,n,r]=e;return new o({...this.#e,node:{...this.#e.node,keyConditions:this.#e.node.keyConditions.concat({kind:"KeyConditionNode",operation:{kind:"BeginsWithFunctionExpression",key:t,substr:r}})}})}else if(e[1]==="BETWEEN"){let[t,n,r,s]=e;return new o({...this.#e,node:{...this.#e.node,keyConditions:this.#e.node.keyConditions.concat({kind:"KeyConditionNode",operation:{kind:"BetweenConditionExpression",key:t,left:r,right:s}})}})}else {let[t,n,r]=e;return new o({...this.#e,node:{...this.#e.node,keyConditions:this.#e.node.keyConditions.concat({kind:"KeyConditionNode",operation:{kind:"KeyConditionComparatorExpression",operation:n,key:t,value:r}})}})}}filterExpression(...e){let n=new d({node:{...this.#e.node.filterExpression}}).expression(...e)._getNode();return new o({...this.#e,node:{...this.#e.node,filterExpression:n}})}orFilterExpression(...e){let n=new d({node:{...this.#e.node.filterExpression}}).orExpression(...e)._getNode();return new o({...this.#e,node:{...this.#e.node,filterExpression:n}})}limit(e){return new o({...this.#e,node:{...this.#e.node,limit:{kind:"LimitNode",limit:e}}})}scanIndexForward(e){return new o({...this.#e,node:{...this.#e.node,scanIndexForward:{kind:"ScanIndexForwardNode",enabled:e}}})}consistentRead(e){return new o({...this.#e,node:{...this.#e.node,consistentRead:{kind:"ConsistentReadNode",enabled:e}}})}attributes(e){return new o({...this.#e,node:{...this.#e.node,attributes:{kind:"AttributesNode",attributes:e}}})}execute=async()=>{let e=this.#e.queryCompiler.compile(this.#e.node);return (await this.#e.ddbClient.send(e)).Items??void 0}};D(b,"Don't await QueryQueryBuilder instances directly. To execute the query you need to call the `execute` method");var c=class{#e;constructor(e){this.#e=e;}getItemFrom(e){return new p({node:{kind:"GetNode",table:{kind:"TableNode",table:e}},ddbClient:this.#e.ddbClient,queryCompiler:this.#e.queryCompiler})}query(e){return new b({node:{kind:"QueryNode",table:{kind:"TableNode",table:e},keyConditions:[],filterExpression:{kind:"ExpressionNode",expressions:[]}},ddbClient:this.#e.ddbClient,queryCompiler:this.#e.queryCompiler})}putItem(e){return new y({node:{kind:"PutNode",table:{kind:"TableNode",table:e},conditionExpression:{kind:"ExpressionNode",expressions:[]}},ddbClient:this.#e.ddbClient,queryCompiler:this.#e.queryCompiler})}};var P=/\[\d+\]/g,B=o=>o.split(".").map(e=>`#${e}`).join("."),m=o=>o.replaceAll(P,"").split(".").map(e=>[`#${e}`,e]);function E(o,e){for(let t in e)o.set(t,e[t]);}var x=class{compile(e){switch(e.kind){case"GetNode":return this.compileGetNode(e);case"QueryNode":return this.compileQueryNode(e);case"PutNode":return this.compilePutNode(e)}}compileGetNode(e){let{table:t,keys:n,consistentRead:r,attributes:s}=e,{ProjectionExpression:i,ExpressionAttributeNames:a}=this.compileAttributeNamesNode(s);return new libDynamodb.GetCommand({TableName:t.table,Key:n?.keys,ConsistentRead:r?.enabled,ProjectionExpression:i,ExpressionAttributeNames:a})}compileQueryNode(e){let{table:t,filterExpression:n,keyConditions:r,limit:s,scanIndexForward:i,consistentRead:a,attributes:u}=e,l=new Map,K=new Map,T=new Map,h=this.compileKeyConditionExpression(r,K,l),f=this.compileExpression(n,T,l),{ProjectionExpression:N,ExpressionAttributeNames:A}=this.compileAttributeNamesNode(u);return new libDynamodb.QueryCommand({TableName:t.table,KeyConditionExpression:h,FilterExpression:f||void 0,Limit:s?.limit,ExpressionAttributeValues:{...Object.fromEntries(K),...Object.fromEntries(T)},ScanIndexForward:i?.enabled,ConsistentRead:a?.enabled,ProjectionExpression:N,ExpressionAttributeNames:l.size>0||A?{...Object.fromEntries(l),...A}:void 0})}compilePutNode(e){let{table:t,item:n,returnValues:r,conditionExpression:s}=e,i=new Map,a=new Map,u=this.compileExpression(s,a,i);return new libDynamodb.PutCommand({TableName:t.table,Item:n?.item,ReturnValues:r?.option,ConditionExpression:u||void 0,ExpressionAttributeValues:a.size>0?{...Object.fromEntries(a)}:void 0,ExpressionAttributeNames:i.size>0?{...Object.fromEntries(i)}:void 0})}compileAttributeNamesNode(e){let t=e?.attributes.map(r=>B(r)).join(", "),n=e?.attributes.map(r=>m(r)).reduce((r,s)=>(s.forEach(([i,a])=>{r[i]=a;}),r),{});return {ProjectionExpression:t,ExpressionAttributeNames:n}}compileAttributeName(e){let t=B(e),n=m(e).reduce((r,[s,i])=>(r[s]=i,r),{});return {expressionAttributeName:t,attributeNameMap:n}}compileExpression=(e,t,n)=>{let r="";return e.expressions.forEach((s,i)=>{i!==0&&(r+=` ${s.joinType} `),r+=this.compileFilterExpressionJoinNodes(s,t,n);}),r};compileFilterExpressionJoinNodes=({expr:e},t,n)=>{let r="",i=`:filterExpressionValue${t.size}`,a;if("key"in e){let{expressionAttributeName:u,attributeNameMap:l}=this.compileAttributeName(e.key);a=u,E(n,l);}switch(e.kind){case"ExpressionNode":{r+="(",r+=this.compileExpression(e,t,n),r+=")";break}case"ExpressionComparatorExpressions":{r+=`${a} ${e.operation} ${i}`,t.set(i,e.value);break}case"ExpressionNotExpression":{r+="NOT (",r+=this.compileExpression(e.expr,t,n),r+=")";break}case"BetweenConditionExpression":{r+=`${a} BETWEEN ${i}left AND ${i}right`,t.set(`${i}left`,e.left),t.set(`${i}right`,e.right);break}case"AttributeExistsFunctionExpression":{r+=`attribute_exists(${a})`;break}case"AttributeNotExistsFunctionExpression":{r+=`attribute_not_exists(${a})`;break}case"BeginsWithFunctionExpression":{r+=`begins_with(${a}, ${i})`,t.set(i,e.substr);break}case"ContainsFunctionExpression":{r+=`contains(${a}, ${i})`,t.set(i,e.value);break}}return r};compileKeyConditionExpression=(e,t,n)=>{let r="";return e.forEach((s,i)=>{i!==0&&(r+=" AND ");let{expressionAttributeName:a,attributeNameMap:u}=this.compileAttributeName(s.operation.key),l=`:keyConditionValue${i}`;s.operation.kind==="KeyConditionComparatorExpression"?(r+=`${a} ${s.operation.operation} ${l}`,t.set(l,s.operation.value)):s.operation.kind==="BetweenConditionExpression"?(r+=`${a} BETWEEN ${l}left AND ${l}right`,t.set(`${l}left`,s.operation.left),t.set(`${l}right`,s.operation.right)):s.operation.kind==="BeginsWithFunctionExpression"&&(r+=`begins_with(${a}, ${l})`,t.set(l,s.operation.substr)),E(n,u);}),r}};var O=class extends c{constructor(e){let t=new x;super({...e,queryCompiler:t});}}; | ||
var D=(s,e)=>{Object.defineProperties(s.prototype,{then:{enumerable:!1,value:()=>{throw new Error(e)}}});};var d=class s{#e;constructor(e){this.#e=e;}_expression(e,...t){if(t[1]==="begins_with"){let[n,r,o]=t;return new s({...this.#e,node:{...this.#e.node,expressions:this.#e.node.expressions.concat({kind:"ExpressionJoinTypeNode",expr:{kind:"BeginsWithFunctionExpression",key:n,substr:o},joinType:e})}})}else if(t[1]==="contains"){let[n,r,o]=t;return new s({...this.#e,node:{...this.#e.node,expressions:this.#e.node.expressions.concat({kind:"ExpressionJoinTypeNode",expr:{kind:"ContainsFunctionExpression",key:n,value:o},joinType:e})}})}else if(t[1]==="attribute_exists"||t[1]==="attribute_not_exists"){let[n,r]=t,o;return r==="attribute_exists"?o={kind:"AttributeExistsFunctionExpression",key:n}:o={kind:"AttributeNotExistsFunctionExpression",key:n},new s({...this.#e,node:{...this.#e.node,expressions:this.#e.node.expressions.concat({kind:"ExpressionJoinTypeNode",expr:o,joinType:e})}})}else if(t[1]==="BETWEEN"){let[n,r,o,i]=t;return new s({...this.#e,node:{...this.#e.node,expressions:this.#e.node.expressions.concat({kind:"ExpressionJoinTypeNode",expr:{kind:"BetweenConditionExpression",key:n,left:o,right:i},joinType:e})}})}else if(typeof t[0]!="function"&&t[0]!=="NOT"&&typeof t[1]!="function"&&t[1]!==void 0&&t[2]!==void 0){let[n,r,o]=t;return new s({...this.#e,node:{...this.#e.node,expressions:this.#e.node.expressions.concat({kind:"ExpressionJoinTypeNode",joinType:e,expr:{kind:"ExpressionComparatorExpressions",key:n,operation:r,value:o}})}})}else if(typeof t[0]=="function"||typeof t[1]=="function"){let n;if(typeof t[0]=="function"?n=t[0]:typeof t[1]=="function"&&(n=t[1]),!n)throw new Error("Could not find builder");let r=new s({...this.#e,node:{expressions:[],kind:"ExpressionNode"}}),i=n(r)._getNode(),a={kind:"ExpressionJoinTypeNode",expr:i,joinType:e};return t[0]==="NOT"&&(a={...a,expr:{kind:"ExpressionNotExpression",expr:i}}),new s({...this.#e,node:{...this.#e.node,expressions:this.#e.node.expressions.concat(a)}})}throw new Error("Invalid arguments given to expression builder")}expression(...e){return this._expression("AND",...e)}orExpression(...e){return this._expression("OR",...e)}_getNode(){return this.#e.node}};var p=class s{#e;constructor(e){this.#e=e;}conditionExpression(...e){let n=new d({node:{...this.#e.node.conditionExpression}}).expression(...e)._getNode();return new s({...this.#e,node:{...this.#e.node,conditionExpression:n}})}orConditionExpression(...e){let n=new d({node:{...this.#e.node.conditionExpression}}).orExpression(...e)._getNode();return new s({...this.#e,node:{...this.#e.node,conditionExpression:n}})}returnValues(e){return new s({...this.#e,node:{...this.#e.node,returnValues:{kind:"ReturnValuesNode",option:e}}})}returnValuesOnConditionCheckFailure(e){return new s({...this.#e,node:{...this.#e.node,returnValuesOnConditionCheckFailure:{kind:"ReturnValuesNode",option:e}}})}keys(e){return new s({...this.#e,node:{...this.#e.node,keys:{kind:"KeysNode",keys:e}}})}execute=async()=>{let e=this.#e.queryCompiler.compile(this.#e.node);return (await this.#e.ddbClient.send(e)).Attributes}};D(p,"Don't await DeleteItemQueryBuilder instances directly. To execute the query you need to call the `execute` method");var y=class s{#e;constructor(e){this.#e=e;}keys(e){return new s({...this.#e,node:{...this.#e.node,keys:{kind:"KeysNode",keys:e}}})}consistentRead(e){return new s({...this.#e,node:{...this.#e.node,consistentRead:{kind:"ConsistentReadNode",enabled:e}}})}attributes(e){return new s({...this.#e,node:{...this.#e.node,attributes:{kind:"AttributesNode",attributes:e}}})}execute=async()=>{let e=this.#e.queryCompiler.compile(this.#e.node);return (await this.#e.ddbClient.send(e)).Item??void 0}};D(y,"Don't await GetQueryBuilder instances directly. To execute the query you need to call the `execute` method");var b=class s{#e;constructor(e){this.#e=e;}conditionExpression(...e){let n=new d({node:{...this.#e.node.conditionExpression}}).expression(...e)._getNode();return new s({...this.#e,node:{...this.#e.node,conditionExpression:n}})}orConditionExpression(...e){let n=new d({node:{...this.#e.node.conditionExpression}}).orExpression(...e)._getNode();return new s({...this.#e,node:{...this.#e.node,conditionExpression:n}})}item(e){return new s({...this.#e,node:{...this.#e.node,item:{kind:"ItemNode",item:e}}})}returnValues(e){return new s({...this.#e,node:{...this.#e.node,returnValues:{kind:"ReturnValuesNode",option:e}}})}execute=async()=>{let e=this.#e.queryCompiler.compile(this.#e.node);return (await this.#e.ddbClient.send(e)).Attributes}};D(b,"Don't await PutQueryBuilder instances directly. To execute the query you need to call the `execute` method");var c=class s{#e;constructor(e){this.#e=e;}keyCondition(...e){if(e[1]==="begins_with"){let[t,n,r]=e;return new s({...this.#e,node:{...this.#e.node,keyConditions:this.#e.node.keyConditions.concat({kind:"KeyConditionNode",operation:{kind:"BeginsWithFunctionExpression",key:t,substr:r}})}})}else if(e[1]==="BETWEEN"){let[t,n,r,o]=e;return new s({...this.#e,node:{...this.#e.node,keyConditions:this.#e.node.keyConditions.concat({kind:"KeyConditionNode",operation:{kind:"BetweenConditionExpression",key:t,left:r,right:o}})}})}else {let[t,n,r]=e;return new s({...this.#e,node:{...this.#e.node,keyConditions:this.#e.node.keyConditions.concat({kind:"KeyConditionNode",operation:{kind:"KeyConditionComparatorExpression",operation:n,key:t,value:r}})}})}}filterExpression(...e){let n=new d({node:{...this.#e.node.filterExpression}}).expression(...e)._getNode();return new s({...this.#e,node:{...this.#e.node,filterExpression:n}})}orFilterExpression(...e){let n=new d({node:{...this.#e.node.filterExpression}}).orExpression(...e)._getNode();return new s({...this.#e,node:{...this.#e.node,filterExpression:n}})}limit(e){return new s({...this.#e,node:{...this.#e.node,limit:{kind:"LimitNode",limit:e}}})}scanIndexForward(e){return new s({...this.#e,node:{...this.#e.node,scanIndexForward:{kind:"ScanIndexForwardNode",enabled:e}}})}consistentRead(e){return new s({...this.#e,node:{...this.#e.node,consistentRead:{kind:"ConsistentReadNode",enabled:e}}})}attributes(e){return new s({...this.#e,node:{...this.#e.node,attributes:{kind:"AttributesNode",attributes:e}}})}execute=async()=>{let e=this.#e.queryCompiler.compile(this.#e.node);return (await this.#e.ddbClient.send(e)).Items??void 0}};D(c,"Don't await QueryQueryBuilder instances directly. To execute the query you need to call the `execute` method");var x=class{#e;constructor(e){this.#e=e;}getItem(e){return new y({node:{kind:"GetNode",table:{kind:"TableNode",table:e}},ddbClient:this.#e.ddbClient,queryCompiler:this.#e.queryCompiler})}query(e){return new c({node:{kind:"QueryNode",table:{kind:"TableNode",table:e},keyConditions:[],filterExpression:{kind:"ExpressionNode",expressions:[]}},ddbClient:this.#e.ddbClient,queryCompiler:this.#e.queryCompiler})}putItem(e){return new b({node:{kind:"PutNode",table:{kind:"TableNode",table:e},conditionExpression:{kind:"ExpressionNode",expressions:[]}},ddbClient:this.#e.ddbClient,queryCompiler:this.#e.queryCompiler})}deleteItem(e){return new p({node:{kind:"DeleteNode",table:{kind:"TableNode",table:e},conditionExpression:{kind:"ExpressionNode",expressions:[]}},ddbClient:this.#e.ddbClient,queryCompiler:this.#e.queryCompiler})}};var P=/\[\d+\]/g,m=s=>s.split(".").map(e=>`#${e}`).join("."),E=s=>s.replaceAll(P,"").split(".").map(e=>[`#${e}`,e]);function T(s,e){for(let t in e)s.set(t,e[t]);}var B=class{compile(e){switch(e.kind){case"GetNode":return this.compileGetNode(e);case"QueryNode":return this.compileQueryNode(e);case"PutNode":return this.compilePutNode(e);case"DeleteNode":return this.compileDeleteNode(e)}}compileGetNode(e){let{table:t,keys:n,consistentRead:r,attributes:o}=e,{ProjectionExpression:i,ExpressionAttributeNames:a}=this.compileAttributeNamesNode(o);return new libDynamodb.GetCommand({TableName:t.table,Key:n?.keys,ConsistentRead:r?.enabled,ProjectionExpression:i,ExpressionAttributeNames:a})}compileQueryNode(e){let{table:t,filterExpression:n,keyConditions:r,limit:o,scanIndexForward:i,consistentRead:a,attributes:u}=e,l=new Map,K=new Map,f=new Map,N=this.compileKeyConditionExpression(r,K,l),O=this.compileExpression(n,f,l),{ProjectionExpression:g,ExpressionAttributeNames:A}=this.compileAttributeNamesNode(u);return new libDynamodb.QueryCommand({TableName:t.table,KeyConditionExpression:N,FilterExpression:O||void 0,Limit:o?.limit,ExpressionAttributeValues:{...Object.fromEntries(K),...Object.fromEntries(f)},ScanIndexForward:i?.enabled,ConsistentRead:a?.enabled,ProjectionExpression:g,ExpressionAttributeNames:l.size>0||A?{...Object.fromEntries(l),...A}:void 0})}compilePutNode(e){let{table:t,item:n,returnValues:r,conditionExpression:o}=e,i=new Map,a=new Map,u=this.compileExpression(o,a,i);return new libDynamodb.PutCommand({TableName:t.table,Item:n?.item,ReturnValues:r?.option,ConditionExpression:u||void 0,ExpressionAttributeValues:a.size>0?{...Object.fromEntries(a)}:void 0,ExpressionAttributeNames:i.size>0?{...Object.fromEntries(i)}:void 0})}compileDeleteNode(e){let{table:t,returnValues:n,returnValuesOnConditionCheckFailure:r,keys:o,conditionExpression:i}=e,a=new Map,u=new Map,l=this.compileExpression(i,u,a);return new libDynamodb.DeleteCommand({TableName:t.table,Key:o?.keys,ReturnValues:n?.option,ReturnValuesOnConditionCheckFailure:r?.option,ConditionExpression:l||void 0,ExpressionAttributeValues:u.size>0?{...Object.fromEntries(u)}:void 0,ExpressionAttributeNames:a.size>0?{...Object.fromEntries(a)}:void 0})}compileAttributeNamesNode(e){let t=e?.attributes.map(r=>m(r)).join(", "),n=e?.attributes.map(r=>E(r)).reduce((r,o)=>(o.forEach(([i,a])=>{r[i]=a;}),r),{});return {ProjectionExpression:t,ExpressionAttributeNames:n}}compileAttributeName(e){let t=m(e),n=E(e).reduce((r,[o,i])=>(r[o]=i,r),{});return {expressionAttributeName:t,attributeNameMap:n}}compileExpression=(e,t,n)=>{let r="";return e.expressions.forEach((o,i)=>{i!==0&&(r+=` ${o.joinType} `),r+=this.compileFilterExpressionJoinNodes(o,t,n);}),r};compileFilterExpressionJoinNodes=({expr:e},t,n)=>{let r="",i=`:filterExpressionValue${t.size}`,a;if("key"in e){let{expressionAttributeName:u,attributeNameMap:l}=this.compileAttributeName(e.key);a=u,T(n,l);}switch(e.kind){case"ExpressionNode":{r+="(",r+=this.compileExpression(e,t,n),r+=")";break}case"ExpressionComparatorExpressions":{r+=`${a} ${e.operation} ${i}`,t.set(i,e.value);break}case"ExpressionNotExpression":{r+="NOT (",r+=this.compileExpression(e.expr,t,n),r+=")";break}case"BetweenConditionExpression":{r+=`${a} BETWEEN ${i}left AND ${i}right`,t.set(`${i}left`,e.left),t.set(`${i}right`,e.right);break}case"AttributeExistsFunctionExpression":{r+=`attribute_exists(${a})`;break}case"AttributeNotExistsFunctionExpression":{r+=`attribute_not_exists(${a})`;break}case"BeginsWithFunctionExpression":{r+=`begins_with(${a}, ${i})`,t.set(i,e.substr);break}case"ContainsFunctionExpression":{r+=`contains(${a}, ${i})`,t.set(i,e.value);break}}return r};compileKeyConditionExpression=(e,t,n)=>{let r="";return e.forEach((o,i)=>{i!==0&&(r+=" AND ");let{expressionAttributeName:a,attributeNameMap:u}=this.compileAttributeName(o.operation.key),l=`:keyConditionValue${i}`;o.operation.kind==="KeyConditionComparatorExpression"?(r+=`${a} ${o.operation.operation} ${l}`,t.set(l,o.operation.value)):o.operation.kind==="BetweenConditionExpression"?(r+=`${a} BETWEEN ${l}left AND ${l}right`,t.set(`${l}left`,o.operation.left),t.set(`${l}right`,o.operation.right)):o.operation.kind==="BeginsWithFunctionExpression"&&(r+=`begins_with(${a}, ${l})`,t.set(l,o.operation.substr)),T(n,u);}),r}};var h=class extends x{constructor(e){let t=new B;super({...e,queryCompiler:t});}}; | ||
exports.Tsynamo = O; | ||
exports.Tsynamo = h; | ||
//# sourceMappingURL=out.js.map | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "tsynamo", | ||
"author": "woltsu", | ||
"version": "0.0.6", | ||
"version": "0.0.7", | ||
"description": "Typed query builder for DynamoDB", | ||
@@ -6,0 +6,0 @@ "main": "dist/index.js", |
@@ -67,3 +67,3 @@ # Tsynamo | ||
await tsynamoClient | ||
.getItemFrom("UserEvents") | ||
.getItem("UserEvents") | ||
.keys({ | ||
@@ -206,4 +206,27 @@ userId: "123", | ||
WIP | ||
### Simple delete item | ||
```ts | ||
await tsynamoClient | ||
.deleteItem("myTable") | ||
.keys({ | ||
userId: "123", | ||
eventId: 313, | ||
}) | ||
.execute(); | ||
``` | ||
### Simple delete item with ConditionExpression | ||
```ts | ||
await tsynamoClient | ||
.deleteItem("myTable") | ||
.keys({ | ||
userId: "123", | ||
eventId: 313, | ||
}) | ||
.conditionExpression("eventType", "attribute_not_exists") | ||
.execute(); | ||
``` | ||
## Update item | ||
@@ -210,0 +233,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
225384
542
245
0