Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Configure introspect to make the date or timestamp a string type. #177

Closed
tzezar opened this issue Jul 30, 2024 · 5 comments
Closed

Configure introspect to make the date or timestamp a string type. #177

tzezar opened this issue Jul 30, 2024 · 5 comments
Labels
bug Something isn't working in progress We're working on it

Comments

@tzezar
Copy link

tzezar commented Jul 30, 2024

Is it possible to configure to what type in this case date/timestamp is introspected to? I would love to work on strings instead eg. Timestamp type.

Upvote & Fund

  • We're using Polar.sh so you can upvote and help fund this issue.
  • We receive the funding once the issue is completed & confirmed by you.
  • Thank you in advance for helping prioritize & fund our backlog.
Fund with Polar
@RobinBlomberg
Copy link
Owner

Which SQL dialect are you using? If the database driver supports inserting/updating with strings, then kysely-codegen should always support strings as well.

@iffa
Copy link
Contributor

iffa commented Aug 4, 2024

+1 on this, I'd love to throw away all usages of Date - we are using pg

@lqmanh
Copy link

lqmanh commented Aug 12, 2024

I'm also encountering this issue. pg returns string for DATE and return Date object for TIMESTAMP

@RobinBlomberg RobinBlomberg added in progress We're working on it bug Something isn't working labels Aug 14, 2024
@tzezar
Copy link
Author

tzezar commented Aug 21, 2024

I created script that fixes it by changing generated types. It allows to work with strings. Remember to change path! Run it after kysely-codegen script in package.json.

I fixed some bugs I found, fresh code is here:
https://github.com/tzezar/kysely-codegen-timestamp-to-string-script

Mine looks like this: "database:push": "npm run database:generate:migration && npm run database:migrate && npm run database:generate:types && node src/utils/kysely-codegen-replace-date-to-string.js"

// kysely-codegen-replace-date-to-string.js 

import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';

// Get the current directory name
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

// Specify the file path you want to modify
const filePath = path.join(__dirname, '../database/kysely-codegen-types.ts');

// Function to replace "Date" with "string"
const replaceDateInFile = (file) => {
  fs.readFile(file, 'utf8', (err, data) => {
    if (err) {
      console.error(`Error reading file: ${err}`);
      return;
    }

    // Replace "Date" with "string"
    const updatedData = data.replace(/Date/g, 'string');

    // Write the updated content back to the file
    fs.writeFile(file, updatedData, 'utf8', (err) => {
      if (err) {
        console.error(`Error writing file: ${err}`);
      } else {
        console.log('File updated successfully.');
      }
    });
  });
};

// Run the function
replaceDateInFile(filePath);

@RobinBlomberg RobinBlomberg removed the in progress We're working on it label Aug 30, 2024
hevar added a commit to hevar/kysely-codegen that referenced this issue Sep 13, 2024
RobinBlomberg pushed a commit that referenced this issue Sep 16, 2024
@RobinBlomberg RobinBlomberg added the in progress We're working on it label Sep 16, 2024
@RobinBlomberg
Copy link
Owner

Should be fixed now in kysely-codegen@0.17.0!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working in progress We're working on it
Projects
None yet
Development

No branches or pull requests

4 participants