-
Notifications
You must be signed in to change notification settings - Fork 110
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #542 from bozzaj/custom-generate
Add ability to use a field-specific custom generate function
- Loading branch information
Showing
6 changed files
with
106 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* | ||
crypto.ts - CRUD with crypto | ||
*/ | ||
import {AWS, Client, Match, Table, print, dump, delay, isV3, isV2} from './utils/init' | ||
import {CustomGenerateSchema} from './schemas' | ||
|
||
const table = new Table({ | ||
name: 'CustomGenerateTestTable', | ||
client: Client, | ||
partial: false, | ||
schema: CustomGenerateSchema, | ||
logger: true, | ||
}) | ||
|
||
let User: any = null | ||
let user: any | ||
let users: any[] | ||
|
||
test('Create Table', async () => { | ||
if (!(await table.exists())) { | ||
await table.createTable() | ||
expect(await table.exists()).toBe(true) | ||
} | ||
User = table.getModel('User') | ||
}) | ||
|
||
test('Create', async () => { | ||
user = await User.create({name: 'Peter Smith'}) | ||
expect(user).toMatchObject({ | ||
name: 'Peter Smith', | ||
}) | ||
}) | ||
|
||
test('Get', async () => { | ||
user = await User.get({id: user.id}) | ||
expect(user).toMatchObject({ | ||
name: 'Peter Smith', | ||
}) | ||
console.log(user) | ||
expect(user.id).toMatch(Match.customUuid) | ||
}) | ||
|
||
test('Get raw ', async () => { | ||
user = await User.get({id: user.id}, {hidden: true, parse: false}) | ||
if (isV3()) { | ||
expect(user.pk.S).toMatch(/^User#/) | ||
} | ||
if (isV2()) { | ||
expect(user.pk).toMatch(/^User#/) | ||
} | ||
}) | ||
|
||
test('Destroy Table', async () => { | ||
await table.deleteTable('DeleteTableForever') | ||
expect(await table.exists()).toBe(false) | ||
}) |
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,44 @@ | ||
|
||
import Crypto from 'crypto' | ||
|
||
/* | ||
Generates a simple uuidv7-like String. | ||
Note: This method is simplified for testing only! | ||
*/ | ||
function customId() { | ||
const randomBytes = Crypto.randomBytes(10); | ||
|
||
randomBytes[2] = randomBytes[2] & 0x3f | 0x80; | ||
|
||
const result = Date.now().toString(16).padStart(12, "0") | ||
+ "7" | ||
+ randomBytes.toString("hex").substring(1) | ||
|
||
return [ | ||
result.substring(0, 8), | ||
result.substring(8, 12), | ||
result.substring(12, 16), | ||
result.substring(16, 20), | ||
result.substring(20) | ||
].join("-") | ||
} | ||
|
||
/* | ||
Schema to use with custom generate function | ||
*/ | ||
export default { | ||
format: 'onetable:1.1.0', | ||
version: '0.0.1', | ||
indexes: { | ||
primary: {hash: 'pk', sort: 'sk'}, | ||
}, | ||
models: { | ||
User: { | ||
pk: {type: String, value: '${_type}#${id}'}, | ||
sk: {type: String, value: '${_type}#'}, | ||
id: {type: String, generate: customId}, | ||
name: {type: String}, | ||
}, | ||
}, | ||
params: {}, | ||
} |
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