PeanutDumplings
PeanutDumplings
XXata
Created by PeanutDumplings on 7/1/2024 in #help
Xata issues with environment variables in NestJS
I have a simple NestJS app, generated through their cli, and have attempted to add Xata to the project to use as my database. I've ran the xata init command, and everything generated correctly and my environment variables were added to my .env file. However, where I try to run my app with pnpm start:dev (a precreated script) I get this error:
E:\***\node_modules\.pnpm\@xata.io+client@0.29.5_typescript@5.4.5\node_modules\@xata.io\client\src\client.ts:108
throw new Error('Option apiKey is required');
^
Error: Option apiKey is required
at XataClient.parseOptions_fn (E:\***\node_modules\.pnpm\@xata.io+client@0.29.5_typescript@5.4.5\node_modules\@xata.io\client\src\client.ts:108:15)
at new _a (E:\***\node_modules\.pnpm\@xata.io+client@0.29.5_typescript@5.4.5\node_modules\@xata.io\client\src\client.ts:47:27)
at new XataClient (E:\***\src\lib\xata.ts:80:5)
at getXataClient (E:\***\src\lib\xata.ts:89:14)
at KeyCache.init (E:\***\remorse\nest-backend\src\lib\KeyCache.ts:12:40)
at new KeyCache (E:\***\src\lib\KeyCache.ts:8:10)
at Object.<anonymous> (E:\***\src\lib\KeyCache.ts:39:16)
at Module._compile (node:internal/modules/cjs/loader:1378:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1437:10)
at Module.load (node:internal/modules/cjs/loader:1212:32)
E:\***\node_modules\.pnpm\@xata.io+client@0.29.5_typescript@5.4.5\node_modules\@xata.io\client\src\client.ts:108
throw new Error('Option apiKey is required');
^
Error: Option apiKey is required
at XataClient.parseOptions_fn (E:\***\node_modules\.pnpm\@xata.io+client@0.29.5_typescript@5.4.5\node_modules\@xata.io\client\src\client.ts:108:15)
at new _a (E:\***\node_modules\.pnpm\@xata.io+client@0.29.5_typescript@5.4.5\node_modules\@xata.io\client\src\client.ts:47:27)
at new XataClient (E:\***\src\lib\xata.ts:80:5)
at getXataClient (E:\***\src\lib\xata.ts:89:14)
at KeyCache.init (E:\***\remorse\nest-backend\src\lib\KeyCache.ts:12:40)
at new KeyCache (E:\***\src\lib\KeyCache.ts:8:10)
at Object.<anonymous> (E:\***\src\lib\KeyCache.ts:39:16)
at Module._compile (node:internal/modules/cjs/loader:1378:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1437:10)
at Module.load (node:internal/modules/cjs/loader:1212:32)
This is my KeyCache.ts file:
import { getXataClient } from './Xata';

class KeyCache {
cache: Set<string>;

constructor() {
this.cache = new Set<string>();
this.init();
}

async init() {
const records = await getXataClient().db.uploads.select(['slug']).getAll();
records.forEach((rec) => {
if (rec && typeof rec.slug === 'string') {
this.cache.add(rec.slug);
}
});
}

getUniqueId(length: number) {
let string = this.generateRandomString(length);
while (this.cache.has(string)) string = this.generateRandomString(length);
this.cache.add(string);
return string;
}

private generateRandomString(length: number) {
let result = '';
const characters =
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
for (let i = 0; i < length; i++)
result += characters.charAt(
Math.floor(Math.random() * characters.length),
);
return result;
}
}

export default new KeyCache();
import { getXataClient } from './Xata';

class KeyCache {
cache: Set<string>;

constructor() {
this.cache = new Set<string>();
this.init();
}

async init() {
const records = await getXataClient().db.uploads.select(['slug']).getAll();
records.forEach((rec) => {
if (rec && typeof rec.slug === 'string') {
this.cache.add(rec.slug);
}
});
}

getUniqueId(length: number) {
let string = this.generateRandomString(length);
while (this.cache.has(string)) string = this.generateRandomString(length);
this.cache.add(string);
return string;
}

private generateRandomString(length: number) {
let result = '';
const characters =
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
for (let i = 0; i < length; i++)
result += characters.charAt(
Math.floor(Math.random() * characters.length),
);
return result;
}
}

export default new KeyCache();
My environment variables are working fine as in the root file (main.ts) I have the following code:
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { ValidationPipe } from '@nestjs/common';

async function bootstrap() {
console.log(process.env.XATA_API_KEY);
const app = await NestFactory.create(AppModule);
app.useGlobalPipes(new ValidationPipe());
await app.listen(3000);
}
bootstrap();
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { ValidationPipe } from '@nestjs/common';

async function bootstrap() {
console.log(process.env.XATA_API_KEY);
const app = await NestFactory.create(AppModule);
app.useGlobalPipes(new ValidationPipe());
await app.listen(3000);
}
bootstrap();
and the appropriate variable is outputted near the start of the logs (before the errors). Does anyone know what might be causing this error? Could it be the way in which files are loaded in NestJS, meaning that the KeyCache file that requires xata be loaded before environment variables are set/available to the app? Thanks in advance :)
5 replies
XXata
Created by PeanutDumplings on 6/13/2023 in #help
Export table as .csv
The title is pretty self explanatory, but is there a way to export a xata table to a .csv file. I'm trying to clone an existing database (on xata) to a new one without having to manually create all the entries
3 replies
XXata
Created by PeanutDumplings on 4/29/2023 in #help
updateOrReplace by field
Instead of having to use the record id, is it possible to use the createOrReplace method and search by one or multiple fields. e.g.
const user = await xata.db.Users.createOrReplace({name: "Tom Hanks", age: "66"}, { name: "Keanu Reeves" });
const user = await xata.db.Users.createOrReplace({name: "Tom Hanks", age: "66"}, { name: "Keanu Reeves" });
2 replies
XXata
Created by PeanutDumplings on 4/11/2023 in #help
Property db doesn't exist on XataClient
With the code
import { getXataClient } from "../../xata";
const xata = getXataClient();
console.log(xata)
// returns
// XataClient {
// db: {},
// search: { all: [AsyncFunction: all], byTable: [AsyncFunction: byTable] },
// transactions: { run: [AsyncFunction: run] }
}
import { getXataClient } from "../../xata";
const xata = getXataClient();
console.log(xata)
// returns
// XataClient {
// db: {},
// search: { all: [AsyncFunction: all], byTable: [AsyncFunction: byTable] },
// transactions: { run: [AsyncFunction: run] }
}
But when I try
console.log(xata.db);
console.log(xata.db);
I get the error: Property 'db' does not exist on type 'XataClient'
17 replies
XXata
Created by PeanutDumplings on 4/11/2023 in #help
Can't install xata cli
When running the command listed on the docs npm install -g @xata.io/cli I get the error
The splatting operator '@' cannot be used to reference variables in an expression. '@xata' can be used only as an argument to a command. To reference variables in an expression use '$xata'.
The splatting operator '@' cannot be used to reference variables in an expression. '@xata' can be used only as an argument to a command. To reference variables in an expression use '$xata'.
. Even the cmd listed on npmjs.com npm i @xata.io/cli, doesn't word throws the same error.
6 replies