-
-
Notifications
You must be signed in to change notification settings - Fork 93
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* install kysely. * add createKysely @ LibAdapters. * remove unused import. * implement createKysely @ Adapters. * add Kysely sample. * add Kysely test suite. * add Kysely to readme. * blankline * blankline * actually it is supported.. * add keywords to package.json.
- Loading branch information
1 parent
37549b5
commit 40a0018
Showing
7 changed files
with
164 additions
and
5 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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 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 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,82 @@ | ||
import { expect } from 'chai'; | ||
import { newDb } from '../../src/db'; | ||
|
||
type GeneratedAlways<T> = import('kysely').GeneratedAlways<T>; | ||
|
||
interface DB { | ||
users: { | ||
id: GeneratedAlways<number>; | ||
user_name: string | null; | ||
}; | ||
accounts: { | ||
id: GeneratedAlways<number>; | ||
account_name: string | null; | ||
user_id: number | null; | ||
}; | ||
} | ||
|
||
export async function kyselySample() { | ||
|
||
// ========= CONNECT ========== | ||
|
||
// Create a new DB instance | ||
const mem = newDb(); | ||
|
||
// create a Kysely instance bound to this db | ||
// => This replaces new require('kysely').Kysely({ ... }) | ||
const kysely = mem.adapters.createKysely() as import('kysely').Kysely<DB>; | ||
|
||
|
||
// ========= USE AS USUAL ========== | ||
|
||
// Create a table | ||
await kysely.schema | ||
.createTable('users') | ||
.addColumn('id', 'serial', (cb) => cb.primaryKey()) | ||
.addColumn('user_name', 'varchar(255)') | ||
.execute(); | ||
|
||
// ...and another | ||
await kysely.schema | ||
.createTable('accounts') | ||
.addColumn('id', 'serial', (cb) => cb.primaryKey()) | ||
.addColumn('account_name', 'varchar(255)') | ||
.addColumn('user_id', 'integer', (cb) => cb.references('users.id')) | ||
.addForeignKeyConstraint('fk_user_id', ['user_id'], 'users', ['id']) | ||
.execute(); | ||
|
||
// Then query user table... | ||
await kysely.insertInto('users').values({ user_name: 'Tim' }).execute(); | ||
|
||
// ... and check | ||
expect(mem.public.many('select * from users')) | ||
.to.deep.equal([{ | ||
id: 1, | ||
user_name: 'Tim', | ||
}]); | ||
|
||
// Then insert into account table... | ||
await kysely | ||
.insertInto('accounts') | ||
.values({ account_name: 'kysely', user_id: 1 }) | ||
.execute(); | ||
|
||
// ... and check | ||
expect(mem.public.many('select * from accounts')) | ||
.to.deep.equal([{ | ||
id: 1, | ||
account_name: 'kysely', | ||
user_id: 1, | ||
}]); | ||
|
||
// Try to run a join | ||
const selectedRows = await kysely.selectFrom('users') | ||
.innerJoin('accounts', 'users.id', 'accounts.user_id') | ||
.select(['users.user_name as user', 'accounts.account_name as account']) | ||
.execute(); | ||
|
||
expect(selectedRows) | ||
.to.deep.equal([ | ||
{ user: 'Tim', account: 'kysely' }, | ||
]); | ||
} |
This file contains 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 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 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,36 @@ | ||
import { newDb } from '../db'; | ||
import { kyselySample } from '../../samples/kysely/kysely'; | ||
import { expect } from 'chai'; | ||
|
||
describe('Kysely', () => { | ||
it('can perform sample', async () => { | ||
await kyselySample(); | ||
}); | ||
|
||
it('should use kysely config from parameter', async () => { | ||
const mem = newDb(); | ||
const camelCasePlugin = new (await import('kysely')).CamelCasePlugin(); | ||
const kysely = mem.adapters.createKysely( | ||
undefined, | ||
{ | ||
plugins: [camelCasePlugin] | ||
} | ||
) as import('kysely').Kysely<any>; | ||
const executor = kysely.getExecutor(); | ||
expect(executor.plugins).to.deep.equal([camelCasePlugin]); | ||
}); | ||
|
||
it('should ignore dialect prop in kysely config', async () => { | ||
const mem = newDb(); | ||
const kysely = mem.adapters.createKysely( | ||
undefined, | ||
{ | ||
dialect: new (await import('kysely')).MysqlDialect({ | ||
pool: {} as any, | ||
}) | ||
} | ||
) as import('kysely').Kysely<any>; | ||
const executor = kysely.getExecutor(); | ||
expect(executor.adapter).to.be.instanceOf((await import('kysely')).PostgresAdapter); | ||
}); | ||
}); |