Skip to content

Commit

Permalink
feature: Indexes with sort keys
Browse files Browse the repository at this point in the history
Add the ability to add sort keys to indexes and querying with those
sort keys as well.
  • Loading branch information
tgandrews committed Jun 18, 2022
1 parent c825847 commit 5baa65b
Show file tree
Hide file tree
Showing 6 changed files with 192 additions and 14 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ coverage/
.tern-port
lib
.npmrc
.vscode
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,8 @@ indexes.
Indexes should be created as a part of your table creation but need to be defined with Omanyd so
they can be used at run time correctly.

Indexes must have a hash key but can also have a sort key.

```ts
import Omanyd from "omanyd";
import Joi from "joi";
Expand Down
149 changes: 144 additions & 5 deletions src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ describe("omanyd", () => {

await Omanyd.createTables();

await expect(async () =>
await expect(
ThingStore.create({
value: () => {
return 1 + 1;
Expand All @@ -418,7 +418,7 @@ describe("omanyd", () => {

await Omanyd.createTables();

await expect(async () =>
await expect(
ThingStore.create({
value: Symbol("hi"),
})
Expand All @@ -441,7 +441,7 @@ describe("omanyd", () => {

await Omanyd.createTables();

await expect(async () =>
await expect(
ThingStore.create({
value: Buffer.from("hello world", "utf-8"),
})
Expand All @@ -464,7 +464,7 @@ describe("omanyd", () => {

await Omanyd.createTables();

await expect(async () =>
await expect(
ThingStore.create({
value: "hello world",
wrongFieldName: "thing",
Expand Down Expand Up @@ -732,10 +732,149 @@ describe("omanyd", () => {

await Omanyd.createTables();

await expect(async () =>
await expect(
ThingStore.getByIndex("ValueIndex", "hello@world.com")
).rejects.toThrow(/No index found with name: 'ValueIndex'/);
});
});
describe("index with sort keys", () => {
it("should be able to define and retrieve by that index", async () => {
interface Thing {
id: string;
email: string;
anotherField: string;
}
const ThingStore = Omanyd.define<Thing>({
name: "indexSKQuery",
hashKey: "id",
schema: Joi.object({
id: Omanyd.types.id(),
email: Joi.string().required(),
anotherField: Joi.string().required(),
}),
indexes: [
{
name: "ValueIndex",
type: "global",
hashKey: "email",
sortKey: "anotherField",
},
],
});

await Omanyd.createTables();

const savedItem1 = await ThingStore.create({
email: "hello@world.com",
anotherField: "1",
});
const savedItem2 = await ThingStore.create({
email: "hello@world.com",
anotherField: "2",
});

const readItem1 = await ThingStore.getByIndex(
"ValueIndex",
"hello@world.com",
"1"
);

const readItem2 = await ThingStore.getByIndex(
"ValueIndex",
"hello@world.com",
"2"
);

expect(savedItem1).toStrictEqual(readItem1);
expect(savedItem2).toStrictEqual(readItem2);

expect(savedItem1).not.toStrictEqual(readItem2);
expect(savedItem2).not.toStrictEqual(readItem1);
});

it("should return null if the item is not found", async () => {
interface Thing {
id: string;
email: string;
anotherField: string;
}
const ThingStore = Omanyd.define<Thing>({
name: "indexSKQueryNotFound",
hashKey: "id",
schema: Joi.object({
id: Omanyd.types.id(),
email: Joi.string().required(),
anotherField: Joi.string().required(),
}),
indexes: [
{
name: "ValueIndex",
type: "global",
hashKey: "email",
sortKey: "anotherField",
},
],
});

await Omanyd.createTables();

const readItem = await ThingStore.getByIndex(
"ValueIndex",
"hello@world.com",
"1"
);

expect(readItem).toBeNull();
});

it("should throw if the index does not exist", async () => {
interface Thing {
id: string;
email: string;
}
const ThingStore = Omanyd.define<Thing>({
name: "indexSKNotDefined",
hashKey: "id",
schema: Joi.object({
id: Omanyd.types.id(),
email: Joi.string().required(),
}),
});

await Omanyd.createTables();

await expect(
ThingStore.getByIndex("ValueIndex", "hello@world.com")
).rejects.toThrow(/No index found with name: 'ValueIndex'/);
});

it("should throw if the index does not have a sort key and one is provided", async () => {
interface Thing {
id: string;
email: string;
}
const ThingStore = Omanyd.define<Thing>({
name: "indexSKNotInIndez",
hashKey: "id",
schema: Joi.object({
id: Omanyd.types.id(),
email: Joi.string().required(),
}),
indexes: [
{
name: "ValueIndex",
type: "global",
hashKey: "email",
},
],
});

await Omanyd.createTables();

await expect(
ThingStore.getByIndex("ValueIndex", "hello@world.com", "1")
).rejects.toThrow(/Index does not have a sort key but one was provided/);
});
});

describe("clearTables", () => {
Expand Down
8 changes: 6 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,12 @@ export function define<T>(options: Options) {
return validated as unknown[] as T[];
},

async getByIndex(name: string, hashKey: string): Promise<T | null> {
const res = await t.getByIndex(name, hashKey);
async getByIndex(
name: string,
hashKey: string,
sortKey?: string
): Promise<T | null> {
const res = await t.getByIndex(name, hashKey, sortKey);
if (res === null) {
return res;
}
Expand Down
45 changes: 38 additions & 7 deletions src/table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ export default class Table {
AttributeName: index.hashKey,
KeyType: "HASH",
},
...(index.sortKey
? [{ AttributeName: index.sortKey, KeyType: "RANGE" }]
: []),
],
Projection: {
ProjectionType: "ALL",
Expand All @@ -55,10 +58,21 @@ export default class Table {
];

if (this.options.indexes) {
const indexDefinitions = this.options.indexes.map((index) => ({
AttributeName: index.hashKey,
AttributeType: "S",
}));
const indexDefinitions = this.options.indexes.flatMap((index) => {
const indexDefinitions = [
{
AttributeName: index.hashKey,
AttributeType: "S",
},
];
if (index.sortKey) {
indexDefinitions.push({
AttributeName: index.sortKey,
AttributeType: "S",
});
}
return indexDefinitions;
});
attributeDefinitions = attributeDefinitions.concat(indexDefinitions);
}

Expand Down Expand Up @@ -248,28 +262,45 @@ export default class Table {

async getByIndex(
indexName: string,
value: string
hashKey: string,
sortKey?: string
): Promise<PlainObject | null> {
const indexDefintion = (this.options.indexes ?? []).find(
(index) => index.name === indexName
);
if (!indexDefintion) {
throw new Error(`No index found with name: '${indexName}'`);
}
if (sortKey && !indexDefintion.sortKey) {
throw new Error("Index does not have a sort key but one was provided");
}

const keyConditionExpression = [`${indexDefintion.hashKey} = :hashKey`];
if (sortKey) {
keyConditionExpression.push(`${indexDefintion.sortKey} = :sortKey`);
}

return new Promise((res, rej) => {
this.dynamoDB.query(
{
TableName: this.options.name,
IndexName: indexDefintion.name,
KeyConditionExpression: `${indexDefintion.hashKey} = :hashKey`,
KeyConditionExpression: keyConditionExpression.join(" AND "),
ExpressionAttributeValues: {
":hashKey": this.serializer.toDynamoValue(
value,
hashKey,
getItemSchemaFromObjectSchema(
this.options.schema,
indexDefintion.hashKey
)
)!,
":sortKey": this.serializer.toDynamoValue(
sortKey,
getItemSchemaFromObjectSchema(
this.options.schema,
indexDefintion.sortKey!
)
)!,
},
},
(err, data) => {
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export interface Options {
name: string;
type: "global";
hashKey: string;
sortKey?: string;
}[];
allowNameClash?: boolean;
}

0 comments on commit 5baa65b

Please sign in to comment.