@ginger.io/beyonce
Advanced tools
Comparing version 0.0.14 to 0.0.15
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
function generateModelHelpers(models) { | ||
const helpers = models.map(m => { | ||
const modelHelperName = m.name.replace(/^\w/, _ => _.toLowerCase()); | ||
const helpers = models.map((m) => { | ||
const modelHelperName = m.name.replace(/^\w/, (_) => _.toLowerCase()); | ||
return `export function ${modelHelperName}(fields: Omit<${m.name}, "model">): ${m.name} { | ||
@@ -7,0 +7,0 @@ return { |
@@ -31,2 +31,3 @@ "use strict"; | ||
const generateModelHelpers_1 = require("./generateModelHelpers"); | ||
const generateTaggedUnion_1 = require("./generateTaggedUnion"); | ||
function generateModels(yamlData) { | ||
@@ -41,4 +42,5 @@ const config = parseYaml(yamlData); | ||
const tables = generateTables_1.generateTables(tableDefs); | ||
const taggedUnion = generateTaggedUnion_1.generateTaggedUnion(models); | ||
const imports = new Set([`import { key, Model } from "@ginger.io/beyonce"`]); | ||
modelInterfaces.imports.forEach(_ => imports.add(_)); | ||
modelInterfaces.imports.forEach((_) => imports.add(_)); | ||
const modelHelpers = generateModelHelpers_1.generateModelHelpers(models); | ||
@@ -54,2 +56,4 @@ const code = ` | ||
${taggedUnion} | ||
${tables} | ||
@@ -59,3 +63,3 @@ `; | ||
parser: "typescript", | ||
semi: false | ||
semi: false, | ||
}); | ||
@@ -73,3 +77,3 @@ } | ||
gsis: [], | ||
models: [] | ||
models: [], | ||
}; | ||
@@ -87,3 +91,3 @@ if (GSIs !== undefined) { | ||
sort, | ||
fields | ||
fields, | ||
}); | ||
@@ -90,0 +94,0 @@ }); |
import { DynamoDB } from "aws-sdk"; | ||
import { JayZConfig } from "./JayZConfig"; | ||
import { Key } from "./Key"; | ||
import { Model } from "./Model"; | ||
import { QueryBuilder } from "./QueryBuilder"; | ||
@@ -19,15 +18,15 @@ import { ItemAndKey, PartitionAndSortKey } from "./util"; | ||
/** Retrieve a single Item out of Dynamo */ | ||
get<T extends Model, U extends Model>(key: PartitionAndSortKey<T, U>): Promise<U | undefined>; | ||
get<T, U>(key: PartitionAndSortKey<T, U>): Promise<U | undefined>; | ||
/** BatchGet items */ | ||
batchGet<T extends Model>(params: { | ||
batchGet<T>(params: { | ||
keys: PartitionAndSortKey<any, T>[]; | ||
}): Promise<T[]>; | ||
query<T extends Model>(pk: Key<T>): QueryBuilder<T>; | ||
queryGSI<T extends Model>(gsiName: string, gsiPk: Key<T>): QueryBuilder<T>; | ||
query<T>(pk: Key<T>): QueryBuilder<T>; | ||
queryGSI<T>(gsiName: string, gsiPk: Key<T>): QueryBuilder<T>; | ||
/** Write an item into Dynamo */ | ||
put<T extends Model>(itemAndKey: ItemAndKey<T>): Promise<void>; | ||
put<T>(itemAndKey: ItemAndKey<T>): Promise<void>; | ||
/** Write multiple items into Dynamo using a transaction. | ||
*/ | ||
batchPutWithTransaction<T extends Model>(items: ItemAndKey<T>[]): Promise<void>; | ||
batchPutWithTransaction<T>(items: ItemAndKey<T>[]): Promise<void>; | ||
private maybeEncryptItems; | ||
} |
@@ -1,6 +0,5 @@ | ||
import { Model } from "./Model"; | ||
/** A DynamoDB partition or sort key. T is the type of model that lives under this key in Dynamo | ||
* (it might be a union type or a single type). And we use a class here to "hold onto" that type | ||
*/ | ||
export declare class Key<T extends Model> { | ||
export declare class Key<T> { | ||
readonly name: string; | ||
@@ -10,2 +9,2 @@ readonly value: string; | ||
} | ||
export declare function key<T, U extends Model>(name: string, createKey: (input: T) => string[]): (input: T) => Key<U>; | ||
export declare function key<T, U>(name: string, createKey: (input: T) => string[]): (input: T) => Key<U>; |
@@ -5,5 +5,4 @@ import { DynamoDB } from "aws-sdk"; | ||
import { Key } from "./Key"; | ||
import { Model } from "./Model"; | ||
declare type Operator = "=" | "<>" | "<" | "<=" | ">" | ">="; | ||
declare type TableQueryParams<T extends Model> = { | ||
declare type TableQueryParams<T> = { | ||
db: DynamoDB.DocumentClient; | ||
@@ -14,3 +13,3 @@ tableName: string; | ||
}; | ||
declare type GSIQueryParams<T extends Model> = { | ||
declare type GSIQueryParams<T> = { | ||
db: DynamoDB.DocumentClient; | ||
@@ -23,3 +22,3 @@ tableName: string; | ||
/** Builds and executes parameters for a DynamoDB Query operation */ | ||
export declare class QueryBuilder<T extends Model> { | ||
export declare class QueryBuilder<T extends Record<string, any>> { | ||
private config; | ||
@@ -26,0 +25,0 @@ private filterExp; |
@@ -81,3 +81,3 @@ "use strict"; | ||
ExpressionAttributeValues: variables, | ||
FilterExpression: filterExp | ||
FilterExpression: filterExp, | ||
}; | ||
@@ -94,3 +94,3 @@ } | ||
ExpressionAttributeValues: variables, | ||
FilterExpression: filterExp | ||
FilterExpression: filterExp, | ||
}; | ||
@@ -97,0 +97,0 @@ } |
import { EncryptedJayZItem } from "@ginger.io/jay-z"; | ||
import { JayZConfig } from "./JayZConfig"; | ||
import { Key } from "./Key"; | ||
import { Model } from "./Model"; | ||
export declare type ItemAndKey<T extends Model> = { | ||
export declare type ItemAndKey<T> = { | ||
key: PartitionAndSortKey<any, T>; | ||
item: T; | ||
}; | ||
export declare type PartitionAndSortKey<T extends Model, U extends Model> = { | ||
export declare type PartitionAndSortKey<T, U> = { | ||
partition: Key<T>; | ||
sort: Key<U>; | ||
}; | ||
export declare type MaybeEncryptedItems<T extends Model> = EncryptedJayZItem<T & { | ||
export declare type MaybeEncryptedItems<T> = EncryptedJayZItem<T & { | ||
[key: string]: string; | ||
@@ -21,3 +20,3 @@ }, string> | (T & { | ||
}): T; | ||
export declare function encryptOrPassThroughItems<T extends Model>(jayz: JayZConfig | undefined, item: T & { | ||
export declare function encryptOrPassThroughItems<T>(jayz: JayZConfig | undefined, item: T & { | ||
[key: string]: string; | ||
@@ -24,0 +23,0 @@ }): Promise<MaybeEncryptedItems<T>>; |
export { Beyonce } from "./dynamo/Beyonce"; | ||
export { key } from "./dynamo/Key"; | ||
export { Model } from "./dynamo/Model"; |
{ | ||
"name": "@ginger.io/beyonce", | ||
"version": "0.0.14", | ||
"version": "0.0.15", | ||
"description": "Type-safe DynamoDB query builder for TypeScript. Designed with single-table architecture in mind.", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
import { Model } from "./types" | ||
export function generateModelHelpers(models: Model[]): string { | ||
const helpers = models.map(m => { | ||
const modelHelperName = m.name.replace(/^\w/, _ => _.toLowerCase()) | ||
const helpers = models.map((m) => { | ||
const modelHelperName = m.name.replace(/^\w/, (_) => _.toLowerCase()) | ||
@@ -7,0 +7,0 @@ return `export function ${modelHelperName}(fields: Omit<${m.name}, "model">): ${m.name} { |
@@ -9,2 +9,3 @@ import yaml from "js-yaml" | ||
import { generateModelHelpers } from "./generateModelHelpers" | ||
import { generateTaggedUnion } from "./generateTaggedUnion" | ||
@@ -22,5 +23,6 @@ export function generateModels(yamlData: string): string { | ||
const tables = generateTables(tableDefs) | ||
const taggedUnion = generateTaggedUnion(models) | ||
const imports = new Set([`import { key, Model } from "@ginger.io/beyonce"`]) | ||
modelInterfaces.imports.forEach(_ => imports.add(_)) | ||
modelInterfaces.imports.forEach((_) => imports.add(_)) | ||
@@ -38,2 +40,4 @@ const modelHelpers = generateModelHelpers(models) | ||
${taggedUnion} | ||
${tables} | ||
@@ -44,3 +48,3 @@ ` | ||
parser: "typescript", | ||
semi: false | ||
semi: false, | ||
}) | ||
@@ -60,3 +64,3 @@ } | ||
gsis: [], | ||
models: [] | ||
models: [], | ||
} | ||
@@ -76,3 +80,3 @@ | ||
sort, | ||
fields | ||
fields, | ||
}) | ||
@@ -79,0 +83,0 @@ } |
import { DynamoDB } from "aws-sdk" | ||
import { JayZConfig } from "./JayZConfig" | ||
import { Key } from "./Key" | ||
import { Model } from "./Model" | ||
import { QueryBuilder } from "./QueryBuilder" | ||
@@ -39,5 +38,3 @@ import { | ||
/** Retrieve a single Item out of Dynamo */ | ||
async get<T extends Model, U extends Model>( | ||
key: PartitionAndSortKey<T, U> | ||
): Promise<U | undefined> { | ||
async get<T, U>(key: PartitionAndSortKey<T, U>): Promise<U | undefined> { | ||
const { Item: item } = await this.client | ||
@@ -59,3 +56,3 @@ .get({ | ||
/** BatchGet items */ | ||
async batchGet<T extends Model>(params: { | ||
async batchGet<T>(params: { | ||
keys: PartitionAndSortKey<any, T>[] | ||
@@ -96,3 +93,3 @@ }): Promise<T[]> { | ||
query<T extends Model>(pk: Key<T>): QueryBuilder<T> { | ||
query<T>(pk: Key<T>): QueryBuilder<T> { | ||
const { tableName, jayz } = this | ||
@@ -107,3 +104,3 @@ return new QueryBuilder<T>({ | ||
queryGSI<T extends Model>(gsiName: string, gsiPk: Key<T>): QueryBuilder<T> { | ||
queryGSI<T>(gsiName: string, gsiPk: Key<T>): QueryBuilder<T> { | ||
const { tableName, jayz } = this | ||
@@ -120,3 +117,3 @@ return new QueryBuilder<T>({ | ||
/** Write an item into Dynamo */ | ||
async put<T extends Model>(itemAndKey: ItemAndKey<T>): Promise<void> { | ||
async put<T>(itemAndKey: ItemAndKey<T>): Promise<void> { | ||
const item = await this.maybeEncryptItems(itemAndKey) | ||
@@ -134,5 +131,3 @@ | ||
*/ | ||
async batchPutWithTransaction<T extends Model>( | ||
items: ItemAndKey<T>[] | ||
): Promise<void> { | ||
async batchPutWithTransaction<T>(items: ItemAndKey<T>[]): Promise<void> { | ||
const asyncEncryptedItems = items.map(async (itemAndKey) => { | ||
@@ -152,3 +147,3 @@ const maybeEncryptedItem = await this.maybeEncryptItems(itemAndKey) | ||
private async maybeEncryptItems<T extends Model>( | ||
private async maybeEncryptItems<T>( | ||
itemAndKey: ItemAndKey<T> | ||
@@ -155,0 +150,0 @@ ): Promise<MaybeEncryptedItems<T>> { |
@@ -1,11 +0,9 @@ | ||
import { Model } from "./Model" | ||
/** A DynamoDB partition or sort key. T is the type of model that lives under this key in Dynamo | ||
* (it might be a union type or a single type). And we use a class here to "hold onto" that type | ||
*/ | ||
export class Key<T extends Model> { | ||
export class Key<T> { | ||
constructor(public readonly name: string, public readonly value: string) {} | ||
} | ||
export function key<T, U extends Model>( | ||
export function key<T, U>( | ||
name: string, | ||
@@ -12,0 +10,0 @@ createKey: (input: T) => string[] |
@@ -5,3 +5,2 @@ import { DynamoDB } from "aws-sdk" | ||
import { Key } from "./Key" | ||
import { Model } from "./Model" | ||
import { decryptOrPassThroughItem, toJSON } from "./util" | ||
@@ -11,3 +10,3 @@ | ||
type TableQueryParams<T extends Model> = { | ||
type TableQueryParams<T> = { | ||
db: DynamoDB.DocumentClient | ||
@@ -19,3 +18,3 @@ tableName: string | ||
type GSIQueryParams<T extends Model> = { | ||
type GSIQueryParams<T> = { | ||
db: DynamoDB.DocumentClient | ||
@@ -29,3 +28,3 @@ tableName: string | ||
/** Builds and executes parameters for a DynamoDB Query operation */ | ||
export class QueryBuilder<T extends Model> { | ||
export class QueryBuilder<T extends Record<string, any>> { | ||
private filterExp: string[] = [] | ||
@@ -68,3 +67,3 @@ private attributes = new Attributes() | ||
if (items !== undefined) { | ||
const jsonItems = items.map(async _ => { | ||
const jsonItems = items.map(async (_) => { | ||
const item = await decryptOrPassThroughItem(this.config.jayz, _) | ||
@@ -116,3 +115,3 @@ return toJSON<T>(item) | ||
ExpressionAttributeValues: variables, | ||
FilterExpression: filterExp | ||
FilterExpression: filterExp, | ||
} | ||
@@ -132,3 +131,3 @@ } else { | ||
ExpressionAttributeValues: variables, | ||
FilterExpression: filterExp | ||
FilterExpression: filterExp, | ||
} | ||
@@ -139,3 +138,3 @@ } | ||
function isTableQuery<T extends Model>( | ||
function isTableQuery<T>( | ||
query: TableQueryParams<T> | GSIQueryParams<T> | ||
@@ -142,0 +141,0 @@ ): query is TableQueryParams<T> { |
import { EncryptedJayZItem } from "@ginger.io/jay-z" | ||
import { JayZConfig } from "./JayZConfig" | ||
import { Key } from "./Key" | ||
import { Model } from "./Model" | ||
export type ItemAndKey<T extends Model> = { | ||
export type ItemAndKey<T> = { | ||
key: PartitionAndSortKey<any, T> | ||
@@ -11,3 +10,3 @@ item: T | ||
export type PartitionAndSortKey<T extends Model, U extends Model> = { | ||
export type PartitionAndSortKey<T, U> = { | ||
partition: Key<T> | ||
@@ -17,3 +16,3 @@ sort: Key<U> | ||
export type MaybeEncryptedItems<T extends Model> = | ||
export type MaybeEncryptedItems<T> = | ||
| EncryptedJayZItem< | ||
@@ -33,3 +32,3 @@ T & { | ||
export async function encryptOrPassThroughItems<T extends Model>( | ||
export async function encryptOrPassThroughItems<T>( | ||
jayz: JayZConfig | undefined, | ||
@@ -36,0 +35,0 @@ item: T & { [key: string]: string } |
export { Beyonce } from "./dynamo/Beyonce" | ||
export { key } from "./dynamo/Key" | ||
export { Model } from "./dynamo/Model" |
@@ -47,2 +47,4 @@ import { generateModels } from "../../main/codegen/generateModels" | ||
const authorAndBookUnion = `export type Model = Author | Book` | ||
it("should generate a simple model", () => { | ||
@@ -73,2 +75,4 @@ const result = generateModels(` | ||
export type Model = Author | ||
export const LibraryTable = { | ||
@@ -127,2 +131,4 @@ name: "Library", | ||
${authorAndBookUnion} | ||
export const LibraryTable = { | ||
@@ -179,2 +185,4 @@ name: "Library", | ||
${authorAndBookUnion} | ||
export const LibraryTable = { | ||
@@ -238,2 +246,4 @@ name: "Library", | ||
${authorAndBookUnion} | ||
export const LibraryTable = { | ||
@@ -322,3 +332,3 @@ name: "Library", | ||
const lines = result.split("\n").map(_ => _.trim()) | ||
const lines = result.split("\n").map((_) => _.trim()) | ||
expect(lines).toContainEqual( | ||
@@ -325,0 +335,0 @@ `import { BestNameEvah } from \"@cool.io/some/sweet/package\"` |
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
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
114107
88
2237