Skip to content

Commit

Permalink
FIX: ULID sorting
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael O'Brien committed Oct 22, 2021
1 parent 8939479 commit a84cb74
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 9 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "dynamodb-onetable",
"version": "2.0.0",
"version": "2.0.1",
"description": "DynamoDB access library for single-table designs",
"main": "dist/cjs/index.js",
"module": "dist/mjs/index.js",
Expand Down
3 changes: 2 additions & 1 deletion src/Table.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ export class Table {
setLog(log: any): void;
setSchema(schema?: OneSchema): Promise<void>;
transact(op: string, transaction: any, params?: OneParams): Promise<void>;
uuid(): {};
ulid(): string;
uuid(): string;

deleteItem(properties: OneProperties, params?: OneParams): Promise<void>;
getItem(properties: OneProperties, params?: OneParams): Promise<AnyEntity | undefined>;
Expand Down
2 changes: 1 addition & 1 deletion src/ULID.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,6 @@ export default class ULID {
bytes[i] = Letters.charAt(mod)
now = (now - mod) / LettersLen
}
return bytes.join('')
return bytes.reverse().join('')
}
}
5 changes: 3 additions & 2 deletions test/debug.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,10 @@ test('Test', async() => {
}, {log: false, hidden: true})
// dump("USER", user)

users = await User.find({sk: null}, {log: false, hidden: true})
// users = await User.find({sk: null}, {log: false, hidden: true})

// users = await AdminUser.find({}, {index: 'gs1', log: false, hidden: true})

// user = await User.get({entity: {id: user.entity.id}})
// dump("USERS", users)
})

Expand Down
6 changes: 5 additions & 1 deletion test/schemas/debugSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,16 @@ export default {
version: '0.0.1',
indexes: {
primary: { hash: 'pk', sort: 'sk' },
// gs1: { hash: 'gs1pk', sort: 'gs1sk', project: ['gs1pk', 'gs1sk', 'name']}
gs1: { hash: 'gs1pk', sort: 'gs1sk', project: 'all' },
},
models: {
User: {
pk: { type: String, value: '${_type}#' },
sk: { type: String, value: '${_type}#${domain}#${id}' },

gs1pk: { type: String, value: '${_type}#' },
gs1sk: { type: String, value: '${_type}#${id}' },

name: { type: String },
email: { type: String },
id: { type: String, uuid: "uuid" },
Expand Down
17 changes: 16 additions & 1 deletion test/ulid.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
ulid.ts - Unit test for ULID
*/
import {Match} from './utils/init'
import {Match, delay} from './utils/init'
import ULID from '../src/ULID'

// jest.setTimeout(7200 * 1000)
Expand Down Expand Up @@ -49,3 +49,18 @@ test('ULID repeat', async() => {
expect(id.length).toBe(26)
}
})

test('Sequence of timestamps', async() => {
const limit = 100
let output = []
for (let i = 0; i < limit; i++) {
let ulid = new ULID()
const id = ulid.toString()
await delay(1)
output.push(id)
}
let sorted = output.sort()
for (let i = 0; i < limit; i++) {
expect(output[i]).toBe(sorted[i])
}
})

0 comments on commit a84cb74

Please sign in to comment.