Skip to content

Commit

Permalink
fix: change UserPoolId to Id in user pool storage
Browse files Browse the repository at this point in the history
BREAKING CHANGE: Nit pick of a breaking change, make the user pool
database representation match what AWS reponds with from the API;
keeping consistent with their response format should make it easier for
us to implement APIs later. Sorry for the breakage.

Migration steps:

1. Open any database json files and rename the UserPoolId key to Id
  • Loading branch information
jagregory committed May 3, 2020
1 parent 6e0c18f commit 71f5e52
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 33 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ You can edit that `.cognito/config.json` and add any of the following settings:
| `TriggerFunctions` | `object` | `{}` | Trigger name to Function name mapping |
| `TriggerFunctions.UserMigration` | `string` | | User Migration lambda name |
| `UserPoolDefaults` | `object` | | Default behaviour to use for the User Pool |
| `UserPoolDefaults.UserPoolId` | `string` | `local` | Default User Pool Id |
| `UserPoolDefaults.Id` | `string` | `local` | Default User Pool Id |
| `UserPoolDefaults.UsernameAttributes` | `string[]` | `["email"]` | Username alias attributes |

The default config is:
Expand All @@ -87,7 +87,7 @@ The default config is:
},
"TriggerFunctions": {},
"UserPoolDefaults": {
"UserPoolId": "local",
"Id": "local",
"UsernameAttributes": ["email"]
}
}
Expand Down
8 changes: 5 additions & 3 deletions integration-tests/dataStore.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@ describe("Data Store", () => {
});
});

describe("get", () => {
it("returns entire db if no key used", async () => {
describe("getRoot", () => {
it("returns entire db", async () => {
const dataStore = await createDataStore(
"example",
{ DefaultValue: true },
Expand All @@ -103,11 +103,13 @@ describe("Data Store", () => {

await dataStore.set("key", "value");

const result = await dataStore.get();
const result = await dataStore.getRoot();

expect(result).toEqual({ DefaultValue: true, key: "value" });
});
});

describe("get", () => {
it("returns a default", async () => {
const dataStore = await createDataStore(
"example",
Expand Down
14 changes: 7 additions & 7 deletions integration-tests/userPool.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ describe("User Pool", () => {

it("creates a database", async () => {
await createUserPool(
{ UserPoolId: "local", UsernameAttributes: [] },
{ Id: "local", UsernameAttributes: [] },
tmpCreateDataStore
);

Expand All @@ -35,7 +35,7 @@ describe("User Pool", () => {
it("saves a user with their username as an additional attribute", async () => {
const now = new Date().getTime();
const userPool = await createUserPool(
{ UserPoolId: "local", UsernameAttributes: [] },
{ Id: "local", UsernameAttributes: [] },
tmpCreateDataStore
);

Expand All @@ -52,7 +52,7 @@ describe("User Pool", () => {
const file = JSON.parse(await readFile(path + "/local.json", "utf-8"));

expect(file).toEqual({
Options: { UserPoolId: "local", UsernameAttributes: [] },
Options: { Id: "local", UsernameAttributes: [] },
Users: {
"1": {
Username: "1",
Expand All @@ -73,7 +73,7 @@ describe("User Pool", () => {
it("updates a user", async () => {
const now = new Date().getTime();
const userPool = await createUserPool(
{ UserPoolId: "local", UsernameAttributes: [] },
{ Id: "local", UsernameAttributes: [] },
tmpCreateDataStore
);

Expand All @@ -91,7 +91,7 @@ describe("User Pool", () => {
let file = JSON.parse(await readFile(path + "/local.json", "utf-8"));

expect(file).toEqual({
Options: { UserPoolId: "local", UsernameAttributes: [] },
Options: { Id: "local", UsernameAttributes: [] },
Users: {
"1": {
Username: "1",
Expand Down Expand Up @@ -122,7 +122,7 @@ describe("User Pool", () => {
file = JSON.parse(await readFile(path + "/local.json", "utf-8"));

expect(file).toEqual({
Options: { UserPoolId: "local", UsernameAttributes: [] },
Options: { Id: "local", UsernameAttributes: [] },
Users: {
"1": {
Username: "1",
Expand All @@ -145,7 +145,7 @@ describe("User Pool", () => {
let userPool: UserPool;
beforeAll(async () => {
userPool = await createUserPool(
{ UserPoolId: "local", UsernameAttributes: [] },
{ Id: "local", UsernameAttributes: [] },
tmpCreateDataStore
);

Expand Down
4 changes: 2 additions & 2 deletions src/server/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ const defaults: Config = {
},
TriggerFunctions: {},
UserPoolDefaults: {
UserPoolId: "local",
Id: "local",
UsernameAttributes: ["email"],
},
};

export const loadConfig = async (): Promise<Config> => {
const dataStore = await createDataStore("config", defaults, ".cognito");

const config = await dataStore.get<Config>();
const config = await dataStore.getRoot<Config>();

return deepmerge(defaults, config ?? {});
};
18 changes: 9 additions & 9 deletions src/services/dataStore.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import StormDB from "stormdb";
import fs from "fs";
import StormDB from "stormdb";
import { promisify } from "util";

const mkdir = promisify(fs.mkdir);

export interface DataStore {
get<T>(key?: string): Promise<T | null>;
getRoot<T>(): Promise<T | null>;
get<T>(key: string): Promise<T | null>;
get<T>(key: string, defaultValue: T): Promise<T>;
set<T>(key: string, value: T): Promise<void>;
}

Expand All @@ -30,14 +32,12 @@ export const createDataStore: CreateDataStore = async (
db.default(defaults);

return {
async get(key?: string) {
if (!key) {
return db.value();
}

const result = await db.get(key).value();
async getRoot() {
return (await db.value()) ?? null;
},

return result ?? null;
async get(key: string, defaultValue?: unknown) {
return (await db.get(key).value()) ?? defaultValue ?? null;
},

async set(key, value) {
Expand Down
9 changes: 5 additions & 4 deletions src/services/userPool.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ describe("User Pool", () => {
mockDataStore = {
set: jest.fn(),
get: jest.fn(),
getRoot: jest.fn(),
};
});

Expand All @@ -24,12 +25,12 @@ describe("User Pool", () => {
() => mockDataStore
) as unknown) as CreateDataStore;
await createUserPool(
{ UserPoolId: "local", UsernameAttributes: [] },
{ Id: "local", UsernameAttributes: [] },
createDataStore
);

expect(createDataStore).toHaveBeenCalledWith("local", {
Options: { UserPoolId: "local", UsernameAttributes: [] },
Options: { Id: "local", UsernameAttributes: [] },
Users: {},
});
});
Expand All @@ -38,7 +39,7 @@ describe("User Pool", () => {
it("saves a user with their username as an additional attribute", async () => {
const now = new Date().getTime();
const userPool = await createUserPool(
{ UserPoolId: "local", UsernameAttributes: [] },
{ Id: "local", UsernameAttributes: [] },
() => Promise.resolve(mockDataStore)
);

Expand Down Expand Up @@ -81,7 +82,7 @@ describe("User Pool", () => {

beforeAll(async () => {
const options = {
UserPoolId: "local",
Id: "local",
UsernameAttributes: username_attributes,
};
const users = {
Expand Down
15 changes: 9 additions & 6 deletions src/services/userPool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,25 +52,28 @@ export interface UserPool {
type UsernameAttribute = "email" | "phone_number";

export interface UserPoolOptions {
UserPoolId?: string;
Id: string;
UsernameAttributes?: UsernameAttribute[];
}

export const createUserPool = async (
options: UserPoolOptions,
defaultOptions: UserPoolOptions,
createDataStore: CreateDataStore
): Promise<UserPool> => {
const dataStore = await createDataStore(options.UserPoolId ?? "local", {
const dataStore = await createDataStore(defaultOptions.Id, {
Users: {},
Options: options,
Options: defaultOptions,
});

return {
async getUserPoolIdForClientId() {
// TODO: support user pool to client mapping
const options = await dataStore.get<UserPoolOptions>("Options");
const options = await dataStore.get<UserPoolOptions>(
"Options",
defaultOptions
);

return Promise.resolve(options?.UserPoolId ?? "local");
return Promise.resolve(options.Id);
},

async getUserByUsername(username) {
Expand Down

0 comments on commit 71f5e52

Please sign in to comment.