0.3.3 • Published 4 months ago

@ttoss/ids v0.3.3

Weekly downloads
-
License
MIT
Repository
github
Last release
4 months ago

@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/ids

Usage

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); // VXNlcjox

Example 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); // 1

Example 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
0.3.3

4 months ago

0.3.2

6 months ago

0.3.1

6 months ago

0.3.0

7 months ago

0.2.14

7 months ago

0.2.13

9 months ago

0.2.12

9 months ago

0.2.11

9 months ago

0.2.10

9 months ago

0.2.9

10 months ago

0.2.7

10 months ago

0.2.8

10 months ago

0.2.6

12 months ago

0.2.5

1 year ago

0.2.4

1 year ago

0.2.3

1 year ago

0.2.2

1 year ago

0.2.1

1 year ago

0.2.0

1 year ago

0.1.1

1 year ago

0.1.0

1 year ago