planetscale / database-js

A Fetch API-compatible PlanetScale database driver
https://planetscale.com/docs/tutorials/planetscale-serverless-driver
Apache License 2.0
1.17k stars 35 forks source link

Set use statements #163

Closed ayrton closed 6 months ago

ayrton commented 9 months ago

What

Add first class support for USE and SET statements.

Why

We want to make it easier to correctly use PlanetScale over HTTP supporting some of more exotic features (boost, replicas, sharding)

How

You can define which database to use, or which variables to set as part of Config. This means you can pass this onto: connect(), Client#new, Client#connection, Connection#new and Connection#execute().

Config defaults and overrides

// `connect`

connect({ url }).execute('select 1 from dual')
// defaults { database: undefined, variables: undefined }
// overrides { database: undefined, variables: undefined }

connect({ url }).execute('select 1 from dual', [], { database: '@replica' })
// defaults { database: undefined, variables: undefined }
// overrides { database: '@replica', variables: undefined }

connect({ url, database: 'keyspace' }).execute('select 1 from dual')
// defaults { database: 'keyspace', variables: undefined }
// overrides { database: undefined, variables: undefined }

connect({ url, database: 'keyspace' }).execute('select 1 from dual', [], { database: '@replica' })
// defaults { database: 'keyspace', variables: undefined }
// overrides { database: '@replica', variables: undefined }

// `new Client

new Client({ url }).execute('select 1 from dual')
// defaults { database: undefined, variables: undefined }
// overrides { database: undefined, variables: undefined }

new Client().execute('select 1 from dual', [], { database: '@replica' })
// defaults { database: undefined, variables: undefined }
// overrides { database: '@replica', variables: undefined }

new Client({ url, database: 'keyspace' }).execute('select 1 from dual')
// defaults { database: 'keyspace', variables: undefined }
// overrides { database: undefined, variables: undefined }

new Client({ url, database: 'keyspace' }).execute('select 1 from dual', { database: '@replica' })
// defaults { database: 'keyspace', variables: undefined }
// overrides { database: '@replica', variables: undefined }

// `new Client#connection`

new Client({ url }).connection().execute('select 1 from dual')
// defaults { database: undefined, variables: undefined }
// overrides { database: undefined, variables: undefined }

new Client().connection().execute('select 1 from dual', [], { database: '@replica' })
// defaults { database: undefined, variables: undefined }
// overrides { database: '@replica', variables: undefined }

new Client({ url, database: 'keyspace' }).connection().execute('select 1 from dual')
// defaults { database: 'keyspace', variables: undefined }
// overrides { database: undefined, variables: undefined }

new Client({ url, database: 'keyspace' }).connection().execute('select 1 from dual', { database: '@replica' })
// defaults { database: 'keyspace', variables: undefined }
// overrides { database: '@replica', variables: undefined }

new Client({ url }).connection({ database: 'otherkeyspace' }).execute('select 1 from dual')
// defaults { database: 'otherkeyspace', variables: undefined }
// overrides { database: undefined, variables: undefined }

new Client().connection({ database: 'otherkeyspace' }).execute('select 1 from dual', [], { database: '@replica' })
// defaults { database: 'otherkeyspace', variables: undefined }
// overrides { database: '@replica', variables: undefined }

new Client({ url, database: 'keyspace' }).connection({ database: 'otherkeyspace' }).execute('select 1 from dual')
// defaults { database: 'otherkeyspace', variables: undefined }
// overrides { database: undefined, variables: undefined }

new Client({ url, database: 'keyspace' }).connection({ database: 'otherkeyspace' }).execute('select 1 from dual', { database: '@replica' })
// defaults { database: 'otherkeyspace', variables: undefined }
// overrides { database: '@replica', variables: undefined }
iheanyi commented 6 months ago

Right now, this isn't a huge priority. Closing for now.