|
| 1 | + |
| 2 | +/** |
| 3 | + * Developer experience - current |
| 4 | + * |
| 5 | + */ |
| 6 | + |
| 7 | +import { Database } from '@sqlitecloud/drivers' |
| 8 | +import { PUBSUB_ENTITY_TYPE } from '@sqlitecloud/drivers/lib/drivers/pubsub' // forces user to import pubsub constants from hard to remember location |
| 9 | + |
| 10 | +const db = new Database('connection-string') |
| 11 | +const pubSub = await db.getPubSub() // couples database to pubsub |
| 12 | + |
| 13 | +/* Database methods */ |
| 14 | +await db.sql`SELECT * FROM users` |
| 15 | +db.exec('command') |
| 16 | +db.run('command') |
| 17 | +db.all('command') |
| 18 | +db.each('command') |
| 19 | +db.close() |
| 20 | + |
| 21 | +/* PubSub usage */ |
| 22 | +/** Listen to a table */ |
| 23 | +pubSub.listen(PUBSUB_ENTITY_TYPE.TABLE, 'users', (error, results, data) => { // note extraneous "data" |
| 24 | + console.log(error, results, data) |
| 25 | +}, ['extra data']) |
| 26 | + |
| 27 | +/** Listen to a channel */ |
| 28 | +pubSub.listen(PUBSUB_ENTITY_TYPE.CHANNEL, 'messages', (error, results, data) => { |
| 29 | + console.log(error, results, data) |
| 30 | +}, ['extra data']) |
| 31 | + |
| 32 | +/** Create a channel */ |
| 33 | +pubSub.createChannel('messages') |
| 34 | + |
| 35 | +/** Unlisten to a table */ |
| 36 | +pubSub.unlisten(PUBSUB_ENTITY_TYPE.TABLE, 'users') |
| 37 | + |
| 38 | +/** Remove a channel (not currently exposed) */ |
| 39 | +// @ts-ignore |
| 40 | +pubSub.removeChannel('messages') |
| 41 | + |
| 42 | +/** Notify a channel */ |
| 43 | +pubSub.notifyChannel('messages', 'my message') |
| 44 | + |
| 45 | + |
| 46 | +/** |
| 47 | + * Developer experience - refactored |
| 48 | + * In the refactor, Database still exists and works as before. |
| 49 | + */ |
| 50 | + |
| 51 | +import { createClient } from './src/refactor/SQLiteCloudClient' |
| 52 | + |
| 53 | +const client = createClient('connection-string/chinook.db') |
| 54 | + |
| 55 | +// Promise sql query |
| 56 | +const { data, error } = await client.sql`SELECT * FROM albums`; |
| 57 | + |
| 58 | +client.defaultDb = 'users'; // helper to set default database for SQL queries |
| 59 | + |
| 60 | +const { data: sessions, error: sessionsError } = await client.sql`SELECT * FROM sessions`; |
| 61 | +// or |
| 62 | +const result = client.db.exec('SELECT * FROM sessions') |
| 63 | + |
| 64 | +// Weblite |
| 65 | +// upload database |
| 66 | +const uploadDatabaseResponse = await client.weblite.upload('new_chinook.db', new File([''], 'new_chinook.db'), { replace: false }); |
| 67 | + |
| 68 | +// download database |
| 69 | +const downloadDatabaseResponse = await client.weblite.download('new_chinook.db'); |
| 70 | + |
| 71 | +// delete database |
| 72 | +const deleteDatabaseResponse = await client.weblite.delete('new_chinook.db'); |
| 73 | + |
| 74 | +// list databases |
| 75 | +const listDatabasesResponse = await client.weblite.listDatabases(); |
| 76 | + |
| 77 | +// create database |
| 78 | +const createDatabaseResponse = await client.weblite.create('new_chinook.db'); |
| 79 | + |
| 80 | +// SQLiteCloudFileClient |
| 81 | +const createBucketResponse = await client.files.createBucket('myBucket'); |
| 82 | +const getBucketResponse = await client.files.getBucket('myBucket'); |
| 83 | +const deleteBucketResponse = await client.files.deleteBucket('myBucket'); |
| 84 | +const listBucketsResponse = await client.files.listBuckets(); |
| 85 | + |
| 86 | +// upload file |
| 87 | +const uploadFileResponse = await client.files.upload('myBucket', 'myPath', new File([''], 'myFile.txt'), { contentType: 'text/plain' }); |
| 88 | + |
| 89 | +// download file |
| 90 | +const downloadFileResponse = await client.files.download('myBucket', 'myPath'); |
| 91 | + |
| 92 | +// remove file |
| 93 | +const removeFileResponse = await client.files.remove('myBucket', 'myPath'); |
| 94 | + |
| 95 | + |
| 96 | +// SQLiteCloudPubSubClient Refactor |
| 97 | +await client.pubSub.create('messages') |
| 98 | +await client.pubSub.notify('messages', 'my message') |
| 99 | +await client.pubSub.subscribe('messages', (error, results) => { |
| 100 | + console.log(error, results) |
| 101 | +}) |
| 102 | +client.pubSub.unsubscribe('messages') |
| 103 | +await client.pubSub.delete('messages') |
| 104 | + |
| 105 | +await client.pubSub.listen({ tableName: 'users' }, (error, results) => { |
| 106 | + console.log(error, results) |
| 107 | +}) |
| 108 | + |
| 109 | +await client.pubSub.listen({ tableName: 'users', dbName: 'chinook.sqlite' }, (error, results) => { // note optional dbName |
| 110 | + console.log(error, results) |
| 111 | +}) |
| 112 | + |
0 commit comments