react-native-nitro-sqlite icon indicating copy to clipboard operation
react-native-nitro-sqlite copied to clipboard

Wrong examples in README

Open giovannilondero opened this issue 2 years ago • 2 comments

Hi,

Library version

8.0.4

Issue

README contains wrong examples of using the library (or the types need to be reviewed, you decide).

If we take the Simple queries example:

try {
  const db = open('myDb.sqlite');

  const { rows } = db.execute('SELECT somevalue FROM sometable');

  rows.forEach((row) => {
    console.log(row);
  });

  let { rowsAffected } = await db.executeAsync(
    'UPDATE sometable SET somecolumn = ? where somekey = ?',
    [0, 1],
  );

  console.log(`Update affected ${rowsAffected} rows`);
} catch (e) {
  console.error('Something went wrong executing SQL commands:', e.message);
}
  1. open function doesn't accept a string, but a { name: string; location?: string; } object

  2. rows might be undefined and is not an array, it's:

     rows?: {
        /** Raw array with all dataset */
        _array: any[];
        /** The lengh of the dataset */
        length: number;
        /** A convenience function to acess the index based the row object
         * @param idx the row index
         * @returns the row structure identified by column names
         */
        item: (idx: number) => any;
    };
    

    (taken from the QueryResult type)


I would expect rows to be an array, so I'd like the types to be reviewed, but I'm not sure it's in scope. Fixing the examples is quicker, for sure. Interestingly, no one else complained; the same code is present in the old repo.

What do you think?

giovannilondero avatar Aug 10 '23 15:08 giovannilondero

expo-sqlite implements the same structure actually: https://docs.expo.dev/versions/latest/sdk/sqlite/#sqlresultsetrowlist

giovannilondero avatar Aug 10 '23 16:08 giovannilondero

You are right insofar that the example in the readme is wrong. That was from the first implementation. The API however is complaint to webSQL, that's also why expo-sqlite also returns it. webSQL is dead though, so the specification is only for legacy purposes as a lot of libraries consume it.

ospfranco avatar Nov 09 '23 16:11 ospfranco