Package Exports
- @ttoss/ids
- @ttoss/ids/dist/esm/index.js
- @ttoss/ids/dist/index.js
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@ttoss/ids) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
@ttoss/ids
This package provides a opinionated way to work with different ids in the application.
Check the blog post for more information.
Installation
pnpm add @ttoss/idsUsage
From database id to global id
Example with PostgreSQL:
import { toRecordId, toGlobalId } from '@ttoss/ids';
const itemFromPostgreSQL = {
id: 1,
name: 'John Doe',
};
const recordId = toRecordId(itemFromPostgreSQL.id);
console.log(recordId); // 1
const globalId = toGlobalId('User', recordId);
console.log(globalId); // VXNlcjoxExample with DynamoDB:
import { toRecordId, toGlobalId } from '@ttoss/ids';
const itemFromDynamoDB = {
partitionKey: 'USER',
sortKey: '1',
name: 'John Doe',
};
const recordId = toRecordId([
itemFromDynamoDB.partitionKey,
itemFromDynamoDB.sortKey,
]);
console.log(recordId); // USER:1
const globalId = toGlobalId('User', recordId);
console.log(globalId); // VXNlcjpVU0VSOjE=From global id to database id
Example with PostgreSQL:
import { fromGlobalId, fromRecordId } from '@ttoss/ids';
const globalId = 'VXNlcjox';
const { type, recordId } = fromGlobalId(globalId);
console.log(type); // User
console.log(recordId); // 1
const [databaseId] = fromRecordId(recordId);
console.log(databaseId); // 1Example with DynamoDB:
import { fromGlobalId, fromRecordId } from '@ttoss/ids';
const globalId = 'VXNlcjpVU0VSOjE=';
const { type, recordId } = fromGlobalId(globalId);
console.log(type); // User
console.log(recordId); // USER:1
const [partitionKey, sortKey] = fromRecordId(recordId);
console.log(partitionKey); // USER
console.log(sortKey); // 1