ai / nanoid

A tiny (124 bytes), secure, URL-friendly, unique string ID generator for JavaScript

Home Page:https://zelark.github.io/nano-id-cc/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Doesn't work with AWS Lambdas

jovi-tsx opened this issue · comments

Greetings! I'm currently having an issue with nanoid at my serverless stack backend + lambda function.

import { customAlphabet } from "nanoid";

export const generateNid = () => {
  const nanoid = customAlphabet(
    "1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
    10
  );

  const nid = nanoid();

  console.log(nid);

  return nanoid() + new Date().getFullYear();
};

export const create = lambda(async (event) => {
  const body = event.body as string;
  const sale: Sale = JSON.parse(body);

  let nid = "SA" + generateNid();
  let nidExists = !!(await SaleModel.scan("nid").eq(nid).exec()).count;

  while (nidExists) {
    nid = "SA" + generateNid();
    nidExists = !!(await SaleModel.scan("nid").eq(nid).exec()).count;
  }

  return SaleModel.create({
    id: uuid.v1(),
    nid,
    ...sale
  });
});

This returns sometimes that "crypto.getRandomValues() is not a function". Other times, it will work, but it will generate only "1111111111"