-
Notifications
You must be signed in to change notification settings - Fork 162
feat(adapters): Add Drizzle #587
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
Merged
paulomarg
merged 25 commits into
Shopify:main
from
tursodatabase:shopify-app-session-storage-drizzle
Feb 26, 2024
Merged
Changes from all commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
d263d5a
initial drizzle adapter
notrab c3531c2
temp workaround
notrab 23624bd
sessionsTable
notrab 11ea3e9
docs(readme): sessions table usage
notrab 4a882f9
revert to any for sessionTable
notrab 2b92dc1
set Date
notrab 49f643a
changeset
notrab 5b5f231
Merge branch 'main' into shopify-app-session-storage-drizzle
notrab bca3bec
add: drizzle adapters for pg & sqlite; (#1)
realmikesolo e41d691
Shopify app session storage drizzle (#2)
realmikesolo e0daf8e
docs(session-storage-drizzle): add db.server example
notrab 39f572c
Shopify app session storage drizzle (#3)
realmikesolo 3e4398a
Shopify app session storage drizzle (#4)
realmikesolo 4018600
SQLite test & refactor (#5)
realmikesolo e6b18b6
chore: yarn lock
notrab 6e343ef
Merge branch 'main' into shopify-app-session-storage-drizzle
notrab d7a8e75
fixed lock
notrab 2197ccf
fix: broken lock
notrab a5c9df0
Merge branch 'main' into shopify-app-session-storage-drizzle
notrab 70bacaf
bump dep
notrab 9373b95
add: mysql adapter & update: tests (#6)
realmikesolo fd781f3
add resolutions
notrab e8e5ddb
merge upstream
notrab 8bce0b2
Shopify app session storage drizzle (#7)
realmikesolo 5eca45b
resolve lint errors
notrab File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@shopify/shopify-app-session-storage-drizzle': major | ||
--- | ||
|
||
Initial release of @shopify/shopify-app-session-storage-drizzle |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
node_modules | ||
# Keep environment variables out of version control | ||
.env |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# @shopify/shopify-app-session-storage-drizzle | ||
|
||
## 1.0.0 | ||
|
||
### Major Changes | ||
|
||
- Initial release of @shopify/shopify-app-session-storage-drizzle |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
MIT License | ||
|
||
Copyright (c) 2024-present, Shopify Inc. | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
# Session Storage Adapters for Drizzle | ||
|
||
This package implements the `SessionStorage` interface that works with an instance of [Drizzle](https://orm.drizzle.team). | ||
|
||
There are 3 adapters for Drizzle: `DrizzleSessionStoragePostgres`, `DrizzleSessionStorageSQLite` and `DrizzleSessionStorageMySQL`. | ||
|
||
Session storage for Drizzle requires a `schema.ts` with a `session` table with at-least the columns as in the example. Make sure to create `session` table and apply changes to the database before using this pakacge. | ||
|
||
## Example with SQLite | ||
|
||
```ts | ||
import {sqliteTable, text, integer, blob} from 'drizzle-orm/sqlite-core'; | ||
|
||
export const sessionTable = sqliteTable('session', { | ||
id: text('id').primaryKey(), | ||
shop: text('shop').notNull(), | ||
state: text('state').notNull(), | ||
isOnline: integer('isOnline', {mode: 'boolean'}).notNull().default(false), | ||
scope: text('scope'), | ||
expires: text('expires'), | ||
accessToken: text('accessToken'), | ||
userId: blob('userId', {mode: 'bigint'}), | ||
}); | ||
``` | ||
|
||
You can then instantiate and use `DrizzleSessionStorageSQLite` like so: | ||
|
||
```ts | ||
import {shopifyApp} from '@shopify/shopify-app-express'; | ||
import {DrizzleSessionStorageSQLite} from '@shopify/shopify-app-session-storage-drizzle'; | ||
|
||
import {db} from './db.server'; | ||
import {sessionTable} from './schema'; | ||
|
||
const storage = new DrizzleSessionStorageSQLite(db, sessionTable); | ||
|
||
const shopify = shopifyApp({ | ||
sessionStorage: storage, | ||
// ... | ||
}); | ||
``` | ||
|
||
## Drizzle Setup | ||
|
||
In the example above the file `db.server.ts` should import your database client, drizzle schema and export `db` that you can pass to the storage adapter: | ||
|
||
```ts | ||
import {drizzle} from 'drizzle-orm/libsql'; | ||
import {createClient} from '@libsql/client'; | ||
|
||
import * as schema from './schema'; | ||
|
||
export const client = createClient({ | ||
url: 'file:./dev.db', | ||
}); | ||
|
||
export const db = drizzle(client, {schema}); | ||
``` |
27 changes: 27 additions & 0 deletions
27
packages/shopify-app-session-storage-drizzle/loom.config.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import {createPackage, createProjectPlugin} from '@shopify/loom'; | ||
import {buildLibrary} from '@shopify/loom-plugin-build-library'; | ||
|
||
export default createPackage((pkg) => { | ||
pkg.entry({root: './src/drizzle.ts'}); | ||
pkg.use( | ||
buildLibrary({ | ||
// Required. A browserslist string for specifying your target output. | ||
// Use browser targets (e.g. `'defaults'`) if your package targets the browser, | ||
// node targets (e.g. `'node 12.22'`) if your package targets node | ||
// or both (e.g.`'defaults, node 12.22'`) if your package targets both | ||
targets: 'node 16', | ||
// Optional. Defaults to false. Defines if commonjs outputs should be generated. | ||
commonjs: true, | ||
// Optional. Defaults to false. Defines if esmodules outputs should be generated. | ||
esmodules: false, | ||
// Optional. Defaults to false. Defines if esnext outputs should be generated. | ||
esnext: false, | ||
// Optional. Defaults to true. Defines if entrypoints should be written at | ||
// the root of the repository. You can disable this if you have a single | ||
// entrypoint or if your package uses the `exports` key in package.json | ||
rootEntrypoints: false, | ||
// Optional. Defaults to 'node'. Defines if the jest environment should be 'node' or 'jsdom'. | ||
jestTestEnvironment: 'node', | ||
}), | ||
); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
{ | ||
"name": "@shopify/shopify-app-session-storage-drizzle", | ||
"version": "1.0.0", | ||
"description": "Shopify App Session Storage for Drizzle", | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/Shopify/shopify-app-js.git" | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/Shopify/shopify-app-js/issues" | ||
}, | ||
"homepage": "https://github.com/Shopify/shopify-app-js/tree/main/packages/shopify-app-session-storage-drizzle", | ||
"author": "Shopify Inc.", | ||
"license": "MIT", | ||
"main": "./build/cjs/drizzle.js", | ||
"types": "./build/ts/drizzle.d.ts", | ||
"scripts": {}, | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"keywords": [ | ||
"shopify", | ||
"node", | ||
"app", | ||
"graphql", | ||
"rest", | ||
"webhook", | ||
"Admin API", | ||
"Storefront API", | ||
"session storage", | ||
"Drizzle" | ||
], | ||
"dependencies": { | ||
"tslib": "^2.6.2" | ||
}, | ||
"peerDependencies": { | ||
"@shopify/shopify-api": "^9.3.2", | ||
"@shopify/shopify-app-session-storage": "^2.1.1", | ||
"drizzle-orm": "^0.29.3" | ||
}, | ||
"devDependencies": { | ||
"@libsql/client": "^0.4.0-pre.7", | ||
"@shopify/eslint-plugin": "^42.1.0", | ||
"@shopify/prettier-config": "^1.1.2", | ||
"@shopify/shopify-app-session-storage-test-utils": "^2.0.1", | ||
"better-sqlite3": "^9.3.0", | ||
"drizzle-kit": "^0.20.12", | ||
"drizzle-orm": "^0.29.3", | ||
"eslint": "^8.55.0", | ||
"eslint-plugin-prettier": "^4.2.1", | ||
"mysql2": "^3.9.1", | ||
"pg": "^8.11.3", | ||
"prettier": "^2.8.7", | ||
"typescript": "4.9.5" | ||
}, | ||
"files": [ | ||
"build/*", | ||
"!bundle/*", | ||
"!tsconfig.tsbuildinfo", | ||
"!node_modules" | ||
] | ||
} |
99 changes: 99 additions & 0 deletions
99
packages/shopify-app-session-storage-drizzle/src/__tests__/drizzle-mysql.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
import * as child_process from 'child_process'; | ||
import {promisify} from 'util'; | ||
|
||
import mysql2 from 'mysql2/promise'; | ||
import { | ||
batteryOfTests, | ||
poll, | ||
} from '@shopify/shopify-app-session-storage-test-utils'; | ||
import { | ||
mysqlTable, | ||
text, | ||
boolean, | ||
timestamp, | ||
bigint, | ||
varchar, | ||
} from 'drizzle-orm/mysql-core'; | ||
import {drizzle} from 'drizzle-orm/mysql2'; | ||
|
||
import {DrizzleSessionStorageMySQL} from '../adapters/drizzle-mysql.adapter'; | ||
|
||
const exec = promisify(child_process.exec); | ||
|
||
const dbConfig = { | ||
host: 'localhost', | ||
user: 'root', | ||
password: 'passify#$', | ||
database: 'shop&test', | ||
port: 3307, | ||
}; | ||
|
||
const sessionTable = mysqlTable('session', { | ||
id: varchar('id', {length: 255}).primaryKey(), | ||
shop: text('shop').notNull(), | ||
state: text('state').notNull(), | ||
isOnline: boolean('isOnline').default(false).notNull(), | ||
scope: text('scope'), | ||
expires: timestamp('expires', {mode: 'date'}), | ||
accessToken: text('accessToken'), | ||
userId: bigint('userId', {mode: 'number'}), | ||
}); | ||
|
||
describe('DrizzleSessionStorageMySQL', () => { | ||
let drizzleSessionStorage: DrizzleSessionStorageMySQL; | ||
let containerId: string; | ||
let connection: mysql2.Connection; | ||
|
||
beforeAll(async () => { | ||
const runCommand = await exec( | ||
"podman run -d -e MYSQL_DATABASE='shop&test' -e MYSQL_USER='shop&fy' -e MYSQL_PASSWORD='passify#$' -e MYSQL_ROOT_PASSWORD='passify#$' -p 3307:3306 mysql:8-oracle", | ||
{encoding: 'utf8'}, | ||
); | ||
|
||
containerId = runCommand.stdout.trim(); | ||
|
||
await poll( | ||
async () => { | ||
try { | ||
connection = await mysql2.createConnection(dbConfig); | ||
} catch (_error) { | ||
// console.error(_error); | ||
return false; | ||
} | ||
return true; | ||
}, | ||
{interval: 500, timeout: 20000}, | ||
); | ||
|
||
await connection.query(` | ||
CREATE TABLE IF NOT EXISTS \`session\` ( | ||
\`id\` varchar(255) NOT NULL, | ||
\`shop\` text NOT NULL, | ||
\`state\` text NOT NULL, | ||
\`isOnline\` boolean NOT NULL DEFAULT false, | ||
\`scope\` text, | ||
\`expires\` timestamp NULL, | ||
\`accessToken\` text, | ||
\`userId\` bigint, | ||
CONSTRAINT \`session_id\` PRIMARY KEY (\`id\`) | ||
); | ||
`); | ||
|
||
const drizzleDb = drizzle(connection); | ||
|
||
drizzleSessionStorage = new DrizzleSessionStorageMySQL( | ||
drizzleDb, | ||
sessionTable, | ||
); | ||
}); | ||
|
||
afterAll(async () => { | ||
if (connection) { | ||
await connection.end(); | ||
} | ||
|
||
await exec(`podman rm -f ${containerId}`); | ||
}); | ||
|
||
batteryOfTests(async () => drizzleSessionStorage); | ||
}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.