-
-
Notifications
You must be signed in to change notification settings - Fork 307
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(insert): adds custom ID capabilities (#223)
* feat(insert): adds custom ID capabilities * feat(insert): adds custom IDs to batchInsert function
- Loading branch information
1 parent
b8ecb11
commit bf82b60
Showing
8 changed files
with
223 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
/* IMPORTANT | ||
* This snapshot file is auto-generated, but designed for humans. | ||
* It should be checked into source control and tracked carefully. | ||
* Re-generate by setting TAP_SNAPSHOT=1 and running tests. | ||
* Make sure to inspect the output below. Do not ignore changes! | ||
*/ | ||
'use strict' | ||
exports[`tests/insert.test.ts TAP insert should throw an error if the 'id' field is already taken > must match snapshot 1`] = ` | ||
Error: Document with ID "john-01" already exists. | ||
` | ||
|
||
exports[`tests/insert.test.ts TAP insert should throw an error if the 'id' field is not a string > must match snapshot 1`] = ` | ||
TypeError: "id" must be of type "string". Got "number" instead. | ||
` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
import t from "tap"; | ||
import { insert, insertBatch } from "../src/methods/insert"; | ||
import { create } from "../src/methods/create"; | ||
|
||
t.test("insert", async t => { | ||
t.plan(6); | ||
|
||
t.test("should use the 'id' field found in the document", async t => { | ||
t.plan(2); | ||
|
||
const db = await create({ | ||
schema: { | ||
id: "string", | ||
name: "string", | ||
}, | ||
}); | ||
|
||
const i1 = await insert(db, { | ||
id: "john-01", | ||
name: "John", | ||
}); | ||
|
||
const i2 = await insert(db, { | ||
id: "doe-02", | ||
name: "Doe", | ||
}); | ||
|
||
t.equal(i1.id, "john-01"); | ||
t.equal(i2.id, "doe-02"); | ||
}); | ||
|
||
t.test("should use the custom 'id' function passed in the configuration object", async t => { | ||
t.plan(2); | ||
|
||
const db = await create({ | ||
schema: { | ||
id: "string", | ||
name: "string", | ||
}, | ||
}); | ||
|
||
const i1 = await insert( | ||
db, | ||
{ | ||
id: "john-01", | ||
name: "John", | ||
}, | ||
{ | ||
id: doc => `${doc.name.toLowerCase()}-foo-bar-baz`, | ||
}, | ||
); | ||
|
||
const i2 = await insert(db, { | ||
id: "doe-02", | ||
name: "Doe", | ||
}); | ||
|
||
t.equal(i1.id, "john-foo-bar-baz"); | ||
t.equal(i2.id, "doe-02"); | ||
}); | ||
|
||
t.test("should throw an error if the 'id' field is not a string", async t => { | ||
t.plan(1); | ||
|
||
const db = await create({ | ||
schema: { | ||
id: "string", | ||
name: "string", | ||
}, | ||
}); | ||
|
||
try { | ||
await insert(db, { | ||
// @ts-expect-error error case | ||
id: 123, | ||
name: "John", | ||
}); | ||
} catch (error) { | ||
t.matchSnapshot(error); | ||
} | ||
}); | ||
|
||
t.test("should throw an error if the 'id' field is already taken", async t => { | ||
t.plan(1); | ||
|
||
const db = await create({ | ||
schema: { | ||
id: "string", | ||
name: "string", | ||
}, | ||
}); | ||
|
||
await insert(db, { | ||
id: "john-01", | ||
name: "John", | ||
}); | ||
|
||
try { | ||
await insert(db, { | ||
id: "john-01", | ||
name: "John", | ||
}); | ||
} catch (error) { | ||
t.matchSnapshot(error); | ||
} | ||
}); | ||
|
||
t.test("should take the ID field even if not specified in the schema", async t => { | ||
t.plan(1); | ||
|
||
const db = await create({ | ||
schema: { | ||
name: "string", | ||
}, | ||
}); | ||
|
||
const i1 = await insert(db, { | ||
// @ts-expect-error error case | ||
id: "john-01", | ||
name: "John", | ||
}); | ||
|
||
t.equal(i1.id, "john-01"); | ||
}); | ||
|
||
t.test("custom ID should work with insertBatch as well", async t => { | ||
t.plan(1); | ||
|
||
const db = await create({ | ||
schema: { | ||
id: "string", | ||
name: "string", | ||
}, | ||
}); | ||
|
||
await insertBatch( | ||
db, | ||
[ | ||
{ | ||
id: "01", | ||
name: "John", | ||
}, | ||
{ | ||
id: "02", | ||
name: "Doe", | ||
}, | ||
], | ||
{ | ||
id: doc => `${doc.name.toLowerCase()}-${doc.id}`, | ||
}, | ||
); | ||
|
||
t.same(Object.keys(db.docs), ["john-01", "doe-02"]); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters