-
Notifications
You must be signed in to change notification settings - Fork 125
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
feat(adapters): Add Drizzle #587
feat(adapters): Add Drizzle #587
Conversation
Hi @notrab, thank you for your contribution! We're reviewing this internally and we'll get back to you as soon as we can. |
Awesome, thanks @matteodepalo. I'm new to using Drizzle but happy to change anything, just let me know. |
This will close #560 |
Hello, @notrab and @matteodepalo . I am an active contributor to Drizzle ORM and have some improvements that I'd like to implement for this adapter. These include enhancing type-safety and optimizing SQL operations for better correctness and performance, which I believe will make them more suitable for Drizzle ORM. I suggest we postpone this pull request until I have made these improvements based on this code. What are your thoughts on this? |
@realmikesolo that would be awesome! We asked a few questions in Discord yesterday but didn't get far, I'd love your help! I'll get you added to the tursodatabase fork as a contributor! ❤️ |
@notrab, thank you! I'm starting to work on this. For any questions or discussions, you can contact me on Discord at |
@realmikesolo sounds good! It’s probably easier you fork and PR the branch. I’ll ping you on Discord. |
update: readme;
I have signed the CLA! |
Hey @matteodepalo, with these changes by @realmikesolo we should be good to continue in the review process ❤️ |
Hey folks, thank you for contributing! I'll spend some time digging into this, but on the surface it makes sense to me. One thing I notice is that we don't seem to have any tests yet. We provide a battery of tests that are shared across all of our adapters (e.g. for SQLite or for PGSQL). We use You should be able to run that if you run Please let me know if you have any issues adding the tests, and in the meantime I'll go through the code, though I don't expect I'll have too much to say there :) |
packages/shopify-app-session-storage-drizzle/src/adapters/drizzle-sqlite.adapter.ts
Show resolved
Hide resolved
packages/shopify-app-session-storage-drizzle/src/schemas/pg.schema.ts
Outdated
Show resolved
Hide resolved
packages/shopify-app-session-storage-drizzle/src/adapters/drizzle-pg.adapter.ts
Outdated
Show resolved
Hide resolved
packages/shopify-app-session-storage-drizzle/src/adapters/drizzle-pg.adapter.ts
Outdated
Show resolved
Hide resolved
packages/shopify-app-session-storage-drizzle/src/adapters/drizzle-pg.adapter.ts
Outdated
Show resolved
Hide resolved
* add: drizzle adapters for pg & sqlite; update: readme; * fixes according to review
Hi, @paulomarg. I am trying to run tests, but have encountered an issue that jest cannot process ES module syntax, as it seems to be using default configuration parameters. Could you provide more details on how to set up the test workflow to allow Jest to properly handle ES module code? |
@paulomarg I just cloned FAIL packages/shopify-app-express/src/middlewares/__tests__/ensure-installed-on-shop.test.ts
● Test suite failed to run
TypeError: Cannot read properties of undefined (reading 'enableGlobally')
at jestAdapter (../../node_modules/jest-circus/build/legacy-code-todo-rewrite/jestAdapter.js:36:25) This was also updated a few days ago: "@shopify/shopify-app-session-storage-memory": "^3.0.0", Not sure if it could be related with that package versioning. I have merged @realmikesolo and I would appreciate any help getting whatever failing tests resolved. I tried to revert the changes to the loom config to see if it was that tripping up tests but I get the same result. |
That's so weird! I just created a PR and it worked just fine, including CI, so this isn't happening every time. I wonder if this could be because of the node version you're running? I've tried it with node 18 and 20 myself. Also, could this be it? jestjs/jest#13259 (comment) - it sounds like the versions of jest and jest-circus need to be aligned. |
@paulomarg ive merged the latest changes @realmikesolo and I were discussing regarding Prisma and main test fixes. This also includes a MySQL adapter. |
Hey @paulomarg, I just wanted to follow up to see if there was anything you could do to help us get this over the line? @realmikesolo and I both have inconsistent tests passing with |
Hey! I played around with this error a bit, and strangely I don't get it on |
Quick update: this seems to work for me, I got the tests to pass reliably by adding this to the root package.json: @@ -26,6 +26,7 @@
"eslint": "^8.55.0",
"eslint-plugin-prettier": "^5.1.3",
"jest": "^29.1.0",
+ "jest-environment-jsdom": "^29.7.0",
"jest-fetch-mock": "^3.0.3",
"jest-runner-eslint": "^2.1.2",
"prettier": "^3.2.4",
@@ -34,6 +35,11 @@
"tslib": "^2.6.2",
"typescript": "5.3.3"
},
+ "resolutions": {
+ "jest": "^29.1.0",
+ "jest-circus": "^29.1.0",
+ "ts-jest": "^29.1.0"
+ },
"dependencies": {},
"workspaces": [
"packages/*" |
That's great @paulomarg! Do you want to commit that or should I? |
Would you mind doing it and running locally to see if that fixes it for you? That should tell us whether it'll work in CI :) |
Hey @paulomarg I've tried with multiple engineers on our side to run the tests locally (with and without the resolutions) and it still fails on Also, I noticed that loom config is using |
I ended up running into a similar issue here: #626 I believe I have it fixed in a way that will make CI happy, including passing Might cause a couple of conflicts in your loom config - you should just have to add the drizzle one.
We'll be moving away from node 16 in the future, but for now there's no need to change that, thank you! |
I've merged upstream and all tests are passing. |
* add: mysql adapter & update: tests * merge upstream * bump shopify packages for drizzle
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tested this out in an app and it worked like a charm! The tests are passing, but we had a few linting errors, could you please fix those so we can merge?
Also: 🎉
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, something just occurred to me while checking this out: the code for all the adapters seems to be very similar.
I may have asked this before, but is there no way to have a generic type for the db
param that we can share across all of the adapters, so we only need to implement them once? Probably ok if there's no easy way to do that, but it would make this adapter easier to maintain in the future.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unfortunately, we can't create a generic type for the db
parameter with Drizzle, but I think I can create a function that will return the proper adapter based on the type of the db
parameter. Something like this:
export function createSessionStorage(params: Params) {
if (is(params.db, BaseSQLiteDatabase)) {
return new DrizzleSessionStorageSQLite(
params.db,
params.sessionTable as SQLiteSessionTable,
);
} else if (is(params.db, MySqlDatabase)) {
return new DrizzleSessionStorageMySQL(
params.db,
params.sessionTable as MySQLSessionTable,
);
} else if (is(params.db, PgDatabase)) {
return new DrizzleSessionStoragePostgres(
params.db,
params.sessionTable as PostgresSessionTable,
);
}
}
export type Params =
| {
db: BaseSQLiteDatabase<'sync' | 'async', any, any>;
sessionTable: SQLiteSessionTable;
}
| {
db: MySqlDatabase<MySQLQueryResultHKT, PreparedQueryHKTBase, any>;
sessionTable: MySQLSessionTable;
}
| {
db: PgDatabase<PostgresQueryResultHKT, any>;
sessionTable: PostgresSessionTable;
};
const storage = createSessionStorage({
db: drizzleDb,
sessionTable,
});
const shopify = shopifyApp({
sessionStorage: storage,
// ...
});
Would this be suitable, or should we just leave it as it is now?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, I think that might end up complicating things a bit more. We can re-evaluate to see if people are running into problems with the way things are now.
@paulomarg lint errors resolved. I think there was a reason PS. Thanks for all your time reviewing and making this PR a reality! Same for @realmikesolo 🥳 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just wanted to say big thanks to @realmikesolo and @notrab for sticking with this and seeing it through.
Hopefully this package will be helpful to folks who prefer to work with Drizzle when they're writing apps!
@paulomarg @notrab Thank you very much for your cooperation! It was a pleasure to work on this package with you! 🥳 |
@paulomarg is there a timeline for when this will be published to NPM? We'd love to blog about this and link to the package. Thanks 🙏🏻 |
It will be included in the next release. We don't have a fixed scheduled but we try to release at least every couple of weeks. |
Keep us updated when this merged and ready for use. |
Hey folks, we just published v1 of the Drizzle package 🎉 Please let us know if you have any issues using it! |
Now all we need is a new docker file to deploy remix app with Drizzle |
WHY are these changes introduced?
Drizzle is popular TypeScript ORM that would benefit Shopify App developers.
WHAT is this pull request doing?
This PR adds the ability to use Drizzle as your storage adapter.
Some things aren't clear from a Drizzle side (it doesn't look to be possible or similar to Prisma). There doesn't appear to be a way that I can find in the Drizzle docs to get the schema from a db instance so that's why the constructor requires 2 args —
db
andsessions
.I tested this using a local SQLite file (via libSQL) and Drizzle following the documentation in the
README.md
.If there are any changes or things incorrect, please let me know.
Type of change
Checklist
yarn changeset
to create a draft changelog entry (do NOT update theCHANGELOG.md
files manually)