-
Notifications
You must be signed in to change notification settings - Fork 594
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
32 changed files
with
463 additions
and
171 deletions.
There are no files selected for viewing
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,2 @@ | ||
node_modules/ | ||
package-lock.json |
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,29 @@ | ||
import pg from 'pg'; | ||
|
||
export function rw_client() { | ||
let types = pg.types | ||
types.setTypeParser(types.builtins.TIMESTAMP, parseTimestamp) | ||
types.setTypeParser(types.builtins.DATE, parseDate) | ||
types.setTypeParser(types.builtins.INT8, parseBigint) | ||
|
||
return new pg.Client({ | ||
host: 'risingwave-standalone', | ||
port: 4566, | ||
database: 'dev', | ||
user: 'root', | ||
password: '', | ||
types: types, | ||
}) | ||
} | ||
|
||
function parseDate(val) { | ||
return val === null ? null : val | ||
} | ||
|
||
function parseTimestamp(val) { | ||
return val === null ? null : val | ||
} | ||
|
||
function parseBigint(val) { | ||
return val === null ? null : BigInt(val) | ||
} |
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,20 @@ | ||
{ | ||
"name": "rw-nodejs-client-test", | ||
"version": "1.0.0", | ||
"description": "rw nodejs client test", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "mocha" | ||
}, | ||
"author": "", | ||
"license": "Apache-2.0", | ||
"devDependencies": { | ||
"mocha": "^10.2.0", | ||
"chai": "^5.0.0" | ||
}, | ||
"dependencies": { | ||
"pg": "^8.11.3", | ||
"postgres-interval": "^4.0.1" | ||
}, | ||
"type": "module" | ||
} |
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,44 @@ | ||
import {checkInsertedData, createTable, deleteDataByName, dropTable, insertData, updateSalaryData} from './util.js' | ||
|
||
export async function test_crud(client) { | ||
await createTable(client) | ||
|
||
let name = "John Doe" | ||
let age = 30 | ||
let salary = BigInt(50000) | ||
let tripIDs = ['12345', '67890'] | ||
let fareData = { | ||
initial_charge: 3.0, | ||
subsequent_charge: 1.5, | ||
surcharge: 0.5, | ||
tolls: 2.0, | ||
} | ||
let deci = 3.14159 | ||
let birthdate = "1993-05-15" | ||
let starttime = "18:20:00" | ||
let timest = "1993-05-15 00:00:00" | ||
let timestz = new Date() | ||
let timegap = "5 hours" | ||
await insertData(client, name, age, salary, tripIDs, birthdate, deci, fareData, starttime, timest, timestz, timegap) | ||
await checkInsertedData(client, name, age, salary, tripIDs, birthdate, deci, fareData, starttime, timest, timestz, timegap) | ||
|
||
// Insert data with null values | ||
let nullName = "Null Person" | ||
let nullAge = 0 | ||
let nullSalary = BigInt(0) | ||
let nullTripIDs = [] | ||
let nullFareData = {} | ||
let nullBirthDate = "0001-01-01" | ||
let nullStarttime = "00:00:00" | ||
let nullTimest = "0001-01-01 00:00:00" | ||
let nullTimestz = new Date(0) | ||
let nullTimegap = "0" | ||
let nullDeci = 0.0 | ||
await insertData(client, nullName, nullAge, nullSalary, nullTripIDs, nullBirthDate, nullDeci, nullFareData, nullStarttime, nullTimest, nullTimestz, nullTimegap) | ||
|
||
await checkInsertedData(client, nullName, nullAge, nullSalary, nullTripIDs, nullBirthDate, nullDeci, nullFareData, nullStarttime, nullTimest, nullTimestz, nullTimegap) | ||
|
||
await updateSalaryData(client, name, BigInt(60000)) | ||
await deleteDataByName(client, name) | ||
await dropTable(client) | ||
} |
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,18 @@ | ||
import { rw_client } from '../index.js' | ||
import { test_crud } from "./crud_test.js"; | ||
|
||
let client = rw_client() | ||
|
||
beforeEach(async function () { | ||
await client.connect() | ||
}) | ||
|
||
afterEach(async function () { | ||
await client.end() | ||
}) | ||
|
||
describe('nodejs client test', function () { | ||
it('crud test', async function () { | ||
await test_crud(client) | ||
}) | ||
}); |
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,108 @@ | ||
import * as chai from "chai"; | ||
|
||
const assert = chai.assert | ||
|
||
export async function createTable(client) { | ||
let createTableQuery = ` | ||
CREATE TABLE sample_table_nodejs | ||
( | ||
name VARCHAR, | ||
age INTEGER, | ||
salary BIGINT, | ||
trip_id VARCHAR[], | ||
birthdate DATE, | ||
deci DOUBLE PRECISION, | ||
fare STRUCT < initial_charge DOUBLE PRECISION, | ||
subsequent_charge DOUBLE PRECISION, | ||
surcharge DOUBLE PRECISION, | ||
tolls DOUBLE PRECISION >, | ||
starttime TIME, | ||
timest TIMESTAMP, | ||
timestz TIMESTAMPTZ, | ||
timegap INTERVAL | ||
) | ||
`; | ||
await client.query(createTableQuery) | ||
console.log("Table created successfully.") | ||
} | ||
|
||
export async function dropTable(client) { | ||
let query = "drop table sample_table_nodejs;" | ||
await client.query(query) | ||
console.log("Table dropped successfully.") | ||
} | ||
|
||
export async function insertData(client, name, age, salary, tripIDs, birthdate, deci, fareData, starttime, timest, timestz, timegap) { | ||
let insertDateQuery = { | ||
text: ` | ||
INSERT INTO sample_table_nodejs (name, age, salary, trip_id, birthdate, deci, fare, starttime, timest, | ||
timestz, timegap) | ||
VALUES ($1, $2, $3, $4, $5, $6, ROW($7, $8, $9, $10), $11, $12, $13, $14); | ||
`, | ||
values: [name, age, salary, tripIDs, birthdate, deci, fareData.initial_charge, fareData.subsequent_charge, fareData.surcharge, fareData.tolls, starttime, timest, timestz, timegap] | ||
} | ||
await client.query(insertDateQuery) | ||
console.log("Data inserted successfully.") | ||
} | ||
|
||
export async function checkInsertedData(client, name, age, salary, tripIDs, birthdate, deci, fareData, starttime, timest, timestz, timegap) { | ||
await client.query("FLUSH;") | ||
|
||
let query = "SELECT name, age, salary, trip_id, birthdate, deci, fare, starttime, timest, timestz, timegap FROM sample_table_nodejs WHERE name=$1" | ||
let res = await client.query(query, [name]) | ||
let row = res.rows[0] | ||
|
||
let retrievedName = row.name | ||
assert.deepStrictEqual(retrievedName, name) | ||
|
||
let retrievedAge = row.age | ||
assert.deepStrictEqual(retrievedAge, age) | ||
|
||
let retrievedSalary = row.salary | ||
assert.deepStrictEqual(retrievedSalary, salary) | ||
|
||
let retrievedTripIDs = row.trip_id | ||
assert.deepStrictEqual(retrievedTripIDs, tripIDs) | ||
|
||
let retrievedBirthdate = row.birthdate | ||
assert.deepStrictEqual(retrievedBirthdate, birthdate) | ||
|
||
let retrievedDeci = row.deci | ||
assert.deepStrictEqual(retrievedDeci, deci) | ||
|
||
let retrievedFareData = row.fare | ||
|
||
let retrievedStarttime = row.starttime | ||
assert.deepStrictEqual(retrievedStarttime, starttime) | ||
|
||
let retrievedTimest = row.timest | ||
assert.deepStrictEqual(retrievedTimest, timest) | ||
|
||
let retrievedTimestz = row.timestz | ||
assert.deepStrictEqual(retrievedTimestz, timestz) | ||
|
||
let retrievedTimegap = row.timegap.toPostgres() | ||
assert.deepStrictEqual(retrievedTimegap, timegap) | ||
|
||
console.log("Data checked successfully.") | ||
} | ||
|
||
export async function updateSalaryData(client, name, salary) { | ||
let query = ` | ||
UPDATE sample_table_nodejs | ||
SET salary=$1 | ||
WHERE name = $2; | ||
` | ||
await client.query(query, [salary, name]) | ||
console.log("Data updated successfully.") | ||
} | ||
|
||
export async function deleteDataByName(client, name) { | ||
let query = ` | ||
DELETE | ||
FROM sample_table_nodejs | ||
WHERE name = $1; | ||
` | ||
await client.query(query, [name]) | ||
console.log("Data deleted successfully.") | ||
} |
Oops, something went wrong.