deepkit-framework icon indicating copy to clipboard operation
deepkit-framework copied to clipboard

Support for CUID and NanoId

Open jackgdll opened this issue 1 year ago • 3 comments

Could support for CUIDs and NanoIds be added? Can be used in the same way as uuid:

import { cuid, CUID, NanoId, nanoId, PrimaryKey } from '@deepkit/type';

class User {
    cuid: CUID & PrimaryKey = cuid();
	// Or the following
	// nanoId: NanoID & PrimaryKey = nanoId();
}

This would be nice to have to shorten ids.

jackgdll avatar Mar 07 '23 18:03 jackgdll

I think string would be enough, no?

class User {
    cuid: string & PrimaryKey = cuid();
}

marcj avatar Mar 07 '23 19:03 marcj

I guess so, it wouldn't be able to validate the field though would it?

The @paralleldrive/cuid2 package exposes an isCuid function so it should be trivial to add validation if I'm not mistaken?

import { createId, isCuid } from '@paralleldrive/cuid2';

console.log(
  isCuid(createId()), // true
  isCuid('not a cuid'), // false
);

jackgdll avatar Mar 07 '23 19:03 jackgdll

True, no validation happens. When we add a new type like CUID we can add validation logic like so https://github.com/deepkit/deepkit-framework/blob/master/packages/type/src/serializer.ts#L1865-L1871 and https://github.com/deepkit/deepkit-framework/blob/master/packages/type/src/serializer.ts#L2050-L2054

marcj avatar Mar 07 '23 19:03 marcj